8 /* ======================================================================== */
9 /* ========================= LICENSING & COPYRIGHT ======================== */
10 /* ======================================================================== */
15 * A portable Motorola M680x0 processor emulation engine.
16 * Copyright 1998-2001 Karl Stenerud. All rights reserved.
18 * This code may be freely used for non-commercial purposes as long as this
19 * copyright notice remains unaltered in the source code and any binary files
20 * containing this code in compiled form.
22 * All other lisencing terms must be negotiated with the author
25 * The latest version of this code can be obtained at:
26 * http://kstenerud.cjb.net
31 /* ======================================================================== */
32 /* ============================ GENERAL DEFINES =========================== */
34 /* ======================================================================== */
36 /* There are 7 levels of interrupt to the 68K.
37 * A transition from < 7 to 7 will cause a non-maskable interrupt (NMI).
39 #define M68K_IRQ_NONE 0
49 /* Special interrupt acknowledge values.
50 * Use these as special returns from the interrupt acknowledge callback
51 * (specified later in this header).
54 /* Causes an interrupt autovector (0x18 + interrupt level) to be taken.
55 * This happens in a real 68K if VPA or AVEC is asserted during an interrupt
56 * acknowledge cycle instead of DTACK.
58 #define M68K_INT_ACK_AUTOVECTOR 0xffffffff
60 /* Causes the spurious interrupt vector (0x18) to be taken
61 * This happens in a real 68K if BERR is asserted during the interrupt
62 * acknowledge cycle (i.e. no devices responded to the acknowledge).
64 #define M68K_INT_ACK_SPURIOUS 0xfffffffe
67 /* CPU types for use in m68k_set_cpu_type() */
70 M68K_CPU_TYPE_INVALID,
73 M68K_CPU_TYPE_68EC020,
75 M68K_CPU_TYPE_68030, /* Supported by disassembler ONLY */
76 M68K_CPU_TYPE_68040 /* Supported by disassembler ONLY */
79 /* Registers used by m68k_get_reg() and m68k_set_reg() */
83 M68K_REG_D0, /* Data registers */
91 M68K_REG_A0, /* Address registers */
99 M68K_REG_PC, /* Program Counter */
100 M68K_REG_SR, /* Status Register */
101 M68K_REG_SP, /* The current Stack Pointer (located in A7) */
102 M68K_REG_USP, /* User Stack Pointer */
103 M68K_REG_ISP, /* Interrupt Stack Pointer */
104 M68K_REG_MSP, /* Master Stack Pointer */
105 M68K_REG_SFC, /* Source Function Code */
106 M68K_REG_DFC, /* Destination Function Code */
107 M68K_REG_VBR, /* Vector Base Register */
108 M68K_REG_CACR, /* Cache Control Register */
109 M68K_REG_CAAR, /* Cache Address Register */
111 /* Assumed registers */
112 /* These are cheat registers which emulate the 1-longword prefetch
113 * present in the 68000 and 68010.
115 M68K_REG_PREF_ADDR, /* Last prefetch address */
116 M68K_REG_PREF_DATA, /* Last prefetch data */
118 /* Convenience registers */
119 M68K_REG_PPC, /* Previous value in the program counter */
120 M68K_REG_IR, /* Instruction register */
121 M68K_REG_CPU_TYPE /* Type of CPU being run */
124 /* ======================================================================== */
125 /* ====================== FUNCTIONS CALLED BY THE CPU ===================== */
126 /* ======================================================================== */
128 /* You will have to implement these functions */
130 /* read/write functions called by the CPU to access memory.
131 * while values used are 32 bits, only the appropriate number
132 * of bits are relevant (i.e. in write_memory_8, only the lower 8 bits
133 * of value should be written to memory).
135 * NOTE: I have separated the immediate and PC-relative memory fetches
136 * from the other memory fetches because some systems require
137 * differentiation between PROGRAM and DATA fetches (usually
138 * for security setups such as encryption).
139 * This separation can either be achieved by setting
140 * M68K_SEPARATE_READS in m68kconf.h and defining
141 * the read functions, or by setting M68K_EMULATE_FC and
142 * making a function code callback function.
143 * Using the callback offers better emulation coverage
144 * because you can also monitor whether the CPU is in SYSTEM or
145 * USER mode, but it is also slower.
148 /* Read from anywhere */
149 unsigned int m68k_read_memory_8(unsigned int address);
150 unsigned int m68k_read_memory_16(unsigned int address);
151 unsigned int m68k_read_memory_32(unsigned int address);
153 /* Read data immediately following the PC */
154 unsigned int m68k_read_immediate_16(unsigned int address);
155 unsigned int m68k_read_immediate_32(unsigned int address);
157 /* Read data relative to the PC */
158 unsigned int m68k_read_pcrelative_8(unsigned int address);
159 unsigned int m68k_read_pcrelative_16(unsigned int address);
160 unsigned int m68k_read_pcrelative_32(unsigned int address);
162 /* Memory access for the disassembler */
163 unsigned int m68k_read_disassembler_8 (unsigned int address);
164 unsigned int m68k_read_disassembler_16 (unsigned int address);
165 unsigned int m68k_read_disassembler_32 (unsigned int address);//*/
166 /*#define m68k_read_disassembler_8 m68k_read_memory_8
167 #define m68k_read_disassembler_16 m68k_read_memory_16
168 #define m68k_read_disassembler_32 m68k_read_memory_32//*/
170 /* Write to anywhere */
171 void m68k_write_memory_8(unsigned int address, unsigned int value);
172 void m68k_write_memory_16(unsigned int address, unsigned int value);
173 void m68k_write_memory_32(unsigned int address, unsigned int value);
177 /* ======================================================================== */
178 /* ============================== CALLBACKS =============================== */
179 /* ======================================================================== */
181 /* These functions allow you to set callbacks to the host when specific events
182 * occur. Note that you must enable the corresponding value in m68kconf.h
183 * in order for these to do anything useful.
184 * Note: I have defined default callbacks which are used if you have enabled
185 * the corresponding #define in m68kconf.h but either haven't assigned a
186 * callback or have assigned a callback of NULL.
189 /* Set the callback for an interrupt acknowledge.
190 * You must enable M68K_EMULATE_INT_ACK in m68kconf.h.
191 * The CPU will call the callback with the interrupt level being acknowledged.
192 * The host program must return either a vector from 0x02-0xff, or one of the
193 * special interrupt acknowledge values specified earlier in this header.
194 * If this is not implemented, the CPU will always assume an autovectored
195 * interrupt, and will automatically clear the interrupt request when it
196 * services the interrupt.
197 * Default behavior: return M68K_INT_ACK_AUTOVECTOR.
199 void m68k_set_int_ack_callback(int (*callback)(int int_level));
202 /* Set the callback for a breakpoint acknowledge (68010+).
203 * You must enable M68K_EMULATE_BKPT_ACK in m68kconf.h.
204 * The CPU will call the callback with whatever was in the data field of the
205 * BKPT instruction for 68020+, or 0 for 68010.
206 * Default behavior: do nothing.
208 void m68k_set_bkpt_ack_callback(void (*callback)(unsigned int data));
211 /* Set the callback for the RESET instruction.
212 * You must enable M68K_EMULATE_RESET in m68kconf.h.
213 * The CPU calls this callback every time it encounters a RESET instruction.
214 * Default behavior: do nothing.
216 void m68k_set_reset_instr_callback(void (*callback)(void));
219 /* Set the callback for informing of a large PC change.
220 * You must enable M68K_MONITOR_PC in m68kconf.h.
221 * The CPU calls this callback with the new PC value every time the PC changes
222 * by a large value (currently set for changes by longwords).
223 * Default behavior: do nothing.
225 void m68k_set_pc_changed_callback(void (*callback)(unsigned int new_pc));
228 /* Set the callback for CPU function code changes.
229 * You must enable M68K_EMULATE_FC in m68kconf.h.
230 * The CPU calls this callback with the function code before every memory
231 * access to set the CPU's function code according to what kind of memory
232 * access it is (supervisor/user, program/data and such).
233 * Default behavior: do nothing.
235 void m68k_set_fc_callback(void (*callback)(unsigned int new_fc));
238 /* Set a callback for the instruction cycle of the CPU.
239 * You must enable M68K_INSTRUCTION_HOOK in m68kconf.h.
240 * The CPU calls this callback just before fetching the opcode in the
242 * Default behavior: do nothing.
244 void m68k_set_instr_hook_callback(void (*callback)(void));
248 /* ======================================================================== */
249 /* ====================== FUNCTIONS TO ACCESS THE CPU ===================== */
250 /* ======================================================================== */
252 /* Use this function to set the CPU type you want to emulate.
253 * Currently supported types are: M68K_CPU_TYPE_68000, M68K_CPU_TYPE_68010,
254 * M68K_CPU_TYPE_EC020, and M68K_CPU_TYPE_68020.
256 void m68k_set_cpu_type(unsigned int cpu_type);
258 /* Pulse the RESET pin on the CPU.
259 * You *MUST* reset the CPU at least once to initialize the emulation
260 * Note: If you didn't call m68k_set_cpu_type() before resetting
261 * the CPU for the first time, the CPU will be set to
262 * M68K_CPU_TYPE_68000.
264 void m68k_pulse_reset(void);
266 /* execute num_cycles worth of instructions. returns number of cycles used */
267 int m68k_execute(int num_cycles);
269 /* These functions let you read/write/modify the number of cycles left to run
270 * while m68k_execute() is running.
271 * These are useful if the 68k accesses a memory-mapped port on another device
272 * that requires immediate processing by another CPU.
274 int m68k_cycles_run(void); /* Number of cycles run so far */
275 int m68k_cycles_remaining(void); /* Number of cycles left */
276 void m68k_modify_timeslice(int cycles); /* Modify cycles left */
277 void m68k_end_timeslice(void); /* End timeslice now */
279 /* Set the IPL0-IPL2 pins on the CPU (IRQ).
280 * A transition from < 7 to 7 will cause a non-maskable interrupt (NMI).
281 * Setting IRQ to 0 will clear an interrupt request.
283 void m68k_set_irq(unsigned int int_level);
286 /* Halt the CPU as if you pulsed the HALT pin. */
287 void m68k_pulse_halt(void);
290 /* Context switching to allow multiple CPUs */
292 /* Get the size of the cpu context in bytes */
293 unsigned int m68k_context_size(void);
295 /* Get a cpu context */
296 unsigned int m68k_get_context(void* dst);
298 /* set the current cpu context */
299 void m68k_set_context(void* dst);
301 /* Save the current cpu context to disk.
302 * You must provide a function pointer of the form:
303 * void save_value(char* identifier, unsigned int value)
305 void m68k_save_context( void (*save_value)(char* identifier, unsigned int value));
307 /* Load a cpu context from disk.
308 * You must provide a function pointer of the form:
309 * unsigned int load_value(char* identifier)
311 void m68k_load_context(unsigned int (*load_value)(char* identifier));
315 /* Peek at the internals of a CPU context. This can either be a context
316 * retrieved using m68k_get_context() or the currently running context.
317 * If context is NULL, the currently running CPU context will be used.
319 unsigned int m68k_get_reg(void* context, m68k_register_t reg);
321 /* Poke values into the internals of the currently running CPU context */
322 void m68k_set_reg(m68k_register_t reg, unsigned int value);
324 /* Check if an instruction is valid for the specified CPU type */
325 unsigned int m68k_is_valid_instruction(unsigned int instruction, unsigned int cpu_type);
327 /* Disassemble 1 instruction using the epecified CPU type at pc. Stores
328 * disassembly in str_buff and returns the size of the instruction in bytes.
330 unsigned int m68k_disassemble(char* str_buff, unsigned int pc, unsigned int cpu_type);
332 /* ======================================================================== */
333 /* ============================= CONFIGURATION ============================ */
334 /* ======================================================================== */
336 /* Import the configuration for this build */
337 #include "m68kconf.h"
341 /* ======================================================================== */
342 /* ============================== END OF FILE ============================= */
343 /* ======================================================================== */
349 #endif /* M68K__HEADER */