pub struct Cpu {
pub regs: Regs,
pub cycles: u64,
pub waiting: bool,
pub stopped: bool,
/* private fields */
}Expand description
Fields§
§regs: RegsArchitectural register file (A/X/Y/S/D/DBR/PBR/PC/P/E).
cycles: u64Cumulative CPU cycles consumed across all instructions (one per bus access or I/O cycle).
waiting: boolWAI latch: the CPU is waiting for an interrupt; cleared when one is taken.
stopped: boolSTP latch: the CPU has been stopped and resumes only on reset.
Implementations§
Source§impl Cpu
impl Cpu
Sourcepub fn step(&mut self, bus: &mut impl Bus) -> u32
pub fn step(&mut self, bus: &mut impl Bus) -> u32
Execute one 65C816 instruction against bus, returning the CPU cycles consumed
(see the crate docs for the unit). Polls NMI then IRQ at the instruction boundary
before fetching; IRQ is honored only when the I flag is clear. If the STP latch is
set, the CPU stays halted (one idle cycle returned) until Cpu::reset.
Source§impl Cpu
impl Cpu
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Construct at power-on (emulation mode). Call Cpu::reset to load PC from the
reset vector before stepping.
Sourcepub fn reset(&mut self, bus: &mut impl Bus)
pub fn reset(&mut self, bus: &mut impl Bus)
Power-on / reset: force emulation mode (E=1, M=1, X=1, I=1, D=0), park the
stack at $01FF, clear the WAI/STP latches, and load PC from the emulation
RESET vector at $00FFFC/$00FFFD. cycles is left as-is (cumulative counter).
Sourcepub fn save_state(&self, w: &mut SaveWriter)
pub fn save_state(&self, w: &mut SaveWriter)
Write the full architectural register file plus the WAI/STP latches and cumulative
cycle counter into a "CPU0" section. cyc (the per-instruction accumulator, reset at
the start of every Cpu::step) is included too even though a save-state can only ever
be taken between instructions (where it’s always 0) — it costs one u32 to round-trip
exactly rather than assume that invariant holds for every future caller of this format.
Sourcepub fn load_state(
&mut self,
r: &mut SaveReader<'_>,
) -> Result<(), SaveStateError>
pub fn load_state( &mut self, r: &mut SaveReader<'_>, ) -> Result<(), SaveStateError>
The inverse of Self::save_state.
§Errors
SaveStateError on truncated/corrupt input or a section with unconsumed trailing
bytes. p is restored via Status::from_bits_truncate, which silently drops any bit
outside the flag set instead of erroring — Status covers all 8 bits of the real
hardware register, so no encoding of a save-state byte is actually invalid here. x/y/
s/pbr are masked to the widths emulation/Status::X force during normal operation
(8-bit X/Y with the high byte forced to zero, S confined to page $01, PBR forced
to 0, per docs/cpu.md’s emulation-mode rules) — the same “apply the engine’s own
normal-operation invariant on load” reasoning already applied elsewhere in this project.