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