summaryrefslogtreecommitdiff
path: root/src/state
diff options
context:
space:
mode:
Diffstat (limited to 'src/state')
-rw-r--r--src/state/bank.rs89
-rw-r--r--src/state/new.rs75
-rw-r--r--src/state/read.rs85
-rw-r--r--src/state/shifter_value.rs130
-rw-r--r--src/state/write.rs125
5 files changed, 504 insertions, 0 deletions
diff --git a/src/state/bank.rs b/src/state/bank.rs
new file mode 100644
index 0000000..7cab594
--- /dev/null
+++ b/src/state/bank.rs
@@ -0,0 +1,89 @@
+/*
+ Copyright 2021-2023 Gabriel Jensen.
+
+ This file is part of Luma.
+
+ Luma is free software: you can redistribute it
+ and/or modify it under the terms of the GNU
+ Affero register Public License as published by
+ the Free Software Foundation, either version 3
+ of the License, or (at your option) any later
+ version.
+
+ Luma is distributed in the hope that it will be
+ useful, but WITHOUT ANY WARRANTY; without even
+ the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Affero register Public License for more details.
+
+ You should have received a copy of the GNU
+ Affero register Public License along with Luma.
+ If not, see <https://www.gnu.org/licenses/>.
+*/
+
+use crate::{log, log_status};
+use crate::cpu_mode::CpuMode;
+use crate::state::State;
+
+impl State {
+ pub fn bank(&mut self, mode: CpuMode) {
+ log_status!("banking to {}", mode.name());
+
+ let bank_index = bank_index(mode);
+
+ self.registers[0x0] = &mut self.banks[bank_index.0][ 0x0] as *mut u32;
+ self.registers[0x1] = &mut self.banks[bank_index.1][ 0x1] as *mut u32;
+ self.registers[0x2] = &mut self.banks[bank_index.2][ 0x2] as *mut u32;
+ self.registers[0x3] = &mut self.banks[bank_index.3][ 0x3] as *mut u32;
+ self.registers[0x4] = &mut self.banks[bank_index.4][ 0x4] as *mut u32;
+ self.registers[0x5] = &mut self.banks[bank_index.5][ 0x5] as *mut u32;
+ self.registers[0x6] = &mut self.banks[bank_index.6][ 0x6] as *mut u32;
+ self.registers[0x7] = &mut self.banks[bank_index.7][ 0x7] as *mut u32;
+ self.registers[0x8] = &mut self.banks[bank_index.8][ 0x8] as *mut u32;
+ self.registers[0x9] = &mut self.banks[bank_index.9][ 0x9] as *mut u32;
+ self.registers[0xA] = &mut self.banks[bank_index.10][0xA] as *mut u32;
+ self.registers[0xB] = &mut self.banks[bank_index.11][0xB] as *mut u32;
+ self.registers[0xC] = &mut self.banks[bank_index.12][0xC] as *mut u32;
+ self.registers[0xD] = &mut self.banks[bank_index.13][0xD] as *mut u32;
+ self.registers[0xE] = &mut self.banks[bank_index.14][0xE] as *mut u32;
+ self.registers[0xF] = &mut self.banks[bank_index.15][0xF] as *mut u32;
+
+ log!("new register layout:");
+ log!(" r0: {:#010X} r4: {:#010X} r8: {:#010X} r12: {:#010X}", self.read_register(0x0), self.read_register(0x4), self.read_register(0x8), self.read_register(0xC));
+ log!(" r1: {:#010X} r5: {:#010X} r9: {:#010X} sp: {:#010X}", self.read_register(0x1), self.read_register(0x5), self.read_register(0x9), self.read_register(0xD));
+ log!(" r2: {:#010X} r6: {:#010X} r10: {:#010X} lr: {:#010X}", self.read_register(0x2), self.read_register(0x6), self.read_register(0xA), self.read_register(0xE));
+ log!(" r3: {:#010X} r7: {:#010X} r11: {:#010X} pc: {:#010X}", self.read_register(0x3), self.read_register(0x7), self.read_register(0xB), self.read_register(0xF));
+ }
+}
+
+#[must_use]
+fn bank_index(mode: CpuMode) -> (
+ usize, // r0
+ usize, // r1
+ usize, // r2
+ usize, // r3
+ usize, // r4
+ usize, // r5
+ usize, // r6
+ usize, // r7
+ usize, // r8
+ usize, // r9
+ usize, // r10
+ usize, // r11
+ usize, // r12
+ usize, // sp
+ usize, // lr
+ usize, // pc
+) {
+ use CpuMode::*;
+
+ return match mode {
+ User => (0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0),
+ System => (0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0),
+ FastInterruptRequest => (0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x0),
+ InterruptRequest => (0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x0),
+ Supervisor => (0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x3, 0x0),
+ Abort => (0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x4, 0x0),
+ Undefined => (0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5, 0x5, 0x0),
+ };
+}
diff --git a/src/state/new.rs b/src/state/new.rs
new file mode 100644
index 0000000..bb6f87e
--- /dev/null
+++ b/src/state/new.rs
@@ -0,0 +1,75 @@
+/*
+ Copyright 2021-2023 Gabriel Jensen.
+
+ This file is part of Luma.
+
+ Luma is free software: you can redistribute it
+ and/or modify it under the terms of the GNU
+ Affero register Public License as published by
+ the Free Software Foundation, either version 3
+ of the License, or (at your option) any later
+ version.
+
+ Luma is distributed in the hope that it will be
+ useful, but WITHOUT ANY WARRANTY; without even
+ the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Affero register Public License for more details.
+
+ You should have received a copy of the GNU
+ Affero register Public License along with Luma.
+ If not, see <https://www.gnu.org/licenses/>.
+*/
+
+use crate::log;
+use crate::MEMORY_LENGTH;
+use crate::cpu_mode::CpuMode;
+use crate::state::State;
+
+use std::ptr::null_mut;
+
+impl State {
+ #[must_use]
+ pub fn new() -> Self {
+ log!("initialising new state");
+
+ let banks = Box::new([DEFAULT_REGISTER_VALUES; 0x6]);
+
+ let cpsr = 0b00000000000000000000000000011111;
+
+ let memory: Vec::<u32> = vec![0x0; MEMORY_LENGTH as usize / 0x4];
+
+ let mut state = Self {
+ registers: [null_mut(); 0x10],
+ banks: banks,
+
+ cpsr: cpsr,
+ spsr: [0b00000000000000000000000000000000; 0x6],
+
+ memory: memory,
+ };
+
+ state.bank(CpuMode::System);
+
+ return state;
+ }
+}
+
+const DEFAULT_REGISTER_VALUES: [u32; 0x10] = [
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000008,
+];
diff --git a/src/state/read.rs b/src/state/read.rs
new file mode 100644
index 0000000..927d528
--- /dev/null
+++ b/src/state/read.rs
@@ -0,0 +1,85 @@
+/*
+ Copyright 2021-2023 Gabriel Jensen.
+
+ This file is part of Luma.
+
+ Luma is free software: you can redistribute it
+ and/or modify it under the terms of the GNU
+ Affero General Public License as published by
+ the Free Software Foundation, either version 3
+ of the License, or (at your option) any later
+ version.
+
+ Luma is distributed in the hope that it will be
+ useful, but WITHOUT ANY WARRANTY; without even
+ the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Affero General Public License for more details.
+
+ You should have received a copy of the GNU
+ Affero General Public License along with Luma.
+ If not, see <https://www.gnu.org/licenses/>.
+*/
+
+use crate::{Error, MEMORY_LENGTH};
+use crate::cpu_mode::CpuMode;
+use crate::state::State;
+
+const MAX_BYTE_ADDRESS: u32 = MEMORY_LENGTH;
+const MAX_HALFWORD_ADDRESS: u32 = MEMORY_LENGTH - 0x1;
+const MAX_WORD_ADDRESS: u32 = MEMORY_LENGTH - 0x3;
+
+impl State {
+ #[inline(always)]
+ #[must_use]
+ pub fn read_register(&self, register: u8) -> u32 {
+ // Limit to 0..=15.
+ let index = (register & 0b00001111) as usize;
+
+ return unsafe { **self.registers.get_unchecked(index) };
+ }
+
+ #[must_use]
+ pub fn read_word(&self, address: u32) -> u32 {
+ if address > MAX_WORD_ADDRESS { Error::OutOfBounds( address).trap(); return 0xFFFFFFFF; }
+ if address % 0x4 != 0x0 { Error::BadAlignment(address, 0x4).trap(); return 0xFFFFFFFF; }
+
+ unsafe {
+ let pointer = (self.memory.as_ptr() as *const u8).add(address as usize) as *const u32;
+ return *pointer;
+ }
+ }
+
+ #[must_use]
+ pub fn read_halfword(&self, address: u32) -> u16 {
+ if address > MAX_HALFWORD_ADDRESS { Error::OutOfBounds( address).trap(); return 0xFFFF; }
+ if address % 0x2 != 0x0 { Error::BadAlignment(address, 0x2).trap(); return 0xFFFF; }
+
+ unsafe {
+ let pointer = (self.memory.as_ptr() as *const u8).add(address as usize) as *const u16;
+ return *pointer;
+ }
+ }
+
+ #[must_use]
+ pub fn read_byte(&self, address: u32) -> u8 {
+ if address > MAX_BYTE_ADDRESS { Error::OutOfBounds(address).trap(); return 0xFF; }
+
+ unsafe {
+ let pointer = (self.memory.as_ptr() as *const u8).add(address as usize);
+ return *pointer;
+ }
+ }
+
+ #[inline(always)]
+ #[must_use]
+ pub fn read_cpsr(&self) -> u32 {
+ return self.cpsr;
+ }
+
+ #[inline(always)]
+ #[must_use]
+ pub fn read_spsr(&self, mode: CpuMode) -> u32 {
+ return unsafe { *self.spsr.get_unchecked(Self::spsr_index(mode)) };
+ }
+}
diff --git a/src/state/shifter_value.rs b/src/state/shifter_value.rs
new file mode 100644
index 0000000..58821fe
--- /dev/null
+++ b/src/state/shifter_value.rs
@@ -0,0 +1,130 @@
+/*
+ Copyright 2021-2023 Gabriel Jensen.
+
+ This file is part of Luma.
+
+ Luma is free software: you can redistribute it
+ and/or modify it under the terms of the GNU
+ Affero General Public License as published by
+ the Free Software Foundation, either version 3
+ of the License, or (at your option) any later
+ version.
+
+ Luma is distributed in the hope that it will be
+ useful, but WITHOUT ANY WARRANTY; without even
+ the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Affero General Public License for more details.
+
+ You should have received a copy of the GNU
+ Affero General Public License along with Luma.
+ If not, see <https://www.gnu.org/licenses/>.
+*/
+
+use crate::shifter::Shifter;
+use crate::state::State;
+
+impl State {
+ #[must_use]
+ pub fn shifter_value(&self, shifter: Shifter) -> (u32, bool) {
+ use Shifter::*;
+
+ let c = self.read_cpsr() & 0b00100000000000000000000000000000 != 0x0;
+
+ let (value, carry) = match shifter {
+ Immediate(imm, rot) => {
+ let result = (imm as u32).rotate_right(rot as u32);
+ let c = if rot == 0x0 { c } else { result & 0b10000000000000000000000000000000 != 0x0 };
+
+ (result, c)
+ },
+
+ ArithmeticShiftRightImmediate(rm, imm) => {
+ let rm_value = self.read_register(rm);
+
+ let result = (rm_value as i32).wrapping_shr(imm as u32) as u32;
+ let c = false; // TODO
+
+ (result, c)
+ },
+
+ ArithmeticShiftRightRegister(rm, rs) => {
+ let rm_value = self.read_register(rm);
+ let rs_value = self.read_register(rs);
+
+ let result = (rm_value as i32).wrapping_shr(rs_value) as u32;
+ let c = false; // TODO
+
+ (result, c)
+ },
+
+ LogicalShiftLeftImmediate(rm, imm) => {
+ let rm_value = self.read_register(rm);
+
+ let result = rm_value.wrapping_shl(imm as u32);
+ let c = false; // TODO
+
+ (result, c)
+ },
+
+ LogicalShiftLeftRegister(rm, rs) => {
+ let rm_value = self.read_register(rm);
+ let rs_value = self.read_register(rs);
+
+ let result = rm_value.wrapping_shl(rs_value);
+ let c = false; // TODO
+
+ (result, c)
+ },
+
+ LogicalShiftRightImmediate(rm, imm) => {
+ let rm_value = self.read_register(rm);
+
+ let result = rm_value.wrapping_shr(imm as u32);
+ let c = false; // TODO
+
+ (result, c)
+ },
+
+ LogicalShiftRightRegister(rm, rs) => {
+ let rm_value = self.read_register(rm);
+ let rs_value = self.read_register(rs);
+
+ let result = rm_value.wrapping_shr(rs_value);
+ let c = false; // TODO
+
+ (result, c)
+ },
+
+ RotateRightImmediate(rm, imm) => {
+ let rm_value = self.read_register(rm);
+
+ let result = rm_value.rotate_right(imm as u32);
+ let c = false; // TODO
+
+ (result, c)
+ },
+
+ RotateRightRegister(rm, rs) => {
+ let rm_value = self.read_register(rm);
+ let rs_value = self.read_register(rs);
+
+ let result = rm_value.rotate_right(rs_value);
+ let c = false; // TODO
+
+ (result, c)
+ },
+
+ RotateRightExtend(rm) => {
+ let rm_value = self.read_register(rm);
+
+ let result = 0x0_u32; // TODO
+ let c = rm_value & 0b00000000000000000000000000000001 != 0x0;
+
+ (result, c)
+ },
+ };
+
+ return (value, carry);
+ }
+}
diff --git a/src/state/write.rs b/src/state/write.rs
new file mode 100644
index 0000000..10955e2
--- /dev/null
+++ b/src/state/write.rs
@@ -0,0 +1,125 @@
+/*
+ Copyright 2021-2023 Gabriel Jensen.
+
+ This file is part of Luma.
+
+ Luma is free software: you can redistribute it
+ and/or modify it under the terms of the GNU
+ Affero General Public License as published by
+ the Free Software Foundation, either version 3
+ of the License, or (at your option) any later
+ version.
+
+ Luma is distributed in the hope that it will be
+ useful, but WITHOUT ANY WARRANTY; without even
+ the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Affero General Public License for more details.
+
+ You should have received a copy of the GNU
+ Affero General Public License along with Luma.
+ If not, see <https://www.gnu.org/licenses/>.
+*/
+
+use crate::{Error, log_assignment, MEMORY_LENGTH};
+use crate::cpu_mode::CpuMode;
+use crate::state::{address_unused, State};
+
+macro_rules! read_only {
+ ($address: expr) => {{
+ match $address {
+ 0x00000000..=0x00003FFF => true,
+ 0x04000130..=0x04000131 => true, // KEYINPUT
+ 0x08000000..=0x0DFFFFFF => true,
+
+ _ => false,
+ }
+ }};
+}
+
+impl State {
+ #[inline(always)]
+ pub fn write_register(&mut self, register: u8, value: u32) {
+ log_assignment!(format!("r{register}"), format!("{value:#010X}"));
+
+ let index = (register & 0b00001111) as usize;
+
+ unsafe { **self.registers.get_unchecked_mut(index) = value };
+ }
+
+ pub fn write_word(&mut self, address: u32, value: u32) {
+ log_assignment!(format!("{address:#010X}"), format!("{value:#010X}"));
+
+ if address > MEMORY_LENGTH - 0x4 { Error::OutOfBounds( address).trap(); return; }
+ if address % 0x4 != 0x0 { Error::BadAlignment(address, 0x4).trap(); return; }
+
+ if read_only!(address) || address_unused!(address) { return };
+
+ unsafe {
+ let pointer = (self.memory.as_mut_ptr() as *mut u8).add(address as usize) as *mut u32;
+ *pointer = value;
+ }
+ }
+
+ pub fn write_halfword(&mut self, address: u32, value: u16) {
+ log_assignment!(format!("{address:#010X}"), format!("{value:#06X}"));
+
+ if address > MEMORY_LENGTH - 0x2 { Error::OutOfBounds( address).trap(); return; }
+ if address % 0x2 != 0x0 { Error::BadAlignment(address, 0x2).trap(); return; }
+
+ if read_only!(address) || address_unused!(address) { return };
+
+ unsafe {
+ let pointer = (self.memory.as_mut_ptr() as *mut u8).add(address as usize) as *mut u16;
+ *pointer = value;
+ }
+ }
+
+ pub fn write_byte(&mut self, address: u32, value: u8) {
+ log_assignment!(format!("{address:#010X}"), format!("{value:#04X}"));
+
+ if address > MEMORY_LENGTH - 0x1 { Error::OutOfBounds(address).trap(); return; }
+
+ if read_only!(address) || address_unused!(address) { return };
+
+ let memory = self.memory.as_mut_ptr() as *mut u8;
+
+ match address {
+ // Extend to halfwords:
+ | 0x05000000..=0x050003FF
+ | 0x06000000..=0x06017FFF
+ | 0x07000000..=0x070003FF => {
+ // Align to halfwords.
+ let address = address & 0b11111111111111111111111111111110;
+
+ // Repeat value.
+ let value = value as u16 | (value as u16) << 0x8;
+
+ unsafe {
+ let pointer = memory.add(address as usize) as *mut u16;
+ *pointer = value;
+ }
+ },
+
+ // Bytes are allowed:
+ _ => unsafe {
+ let pointer = memory.add(address as usize);
+ *pointer = value;
+ },
+ };
+ }
+
+ #[inline(always)]
+ pub fn write_cpsr(&mut self, value: u32) {
+ log_assignment!("cpsr", format!("{value:#034b}"));
+
+ self.cpsr = value;
+ }
+
+ #[inline(always)]
+ pub fn write_spsr(&mut self, mode: CpuMode, value: u32) {
+ log_assignment!("spsr", format!("{value:#034b}"));
+
+ unsafe { *self.spsr.get_unchecked_mut(Self::spsr_index(mode)) = value };
+ }
+}