@@ -19,7 +19,6 @@ pub struct OutputState<'a> {
1919
2020enum ProgramCounter {
2121 Unknown ( u16 ) ,
22- //Stay,
2322 Next ,
2423 Skip ,
2524 Jump ( usize ) ,
@@ -54,9 +53,10 @@ pub struct Processor {
5453impl 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