Skip to content

Commit

Permalink
adding doc in the code
Browse files Browse the repository at this point in the history
  • Loading branch information
gthvn1 committed Jan 24, 2024
1 parent 7122328 commit aaaef29
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/chip8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,25 @@
use std::{fs::File, io::Read};

pub struct Chip8 {
/// program counter
pc: u16,
/// 4K memory
mem: [u8; 4096],
}

impl Chip8 {
pub fn new(rom_name: &str) -> Self {
/// Loads in memory the `rom` passed as a parameter.
/// The `rom` must be a file that contains a valid ROM.
/// There is no check done when loading it.
pub fn new(rom: &str) -> Self {
let mut chip = Chip8 {
pc: 0x200, // Entry point of our code
mem: [0; 4096],
};
let mut opcode = [0; 4]; // opcode is 4 bytes
let mut pc = chip.pc as usize;

let mut f = File::open(rom_name).unwrap();
let mut f = File::open(rom).unwrap();
while let Ok(()) = f.read_exact(&mut opcode) {
if pc >= (0x0EA0 - 4) {
println!("Memory is full");
Expand All @@ -66,6 +71,7 @@ impl Chip8 {
chip
}

/// Dumps the content of all memory on stdin.
pub fn dump_memory(&self) {
for (i, byte) in self.mem.iter().enumerate() {
if i % 0x10 == 0 {
Expand Down

0 comments on commit aaaef29

Please sign in to comment.