Skip to content

Commit 2d35419

Browse files
author
robo9k
committed
Add logging via log crate
1 parent 4f591f3 commit 2d35419

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ authors = [
1515
]
1616

1717
[dependencies]
18-
rand = "0.1.2"
18+
rand = "0.1.3"
19+
log = "0.2.4"

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
extern crate rand;
2828

29+
#[macro_use]
30+
extern crate log;
31+
2932
pub mod error;
3033
pub mod instructions;
3134
pub mod vm;

src/vm.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)