Search
lxdream.org :: lxdream/src/mmio.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/mmio.h
changeset 31:495e480360d7
prev10:c898b37506e0
next55:96323c198da3
author nkeynes
date Sun Dec 25 08:24:11 2005 +0000 (14 years ago)
permissions -rw-r--r--
last change Finish adding header blocks to all files
view annotate diff log raw
     1 /**
     2  * $Id: mmio.h,v 1.2 2005-12-25 08:24:07 nkeynes Exp $
     3  *
     4  * mmio.h defines a complicated batch of macros used to build up the 
     5  * memory-mapped I/O regions in a reasonably readable fashion.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    19 #ifndef dream_mmio_H
    20 #define dream_mmio_H 1
    22 #ifdef __cplusplus
    23 extern "C" {
    24 #if 0
    25 }
    26 #endif
    27 #endif
    29 #include <stdint.h>
    30 #include <stdlib.h>
    31 #include "sh4core.h"
    33 #define PAGE_TABLE_ENTRIES 128*1024
    34 #define PAGE_SIZE 4096
    35 #define PAGE_BITS 12
    37 #define PORT_R 1
    38 #define PORT_W 2
    39 #define PORT_MEM 4 /* store written value */
    40 #define PORT_RW 3
    41 #define PORT_MR 5
    42 #define PORT_MRW 7
    43 #define UNDEFINED 0xDEADBEEF /* This has to be a value that nothing inits to */
    45 struct mmio_region {
    46     char *id, *desc;
    47     uint32_t base;
    48     int32_t (*io_read)(uint32_t addr);
    49     void (*io_write)(uint32_t addr, uint32_t val);
    50     char *mem;
    51     char *save_mem; /* Used to compare for gui updates */
    52     struct mmio_port {
    53         char *id, *desc;
    54         int width;
    55         uint32_t offset;
    56         uint32_t def_val;
    57         int flags;
    58         uint32_t *val;
    59     } ports[80];
    60     struct mmio_port **index; /* reverse lookup by address */
    61     int trace_flag; /* set to 1 to enable transfer traces */
    62 };
    64 void register_io_region( struct mmio_region *mmio );
    65 void register_io_regions( struct mmio_region **mmiolist );
    67 extern struct mmio_region *io_rgn[];
    68 extern int num_io_rgns;
    70 #define MMIO_READ( id, r ) *((int32_t *)(mmio_region_##id.mem + (r)))
    71 #define MMIO_WRITE( id, r, v ) *((int32_t *)(mmio_region_##id.mem + (r))) = (v)
    72 #define MMIO_REG( id, r ) ((int32_t *)(mmio_region_##id.mem + (r)))
    73 #define MMIO_REGID( mid, r ) (mmio_region_##mid.index[(r)>>2] != NULL ? \
    74             mmio_region_##mid.index[(r)>>2]->id : "<UNDEF>" )
    75 #define MMIO_REGDESC( mid, r) (mmio_region_##mid.index[(r)>>2] != NULL ? \
    76             mmio_region_##mid.index[(r)>>2]->desc : "Undefined register" )
    77 #define MMIO_TRACE( mid ) mmio_region_##mid.trace_flag = 1
    78 #define MMIO_NOTRACE( mid ) mmio_region_##mid.trace_flag = 0
    80 #define MMIO_REGID_BYNUM( mid, r ) (io_rgn[mid]->index[(r)>>2] != NULL ? \
    81             io_rgn[mid]->index[(r)>>2]->id : "<UNDEF>" )
    82 #define MMIO_REGDESC_BYNUM( mid, r ) (io_rgn[mid]->index[(r)>>2] != NULL ? \
    83             io_rgn[mid]->index[(r)>>2]->desc : "Undefined register" )
    84 #define MMIO_NAME_BYNUM( mid ) (io_rgn[mid]->id)
    86 #ifdef __cplusplus
    87 }
    88 #endif
    90 #endif
    92 #ifdef MMIO_IMPL
    94 #ifndef MMIO_IMPL_INCLUDED
    95 #define MMIO_IMPL_INCLUDED
    96 #undef MMIO_REGION_BEGIN
    97 #undef LONG_PORT
    98 #undef WORD_PORT
    99 #undef BYTE_PORT
   100 #undef MMIO_REGION_END
   101 #undef MMIO_REGION_LIST_BEGIN
   102 #undef MMIO_REGION
   103 #undef MMIO_REGION_LIST_END
   104 #define MMIO_REGION_BEGIN(b,id,d) struct mmio_region mmio_region_##id = { #id, d, b, mmio_region_##id##_read, mmio_region_##id##_write, 0, 0, {
   105 #define LONG_PORT( o,id,f,def,d ) { #id, d, 32, o, def, f },
   106 #define WORD_PORT( o,id,f,def,d ) { #id, d, 16, o, def, f },
   107 #define BYTE_PORT( o,id,f,def,d ) { #id, d, 8, o, def, f },
   108 #define MMIO_REGION_END {NULL, NULL, 0, 0, 0, 0} } };
   109 #define MMIO_REGION_LIST_BEGIN(id) struct mmio_region *mmio_list_##id[] = {
   110 #define MMIO_REGION( id ) &mmio_region_##id,
   111 #define MMIO_REGION_LIST_END NULL};
   113 /* Stub defines for modules we haven't got to yet, or ones which don't
   114  * actually need any direct code on read and/or write
   115  */
   116 #define MMIO_REGION_READ_STUBFN( id ) \
   117 int32_t mmio_region_##id##_read( uint32_t reg ) { \
   118     int32_t val = MMIO_READ( id, reg ); \
   119     WARN( "Read from unimplemented module %s (%03X => %08X) [%s: %s]",\
   120           #id, reg, val, MMIO_REGID(id,reg), MMIO_REGDESC(id,reg) ); \
   121     return val; \
   122 }
   123 #define MMIO_REGION_WRITE_STUBFN( id ) \
   124 void mmio_region_##id##_write( uint32_t reg, uint32_t val ) { \
   125     WARN( "Write to unimplemented module %s (%03X <= %08X) [%s: %s]", \
   126           #id, reg, val, MMIO_REGID(id,reg), MMIO_REGDESC(id,reg) ); \
   127     MMIO_WRITE( id, reg, val ); \
   128 }
   129 #define MMIO_REGION_STUBFNS( id ) \
   130     MMIO_REGION_READ_STUBFN( id ) \
   131     MMIO_REGION_WRITE_STUBFN( id )
   132 #define MMIO_REGION_READ_DEFFN( id ) \
   133 int32_t mmio_region_##id##_read( uint32_t reg ) { \
   134     return MMIO_READ( id, reg ); \
   135 }
   136 #define MMIO_REGION_WRITE_DEFFN( id ) \
   137 void mmio_region_##id##_write( uint32_t reg, uint32_t val ) { \
   138     MMIO_WRITE( id, reg, val ); \
   139 }
   140 #define MMIO_REGION_DEFFNS( id ) \
   141     MMIO_REGION_READ_DEFFN( id ) \
   142     MMIO_REGION_WRITE_DEFFN( id )
   143 #endif
   145 #define MMIO_REGION_WRITE_FN( id, reg, val ) \
   146 void mmio_region_##id##_write( uint32_t reg, uint32_t val )
   148 #define MMIO_REGION_READ_FN( id, reg ) \
   149 int32_t mmio_region_##id##_read( uint32_t reg )
   151 #else
   153 #ifndef MMIO_IFACE_INCLUDED
   154 #define MMIO_IFACE_INCLUDED
   155 #define MMIO_REGION_BEGIN(b,id,d) \
   156 extern struct mmio_region mmio_region_##id; \
   157 int32_t mmio_region_##id##_read(uint32_t); \
   158 void mmio_region_##id##_write(uint32_t, uint32_t); \
   159 enum mmio_region_##id##_port_t {
   160 #define LONG_PORT( o,id,f,def,d ) id = o,
   161 #define WORD_PORT( o,id,f,def,d ) id = o,
   162 #define BYTE_PORT( o,id,f,def,d ) id = o,
   163 #define MMIO_REGION_END };
   164 #define MMIO_REGION_LIST_BEGIN(id) extern struct mmio_region *mmio_list_##id[];
   165 #define MMIO_REGION( id )
   166 #define MMIO_REGION_LIST_END
   167 #endif
   169 #endif
.