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