Small refactor and define rom trait

This commit is contained in:
2022-12-10 15:26:48 -05:00
parent c0e0afb1c2
commit 4678647cf0
8 changed files with 80 additions and 3 deletions
+6 -1
View File
@@ -1,5 +1,5 @@
use super::cpu::CPU;
use crate::bus::Bus;
use crate::cpu::bus::Bus;
use crate::utils::addressing::{AddressingMode, IndexRegister};
use crate::utils::alu;
use crate::utils::num_trait::SnesNum;
@@ -1098,6 +1098,11 @@ impl CPU {
self.increment_cycles_test(addressing_mode);
}
pub fn run(&mut self, bus: &mut Bus) {
let opcode = bus.read(self.registers.get_pc_address());
self.execute_opcode(opcode, bus);
}
pub fn execute_opcode(&mut self, opcode: u8, bus: &mut Bus) {
type A = AddressingMode;
type I = IndexRegister;
+2
View File
@@ -1,4 +1,6 @@
pub mod cpu;
pub use cpu::CPU;
pub mod bus;
pub mod registers;
pub mod instructions;
pub mod cycles;
+24
View File
@@ -0,0 +1,24 @@
use crate::cpu::CPU;
use crate::cpu::bus::Bus;
use crate::rom::ROM;
use crate::rom::lo_rom::LoROM;
pub struct Emulator {
cpu: CPU,
bus: Bus,
pub rom: Box<dyn ROM>,
}
impl Emulator {
pub fn new() -> Self {
Self {
cpu: CPU::new(),
bus: Bus::new(),
rom: Box::new(LoROM::new()),
}
}
pub fn run(&mut self) {
self.cpu.run(&mut self.bus);
}
}
+2 -1
View File
@@ -1,4 +1,5 @@
pub mod cpu;
pub mod bus;
pub mod rom;
pub mod utils;
pub mod common;
pub mod emulator;
+28
View File
@@ -0,0 +1,28 @@
use super::{ROM, load_rom};
pub struct LoROM {
data: Vec<u8>,
}
impl LoROM {
pub fn new() -> Self {
Self {
data: vec![],
}
}
}
impl ROM for LoROM {
fn load(&mut self, filename: &String) -> std::io::Result<bool> {
load_rom(filename, &mut self.data)
}
fn read(&self, address: u32) -> u8 {
match self.data.get(address as usize) {
Some(byte) => *byte,
None => 0xFF,
}
}
fn write(&mut self, _address: u32, _value: u8) {}
}
+17
View File
@@ -0,0 +1,17 @@
pub mod lo_rom;
use std::fs::File;
use std::io::Read;
pub fn load_rom(filename: &String, target: &mut Vec<u8>) -> std::io::Result<bool> {
let mut file = File::open(filename)?;
file.read_to_end(target)?;
// TODO: header checksum here
Ok(true)
}
pub trait ROM {
fn load(&mut self, filename: &String) -> std::io::Result<bool>;
fn read(&self, address: u32) -> u8;
fn write(&mut self, address: u32, value: u8);
}
+1 -1
View File
@@ -1,4 +1,4 @@
use crate::bus::Bus;
use crate::cpu::bus::Bus;
/// OPCODE #const
pub fn immediate(pc_addr: u32) -> u32 {