1 /* ======================================================================== */
2 /* ========================= LICENSING & COPYRIGHT ======================== */
3 /* ======================================================================== */
8 * A portable Motorola M680x0 processor emulation engine.
9 * Copyright Karl Stenerud. All rights reserved.
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 #define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0]))
46 /* ======================================================================== */
47 /* ============================= CONFIGURATION ============================ */
48 /* ======================================================================== */
50 /* Import the configuration for this build */
57 /* ======================================================================== */
58 /* ============================ GENERAL DEFINES =========================== */
60 /* ======================================================================== */
62 /* There are 7 levels of interrupt to the 68K.
63 * A transition from < 7 to 7 will cause a non-maskable interrupt (NMI).
65 #define M68K_IRQ_NONE 0
75 /* Special interrupt acknowledge values.
76 * Use these as special returns from the interrupt acknowledge callback
77 * (specified later in this header).
80 /* Causes an interrupt autovector (0x18 + interrupt level) to be taken.
81 * This happens in a real 68K if VPA or AVEC is asserted during an interrupt
82 * acknowledge cycle instead of DTACK.
84 #define M68K_INT_ACK_AUTOVECTOR 0xffffffff
86 /* Causes the spurious interrupt vector (0x18) to be taken
87 * This happens in a real 68K if BERR is asserted during the interrupt
88 * acknowledge cycle (i.e. no devices responded to the acknowledge).
90 #define M68K_INT_ACK_SPURIOUS 0xfffffffe
93 /* CPU types for use in m68k_set_cpu_type() */
96 M68K_CPU_TYPE_INVALID,
99 M68K_CPU_TYPE_68EC020,
101 M68K_CPU_TYPE_68EC030,
103 M68K_CPU_TYPE_68EC040,
104 M68K_CPU_TYPE_68LC040,
106 M68K_CPU_TYPE_SCC68070
109 /* Registers used by m68k_get_reg() and m68k_set_reg() */
113 M68K_REG_D0, /* Data registers */
121 M68K_REG_A0, /* Address registers */
129 M68K_REG_PC, /* Program Counter */
130 M68K_REG_SR, /* Status Register */
131 M68K_REG_SP, /* The current Stack Pointer (located in A7) */
132 M68K_REG_USP, /* User Stack Pointer */
133 M68K_REG_ISP, /* Interrupt Stack Pointer */
134 M68K_REG_MSP, /* Master Stack Pointer */
135 M68K_REG_SFC, /* Source Function Code */
136 M68K_REG_DFC, /* Destination Function Code */
137 M68K_REG_VBR, /* Vector Base Register */
138 M68K_REG_CACR, /* Cache Control Register */
139 M68K_REG_CAAR, /* Cache Address Register */
141 /* Assumed registers */
142 /* These are cheat registers which emulate the 1-longword prefetch
143 * present in the 68000 and 68010.
145 M68K_REG_PREF_ADDR, /* Last prefetch address */
146 M68K_REG_PREF_DATA, /* Last prefetch data */
148 /* Convenience registers */
149 M68K_REG_PPC, /* Previous value in the program counter */
150 M68K_REG_IR, /* Instruction register */
151 M68K_REG_CPU_TYPE /* Type of CPU being run */
154 /* ======================================================================== */
155 /* ====================== FUNCTIONS CALLED BY THE CPU ===================== */
156 /* ======================================================================== */
158 /* You will have to implement these functions */
160 /* read/write functions called by the CPU to access memory.
161 * while values used are 32 bits, only the appropriate number
162 * of bits are relevant (i.e. in write_memory_8, only the lower 8 bits
163 * of value should be written to memory).
165 * NOTE: I have separated the immediate and PC-relative memory fetches
166 * from the other memory fetches because some systems require
167 * differentiation between PROGRAM and DATA fetches (usually
168 * for security setups such as encryption).
169 * This separation can either be achieved by setting
170 * M68K_SEPARATE_READS in m68kconf.h and defining
171 * the read functions, or by setting M68K_EMULATE_FC and
172 * making a function code callback function.
173 * Using the callback offers better emulation coverage
174 * because you can also monitor whether the CPU is in SYSTEM or
175 * USER mode, but it is also slower.
178 #define m68k_read_disassembler_8 m68k_read_memory_8
179 #define m68k_read_disassembler_16 m68k_read_memory_16
180 #define m68k_read_disassembler_32 m68k_read_memory_32
182 /* Read from anywhere */
183 unsigned int m68k_read_memory_8(unsigned int address);
184 unsigned int m68k_read_memory_16(unsigned int address);
185 unsigned int m68k_read_memory_32(unsigned int address);
187 /* Read data immediately following the PC */
188 unsigned int m68k_read_immediate_16(unsigned int address);
189 unsigned int m68k_read_immediate_32(unsigned int address);
191 /* Read data relative to the PC */
192 unsigned int m68k_read_pcrelative_8(unsigned int address);
193 unsigned int m68k_read_pcrelative_16(unsigned int address);
194 unsigned int m68k_read_pcrelative_32(unsigned int address);
196 /* Memory access for the disassembler */
197 unsigned int m68k_read_disassembler_8 (unsigned int address);
198 unsigned int m68k_read_disassembler_16 (unsigned int address);
199 unsigned int m68k_read_disassembler_32 (unsigned int address);
201 /* Write to anywhere */
202 void m68k_write_memory_8(unsigned int address, unsigned int value);
203 void m68k_write_memory_16(unsigned int address, unsigned int value);
204 void m68k_write_memory_32(unsigned int address, unsigned int value);
206 /* PiStorm speed hax */
207 void m68k_add_ram_range(uint32_t addr, uint32_t upper, unsigned char *ptr);
208 void m68k_add_rom_range(uint32_t addr, uint32_t upper, unsigned char *ptr);
210 /* Special call to simulate undocumented 68k behavior when move.l with a
211 * predecrement destination mode is executed.
212 * To simulate real 68k behavior, first write the high word to
213 * [address+2], and then write the low word to [address].
215 * Enable this functionality with M68K_SIMULATE_PD_WRITES in m68kconf.h.
217 void m68k_write_memory_32_pd(unsigned int address, unsigned int value);
221 /* ======================================================================== */
222 /* ============================== CALLBACKS =============================== */
223 /* ======================================================================== */
225 /* These functions allow you to set callbacks to the host when specific events
226 * occur. Note that you must enable the corresponding value in m68kconf.h
227 * in order for these to do anything useful.
228 * Note: I have defined default callbacks which are used if you have enabled
229 * the corresponding #define in m68kconf.h but either haven't assigned a
230 * callback or have assigned a callback of NULL.
233 /* Set the callback for an interrupt acknowledge.
234 * You must enable M68K_EMULATE_INT_ACK in m68kconf.h.
235 * The CPU will call the callback with the interrupt level being acknowledged.
236 * The host program must return either a vector from 0x02-0xff, or one of the
237 * special interrupt acknowledge values specified earlier in this header.
238 * If this is not implemented, the CPU will always assume an autovectored
239 * interrupt, and will automatically clear the interrupt request when it
240 * services the interrupt.
241 * Default behavior: return M68K_INT_ACK_AUTOVECTOR.
243 void m68k_set_int_ack_callback(int (*callback)(int int_level));
246 /* Set the callback for a breakpoint acknowledge (68010+).
247 * You must enable M68K_EMULATE_BKPT_ACK in m68kconf.h.
248 * The CPU will call the callback with whatever was in the data field of the
249 * BKPT instruction for 68020+, or 0 for 68010.
250 * Default behavior: do nothing.
252 void m68k_set_bkpt_ack_callback(void (*callback)(unsigned int data));
255 /* Set the callback for the RESET instruction.
256 * You must enable M68K_EMULATE_RESET in m68kconf.h.
257 * The CPU calls this callback every time it encounters a RESET instruction.
258 * Default behavior: do nothing.
260 void m68k_set_reset_instr_callback(void (*callback)(void));
263 /* Set the callback for informing of a large PC change.
264 * You must enable M68K_MONITOR_PC in m68kconf.h.
265 * The CPU calls this callback with the new PC value every time the PC changes
266 * by a large value (currently set for changes by longwords).
267 * Default behavior: do nothing.
269 void m68k_set_pc_changed_callback(void (*callback)(unsigned int new_pc));
271 /* Set the callback for the TAS instruction.
272 * You must enable M68K_TAS_HAS_CALLBACK in m68kconf.h.
273 * The CPU calls this callback every time it encounters a TAS instruction.
274 * Default behavior: return 1, allow writeback.
276 void m68k_set_tas_instr_callback(int (*callback)(void));
278 /* Set the callback for illegal instructions.
279 * You must enable M68K_ILLG_HAS_CALLBACK in m68kconf.h.
280 * The CPU calls this callback every time it encounters an illegal instruction
281 * which must return 1 if it handles the instruction normally or 0 if it's really an illegal instruction.
282 * Default behavior: return 0, exception will occur.
284 void m68k_set_illg_instr_callback(int (*callback)(int));
286 /* Set the callback for CPU function code changes.
287 * You must enable M68K_EMULATE_FC in m68kconf.h.
288 * The CPU calls this callback with the function code before every memory
289 * access to set the CPU's function code according to what kind of memory
290 * access it is (supervisor/user, program/data and such).
291 * Default behavior: do nothing.
293 void m68k_set_fc_callback(void (*callback)(unsigned int new_fc));
296 /* Set a callback for the instruction cycle of the CPU.
297 * You must enable M68K_INSTRUCTION_HOOK in m68kconf.h.
298 * The CPU calls this callback just before fetching the opcode in the
300 * Default behavior: do nothing.
302 void m68k_set_instr_hook_callback(void (*callback)(unsigned int pc));
306 /* ======================================================================== */
307 /* ====================== FUNCTIONS TO ACCESS THE CPU ===================== */
308 /* ======================================================================== */
310 /* Use this function to set the CPU type you want to emulate.
311 * Currently supported types are: M68K_CPU_TYPE_68000, M68K_CPU_TYPE_68010,
312 * M68K_CPU_TYPE_EC020, and M68K_CPU_TYPE_68020.
314 void m68k_set_cpu_type(unsigned int cpu_type);
316 /* Do whatever initialisations the core requires. Should be called
317 * at least once at init time.
319 void m68k_init(void);
321 /* Pulse the RESET pin on the CPU.
322 * You *MUST* reset the CPU at least once to initialize the emulation
323 * Note: If you didn't call m68k_set_cpu_type() before resetting
324 * the CPU for the first time, the CPU will be set to
325 * M68K_CPU_TYPE_68000.
327 void m68k_pulse_reset(void);
329 /* execute num_cycles worth of instructions. returns number of cycles used */
330 int m68k_execute(int num_cycles);
332 /* These functions let you read/write/modify the number of cycles left to run
333 * while m68k_execute() is running.
334 * These are useful if the 68k accesses a memory-mapped port on another device
335 * that requires immediate processing by another CPU.
337 int m68k_cycles_run(void); /* Number of cycles run so far */
338 int m68k_cycles_remaining(void); /* Number of cycles left */
339 void m68k_modify_timeslice(int cycles); /* Modify cycles left */
340 void m68k_end_timeslice(void); /* End timeslice now */
342 /* Set the IPL0-IPL2 pins on the CPU (IRQ).
343 * A transition from < 7 to 7 will cause a non-maskable interrupt (NMI).
344 * Setting IRQ to 0 will clear an interrupt request.
346 void m68k_set_irq(unsigned int int_level);
348 /* Set the virtual irq lines, where the highest level
349 * active line is automatically selected. If you use this function,
350 * do not use m68k_set_irq.
352 void m68k_set_virq(unsigned int level, unsigned int active);
353 unsigned int m68k_get_virq(unsigned int level);
355 /* Halt the CPU as if you pulsed the HALT pin. */
356 void m68k_pulse_halt(void);
359 /* Trigger a bus error exception */
360 void m68k_pulse_bus_error(void);
363 /* Context switching to allow multiple CPUs */
365 /* Get the size of the cpu context in bytes */
366 unsigned int m68k_context_size(void);
368 /* Get a cpu context */
369 unsigned int m68k_get_context(void* dst);
371 /* set the current cpu context */
372 void m68k_set_context(void* dst);
374 /* Register the CPU state information */
375 void m68k_state_register(const char *type, int index);
378 /* Peek at the internals of a CPU context. This can either be a context
379 * retrieved using m68k_get_context() or the currently running context.
380 * If context is NULL, the currently running CPU context will be used.
382 unsigned int m68k_get_reg(void* context, m68k_register_t reg);
384 /* Poke values into the internals of the currently running CPU context */
385 void m68k_set_reg(m68k_register_t reg, unsigned int value);
387 /* Check if an instruction is valid for the specified CPU type */
388 unsigned int m68k_is_valid_instruction(unsigned int instruction, unsigned int cpu_type);
390 /* Disassemble 1 instruction using the epecified CPU type at pc. Stores
391 * disassembly in str_buff and returns the size of the instruction in bytes.
393 unsigned int m68k_disassemble(char* str_buff, unsigned int pc, unsigned int cpu_type);
395 /* Same as above but accepts raw opcode data directly rather than fetching
396 * via the read/write interfaces.
398 unsigned int m68k_disassemble_raw(char* str_buff, unsigned int pc, const unsigned char* opdata, const unsigned char* argdata, unsigned int cpu_type);
401 /* ======================================================================== */
402 /* ============================== MAME STUFF ============================== */
403 /* ======================================================================== */
405 #if M68K_COMPILE_FOR_MAME == OPT_ON
406 #include "m68kmame.h"
407 #endif /* M68K_COMPILE_FOR_MAME */
410 /* ======================================================================== */
411 /* ============================== END OF FILE ============================= */
412 /* ======================================================================== */
419 #endif /* M68K__HEADER */