1 #ifndef M68K__HEADER
2 #define M68K__HEADER
3 
4 #ifdef __cplusplus
5  extern "C" {
6 #endif
7 
8 /* ======================================================================== */
9 /* ========================= LICENSING & COPYRIGHT ======================== */
10 /* ======================================================================== */
11 /*
12  *                                  MUSASHI
13  *                                Version 3.32
14  *
15  * A portable Motorola M680x0 processor emulation engine.
16  * Copyright Karl Stenerud.  All rights reserved.
17  *
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.
21  *
22  * All other licensing terms must be negotiated with the author
23  * (Karl Stenerud).
24  *
25  * The latest version of this code can be obtained at:
26  * http://kstenerud.cjb.net
27  */
28 
29 
30 
31 /* ============================ GENERAL DEFINES =========================== */
32 
33 /* ======================================================================== */
34 
35 /* There are 7 levels of interrupt to the 68K.
36  * A transition from < 7 to 7 will cause a non-maskable interrupt (NMI).
37  */
38 #define M68K_IRQ_NONE 0
39 #define M68K_IRQ_1    1
40 #define M68K_IRQ_2    2
41 #define M68K_IRQ_3    3
42 #define M68K_IRQ_4    4
43 #define M68K_IRQ_5    5
44 #define M68K_IRQ_6    6
45 #define M68K_IRQ_7    7
46 
47 
48 /* Special interrupt acknowledge values.
49  * Use these as special returns from the interrupt acknowledge callback
50  * (specified later in this header).
51  */
52 
53 /* Causes an interrupt autovector (0x18 + interrupt level) to be taken.
54  * This happens in a real 68K if VPA or AVEC is asserted during an interrupt
55  * acknowledge cycle instead of DTACK.
56  */
57 #define M68K_INT_ACK_AUTOVECTOR    0xffffffff
58 
59 /* Causes the spurious interrupt vector (0x18) to be taken
60  * This happens in a real 68K if BERR is asserted during the interrupt
61  * acknowledge cycle (i.e. no devices responded to the acknowledge).
62  */
63 #define M68K_INT_ACK_SPURIOUS      0xfffffffe
64 
65 
66 /* CPU types for use in m68k_set_cpu_type() */
67 enum
68 {
69 	M68K_CPU_TYPE_INVALID,
70 	M68K_CPU_TYPE_68000,
71 	M68K_CPU_TYPE_68008,
72 	M68K_CPU_TYPE_68010,
73 	M68K_CPU_TYPE_68EC020,
74 	M68K_CPU_TYPE_68020,
75 	M68K_CPU_TYPE_68030,	/* Supported by disassembler ONLY */
76 	M68K_CPU_TYPE_68040		/* Supported by disassembler ONLY */
77 };
78 
79 /* Registers used by m68k_get_reg() and m68k_set_reg() */
80 enum _m68k_register_t
81 {
82 	/* Real registers */
83 	M68K_REG_D0,		/* Data registers */
84 	M68K_REG_D1,
85 	M68K_REG_D2,
86 	M68K_REG_D3,
87 	M68K_REG_D4,
88 	M68K_REG_D5,
89 	M68K_REG_D6,
90 	M68K_REG_D7,
91 	M68K_REG_A0,		/* Address registers */
92 	M68K_REG_A1,
93 	M68K_REG_A2,
94 	M68K_REG_A3,
95 	M68K_REG_A4,
96 	M68K_REG_A5,
97 	M68K_REG_A6,
98 	M68K_REG_A7,
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 */
110 
111 	/* Assumed registers */
112 	/* These are cheat registers which emulate the 1-longword prefetch
113      * present in the 68000 and 68010.
114      */
115 	M68K_REG_PREF_ADDR,	/* Last prefetch address */
116 	M68K_REG_PREF_DATA,	/* Last prefetch data */
117 
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 */
122 };
123 typedef enum _m68k_register_t m68k_register_t;
124 
125 
126 
127 /* ======================================================================== */
128 /* ====================== FUNCTIONS CALLED BY THE CPU ===================== */
129 /* ======================================================================== */
130 
131 /* You will have to implement these functions */
132 
133 /* read/write functions called by the CPU to access memory.
134  * while values used are 32 bits, only the appropriate number
135  * of bits are relevant (i.e. in write_memory_8, only the lower 8 bits
136  * of value should be written to memory).
137  *
138  * NOTE: I have separated the immediate and PC-relative memory fetches
139  *       from the other memory fetches because some systems require
140  *       differentiation between PROGRAM and DATA fetches (usually
141  *       for security setups such as encryption).
142  *       This separation can either be achieved by setting
143  *       M68K_SEPARATE_READS in m68kconf.h and defining
144  *       the read functions, or by setting M68K_EMULATE_FC and
145  *       making a function code callback function.
146  *       Using the callback offers better emulation coverage
147  *       because you can also monitor whether the CPU is in SYSTEM or
148  *       USER mode, but it is also slower.
149  */
150 
151 /* Read from anywhere */
152 unsigned int  m68k_read_memory_8(unsigned int address);
153 unsigned int  m68k_read_memory_16(unsigned int address);
154 unsigned int  m68k_read_memory_32(unsigned int address);
155 
156 /* Read data immediately following the PC */
157 unsigned int  m68k_read_immediate_16(unsigned int address);
158 unsigned int  m68k_read_immediate_32(unsigned int address);
159 
160 /* Read data relative to the PC */
161 unsigned int  m68k_read_pcrelative_8(unsigned int address);
162 unsigned int  m68k_read_pcrelative_16(unsigned int address);
163 unsigned int  m68k_read_pcrelative_32(unsigned int address);
164 
165 /* Memory access for the disassembler */
166 unsigned int m68k_read_disassembler_8  (unsigned int address);
167 unsigned int m68k_read_disassembler_16 (unsigned int address);
168 unsigned int m68k_read_disassembler_32 (unsigned int address);
169 
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);
174 
175 /* Special call to simulate undocumented 68k behavior when move.l with a
176  * predecrement destination mode is executed.
177  * To simulate real 68k behavior, first write the high word to
178  * [address+2], and then write the low word to [address].
179  *
180  * Enable this functionality with M68K_SIMULATE_PD_WRITES in m68kconf.h.
181  */
182 void m68k_write_memory_32_pd(unsigned int address, unsigned int value);
183 
184 
185 
186 /* ======================================================================== */
187 /* ============================== CALLBACKS =============================== */
188 /* ======================================================================== */
189 
190 /* These functions allow you to set callbacks to the host when specific events
191  * occur.  Note that you must enable the corresponding value in m68kconf.h
192  * in order for these to do anything useful.
193  * Note: I have defined default callbacks which are used if you have enabled
194  * the corresponding #define in m68kconf.h but either haven't assigned a
195  * callback or have assigned a callback of NULL.
196  */
197 
198 /* Set the callback for an interrupt acknowledge.
199  * You must enable M68K_EMULATE_INT_ACK in m68kconf.h.
200  * The CPU will call the callback with the interrupt level being acknowledged.
201  * The host program must return either a vector from 0x02-0xff, or one of the
202  * special interrupt acknowledge values specified earlier in this header.
203  * If this is not implemented, the CPU will always assume an autovectored
204  * interrupt, and will automatically clear the interrupt request when it
205  * services the interrupt.
206  * Default behavior: return M68K_INT_ACK_AUTOVECTOR.
207  */
208 void m68k_set_int_ack_callback(int  (*callback)(int int_level));
209 
210 
211 /* Set the callback for a breakpoint acknowledge (68010+).
212  * You must enable M68K_EMULATE_BKPT_ACK in m68kconf.h.
213  * The CPU will call the callback with whatever was in the data field of the
214  * BKPT instruction for 68020+, or 0 for 68010.
215  * Default behavior: do nothing.
216  */
217 void m68k_set_bkpt_ack_callback(void (*callback)(unsigned int data));
218 
219 
220 /* Set the callback for the RESET instruction.
221  * You must enable M68K_EMULATE_RESET in m68kconf.h.
222  * The CPU calls this callback every time it encounters a RESET instruction.
223  * Default behavior: do nothing.
224  */
225 void m68k_set_reset_instr_callback(void  (*callback)(void));
226 
227 
228 /* Set the callback for the CMPI.L #v, Dn instruction.
229  * You must enable M68K_CMPILD_HAS_CALLBACK in m68kconf.h.
230  * The CPU calls this callback every time it encounters a CMPI.L #v, Dn instruction.
231  * Default behavior: do nothing.
232  */
233 void m68k_set_cmpild_instr_callback(void  (*callback)(unsigned int val, int reg));
234 
235 
236 /* Set the callback for the RTE instruction.
237  * You must enable M68K_RTE_HAS_CALLBACK in m68kconf.h.
238  * The CPU calls this callback every time it encounters a RTE instruction.
239  * Default behavior: do nothing.
240  */
241 void m68k_set_rte_instr_callback(void  (*callback)(void));
242 
243 /* Set the callback for the TAS instruction.
244  * You must enable M68K_TAS_HAS_CALLBACK in m68kconf.h.
245  * The CPU calls this callback every time it encounters a TAS instruction.
246  * Default behavior: return 1, allow writeback.
247  */
248 void m68k_set_tas_instr_callback(int  (*callback)(void));
249 
250 
251 
252 /* Set the callback for informing of a large PC change.
253  * You must enable M68K_MONITOR_PC in m68kconf.h.
254  * The CPU calls this callback with the new PC value every time the PC changes
255  * by a large value (currently set for changes by longwords).
256  * Default behavior: do nothing.
257  */
258 void m68k_set_pc_changed_callback(void  (*callback)(unsigned int new_pc));
259 
260 
261 /* Set the callback for CPU function code changes.
262  * You must enable M68K_EMULATE_FC in m68kconf.h.
263  * The CPU calls this callback with the function code before every memory
264  * access to set the CPU's function code according to what kind of memory
265  * access it is (supervisor/user, program/data and such).
266  * Default behavior: do nothing.
267  */
268 void m68k_set_fc_callback(void  (*callback)(unsigned int new_fc));
269 
270 
271 /* Set a callback for the instruction cycle of the CPU.
272  * You must enable M68K_INSTRUCTION_HOOK in m68kconf.h.
273  * The CPU calls this callback just before fetching the opcode in the
274  * instruction cycle.
275  * Default behavior: do nothing.
276  */
277 void m68k_set_instr_hook_callback(void  (*callback)(unsigned int pc));
278 
279 
280 
281 /* ======================================================================== */
282 /* ====================== FUNCTIONS TO ACCESS THE CPU ===================== */
283 /* ======================================================================== */
284 
285 /* Use this function to set the CPU type you want to emulate.
286  * Currently supported types are: M68K_CPU_TYPE_68000, M68K_CPU_TYPE_68008,
287  * M68K_CPU_TYPE_68010, M68K_CPU_TYPE_EC020, and M68K_CPU_TYPE_68020.
288  */
289 void m68k_set_cpu_type(unsigned int cpu_type);
290 
291 /* Do whatever initialisations the core requires.  Should be called
292  * at least once at init time.
293  */
294 void m68k_init(void);
295 
296 /* Pulse the RESET pin on the CPU.
297  * You *MUST* reset the CPU at least once to initialize the emulation
298  * Note: If you didn't call m68k_set_cpu_type() before resetting
299  *       the CPU for the first time, the CPU will be set to
300  *       M68K_CPU_TYPE_68000.
301  */
302 void m68k_pulse_reset(void);
303 
304 /* execute num_cycles worth of instructions.  returns number of cycles used */
305 int m68k_execute(int num_cycles);
306 int m68k_executeMD(int num_cycles);     /* slight modification for Megadrive */
307 void m68k_megadrive_sr_checkint_mode(int onoff); /* enable check cycles before servicing interrupts in m68ki_set_sr() */
308 
309 /* These functions let you read/write/modify the number of cycles left to run
310  * while m68k_execute() is running.
311  * These are useful if the 68k accesses a memory-mapped port on another device
312  * that requires immediate processing by another CPU.
313  */
314 int m68k_cycles_run(void);              /* Number of cycles run so far */
315 int m68k_cycles_remaining(void);        /* Number of cycles left */
316 void m68k_cycles_remaining_set(int cycles); /* for megadrive, similar to modify_timeslice() but without changing initial cycles */
317 void m68k_modify_timeslice(int cycles); /* Modify cycles left */
318 void m68k_end_timeslice(void);          /* End timeslice now */
319 void m68k_burn_until_irq(int enabled);  /* Sleep until INT (speedhacks) */
320 
321 /* Set the IPL0-IPL2 pins on the CPU (IRQ).
322  * A transition from < 7 to 7 will cause a non-maskable interrupt (NMI).
323  * Setting IRQ to 0 will clear an interrupt request.
324  */
325 void m68k_set_irq(unsigned int int_level);
326 void m68k_set_irq_delay(unsigned int int_level);
327 
328 /* Set the virtual irq lines, where the highest level
329  * active line is automatically selected.  If you use this function,
330  * do not use m68k_set_irq.
331  */
332 void m68k_set_virq(unsigned int level, unsigned int active);
333 unsigned int m68k_get_virq(unsigned int level);
334 
335 /* For Megadrive -dink */
336 int m68k_check_shouldinterrupt(void);
337 
338 /* Halt the CPU as if you pulsed the HALT pin. */
339 void m68k_pulse_halt(void);
340 
341 
342 /* Context switching to allow multiple CPUs */
343 
344 /* Get the size of the cpu context in bytes */
345 unsigned int m68k_context_size(void);
346 
347 /* Get the size of the cpu context without the pointer-block, for savestates */
348 unsigned int m68k_context_size_no_pointers(void);
349 
350 /* Get a cpu context */
351 unsigned int m68k_get_context(void* dst);
352 
353 /* set the current cpu context */
354 void m68k_set_context(void* dst);
355 
356 /* Register the CPU state information */
357 void m68k_state_register(const char *type, int index);
358 
359 
360 /* Peek at the internals of a CPU context.  This can either be a context
361  * retrieved using m68k_get_context() or the currently running context.
362  * If context is NULL, the currently running CPU context will be used.
363  */
364 unsigned int m68k_get_reg(void* context, m68k_register_t reg);
365 
366 /* Poke values into the internals of the currently running CPU context */
367 void m68k_set_reg(m68k_register_t reg, unsigned int value);
368 
369 /* Check if an instruction is valid for the specified CPU type */
370 unsigned int m68k_is_valid_instruction(unsigned int instruction, unsigned int cpu_type);
371 
372 /* Disassemble 1 instruction using the epecified CPU type at pc.  Stores
373  * disassembly in str_buff and returns the size of the instruction in bytes.
374  */
375 unsigned int m68k_disassemble(char* str_buff, unsigned int pc, unsigned int cpu_type);
376 
377 /* Same as above but accepts raw opcode data directly rather than fetching
378  * via the read/write interfaces.
379  */
380 unsigned int m68k_disassemble_raw(char* str_buff, unsigned int pc, const unsigned char* opdata, const unsigned char* argdata, unsigned int cpu_type);
381 
382 
383 /* ======================================================================== */
384 /* ============================= CONFIGURATION ============================ */
385 /* ======================================================================== */
386 
387 /* Import the configuration for this build */
388 #include "m68kconf.h"
389 
390 
391 /* ======================================================================== */
392 /* ============================== END OF FILE ============================= */
393 /* ======================================================================== */
394 
395 #ifdef __cplusplus
396  }
397 #endif
398 
399 #endif /* M68K__HEADER */
400