From 3de8f9158d89f874547cca019fba8959309d4761 Mon Sep 17 00:00:00 2001 From: Sam M W Date: Tue, 18 Oct 2022 09:59:52 +0100 Subject: [PATCH] move tests and README away from Address --- README.md | 6 ++-- examples/euclidean_algo.rs | 8 ++--- examples/mos6502.rs | 11 +++---- src/cpu.rs | 64 +++++++++++++++++++------------------- src/instruction.rs | 2 -- src/memory.rs | 12 +++---- 6 files changed, 47 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 9272259..86ebadc 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,9 @@ fn main() { let mut cpu = cpu::CPU::new(); - cpu.memory.set_bytes(Address(0x00), &zero_page_data); - cpu.memory.set_bytes(Address(0x10), &program); - cpu.registers.program_counter = Address(0x10); + cpu.memory.set_bytes(0x00, &zero_page_data); + cpu.memory.set_bytes(0x10, &program); + cpu.registers.program_counter = 0x10; cpu.run(); diff --git a/examples/euclidean_algo.rs b/examples/euclidean_algo.rs index cf22eef..0177362 100644 --- a/examples/euclidean_algo.rs +++ b/examples/euclidean_algo.rs @@ -1,5 +1,5 @@ extern crate mos6502; -use mos6502::address::Address; + use mos6502::cpu; fn main() { @@ -35,9 +35,9 @@ fn main() { let mut cpu = cpu::CPU::new(); - cpu.memory.set_bytes(Address(0x00), &zero_page_data); - cpu.memory.set_bytes(Address(0x10), &program); - cpu.registers.program_counter = Address(0x10); + cpu.memory.set_bytes(0x00, &zero_page_data); + cpu.memory.set_bytes(0x10, &program); + cpu.registers.program_counter = 0x10; cpu.run(); diff --git a/examples/mos6502.rs b/examples/mos6502.rs index 04c163b..c71080c 100644 --- a/examples/mos6502.rs +++ b/examples/mos6502.rs @@ -30,9 +30,6 @@ extern crate mos6502; #[cfg(not(test))] use mos6502::cpu; -#[cfg(not(test))] -use mos6502::address::Address; - #[cfg(not(test))] fn main() { let mut cpu = cpu::CPU::new(); @@ -94,11 +91,11 @@ fn main() { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, // ADC IndirectIndexedY target ]; - cpu.memory.set_bytes(Address(0x0000), &zero_page_data); - cpu.memory.set_bytes(Address(0x4000), &program); - cpu.memory.set_bytes(Address(0x8000), &data); + cpu.memory.set_bytes(0x0000, &zero_page_data); + cpu.memory.set_bytes(0x4000, &program); + cpu.memory.set_bytes(0x8000, &data); - cpu.registers.program_counter = Address(0x4000); + cpu.registers.program_counter = 0x4000; cpu.run(); diff --git a/src/cpu.rs b/src/cpu.rs index 08fa4ee..a2b73b1 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -25,7 +25,6 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. - use crate::instruction::{self, DecodedInstr, Instruction, OpInput}; use crate::memory::Memory; use crate::registers::{Registers, StackPointer, Status, StatusArgs}; @@ -68,7 +67,8 @@ impl CPU { let am_out = am.process(self, slice); // Increment program counter - self.registers.program_counter = self.registers.program_counter.wrapping_add(num_bytes); + self.registers.program_counter = + self.registers.program_counter.wrapping_add(num_bytes); Some((instr, am_out)) } @@ -1022,7 +1022,7 @@ mod tests { #[test] fn decrement_memory_test() { let mut cpu = CPU::new(); - let addr = Address(0xA1B2); + let addr: u16 = 0xA1B2; cpu.memory.set_byte(addr, 5); @@ -1135,7 +1135,7 @@ mod tests { #[test] fn jump_test() { let mut cpu = CPU::new(); - let addr = Address(0xA1B1); + let addr: u16 = 0xA1B1; cpu.jump(addr); assert_eq!(cpu.registers.program_counter, addr); @@ -1146,12 +1146,12 @@ mod tests { let mut cpu = CPU::new(); cpu.execute_instruction((Instruction::SEC, OpInput::UseImplied)); - cpu.branch_if_carry_clear(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0)); + cpu.branch_if_carry_clear(0xABCD); + assert_eq!(cpu.registers.program_counter, (0)); cpu.execute_instruction((Instruction::CLC, OpInput::UseImplied)); - cpu.branch_if_carry_clear(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0xABCD)); + cpu.branch_if_carry_clear(0xABCD); + assert_eq!(cpu.registers.program_counter, (0xABCD)); } #[test] @@ -1159,24 +1159,24 @@ mod tests { let mut cpu = CPU::new(); cpu.execute_instruction((Instruction::CLC, OpInput::UseImplied)); - cpu.branch_if_carry_set(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0)); + cpu.branch_if_carry_set(0xABCD); + assert_eq!(cpu.registers.program_counter, (0)); cpu.execute_instruction((Instruction::SEC, OpInput::UseImplied)); - cpu.branch_if_carry_set(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0xABCD)); + cpu.branch_if_carry_set(0xABCD); + assert_eq!(cpu.registers.program_counter, (0xABCD)); } #[test] fn branch_if_equal_test() { let mut cpu = CPU::new(); - cpu.branch_if_equal(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0)); + cpu.branch_if_equal(0xABCD); + assert_eq!(cpu.registers.program_counter, (0)); cpu.registers.status.or(Status::PS_ZERO); - cpu.branch_if_equal(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0xABCD)); + cpu.branch_if_equal(0xABCD); + assert_eq!(cpu.registers.program_counter, (0xABCD)); } #[test] @@ -1185,9 +1185,9 @@ mod tests { let mut cpu = CPU::new(); let registers_before = cpu.registers; - cpu.branch_if_minus(Address(0xABCD)); + cpu.branch_if_minus(0xABCD); assert_eq!(cpu.registers, registers_before); - assert_eq!(cpu.registers.program_counter, Address(0)); + assert_eq!(cpu.registers.program_counter, (0)); } { @@ -1196,9 +1196,9 @@ mod tests { cpu.registers.status.or(Status::PS_NEGATIVE); let registers_before = cpu.registers; - cpu.branch_if_minus(Address(0xABCD)); + cpu.branch_if_minus(0xABCD); assert_eq!(cpu.registers.status, registers_before.status); - assert_eq!(cpu.registers.program_counter, Address(0xABCD)); + assert_eq!(cpu.registers.program_counter, (0xABCD)); } } @@ -1207,12 +1207,12 @@ mod tests { let mut cpu = CPU::new(); cpu.registers.status.insert(Status::PS_NEGATIVE); - cpu.branch_if_positive(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0)); + cpu.branch_if_positive(0xABCD); + assert_eq!(cpu.registers.program_counter, (0)); cpu.registers.status.remove(Status::PS_NEGATIVE); - cpu.branch_if_positive(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0xABCD)); + cpu.branch_if_positive(0xABCD); + assert_eq!(cpu.registers.program_counter, (0xABCD)); } #[test] @@ -1220,24 +1220,24 @@ mod tests { let mut cpu = CPU::new(); cpu.registers.status.insert(Status::PS_OVERFLOW); - cpu.branch_if_overflow_clear(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0)); + cpu.branch_if_overflow_clear(0xABCD); + assert_eq!(cpu.registers.program_counter, (0)); cpu.registers.status.remove(Status::PS_OVERFLOW); - cpu.branch_if_overflow_clear(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0xABCD)); + cpu.branch_if_overflow_clear(0xABCD); + assert_eq!(cpu.registers.program_counter, (0xABCD)); } #[test] fn branch_if_overflow_set_test() { let mut cpu = CPU::new(); - cpu.branch_if_overflow_set(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0)); + cpu.branch_if_overflow_set(0xABCD); + assert_eq!(cpu.registers.program_counter, (0)); cpu.registers.status.insert(Status::PS_OVERFLOW); - cpu.branch_if_overflow_set(Address(0xABCD)); - assert_eq!(cpu.registers.program_counter, Address(0xABCD)); + cpu.branch_if_overflow_set(0xABCD); + assert_eq!(cpu.registers.program_counter, (0xABCD)); } #[cfg(test)] diff --git a/src/instruction.rs b/src/instruction.rs index d9e4ad7..042c50e 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -25,8 +25,6 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. - - use crate::cpu::CPU; // Abbreviations diff --git a/src/memory.rs b/src/memory.rs index e491243..b176e6a 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -25,8 +25,7 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -use crate::address::{Address}; - +use crate::address::Address; // JAM: We can probably come up with a better way to represent address ranges. // Address range type? @@ -112,17 +111,14 @@ mod tests { #[test] fn test_memory_set_bytes() { let mut memory = Memory::new(); - memory.set_bytes(Address(0x0100), &[1, 2, 3, 4, 5]); - assert_eq!( - memory.get_slice(Address(0x00FF), AddressDiff(7)), - &[0, 1, 2, 3, 4, 5, 0] - ); + memory.set_bytes(0x0100, &[1, 2, 3, 4, 5]); + assert_eq!(memory.get_slice(0x00FF, 7), &[0, 1, 2, 3, 4, 5, 0]); } #[test] #[should_panic] fn test_memory_overflow_panic() { let mut memory = Memory::new(); - memory.set_bytes(Address(0xFFFE), &[1, 2, 3]); + memory.set_bytes(0xFFFE, &[1, 2, 3]); } }