readme.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. MUSASHI
  2. =======
  3. Version 3.3
  4. A portable Motorola M680x0 processor emulation engine.
  5. Copyright 1998-2001 Karl Stenerud. All rights reserved.
  6. INTRODUCTION:
  7. ------------
  8. Musashi is a Motorola 68000, 68010, 68EC020, and 68020 emulator written in C.
  9. This emulator was written with two goals in mind: portability and speed.
  10. The emulator is written to ANSI C specifications with the exception that I use
  11. inline functions. This is not compliant to the ANSI spec, but will be
  12. compliant to the ANSI C9X spec.
  13. It has been successfully running in the MAME project (www.mame.net) for over 2
  14. years and so has had time to mature.
  15. LICENSE AND COPYRIGHT:
  16. ---------------------
  17. The Musashi M680x0 emulator is copyright 1998-2001 Karl Stenerud.
  18. The source code included in this archive is provided AS-IS, free for any
  19. non-commercial purpose.
  20. If you build a program using this core, please give credit to the author.
  21. If you wish to use this core in a commercial environment, please contact
  22. the author to discuss commercial licensing.
  23. AVAILABILITY:
  24. ------------
  25. The latest version of this code can be obtained at:
  26. http://kstenerud.cjb.net
  27. CONTACTING THE AUTHOR:
  28. ---------------------
  29. I can be reached at kstenerud@mame.net
  30. BASIC CONFIGURATION:
  31. -------------------
  32. The basic configuration will give you a standard 68000 that has sufficient
  33. functionality to work in a primitive environment.
  34. This setup assumes that you only have 1 device interrupting it, that the
  35. device will always request an autovectored interrupt, and it will always clear
  36. the interrupt before the interrupt service routine finishes (but could
  37. possibly re-assert the interrupt).
  38. You will have only one address space, no tracing, and no instruction prefetch.
  39. To implement the basic configuration:
  40. - Open m68kconf.h and verify that the settings for INLINE and DECL_SPEC will
  41. work with your compiler. (They are set for gcc)
  42. - In your host program, implement the following functions:
  43. unsigned int m68k_read_memory_8(unsigned int address);
  44. unsigned int m68k_read_memory_16(unsigned int address);
  45. unsigned int m68k_read_memory_32(unsigned int address);
  46. void m68k_write_memory_8(unsigned int address, unsigned int value);
  47. void m68k_write_memory_16(unsigned int address, unsigned int value);
  48. void m68k_write_memory_32(unsigned int address, unsigned int value);
  49. - In your host program, be sure to call m68k_pulse_reset() once before calling
  50. any of the other functions as this initializes the core.
  51. - Use m68k_execute() to execute instructions and m68k_set_irq() to cause an
  52. interrupt.
  53. ADDING PROPER INTERRUPT HANDLING:
  54. --------------------------------
  55. The interrupt handling in the basic configuration doesn't emulate the
  56. interrupt acknowledge phase of the CPU and automatically clears an interrupt
  57. request during interrupt processing.
  58. While this works for most systems, you may need more accurate interrupt
  59. handling.
  60. To add proper interrupt handling:
  61. - In m68kconf.h, set M68K_EMULATE_INT_ACK to OPT_SPECIFY_HANDLER
  62. - In m68kconf.h, set M68K_INT_ACK_CALLBACK(A) to your interrupt acknowledge
  63. routine
  64. - Your interrupt acknowledge routine must return an interrupt vector,
  65. M68K_INT_ACK_AUTOVECTOR, or M68K_INT_ACK_SPURIOUS. most m68k
  66. implementations just use autovectored interrupts.
  67. - When the interrupting device is satisfied, you must call m68k_set_irq(0) to
  68. remove the interrupt request.
  69. MULTIPLE INTERRUPTS:
  70. -------------------
  71. The above system will work if you have only one device interrupting the CPU,
  72. but if you have more than one device, you must do a bit more.
  73. To add multiple interrupts:
  74. - You must make an interrupt arbitration device that will take the highest
  75. priority interrupt and encode it onto the IRQ pins on the CPU.
  76. - The interrupt arbitration device should use m68k_set_irq() to set the
  77. highest pending interrupt, or 0 for no interrupts pending.
  78. SEPARATE IMMEDIATE AND PC-RELATIVE READS:
  79. ----------------------------------------
  80. You can write faster memory access functions if you know whether you are
  81. fetching from ROM or RAM. Immediate reads are always from the program space
  82. (Always in ROM unless it is running self-modifying code).
  83. This will also separate the pc-relative reads, since some systems treat
  84. PROGRAM mode reads and DATA mode reads differently (for program encryption,
  85. for instance). See the section below (ADDRESS SPACE) for an explanation of
  86. PROGRAM and DATA mode.
  87. To enable separate reads:
  88. - In m68kconf.h, turn on M68K_SEPARATE_READS.
  89. - In your host program, implement the following functions:
  90. unsigned int m68k_read_immediate_16(unsigned int address);
  91. unsigned int m68k_read_immediate_32(unsigned int address);
  92. unsigned int m68k_read_pcrelative_8(unsigned int address);
  93. unsigned int m68k_read_pcrelative_16(unsigned int address);
  94. unsigned int m68k_read_pcrelative_32(unsigned int address);
  95. - If you need to know the current PC (for banking and such), set
  96. M68K_MONITOR_PC to OPT_SPECIFY_HANDLER, and set M68K_SET_PC_CALLBACK(A) to
  97. your routine.
  98. ADDRESS SPACES:
  99. --------------
  100. Most systems will only implement one address space, placing ROM at the lower
  101. addresses and RAM at the higher. However, there is the possibility that a
  102. system will implement ROM and RAM in the same address range, but in different
  103. address spaces, or will have different mamory types that require different
  104. handling for the program and the data.
  105. The 68k accomodates this by allowing different program spaces, the most
  106. important to us being PROGRAM and DATA space. Here is a breakdown of
  107. how information is fetched:
  108. - All immediate reads are fetched from PROGRAM space.
  109. - All PC-relative reads are fetched from PROGRAM space.
  110. - The initial stack pointer and program counter are fetched from PROGRAM space.
  111. - All other reads (except for those from the moves instruction for 68020)
  112. are fetched from DATA space.
  113. The m68k deals with this by encoding the requested address space on the
  114. function code pins:
  115. FC
  116. Address Space 210
  117. ------------------ ---
  118. USER DATA 001
  119. USER PROGRAM 010
  120. SUPERVISOR DATA 101
  121. SUPERVISOR PROGRAM 110
  122. CPU SPACE 111 <-- not emulated in this core since we emulate
  123. interrupt acknowledge in another way.
  124. Problems arise here if you need to emulate this distinction (if, for example,
  125. your ROM and RAM are at the same address range, with RAM and ROM enable
  126. wired to the function code pins).
  127. There are 2 ways to deal with this situation using Musashi:
  128. 1. If you only need the distinction between PROGRAM and DATA (the most common),
  129. you can just separate the reads (see the preceeding section). This is the
  130. faster solution.
  131. 2. You can emulate the function code pins entirely.
  132. To emulate the function code pins:
  133. - In m68kconf.h, set M68K_EMULATE_FC to OPT_SPECIFY_HANDLER and set
  134. M68K_SET_FC_CALLBACK(A) to your function code handler function.
  135. - Your function code handler should select the proper address space for
  136. subsequent calls to m68k_read_xx (and m68k_write_xx for 68010+).
  137. Note: immediate reads are always done from program space, so technically you
  138. don't need to implement the separate immediate reads, although you could
  139. gain more speed improvements leaving them in and doing some clever
  140. programming.
  141. USING DIFFERENT CPU TYPES:
  142. -------------------------
  143. The default is to enable only the 68000 cpu type. To change this, change the
  144. settings for M68K_EMULATE_010 etc in m68kconf.h.
  145. To set the CPU type you want to use:
  146. - Make sure it is enabled in m68kconf.h. Current switches are:
  147. M68K_EMULATE_010
  148. M68K_EMULATE_EC020
  149. M68K_EMULATE_020
  150. - In your host program, call m68k_set_cpu_type() and then call
  151. m68k_pulse_reset(). Valid CPU types are:
  152. M68K_CPU_TYPE_68000,
  153. M68K_CPU_TYPE_68010,
  154. M68K_CPU_TYPE_68EC020,
  155. M68K_CPU_TYPE_68020
  156. CLOCK FREQUENCY:
  157. ---------------
  158. In order to emulate the correct clock frequency, you will have to calculate
  159. how long it takes the emulation to execute a certain number of "cycles" and
  160. vary your calls to m68k_execute() accordingly.
  161. As well, it is a good idea to take away the CPU's timeslice when it writes to
  162. a memory-mapped port in order to give the device it wrote to a chance to
  163. react.
  164. You can use the functions m68k_cycles_run(), m68k_cycles_remaining(),
  165. m68k_modify_timeslice(), and m68k_end_timeslice() to do this.
  166. Try to use large cycle values in your calls to m68k_execute() since it will
  167. increase throughput. You can always take away the timeslice later.
  168. MORE CORRECT EMULATION:
  169. ----------------------
  170. You may need to enable these in order to properly emulate some of the more
  171. obscure functions of the m68k:
  172. - M68K_EMULATE_BKPT_ACK causes the CPU to call a breakpoint handler on a BKPT
  173. instruction
  174. - M68K_EMULATE_TRACE causes the CPU to generate trace exceptions when the
  175. trace bits are set
  176. - M68K_EMULATE_RESET causes the CPU to call a reset handler on a RESET
  177. instruction.
  178. - M68K_EMULATE_PREFETCH emulates the 4-word instruction prefetch that is part
  179. of the 68000/68010 (needed for Amiga emulation).
  180. - call m68k_pulse_halt() to emulate the HALT pin.
  181. CONVENIENCE FUNCTIONS:
  182. ---------------------
  183. These are in here for programmer convenience:
  184. - M68K_INSTRUCTION_HOOK lets you call a handler before each instruction.
  185. - M68K_LOG_ENABLE and M68K_LOG_1010_1111 lets you log illegal and A/F-line
  186. instructions.
  187. MULTIPLE CPU EMULATION:
  188. ----------------------
  189. The default is to use only one CPU. To use more than one CPU in this core,
  190. there are some things to keep in mind:
  191. - To have different cpus call different functions, use OPT_ON instead of
  192. OPT_SPECIFY_HANDLER, and use the m68k_set_xxx_callback() functions to set
  193. your callback handlers on a per-cpu basis.
  194. - Be sure to call set_cpu_type() for each CPU you use.
  195. - Use m68k_set_context() and m68k_get_context() to switch to another CPU.
  196. LOAD AND SAVE CPU CONTEXTS FROM DISK:
  197. ------------------------------------
  198. You can use them68k_load_context() and m68k_save_context() functions to load
  199. and save the CPU state to disk.
  200. GET/SET INFORMATION FROM THE CPU:
  201. --------------------------------
  202. You can use m68k_get_reg() and m68k_set_reg() to gain access to the internals
  203. of the CPU.
  204. EXAMPLE:
  205. -------
  206. I have included a file example.zip that contains a full example.