LakeSnes: SNES emulator core by elzo-d (Elzo Doeleman)
https://github.com/elzo-d/LakeSnes
License: MIT (see LICENSE.txt)

Vendored commit: 9db90b86e46a377609305e298dd92d71cd1d4c8a  (2023-12-02)
Vendored on:     2026-09-10

Why a copy and not a submodule
------------------------------
The changes below have to live inside the emulator core (a hook callback in the
CPU instruction loop, and its twin in the SPC700's), which a submodule cannot carry. The core is otherwise
self-contained: only snes/ is copied, and it pulls in nothing but libc.

What was copied
---------------
  snes/{apu,cart,cpu,dma,dsp,input,ppu,snes,snes_other,spc,statehandler}.c
  snes/{apu,cart,cpu,dma,dsp,input,ppu,snes,spc,statehandler}.h
  LICENSE.txt

Not copied: main.c (SDL frontend), tracing.[ch], zip/, resources/, Makefile.

Local modifications (all marked with a "// dream:" comment)
-----------------------------------------------------------
1. cpu.h    added the CpuHookHandler typedef, the `hook` / `hookCtx` fields on
            struct Cpu, and the cpu_checkIntPublic() / cpu_runNonInstruction()
            declarations.
2. cpu.c    cpu_init() clears hook/hookCtx; cpu_runOpcode() calls the hook with the
            24-bit PC before fetching an opcode and returns early if it reports the
            instruction handled; added cpu_checkIntPublic(), a public wrapper around
            the static cpu_checkInt() so a hook can end a replaced routine with the
            same interrupt latch the real last cycle performs; and split the
            non-instruction half of cpu_runOpcode() out into cpu_runNonInstruction()
            (the reset sequence, the stp/wai park and interrupt entry, unchanged and
            in the same order), which cpu_runOpcode() now calls first. The recomp's
            --no-cpu scheduler runs the machine's non-instruction steps through it,
            so the frame an NMI pushes and the cycles it spends are the core's own
            rather than a copy (recomp/README.md, "Running without the CPUs").
3. spc.h    added the SpcHookHandler typedef and the `hook` / `hookCtx` fields on
            struct Spc: the SPC700 twin of 1, for the sound driver's recomp.
4. spc.c    spc_init() clears hook/hookCtx; spc_runOpcode() calls the hook with the
            16-bit pc before fetching an opcode and returns early if it reports the
            instruction handled.
5. apu.h, apu.c   added `sliceEnd` to struct Apu, set by apu_runCycles() to the
            cycle count at which the catch-up step it is about to run would end,
            and cleared by apu_reset()/carried in apu_handleState(). The SPC runs
            in catch-up steps relative to the CPU, so the reference SPC stops
            between two instructions of a routine; publishing where the step ends
            is what lets a recomp hook stop on the same boundary instead of
            running the routine to its end (recomp/include/spc_state.h,
            sps_yield_wanted). Nothing in the core reads it.
6. apu.c, snes.c, cart.c, snes_other.c   five upstream -Wall -Wextra warnings
            silenced ((void) on two unused `waiting` parameters and one unused
            `cart`, two sign-compare casts). No behaviour change.
7. ppu.c    two __attribute__((fallthrough)) statements at the $210D and $210E
            register cases, silencing -Wimplicit-fallthrough. The attribute is a
            null statement and both cases still fall into the BG-HOFS/BG-VOFS
            bodies below them. No behaviour change.
8. spc.c    __attribute__((unused)) on the forward declaration of spc_writeWord,
            which upstream declares and never calls, silencing
            -Wunused-function. No behaviour change.
9. dsp.c    the BRR decoder's two shifts of a negative sample value cast through
            uint32_t (`(int) ((uint32_t) s << shift) >> 1` and its sibling).
            C11 6.5.7p4 leaves a left shift of a negative value undefined and
            UBSan reports both on an ordinary run. Every value involved fits in
            16 bits, so the round trip is the same number on two's-complement
            hardware. No behaviour change.

Items 6 to 8 are the eight upstream warnings this build silences; 9 is the one
piece of undefined behaviour it removes.

Cart type forcing is NOT a source modification: the harness bypasses
snes_loadRom()'s header scoring by calling cart_load(cart, 2 /* HiROM */, rom,
0x200000, 0 /* no SRAM */) directly and then snes_reset(snes, true).
See recomp/README.md and recomp/harness/main.c (machine_load_rom).
