]> Shamusworld >> Repos - virtualjaguar/blob - src/m68000/m68kinterface.c
Compilation fixes for the M68000 core.
[virtualjaguar] / src / m68000 / m68kinterface.c
1 //
2 // m68kinterface.c: Code interface to the UAE 68000 core and support code
3 //
4 // by James L. Hammons
5 // (C) 2011 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  10/28/2011  Created this file ;-)
12 //
13
14 #include "m68kinterface.h"
15 #include "cpudefs.h"
16 #include "inlines.h"
17 #include "cpuextra.h"
18 #include "readcpu.h"
19
20
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
37
38 // These are found in obj/cpustbl.c (generated by gencpu)
39
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.  */
46
47 // Externs, supplied by the user...
48 extern int irq_ack_handler(int);
49
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);
57
58 // Local "Global" vars
59 static int32_t initialCycles;
60 cpuop_func * cpuFunctionTable[65536];
61
62
63 #if 0
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
69
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
77 #endif
78
79 #define CPU_DEBUG
80 void Dasm(uint32_t offset, uint32_t qt)
81 {
82 #ifdef CPU_DEBUG
83         static char buffer[2048];//, mem[64];
84         int pc = offset, oldpc;
85         uint32_t i;
86
87         for(i=0; i<qt; i++)
88         {
89 /*              oldpc = pc;
90                 for(int j=0; j<64; j++)
91                         mem[j^0x01] = jaguar_byte_read(pc + j);
92
93                 pc += Dasm68000((char *)mem, buffer, 0);
94                 WriteLog("%08X: %s\n", oldpc, buffer);//*/
95                 oldpc = pc;
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);//*/
99         }
100 #endif
101 }
102
103
104 void m68k_set_cpu_type(unsigned int type)
105 {
106 }
107
108 // Pulse the RESET line on the CPU
109 void m68k_pulse_reset(void)
110 {
111         static uint32_t emulation_initialized = 0;
112
113         // The first call to this function initializes the opcode handler jump table
114         if (!emulation_initialized)
115         {
116 #if 0
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);
124 #else
125                 // Build opcode handler table here...
126                 read_table68k();
127                 do_merges();
128                 BuildCPUFunctionTable();
129 #endif
130                 emulation_initialized = 1;
131         }
132
133 //      if (CPU_TYPE == 0)      /* KW 990319 */
134 //              m68k_set_cpu_type(M68K_CPU_TYPE_68000);
135
136 #if 0
137         /* Clear all stop levels and eat up all remaining cycles */
138         CPU_STOPPED = 0;
139         SET_CYCLES(0);
140
141         /* Turn off tracing */
142         FLAG_T1 = FLAG_T0 = 0;
143         m68ki_clear_trace();
144         /* Interrupt mask to level 7 */
145         FLAG_INT_MASK = 0x0700;
146         /* Reset VBR */
147         REG_VBR = 0;
148         /* Go to supervisor mode */
149         m68ki_set_sm_flag(SFLAG_SET | MFLAG_CLEAR);
150
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 */
156
157         /* Read the initial stack pointer and program counter */
158         m68ki_jump(0);
159         REG_SP = m68ki_read_imm_32();
160         REG_PC = m68ki_read_imm_32();
161         m68ki_jump(REG_PC);
162 #else
163         regs.stopped = 0;
164         regs.remainingCycles = 0;
165         
166         regs.intmask = 0x07;
167         regs.s = 1;                                                             // Supervisor mode ON
168
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);
173 #endif
174 }
175
176 int m68k_execute(int num_cycles)
177 {
178 #if 0
179         /* Make sure we're not stopped */
180         if (CPU_STOPPED)
181         {
182                 /* We get here if the CPU is stopped or halted */
183                 SET_CYCLES(0);
184                 CPU_INT_CYCLES = 0;
185
186                 return num_cycles;
187         }
188 #else
189         if (regs.stopped)
190         {
191                 regs.remainingCycles = 0;       // int32_t
192                 regs.interruptCycles = 0;       // uint32_t
193
194                 return num_cycles;
195         }
196 #endif
197
198 #if 0
199         /* Set our pool of clock cycles available */
200         SET_CYCLES(num_cycles);
201         m68ki_initial_cycles = num_cycles;
202
203         /* ASG: update cycles */
204         USE_CYCLES(CPU_INT_CYCLES);
205         CPU_INT_CYCLES = 0;
206
207         /* Return point if we had an address error */
208         m68ki_set_address_error_trap(); /* auto-disable (see m68kcpu.h) */
209 #else
210         regs.remainingCycles = num_cycles;
211         /*int32_t*/ initialCycles = num_cycles;
212         
213         regs.remainingCycles -= regs.interruptCycles;
214         regs.interruptCycles = 0;
215 #endif
216
217         /* Main loop.  Keep going until we run out of clock cycles */
218         do
219         {
220 #if 0
221                 /* Set tracing accodring to T1. (T0 is done inside instruction) */
222                 m68ki_trace_t1(); /* auto-disable (see m68kcpu.h) */
223
224                 /* Set the address space for reads */
225                 m68ki_use_data_space(); /* auto-disable (see m68kcpu.h) */
226
227                 /* Call external hook to peek at CPU */
228                 m68ki_instr_hook(); /* auto-disable (see m68kcpu.h) */
229
230                 /* Record previous program counter */
231                 REG_PPC = REG_PC;
232
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]);
237
238                 /* Trace m68k_exception, if necessary */
239                 m68ki_exception_if_trace(); /* auto-disable (see m68kcpu.h) */
240 #else
241 //Testing Hover Strike...
242 #if 0
243 //Dasm(regs.pc, 1);
244 static int hitCount = 0;
245 static int inRoutine = 0;
246 static int instSeen;
247
248 //if (regs.pc == 0x80340A)
249 if (regs.pc == 0x803416)
250 {
251         hitCount++;
252         inRoutine = 1;
253         instSeen = 0;
254         printf("%i: $80340A start. A0=%08X, A1=%08X ", hitCount, regs.regs[8], regs.regs[9]);
255 }
256 else if (regs.pc == 0x803422)
257 {
258         inRoutine = 0;
259         printf("(%i instructions)\n", instSeen);
260 }
261
262 if (inRoutine)
263         instSeen++;
264 #endif
265                 uint32_t opcode = get_iword(0);
266                 int32_t cycles = (int32_t)(*cpuFunctionTable[opcode])(opcode);
267                 regs.remainingCycles -= cycles;
268 //printf("Executed opcode $%04X (%i cycles)...\n", opcode, cycles);
269 #endif
270         }
271 #if 0
272         while (GET_CYCLES() > 0);
273 #else
274         while (regs.remainingCycles > 0);
275 #endif
276
277 #if 0
278         /* set previous PC to current PC for the next entry into the loop */
279         REG_PPC = REG_PC;
280
281         /* ASG: update cycles */
282         USE_CYCLES(CPU_INT_CYCLES);
283         CPU_INT_CYCLES = 0;
284
285         /* return how many clocks we used */
286         return m68ki_initial_cycles - GET_CYCLES();
287 #else
288         regs.remainingCycles -= regs.interruptCycles;
289         regs.interruptCycles = 0;
290
291         // Return # of clock cycles used
292         return initialCycles - regs.remainingCycles;
293 #endif
294 }
295
296 /* ASG: rewrote so that the int_level is a mask of the IPL0/IPL1/IPL2 bits */
297 void m68k_set_irq(unsigned int intLevel)
298 {
299 #if 0
300         uint old_level = CPU_INT_LEVEL;
301         CPU_INT_LEVEL = int_level << 8;
302
303         /* A transition from < 7 to 7 always interrupts (NMI) */
304         /* Note: Level 7 can also level trigger like a normal IRQ */
305         if(old_level != 0x0700 && CPU_INT_LEVEL == 0x0700)
306                 m68ki_exception_interrupt(7); /* Edge triggered level 7 (NMI) */
307         else
308                 m68ki_check_interrupts(); /* Level triggered (IRQ) */
309 #else
310         int oldLevel = regs.intLevel;
311         regs.intLevel = intLevel;
312
313         // A transition from < 7 to 7 always interrupts (NMI)
314         // Note: Level 7 can also level trigger like a normal IRQ
315         if (oldLevel != 0x07 && regs.intLevel == 0x07)
316                 m68ki_exception_interrupt(7);           // Edge triggered level 7 (NMI)
317         else
318                 m68ki_check_interrupts();                       // Level triggered (IRQ)
319 #endif
320 }
321
322 // Check for interrupts
323 STATIC_INLINE void m68ki_check_interrupts(void)
324 {
325 #if 0
326         if(CPU_INT_LEVEL > FLAG_INT_MASK)
327                 m68ki_exception_interrupt(CPU_INT_LEVEL>>8);
328 #else
329         if (regs.intLevel > regs.intmask)
330                 m68ki_exception_interrupt(regs.intLevel);
331 #endif
332 }
333
334 // Service an interrupt request and start exception processing
335 void m68ki_exception_interrupt(uint32_t intLevel)
336 {
337 #if 0
338         uint vector;
339         uint sr;
340         uint new_pc;
341
342         /* Turn off the stopped state */
343         CPU_STOPPED &= ~STOP_LEVEL_STOP;
344
345         /* If we are halted, don't do anything */
346         if(CPU_STOPPED)
347                 return;
348
349         /* Acknowledge the interrupt */
350         vector = m68ki_int_ack(int_level);
351
352         /* Get the interrupt vector */
353         if(vector == M68K_INT_ACK_AUTOVECTOR)
354                 /* Use the autovectors.  This is the most commonly used implementation */
355                 vector = EXCEPTION_INTERRUPT_AUTOVECTOR+int_level;
356         else if(vector == M68K_INT_ACK_SPURIOUS)
357                 /* Called if no devices respond to the interrupt acknowledge */
358                 vector = EXCEPTION_SPURIOUS_INTERRUPT;
359         else if(vector > 255)
360         {
361                 M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: Interrupt acknowledge returned invalid vector $%x\n",
362                          m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PC), vector));
363                 return;
364         }
365
366         /* Start exception processing */
367         sr = m68ki_init_exception();
368
369         /* Set the interrupt mask to the level of the one being serviced */
370         FLAG_INT_MASK = int_level<<8;
371
372         /* Get the new PC */
373         new_pc = m68ki_read_data_32((vector<<2) + REG_VBR);
374
375         /* If vector is uninitialized, call the uninitialized interrupt vector */
376         if(new_pc == 0)
377                 new_pc = m68ki_read_data_32((EXCEPTION_UNINITIALIZED_INTERRUPT<<2) + REG_VBR);
378
379         /* Generate a stack frame */
380         m68ki_stack_frame_0000(REG_PC, sr, vector);
381
382         if(FLAG_M && CPU_TYPE_IS_EC020_PLUS(CPU_TYPE))
383         {
384                 /* Create throwaway frame */
385                 m68ki_set_sm_flag(FLAG_S);      /* clear M */
386                 sr |= 0x2000; /* Same as SR in master stack frame except S is forced high */
387                 m68ki_stack_frame_0001(REG_PC, sr, vector);
388         }
389
390         m68ki_jump(new_pc);
391
392         /* Defer cycle counting until later */
393         CPU_INT_CYCLES += CYC_EXCEPTION[vector];
394
395 #if !M68K_EMULATE_INT_ACK
396         /* Automatically clear IRQ if we are not using an acknowledge scheme */
397         CPU_INT_LEVEL = 0;
398 #endif /* M68K_EMULATE_INT_ACK */
399 #else
400         // Turn off the stopped state
401         regs.stopped = 0;
402
403 //JLH: need to add halt state?
404         // If we are halted, don't do anything
405 //      if (CPU_STOPPED)
406 //              return;
407
408         // Acknowledge the interrupt (NOTE: This is a user supplied function!)
409         uint32_t vector = irq_ack_handler(intLevel);
410
411         // Get the interrupt vector
412         if (vector == M68K_INT_ACK_AUTOVECTOR)
413                 // Use the autovectors.  This is the most commonly used implementation
414                 vector = EXCEPTION_INTERRUPT_AUTOVECTOR + intLevel;
415         else if (vector == M68K_INT_ACK_SPURIOUS)
416                 // Called if no devices respond to the interrupt acknowledge
417                 vector = EXCEPTION_SPURIOUS_INTERRUPT;
418         else if (vector > 255)
419         {
420 //              M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: Interrupt acknowledge returned invalid vector $%x\n",
421 //                       m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PC), vector));
422                 return;
423         }
424
425         // Start exception processing
426         uint32_t sr = m68ki_init_exception();
427
428         // Set the interrupt mask to the level of the one being serviced
429         regs.intmask = intLevel;
430
431         // Get the new PC
432         uint32_t newPC = m68k_read_memory_32(vector << 2);
433
434         // If vector is uninitialized, call the uninitialized interrupt vector
435         if (newPC == 0)
436                 newPC = m68k_read_memory_32(EXCEPTION_UNINITIALIZED_INTERRUPT << 2);
437
438         // Generate a stack frame
439         m68ki_stack_frame_3word(regs.pc, sr);
440
441         m68k_setpc(newPC);
442
443         // Defer cycle counting until later
444         regs.interruptCycles += 56;     // NOT ACCURATE-- !!! FIX !!!
445 //      CPU_INT_CYCLES += CYC_EXCEPTION[vector];
446 #endif
447 }
448
449 // Initiate exception processing
450 STATIC_INLINE uint32_t m68ki_init_exception(void)
451 {
452 #if 0
453         /* Save the old status register */
454         uint sr = m68ki_get_sr();
455
456         /* Turn off trace flag, clear pending traces */
457         FLAG_T1 = FLAG_T0 = 0;
458         m68ki_clear_trace();
459         /* Enter supervisor mode */
460         m68ki_set_s_flag(SFLAG_SET);
461
462         return sr;
463 #else
464         MakeSR();
465         uint32_t sr = regs.sr;                                  // Save old status register
466         regs.s = 1;                                                             // Set supervisor mode
467
468         return sr;
469 #endif
470 }
471
472 // 3 word stack frame (68000 only)
473 STATIC_INLINE void m68ki_stack_frame_3word(uint32_t pc, uint32_t sr)
474 {
475 #if 0
476         m68ki_push_32(pc);
477         m68ki_push_16(sr);
478 #else
479         // Push PC on stack:
480         m68k_areg(regs, 7) -= 4;
481         m68k_write_memory_32(m68k_areg(regs, 7), pc);
482         // Push SR on stack:
483         m68k_areg(regs, 7) -= 2;
484         m68k_write_memory_16(m68k_areg(regs, 7), sr);
485 #endif
486 }
487
488 unsigned int m68k_get_reg(void * context, m68k_register_t reg)
489 {
490         if (reg <= M68K_REG_A7)
491                 return regs.regs[reg];
492         else if (reg == M68K_REG_PC)
493                 return regs.pc;
494         else if (reg == M68K_REG_SR)
495         {
496                 MakeSR();
497                 return regs.sr;
498         }
499         else if (reg == M68K_REG_SP)
500                 return regs.regs[15];
501
502         return 0;
503 }
504
505 void m68k_set_reg(m68k_register_t reg, unsigned int value)
506 {
507         if (reg <= M68K_REG_A7)
508                 regs.regs[reg] = value;
509         else if (reg == M68K_REG_PC)
510                 regs.pc = value;
511         else if (reg == M68K_REG_SR)
512         {
513                 regs.sr = value;
514                 MakeFromSR();
515         }
516         else if (reg == M68K_REG_SP)
517                 regs.regs[15] = value;
518 }
519
520 //
521 // Check if the instruction is a valid one
522 //
523 unsigned int m68k_is_valid_instruction(unsigned int instruction, unsigned int cpu_type)
524 {
525         instruction &= 0xFFFF;
526
527         if (cpuFunctionTable[instruction] == IllegalOpcode)
528                 return 0;
529
530         return 1;
531 }
532
533 // Dummy functions, for now, until we prove the concept here. :-)
534
535 // Temp, while we're using the Musashi disassembler...
536 #if 0
537 unsigned int m68k_disassemble(char * str_buff, unsigned int pc, unsigned int cpu_type)
538 {
539         return 0;
540 }
541 #endif
542
543 int m68k_cycles_run(void) {}              /* Number of cycles run so far */
544 int m68k_cycles_remaining(void) {}        /* Number of cycles left */
545 void m68k_modify_timeslice(int cycles) {} /* Modify cycles left */
546 //void m68k_end_timeslice(void) {}          /* End timeslice now */
547
548 void m68k_end_timeslice(void)
549 {
550 #if 0
551         m68ki_initial_cycles = GET_CYCLES();
552         SET_CYCLES(0);
553 #else
554         initialCycles = regs.remainingCycles;
555         regs.remainingCycles = 0;
556 #endif
557 }
558
559
560 unsigned long IllegalOpcode(uint32_t opcode)
561 {
562 #if 0
563         uint32_t pc = m68k_getpc ();
564 #endif
565         if ((opcode & 0xF000) == 0xF000)
566         {
567                 Exception(0x0B, 0, M68000_EXC_SRC_CPU); // LineF exception...
568                 return 4;
569         }
570         else if ((opcode & 0xF000) == 0xA000)
571         {
572                 Exception(0x0A, 0, M68000_EXC_SRC_CPU); // LineA exception...
573                 return 4;
574         }
575
576 #if 0
577         write_log ("Illegal instruction: %04x at %08lx\n", opcode, (long)pc);
578 #endif
579
580         Exception(0x04, 0, M68000_EXC_SRC_CPU);         // Illegal opcode exception...
581         return 4;
582 }
583
584
585 void BuildCPUFunctionTable(void)
586 {
587         int i;
588         unsigned long opcode;
589
590         // We're only using the "fast" 68000 emulation here, not the "compatible"
591 #if 0
592         const struct cputbl * tbl = (currprefs.cpu_compatible
593                 ? op_smalltbl_5_ff : op_smalltbl_4_ff);
594 #else
595         const struct cputbl * tbl = op_smalltbl_4_ff;
596 #endif
597
598 //      Log_Printf(LOG_DEBUG, "Building CPU function table (%d %d %d).\n",
599 //              currprefs.cpu_level, currprefs.cpu_compatible, currprefs.address_space_24);
600
601         // Set all instructions to Illegal...
602         for(opcode=0; opcode<65536; opcode++)
603                 cpuFunctionTable[opcode] = IllegalOpcode;
604
605         // Move functions from compact table into our full function table...
606         for(i=0; tbl[i].handler!=NULL; i++)
607                 cpuFunctionTable[tbl[i].opcode] = tbl[i].handler;
608
609 //JLH: According to readcpu.c, handler is set to -1 and never changes.
610 // Actually, it does read this crap in readcpu.c, do_merges() does it... :-P
611 // Again, seems like a build time thing could be done here...
612 #if 1
613         for(opcode=0; opcode<65536; opcode++)
614         {
615 //              if (table68k[opcode].mnemo == i_ILLG || table68k[opcode].clev > currprefs.cpu_level)
616                 if (table68k[opcode].mnemo == i_ILLG || table68k[opcode].clev > 0)
617                         continue;
618
619                 if (table68k[opcode].handler != -1)
620                 {
621 //printf("Relocate: $%04X->$%04X\n", table68k[opcode].handler, opcode);
622                         cpuop_func * f = cpuFunctionTable[table68k[opcode].handler];
623
624                         if (f == IllegalOpcode)
625                                 abort();
626
627                         cpuFunctionTable[opcode] = f;
628                 }
629         }
630 #endif
631 }