phakebit/
lib.rs

1//! This is a crate for emulating a 6502 CPU.
2//!
3//! ## Examples
4//! ```rust
5//! use phakebit::memory::PlainMemory;
6//! use phakebit::cpu::CPU;
7//! use phakebit::state::CPUState;
8//! use phakebit::state;
9//!
10//! let memory = PlainMemory::new();
11//! let mut cpu_state = CPUState::new(memory);
12//! // set reset vector to program start or just point `cpu_state.sp` address
13//! cpu_state.write_word(state::RESET_VECTOR_ADDR, 0x1234);
14//! cpu_state.reset();
15//!
16//! let mut cpu = CPU::new(cpu_state);
17//! cpu.execute(100000);
18//! ```
19//!
20//! ## Memory maps
21//! The `Memory` trait is used to implement memory. The PlainMemory struct is a
22//! simple implementation of this trait without any mappings. If you want to map
23//! parts of the memory to IO or ROM, you can implement the `Memory` trait for
24//! your own struct.
25//!
26//! ### Example
27//!
28//! ```rust
29//! use phakebit::memory::Memory;
30//!
31//! struct MemoryMappedIO {
32//!    state: [u8; 0x10000],
33//! }
34//!
35//! fn read_io(address: u16) -> u8 { 0 }
36//! fn write_io(address: u16, value: u8) {}
37//!
38//! impl Memory for MemoryMappedIO {
39//!    fn get(&self, address: u16) -> u8 {
40//!       match address {
41//!         0x0000..=0x1FFF => self.state[address as usize],
42//!         0x2000..=0x3FFF => read_io(address),
43//!         0x4000..=0xFFFF => self.state[address as usize],
44//!       }
45//!   }
46//!
47//!  fn set(&mut self, address: u16, value: u8) {
48//!      match address {
49//!        0x0000..=0x1FFF => self.state[address as usize] = value,
50//!        0x2000..=0x3FFF => write_io(address, value),
51//!        0x4000..=0xFFFF => self.state[address as usize] = value,
52//!      }
53//!   }
54//! }
55//! ```
56//!
57//! ## Instrumentation
58//! The `Trace` struct is used to instrument the CPU. It contains the state of
59//! the CPU _after_ executing the instruction. The `CPU::step()` method returns
60//! a `Trace`.
61//!
62//! ```text
63//! PC   Op Oper   Disassembly   |A  X  Y  SP|NVDIZC|C
64//! ---- -- ----   -----------   |-----------|------|-
65//! 33D1 A5 0E     LDA $0E       |00 0E FF FF|011010|3
66//! 33D3 F0 30     BEQ $3405     |00 0E FF FF|011010|2
67//! 3405 AD 00 02  LDA $0200     |2A 0E FF FF|011000|4
68//! 3408 C9 2A     CMP #$2A      |2A 0E FF FF|011011|2
69//! 340A D0 FE     BNE $340A     |2A 0E FF FF|011011|2
70//! 340C A9 2B     LDA #$2B      |2B 0E FF FF|011001|2
71//! 340E 8D 00 02  STA $0200     |2B 0E FF FF|011001|4
72//! ```
73
74pub mod cpu;
75pub mod instruction;
76pub mod instrumentation;
77pub mod memory;
78pub mod state;