@@ -107,6 +107,7 @@ impl Vm {
107107 {
108108 let mut ram = BufWriter :: new ( & mut vm. ram [ FONT_ADDR ..( FONT_ADDR + FONT_BYTES ) ] ) ;
109109 ram. write_all ( FONT . as_slice ( ) ) . unwrap ( ) ;
110+ debug ! ( "Initialized VM with built-in font" ) ;
110111 }
111112 vm
112113 }
@@ -115,13 +116,16 @@ impl Vm {
115116 pub fn load_rom ( & mut self , reader : & mut Read ) -> Result < usize , Chip8Error > {
116117 let mut rom = Vec :: new ( ) ;
117118 try!( reader. read_to_end ( & mut rom) ) ;
118- if rom. len ( ) > ( RAM_SIZE - PROGRAM_START ) {
119- println ! ( "Rom size {}" , rom. len( ) ) ;
119+ let rom_len = rom. len ( ) ;
120+ let available_ram = RAM_SIZE - PROGRAM_START ;
121+ if rom_len > available_ram {
122+ error ! ( "ROM size ({}) is larger than available RAM ({})!" , rom_len, available_ram) ;
120123 return Err ( Chip8Error :: Io ( "ROM was larger than available RAM" , None ) )
121124 }
122125 let mut ram = BufWriter :: new ( & mut self . ram [ PROGRAM_START ..RAM_SIZE ] ) ;
123126 try!( ram. write_all ( rom. as_slice ( ) ) ) ;
124- return Ok ( rom. len ( ) ) ;
127+ debug ! ( "Loaded ROM of size {}" , rom_len) ;
128+ return Ok ( rom_len) ;
125129 }
126130
127131 #[ allow( dead_code) ]
@@ -136,15 +140,18 @@ impl Vm {
136140
137141 /// Marks the key with index `idx` as being set
138142 pub fn set_key ( & mut self , idx : u8 ) {
143+ debug ! ( "Set key {}" , idx) ;
139144 self . keys [ idx as usize ] = 1 ;
140145 if let Some ( vx) = self . waiting_on_key {
146+ debug ! ( "No longer waiting on key" ) ;
141147 self . reg [ vx as usize ] = idx;
142148 self . waiting_on_key = None ;
143149 }
144150 }
145151
146152 /// Marks they key with index `idx` as being unset
147153 pub fn unset_key ( & mut self , idx : u8 ) {
154+ debug ! ( "Unset key {}" , idx) ;
148155 self . keys [ idx as usize ] = 0 ;
149156 }
150157
@@ -347,7 +354,7 @@ impl Vm {
347354 self . i += vx+1 ;
348355 } ,
349356 ref other => {
350- println ! ( "Instruction not implemented {:?} skipping..." , other)
357+ debug ! ( "Instruction not implemented {:?} skipping..." , other)
351358 }
352359 }
353360 return false ;
@@ -378,9 +385,11 @@ impl Vm {
378385 let sub_steps = ( CLOCK_HZ * dt) . round ( ) as usize ;
379386 let ddt = dt / sub_steps as f32 ;
380387
381- for _ in 0 ..sub_steps {
388+ for step in 0 ..sub_steps {
389+ trace ! ( "Executing step {}/{}" , step, sub_steps) ;
382390 self . time_step ( ddt) ;
383391 if self . waiting_on_key . is_some ( ) {
392+ debug ! ( "Cancel remaining execution steps while waiting for key" ) ;
384393 return ;
385394 }
386395
0 commit comments