1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
use std::io::Read;
use std::io::Write;
const WIDTH: u16 = 64;
const HEIGHT: u16 = 32;
const PIXELS: usize = (WIDTH * HEIGHT) as usize;
fn unknown_opcode(opcode: u16) {
eprintln!("Unknwown opcode {:#06x}", opcode);
std::process::exit(1);
}
pub struct Chip8 {
v: [u8; 4096],
i: u16,
pc: u16,
stack: [u16; 16],
sp: u16,
memory: [u8; 4096],
screen: [u8; PIXELS],
delay_timer: u8,
sound_timer: u8,
key: [u8; 16],
draw: bool,
}
impl Chip8 {
pub fn new() -> Self {
Self {
v: [0; 4096],
i: 0,
pc: 0x200,
stack: [0; 16],
sp: 0,
memory: [0; 4096],
screen: [0; PIXELS],
delay_timer: 0,
sound_timer: 0,
key: [0; 16],
draw: false,
}
}
pub fn load_rom<R: Read>(&mut self, mut reader: R) -> std::io::Result<()> {
reader.read(&mut self.memory[0x200..])?;
Ok(())
}
pub fn cycle(&mut self) {
let pc = self.pc as usize;
let opcode = ((self.memory[pc] as u16) << 8) | self.memory[pc + 1] as u16;
let x = ((opcode & 0x0F00) >> 8) as usize;
let y = ((opcode & 0x00F0) >> 4) as usize;
let n = opcode & 0x000F;
let nn = (opcode & 0x00FF) as u8;
let nnn = opcode & 0x0FFF;
self.pc += 2;
match opcode & 0xF000 {
0x0000 => match opcode & 0x000F {
0x0000 => {
eprintln!("CLS");
self.screen = [0; PIXELS];
self.draw = true;
}
0x000E => {
eprintln!("RET");
}
_ => unknown_opcode(opcode),
},
0x1000 => {
eprintln!("JP {}", nnn);
self.pc = nnn;
}
0x3000 => {
eprintln!("SE V{}, {}", x, nn);
if self.v[x] == nn {
self.pc += 2;
}
}
0x4000 => {
eprintln!("SNE V{}, {}", x, nn);
if self.v[x] != nn {
self.pc += 2;
}
}
0x5000 => {
eprintln!("SE V{}, V{}", x, y);
if self.v[x] == self.v[y] {
self.pc += 2;
}
}
0x6000 => {
eprintln!("LD V{}, {}", x, nn);
self.v[x] = nn;
}
0x7000 => {
eprintln!("ADD V{}, {}", x, nn);
self.v[x] = self.v[x].wrapping_add(nn);
}
0x9000 => {
eprintln!("SNE V{}, V{}", x, y);
if self.v[x] != self.v[y] {
self.pc += 2;
}
}
0xA000 => {
eprintln!("LD I, {}", nnn);
self.i = nnn;
}
0xD000 => {
eprintln!("DRW V{}, V{}, {}", x, y, n);
let vx = self.v[x] as u16;
let vy = self.v[y] as u16;
self.v[0xF] = 0;
for yy in 0..n {
let pixel = self.memory[(self.i + yy) as usize];
for xx in 0..8 {
if (pixel & (0x80 >> xx)) != 0 {
let si = vx + xx + ((vy + yy) * WIDTH);
if self.screen[si as usize] == 1 {
self.v[0xF] = 1;
}
self.screen[si as usize] ^= 1;
}
}
}
self.draw = true;
}
_ => unknown_opcode(opcode),
};
if self.delay_timer > 0 {
self.delay_timer -= 1;
}
if self.sound_timer > 0 {
if self.sound_timer == 1 {
eprintln!("BEEP");
}
self.sound_timer -= 1;
}
}
pub fn display(&mut self) -> std::io::Result<()> {
if self.draw {
self.draw = false;
self.draw()
} else {
Ok(())
}
}
fn draw(&self) -> std::io::Result<()> {
let stdout = std::io::stdout();
let mut handle = stdout.lock();
handle.write_all(b"\x1B[2J\n")?;
for y in 0..HEIGHT {
for x in 0..WIDTH {
let si = x + y * WIDTH;
let buf = if self.screen[si as usize] == 0 {
"░".as_bytes()
} else {
"█".as_bytes()
};
handle.write_all(buf)?;
}
handle.write_all(b"\n")?;
}
handle.flush()
}
}
|