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