Skip to content

Commit d761ec3

Browse files
committed
Extracted MOS 6502 from NES project and turned into isoleted crate
0 parents  commit d761ec3

9 files changed

+1358
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

Cargo.lock

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "mos6502"
3+
description = "A MOS 6502 Simple CPU Emulator"
4+
version = "0.1.0"
5+
authors = ["[email protected]"]
6+
exclude = ["examples/**"]
7+
8+
[lib]
9+
name = "mos6502"

examples/example1

651 KB
Binary file not shown.

examples/example1.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
extern crate mos6502;
2+
3+
use mos6502::cpu::CPU;
4+
use mos6502::memory_map::memory_map::MemMappeable;
5+
6+
struct Ram {
7+
ram: [u8; 4096]
8+
}
9+
10+
impl Ram {
11+
pub fn new() -> Ram {
12+
Ram {
13+
ram: [0; 4096]
14+
}
15+
}
16+
}
17+
18+
impl MemMappeable for Ram {
19+
fn read(&self, address: usize) -> u8 {
20+
self.ram[address]
21+
}
22+
23+
fn slice(&self, from: usize) -> &[u8] {
24+
&self.ram[from..self.ram.len()]
25+
}
26+
27+
fn write(&mut self, address: usize, value: u8) -> u8 {
28+
self.ram[address as usize] = value;
29+
value
30+
}
31+
}
32+
33+
struct Rom {
34+
rom: Vec<u8>
35+
}
36+
37+
impl Rom {
38+
pub fn new(rom: Vec<u8>) -> Rom {
39+
Rom {
40+
rom: rom
41+
}
42+
}
43+
}
44+
45+
impl MemMappeable for Rom {
46+
fn read(&self, address: usize) -> u8 {
47+
self.rom[address]
48+
}
49+
50+
fn slice(&self, from: usize) -> &[u8] {
51+
&self.rom[from..self.rom.len()]
52+
}
53+
54+
fn write(&mut self, _address: usize, _value: u8) -> u8 {
55+
panic!("No writes in ROM!")
56+
}
57+
}
58+
59+
fn main() {
60+
let ram = Ram::new();
61+
62+
let program = vec![
63+
0xa2, 0x01,
64+
0xa9, 0x05,
65+
0x85, 0x01,
66+
0xa9, 0x07,
67+
0x85, 0x02,
68+
0xa0, 0x0a,
69+
0x8c, 0x05, 0x07,
70+
0xa1, 0x00
71+
];
72+
73+
let rom = Rom::new(program);
74+
let mut cpu = CPU::new();
75+
76+
cpu.mount_in_bus(0x0000..0x1000, ram);
77+
cpu.mount_in_bus(0xe000..0xe010, rom);
78+
79+
cpu.set_pc(0xe000);
80+
81+
for _ in 0..8 {
82+
cpu.step();
83+
}
84+
85+
assert_eq!(0x0a, cpu.a);
86+
assert_eq!(0x01, cpu.x);
87+
assert_eq!(0x0a, cpu.y);
88+
}

0 commit comments

Comments
 (0)