From 0708a1084c12078ee16529e5458b672b2e73b5f1 Mon Sep 17 00:00:00 2001 From: Sam M W Date: Mon, 3 Apr 2023 07:31:49 +0100 Subject: [PATCH] fix the tests back up --- README.md | 3 +- examples/euclidean_algo.rs | 3 +- examples/mos6502.rs | 3 +- src/cpu.rs | 69 ++++++++++++++++++++------------------ 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index bcd6c4d..057ea7a 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Source: [Wikipedia](https://en.wikipedia.org/wiki/MOS_Technology_6502) ```rust use mos6502::memory::Bus; +use mos6502::memory::Memory; use mos6502::cpu; fn main() { @@ -50,7 +51,7 @@ fn main() { 0x4c, 0x10, 0x00, // Jump to .algo ]; - let mut cpu = cpu::CPU::new(); + let mut cpu = cpu::CPU::new(Memory::new()); cpu.memory.set_bytes(0x00, &zero_page_data); cpu.memory.set_bytes(0x10, &program); diff --git a/examples/euclidean_algo.rs b/examples/euclidean_algo.rs index 446f5d2..04fc087 100644 --- a/examples/euclidean_algo.rs +++ b/examples/euclidean_algo.rs @@ -2,6 +2,7 @@ extern crate mos6502; use mos6502::cpu; use mos6502::memory::Bus; +use mos6502::memory::Memory; fn main() { println!("Enter two numbers (< 128) to know their GCD:"); @@ -34,7 +35,7 @@ fn main() { 0x4c, 0x10, 0x00, // Jump to .algo ]; - let mut cpu = cpu::CPU::new(); + let mut cpu = cpu::CPU::new(Memory::new()); cpu.memory.set_bytes(0x00, &zero_page_data); cpu.memory.set_bytes(0x10, &program); diff --git a/examples/mos6502.rs b/examples/mos6502.rs index 26fa717..2d0a788 100644 --- a/examples/mos6502.rs +++ b/examples/mos6502.rs @@ -30,10 +30,11 @@ extern crate mos6502; #[cfg(not(test))] use mos6502::cpu; use mos6502::memory::Bus; +use mos6502::memory::Memory; #[cfg(not(test))] fn main() { - let mut cpu = cpu::CPU::new(); + let mut cpu = cpu::CPU::new(Memory::new()); // "Load" a program diff --git a/src/cpu.rs b/src/cpu.rs index 81d017f..371e3ff 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -949,7 +949,7 @@ mod tests { #[test] fn dont_panic_for_overflow() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.add_with_carry(-128); assert_eq!(cpu.registers.accumulator, -128); cpu.add_with_carry(-128); @@ -963,7 +963,7 @@ mod tests { #[cfg_attr(feature = "decimal_mode", test)] fn decimal_add_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.registers.status.or(Status::PS_DECIMAL_MODE); cpu.add_with_carry(0x09); @@ -990,7 +990,7 @@ mod tests { #[cfg_attr(feature = "decimal_mode", test)] fn decimal_subtract_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.registers .status .or(Status::PS_DECIMAL_MODE | Status::PS_CARRY); @@ -1012,7 +1012,7 @@ mod tests { #[test] fn add_with_carry_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.add_with_carry(1); assert_eq!(cpu.registers.accumulator, 1); @@ -1035,7 +1035,7 @@ mod tests { assert!(!cpu.registers.status.contains(Status::PS_NEGATIVE)); assert!(!cpu.registers.status.contains(Status::PS_OVERFLOW)); - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.add_with_carry(127); assert_eq!(cpu.registers.accumulator, 127); @@ -1066,7 +1066,7 @@ mod tests { assert!(cpu.registers.status.contains(Status::PS_NEGATIVE)); assert!(!cpu.registers.status.contains(Status::PS_OVERFLOW)); - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.add_with_carry(127); assert_eq!(cpu.registers.accumulator, 127); @@ -1082,7 +1082,7 @@ mod tests { assert!(cpu.registers.status.contains(Status::PS_NEGATIVE)); assert!(cpu.registers.status.contains(Status::PS_OVERFLOW)); - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.registers.status.or(Status::PS_CARRY); cpu.add_with_carry(-1); assert_eq!(cpu.registers.accumulator, 0); @@ -1091,7 +1091,7 @@ mod tests { #[test] fn and_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.registers.accumulator = 0; cpu.and(-1); @@ -1120,7 +1120,7 @@ mod tests { #[test] fn subtract_with_carry_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.execute_instruction((Instruction::SEC, OpInput::UseImplied)); cpu.registers.accumulator = 0; @@ -1180,7 +1180,7 @@ mod tests { #[test] fn decrement_memory_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); let addr: u16 = 0xA1B2; cpu.memory.set_byte(addr, 5); @@ -1217,7 +1217,7 @@ mod tests { #[test] fn decrement_x_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.registers.index_x = 0x80; cpu.execute_instruction((Instruction::DEX, OpInput::UseImplied)); assert_eq!(cpu.registers.index_x, 127); @@ -1227,7 +1227,7 @@ mod tests { #[test] fn decrement_y_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.registers.index_y = 0x80; cpu.execute_instruction((Instruction::DEY, OpInput::UseImplied)); assert_eq!(cpu.registers.index_y, 127); @@ -1239,7 +1239,7 @@ mod tests { fn logical_shift_right_test() { // Testing UseImplied version (which targets the accumulator) only, for now - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.execute_instruction((Instruction::LDA, OpInput::UseImmediate(0))); cpu.execute_instruction((Instruction::LSR, OpInput::UseImplied)); assert_eq!(cpu.registers.accumulator, 0); @@ -1275,7 +1275,7 @@ mod tests { #[test] fn dec_x_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.execute_instruction((Instruction::DEX, OpInput::UseImplied)); assert_eq!(cpu.registers.index_x, 0xff); @@ -1320,7 +1320,7 @@ mod tests { #[test] fn jump_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); let addr: u16 = 0xA1B1; cpu.jump(addr); @@ -1329,7 +1329,7 @@ mod tests { #[test] fn branch_if_carry_clear_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.execute_instruction((Instruction::SEC, OpInput::UseImplied)); cpu.branch_if_carry_clear(0xABCD); @@ -1342,7 +1342,7 @@ mod tests { #[test] fn branch_if_carry_set_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.execute_instruction((Instruction::CLC, OpInput::UseImplied)); cpu.branch_if_carry_set(0xABCD); @@ -1355,7 +1355,7 @@ mod tests { #[test] fn branch_if_equal_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.branch_if_equal(0xABCD); assert_eq!(cpu.registers.program_counter, (0)); @@ -1368,7 +1368,7 @@ mod tests { #[test] fn branch_if_minus_test() { { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); let registers_before = cpu.registers; cpu.branch_if_minus(0xABCD); @@ -1377,7 +1377,7 @@ mod tests { } { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.registers.status.or(Status::PS_NEGATIVE); let registers_before = cpu.registers; @@ -1390,7 +1390,7 @@ mod tests { #[test] fn branch_if_positive_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.registers.status.insert(Status::PS_NEGATIVE); cpu.branch_if_positive(0xABCD); @@ -1403,7 +1403,7 @@ mod tests { #[test] fn branch_if_overflow_clear_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.registers.status.insert(Status::PS_OVERFLOW); cpu.branch_if_overflow_clear(0xABCD); @@ -1416,7 +1416,7 @@ mod tests { #[test] fn branch_across_end_of_address_space() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.registers.program_counter = 0xffff; cpu.registers.status.insert(Status::PS_OVERFLOW); @@ -1426,7 +1426,7 @@ mod tests { #[test] fn branch_if_overflow_set_test() { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.branch_if_overflow_set(0xABCD); assert_eq!(cpu.registers.program_counter, (0)); @@ -1437,11 +1437,11 @@ mod tests { } #[cfg(test)] - fn compare_test_helper(compare: &mut F, load_instruction: Instruction) + fn compare_test_helper(compare: &mut F, load_instruction: Instruction) where - F: FnMut(&mut CPU, u8), + F: FnMut(&mut CPU, u8), { - let mut cpu = CPU::new(); + let mut cpu = CPU::new(crate::memory::Memory::new()); cpu.execute_instruction((load_instruction, OpInput::UseImmediate(127))); @@ -1489,7 +1489,7 @@ mod tests { #[test] fn compare_with_a_register_test() { compare_test_helper( - &mut |cpu: &mut CPU, val: u8| { + &mut |cpu: &mut CPU, val: u8| { cpu.compare_with_a_register(val); }, Instruction::LDA, @@ -1499,7 +1499,7 @@ mod tests { #[test] fn compare_with_x_register_test() { compare_test_helper( - &mut |cpu: &mut CPU, val: u8| { + &mut |cpu: &mut CPU, val: u8| { cpu.compare_with_x_register(val); }, Instruction::LDX, @@ -1509,7 +1509,7 @@ mod tests { #[test] fn compare_with_y_register_test() { compare_test_helper( - &mut |cpu: &mut CPU, val: u8| { + &mut |cpu: &mut CPU, val: u8| { cpu.compare_with_y_register(val); }, Instruction::LDY, @@ -1518,7 +1518,8 @@ mod tests { #[test] fn exclusive_or_test() { - let mut cpu = CPU::new(); + use crate::memory::Memory; + let mut cpu = CPU::new(Memory::new()); for a_before in range_inclusive(0u8, 255u8) { for val in range_inclusive(0u8, 255u8) { @@ -1546,7 +1547,8 @@ mod tests { #[test] fn inclusive_or_test() { - let mut cpu = CPU::new(); + use crate::memory::Memory; + let mut cpu = CPU::new(Memory::new()); for a_before in range_inclusive(0u8, 255u8) { for val in range_inclusive(0u8, 255u8) { @@ -1574,7 +1576,8 @@ mod tests { #[test] fn stack_underflow() { - let mut cpu = CPU::new(); + use crate::memory::Memory; + let mut cpu = CPU::new(Memory::new()); let _val: u8 = cpu.pull_from_stack(); } }