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