]> Shamusworld >> Repos - virtualjaguar/blob - src/m68000/m68kinterface.c
dd5dfc1e74f1b7aea3880294f3621b9bdf2807a2
[virtualjaguar] / src / m68000 / m68kinterface.c
1 //
2 // m68kinterface.c: Code interface to the UAE 68000 core and support code
3 //
4 // by James Hammons
5 // (C) 2011 Underground Software
6 //
7 // JLH = James 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 <pthread.h>
16 #include "cpudefs.h"
17 #include "inlines.h"
18 #include "cpuextra.h"
19 #include "readcpu.h"
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 void m68k_set_irq2(unsigned int intLevel);
58
59 // Local "Global" vars
60 static int32_t initialCycles;
61 cpuop_func * cpuFunctionTable[65536];
62
63 // By virtue of the fact that m68k_set_irq() can be called asychronously by
64 // another thread, we need something along the lines of this:
65 static int checkForIRQToHandle = 0;
66 //static pthread_mutex_t executionLock = PTHREAD_MUTEX_INITIALIZER;
67 static int IRQLevelToHandle = 0;
68
69 #if 0
70 #define ADD_CYCLES(A)    m68ki_remaining_cycles += (A)
71 #define USE_CYCLES(A)    m68ki_remaining_cycles -= (A)
72 #define SET_CYCLES(A)    m68ki_remaining_cycles = A
73 #define GET_CYCLES()     m68ki_remaining_cycles
74 #define USE_ALL_CYCLES() m68ki_remaining_cycles = 0
75
76 #define CPU_INT_LEVEL    m68ki_cpu.int_level /* ASG: changed from CPU_INTS_PENDING */
77 #define CPU_INT_CYCLES   m68ki_cpu.int_cycles /* ASG */
78 #define CPU_STOPPED      m68ki_cpu.stopped
79 #define CPU_PREF_ADDR    m68ki_cpu.pref_addr
80 #define CPU_PREF_DATA    m68ki_cpu.pref_data
81 #define CPU_ADDRESS_MASK m68ki_cpu.address_mask
82 #define CPU_SR_MASK      m68ki_cpu.sr_mask
83 #endif
84
85 #define CPU_DEBUG
86
87
88 void Dasm(uint32_t offset, uint32_t qt)
89 {
90 #ifdef CPU_DEBUG
91 // back up a few instructions...
92 //offset -= 100;
93         static char buffer[2048];//, mem[64];
94         int pc = offset, oldpc;
95         uint32_t i;
96
97         for(i=0; i<qt; i++)
98         {
99 /*              oldpc = pc;
100                 for(int j=0; j<64; j++)
101                         mem[j^0x01] = jaguar_byte_read(pc + j);
102
103                 pc += Dasm68000((char *)mem, buffer, 0);
104                 WriteLog("%08X: %s\n", oldpc, buffer);//*/
105                 oldpc = pc;
106                 pc += m68k_disassemble(buffer, pc, 0);//M68K_CPU_TYPE_68000);
107 //              WriteLog("%08X: %s\n", oldpc, buffer);//*/
108                 printf("%08X: %s\n", oldpc, buffer);//*/
109         }
110 #endif
111 }
112
113
114 #ifdef CPU_DEBUG
115 void DumpRegisters(void)
116 {
117         uint32_t i;
118
119         for(i=0; i<16; i++)
120         {
121                 printf("%s%i: %08X ", (i < 8 ? "D" : "A"), i & 0x7, regs.regs[i]);
122
123                 if ((i & 0x03) == 3)
124                         printf("\n");
125         }
126 }
127 #endif
128
129
130 void m68k_set_cpu_type(unsigned int type)
131 {
132 }
133
134
135 // Pulse the RESET line on the CPU
136 void m68k_pulse_reset(void)
137 {
138         static uint32_t emulation_initialized = 0;
139
140         // The first call to this function initializes the opcode handler jump table
141         if (!emulation_initialized)
142         {
143 #if 0
144                 m68ki_build_opcode_table();
145                 m68k_set_int_ack_callback(NULL);
146                 m68k_set_bkpt_ack_callback(NULL);
147                 m68k_set_reset_instr_callback(NULL);
148                 m68k_set_pc_changed_callback(NULL);
149                 m68k_set_fc_callback(NULL);
150                 m68k_set_instr_hook_callback(NULL);
151 #else
152                 // Build opcode handler table here...
153                 read_table68k();
154                 do_merges();
155                 BuildCPUFunctionTable();
156 #endif
157                 emulation_initialized = 1;
158         }
159
160 //      if (CPU_TYPE == 0)      /* KW 990319 */
161 //              m68k_set_cpu_type(M68K_CPU_TYPE_68000);
162
163 #if 0
164         /* Clear all stop levels and eat up all remaining cycles */
165         CPU_STOPPED = 0;
166         SET_CYCLES(0);
167
168         /* Turn off tracing */
169         FLAG_T1 = FLAG_T0 = 0;
170         m68ki_clear_trace();
171         /* Interrupt mask to level 7 */
172         FLAG_INT_MASK = 0x0700;
173         /* Reset VBR */
174         REG_VBR = 0;
175         /* Go to supervisor mode */
176         m68ki_set_sm_flag(SFLAG_SET | MFLAG_CLEAR);
177
178         /* Invalidate the prefetch queue */
179 #if M68K_EMULATE_PREFETCH
180         /* Set to arbitrary number since our first fetch is from 0 */
181         CPU_PREF_ADDR = 0x1000;
182 #endif /* M68K_EMULATE_PREFETCH */
183
184         /* Read the initial stack pointer and program counter */
185         m68ki_jump(0);
186         REG_SP = m68ki_read_imm_32();
187         REG_PC = m68ki_read_imm_32();
188         m68ki_jump(REG_PC);
189 #else
190         regs.stopped = 0;
191         regs.remainingCycles = 0;
192         
193         regs.intmask = 0x07;
194         regs.s = 1;                                                             // Supervisor mode ON
195
196         // Read initial SP and PC
197         m68k_areg(regs, 7) = m68k_read_memory_32(0);
198         m68k_setpc(m68k_read_memory_32(4));
199         refill_prefetch(m68k_getpc(), 0);
200 #endif
201 }
202
203
204 int m68k_execute(int num_cycles)
205 {
206         if (regs.stopped)
207         {
208                 regs.remainingCycles = 0;       // int32_t
209                 regs.interruptCycles = 0;       // uint32_t
210
211                 return num_cycles;
212         }
213
214 #if 0
215         /* Set our pool of clock cycles available */
216         SET_CYCLES(num_cycles);
217         m68ki_initial_cycles = num_cycles;
218
219         /* ASG: update cycles */
220         USE_CYCLES(CPU_INT_CYCLES);
221         CPU_INT_CYCLES = 0;
222
223         /* Return point if we had an address error */
224         m68ki_set_address_error_trap(); /* auto-disable (see m68kcpu.h) */
225 #else
226         regs.remainingCycles = num_cycles;
227         /*int32_t*/ initialCycles = num_cycles;
228         
229         regs.remainingCycles -= regs.interruptCycles;
230         regs.interruptCycles = 0;
231 #endif
232
233         /* Main loop.  Keep going until we run out of clock cycles */
234         do
235         {
236 #if 0
237                 /* Set tracing accodring to T1. (T0 is done inside instruction) */
238                 m68ki_trace_t1(); /* auto-disable (see m68kcpu.h) */
239
240                 /* Set the address space for reads */
241                 m68ki_use_data_space(); /* auto-disable (see m68kcpu.h) */
242
243                 /* Call external hook to peek at CPU */
244                 m68ki_instr_hook(); /* auto-disable (see m68kcpu.h) */
245
246                 /* Record previous program counter */
247                 REG_PPC = REG_PC;
248
249                 /* Read an instruction and call its handler */
250                 REG_IR = m68ki_read_imm_16();
251                 m68ki_instruction_jump_table[REG_IR]();
252                 USE_CYCLES(CYC_INSTRUCTION[REG_IR]);
253
254                 /* Trace m68k_exception, if necessary */
255                 m68ki_exception_if_trace(); /* auto-disable (see m68kcpu.h) */
256 #else
257 //Testing Hover Strike...
258 #if 0
259 //Dasm(regs.pc, 1);
260 static int hitCount = 0;
261 static int inRoutine = 0;
262 static int instSeen;
263
264 //if (regs.pc == 0x80340A)
265 if (regs.pc == 0x803416)
266 {
267         hitCount++;
268         inRoutine = 1;
269         instSeen = 0;
270         printf("%i: $80340A start. A0=%08X, A1=%08X ", hitCount, regs.regs[8], regs.regs[9]);
271 }
272 else if (regs.pc == 0x803422)
273 {
274         inRoutine = 0;
275         printf("(%i instructions)\n", instSeen);
276 }
277
278 if (inRoutine)
279         instSeen++;
280 #endif
281 // AvP testing... (problem was: 32 bit addresses on 24 bit address cpu--FIXED)
282 #if 0
283         static int go = 0;
284
285         if (regs.pc == 0x94BA)
286         {
287                 go = 1;
288                 printf("\n");
289         }
290
291         if (regs.pc == 0x94C6)
292                 go = 0;
293
294 //      if (regs.regs[10] == 0xFFFFFFFF && go)
295         if (go)
296         {
297 //              printf("A2=-1, PC=%08X\n", regs.pc);
298 //              go = 0;
299 //              Dasm(regs.pc, 130);
300                 Dasm(regs.pc, 1);
301                 DumpRegisters();
302         }
303 //94BA: 2468 0000                MOVEA.L        (A0,$0000) == $0002328A, A2
304 //94BE: 200A                     MOVE.L A2, D0
305 //94C0: 6A02                     BPL.B  $94C4
306 //94C2: 2452                     MOVEA.L        (A2), A2                        ; <--- HERE
307 //94C4: 4283                     CLR.L  D3
308 #endif
309 //              pthread_mutex_lock(&executionLock);
310                 if (checkForIRQToHandle)
311                 {
312                         checkForIRQToHandle = 0;
313                         m68k_set_irq2(IRQLevelToHandle);
314                 }
315
316 #ifdef M68K_HOOK_FUNCTION
317                 M68KInstructionHook();
318 #endif
319                 uint32_t opcode = get_iword(0);
320 //if ((opcode & 0xFFF8) == 0x31C0)
321 //{
322 //      printf("MOVE.W D%i, EA\n", opcode & 0x07);
323 //}
324                 int32_t cycles = (int32_t)(*cpuFunctionTable[opcode])(opcode);
325                 regs.remainingCycles -= cycles;
326 //              pthread_mutex_unlock(&executionLock);
327
328 //printf("Executed opcode $%04X (%i cycles)...\n", opcode, cycles);
329 #endif
330         }
331         while (regs.remainingCycles > 0);
332
333 #if 0
334         /* set previous PC to current PC for the next entry into the loop */
335         REG_PPC = REG_PC;
336
337         /* ASG: update cycles */
338         USE_CYCLES(CPU_INT_CYCLES);
339         CPU_INT_CYCLES = 0;
340
341         /* return how many clocks we used */
342         return m68ki_initial_cycles - GET_CYCLES();
343 #else
344         regs.remainingCycles -= regs.interruptCycles;
345         regs.interruptCycles = 0;
346
347         // Return # of clock cycles used
348         return initialCycles - regs.remainingCycles;
349 #endif
350 }
351
352
353 void m68k_set_irq(unsigned int intLevel)
354 {
355         // We need to check for stopped state as well...
356         if (regs.stopped)
357         {
358                 m68k_set_irq2(intLevel);
359                 return;
360         }
361
362         // Since this can be called asynchronously, we need to fix it so that it
363         // doesn't fuck up the main execution loop.
364         IRQLevelToHandle = intLevel;
365         checkForIRQToHandle = 1;
366 }
367
368
369 /* ASG: rewrote so that the int_level is a mask of the IPL0/IPL1/IPL2 bits */
370 void m68k_set_irq2(unsigned int intLevel)
371 {
372 //      pthread_mutex_lock(&executionLock);
373 //              printf("m68k_set_irq: Could not get the lock!!!\n");
374
375         int oldLevel = regs.intLevel;
376         regs.intLevel = intLevel;
377
378         // A transition from < 7 to 7 always interrupts (NMI)
379         // Note: Level 7 can also level trigger like a normal IRQ
380         if (oldLevel != 0x07 && regs.intLevel == 0x07)
381                 m68ki_exception_interrupt(7);           // Edge triggered level 7 (NMI)
382         else
383                 m68ki_check_interrupts();                       // Level triggered (IRQ)
384
385 //      pthread_mutex_unlock(&executionLock);
386 }
387
388
389 // Check for interrupts
390 STATIC_INLINE void m68ki_check_interrupts(void)
391 {
392 #if 0
393         if(CPU_INT_LEVEL > FLAG_INT_MASK)
394                 m68ki_exception_interrupt(CPU_INT_LEVEL>>8);
395 #else
396         if (regs.intLevel > regs.intmask)
397                 m68ki_exception_interrupt(regs.intLevel);
398 #endif
399 }
400
401
402 // Service an interrupt request and start exception processing
403 void m68ki_exception_interrupt(uint32_t intLevel)
404 {
405 #if 0
406         uint vector;
407         uint sr;
408         uint new_pc;
409
410         /* Turn off the stopped state */
411         CPU_STOPPED &= ~STOP_LEVEL_STOP;
412
413         /* If we are halted, don't do anything */
414         if(CPU_STOPPED)
415                 return;
416
417         /* Acknowledge the interrupt */
418         vector = m68ki_int_ack(int_level);
419
420         /* Get the interrupt vector */
421         if(vector == M68K_INT_ACK_AUTOVECTOR)
422                 /* Use the autovectors.  This is the most commonly used implementation */
423                 vector = EXCEPTION_INTERRUPT_AUTOVECTOR+int_level;
424         else if(vector == M68K_INT_ACK_SPURIOUS)
425                 /* Called if no devices respond to the interrupt acknowledge */
426                 vector = EXCEPTION_SPURIOUS_INTERRUPT;
427         else if(vector > 255)
428         {
429                 M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: Interrupt acknowledge returned invalid vector $%x\n",
430                          m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PC), vector));
431                 return;
432         }
433
434         /* Start exception processing */
435         sr = m68ki_init_exception();
436
437         /* Set the interrupt mask to the level of the one being serviced */
438         FLAG_INT_MASK = int_level<<8;
439
440         /* Get the new PC */
441         new_pc = m68ki_read_data_32((vector<<2) + REG_VBR);
442
443         /* If vector is uninitialized, call the uninitialized interrupt vector */
444         if(new_pc == 0)
445                 new_pc = m68ki_read_data_32((EXCEPTION_UNINITIALIZED_INTERRUPT<<2) + REG_VBR);
446
447         /* Generate a stack frame */
448         m68ki_stack_frame_0000(REG_PC, sr, vector);
449
450         if(FLAG_M && CPU_TYPE_IS_EC020_PLUS(CPU_TYPE))
451         {
452                 /* Create throwaway frame */
453                 m68ki_set_sm_flag(FLAG_S);      /* clear M */
454                 sr |= 0x2000; /* Same as SR in master stack frame except S is forced high */
455                 m68ki_stack_frame_0001(REG_PC, sr, vector);
456         }
457
458         m68ki_jump(new_pc);
459
460         /* Defer cycle counting until later */
461         CPU_INT_CYCLES += CYC_EXCEPTION[vector];
462
463 #if !M68K_EMULATE_INT_ACK
464         /* Automatically clear IRQ if we are not using an acknowledge scheme */
465         CPU_INT_LEVEL = 0;
466 #endif /* M68K_EMULATE_INT_ACK */
467 #else
468         // Turn off the stopped state
469         regs.stopped = 0;
470
471 //JLH: need to add halt state?
472         // If we are halted, don't do anything
473 //      if (CPU_STOPPED)
474 //              return;
475
476         // Acknowledge the interrupt (NOTE: This is a user supplied function!)
477         uint32_t vector = irq_ack_handler(intLevel);
478
479         // Get the interrupt vector
480         if (vector == M68K_INT_ACK_AUTOVECTOR)
481                 // Use the autovectors.  This is the most commonly used implementation
482                 vector = EXCEPTION_INTERRUPT_AUTOVECTOR + intLevel;
483         else if (vector == M68K_INT_ACK_SPURIOUS)
484                 // Called if no devices respond to the interrupt acknowledge
485                 vector = EXCEPTION_SPURIOUS_INTERRUPT;
486         else if (vector > 255)
487         {
488 //              M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: Interrupt acknowledge returned invalid vector $%x\n",
489 //                       m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PC), vector));
490                 return;
491         }
492
493         // Start exception processing
494         uint32_t sr = m68ki_init_exception();
495
496         // Set the interrupt mask to the level of the one being serviced
497         regs.intmask = intLevel;
498
499 #if 0
500 extern int startM68KTracing;
501 if (startM68KTracing)
502 {
503         printf("IRQ: old PC=%06X, ", regs.pc);
504 }
505 #endif
506
507         // Get the new PC
508         uint32_t newPC = m68k_read_memory_32(vector << 2);
509
510 #if 0
511 if (startM68KTracing)
512 {
513         printf("new PC=%06X, vector=%u, ", newPC, vector);
514 }
515 #endif
516
517         // If vector is uninitialized, call the uninitialized interrupt vector
518         if (newPC == 0)
519                 newPC = m68k_read_memory_32(EXCEPTION_UNINITIALIZED_INTERRUPT << 2);
520
521         // Generate a stack frame
522         m68ki_stack_frame_3word(regs.pc, sr);
523
524         m68k_setpc(newPC);
525 #if 0
526 if (startM68KTracing)
527 {
528         printf("(PC=%06X)\n", regs.pc);
529 }
530 #endif
531
532         // Defer cycle counting until later
533         regs.interruptCycles += 56;     // NOT ACCURATE-- !!! FIX !!!
534 //      CPU_INT_CYCLES += CYC_EXCEPTION[vector];
535 #endif
536 }
537
538
539 // Initiate exception processing
540 STATIC_INLINE uint32_t m68ki_init_exception(void)
541 {
542 #if 0
543         /* Save the old status register */
544         uint sr = m68ki_get_sr();
545
546         /* Turn off trace flag, clear pending traces */
547         FLAG_T1 = FLAG_T0 = 0;
548         m68ki_clear_trace();
549         /* Enter supervisor mode */
550         m68ki_set_s_flag(SFLAG_SET);
551
552         return sr;
553 #else
554         MakeSR();
555         uint32_t sr = regs.sr;                                  // Save old status register
556         regs.s = 1;                                                             // Set supervisor mode
557
558         return sr;
559 #endif
560 }
561
562
563 // 3 word stack frame (68000 only)
564 STATIC_INLINE void m68ki_stack_frame_3word(uint32_t pc, uint32_t sr)
565 {
566 #if 0
567         m68ki_push_32(pc);
568         m68ki_push_16(sr);
569 #else
570         // Push PC on stack:
571         m68k_areg(regs, 7) -= 4;
572         m68k_write_memory_32(m68k_areg(regs, 7), pc);
573         // Push SR on stack:
574         m68k_areg(regs, 7) -= 2;
575         m68k_write_memory_16(m68k_areg(regs, 7), sr);
576 #endif
577 }
578
579
580 unsigned int m68k_get_reg(void * context, m68k_register_t reg)
581 {
582         if (reg <= M68K_REG_A7)
583                 return regs.regs[reg];
584         else if (reg == M68K_REG_PC)
585                 return regs.pc;
586         else if (reg == M68K_REG_SR)
587         {
588                 MakeSR();
589                 return regs.sr;
590         }
591         else if (reg == M68K_REG_SP)
592                 return regs.regs[15];
593
594         return 0;
595 }
596
597
598 void m68k_set_reg(m68k_register_t reg, unsigned int value)
599 {
600         if (reg <= M68K_REG_A7)
601                 regs.regs[reg] = value;
602         else if (reg == M68K_REG_PC)
603                 regs.pc = value;
604         else if (reg == M68K_REG_SR)
605         {
606                 regs.sr = value;
607                 MakeFromSR();
608         }
609         else if (reg == M68K_REG_SP)
610                 regs.regs[15] = value;
611 }
612
613
614 //
615 // Check if the instruction is a valid one
616 //
617 unsigned int m68k_is_valid_instruction(unsigned int instruction, unsigned int cpu_type)
618 {
619         instruction &= 0xFFFF;
620
621         if (cpuFunctionTable[instruction] == IllegalOpcode)
622                 return 0;
623
624         return 1;
625 }
626
627
628 // Dummy functions, for now, until we prove the concept here. :-)
629
630 // Temp, while we're using the Musashi disassembler...
631 #if 0
632 unsigned int m68k_disassemble(char * str_buff, unsigned int pc, unsigned int cpu_type)
633 {
634         return 0;
635 }
636 #endif
637
638 int m68k_cycles_run(void) {}              /* Number of cycles run so far */
639 int m68k_cycles_remaining(void) {}        /* Number of cycles left */
640 //void m68k_modify_timeslice(int cycles) {} /* Modify cycles left */
641 //void m68k_end_timeslice(void) {}          /* End timeslice now */
642
643
644 void m68k_modify_timeslice(int cycles)
645 {
646         regs.remainingCycles = cycles;
647 }
648
649
650 void m68k_end_timeslice(void)
651 {
652 #if 0
653         m68ki_initial_cycles = GET_CYCLES();
654         SET_CYCLES(0);
655 #else
656         initialCycles = regs.remainingCycles;
657         regs.remainingCycles = 0;
658 #endif
659 }
660
661
662 unsigned long IllegalOpcode(uint32_t opcode)
663 {
664 #if 0
665         uint32_t pc = m68k_getpc ();
666 #endif
667         if ((opcode & 0xF000) == 0xF000)
668         {
669                 Exception(0x0B, 0, M68000_EXC_SRC_CPU); // LineF exception...
670                 return 4;
671         }
672         else if ((opcode & 0xF000) == 0xA000)
673         {
674                 Exception(0x0A, 0, M68000_EXC_SRC_CPU); // LineA exception...
675                 return 4;
676         }
677
678 #if 0
679         write_log ("Illegal instruction: %04x at %08lx\n", opcode, (long)pc);
680 #endif
681
682         Exception(0x04, 0, M68000_EXC_SRC_CPU);         // Illegal opcode exception...
683         return 4;
684 }
685
686
687 void BuildCPUFunctionTable(void)
688 {
689         int i;
690         unsigned long opcode;
691
692         // We're only using the "fast" 68000 emulation here, not the "compatible"
693         // ("fast" doesn't throw exceptions, so we're using "compatible" now :-P)
694 #if 0
695         const struct cputbl * tbl = (currprefs.cpu_compatible
696                 ? op_smalltbl_5_ff : op_smalltbl_4_ff);
697 #else
698 //let's try "compatible" and see what happens here...
699 //      const struct cputbl * tbl = op_smalltbl_4_ff;
700         const struct cputbl * tbl = op_smalltbl_5_ff;
701 #endif
702
703 //      Log_Printf(LOG_DEBUG, "Building CPU function table (%d %d %d).\n",
704 //              currprefs.cpu_level, currprefs.cpu_compatible, currprefs.address_space_24);
705
706         // Set all instructions to Illegal...
707         for(opcode=0; opcode<65536; opcode++)
708                 cpuFunctionTable[opcode] = IllegalOpcode;
709
710         // Move functions from compact table into our full function table...
711         for(i=0; tbl[i].handler!=NULL; i++)
712                 cpuFunctionTable[tbl[i].opcode] = tbl[i].handler;
713
714 //JLH: According to readcpu.c, handler is set to -1 and never changes.
715 // Actually, it does read this crap in readcpu.c, do_merges() does it... :-P
716 // Again, seems like a build time thing could be done here...
717 #if 1
718         for(opcode=0; opcode<65536; opcode++)
719         {
720 //              if (table68k[opcode].mnemo == i_ILLG || table68k[opcode].clev > currprefs.cpu_level)
721                 if (table68k[opcode].mnemo == i_ILLG || table68k[opcode].clev > 0)
722                         continue;
723
724                 if (table68k[opcode].handler != -1)
725                 {
726 //printf("Relocate: $%04X->$%04X\n", table68k[opcode].handler, opcode);
727                         cpuop_func * f = cpuFunctionTable[table68k[opcode].handler];
728
729                         if (f == IllegalOpcode)
730                                 abort();
731
732                         cpuFunctionTable[opcode] = f;
733                 }
734         }
735 #endif
736 }