summaryrefslogtreecommitdiff
path: root/src/core.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/core.rs')
-rw-r--r--src/core.rs45
1 files changed, 23 insertions, 22 deletions
diff --git a/src/core.rs b/src/core.rs
index ef3c115..a85d830 100644
--- a/src/core.rs
+++ b/src/core.rs
@@ -1,4 +1,5 @@
use crate::rom::Rom;
+use crate::memory::Memory;
use std::fmt;
#[derive(Debug, Clone, Copy)]
@@ -360,21 +361,21 @@ impl IncompleteOp {
impl Core {
/// Constructor
- pub fn new_with_rom_from_hex(hex: String) -> Result<Self, String> {
- let mut ram: [u8; u8::max_value() as usize + 1] = [0; u8::max_value() as usize + 1];
+ pub fn new() -> Self {
+ let mut ram = [0; u8::max_value() as usize + 1];
ram[RAM_OFFSET_SP as usize] = RESET_VALUE_SP;
ram[RAM_OFFSET_ACC as usize] = RESET_VALUE_ACC;
- let progmem = match Rom::from_hex(hex) {
- Ok(value) => value,
- Err(err_string) => return Err(err_string),
- };
- let extmem: [u8; u16::max_value() as usize + 1] = [0xFF; u16::max_value() as usize + 1];
- Ok(Self {
+ Self {
pc: 0,
ram,
- progmem,
- extmem,
- })
+ progmem: Rom::new(),
+ extmem: [0xFF; u16::max_value() as usize + 1],
+ }
+ }
+
+ pub fn rom(mut self, rom: Rom) -> Self {
+ self.progmem = rom;
+ self
}
/// Fetch and execute one instruction
@@ -391,14 +392,14 @@ impl Core {
/// Get current instruction
pub fn op(&self) -> Op {
- let op = IncompleteOp::from_u8(self.progmem.array[self.pc as usize]);
+ let op = IncompleteOp::from_u8(self.progmem.get(self.pc));
let operand1 = if let Some(incomplete_operand) = op.operand1 {
Some(self.fetch_operand(incomplete_operand, 1))
} else {
None
};
let operand2_offset = match &operand1 {
- Some(operand) => operand.size(),
+ Some(operand) => operand.size() as u16,
_ => 0,
};
let operand2 = if let Some(incomplete_operand) = op.operand2 {
@@ -452,18 +453,18 @@ impl Core {
}
/// Get operand by offset, based on meta information
- fn fetch_operand(&self, operand: IncompleteOperand, offset: usize) -> Operand {
+ fn fetch_operand(&self, operand: IncompleteOperand, offset: u16) -> Operand {
match operand {
IncompleteOperand::Acc => Operand::Acc,
IncompleteOperand::Direct => {
- Operand::Direct(self.progmem.array[(self.pc as usize + offset)])
+ Operand::Direct(self.progmem.get(self.pc + offset))
}
IncompleteOperand::Data => {
- Operand::Data(self.progmem.array[(self.pc as usize + offset)])
+ Operand::Data(self.progmem.get(self.pc + offset))
}
IncompleteOperand::Data16 => Operand::Data16({
- let high = self.progmem.array[(self.pc as usize + offset)];
- let low = self.progmem.array[(self.pc as usize + offset + 1)];
+ let high = self.progmem.get(self.pc + offset);
+ let low = self.progmem.get(self.pc + offset + 1);
u16::from(high) << 8 & u16::from(low)
}),
IncompleteOperand::Dptr => Operand::Dptr,
@@ -471,15 +472,15 @@ impl Core {
IncompleteOperand::Indirect(r) => Operand::Indirect(r),
IncompleteOperand::Register(r) => Operand::Register(r),
IncompleteOperand::Reladdr => {
- Operand::Reladdr(self.progmem.array[(self.pc as usize + offset)] as i8)
+ Operand::Reladdr(self.progmem.get(self.pc + offset) as i8)
}
IncompleteOperand::Addr11(high) => Operand::Addr11({
- let low = self.progmem.array[(self.pc as usize + offset)];
+ let low = self.progmem.get(self.pc + offset);
u16::from(high) << 8 & u16::from(low)
}),
IncompleteOperand::Addr16 => Operand::Addr16({
- let high = self.progmem.array[(self.pc as usize + offset)];
- let low = self.progmem.array[(self.pc as usize + offset + 1)];
+ let high = self.progmem.get(self.pc + offset);
+ let low = self.progmem.get(self.pc + offset + 1);
u16::from(high) << 8 & u16::from(low)
}),
}