#define VBAPI /* Header includes */ #include #include /********************************* Constants *********************************/ /* Type sizes */ static const uint8_t TYPE_SIZES[] = { 1, 1, 2, 2, 4 }; /********************************** Macros ***********************************/ /* Sign-extend a value of some number of bits to 32 bits */ #define SignExtend(v,b) \ ((v) | (((v) & (1 << ((b) - 1))) ? (uint32_t) 0xFFFFFFFF << (b) : 0)) /*************************** Subsystem Components ****************************/ /* Component includes */ #include "bus.c" #include "cpu.c" /***************************** Module Functions ******************************/ /* Process a simulation for some number of clocks */ static int sysEmulate(VB *emu, uint32_t clocks) { int broke; broke = cpuEmulate(emu, clocks); return broke; } /* Determine the number of clocks before a break condititon could occur */ static uint32_t sysUntil(VB *emu, uint32_t clocks) { clocks = cpuUntil(emu, clocks); return clocks; } /******************************* API Functions *******************************/ /* Associate two simulations as peers */ void vbConnect(VB *emu1, VB *emu2) { /* Disconnect any existing link associations */ if (emu1->peer != NULL && emu1->peer != emu2) emu1->peer->peer = NULL; if (emu2->peer != NULL && emu2->peer != emu1) emu2->peer->peer = NULL; /* Link the two simulations */ emu1->peer = emu2; emu2->peer = emu1; } /* Disassociate linked peers */ void vbDisconnect(VB *emu) { if (emu->peer != NULL) emu->peer->peer = NULL; emu->peer = NULL; } /* Process one or two simulations */ int vbEmulate(VB *emu1, VB *emu2, uint32_t *clocks) { int broke; /* The simulation requested an application break */ uint32_t until; /* Maximum clocks before a break could happen */ /* Processing one simulaiton */ if (emu2 == NULL) { do { until = sysUntil (emu1, *clocks); broke = sysEmulate(emu1, until ); *clocks -= until; } while (!broke && *clocks > 0); } /* Processing two simulations */ else { do { until = sysUntil (emu1, *clocks); until = sysUntil (emu2, until ); broke = sysEmulate(emu1, until ); broke |= sysEmulate(emu2, until ); *clocks -= until; } while (!broke && *clocks > 0); } return broke; } /* Retrieve the value of PC */ uint32_t vbGetProgramCounter(VB *emu, int type) { switch (type) { case VB_PC : return emu->cpu.pc; case VB_PC_FROM: return emu->cpu.pcFrom; case VB_PC_TO : return emu->cpu.pcTo; } return 0; } /* Retrieve the value of a program register */ int32_t vbGetProgramRegister(VB *emu, int id) { return id < 1 || id > 31 ? 0 : emu->cpu.program[id]; } /* Retrieve the ROM buffer */ void* vbGetROM(VB *emu, uint32_t *size) { if (size != NULL) *size = emu->cart.romSize; return emu->cart.rom; } /* Retrieve the SRAM buffer */ void* vbGetSRAM(VB *emu, uint32_t *size) { if (size != NULL) *size = emu->cart.sramSize; return emu->cart.sram; } /* Retrieve the value of a system register */ uint32_t vbGetSystemRegister(VB *emu, int id) { switch (id) { case VB_ADTRE: return emu->cpu.adtre; case VB_CHCW : return emu->cpu.chcw.ice << 1; case VB_EIPC : return emu->cpu.eipc; case VB_EIPSW: return emu->cpu.eipsw; case VB_FEPC : return emu->cpu.fepc; case VB_FEPSW: return emu->cpu.fepsw; case VB_PIR : return 0x00005346; case VB_TKCW : return 0x000000E0; case 29 : return emu->cpu.sr29; case 30 : return 0x00000004; case 31 : return emu->cpu.sr31; case VB_ECR : return (uint32_t) emu->cpu.ecr.fecc << 16 | emu->cpu.ecr.eicc; case VB_PSW : return (uint32_t) emu->cpu.psw.i << 16 | (uint32_t) emu->cpu.psw.np << 15 | (uint32_t) emu->cpu.psw.ep << 14 | (uint32_t) emu->cpu.psw.ae << 13 | (uint32_t) emu->cpu.psw.id << 12 | (uint32_t) emu->cpu.psw.fro << 9 | (uint32_t) emu->cpu.psw.fiv << 8 | (uint32_t) emu->cpu.psw.fzd << 7 | (uint32_t) emu->cpu.psw.fov << 6 | (uint32_t) emu->cpu.psw.fud << 5 | (uint32_t) emu->cpu.psw.fpr << 4 | (uint32_t) emu->cpu.psw.cy << 3 | (uint32_t) emu->cpu.psw.ov << 2 | (uint32_t) emu->cpu.psw.s << 1 | (uint32_t) emu->cpu.psw.z ; } return 0; } /* Prepare a simulation state instance for use */ void vbInit(VB *emu) { /* Breakpoint callbacks */ emu->onException = NULL; emu->onExecute = NULL; emu->onFetch = NULL; emu->onRead = NULL; emu->onWrite = NULL; /* System */ emu->peer = NULL; /* Cartridge */ emu->cart.rom = NULL; emu->cart.romSize = 0; emu->cart.sram = NULL; emu->cart.sramSize = 0; /* Everything else */ vbReset(emu); } /* Read a data unit from the bus */ int32_t vbRead(VB *emu, uint32_t address, int type, int debug) { return type < 0 || type >= (int) sizeof TYPE_SIZES ? 0 : busRead(emu, address, type, debug); } /* Simulate a hardware reset */ void vbReset(VB *emu) { uint32_t x; /* Iterator */ /* CPU registers */ vbSetProgramCounter(emu, 0xFFFFFFF0); vbSetSystemRegister(emu, VB_ECR, 0x0000FFF0); vbSetSystemRegister(emu, VB_PSW, 0x00008000); /* Extra CPU registers (the hardware does not do this) */ for (x = 0; x < 32; x++) emu->cpu.program[x] = 0x00000000; emu->cpu.adtre = 0x00000000; emu->cpu.eipc = 0x00000000; emu->cpu.eipsw = 0x00000000; emu->cpu.fepc = 0x00000000; emu->cpu.fepsw = 0x00000000; emu->cpu.sr29 = 0x00000000; emu->cpu.sr31 = 0x00000000; /* History tracking */ emu->cpu.eipcFrom = 0xFFFFFFF0; emu->cpu.eipcTo = 0xFFFFFFF0; emu->cpu.fepcFrom = 0xFFFFFFF0; emu->cpu.fepcTo = 0xFFFFFFF0; emu->cpu.pcFrom = 0xFFFFFFF0; emu->cpu.pcTo = 0xFFFFFFF0; /* Other CPU state */ emu->cpu.busWait = 0; emu->cpu.state = CPU_FETCH; emu->cpu.substring = 0; for (x = 0; x < 5; x++) emu->cpu.irq[x] = 0; /* WRAM (the hardware does not do this) */ for (x = 0; x < 0x10000; x++) emu->wram[x] = 0x00; } /* Specify a new value for PC */ uint32_t vbSetProgramCounter(VB *emu, uint32_t value) { value &= 0xFFFFFFFE; emu->cpu.fetch = 0; emu->cpu.pc = value; emu->cpu.state = CPU_FETCH; emu->cpu.substring = 0; return value; } /* Specify a new value for a program register */ int32_t vbSetProgramRegister(VB *emu, int id, int32_t value) { return id < 1 || id > 31 ? 0 : (emu->cpu.program[id] = value); } /* Supply a ROM buffer */ int vbSetROM(VB *emu, void *rom, uint32_t size) { /* Check the buffer size */ if (size < 1024 || size > 0x1000000 || ((size - 1) & size) != 0) return 0; /* Configure the ROM buffer */ emu->cart.rom = (uint8_t *) rom; emu->cart.romSize = size; return 1; } /* Supply an SRAM buffer */ int vbSetSRAM(VB *emu, void *sram, uint32_t size) { /* Check the buffer size */ if (size == 0 || ((size - 1) & size) != 0) return 0; /* Configure the SRAM buffer */ emu->cart.sram = (uint8_t *) sram; emu->cart.sramSize = size; return 1; } /* Specify a new value for a system register */ uint32_t vbSetSystemRegister(VB *emu, int id, uint32_t value) { return cpuSetSystemRegister(emu, id, value, 1); } /* Write a data unit to the bus */ void vbWrite(VB *emu, uint32_t address, int type, int32_t value, int debug) { if (type >= 0 && type < (int32_t) sizeof TYPE_SIZES) busWrite(emu, address, type, value, debug); }