2 // m68kinterface.c: Code interface to the UAE 68000 core and support code
5 // (C) 2011 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- -------------------------------------------------------------
11 // JLH 10/28/2011 Created this file ;-)
14 #include "m68kinterface.h"
21 // Exception Vectors handled by emulation
22 #define EXCEPTION_BUS_ERROR 2 /* This one is not emulated! */
23 #define EXCEPTION_ADDRESS_ERROR 3 /* This one is partially emulated (doesn't stack a proper frame yet) */
24 #define EXCEPTION_ILLEGAL_INSTRUCTION 4
25 #define EXCEPTION_ZERO_DIVIDE 5
26 #define EXCEPTION_CHK 6
27 #define EXCEPTION_TRAPV 7
28 #define EXCEPTION_PRIVILEGE_VIOLATION 8
29 #define EXCEPTION_TRACE 9
30 #define EXCEPTION_1010 10
31 #define EXCEPTION_1111 11
32 #define EXCEPTION_FORMAT_ERROR 14
33 #define EXCEPTION_UNINITIALIZED_INTERRUPT 15
34 #define EXCEPTION_SPURIOUS_INTERRUPT 24
35 #define EXCEPTION_INTERRUPT_AUTOVECTOR 24
36 #define EXCEPTION_TRAP_BASE 32
38 // These are found in obj/cpustbl.c (generated by gencpu)
40 //extern const struct cputbl op_smalltbl_0_ff[]; /* 68040 */
41 //extern const struct cputbl op_smalltbl_1_ff[]; /* 68020 + 68881 */
42 //extern const struct cputbl op_smalltbl_2_ff[]; /* 68020 */
43 //extern const struct cputbl op_smalltbl_3_ff[]; /* 68010 */
44 extern const struct cputbl op_smalltbl_4_ff[]; /* 68000 */
45 extern const struct cputbl op_smalltbl_5_ff[]; /* 68000 slow but compatible. */
47 // Externs, supplied by the user...
48 extern int irq_ack_handler(int);
50 // Function prototypes...
51 STATIC_INLINE void m68ki_check_interrupts(void);
52 void m68ki_exception_interrupt(uint32_t intLevel);
53 STATIC_INLINE uint32_t m68ki_init_exception(void);
54 STATIC_INLINE void m68ki_stack_frame_3word(uint32_t pc, uint32_t sr);
55 unsigned long IllegalOpcode(uint32_t opcode);
56 void BuildCPUFunctionTable(void);
58 // Local "Global" vars
59 static int32_t initialCycles;
60 cpuop_func * cpuFunctionTable[65536];
64 #define ADD_CYCLES(A) m68ki_remaining_cycles += (A)
65 #define USE_CYCLES(A) m68ki_remaining_cycles -= (A)
66 #define SET_CYCLES(A) m68ki_remaining_cycles = A
67 #define GET_CYCLES() m68ki_remaining_cycles
68 #define USE_ALL_CYCLES() m68ki_remaining_cycles = 0
70 #define CPU_INT_LEVEL m68ki_cpu.int_level /* ASG: changed from CPU_INTS_PENDING */
71 #define CPU_INT_CYCLES m68ki_cpu.int_cycles /* ASG */
72 #define CPU_STOPPED m68ki_cpu.stopped
73 #define CPU_PREF_ADDR m68ki_cpu.pref_addr
74 #define CPU_PREF_DATA m68ki_cpu.pref_data
75 #define CPU_ADDRESS_MASK m68ki_cpu.address_mask
76 #define CPU_SR_MASK m68ki_cpu.sr_mask
80 void Dasm(uint32_t offset, uint32_t qt)
83 static char buffer[2048];//, mem[64];
84 int pc = offset, oldpc;
90 for(int j=0; j<64; j++)
91 mem[j^0x01] = jaguar_byte_read(pc + j);
93 pc += Dasm68000((char *)mem, buffer, 0);
94 WriteLog("%08X: %s\n", oldpc, buffer);//*/
96 pc += m68k_disassemble(buffer, pc, 0);//M68K_CPU_TYPE_68000);
97 // WriteLog("%08X: %s\n", oldpc, buffer);//*/
98 printf("%08X: %s\n", oldpc, buffer);//*/
104 void m68k_set_cpu_type(unsigned int type)
108 // Pulse the RESET line on the CPU
109 void m68k_pulse_reset(void)
111 static uint32_t emulation_initialized = 0;
113 // The first call to this function initializes the opcode handler jump table
114 if (!emulation_initialized)
117 m68ki_build_opcode_table();
118 m68k_set_int_ack_callback(NULL);
119 m68k_set_bkpt_ack_callback(NULL);
120 m68k_set_reset_instr_callback(NULL);
121 m68k_set_pc_changed_callback(NULL);
122 m68k_set_fc_callback(NULL);
123 m68k_set_instr_hook_callback(NULL);
125 // Build opcode handler table here...
128 BuildCPUFunctionTable();
130 emulation_initialized = 1;
133 // if (CPU_TYPE == 0) /* KW 990319 */
134 // m68k_set_cpu_type(M68K_CPU_TYPE_68000);
137 /* Clear all stop levels and eat up all remaining cycles */
141 /* Turn off tracing */
142 FLAG_T1 = FLAG_T0 = 0;
144 /* Interrupt mask to level 7 */
145 FLAG_INT_MASK = 0x0700;
148 /* Go to supervisor mode */
149 m68ki_set_sm_flag(SFLAG_SET | MFLAG_CLEAR);
151 /* Invalidate the prefetch queue */
152 #if M68K_EMULATE_PREFETCH
153 /* Set to arbitrary number since our first fetch is from 0 */
154 CPU_PREF_ADDR = 0x1000;
155 #endif /* M68K_EMULATE_PREFETCH */
157 /* Read the initial stack pointer and program counter */
159 REG_SP = m68ki_read_imm_32();
160 REG_PC = m68ki_read_imm_32();
164 regs.remainingCycles = 0;
167 regs.s = 1; // Supervisor mode ON
169 // Read initial SP and PC
170 m68k_areg(regs, 7) = m68k_read_memory_32(0);
171 m68k_setpc(m68k_read_memory_32(4));
172 refill_prefetch(m68k_getpc(), 0);
176 int m68k_execute(int num_cycles)
179 /* Make sure we're not stopped */
182 /* We get here if the CPU is stopped or halted */
191 regs.remainingCycles = 0; // int32_t
192 regs.interruptCycles = 0; // uint32_t
199 /* Set our pool of clock cycles available */
200 SET_CYCLES(num_cycles);
201 m68ki_initial_cycles = num_cycles;
203 /* ASG: update cycles */
204 USE_CYCLES(CPU_INT_CYCLES);
207 /* Return point if we had an address error */
208 m68ki_set_address_error_trap(); /* auto-disable (see m68kcpu.h) */
210 regs.remainingCycles = num_cycles;
211 /*int32_t*/ initialCycles = num_cycles;
213 regs.remainingCycles -= regs.interruptCycles;
214 regs.interruptCycles = 0;
217 /* Main loop. Keep going until we run out of clock cycles */
221 /* Set tracing accodring to T1. (T0 is done inside instruction) */
222 m68ki_trace_t1(); /* auto-disable (see m68kcpu.h) */
224 /* Set the address space for reads */
225 m68ki_use_data_space(); /* auto-disable (see m68kcpu.h) */
227 /* Call external hook to peek at CPU */
228 m68ki_instr_hook(); /* auto-disable (see m68kcpu.h) */
230 /* Record previous program counter */
233 /* Read an instruction and call its handler */
234 REG_IR = m68ki_read_imm_16();
235 m68ki_instruction_jump_table[REG_IR]();
236 USE_CYCLES(CYC_INSTRUCTION[REG_IR]);
238 /* Trace m68k_exception, if necessary */
239 m68ki_exception_if_trace(); /* auto-disable (see m68kcpu.h) */
241 //Testing Hover Strike...
244 static int hitCount = 0;
245 static int inRoutine = 0;
248 //if (regs.pc == 0x80340A)
249 if (regs.pc == 0x803416)
254 printf("%i: $80340A start. A0=%08X, A1=%08X ", hitCount, regs.regs[8], regs.regs[9]);
256 else if (regs.pc == 0x803422)
259 printf("(%i instructions)\n", instSeen);
265 uint32_t opcode = get_iword(0);
266 //if ((opcode & 0xFFF8) == 0x31C0)
268 // printf("MOVE.W D%i, EA\n", opcode & 0x07);
270 int32_t cycles = (int32_t)(*cpuFunctionTable[opcode])(opcode);
271 regs.remainingCycles -= cycles;
272 //printf("Executed opcode $%04X (%i cycles)...\n", opcode, cycles);
276 while (GET_CYCLES() > 0);
278 while (regs.remainingCycles > 0);
282 /* set previous PC to current PC for the next entry into the loop */
285 /* ASG: update cycles */
286 USE_CYCLES(CPU_INT_CYCLES);
289 /* return how many clocks we used */
290 return m68ki_initial_cycles - GET_CYCLES();
292 regs.remainingCycles -= regs.interruptCycles;
293 regs.interruptCycles = 0;
295 // Return # of clock cycles used
296 return initialCycles - regs.remainingCycles;
300 /* ASG: rewrote so that the int_level is a mask of the IPL0/IPL1/IPL2 bits */
301 void m68k_set_irq(unsigned int intLevel)
304 uint old_level = CPU_INT_LEVEL;
305 CPU_INT_LEVEL = int_level << 8;
307 /* A transition from < 7 to 7 always interrupts (NMI) */
308 /* Note: Level 7 can also level trigger like a normal IRQ */
309 if(old_level != 0x0700 && CPU_INT_LEVEL == 0x0700)
310 m68ki_exception_interrupt(7); /* Edge triggered level 7 (NMI) */
312 m68ki_check_interrupts(); /* Level triggered (IRQ) */
314 int oldLevel = regs.intLevel;
315 regs.intLevel = intLevel;
317 // A transition from < 7 to 7 always interrupts (NMI)
318 // Note: Level 7 can also level trigger like a normal IRQ
319 if (oldLevel != 0x07 && regs.intLevel == 0x07)
320 m68ki_exception_interrupt(7); // Edge triggered level 7 (NMI)
322 m68ki_check_interrupts(); // Level triggered (IRQ)
326 // Check for interrupts
327 STATIC_INLINE void m68ki_check_interrupts(void)
330 if(CPU_INT_LEVEL > FLAG_INT_MASK)
331 m68ki_exception_interrupt(CPU_INT_LEVEL>>8);
333 if (regs.intLevel > regs.intmask)
334 m68ki_exception_interrupt(regs.intLevel);
338 // Service an interrupt request and start exception processing
339 void m68ki_exception_interrupt(uint32_t intLevel)
346 /* Turn off the stopped state */
347 CPU_STOPPED &= ~STOP_LEVEL_STOP;
349 /* If we are halted, don't do anything */
353 /* Acknowledge the interrupt */
354 vector = m68ki_int_ack(int_level);
356 /* Get the interrupt vector */
357 if(vector == M68K_INT_ACK_AUTOVECTOR)
358 /* Use the autovectors. This is the most commonly used implementation */
359 vector = EXCEPTION_INTERRUPT_AUTOVECTOR+int_level;
360 else if(vector == M68K_INT_ACK_SPURIOUS)
361 /* Called if no devices respond to the interrupt acknowledge */
362 vector = EXCEPTION_SPURIOUS_INTERRUPT;
363 else if(vector > 255)
365 M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: Interrupt acknowledge returned invalid vector $%x\n",
366 m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PC), vector));
370 /* Start exception processing */
371 sr = m68ki_init_exception();
373 /* Set the interrupt mask to the level of the one being serviced */
374 FLAG_INT_MASK = int_level<<8;
377 new_pc = m68ki_read_data_32((vector<<2) + REG_VBR);
379 /* If vector is uninitialized, call the uninitialized interrupt vector */
381 new_pc = m68ki_read_data_32((EXCEPTION_UNINITIALIZED_INTERRUPT<<2) + REG_VBR);
383 /* Generate a stack frame */
384 m68ki_stack_frame_0000(REG_PC, sr, vector);
386 if(FLAG_M && CPU_TYPE_IS_EC020_PLUS(CPU_TYPE))
388 /* Create throwaway frame */
389 m68ki_set_sm_flag(FLAG_S); /* clear M */
390 sr |= 0x2000; /* Same as SR in master stack frame except S is forced high */
391 m68ki_stack_frame_0001(REG_PC, sr, vector);
396 /* Defer cycle counting until later */
397 CPU_INT_CYCLES += CYC_EXCEPTION[vector];
399 #if !M68K_EMULATE_INT_ACK
400 /* Automatically clear IRQ if we are not using an acknowledge scheme */
402 #endif /* M68K_EMULATE_INT_ACK */
404 // Turn off the stopped state
407 //JLH: need to add halt state?
408 // If we are halted, don't do anything
412 // Acknowledge the interrupt (NOTE: This is a user supplied function!)
413 uint32_t vector = irq_ack_handler(intLevel);
415 // Get the interrupt vector
416 if (vector == M68K_INT_ACK_AUTOVECTOR)
417 // Use the autovectors. This is the most commonly used implementation
418 vector = EXCEPTION_INTERRUPT_AUTOVECTOR + intLevel;
419 else if (vector == M68K_INT_ACK_SPURIOUS)
420 // Called if no devices respond to the interrupt acknowledge
421 vector = EXCEPTION_SPURIOUS_INTERRUPT;
422 else if (vector > 255)
424 // M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: Interrupt acknowledge returned invalid vector $%x\n",
425 // m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PC), vector));
429 // Start exception processing
430 uint32_t sr = m68ki_init_exception();
432 // Set the interrupt mask to the level of the one being serviced
433 regs.intmask = intLevel;
436 uint32_t newPC = m68k_read_memory_32(vector << 2);
438 // If vector is uninitialized, call the uninitialized interrupt vector
440 newPC = m68k_read_memory_32(EXCEPTION_UNINITIALIZED_INTERRUPT << 2);
442 // Generate a stack frame
443 m68ki_stack_frame_3word(regs.pc, sr);
447 // Defer cycle counting until later
448 regs.interruptCycles += 56; // NOT ACCURATE-- !!! FIX !!!
449 // CPU_INT_CYCLES += CYC_EXCEPTION[vector];
453 // Initiate exception processing
454 STATIC_INLINE uint32_t m68ki_init_exception(void)
457 /* Save the old status register */
458 uint sr = m68ki_get_sr();
460 /* Turn off trace flag, clear pending traces */
461 FLAG_T1 = FLAG_T0 = 0;
463 /* Enter supervisor mode */
464 m68ki_set_s_flag(SFLAG_SET);
469 uint32_t sr = regs.sr; // Save old status register
470 regs.s = 1; // Set supervisor mode
476 // 3 word stack frame (68000 only)
477 STATIC_INLINE void m68ki_stack_frame_3word(uint32_t pc, uint32_t sr)
484 m68k_areg(regs, 7) -= 4;
485 m68k_write_memory_32(m68k_areg(regs, 7), pc);
487 m68k_areg(regs, 7) -= 2;
488 m68k_write_memory_16(m68k_areg(regs, 7), sr);
492 unsigned int m68k_get_reg(void * context, m68k_register_t reg)
494 if (reg <= M68K_REG_A7)
495 return regs.regs[reg];
496 else if (reg == M68K_REG_PC)
498 else if (reg == M68K_REG_SR)
503 else if (reg == M68K_REG_SP)
504 return regs.regs[15];
509 void m68k_set_reg(m68k_register_t reg, unsigned int value)
511 if (reg <= M68K_REG_A7)
512 regs.regs[reg] = value;
513 else if (reg == M68K_REG_PC)
515 else if (reg == M68K_REG_SR)
520 else if (reg == M68K_REG_SP)
521 regs.regs[15] = value;
525 // Check if the instruction is a valid one
527 unsigned int m68k_is_valid_instruction(unsigned int instruction, unsigned int cpu_type)
529 instruction &= 0xFFFF;
531 if (cpuFunctionTable[instruction] == IllegalOpcode)
537 // Dummy functions, for now, until we prove the concept here. :-)
539 // Temp, while we're using the Musashi disassembler...
541 unsigned int m68k_disassemble(char * str_buff, unsigned int pc, unsigned int cpu_type)
547 int m68k_cycles_run(void) {} /* Number of cycles run so far */
548 int m68k_cycles_remaining(void) {} /* Number of cycles left */
549 void m68k_modify_timeslice(int cycles) {} /* Modify cycles left */
550 //void m68k_end_timeslice(void) {} /* End timeslice now */
552 void m68k_end_timeslice(void)
555 m68ki_initial_cycles = GET_CYCLES();
558 initialCycles = regs.remainingCycles;
559 regs.remainingCycles = 0;
564 unsigned long IllegalOpcode(uint32_t opcode)
567 uint32_t pc = m68k_getpc ();
569 if ((opcode & 0xF000) == 0xF000)
571 Exception(0x0B, 0, M68000_EXC_SRC_CPU); // LineF exception...
574 else if ((opcode & 0xF000) == 0xA000)
576 Exception(0x0A, 0, M68000_EXC_SRC_CPU); // LineA exception...
581 write_log ("Illegal instruction: %04x at %08lx\n", opcode, (long)pc);
584 Exception(0x04, 0, M68000_EXC_SRC_CPU); // Illegal opcode exception...
589 void BuildCPUFunctionTable(void)
592 unsigned long opcode;
594 // We're only using the "fast" 68000 emulation here, not the "compatible"
595 // ("fast" doesn't throw exceptions, so we're using "compatible" now :-P)
597 const struct cputbl * tbl = (currprefs.cpu_compatible
598 ? op_smalltbl_5_ff : op_smalltbl_4_ff);
600 //let's try "compatible" and see what happens here...
601 // const struct cputbl * tbl = op_smalltbl_4_ff;
602 const struct cputbl * tbl = op_smalltbl_5_ff;
605 // Log_Printf(LOG_DEBUG, "Building CPU function table (%d %d %d).\n",
606 // currprefs.cpu_level, currprefs.cpu_compatible, currprefs.address_space_24);
608 // Set all instructions to Illegal...
609 for(opcode=0; opcode<65536; opcode++)
610 cpuFunctionTable[opcode] = IllegalOpcode;
612 // Move functions from compact table into our full function table...
613 for(i=0; tbl[i].handler!=NULL; i++)
614 cpuFunctionTable[tbl[i].opcode] = tbl[i].handler;
616 //JLH: According to readcpu.c, handler is set to -1 and never changes.
617 // Actually, it does read this crap in readcpu.c, do_merges() does it... :-P
618 // Again, seems like a build time thing could be done here...
620 for(opcode=0; opcode<65536; opcode++)
622 // if (table68k[opcode].mnemo == i_ILLG || table68k[opcode].clev > currprefs.cpu_level)
623 if (table68k[opcode].mnemo == i_ILLG || table68k[opcode].clev > 0)
626 if (table68k[opcode].handler != -1)
628 //printf("Relocate: $%04X->$%04X\n", table68k[opcode].handler, opcode);
629 cpuop_func * f = cpuFunctionTable[table68k[opcode].handler];
631 if (f == IllegalOpcode)
634 cpuFunctionTable[opcode] = f;