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
74 #define M68K_SZ_LONG 0
75 #define M68K_SZ_BYTE 1
76 #define M68K_SZ_WORD 2
79 /* Special interrupt acknowledge values.
80 * Use these as special returns from the interrupt acknowledge callback
81 * (specified later in this header).
84 /* Causes an interrupt autovector (0x18 + interrupt level) to be taken.
85 * This happens in a real 68K if VPA or AVEC is asserted during an interrupt
86 * acknowledge cycle instead of DTACK.
88 #define M68K_INT_ACK_AUTOVECTOR 0xffffffff
90 /* Causes the spurious interrupt vector (0x18) to be taken
91 * This happens in a real 68K if BERR is asserted during the interrupt
92 * acknowledge cycle (i.e. no devices responded to the acknowledge).
94 #define M68K_INT_ACK_SPURIOUS 0xfffffffe
97 /* CPU types for use in m68k_set_cpu_type() */
100 M68K_CPU_TYPE_INVALID,
103 M68K_CPU_TYPE_68EC020,
105 M68K_CPU_TYPE_68EC030,
107 M68K_CPU_TYPE_68EC040,
108 M68K_CPU_TYPE_68LC040,
110 M68K_CPU_TYPE_SCC68070
113 /* Registers used by m68k_get_reg() and m68k_set_reg() */
117 M68K_REG_D0, /* Data registers */
125 M68K_REG_A0, /* Address registers */
133 M68K_REG_PC, /* Program Counter */
134 M68K_REG_SR, /* Status Register */
135 M68K_REG_SP, /* The current Stack Pointer (located in A7) */
136 M68K_REG_USP, /* User Stack Pointer */
137 M68K_REG_ISP, /* Interrupt Stack Pointer */
138 M68K_REG_MSP, /* Master Stack Pointer */
139 M68K_REG_SFC, /* Source Function Code */
140 M68K_REG_DFC, /* Destination Function Code */
141 M68K_REG_VBR, /* Vector Base Register */
142 M68K_REG_CACR, /* Cache Control Register */
143 M68K_REG_CAAR, /* Cache Address Register */
145 /* Assumed registers */
146 /* These are cheat registers which emulate the 1-longword prefetch
147 * present in the 68000 and 68010.
149 M68K_REG_PREF_ADDR, /* Last prefetch address */
150 M68K_REG_PREF_DATA, /* Last prefetch data */
152 /* Convenience registers */
153 M68K_REG_PPC, /* Previous value in the program counter */
154 M68K_REG_IR, /* Instruction register */
155 M68K_REG_CPU_TYPE /* Type of CPU being run */
158 /* ======================================================================== */
159 /* ====================== FUNCTIONS CALLED BY THE CPU ===================== */
160 /* ======================================================================== */
162 /* You will have to implement these functions */
164 /* read/write functions called by the CPU to access memory.
165 * while values used are 32 bits, only the appropriate number
166 * of bits are relevant (i.e. in write_memory_8, only the lower 8 bits
167 * of value should be written to memory).
169 * NOTE: I have separated the immediate and PC-relative memory fetches
170 * from the other memory fetches because some systems require
171 * differentiation between PROGRAM and DATA fetches (usually
172 * for security setups such as encryption).
173 * This separation can either be achieved by setting
174 * M68K_SEPARATE_READS in m68kconf.h and defining
175 * the read functions, or by setting M68K_EMULATE_FC and
176 * making a function code callback function.
177 * Using the callback offers better emulation coverage
178 * because you can also monitor whether the CPU is in SYSTEM or
179 * USER mode, but it is also slower.
182 #define m68k_read_disassembler_8 m68k_read_memory_8
183 #define m68k_read_disassembler_16 m68k_read_memory_16
184 #define m68k_read_disassembler_32 m68k_read_memory_32
186 /* Read from anywhere */
187 unsigned int m68k_read_memory_8(unsigned int address);
188 unsigned int m68k_read_memory_16(unsigned int address);
189 unsigned int m68k_read_memory_32(unsigned int address);
191 /* Read data immediately following the PC */
192 unsigned int m68k_read_immediate_16(unsigned int address);
193 unsigned int m68k_read_immediate_32(unsigned int address);
195 /* Read data relative to the PC */
196 unsigned int m68k_read_pcrelative_8(unsigned int address);
197 unsigned int m68k_read_pcrelative_16(unsigned int address);
198 unsigned int m68k_read_pcrelative_32(unsigned int address);
200 /* Memory access for the disassembler */
201 unsigned int m68k_read_disassembler_8 (unsigned int address);
202 unsigned int m68k_read_disassembler_16 (unsigned int address);
203 unsigned int m68k_read_disassembler_32 (unsigned int address);
205 /* Write to anywhere */
206 void m68k_write_memory_8(unsigned int address, unsigned int value);
207 void m68k_write_memory_16(unsigned int address, unsigned int value);
208 void m68k_write_memory_32(unsigned int address, unsigned int value);
210 /* PiStorm speed hax */
211 void m68k_add_ram_range(uint32_t addr, uint32_t upper, unsigned char *ptr);
212 void m68k_add_rom_range(uint32_t addr, uint32_t upper, unsigned char *ptr);
214 /* Special call to simulate undocumented 68k behavior when move.l with a
215 * predecrement destination mode is executed.
216 * To simulate real 68k behavior, first write the high word to
217 * [address+2], and then write the low word to [address].
219 * Enable this functionality with M68K_SIMULATE_PD_WRITES in m68kconf.h.
221 void m68k_write_memory_32_pd(unsigned int address, unsigned int value);
223 /* ======================================================================== */
224 /* ============================== CALLBACKS =============================== */
225 /* ======================================================================== */
227 /* These functions allow you to set callbacks to the host when specific events
228 * occur. Note that you must enable the corresponding value in m68kconf.h
229 * in order for these to do anything useful.
230 * Note: I have defined default callbacks which are used if you have enabled
231 * the corresponding #define in m68kconf.h but either haven't assigned a
232 * callback or have assigned a callback of NULL.
235 /* Set the callback for an interrupt acknowledge.
236 * You must enable M68K_EMULATE_INT_ACK in m68kconf.h.
237 * The CPU will call the callback with the interrupt level being acknowledged.
238 * The host program must return either a vector from 0x02-0xff, or one of the
239 * special interrupt acknowledge values specified earlier in this header.
240 * If this is not implemented, the CPU will always assume an autovectored
241 * interrupt, and will automatically clear the interrupt request when it
242 * services the interrupt.
243 * Default behavior: return M68K_INT_ACK_AUTOVECTOR.
245 void m68k_set_int_ack_callback(int (*callback)(int int_level));
248 /* Set the callback for a breakpoint acknowledge (68010+).
249 * You must enable M68K_EMULATE_BKPT_ACK in m68kconf.h.
250 * The CPU will call the callback with whatever was in the data field of the
251 * BKPT instruction for 68020+, or 0 for 68010.
252 * Default behavior: do nothing.
254 void m68k_set_bkpt_ack_callback(void (*callback)(unsigned int data));
257 /* Set the callback for the RESET instruction.
258 * You must enable M68K_EMULATE_RESET in m68kconf.h.
259 * The CPU calls this callback every time it encounters a RESET instruction.
260 * Default behavior: do nothing.
262 void m68k_set_reset_instr_callback(void (*callback)(void));
265 /* Set the callback for informing of a large PC change.
266 * You must enable M68K_MONITOR_PC in m68kconf.h.
267 * The CPU calls this callback with the new PC value every time the PC changes
268 * by a large value (currently set for changes by longwords).
269 * Default behavior: do nothing.
271 void m68k_set_pc_changed_callback(void (*callback)(unsigned int new_pc));
273 /* Set the callback for the TAS instruction.
274 * You must enable M68K_TAS_HAS_CALLBACK in m68kconf.h.
275 * The CPU calls this callback every time it encounters a TAS instruction.
276 * Default behavior: return 1, allow writeback.
278 void m68k_set_tas_instr_callback(int (*callback)(void));
280 /* Set the callback for illegal instructions.
281 * You must enable M68K_ILLG_HAS_CALLBACK in m68kconf.h.
282 * The CPU calls this callback every time it encounters an illegal instruction
283 * which must return 1 if it handles the instruction normally or 0 if it's really an illegal instruction.
284 * Default behavior: return 0, exception will occur.
286 void m68k_set_illg_instr_callback(int (*callback)(int));
288 /* Set the callback for CPU function code changes.
289 * You must enable M68K_EMULATE_FC in m68kconf.h.
290 * The CPU calls this callback with the function code before every memory
291 * access to set the CPU's function code according to what kind of memory
292 * access it is (supervisor/user, program/data and such).
293 * Default behavior: do nothing.
295 void m68k_set_fc_callback(void (*callback)(unsigned int new_fc));
298 /* Set a callback for the instruction cycle of the CPU.
299 * You must enable M68K_INSTRUCTION_HOOK in m68kconf.h.
300 * The CPU calls this callback just before fetching the opcode in the
302 * Default behavior: do nothing.
304 void m68k_set_instr_hook_callback(void (*callback)(unsigned int pc));
308 /* ======================================================================== */
309 /* ====================== FUNCTIONS TO ACCESS THE CPU ===================== */
310 /* ======================================================================== */
312 /* Use this function to set the CPU type you want to emulate.
313 * Currently supported types are: M68K_CPU_TYPE_68000, M68K_CPU_TYPE_68010,
314 * M68K_CPU_TYPE_EC020, and M68K_CPU_TYPE_68020.
316 void m68k_set_cpu_type(unsigned int cpu_type);
318 /* Do whatever initialisations the core requires. Should be called
319 * at least once at init time.
321 void m68k_init(void);
323 /* Pulse the RESET pin on the CPU.
324 * You *MUST* reset the CPU at least once to initialize the emulation
325 * Note: If you didn't call m68k_set_cpu_type() before resetting
326 * the CPU for the first time, the CPU will be set to
327 * M68K_CPU_TYPE_68000.
329 void m68k_pulse_reset(void);
331 /* execute num_cycles worth of instructions. returns number of cycles used */
332 int m68k_execute(int num_cycles);
334 /* These functions let you read/write/modify the number of cycles left to run
335 * while m68k_execute() is running.
336 * These are useful if the 68k accesses a memory-mapped port on another device
337 * that requires immediate processing by another CPU.
339 int m68k_cycles_run(void); /* Number of cycles run so far */
340 int m68k_cycles_remaining(void); /* Number of cycles left */
341 void m68k_modify_timeslice(int cycles); /* Modify cycles left */
342 void m68k_end_timeslice(void); /* End timeslice now */
344 /* Set the IPL0-IPL2 pins on the CPU (IRQ).
345 * A transition from < 7 to 7 will cause a non-maskable interrupt (NMI).
346 * Setting IRQ to 0 will clear an interrupt request.
348 void m68k_set_irq(unsigned int int_level);
350 /* Set the virtual irq lines, where the highest level
351 * active line is automatically selected. If you use this function,
352 * do not use m68k_set_irq.
354 void m68k_set_virq(unsigned int level, unsigned int active);
355 unsigned int m68k_get_virq(unsigned int level);
357 /* Halt the CPU as if you pulsed the HALT pin. */
358 void m68k_pulse_halt(void);
361 /* Trigger a bus error exception */
362 void m68k_pulse_bus_error(void);
365 /* Context switching to allow multiple CPUs */
367 /* Get the size of the cpu context in bytes */
368 unsigned int m68k_context_size(void);
370 /* Get a cpu context */
371 unsigned int m68k_get_context(void* dst);
373 /* set the current cpu context */
374 void m68k_set_context(void* dst);
376 /* Register the CPU state information */
377 void m68k_state_register(const char *type, int index);
380 /* Peek at the internals of a CPU context. This can either be a context
381 * retrieved using m68k_get_context() or the currently running context.
382 * If context is NULL, the currently running CPU context will be used.
384 unsigned int m68k_get_reg(void* context, m68k_register_t reg);
386 /* Poke values into the internals of the currently running CPU context */
387 void m68k_set_reg(m68k_register_t reg, unsigned int value);
389 /* Check if an instruction is valid for the specified CPU type */
390 unsigned int m68k_is_valid_instruction(unsigned int instruction, unsigned int cpu_type);
392 /* Disassemble 1 instruction using the epecified CPU type at pc. Stores
393 * disassembly in str_buff and returns the size of the instruction in bytes.
395 unsigned int m68k_disassemble(char* str_buff, unsigned int pc, unsigned int cpu_type);
397 /* Same as above but accepts raw opcode data directly rather than fetching
398 * via the read/write interfaces.
400 unsigned int m68k_disassemble_raw(char* str_buff, unsigned int pc, const unsigned char* opdata, const unsigned char* argdata, unsigned int cpu_type);
403 /* ======================================================================== */
404 /* ============================== MAME STUFF ============================== */
405 /* ======================================================================== */
407 #if M68K_COMPILE_FOR_MAME == OPT_ON
408 #include "m68kmame.h"
409 #endif /* M68K_COMPILE_FOR_MAME */
412 /* ======================================================================== */
413 /* ============================== END OF FILE ============================= */
414 /* ======================================================================== */
421 #endif /* M68K__HEADER */