/******************************************************************************\ ** ** This file is part of the Hades GBA Emulator, and is made available under ** the terms of the GNU General Public License version 2. ** ** Copyright (C) 2021-2026 - The Hades Authors ** \******************************************************************************/ /* ** References: ** * ARM7TDMI-S Data Sheet ** https://www.dwedit.org/files/ARM7TDMI.pdf ** * GBATEK ** https://problemkaputt.de/gbatek.htm */ #include #include "hades.h" #include "gba/gba.h" #include "gba/memory.h" #include "gba/core.h" #include "gba/core/arm.h" #include "gba/core/thumb.h" #include "gba/core/helpers.h" /* ** Fetch, decode and execute the next instruction. ** ** First check if the IRQ line is raised and if IRQs aren't disabled in the CPSR and ** interrupt the core appropriately. ** ** Then, take the appropriate action depending on the core's state: ** - Run: Fetch, decode and execute the next instruction ** - Halt: Idle until the next event ** ** The `Stop` state isn't handled here, hence why it isn't mentioned. */ void core_next( struct gba *gba ) { struct core *core; core = &gba->core; // Fire an interrupt if the IRQ line is set. if (core->irq_line && !gba->core.cpsr.irq_disable) { hs_dbgln(HS_IRQ, "Received new IRQ: 0x%04x.", gba->io.int_enabled.raw & gba->io.int_flag.raw); core_interrupt(gba, VEC_IRQ, MODE_IRQ, true); } if (__hs_likely(core->state == CORE_RUN)) { if (core->cpsr.thumb) { uint16_t op; op = core->prefetch[0]; core->prefetch[0] = core->prefetch[1]; core->prefetch[1] = mem_read16(gba, core->pc, core->prefetch_access_type | PIPELINE); gba->memory.was_last_access_from_dma = false; core_execute_thumb_opcode(gba, op); } else { uint32_t op; op = core->prefetch[0]; core->prefetch[0] = core->prefetch[1]; core->prefetch[1] = mem_read32(gba, core->pc, core->prefetch_access_type | PIPELINE); gba->memory.was_last_access_from_dma = false; core_execute_arm_opcode(gba, op); } } else if (core->state == CORE_HALT) { if (gba->core.pending_dma && !gba->core.is_dma_running) { mem_dma_do_all_pending_transfers(gba); } if (gba->scheduler.next_event > gba->scheduler.cycles) { mem_bus_wait_for(gba, gba->scheduler.next_event - gba->scheduler.cycles); } else { mem_bus_wait(gba); } } #ifdef WITH_DEBUGGER debugger_eval_hw_breakpoints(gba); #else (void)0; #endif } void core_execute_arm_opcode( struct gba *gba, uint32_t op ) { struct core *core; size_t idx; core = &gba->core; // Test if the conditions required to execute the instruction are met using a Lookup Table (LUT). // // The index of the LUT is both the CPSR and the condition combined in an 8-bit integer // unique per situation. idx = (bitfield_get_range(core->cpsr.raw, 28, 32) << 4) | (bitfield_get_range(op, 28, 32)); if (__hs_unlikely(!cond_lut[idx])) { core->pc += 4; core->prefetch_access_type = SEQUENTIAL; return; } // Build a unique index based on the instruction's opcode, which is then used to index // the Lookup Table (LUT) of ARM instructions. // // TODO FIXME: We need to properly handle unknown instructions instead of crashing. idx = ((op >> 16) & 0xFF0) | ((op >> 4) & 0x00F); if (__hs_unlikely(arm_lut[idx] == NULL)) { hs_panic(HS_CORE, "Unknown ARM op-code 0x%08x (pc=0x%08x).", op, core->pc); } arm_lut[idx](gba, op); } void core_execute_thumb_opcode( struct gba *gba, uint16_t op ) { struct core *core; core = &gba->core; // Build a unique index based on the instruction's opcode, which is then used to index // the Lookup Table (LUT) of Thumb instructions. // // TODO FIXME: We need to properly handle unknown instructions instead of crashing. if (__hs_unlikely(thumb_lut[op >> 8] == NULL)) { hs_panic(HS_CORE, "Unknown Thumb op-code 0x%04x (pc=0x%08x).", op, core->pc); } thumb_lut[op >> 8](gba, op); } /* ** Reload the cached opcodes of the 3-stage pipeline. ** This must be called when the r15 (PC) is written. ** ** This function takes 1N and 1S cycles to complete, and set the ** next prefetch access type to SEQUENTIAL. */ void core_reload_pipeline( struct gba *gba ) { struct core *core; core = &gba->core; if (core->cpsr.thumb) { core->pc &= 0xFFFFFFFE; core->prefetch[0] = mem_read16(gba, core->pc, NON_SEQUENTIAL | PIPELINE); core->pc += 2; core->prefetch[1] = mem_read16(gba, core->pc, SEQUENTIAL | PIPELINE); core->pc += 2; } else { core->pc &= 0xFFFFFFFC; core->prefetch[0] = mem_read32(gba, core->pc, NON_SEQUENTIAL | PIPELINE); core->pc += 4; core->prefetch[1] = mem_read32(gba, core->pc, SEQUENTIAL | PIPELINE); core->pc += 4; } core->prefetch_access_type = SEQUENTIAL; } /* ** Get the SPSR of the given mode. */ struct psr core_spsr_get( struct core const *core, enum arm_modes mode ) { switch (mode) { case MODE_USR: case MODE_SYS: return core->cpsr; case MODE_FIQ: return core->spsr_fiq; case MODE_IRQ: return core->spsr_irq; case MODE_SVC: return core->spsr_svc; case MODE_ABT: return core->spsr_abt; case MODE_UND: return core->spsr_und; default: hs_panic(HS_CORE, "core_spsr_get(): unsupported mode (%u)", mode); break; } } /* ** Set the SPSR of the given mode to the given value. */ void core_spsr_set( struct core *core, enum arm_modes mode, struct psr psr ) { switch (mode) { case MODE_USR: case MODE_SYS: core->cpsr.raw = psr.raw; break; case MODE_FIQ: core->spsr_fiq.raw = psr.raw; break; case MODE_IRQ: core->spsr_irq.raw = psr.raw; break; case MODE_SVC: core->spsr_svc.raw = psr.raw; break; case MODE_ABT: core->spsr_abt.raw = psr.raw; break; case MODE_UND: core->spsr_und.raw = psr.raw; break; default: hs_panic(HS_CORE, "core_spsr_set(): unsupported mode (%u)", mode); break; } } /* ** Switch from the current mode to the given one. ** ** In practice, this function saves the content of the registers ** to the current mode's bank and replace their value with the ** ones from the new mode's bank. It also sets the CPSR's mode bits\ ** to the given mode. ** ** No SPSRs are updated. */ void core_switch_mode( struct core *core, enum arm_modes mode ) { if (mode != core->cpsr.mode) { hs_dbgln( HS_CORE, "Switching from %s to %s mode.", arm_modes_name[core->cpsr.mode], arm_modes_name[mode] ); // Save current registers to their bank switch (core->cpsr.mode) { case MODE_SYS: case MODE_USR: core->r8_sys = core->r8; core->r9_sys = core->r9; core->r10_sys = core->r10; core->r11_sys = core->fp; core->r12_sys = core->ip; core->r13_sys = core->sp; core->r14_sys = core->lr; break; case MODE_FIQ: core->r8_fiq = core->r8; core->r9_fiq = core->r9; core->r10_fiq = core->r10; core->r11_fiq = core->fp; core->r12_fiq = core->ip; core->r13_fiq = core->sp; core->r14_fiq = core->lr; break; case MODE_IRQ: core->r8_sys = core->r8; core->r9_sys = core->r9; core->r10_sys = core->r10; core->r11_sys = core->fp; core->r12_sys = core->ip; core->r13_irq = core->sp; core->r14_irq = core->lr; break; case MODE_SVC: core->r8_sys = core->r8; core->r9_sys = core->r9; core->r10_sys = core->r10; core->r11_sys = core->fp; core->r12_sys = core->ip; core->r13_svc = core->sp; core->r14_svc = core->lr; break; case MODE_ABT: core->r8_sys = core->r8; core->r9_sys = core->r9; core->r10_sys = core->r10; core->r11_sys = core->fp; core->r12_sys = core->ip; core->r13_abt = core->sp; core->r14_abt = core->lr; break; case MODE_UND: core->r8_sys = core->r8; core->r9_sys = core->r9; core->r10_sys = core->r10; core->r11_sys = core->fp; core->r12_sys = core->ip; core->r13_und = core->sp; core->r14_und = core->lr; break; default: hs_panic(HS_CORE, "core_switch_mode(): unsupported mode (%u)", core->cpsr.mode); break; } core->cpsr.mode = mode; // Restore the registers based on the bank's content switch (mode) { case MODE_SYS: case MODE_USR: core->r8 = core->r8_sys; core->r9 = core->r9_sys; core->r10 = core->r10_sys; core->fp = core->r11_sys; core->ip = core->r12_sys; core->sp = core->r13_sys; core->lr = core->r14_sys; break; case MODE_FIQ: core->r8 = core->r8_fiq; core->r9 = core->r9_fiq; core->r10 = core->r10_fiq; core->fp = core->r11_fiq; core->ip = core->r12_fiq; core->sp = core->r13_fiq; core->lr = core->r14_fiq; break; case MODE_IRQ: core->r8 = core->r8_sys; core->r9 = core->r9_sys; core->r10 = core->r10_sys; core->fp = core->r11_sys; core->ip = core->r12_sys; core->sp = core->r13_irq; core->lr = core->r14_irq; break; case MODE_SVC: core->r8 = core->r8_sys; core->r9 = core->r9_sys; core->r10 = core->r10_sys; core->fp = core->r11_sys; core->ip = core->r12_sys; core->sp = core->r13_svc; core->lr = core->r14_svc; break; case MODE_ABT: core->r8 = core->r8_sys; core->r9 = core->r9_sys; core->r10 = core->r10_sys; core->fp = core->r11_sys; core->ip = core->r12_sys; core->sp = core->r13_abt; core->lr = core->r14_abt; break; case MODE_UND: core->r8 = core->r8_sys; core->r9 = core->r9_sys; core->r10 = core->r10_sys; core->fp = core->r11_sys; core->ip = core->r12_sys; core->sp = core->r13_und; core->lr = core->r14_und; break; default: hs_panic(HS_CORE, "core_switch_mode(): unsupported mode (%u)", mode); break; } } } /* ** Interrupt the CPU, switching to the given interrupt vector/mode. */ void core_interrupt( struct gba *gba, enum arm_vectors vector, enum arm_modes mode, bool do_aborted_prefetch ) { struct core *core; struct psr cpsr; core = &gba->core; // According to the ARM7TDMI manual, the prefetch of the aborted instructions // happens regardless, even if it is discarded shortly after when the pipeline is // reloaded. // // This is necessary for timing reasons, like the `irq-delay` test rom of NBA. // NOTE: There's no prefetch with the SWI instruction. if (do_aborted_prefetch) { if (core->cpsr.thumb) { mem_read16(gba, core->pc, core->prefetch_access_type); } else { mem_read32(gba, core->pc, core->prefetch_access_type); } } cpsr = core->cpsr; core_switch_mode(core, mode); core_spsr_set(core, mode, cpsr); if (vector == VEC_SVC || vector == VEC_UND) { core->lr = core->pc - (core->cpsr.thumb ? 2 : 4); } else if (vector != VEC_RESET) { core->lr = core->pc - (core->cpsr.thumb ? 0 : 4); } core->pc = vector; core->cpsr.irq_disable = true; core->cpsr.thumb = false; core_reload_pipeline(gba); } void core_schedule_irq( struct gba *gba, enum arm_irq irq ) { gba->io.pending.int_flag.raw |= (1 << irq); io_schedule_register_delayed_write(gba, IO_REG_IF); } void core_update_irq_line( struct gba *gba, struct event_args args ) { gba->core.irq_line = (bool)args.a1.u32; } void core_schedule_update_irq_line( struct gba *gba, bool irq_line ) { sched_add_event( gba, NEW_FIX_EVENT_ARGS( SCHED_EVENT_CORE_UPDATE_IRQ_LINE, gba->scheduler.cycles + 2, // Two cycles delay for the CPU to register the new IRQ line EVENT_ARG(u32, irq_line) ) ); } /* ** Compute the operand of an instruction that uses an encoded shift register. ** If `carry` is not NULL, this will set the value pointed by `carry` to the ** shifter carry output. */ uint32_t core_compute_shift( struct core *core, uint32_t encoded_shift, uint32_t value, bool *carry ){ uint32_t type; uint32_t bits; bool carry_out; // The first bit tells us if the amount of bits to shift is either stored as // an immediate value or within a register. if (bitfield_get(encoded_shift, 0)) { // Register uint32_t rs; rs = (encoded_shift >> 4) & 0xF; bits = core->registers[rs] & 0xFF; // The spec requires a bit of error handling regarding register // specified shift amount. if (bits == 0) { return value; } } else { // Immediate value bits = (encoded_shift >> 3) & 0x1F; } type = (encoded_shift >> 1) & 0b11; carry_out = false; // There's four kind of shifts: logical left, logicial right, arithmetic // right and rotate right. switch (type) { // Logical left case 0: // If LSL#0 then the carry bit is the old content of the CPSR C flag // and the value is left untouched. // LSL by 32 has result zero, carry out equal to bit 0 of Rm. // LSL by more than 32 has result zero, carry out zero. if (bits == 0) { carry_out = core->cpsr.carry; } else if (bits <= 32) { value <<= bits - 1; carry_out = (value >> 31) & 0b1; // Save the carry value <<= 1; } else { value = 0; carry_out = false; } break; // Logical right case 1: // LSR#0 is used to encode LSR#32 if (bits <= 32) { if (bits == 0) { bits = 32; } value >>= bits - 1; carry_out = value & 0b1; // Save the carry value >>= 1; } else { value = 0; carry_out = false; } break; // Arithmetic right case 2: // ASR#0 is used to encode ASR#32 if (bits == 0 || bits > 32) { bits = 32; } value = (int32_t)value >> (bits - 1); carry_out = value & 0b1; // Save the carry value = (int32_t)value >> 1; break; // Rotate right case 3: // ROR by n where n is greater than 32 will give the same result and carry out // as ROR by n-32; therefore repeatedly subtract 32 from n until the amount is // in the range 1 to 32 and see above if (bits > 32) { bits = ((bits - 1) % 32) + 1; } // ROR#0 is used to encode RRX if (bits == 0) { carry_out = value & 0b1; value >>= 1; value |= (uint32_t)core->cpsr.carry << 31; } else { carry_out = (value >> (bits - 1)) & 0b1; // Save the carry value = ror32(value, bits & 0x1F); } break; } if (carry) { *carry = carry_out; } return value; } /* ** Idle loop elimination. ** ** This function is called before executing the instruction identified as a main component of the game's idle loop. ** This instruction is identified by the game's idle loop database and is stored in `gba->core.idle_loop.addr`. ** ** This instruction is trapped (using software breakpoints) and we eventually end up here. ** ** We give the idle loop a normal, unassisted try first; only once it is hit a second time in a row does it ** fast-forward to the next interrupt and jump straight into it instead of ** re-running the loop's body. ** ** The return value indicates if the trapped instruction should be skipped. Since we end up interrupting the core when ** eliminating the loop, the trapped instruction shouldn't be run until after the ISR is finished. */ bool core_idle_loop_eval( struct gba *gba, uint32_t addr ) { struct core *core; core = &gba->core; if (!core->idle_loop.enabled || addr != core->idle_loop.addr) { return false; } if (core->idle_loop.first_visit) { core->idle_loop.first_visit = false; return false; } while (!(core->irq_line && !core->cpsr.irq_disable)) { if (core->pending_dma && !core->is_dma_running) { mem_dma_do_all_pending_transfers(gba); } if (gba->scheduler.next_event > gba->scheduler.cycles) { mem_bus_wait_for(gba, gba->scheduler.next_event - gba->scheduler.cycles); } else { mem_bus_wait(gba); } } core->idle_loop.first_visit = true; core_interrupt(gba, VEC_IRQ, MODE_IRQ, true); return true; }