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