--- hatari/src/uae-cpu/newcpu.c 2019/04/01 07:10:24 1.1.1.5 +++ hatari/src/uae-cpu/newcpu.c 2019/04/01 07:11:01 1.1.1.7 @@ -7,23 +7,25 @@ * * Adaptation to Hatari by Thomas Huth * + * This file is distributed under the GNU Public License, version 2 or at + * your option any later version. Read the file gpl.txt for details. */ +static char rcsid[] = "Hatari $Id: newcpu.c,v 1.1.1.7 2019/04/01 07:11:01 root Exp $"; #include "sysdeps.h" #include "hatari-glue.h" #include "maccess.h" #include "memory.h" #include "newcpu.h" -#include "compiler.h" #include "events.h" #include "../includes/main.h" +#include "../includes/m68000.h" #include "../includes/tos.h" #include "../includes/vdi.h" #include "../includes/cart.h" #include "../includes/debugui.h" - struct flag_struct regflags; int lastInstructionCycles; /* how many cycles last instruction took to execute */ @@ -48,6 +50,7 @@ int fpp_movem_next[256]; cpuop_func *cpufunctbl[65536]; + #define COUNT_INSTRS 0 #if COUNT_INSTRS @@ -102,20 +105,11 @@ void dump_counts (void) int broken_in; -static __inline__ unsigned int cft_map (unsigned int f) -{ -#ifndef HAVE_GET_WORD_UNSWAPPED - return f; -#else - return ((f >> 8) & 255) | ((f & 255) << 8); -#endif -} - static unsigned long op_illg_1 (uae_u32 opcode) REGPARAM; static unsigned long REGPARAM2 op_illg_1 (uae_u32 opcode) { - op_illg (cft_map (opcode)); + op_illg (opcode); return 4; } @@ -135,10 +129,10 @@ void build_cpufunctbl(void) cpu_level, cpu_compatible, address_space_24); for (opcode = 0; opcode < 65536; opcode++) - cpufunctbl[cft_map(opcode)] = op_illg_1; + cpufunctbl[opcode] = op_illg_1; for (i = 0; tbl[i].handler != NULL; i++) { if (! tbl[i].specific) - cpufunctbl[cft_map (tbl[i].opcode)] = tbl[i].handler; + cpufunctbl[tbl[i].opcode] = tbl[i].handler; } for (opcode = 0; opcode < 65536; opcode++) { cpuop_func *f; @@ -147,23 +141,22 @@ void build_cpufunctbl(void) continue; if (table68k[opcode].handler != -1) { - f = cpufunctbl[cft_map (table68k[opcode].handler)]; + f = cpufunctbl[table68k[opcode].handler]; if (f == op_illg_1) abort(); - cpufunctbl[cft_map(opcode)] = f; + cpufunctbl[opcode] = f; } } for (i = 0; tbl[i].handler != NULL; i++) { if (tbl[i].specific) - cpufunctbl[cft_map(tbl[i].opcode)] = tbl[i].handler; + cpufunctbl[tbl[i].opcode] = tbl[i].handler; } - /* Hataris illegal opcodes: */ - cpufunctbl[cft_map(GEMDOS_OPCODE)] = OpCode_GemDos; - cpufunctbl[cft_map(RUNOLDGEMDOS_OPCODE)] = OpCode_OldGemDos; - cpufunctbl[cft_map(CONDRV_OPCODE)] = OpCode_ConnectedDrive; - cpufunctbl[cft_map(TIMERD_OPCODE)] = OpCode_TimerD; - cpufunctbl[cft_map(VDI_OPCODE)] = OpCode_VDI; + /* Hatari's illegal opcodes: */ + cpufunctbl[GEMDOS_OPCODE] = OpCode_GemDos; + cpufunctbl[RUNOLDGEMDOS_OPCODE] = OpCode_OldGemDos; + cpufunctbl[SYSINIT_OPCODE] = OpCode_SysInit; + cpufunctbl[VDI_OPCODE] = OpCode_VDI; } @@ -230,7 +223,7 @@ void init_m68k (void) if (cpu_compatible) write_log (" (compatible mode)"); write_log ("\n"); - + read_table68k (); do_merges (); @@ -669,7 +662,9 @@ void MakeFromSR (void) if (regs.t1 || regs.t0) set_special (SPCFLAG_TRACE); else - unset_special (SPCFLAG_TRACE | SPCFLAG_DOTRACE); + /* Keep SPCFLAG_DOTRACE, we still want a trace exception for + SR-modifying instructions (including STOP). */ + unset_special (SPCFLAG_TRACE); } @@ -679,40 +674,19 @@ void Exception(int nr, uaecptr oldpc) /*if( nr>=2 && nr<10 ) fprintf(stderr,"Exception (-> %i bombs)!\n",nr);*/ - /* Intercept exceptions... - FIXME: Find a better way to do this! */ - if(bUseVDIRes) + /* Intercept VDI exception (Trap #2 with D0 = 0x73) */ + if(bUseVDIRes && nr == 0x22 && regs.regs[0] == 0x73) { - if(nr == 0x22) /* Trap 2 - intercept VDI call */ - { - if( !VDI() ) - { - /* Set 'PC' as address of 'VDI_OPCODE' illegal instruction - * This will call OpCode_VDI after completion of Trap call! - * Use to modify return structure from VDI */ - /*if (bUseVDIRes)*/ - { - VDI_OldPC = currpc; - currpc = CART_VDI_OPCODE_ADDR; - } - } - } - else if(nr == 0x0a) /* Line A */ + if(!VDI()) { - if((get_word(currpc)&0x0fff) == 0x0ff) /* 0xA0FF opcode? */ - { - /* we use this to get pointer to Line-A structure details - * (to fix for extended VDI res) */ - LineABase = regs.regs[0]; /* D0 */ - FontBase = regs.regs[9]; /* A1 */ - VDI_LineA(); - m68k_setpc(currpc + 2); - fill_prefetch_0(); - return; - } + /* Set 'PC' as address of 'VDI_OPCODE' illegal instruction + * This will call OpCode_VDI after completion of Trap call! + * Use to modify return structure from VDI */ + VDI_OldPC = currpc; + currpc = CART_VDI_OPCODE_ADDR; } } - compiler_flush_jsr_stack(); MakeSR(); if (!regs.s) { @@ -764,10 +738,13 @@ void Exception(int nr, uaecptr oldpc) put_word (m68k_areg(regs, 7), regs.sr); /* 68000 bus/address errors: */ + /* Well these 8 more bytes are not here just for debuging. + It seems adebug expects them to be on the stack when it receives + a bus error... */ if (cpu_level==0 && (nr==2 || nr==3)) { m68k_areg(regs, 7) -= 8; if (nr == 3) { /* Address error */ - put_word (m68k_areg(regs, 7), regs.sr); /*?*/ + put_word (m68k_areg(regs, 7), 0); /* FIXME: Add real function code value */ put_long (m68k_areg(regs, 7)+2, last_fault_for_exception_3); put_word (m68k_areg(regs, 7)+6, last_op_for_exception_3); put_long (m68k_areg(regs, 7)+10, last_addr_for_exception_3); @@ -777,11 +754,11 @@ void Exception(int nr, uaecptr oldpc) } } else { /* Bus error */ - put_word (m68k_areg(regs, 7), regs.sr); /*?*/ + put_word (m68k_areg(regs, 7), 0); /* FIXME: Add real function code value */ put_long (m68k_areg(regs, 7)+2, BusAddressLocation); - put_word (m68k_areg(regs, 7)+6, get_word(currpc)); + put_word (m68k_areg(regs, 7)+6, BusErrorOpcode); if( bEnableDebug && BusAddressLocation!=0xff8a00) { - fprintf(stderr,"Bus Error at address $%lx, PC=$%lx\n",BusAddressLocation,(long)currpc); + fprintf(stderr,"Bus Error at address $%x, PC=$%lx\n",BusAddressLocation,(long)currpc); DebugUI(); } } @@ -791,8 +768,40 @@ void Exception(int nr, uaecptr oldpc) fill_prefetch_0 (); regs.t1 = regs.t0 = regs.m = 0; unset_special (SPCFLAG_TRACE | SPCFLAG_DOTRACE); + + /* Handle exception cycles: */ + if(nr >= 24 && nr <= 31) + { + ADD_CYCLES(44+4, 5, 3); /* Interrupt */ + } + else if(nr >= 32 && nr <= 47) + { + ADD_CYCLES(34, 5, 3); /* Trap */ + } + else switch(nr) + { + case 2: ADD_CYCLES(50, 4, 7); break; /* Bus error */ + case 3: ADD_CYCLES(50, 4, 7); break; /* Address error */ + case 4: ADD_CYCLES(34, 4, 3); break; /* Illegal instruction */ + case 5: ADD_CYCLES(38, 4, 3); break; /* Div by zero */ + case 6: ADD_CYCLES(40, 4, 3); break; /* CHK */ + case 7: ADD_CYCLES(34, 5, 3); break; /* TRAPV */ + case 8: ADD_CYCLES(34, 4, 3); break; /* Privilege violation */ + case 9: ADD_CYCLES(34, 4, 3); break; /* Trace */ + case 10: ADD_CYCLES(34, 4, 3); break; /* Line-A - probably wrong */ + case 11: ADD_CYCLES(34, 4, 3); break; /* Line-F - probably wrong */ + default: +#if 0 /* Hatari currently seems to run more instable when adding MFP cycles */ + if(nr < 64) + ADD_CYCLES(0, 0, 0); /* Coprocessor and unassigned exceptions (???) */ + else + ADD_CYCLES(44+4, 5, 3); /* Must be a MFP interrupt */ +#endif + break; + } } + static void Interrupt(int nr) { assert(nr < 8 && nr >= 0); @@ -804,7 +813,9 @@ static void Interrupt(int nr) set_special (SPCFLAG_INT); } -static uae_u32 caar, cacr, itt0, itt1, dtt0, dtt1, tc, mmusr; + +static uae_u32 caar, cacr, itt0, itt1, dtt0, dtt1, tc, mmusr, urp, srp; + int m68k_move2c (int regno, uae_u32 *regp) { @@ -825,12 +836,15 @@ int m68k_move2c (int regno, uae_u32 *reg case 5: itt1 = *regp & 0xffffe364; break; case 6: dtt0 = *regp & 0xffffe364; break; case 7: dtt1 = *regp & 0xffffe364; break; - + case 0x800: regs.usp = *regp; break; case 0x801: regs.vbr = *regp; break; case 0x802: caar = *regp & 0xfc; break; case 0x803: regs.msp = *regp; if (regs.m == 1) m68k_areg(regs, 7) = regs.msp; break; case 0x804: regs.isp = *regp; if (regs.m == 0) m68k_areg(regs, 7) = regs.isp; break; + case 0x805: mmusr = *regp; break; + case 0x806: urp = *regp; break; + case 0x807: srp = *regp; break; default: op_illg (0x4E7B); return 0; @@ -863,6 +877,8 @@ int m68k_movec2 (int regno, uae_u32 *reg case 0x803: *regp = regs.m == 1 ? m68k_areg(regs, 7) : regs.msp; break; case 0x804: *regp = regs.m == 0 ? m68k_areg(regs, 7) : regs.isp; break; case 0x805: *regp = mmusr; break; + case 0x806: *regp = urp; break; + case 0x807: *regp = srp; break; default: op_illg (0x4E7A); return 0; @@ -1122,15 +1138,13 @@ void m68k_mull (uae_u32 opcode, uae_u32 } #endif } + static char* ccnames[] = { "T ","F ","HI","LS","CC","CS","NE","EQ", "VC","VS","PL","MI","GE","LT","GT","LE" }; void m68k_reset (void) { - m68k_areg(regs, 7) = get_long(0); - m68k_setpc(get_long(4)); - fill_prefetch_0 (); regs.s = 1; regs.m = 0; regs.stopped = 0; @@ -1141,75 +1155,38 @@ void m68k_reset (void) SET_CFLG (0); SET_VFLG (0); SET_NFLG (0); - regs.spcflags = 0; + regs.spcflags &= SPCFLAG_MODE_CHANGE; /* Clear specialflags except mode-change */ regs.intmask = 7; regs.vbr = regs.sfc = regs.dfc = 0; regs.fpcr = regs.fpsr = regs.fpiar = 0; + + m68k_areg(regs, 7) = get_long(0); + m68k_setpc(get_long(4)); + refill_prefetch (m68k_getpc(), 0); } unsigned long REGPARAM2 op_illg (uae_u32 opcode) { uaecptr pc = m68k_getpc (); -/* - if (cloanto_rom && (opcode & 0xF100) == 0x7100) { - m68k_dreg (regs, (opcode >> 9) & 7) = (uae_s8)(opcode & 0xFF); - m68k_incpc (2); - fill_prefetch_0 (); - return 4; - } -*/ - compiler_flush_jsr_stack (); - if (opcode == 0x4E7B && get_long (0x10) == 0 ) - { + + if (opcode == 0x4E7B && get_long (0x10) == 0 ) { write_log ("This program requires a 68020 CPU!\n"); broken_in = 1; set_special (SPCFLAG_BRK); bQuitProgram = 1; - } -/* - if (opcode == 0xFF0D) { - if ((pc & 0xF80000) == 0xF80000) { - // This is from the dummy Kickstart replacement - uae_u16 arg = get_iword (2); - m68k_incpc (4); - ersatz_perform (arg); - fill_prefetch_0 (); - return 4; - } else if ((pc & 0xF80000) == 0xF00000) { - // User-mode STOP replacement - m68k_setstopped (1); - return 4; - } - } -*/ -/* - if ((opcode & 0xF000) == 0xA000 && (pc & 0xF80000) == 0xF00000) { - // Calltrap. - m68k_incpc(2); - call_calltrap (opcode & 0xFFF); - fill_prefetch_0 (); - return 4; } -*/ - if ((opcode & 0xF000) == 0xF000) - { + + if ((opcode & 0xF000) == 0xF000) { Exception(0xB,0); return 4; - } - if ((opcode & 0xF000) == 0xA000) - { -/* - if ((pc & 0xF80000) == 0xF00000) { - // Calltrap. - call_calltrap (opcode & 0xFFF); - } -*/ + } + if ((opcode & 0xF000) == 0xA000) { Exception(0xA,0); return 4; - } + } #if 0 - write_log ("Illegal instruction: %04x at %08lx\n", opcode, pc); + write_log ("Illegal instruction: %04x at %08lx\n", opcode, (long)pc); #endif Exception (4,0); return 4; @@ -1269,37 +1246,44 @@ static void do_trace (void) static int do_specialties (void) { - run_compiled_code(); + if(regs.spcflags & SPCFLAG_BUSERROR) { + /* We can not execute bus errors directly in the memory handler + * functions since the PC should point to the address of the next + * instruction, so we're executing the bus errors here: */ + unset_special(SPCFLAG_BUSERROR); + Exception(2,0); + } + if (regs.spcflags & SPCFLAG_DOTRACE) { Exception (9,last_trace_ad); } while (regs.spcflags & SPCFLAG_STOP) { - do_cycles (4); + do_cycles (8); if (regs.intmask>5) { /* We still have to care about events when IPL==7 ! */ Main_EventHandler(); if(bQuitProgram) unset_special(SPCFLAG_STOP); } if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)) { - /*int intr = intlev ();*/ + int intr = intlev (); unset_special (SPCFLAG_INT | SPCFLAG_DOINT); - /*if (intr != -1 && intr > regs.intmask) { + if (intr != -1 && intr > regs.intmask) { Interrupt (intr); regs.stopped = 0; unset_special (SPCFLAG_STOP); - }*/ + } } } if (regs.spcflags & SPCFLAG_TRACE) do_trace (); if (regs.spcflags & SPCFLAG_DOINT) { - /*int intr = intlev ();*/ + int intr = intlev (); unset_special (SPCFLAG_DOINT); - /*if (intr != -1 && intr > regs.intmask) { + if (intr != -1 && intr > regs.intmask) { Interrupt (intr); regs.stopped = 0; - }*/ + } } if (regs.spcflags & SPCFLAG_INT) { unset_special (SPCFLAG_INT); @@ -1346,7 +1330,7 @@ static void m68k_run_1 (void) instrcount[opcode]++; #endif - cycles = (*cpufunctbl[cft_map(opcode)])(opcode); + cycles = (*cpufunctbl[opcode])(opcode); #ifdef DEBUG_PREFETCH if (memcmp (saved_bytes, oldpcp, 20) != 0) { @@ -1370,11 +1354,7 @@ static void m68k_run_2 (void) { while(!bQuitProgram) { int cycles; -#ifdef HAVE_GET_WORD_UNSWAPPED - uae_u32 opcode = do_get_mem_word_unswapped (regs.pc_p); -#else uae_u32 opcode = get_iword (0); -#endif /*m68k_dumpstate(stderr, NULL);*/ /*m68k_disasm(stderr, m68k_getpc (), NULL, 1);*/ @@ -1388,7 +1368,7 @@ static void m68k_run_2 (void) instrcount[opcode]++; #endif - cycles = (*cpufunctbl[cft_map(opcode)])(opcode); + cycles = (*cpufunctbl[opcode])(opcode); do_cycles (cycles); if (regs.spcflags) { @@ -1427,7 +1407,7 @@ static void m68k_verify (uaecptr addr, u last_op_for_exception_3 = opcode; m68kpc_offset = 2; - if (cpufunctbl[cft_map(opcode)] == op_illg_1) { + if (cpufunctbl[opcode] == op_illg_1) { opcode = 0x4AFC; } dp = table68k + opcode; @@ -1462,7 +1442,7 @@ void m68k_disasm (FILE *f, uaecptr addr, } opcode = get_iword_1 (m68kpc_offset); m68kpc_offset += 2; - if (cpufunctbl[cft_map(opcode)] == op_illg_1) { + if (cpufunctbl[opcode] == op_illg_1) { opcode = 0x4AFC; } dp = table68k + opcode;