Skip to content

Commit 1ebac4d

Browse files
Csatinapppplecuds
andauthored
changed processor functions that use for to a functional approach (#3)
* changed processor functions that use for to a functional approach * changed the way a color is picked from a conditional to a simple lookup table --------- Co-authored-by: cuds <[email protected]>
1 parent 537ee53 commit 1ebac4d

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

src/drivers/display_driver.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ impl DisplayDriver {
3838
DisplayDriver { canvas: canvas }
3939
}
4040

41+
const COLORS: [pixels::Color; 2] =
42+
[pixels::Color::RGB(0, 0, 100), pixels::Color::RGB(0, 150, 0)];
43+
4144
pub fn draw(&mut self, pixels: &[u64; CHIP8_HEIGHT]) {
42-
pixels.iter().enumerate().for_each(|(y_index, y)| {
45+
pixels.iter().enumerate().for_each(|(y_index, &row)| {
4346
(0usize..CHIP8_WIDTH).for_each(|x_index| {
4447
let _x_start = (x_index as u32) * SCALE_FACTOR;
4548
let _y_start = (y_index as u32) * SCALE_FACTOR;
4649

4750
self.canvas
48-
.set_draw_color(color((*y >> (63 - x_index)) & 1));
51+
.set_draw_color(Self::COLORS[((row >> (63 - x_index)) & 1) as usize]);
4952

5053
let _ = self.canvas.fill_rect(Rect::new(
5154
_x_start as i32,
@@ -59,11 +62,3 @@ impl DisplayDriver {
5962
self.canvas.present();
6063
}
6164
}
62-
63-
fn color(value: u64) -> pixels::Color {
64-
if value == 0 {
65-
pixels::Color::RGB(0, 0, 100)
66-
} else {
67-
pixels::Color::RGB(0, 150, 0)
68-
}
69-
}

src/processor.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub struct OutputState<'a> {
1919

2020
enum ProgramCounter {
2121
Unknown(u16),
22-
//Stay,
2322
Next,
2423
Skip,
2524
Jump(usize),
@@ -54,9 +53,10 @@ pub struct Processor {
5453
impl Processor {
5554
pub fn new() -> Self {
5655
let mut ram = [0u8; CHIP8_RAM];
57-
for i in 0..FONT_SET.len() {
58-
ram[i] = FONT_SET[i];
59-
}
56+
57+
FONT_SET.iter().enumerate().for_each(|(i, &font)| {
58+
ram[i] = font;
59+
});
6060

6161
Processor {
6262
vram: [0; CHIP8_HEIGHT],
@@ -76,18 +76,16 @@ impl Processor {
7676
}
7777

7878
pub fn load(&mut self, data: &[u8]) {
79-
for (i, &byte) in data.iter().enumerate() {
79+
data.iter().enumerate().for_each(|(i, &byte)| {
8080
let addr = 0x200 + i;
81-
if addr < 4096 {
82-
self.ram[0x200 + i] = byte;
83-
} else {
84-
break;
81+
if addr >= 4096 {
82+
panic!("ROM Data is bigger than chip8 RAM");
8583
}
86-
}
84+
self.ram[addr] = byte;
85+
});
8786
}
8887

8988
pub fn tick(&mut self, keypad: u16) -> OutputState {
90-
//self.vram_changed = false;
9189
self.keypad = keypad;
9290

9391
if self.keypad_wait {
@@ -170,7 +168,6 @@ impl Processor {
170168
println!("ERROR: OPCODE {:#06x} UNKNOWN", opcode);
171169
self.pc += OPCODE_SIZE;
172170
}
173-
//ProgramCounter::Stay => (),
174171
ProgramCounter::Next => self.pc += OPCODE_SIZE,
175172
ProgramCounter::Skip => self.pc += 2 * OPCODE_SIZE,
176173
ProgramCounter::Jump(addr) => self.pc = addr,
@@ -356,7 +353,8 @@ impl Processor {
356353

357354
fn op_dxyn(&mut self, x: usize, y: usize, n: usize) -> ProgramCounter {
358355
self.v[0x0f] = 0;
359-
for byte in 0..n {
356+
357+
(0..n).for_each(|byte| {
360358
let y = (self.v[y] as usize + byte) % CHIP8_HEIGHT;
361359
let x = self.v[x] as usize % CHIP8_WIDTH;
362360

@@ -371,7 +369,7 @@ impl Processor {
371369
}
372370
self.v[0xf] |= if self.vram[y] & mask > 0 { 1 } else { 0 };
373371
self.vram[y] = self.vram[y] ^ mask;
374-
}
372+
});
375373
self.vram_changed = true;
376374
ProgramCounter::Next
377375
}
@@ -448,19 +446,15 @@ impl Processor {
448446
// The interpreter copies the values of registers V0 through Vx
449447
// into memory, starting at the address in I.
450448
fn op_fx55(&mut self, x: usize) -> ProgramCounter {
451-
for i in 0..x + 1 {
452-
self.ram[self.i + i] = self.v[i];
453-
}
449+
(0..x + 1).for_each(|i| self.ram[self.i + i] = self.v[i]);
454450
ProgramCounter::Next
455451
}
456452

457453
// LD Vx, [I]
458454
// The interpreter reads values from memory starting at location
459455
// I into registers V0 through Vx.
460456
fn op_fx65(&mut self, x: usize) -> ProgramCounter {
461-
for i in 0..x + 1 {
462-
self.v[i] = self.ram[self.i + i];
463-
}
457+
(0..x + 1).for_each(|i| self.v[i] = self.ram[self.i + i]);
464458
ProgramCounter::Next
465459
}
466460
}

0 commit comments

Comments
 (0)