6 A portable Motorola M680x0 processor emulation engine.
7 Copyright 1998-2002 Karl Stenerud. All rights reserved.
14 Musashi is a Motorola 68000, 68010, 68EC020, and 68020 emulator written in C.
15 This emulator was written with two goals in mind: portability and speed.
17 The emulator is written to ANSI C89 specifications. It also uses inline
18 functions, which are C9X compliant.
20 It has been successfully running in the MAME project (www.mame.net) for years
21 and so has had time to mature.
25 LICENSE AND COPYRIGHT:
28 Copyright © 1998-2001 Karl Stenerud
30 Permission is hereby granted, free of charge, to any person obtaining a copy
31 of this software and associated documentation files (the "Software"), to deal
32 in the Software without restriction, including without limitation the rights
33 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
34 copies of the Software, and to permit persons to whom the Software is
35 furnished to do so, subject to the following conditions:
37 The above copyright notice and this permission notice shall be included in
38 all copies or substantial portions of the Software.
40 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
43 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
45 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
52 The latest version of this code can be obtained at:
53 https://github.com/kstenerud/Musashi
57 CONTACTING THE AUTHOR:
59 I can be reached at kstenerud@gmail.com
65 The basic configuration will give you a standard 68000 that has sufficient
66 functionality to work in a primitive environment.
68 This setup assumes that you only have 1 device interrupting it, that the
69 device will always request an autovectored interrupt, and it will always clear
70 the interrupt before the interrupt service routine finishes (but could
71 possibly re-assert the interrupt).
72 You will have only one address space, no tracing, and no instruction prefetch.
74 To implement the basic configuration:
76 - Open m68kconf.h and verify that the settings for INLINE will work with your
77 compiler. (Currently set to "static __inline__", which works in gcc 2.9.
78 For C9X compliance, it should be "inline")
80 - In your host program, implement the following functions:
81 unsigned int m68k_read_memory_8(unsigned int address);
82 unsigned int m68k_read_memory_16(unsigned int address);
83 unsigned int m68k_read_memory_32(unsigned int address);
84 void m68k_write_memory_8(unsigned int address, unsigned int value);
85 void m68k_write_memory_16(unsigned int address, unsigned int value);
86 void m68k_write_memory_32(unsigned int address, unsigned int value);
88 - In your host program, be sure to call m68k_pulse_reset() once before calling
89 any of the other functions as this initializes the core.
91 - Use m68k_execute() to execute instructions and m68k_set_irq() to cause an
96 ADDING PROPER INTERRUPT HANDLING:
97 --------------------------------
98 The interrupt handling in the basic configuration doesn't emulate the
99 interrupt acknowledge phase of the CPU and automatically clears an interrupt
100 request during interrupt processing.
101 While this works for most systems, you may need more accurate interrupt
104 To add proper interrupt handling:
106 - In m68kconf.h, set M68K_EMULATE_INT_ACK to OPT_SPECIFY_HANDLER
108 - In m68kconf.h, set M68K_INT_ACK_CALLBACK(A) to your interrupt acknowledge
111 - Your interrupt acknowledge routine must return an interrupt vector,
112 M68K_INT_ACK_AUTOVECTOR, or M68K_INT_ACK_SPURIOUS. most m68k
113 implementations just use autovectored interrupts.
115 - When the interrupting device is satisfied, you must call m68k_set_irq(0) to
116 remove the interrupt request.
122 The above system will work if you have only one device interrupting the CPU,
123 but if you have more than one device, you must do a bit more.
125 To add multiple interrupts:
127 - You must make an interrupt arbitration device that will take the highest
128 priority interrupt and encode it onto the IRQ pins on the CPU.
130 - The interrupt arbitration device should use m68k_set_irq() to set the
131 highest pending interrupt, or 0 for no interrupts pending.
135 SEPARATE IMMEDIATE READS:
136 ------------------------
137 You can write faster memory access functions if you know whether you are
138 fetching from ROM or RAM. Immediate reads are always from the program space
139 (Always in ROM unless it is running self-modifying code).
141 To enable separate immediate reads:
143 - In m68kconf.h, turn on M68K_SEPARATE_READ_IMM.
145 - In your host program, implement the following functions:
146 unsigned int m68k_read_immediate_16(unsigned int address);
147 unsigned int m68k_read_immediate_32(unsigned int address);
149 - If you need to know the current PC (for banking and such), set
150 M68K_MONITOR_PC to OPT_SPECIFY_HANDLER, and set M68K_SET_PC_CALLBACK(A) to
157 Most systems will only implement one address space, placing ROM at the lower
158 addresses and RAM at the higher. However, there is the possibility that a
159 system will implement ROM and RAM in the same address range, but in different
162 In this case, you might get away with assuming that immediate reads are in the
163 program space and all other reads are in the data space, if it weren't for the
164 fact that the exception vectors are fetched from the data space. As a result,
165 anyone implementing this kind of system will have to copy the vector table
166 from ROM to RAM using pc-relative instructions.
168 This makes things bad for emulation, because this means that a non-immediate
169 read is not necessarily in the data space.
170 The m68k deals with this by encoding the requested address space on the
175 ------------------ ---
179 SUPERVISOR PROGRAM 110
180 CPU SPACE 111 <-- not emulated in this core since we emulate
181 interrupt acknowledge in another way.
183 To emulate the function code pins:
185 - In m68kconf.h, set M68K_EMULATE_FC to OPT_SPECIFY_HANDLER and set
186 M68K_SET_FC_CALLBACK(A) to your function code handler function.
188 - Your function code handler should select the proper address space for
189 subsequent calls to m68k_read_xx (and m68k_write_xx for 68010+).
191 Note: immediate reads are always done from program space, so technically you
192 don't need to implement the separate immediate reads, although you could
193 gain more speed improvements leaving them in and doing some clever
198 USING DIFFERENT CPU TYPES:
199 -------------------------
200 The default is to enable only the 68000 cpu type. To change this, change the
201 settings for M68K_EMULATE_010 etc in m68kconf.h.
203 To set the CPU type you want to use:
205 - Make sure it is enabled in m68kconf.h. Current switches are:
210 - In your host program, call m68k_set_cpu_type() and then call
211 m68k_pulse_reset(). Valid CPU types are:
214 M68K_CPU_TYPE_68EC020,
221 In order to emulate the correct clock frequency, you will have to calculate
222 how long it takes the emulation to execute a certain number of "cycles" and
223 vary your calls to m68k_execute() accordingly.
224 As well, it is a good idea to take away the CPU's timeslice when it writes to
225 a memory-mapped port in order to give the device it wrote to a chance to
228 You can use the functions m68k_cycles_run(), m68k_cycles_remaining(),
229 m68k_modify_timeslice(), and m68k_end_timeslice() to do this.
230 Try to use large cycle values in your calls to m68k_execute() since it will
231 increase throughput. You can always take away the timeslice later.
235 MORE CORRECT EMULATION:
236 ----------------------
237 You may need to enable these in order to properly emulate some of the more
238 obscure functions of the m68k:
240 - M68K_EMULATE_BKPT_ACK causes the CPU to call a breakpoint handler on a BKPT
243 - M68K_EMULATE_TRACE causes the CPU to generate trace exceptions when the
246 - M68K_EMULATE_RESET causes the CPU to call a reset handler on a RESET
249 - M68K_EMULATE_PREFETCH emulates the 4-word instruction prefetch that is part
250 of the 68000/68010 (needed for Amiga emulation).
251 NOTE: if the CPU fetches a word or longword at an odd address when this
252 option is on, it will yield unpredictable results, which is why a real
253 68000 will generate an address error exception.
255 - M68K_EMULATE_ADDRESS_ERROR will cause the CPU to generate address error
256 exceptions if it attempts to read a word or longword at an odd address.
258 - call m68k_pulse_halt() to emulate the HALT pin.
262 CONVENIENCE FUNCTIONS:
263 ---------------------
264 These are in here for programmer convenience:
266 - M68K_INSTRUCTION_HOOK lets you call a handler before each instruction.
268 - M68K_LOG_ENABLE and M68K_LOG_1010_1111 lets you log illegal and A/F-line
273 MULTIPLE CPU EMULATION:
274 ----------------------
275 The default is to use only one CPU. To use more than one CPU in this core,
276 there are some things to keep in mind:
278 - To have different cpus call different functions, use OPT_ON instead of
279 OPT_SPECIFY_HANDLER, and use the m68k_set_xxx_callback() functions to set
280 your callback handlers on a per-cpu basis.
282 - Be sure to call set_cpu_type() for each CPU you use.
284 - Use m68k_set_context() and m68k_get_context() to switch to another CPU.
288 LOAD AND SAVE CPU CONTEXTS FROM DISK:
289 ------------------------------------
290 You can use them68k_load_context() and m68k_save_context() functions to load
291 and save the CPU state to disk.
295 GET/SET INFORMATION FROM THE CPU:
296 --------------------------------
297 You can use m68k_get_reg() and m68k_set_reg() to gain access to the internals
305 The subdir example contains a full example (currently DOS only).