Added writing binary file to memory
This commit is contained in:
parent
7f2742e18a
commit
17b399f98b
69
src/main.rs
69
src/main.rs
|
@ -1,6 +1,13 @@
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
use core::panic;
|
use std::{
|
||||||
use std::{collections::HashMap, ptr::addr_of, thread::sleep, time::Duration, u16};
|
collections::HashMap,
|
||||||
|
fs::{self, File},
|
||||||
|
io::Read,
|
||||||
|
path::PathBuf,
|
||||||
|
thread::sleep,
|
||||||
|
time::Duration,
|
||||||
|
u16,
|
||||||
|
};
|
||||||
|
|
||||||
type Byte = u8;
|
type Byte = u8;
|
||||||
type Word = u16;
|
type Word = u16;
|
||||||
|
@ -27,7 +34,7 @@ impl MemArea {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn contains(&self, address: Word) -> bool {
|
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> {
|
fn swap_page(&mut self, page: usize) -> Result<(), MappingError> {
|
||||||
match page > self.pages {
|
match page > self.pages {
|
||||||
|
@ -53,6 +60,7 @@ struct Mem {
|
||||||
enum MemoryError {
|
enum MemoryError {
|
||||||
Unmapped,
|
Unmapped,
|
||||||
NoDataAtAddress,
|
NoDataAtAddress,
|
||||||
|
Unwritable,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -98,6 +106,17 @@ impl Mem {
|
||||||
}
|
}
|
||||||
Err(MemoryError::Unmapped)
|
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)]
|
#[derive(Debug)]
|
||||||
enum ExecutionError {
|
enum ExecutionError {
|
||||||
|
@ -312,6 +331,9 @@ impl Cpu {
|
||||||
"Cpu tried to read from address {:#06x} but there was no data",
|
"Cpu tried to read from address {:#06x} but there was no data",
|
||||||
self.pc
|
self.pc
|
||||||
),
|
),
|
||||||
|
_ => {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
ExecutionError::InterruptsDisabled => panic!("InterruptsDisabled"),
|
ExecutionError::InterruptsDisabled => panic!("InterruptsDisabled"),
|
||||||
ExecutionError::IncompatibleAddrMode => {
|
ExecutionError::IncompatibleAddrMode => {
|
||||||
|
@ -3396,44 +3418,31 @@ impl Opcode {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let ram = MemArea::new(0x0000, 0x3FFF, 2, "ram".into());
|
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 vram = MemArea::new(0x6000, 0xDFFF, 1, "vram".into());
|
||||||
|
let rom = MemArea::new(0xE000, 0xFFFF, 4, "rom".into());
|
||||||
let mut memory = Mem::new(ram);
|
let mut memory = Mem::new(ram);
|
||||||
match memory.add_area(rom) {
|
match memory.add_area(control) {
|
||||||
Err(error) => println!("Error adding rom: {:?}", error),
|
Err(error) => println!("Error adding vram: {:?}", error),
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
};
|
};
|
||||||
match memory.add_area(vram) {
|
match memory.add_area(vram) {
|
||||||
Err(error) => println!("Error adding vram: {:?}", error),
|
Err(error) => println!("Error adding vram: {:?}", error),
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
};
|
};
|
||||||
match memory.write(0xFFFC, 0x00) {
|
match memory.add_area(rom) {
|
||||||
Err(error) => println!("Error writing to memory at address 0xFFFC: {:?}", error),
|
Err(error) => println!("Error adding rom: {:?}", error),
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
};
|
};
|
||||||
match memory.write(0xFFFD, 0x00) {
|
let binary = match fs::File::open(PathBuf::from(
|
||||||
Err(error) => println!("Error writing to memory at address 0xFFFD: {:?}", error),
|
"/Users/kline/projects/winter/georgeemu/src/george.bin",
|
||||||
Ok(()) => {}
|
)) {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(error) => panic!("Couldn't open binary file! {:?}", error),
|
||||||
};
|
};
|
||||||
match memory.write(0x0000, 0xEA) {
|
match memory.from_bin(binary) {
|
||||||
Err(error) => println!("Error writing to memory at address 0x0000: {:?}", error),
|
Err(error) => println!("{:?}", error),
|
||||||
Ok(()) => {}
|
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(()) => {}
|
|
||||||
};
|
};
|
||||||
let mut cpu = Cpu::new(memory);
|
let mut cpu = Cpu::new(memory);
|
||||||
match cpu.reset() {
|
match cpu.reset() {
|
||||||
|
|
Loading…
Reference in New Issue