Crates.io | r6502 |
lib.rs | r6502 |
version | 1.1.1 |
source | src |
created_at | 2024-04-02 04:36:58.013859 |
updated_at | 2024-04-04 17:31:43.073825 |
description | A simple MOS 6502 emulator. |
homepage | |
repository | https://github.com/balt-dev/r6502/ |
max_upload_size | |
id | 1193205 |
size | 190,115 |
Designed to support no-std
and not require an allocator nor any unsafe code, and be reasonably fast.
The API of this crate shies away from implementing interrupt handling,
instead having you step the emulator one opcode at a time and handle them yourself.
Note that this does not emulate cycle-by-cycle, and as such may not be 100% accurate.
The following feature flags exist:
Name | Description |
---|---|
bcd | Enable binary-coded decimal arithmetic. Enabled by default. Disable if you're writing a NES emulator. Note that invalid BCD is left untested and will not function faithfully to the MOS 6502. |
bytemuck | Enables bytemuck support. |
arbitrary | Enables arbitrary support. This will pull in std . |
serde | Enables serde support. |
extern crate std;
use std::eprintln;
use r6502::{Emulator, FunctionReadCallback, FunctionWriteCallback};
fn main() {
let mut emu = Emulator::default()
.with_read_callback(FunctionReadCallback(|state: &mut State, addr| {
// Log reads
eprintln!("Read from #${addr:04x}");
state.memory[addr as usize]
}))
.with_write_callback(FunctionWriteCallback(|state: &mut State, addr, byte|
// Don't write to ROM
if addr < 0xFF00 {
state.memory[addr as usize] = byte
})
)
.with_rom(include_bytes!("rom.bin"))
.with_program_counter(0x200);
loop {
let interrupt_requested = emu.step()
.expect("found an invalid opcode (only MOS 6502 opcodes are supported)");
if interrupt_requested { // Go to IRQ interrupt vector
let vector = u16::from_le_bytes([
emu.read(0xFFFE),
emu.read(0xFFFF)
]);
emu.state.program_counter = vector;
}
}
}
This may be licensed under either the MIT or Apache-2.0 license, at your option.