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