chore: Rename Operand -> ImmediateOperand

This commit is contained in:
2025-06-05 10:07:57 +09:00
parent ef4663a245
commit 232b73aad8
8 changed files with 141 additions and 130 deletions

View File

@@ -1,4 +1,4 @@
use crate::operands::{Byte, Displacement, MemoryIndex, Operand, Word};
use crate::operands::{Byte, Displacement, ImmediateOperand, MemoryIndex, Word};
#[derive(Debug, Clone, Copy)]
pub struct Memory {
@@ -10,23 +10,23 @@ impl Memory {
Self { memory: [0; 65536] }
}
// Write an [`Operand`] to a memory location indexed by a [`MemoryIndex`].
// Write an [`ImmediateOperand`] to a memory location indexed by a [`MemoryIndex`].
pub fn write(
&mut self,
regs: &crate::interpreter::register::Register,
idx: MemoryIndex,
val: Operand,
val: ImmediateOperand,
) {
match Memory::calc_memidx(regs, idx) {
Operand::Byte(idx_byte) => match val {
Operand::Byte(value) => {
ImmediateOperand::Byte(idx_byte) => match val {
ImmediateOperand::Byte(value) => {
log::debug!("Writing byte {value:#04x} to memory location {idx_byte:#04x}");
self.memory[idx_byte as usize] = value
}
Operand::Word(_) => panic!("Cannot add Word to Byte Memory Index"),
ImmediateOperand::Word(_) => panic!("Cannot add Word to Byte Memory Index"),
},
Operand::Word(idx_word) => match val {
Operand::Word(value) => {
ImmediateOperand::Word(idx_word) => match val {
ImmediateOperand::Word(value) => {
let byte1 = idx_word / 2;
let byte2 = idx_word / 2 + 1;
let [low, high]: [u8; 2] = value.to_le_bytes();
@@ -36,7 +36,7 @@ impl Memory {
self.memory[byte1 as usize] = low;
self.memory[byte1 as usize] = high;
}
Operand::Byte(value) => {
ImmediateOperand::Byte(value) => {
self.memory[(idx_word * 2) as usize] = value;
}
},
@@ -44,17 +44,21 @@ impl Memory {
}
/// Read into memory with a [`MemoryIndex`] as index.
pub fn read(&self, regs: &crate::interpreter::register::Register, idx: MemoryIndex) -> Operand {
pub fn read(
&self,
regs: &crate::interpreter::register::Register,
idx: MemoryIndex,
) -> ImmediateOperand {
match Memory::calc_memidx(regs, idx) {
Operand::Byte(byte) => {
ImmediateOperand::Byte(byte) => {
log::debug!("Reading byte {byte:#04x} from memory");
Operand::Byte(self.memory[byte as usize])
ImmediateOperand::Byte(self.memory[byte as usize])
}
Operand::Word(word) => {
ImmediateOperand::Word(word) => {
let byte1 = word / 2;
let byte2 = word / 2 + 1;
log::debug!("Reading bytes {byte1:#04x} and {byte2:#04x} from memory");
Operand::Word(Word::from_le_bytes([
ImmediateOperand::Word(Word::from_le_bytes([
self.memory[byte1 as usize],
self.memory[byte2 as usize],
]))
@@ -63,9 +67,12 @@ impl Memory {
}
/// Calculate the absolute Memory Index from a [`MemoryIndex`] struct.
fn calc_memidx(regs: &crate::interpreter::register::Register, idx: MemoryIndex) -> Operand {
let mut base = Operand::Word(0);
let mut index = Operand::Word(0);
fn calc_memidx(
regs: &crate::interpreter::register::Register,
idx: MemoryIndex,
) -> ImmediateOperand {
let mut base = ImmediateOperand::Word(0);
let mut index = ImmediateOperand::Word(0);
let mut disp = Displacement::IWord(0);
if let Some(base_reg) = idx.base {