96 lines
2.8 KiB
Rust
96 lines
2.8 KiB
Rust
use log::{trace};
|
|
|
|
use crate::constants::*;
|
|
|
|
#[derive(Clone, Copy)]
|
|
pub struct Chip8SystemMemory {
|
|
memory: [u8; CHIP8_MEMORY_SIZE as usize],
|
|
}
|
|
|
|
impl Default for Chip8SystemMemory {
|
|
fn default() -> Self {
|
|
|
|
let mut x = Chip8SystemMemory {
|
|
memory: [0x00; CHIP8_MEMORY_SIZE as usize],
|
|
};
|
|
|
|
x.load_fonts_to_memory();
|
|
x.load_schip_fonts_to_memory();
|
|
x
|
|
}
|
|
}
|
|
impl Chip8SystemMemory {
|
|
|
|
pub fn reset(&mut self){
|
|
self.memory = [0x00; CHIP8_MEMORY_SIZE as usize];
|
|
self.load_fonts_to_memory();
|
|
self.load_schip_fonts_to_memory();
|
|
}
|
|
|
|
pub fn new() -> Self {
|
|
Chip8SystemMemory {
|
|
memory: [0x00; CHIP8_MEMORY_SIZE as usize],
|
|
}
|
|
}
|
|
|
|
pub fn peek(&self, address: u16) -> u8 {
|
|
trace!("PEEK: {} / {}", address, self.memory[address as usize].clone());
|
|
let effective = address as i32 % CHIP8_MEMORY_SIZE;
|
|
self.memory[effective as usize]
|
|
}
|
|
|
|
pub fn poke(&mut self, address: u16, value: u8) {
|
|
trace!("POKE: {} / {} to {}", address, self.memory[address as usize], value);
|
|
self.memory[address as usize] = value;
|
|
}
|
|
|
|
pub fn load_program(&mut self, program_to_load: Box<Vec<u8>>) {
|
|
for load_index in 0..program_to_load.len() {
|
|
self.poke((load_index + 0x200) as u16, program_to_load[load_index]);
|
|
}
|
|
}
|
|
|
|
pub fn load_fonts_to_memory(&mut self) {
|
|
let all_font_characters = [
|
|
CHIP8FONT_0,
|
|
CHIP8FONT_1,
|
|
CHIP8FONT_2,
|
|
CHIP8FONT_3,
|
|
CHIP8FONT_4,
|
|
CHIP8FONT_5,
|
|
CHIP8FONT_6,
|
|
CHIP8FONT_7,
|
|
CHIP8FONT_8,
|
|
CHIP8FONT_9,
|
|
CHIP8FONT_A,
|
|
CHIP8FONT_B,
|
|
CHIP8FONT_C,
|
|
CHIP8FONT_D,
|
|
CHIP8FONT_E,
|
|
CHIP8FONT_F,
|
|
];
|
|
|
|
for (font_index, current_font) in all_font_characters.iter().enumerate() {
|
|
for font_mem_offset in 0..=4 {
|
|
let real_offset = font_index * 5 + font_mem_offset;
|
|
self.poke(real_offset as u16, current_font[font_mem_offset]);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn load_schip_fonts_to_memory(&mut self) {
|
|
let all_font_characters = [
|
|
SCHIPFONT_0, SCHIPFONT_1, SCHIPFONT_2, SCHIPFONT_3,
|
|
SCHIPFONT_4, SCHIPFONT_5, SCHIPFONT_6, SCHIPFONT_7,
|
|
SCHIPFONT_8, SCHIPFONT_9, SCHIPFONT_A, SCHIPFONT_B,
|
|
SCHIPFONT_C, SCHIPFONT_D, SCHIPFONT_E, SCHIPFONT_F
|
|
];
|
|
for (font_index, current_font) in all_font_characters.iter().enumerate() {
|
|
let base_offset = 0x100;
|
|
for font_mem_offset in 0..=4 {
|
|
let real_offset = base_offset + font_index * 0x10 + font_mem_offset;
|
|
self.poke(real_offset as u16, current_font[font_mem_offset]);
|
|
}
|
|
}
|
|
}
|
|
} |