From 8422925312cf5d64d0411378e19444254d51541a Mon Sep 17 00:00:00 2001 From: august Date: Fri, 2 Feb 2024 13:44:15 -0500 Subject: [PATCH] Added writing binary file to memory --- src/main.rs | 69 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index 15cffa1..a762028 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,13 @@ #![allow(dead_code)] -use core::panic; -use std::{collections::HashMap, ptr::addr_of, thread::sleep, time::Duration, u16}; +use std::{ + collections::HashMap, + fs::{self, File}, + io::Read, + path::PathBuf, + thread::sleep, + time::Duration, + u16, +}; type Byte = u8; type Word = u16; @@ -27,7 +34,7 @@ impl MemArea { } } fn contains(&self, address: Word) -> bool { - self.start <= address && self.end >= address + self.start <= address && self.end > address } fn swap_page(&mut self, page: usize) -> Result<(), MappingError> { match page > self.pages { @@ -53,6 +60,7 @@ struct Mem { enum MemoryError { Unmapped, NoDataAtAddress, + Unwritable, } #[derive(Debug)] @@ -98,6 +106,17 @@ impl Mem { } Err(MemoryError::Unmapped) } + + fn from_bin(&mut self, f: File) -> Result<(), MemoryError> { + let bytes = f.bytes(); + for (address, byte) in bytes.enumerate() { + match byte { + Ok(value) => self.write(address as u16, value)?, + Err(_) => return Err(MemoryError::Unwritable), + } + } + Ok(()) + } } #[derive(Debug)] enum ExecutionError { @@ -312,6 +331,9 @@ impl Cpu { "Cpu tried to read from address {:#06x} but there was no data", self.pc ), + _ => { + unimplemented!() + } }, ExecutionError::InterruptsDisabled => panic!("InterruptsDisabled"), ExecutionError::IncompatibleAddrMode => { @@ -3396,44 +3418,31 @@ impl Opcode { fn main() { let ram = MemArea::new(0x0000, 0x3FFF, 2, "ram".into()); - let rom = MemArea::new(0xE000, 0xFFFF, 4, "rom".into()); + let control = MemArea::new(0x4000, 0x5FFF, 4, "rom".into()); let vram = MemArea::new(0x6000, 0xDFFF, 1, "vram".into()); + let rom = MemArea::new(0xE000, 0xFFFF, 4, "rom".into()); let mut memory = Mem::new(ram); - match memory.add_area(rom) { - Err(error) => println!("Error adding rom: {:?}", error), + match memory.add_area(control) { + Err(error) => println!("Error adding vram: {:?}", error), Ok(()) => {} }; match memory.add_area(vram) { Err(error) => println!("Error adding vram: {:?}", error), Ok(()) => {} }; - match memory.write(0xFFFC, 0x00) { - Err(error) => println!("Error writing to memory at address 0xFFFC: {:?}", error), + match memory.add_area(rom) { + Err(error) => println!("Error adding rom: {:?}", error), Ok(()) => {} }; - match memory.write(0xFFFD, 0x00) { - Err(error) => println!("Error writing to memory at address 0xFFFD: {:?}", error), - Ok(()) => {} + let binary = match fs::File::open(PathBuf::from( + "/Users/kline/projects/winter/georgeemu/src/george.bin", + )) { + Ok(file) => file, + Err(error) => panic!("Couldn't open binary file! {:?}", error), }; - match memory.write(0x0000, 0xEA) { - Err(error) => println!("Error writing to memory at address 0x0000: {:?}", error), - Ok(()) => {} - }; - match memory.write(0x0001, 0xA0) { - Err(error) => println!("Error writing to memory at address 0x0000: {:?}", error), - Ok(()) => {} - }; - match memory.write(0x0002, 0xAE) { - Err(error) => println!("Error writing to memory at address 0x0000: {:?}", error), - Ok(()) => {} - }; - match memory.write(0x0003, 0xA2) { - Err(error) => println!("Error writing to memory at address 0x0000: {:?}", error), - Ok(()) => {} - }; - match memory.write(0x0004, 0xA2) { - Err(error) => println!("Error writing to memory at address 0x0000: {:?}", error), - Ok(()) => {} + match memory.from_bin(binary) { + Err(error) => println!("{:?}", error), + Ok(_) => {} }; let mut cpu = Cpu::new(memory); match cpu.reset() {