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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
extern crate minifb;
extern crate cpal;
extern crate rand;
use std::env;
use std::fs::File;
use std::io::Read;
use std::time::{Instant, Duration};
use std::thread;
use std::sync::mpsc;
use std::path::Path;
use minifb::{Window, WindowOptions, Key};
use cpal::traits::{HostTrait, DeviceTrait, StreamTrait};
use cpal::SampleFormat;
const PROGRAM_START_ADDR: usize = 0x200;
const FONT_START_ADDR: usize = 0x000;
const MEMORY_LENGTH: usize = 0x1000;
const SCREEN_HEIGHT: usize = 0x20;
const SCREEN_WIDTH: usize = 0x40;
const PIXEL_OFF_COLOR: u32 = 0x00000000;
const PIXEL_ON_COLOR: u32 = 0x00FFFFFF;
const FONT_BYTES_PER_CHAR: u16 = 5;
const FONT_DATA: [u8; 80] = [
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80, // F
];
struct Registers {
pub i: u16,
pub pc: u16,
pub v: [u8; 0x10],
}
impl Registers {
fn new() -> Self {
Self {
i: 0,
pc: PROGRAM_START_ADDR as u16,
v: [0; 0x10],
}
}
}
struct Display {
framebuffer: Vec<u32>,
window: Window,
scale_factor: usize,
}
impl Display {
fn new(scale_factor: usize) -> Self {
Self {
framebuffer: vec![PIXEL_OFF_COLOR; SCREEN_HEIGHT * SCREEN_WIDTH * scale_factor * scale_factor],
window: Window::new("CHIP-8 Emulator", SCREEN_WIDTH * scale_factor, SCREEN_HEIGHT * scale_factor, WindowOptions::default()).unwrap(),
scale_factor,
}
}
fn clear(&mut self) {
for pixel in &mut self.framebuffer {
*pixel = PIXEL_OFF_COLOR;
}
}
fn redraw(&mut self) {
self.window.update_with_buffer(&self.framebuffer, SCREEN_WIDTH * self.scale_factor, SCREEN_HEIGHT * self.scale_factor).unwrap();
}
fn flip_pixel(&mut self, x: usize, y: usize) -> bool {
if let Some(pixel) = self.get_pixel(x, y) {
if *pixel == PIXEL_OFF_COLOR {
self.set_pixel(x, y, PIXEL_ON_COLOR);
false
} else {
self.set_pixel(x, y, PIXEL_OFF_COLOR);
true
}
} else {
false
}
}
fn get_pixel(&self, x: usize, y: usize) -> Option<&u32> {
self.framebuffer.get(y * self.scale_factor * self.scale_factor * SCREEN_WIDTH + x * self.scale_factor)
}
fn set_pixel(&mut self, x: usize, y: usize, color: u32) {
for x_scaled in 0..self.scale_factor {
for y_scaled in 0..self.scale_factor {
//if let Some(pixel) = self.framebuffer.get_mut((y * self.scale_factor + y_scaled) * SCREEN_WIDTH + x * self.scale_factor + x_scaled) {
if let Some(pixel) = self.framebuffer.get_mut((y * self.scale_factor + y_scaled) * self.scale_factor * SCREEN_WIDTH + x * self.scale_factor + x_scaled ) {
*pixel = color;
}
}
}
}
fn is_key_down(&self, key: Key) -> bool {
self.window.is_key_down(key)
}
fn is_open(&self) -> bool {
self.window.is_open()
}
fn update_without_redraw(&mut self) {
self.window.update();
}
}
struct Buzzer {
stream: cpal::Stream,
}
impl Buzzer {
fn new() -> Self {
let host = cpal::default_host();
let device = host.devices().unwrap().next().unwrap();
let supported_config = device.default_output_config().expect("Failed to determine default output configuration for audio device!");
let sample_format = supported_config.sample_format();
let config = supported_config.into();
let stream = match sample_format {
SampleFormat::F32 => Self::build_stream::<f32>(&device, &config),
SampleFormat::I16 => Self::build_stream::<i16>(&device, &config),
SampleFormat::U16 => Self::build_stream::<u16>(&device, &config),
};
stream.pause().unwrap();
Self {
stream,
}
}
fn build_stream<T>(device: &cpal::Device, config: &cpal::StreamConfig) -> cpal::Stream
where
T: cpal::Sample
{
let sample_rate = config.sample_rate.0 as f32;
let channels = config.channels as usize;
let mut sample_clock = 0f32;
let mut next_value = move || {
sample_clock = (sample_clock + 1.0) % sample_rate;
(sample_clock * 440.0 * 2.0 * std::f32::consts::PI / sample_rate).sin()
};
let err_fn = |err| println!("An error occurend on audio stream: {}", err);
device.build_output_stream(
config,
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
Self::write_data(data, channels, &mut next_value)
},
err_fn,
).expect("Failed to build audio output stream!")
}
fn write_data<T>(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32)
where
T: cpal::Sample
{
for frame in output.chunks_mut(channels) {
let value: T = cpal::Sample::from::<f32>(&next_sample());
for sample in frame.iter_mut() {
*sample = value;
}
}
}
fn play(&self) {
self.stream.play().unwrap();
}
fn pause(&self) {
self.stream.pause().unwrap();
}
}
// TODO: play sound
fn main() {
let mut args = env::args();
args.next(); // Discard path to binary
let mut memory = vec![0; MEMORY_LENGTH];
memory[FONT_START_ADDR..(FONT_START_ADDR + FONT_DATA.len())].copy_from_slice(&FONT_DATA);
let mut display = Display::new(args.next()
.expect("Invalid arguments. Specify a scale factor.")
.trim()
.parse::<usize>()
.expect("Only integer scale factors are supported."));
let cycle_duration = Duration::from_secs(1) / args.next()
.expect("Invalid arguments. Specify a clock speed.")
.trim()
.parse::<u32>()
.expect("Only integer clock speeds are supported.");
{
let file_name = args.next().expect("Invalid arguments. Specify the path to the program.");
let mut program = File::open(Path::new(&file_name)).expect(&format!("Failed to open: {}", file_name));
program.read(&mut memory[PROGRAM_START_ADDR..MEMORY_LENGTH]).expect("Failed to read program into memory!");
}
let mut stack: Vec<u16> = Vec::new();
let mut reg = Registers::new();
let mut delay_timer: u8 = 0x00;
let mut sound_timer: u8 = 0x00;
let buzzer = Buzzer::new();
let (timer_tx, timer_rx) = mpsc::channel::<()>();
let (end_tx, end_rx) = mpsc::channel::<()>();
thread::spawn(move || {
let cycle_duration = Duration::from_secs(1) / 60;
loop {
let start_time = Instant::now();
if let Ok(_) = end_rx.try_recv() {
break;
}
timer_tx.send(()).expect("Failed to update timers!");
thread::sleep(cycle_duration.saturating_sub(start_time.elapsed()));
}
});
let mut waiting_for_key: Option<usize> = None;
let keymap = [
Key::X,
Key::Key1,
Key::Key2,
Key::Key3,
Key::Q,
Key::W,
Key::E,
Key::A,
Key::S,
Key::D,
Key::Z,
Key::C,
Key::Key4,
Key::R,
Key::F,
Key::V,
];
while !display.is_key_down(Key::Escape) && display.is_open() {
let cycle_start_time = Instant::now();
if let Some(register) = waiting_for_key {
for i in 0..keymap.len() {
if display.is_key_down(keymap[i]) {
reg.v[register] = i as u8;
waiting_for_key = None;
break;
}
}
display.update_without_redraw();
thread::sleep(cycle_duration.saturating_sub(cycle_start_time.elapsed()));
continue;
}
let operation = u16::from_be_bytes([memory[reg.pc as usize], memory[(reg.pc + 1) as usize]]);
reg.pc += 2;
let high_nibble = (operation & 0xF000) >> 12;
match high_nibble {
0x00 => {
match operation {
0x00E0 => {
display.clear();
display.redraw();
},
0x00EE => reg.pc = stack.pop().expect("Attempted to return with an empty stack!"),
_ => panic!("Not implemented: {:#x} (execute machine language subroutine)", operation),
}
},
0x01 => reg.pc = operation & 0x0FFF,
0x02 => {
stack.push(reg.pc);
reg.pc = operation & 0x0FFF;
},
0x03 => {
let register = (operation & 0x0F00) >> 8;
if reg.v[register as usize] == (operation & 0x00FF) as u8 {
reg.pc += 2;
}
},
0x04 => {
let register = (operation & 0x0F00) >> 8;
if reg.v[register as usize] != (operation & 0x00FF) as u8 {
reg.pc += 2;
}
},
0x05 => {
let x = (operation & 0x0F00) >> 8;
let y = (operation & 0x00F0) >> 4;
if reg.v[x as usize] == reg.v[y as usize] {
reg.pc += 2;
}
},
0x06 => {
let register = (operation & 0x0F00) >> 8;
let value = (operation & 0x00FF) as u8;
reg.v[register as usize] = value;
},
0x07 => {
let register = ((operation & 0x0F00) >> 8) as usize;
let value = (operation & 0x00FF) as u8;
reg.v[register] = reg.v[register].wrapping_add(value);
},
0x08 => {
let x = ((operation & 0x0F00) >> 8) as usize;
let y = ((operation & 0x00F0) >> 4) as usize;
match operation & 0x000F {
0x00 => reg.v[x] = reg.v[y],
0x01 => reg.v[x] = reg.v[x] | reg.v[y],
0x02 => reg.v[x] = reg.v[x] & reg.v[y],
0x03 => reg.v[x] = reg.v[x] ^ reg.v[y],
0x04 => {
let (result, overflowed) = reg.v[x].overflowing_add(reg.v[y]);
reg.v[x] = result;
reg.v[0xF] = overflowed as u8;
},
0x05 => {
let (result, overflowed) = reg.v[x].overflowing_sub(reg.v[y]);
reg.v[x] = result;
reg.v[0xF] = (!overflowed) as u8;
},
0x06 => {
reg.v[x] = reg.v[y] >> 1;
reg.v[0xF] = reg.v[y] & 0x01;
},
0x07 => {
let (result, overflowed) = reg.v[y].overflowing_sub(reg.v[x]);
reg.v[x] = result;
reg.v[0xF] = (!overflowed) as u8;
}
0x0E => {
reg.v[x] = reg.v[y] << 1;
reg.v[0xF] = (reg.v[y] & 0x80) >> 7;
}
_ => panic!("Invalid instruction: {:#x}", operation),
}
},
0x09 => {
let x = (operation & 0x0F00) >> 8;
let y = (operation & 0x00F0) >> 4;
if reg.v[x as usize] != reg.v[y as usize] {
reg.pc += 2;
}
},
0x0A => reg.i = operation & 0x0FFF,
0x0B => reg.pc = (operation & 0x0FFF) + (reg.v[0] as u16),
0x0C => {
let register = ((operation & 0x0F00) >> 8) as usize;
let mask = (operation & 0x00FF) as u8;
reg.v[register] = rand::random::<u8>() & mask;
},
0x0D => {
reg.v[0xF] = 0x00;
let x = reg.v[((operation & 0x0F00) >> 8) as usize] % (SCREEN_WIDTH as u8);
let y = reg.v[((operation & 0x00F0) >> 4) as usize] % (SCREEN_HEIGHT as u8);
let rows = operation & 0x000F;
for row in 0..rows {
let row_data = memory[(reg.i + row) as usize];
for column in 0..8 {
let pixel_state = (row_data << column) & 0x80;
if pixel_state == 0x80 {
reg.v[0xF] |= display.flip_pixel((x + column) as usize, (y + (row as u8)) as usize) as u8;
}
}
}
display.redraw();
},
0x0E => {
let key = keymap[reg.v[((operation & 0x0F00) >> 8) as usize] as usize];
match operation & 0x00FF {
0x9E => {
if display.is_key_down(key) {
reg.pc += 2;
}
},
0xA1 => {
if !display.is_key_down(key) {
reg.pc += 2;
}
},
_ => panic!("Invalid instruction: {:#x}", operation),
}
},
0x0F => {
let register = ((operation & 0x0F00) >> 8) as usize;
match operation & 0x00FF {
0x07 => reg.v[register] = delay_timer,
0x0A => waiting_for_key = Some(register),
0x15 => delay_timer = reg.v[register],
0x18 => sound_timer = reg.v[register],
0x1E => reg.i = reg.i.wrapping_add(reg.v[register] as u16),
0x29 => reg.i = FONT_START_ADDR as u16 + reg.v[register] as u16 * FONT_BYTES_PER_CHAR,
0x33 => {
memory[reg.i as usize] = reg.v[register] / 100;
memory[reg.i as usize + 1] = (reg.v[register] % 100) / 10;
memory[reg.i as usize + 2] = reg.v[register] % 10;
},
0x55 => {
for i in 0..=register {
memory[reg.i as usize] = reg.v[i];
reg.i += 1;
}
},
0x65 => {
for i in 0..=register {
reg.v[i] = memory[reg.i as usize];
reg.i += 1;
}
},
_ => panic!("Invalid instruction: {:#x}", operation),
}
}
_ => panic!("Not implemented: {:#x}", operation),
}
if let Ok(()) = timer_rx.try_recv() {
delay_timer = delay_timer.saturating_sub(1);
sound_timer = sound_timer.saturating_sub(1);
}
if sound_timer > 1 {
buzzer.play();
} else {
buzzer.pause();
}
// saturating_sub is still unstable for durations
/*if cycle_duration > cycle_start_time.elapsed() {
thread::sleep(cycle_duration - cycle_start_time.elapsed());
}*/
thread::sleep(cycle_duration.saturating_sub(cycle_start_time.elapsed()));
}
end_tx.send(()).unwrap();
}
|