Skip to content

Commit 19ac187

Browse files
committed
add config file with simple menu
1 parent ba1aa80 commit 19ac187

File tree

6 files changed

+391
-104
lines changed

6 files changed

+391
-104
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ SDL2main.lib
88
SDL2test.lib
99

1010
# unimportant notes
11-
notes/todo.md
11+
notes/todo.md
12+
13+
# test folders
14+
programs
15+
fonts
16+
config

Cargo.lock

Lines changed: 188 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
# used for generating random numbers
910
rand = "0.7.3"
1011

12+
# used for serializing and deserializing the config
13+
serde = { version = "1", features = ["derive"] }
14+
15+
# used for loading and storing the config file
16+
confy = "0.4.0"
17+
18+
# used for GUI
1119
[dependencies.sdl2]
1220
version = "0.35"
1321
default-features = true

src/chip8.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ use rand::rngs::ThreadRng;
1212
/// specifies the ID of the VF register which is often used for flags
1313
const FLAG_REG_ID: u8 = 0xF;
1414

15-
/// specifies the address where the font data is stored in memory
15+
/// specifies the address where the fonts data is stored in memory
1616
const FONT_START_ADDRESS: u16 = 0x050;
1717

1818
/// specifies the address where the program is stored in memory
1919
pub const PROGRAM_START_ADDRESS: u16 = 0x200;
2020

2121
const INSTRUCTION_EXEC_DURATION: Duration = Duration::from_nanos(1_428_571); // 1_428_571
2222

23+
const FONT_DATA_SIZE: usize = 80; // 5 rows per char * 16 chars
24+
2325
#[derive(Debug)]
2426
pub enum Chip8Error {
2527
InstructionNotImplemented(String),
26-
28+
MemoryOverflow(String),
29+
InvalidFontData(String),
2730
}
2831

2932
pub struct Chip8 {
@@ -470,7 +473,7 @@ impl Chip8 {
470473
self.exec_time += frame_duration;
471474

472475
// run instructions
473-
while self.exec_time >= INSTRUCTION_EXEC_DURATION {
476+
while self.exec_time >= INSTRUCTION_EXEC_DURATION && !self.reached_end_of_file {
474477
self.exec_next_instruction()?;
475478
self.exec_time -= INSTRUCTION_EXEC_DURATION;
476479
}
@@ -497,9 +500,9 @@ impl Chip8 {
497500
}
498501
}
499502

500-
pub fn load_bytes_into_memory(&mut self, bytes: &Vec<u8>, address: u16) {
501-
for (idx, byte) in bytes.iter().enumerate() {
502-
self.memory[(address as usize) + idx] = *byte;
503+
pub fn load_bytes_into_memory(&mut self, data: &Vec<u8>, address: u16) {
504+
for (offset, byte) in data.iter().enumerate() {
505+
self.memory[(address as usize) + offset] = *byte;
503506
}
504507
}
505508

@@ -518,16 +521,24 @@ impl Chip8 {
518521
}
519522
}
520523

521-
pub fn load_font_into_memory(&mut self, font_data: [[u8; 5]; 16]) {
522-
let mut address: u16 = FONT_START_ADDRESS;
523-
for character in font_data {
524-
for byte in character {
525-
self.memory[address as usize] = byte;
526-
address += 1;
527-
}
524+
pub fn load_program(&mut self, program_data: &Vec<u8>) -> Result<(), Chip8Error> {
525+
if program_data.len() > (self.memory.len() - (PROGRAM_START_ADDRESS as usize)) {
526+
return Err(Chip8Error::MemoryOverflow(String::from("the program does not fit into its predefined memory space")));
527+
}
528+
529+
self.load_bytes_into_memory(program_data, PROGRAM_START_ADDRESS);
530+
531+
return Ok(());
532+
}
533+
534+
pub fn load_font(&mut self, font_data: &Vec<u8>) -> Result<(), Chip8Error> {
535+
if font_data.len() != FONT_DATA_SIZE {
536+
return Err(Chip8Error::InvalidFontData(String::from("the fonts data does not fit into its predefined memory space")));
528537
}
529538

530-
assert_eq!(address, 0xA0);
539+
self.load_bytes_into_memory(font_data, FONT_START_ADDRESS);
540+
541+
return Ok(());
531542
}
532543

533544
pub fn load_register(&mut self, reg_id: u8, value: u8) {
@@ -1072,7 +1083,7 @@ mod tests {
10721083
run_emulator(&mut chip8);
10731084

10741085
// verify result
1075-
assert_eq!(chip8.index_reg, FONT_START_ADDRESS + (15 * 5), "failed to correctly set the index register to the font location; index_reg: 0x{:04x}; character: 0x{:02x}", chip8.index_reg, val_1);
1086+
assert_eq!(chip8.index_reg, FONT_START_ADDRESS + (15 * 5), "failed to correctly set the index register to the fonts location; index_reg: 0x{:04x}; character: 0x{:02x}", chip8.index_reg, val_1);
10761087
}
10771088

10781089
#[test]

0 commit comments

Comments
 (0)