home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.7z / ftp.whtech.com / emulators / v9t9 / linux / sources / V9t9 / source / memory.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-10-19  |  9.6 KB  |  324 lines

  1.  
  2. /*
  3.         MEMORY.H
  4.         ========
  5.  
  6.         99/4A-specific memory stuff
  7.         
  8.         INVARIANT:
  9.             memory[] has ALL addressable rom/ram,
  10.                 including DSR, module, etc.
  11. */
  12.  
  13. #ifndef __MEMORY_H__
  14. #define __MEMORY_H__
  15.  
  16. #include "16bit.h"
  17. #include "sysdeps.h"
  18. #include "command.h"
  19.  
  20. #include "centry.h"
  21.  
  22. struct mrstruct;
  23.  
  24. typedef u16 (*mrword)(const struct mrstruct *mr, const u32 addr);
  25. typedef s8 (*mrbyte)(const struct mrstruct *mr, u32 addr);
  26.  
  27. typedef void (*mwword)(const struct mrstruct *mr, u32 addr,u16 val);
  28. typedef void (*mwbyte)(const struct mrstruct *mr, u32 addr,s8 val);
  29.  
  30. typedef struct mrstruct
  31. {
  32.     u8        *areamemory;    /* actual memory for area, except for empty mem */
  33.     u8        *arearead;        /* if non-NULL, we can statically read */
  34.     u8        *areawrite;        /* if non-NULL, we can statically write */
  35.     mrword    read_word;        /* these routines are used before the */
  36.     mrbyte    read_byte;        /* memory maps when using the memory_xxx_xxx */
  37.     mwword    write_word;        /* functions, but the memory is used first */
  38.     mwbyte    write_byte;        /* when the MEMORY_XXX_XXX macros are used. */
  39. }    mrstruct;
  40.  
  41. #define AREA_IS_MEMORY_MAPPED(a) \
  42.         ((a)->read_word || (a)->read_byte || \
  43.         (a)->write_word || (a)->write_byte)
  44.  
  45. /*    This must remain 64K, even if mega-memory expansion
  46.     is emulated.  All the public routines expect to be passed
  47.     16-bit addresses. */
  48. #define PHYSMEMORYSIZE 65536
  49.  
  50. extern u8 zeroes[PHYSMEMORYSIZE];
  51.  
  52. /*    Size of an area of memory.  This is the minimum
  53.     size of memory for which each address therein is
  54.     expected to act "the same". */
  55. #define AREASIZE 1024
  56. #define AREASHIFT 10
  57.  
  58. #define NUMAREAS     (PHYSMEMORYSIZE >> AREASHIFT)
  59. #define NUMDOMAINS     4
  60.  
  61. typedef enum
  62. {
  63.     md_cpu,
  64.     md_graphics,
  65.     md_video,
  66.     md_speech
  67. }    mem_domain;
  68.  
  69. extern mrstruct     __areahandlers[NUMDOMAINS][NUMAREAS];
  70.  
  71. //    shorthand for CPU memory 
  72. #define SET_AREA_HANDLER(addr,size,handler) set_area_handler(md_cpu, addr,size,handler)
  73.  
  74. void    set_area_handler(mem_domain domain, u16 addr, u32 size, mrstruct *handler);
  75.  
  76. /*********************************************/
  77.  
  78. /*
  79.     These routines are global and available for use by any
  80.     other module.  Choose one of the two functions depending
  81.     on side effects.  For routines that are strictly for 
  82.     static reads or writes (for example, in a debugger), use
  83.     the macro versions.  For routines used during emulation,
  84.     use the function ones.
  85. */
  86.  
  87. //    more CPU shorthands
  88.  
  89. #define THE_AREA(dmn, addr) (&__areahandlers[dmn][((addr) & (PHYSMEMORYSIZE-1)) >> AREASHIFT])
  90.  
  91. #define AREA_SETUP(dmn, addr) \
  92.     mrstruct *area = THE_AREA(dmn, addr); \
  93.     my_assert((dmn) >= md_cpu && (dmn) < NUMDOMAINS &&(u32)area > addr) 
  94.  
  95. #define HAS_RAM_ACCESS(dmn, addr) \
  96.     (THE_AREA(dmn, addr)->arearead != NULL && \
  97.     THE_AREA(dmn, addr)->areawrite != NULL)
  98.  
  99. #define HAS_ROM_ACCESS(dmn, addr) \
  100.     (THE_AREA(dmn, addr)->arearead != NULL)
  101.  
  102. /*    These macros are for strict static access to memory.
  103.     No routines will be called, so there will be no side effects.
  104.     Unfortunately, though, a lot of memory looks like zeroes. */    
  105. #define AREA_READ_WORD(area, addr)     \
  106.     ((area)->arearead ? \
  107.         WORD((area)->arearead, (addr & (AREASIZE-1))) : 0)
  108. #define AREA_READ_BYTE(area, addr)     \
  109.     ((area)->arearead ? \
  110.         BYTE((area)->arearead, (addr & (AREASIZE-1))) : 0)
  111. #define AREA_WRITE_WORD(area, addr, val)     \
  112.     do { if ((area)->areawrite) \
  113.         WORD((area)->areawrite, (addr & (AREASIZE-1))) = val; } while (0)
  114. #define AREA_WRITE_BYTE(area, addr, val)     \
  115.     do { if ((area)->areawrite) \
  116.         BYTE((area)->areawrite, (addr & (AREASIZE-1))) = val; } while (0)
  117.  
  118. #define DOMAIN_READ_WORD(dmn, addr)  \
  119.         AREA_READ_WORD(THE_AREA(dmn,addr), addr)
  120. #define DOMAIN_READ_BYTE(dmn, addr)  \
  121.         AREA_READ_BYTE(THE_AREA(dmn,addr), addr)
  122. #define DOMAIN_WRITE_WORD(dmn, addr, val)  \
  123.         AREA_WRITE_WORD(THE_AREA(dmn, addr), addr, val)
  124. #define DOMAIN_WRITE_BYTE(dmn, addr, val)  \
  125.         AREA_WRITE_BYTE(THE_AREA(dmn, addr), addr, val)
  126.  
  127. #define MEMORY_READ_WORD(addr)     DOMAIN_READ_WORD(md_cpu, addr)
  128. #define MEMORY_READ_BYTE(addr)     DOMAIN_READ_BYTE(md_cpu, addr)
  129. #define MEMORY_WRITE_WORD(addr, val) DOMAIN_WRITE_WORD(md_cpu, addr, val)
  130. #define MEMORY_WRITE_BYTE(addr, val) DOMAIN_WRITE_BYTE(md_cpu, addr, val)
  131.  
  132. u16        domain_read_word(mem_domain md, u32 addr);
  133. void    domain_write_word(mem_domain md, u32 addr, u16 val);
  134. s8        domain_read_byte(mem_domain md, u32 addr);
  135. void    domain_write_byte(mem_domain md, u32 addr, s8 val);
  136.  
  137. INLINE    u16        memory_read_word(u32 addr) { 
  138.     return domain_read_word(md_cpu, addr); 
  139. }
  140. INLINE    void    memory_write_word(u32 addr, u16 val) {
  141.     domain_write_word(md_cpu, addr, val);
  142. }
  143. INLINE    s8        memory_read_byte(u32 addr) {
  144.     return domain_read_byte(md_cpu, addr);
  145. }
  146. INLINE    void    memory_write_byte(u32 addr, s8 val) {
  147.     domain_write_byte(md_cpu, addr, val);
  148. }
  149.  
  150.  
  151. //    Empty ROM memory handler
  152.  
  153. extern mrstruct    zero_memory_handler;
  154.  
  155. /******************************************/
  156.  
  157. //    32K memory expansion
  158.  
  159. extern int    isexpram;                /* is there expansion RAM? */
  160.  
  161. //    Alternate >8000->83FF mapping
  162.  
  163. extern int    isenhconsoleram;        /* is there extra console RAM >8000->82FF? */
  164.  
  165. extern void memory_init(void);
  166. extern void memory_ram_init(void);
  167.  
  168. /**********************************/
  169.  
  170. #if 0
  171. #define FLAT_MEMORY_PTR(dmn, addr)        \
  172.         (THE_AREA(dmn, addr)->areamemory ? \
  173.                 THE_AREA(dmn, addr)->areamemory + ((addr) & (AREASIZE-1)) : \
  174.                  zeroes)
  175. #else
  176. #define FLAT_MEMORY_PTR(dmn, addr)        \
  177.             (THE_AREA(dmn, addr)->areamemory + ((addr) & (AREASIZE-1)))
  178. #endif
  179.  
  180. #define registerptr(reg)    (u16 *)FLAT_MEMORY_PTR(md_cpu, (wp+reg+reg) & 0xffff)
  181.  
  182. /*
  183. INLINE u16 *registerptr(int reg) {
  184.     extern uaddr wp;
  185.     mrstruct *area = THE_AREA(md_cpu, wp+reg+reg);
  186.     return &WORD(area->areamemory, (wp+reg+reg) & (AREASIZE - 1));
  187. }
  188. */
  189.  
  190. /*
  191.     These enums and struct define a higher-level organization of
  192.     the memory map, used to allow radical customization of the
  193.     emulated computer's architecture.  
  194.  
  195.     A MemoryEntry bridges the gap between ROM, RAM, and nonvolatile
  196.     RAM.  By associating a primary purpose with an area of memory,
  197.     this allows us to transparently map ROMs from disk into an
  198.     area of memory as well as maintain on disk an image of an
  199.     area of RAM.  
  200.  
  201.     The mementlist can be used to find out what the contents of 
  202.     memory are without knowing about the specific routines referenced
  203.     in an mrstruct through the use of the flags.  
  204.  */
  205.  
  206. enum
  207. {
  208.     /* MEMENT_ROM = 0, */    /* entry is ROM */
  209.     MEMENT_RAM = 1,            /* entry is RAM */
  210.     MEMENT_STORED = 3,        /* RAM entry stored to disk */
  211.  
  212.     MEMENT_BANKING = 4+8,    /* mask for banking */
  213.     /* MEMENT_NOT_BANKED = 0, */
  214.     MEMENT_BANK_1 = 4,         /* bank #0 */
  215.     MEMENT_BANK_2 = 8,        /* bank #1 */
  216.  
  217.     MEMENT_CART = 16,        /* this entry is part of a module */
  218.  
  219.     MEMENT_DOMAIN_SHIFT = 5,
  220.     MEMENT_DOMAIN     = 7<<5,    /* mask for domain of memory */
  221.     MEMENT_CONSOLE     = md_cpu<<5,    /* console ROM/RAM */
  222.     MEMENT_GRAPHICS = md_graphics<<5,    /* GROM/GRAM */
  223.     MEMENT_VIDEO    = md_video<<5, /* video */
  224.     MEMENT_SPEECH    = md_speech<<5,    /* speech */
  225. //    MEMENT_DSR        = 4<<5, /* DSR ROM */
  226.  
  227.     MEMENT_USER        = 256    /* allocated via DefineMemory, destroy as needed 
  228.                              * (when new memory overlaps this), else caller
  229.                              * will destroy */
  230. };
  231.  
  232. typedef struct MemoryEntry {
  233.     int            flags;        /* memory flags MEMENT_xxx */
  234.  
  235.     u32               addr;        /* start address */
  236.     u32            offs;        /* offset of entry into original entry */
  237.     s32            size;        /* size in bytes, 0 = unknown, -xxx = magnitude of maximum */
  238.     u32            realsize;    /* size in bytes if loaded */
  239.     char        *name;        /* name of entry for debugging */
  240.     char        *filename;    /* file for loading/saving */
  241.     u32            fileoffs;    /* offset into file */
  242.  
  243.     mrstruct    memact;        /* how the memory acts, memory referenced must be malloc'ed */
  244.     struct MemoryEntry *next; /* next entry in database */
  245. } MemoryEntry;
  246.  
  247. extern    MemoryEntry *mementlist;  /* active memory list */
  248. extern    char *modulespath, *romspath, *ramspath;
  249. extern    char *systemmodulespath, *systemromspath, *systemramspath;
  250.  
  251. /*    Handlers for bank-switched module */
  252. extern mrstruct *memory_module_bank_handlers[2];
  253. extern int memory_module_bank;
  254. void
  255. memory_set_module_bank(u8 bank);
  256.  
  257. /*    Free memory map */
  258. void     memory_free_list(void);
  259.  
  260. /*    Initialize memory map */
  261. int        memory_init_list(void);
  262.  
  263. /*  Create a new memory entry 
  264.  
  265.     flags:     bitmask of MEMENT_xxx
  266.     addr:      address of new memory
  267.     size:    size of memory in bytes, 
  268.                 or if negative, the magnitude is the maximum size
  269.     name:    user name for memory segment
  270.     filename:  location of ROM or RAM on disk (MEMENT_ROM/MEMENT_STORED)
  271.     fileoffs:  offset into file where memory is stored
  272.     memact: actions on memory access to area
  273. */
  274. MemoryEntry *memory_new_entry(int flags, u32 addr, s32 size,
  275.                               char *name, char *filename, u32 fileoffs,
  276.                               mrstruct *memact);
  277.  
  278. /*    Destroy entry (free memory) */
  279. void    memory_destroy_entry(MemoryEntry *ent);
  280.  
  281. /*    Save a volatile entry in the memory map to disk */
  282. int        memory_save_entry(MemoryEntry *ent);
  283.  
  284. /*    Load a volatile entry in the memory map from disk */
  285. int        memory_load_entry(MemoryEntry *ent);
  286.  
  287. /*    Add entry to memory list and destroy entries this overrides */
  288. int        memory_add_entry_to_list(MemoryEntry *ent);
  289.  
  290. /*    Remove entry from memory list, return ent->next */
  291. MemoryEntry *
  292. memory_remove_entry_from_list(MemoryEntry *prev, MemoryEntry *ent);
  293.  
  294. /*    Map an entry into the memory map */
  295. int        memory_map_entry(MemoryEntry *ent);
  296.  
  297. /*    Unmap an entry from the memory map */
  298. void    memory_unmap_entry(MemoryEntry *ent);
  299.  
  300. /*    Save volatile memory */
  301. void    memory_volatile_save(void);
  302.  
  303. /*    Load volatile memory  */
  304. int        memory_volatile_load(void);
  305.  
  306. /*    Load all memory  */
  307. int        memory_complete_load(void);
  308.  
  309. /*    Do it all */
  310. int        memory_insert_new_entry(int flags, u32 addr, s32 size,
  311.                                 char *name, char *filename, u32 fileoffs,
  312.                                 mrstruct *memact);
  313.  
  314. /*    DefineMemory */
  315. DECL_SYMBOL_ACTION(memory_define_entry);
  316. /*    ListMemory */
  317. DECL_SYMBOL_ACTION(memory_dump);
  318. /*    DefaultMemoryMap */
  319. DECL_SYMBOL_ACTION(memory_default_list);
  320.  
  321. #include "cexit.h"
  322.  
  323. #endif
  324.