home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / misc / emu / z80 / z80.i < prev    next >
Text File  |  1993-12-21  |  10KB  |  247 lines

  1.     IFND Z80_INCLUDE
  2. Z80_INCLUDE SET 1
  3. ** General Z80 emulator definitions and declarations.
  4. ** The control structure is defined in Z80_struct.i
  5.  
  6. ** =====================================================================
  7.  
  8.     IFD VERBOSE
  9.     LIST
  10. ** Compiling the Z80.i file.
  11.     NOLIST
  12.     ENDC
  13.  
  14. ** All sizes and offsets are in bytes:
  15.  
  16.     ;The amount of bytes needed for the Z80 memory space
  17. Z80_MEMSIZE    EQU    $10000
  18.  
  19. Z80_LBUFSIZE    EQU    16    ;Uninteresting to the normal user
  20. Z80_HBUFSIZE    EQU    16
  21. Z80_0_OFFSET     EQU    $8000
  22. CACHE_0_OFFSET    EQU    Z80_LBUFSIZE+(2*Z80_0_OFFSET)
  23. FLAGS_0_OFFSET    EQU    Z80_0_OFFSET
  24.  
  25.     ;The size of the memory used for the cache.
  26.     ;It must be word-aligned!
  27. Z80_CACHESIZE    EQU    (2*Z80_MEMSIZE)+Z80_LBUFSIZE+Z80_HBUFSIZE
  28.  
  29.     ;The space needed for memory control flags
  30. Z80_FLAGMEMSIZE    EQU    Z80_MEMSIZE
  31.  
  32. ** Memory write access control flags:
  33.  
  34.     ;Memory flag values (signed byte):
  35.     ;    -1 to -128    read-only
  36.     ;    0        ok to write (no detection)
  37.     ;    1 to 16     access counters 1 to 16
  38.     ;    17 to 63    reserved
  39.     ;    64 to 127    user exception
  40.  
  41. ** Call Z80_SetMemFlag (described below) to set these flags.
  42. ** A zero flag marks ordinary RAM, and no detection is made.
  43. **   A negative flag (use Z80_MEM_ROM) marks read-only memory. Attempts
  44. ** to write are ignored.
  45. **   A value in the range Z80_MEM_CNT to Z80_MEM_CNT+Z80_MEM_CNTNUM-1
  46. ** corresponds to write a access counter from 0 to Z80_MEM_CNTNUM-1.
  47. ** On each write access, the corresponding counter in the Z80_AccessCnt
  48. ** array is incremented by one. If the corresponding flag in the
  49. ** Z80_CntType array is zero, the value is written, otherwise the attempt
  50. ** to write is ignored.
  51. **   A value in the range Z80_MEM_USR to Z80_MEM_USR+Z80_MEM_USRNUM-1
  52. ** corresponds to a user memory exception from 0 to Z80_MEM_USRNUM-1.
  53. ** If the Z80_MemHandler pointer is nonzero (non-NULL), the routine it
  54. ** points to is called with the following parameters:
  55. **   d1 contains the exception number (word).
  56. **   d2 contains the value (byte).
  57. **   a1 contains the Z80 address (word).
  58. **   a2 is scratch (address of user-def routine).
  59. ** Changes to a1 and a2 have no effect. The handler must protect any other
  60. ** registers it uses, and return the following values:
  61. **   d1 zero (longword) if value should be written, nonzero if not.
  62. **   d2 countains the value (byte).
  63. ** The memory handler call is mostly intended for more complex cases of
  64. ** write access detection than the simple counters can handle. Not many
  65. ** details can be found out about the current Cpu status from the memory
  66. ** handler, since it is called in mid-execution of an instruction, and only
  67. ** the base pointer registers TableB, Z0, CacheB and FlagsB can be trusted
  68. ** to have correct values. For a list of register aliases, see the file
  69. ** Z80_coding.i.
  70.  
  71. ** Example in C:
  72. **    Z80_SetMemFlag(Control, $4000, $1B00, Z80_MEM_CNT+5);
  73. ** flags the area from $4000 to $5AFF as access counter 5.
  74. **    Z80_CntType[5] = 1;
  75. ** sets the type of counter 5 to 'do not write'.
  76. ** (Remember: the range is from 0 to Z80_MEM_CNTNUM - 1.)
  77.  
  78.     ;(These values are hard-coded in the detection routine.)
  79. Z80_MEM_ROM    EQU    -1
  80. Z80_MEM_CNT    EQU    1
  81. Z80_MEM_CNTNUM    EQU    16
  82. Z80_MEM_USR    EQU    64
  83. Z80_MEM_USRNUM    EQU    128-Z80_MEM_USR
  84.  
  85.  
  86. ** ------------
  87. ** Cross-references:
  88.  
  89.     ;Depending on whether Z80_MAIN is defined or not, this
  90.     ;defines a macro to either XREF or XDEF a program label.
  91.  
  92.     IFD    Z80_MAIN    ;XDEF in all cases if compiling emulator
  93. Z80_PUB   MACRO ;label
  94.         XDEF \1
  95.       ENDM
  96.     ELSE
  97. Z80_PUB   MACRO ;label        ;otherwise XREF program label
  98.         XREF \1
  99.       ENDM
  100.     ENDC ;IFD Z80_MAIN
  101.  
  102. ** Function labels
  103.  
  104.     Z80_PUB Z80_Init
  105.  
  106. ** Before beginning emulation, the control structure must be initialised
  107. ** by calling Z80_Init.
  108. **   a0 must point to the control structure. The fields Z80_Memory and
  109. **      Z80_Cachemem must be pointing to allocated memory.
  110. **      If the memory write access checking feature is used, the field
  111. **      Z80_Flagmem must also be set, and if Z80_MemHandler is nonzero
  112. **      it is assumed to point to a user memory exception handler. See
  113. **      MemoryHandler below for details.
  114. **      The user environment data area is not changed. All other fields
  115. **      are automatically initialised. The Cpu Status fields are set up
  116. **      as after a reset.
  117. **   The return value in d0 is nonzero if an error occurred, and zero
  118. ** otherwise. All other registers are automatically protected.
  119.  
  120. ** -------------------------------------------------------------------
  121.  
  122.     Z80_PUB Z80_Coldstart
  123.  
  124. ** Start emulation 'from scratch', with a CPU reset.
  125. **   a0 must point to the control structure, which must be initialised
  126. ** (see Z80_Init above).
  127. **   The return value in d0 is nonzero if an error occurred, and zero
  128. ** otherwise. All other registers are automatically protected.
  129.  
  130. ** -------------------------------------------------------------------
  131.  
  132.     Z80_PUB Z80_Continue
  133.  
  134. ** Continue with the as if nothing happened since last exit (unless the
  135. ** saved processor status has been manipulated).
  136. **   Changes to other fields than the CPU status structure entries
  137. ** are not recommended; calling Z80_Continue does not guarantee
  138. ** that such changes have any effect (see Z80_NewSettings).
  139. **   a0 must point to the control structure.
  140. **   The return value in d0 is the same as for Z80_Coldstart. All other
  141. ** registers are automatically protected.
  142.  
  143. ** ----------------------------------------------------------------------
  144.  
  145. ** About reallocating memory:
  146. **
  147. ** Call Z80_Exitreq and make sure the emulator has stopped before
  148. ** reallocating memory (remember to copy the old memory contents to the
  149. ** new areas), then update the corresponding entries in the control
  150. ** structure (Z80_Memory, Z80_Cachemem and Z80_Flagmem). Calling
  151. ** Z80_NewSettings will then make sure that Z80_Continue will resume
  152. ** the emulation from the new addresses.
  153. **   The control structure can be moved whenever the emulator is not
  154. ** running, without doing anything other than passing the new address
  155. ** to Z80_Continue.
  156.  
  157.  
  158.     Z80_PUB Z80_NewSettings
  159.  
  160. ** Makes the emulator update the private fields of the control structure
  161. ** when changes to any of the public fields have taken place, for instance
  162. ** if the Z80_Memory field is changed. Do not call this routine while the
  163. ** emulator is running.
  164. **   a0 must point to the control structure.
  165. **   The return value in d0 is nonzero if an error occurred, and zero
  166. ** otherwise.
  167.  
  168. ** -------------------------------------------------------------------
  169.  
  170.     Z80_PUB Z80_SetByte    ;(a0,d0,d1)
  171.     Z80_PUB Z80_SetWordLH    ;(a0,d0,d1)
  172.     Z80_PUB Z80_GetByte    ;(a0,d0)
  173.     Z80_PUB Z80_GetWordLH    ;(a0,d0)
  174.     Z80_PUB Z80_SetBlock    ;(a0,d0,d1,d2)
  175.     Z80_PUB Z80_ReadBlock    ;(a0,a1,d0,d1)
  176.     Z80_PUB Z80_WriteBlock    ;(a0,a1,d0,d1)
  177.  
  178. ** Routines to access the Z80 address space. They mirror any changes
  179. ** in the cache and handle the wraparound of addresses transparently.
  180. ** I recommend that they are used rathed than writing directly into
  181. ** the Z80 address space.
  182. **   All need a pointer to the control structure in a0. The Z80 address
  183. ** (word-sized) is passed in d0 (for block functions this is the
  184. ** block start address).
  185. **   Z80_SetByte is passed a byte value to be written in d1. It returns
  186. ** nothing.
  187. **   Z80_SetWordLH is passed a (high-end first) word value to be written
  188. ** in d1 and writes it low-end first. It returns nothing.
  189. **   Z80_GetByte returns the byte value in d0.
  190. **   Z80_GetWordLH reads a (low-end first) word value and returns it
  191. ** high-end first in d0.
  192. **   All block functions are passed the block size (an unsigned longword)
  193. ** in d1. They return nothing.
  194. **   Z80_SetBlock is passed the byte value to be written in d2.
  195. **   Z80_ReadBlock and Z80_WriteBlock are passed the buffer address
  196. ** (in the 680x0 address space) in a1.
  197. **   All Z80 address arithmetic is word-sized. For instance, calling
  198. ** Z80_SetWordLH(ctrl, $ffff, $1234) will set address $ffff to $34 and
  199. ** address $0000 to $12. The block functions will also wrap at $ffff.
  200.  
  201. ** -------------------------------------------------------------------
  202.  
  203.     Z80_PUB Z80_SetMemFlag    ;(a0,d0,d1,d2)
  204.  
  205. ** Sets memory flags. Has no effect if the Z80_Flagmem field in the
  206. ** control structure is zero.
  207. **   a0 must point to the control structure. The Z80 address (word) is
  208. ** passed in d0