diff --git a/README.md b/README.md index fc3570b..1e55e56 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Source: [Wikipedia](https://en.wikipedia.org/wiki/MOS_Technology_6502) ```rust use mos6502::memory::Bus; use mos6502::memory::Memory; +use mos6502::instruction::Nmos6502; use mos6502::cpu; fn main() { @@ -52,7 +53,7 @@ fn main() { 0x4c, 0x10, 0x00, // Jump to .algo ]; - let mut cpu = cpu::CPU::new(Memory::new()); + let mut cpu = cpu::CPU::new(Memory::new(), Nmos6502); cpu.memory.set_bytes(0x00, &zero_page_data); cpu.memory.set_bytes(0x10, &program); @@ -86,6 +87,7 @@ This will create a binary file `euclid.bin` that you can load into the emulator: ```rust use mos6502::memory::Bus; use mos6502::memory::Memory; +use mos6502::instruction::Nmos6502; use mos6502::cpu; use std::fs::read; @@ -103,7 +105,7 @@ fn main() { } }; - let mut cpu = cpu::CPU::new(Memory::new()); + let mut cpu = cpu::CPU::new(Memory::new(), Nmos6502); cpu.memory.set_bytes(0x00, &zero_page_data); cpu.memory.set_bytes(0x10, &program); diff --git a/examples/euclid.rs b/examples/euclid.rs index 5636deb..8955389 100644 --- a/examples/euclid.rs +++ b/examples/euclid.rs @@ -1,4 +1,5 @@ use mos6502::cpu; +use mos6502::instruction::Nmos6502; use mos6502::memory::Bus; use mos6502::memory::Memory; use std::fs::read; @@ -22,7 +23,7 @@ fn main() { } }; - let mut cpu = cpu::CPU::new(Memory::new()); + let mut cpu = cpu::CPU::new(Memory::new(), Nmos6502); cpu.memory.set_bytes(0x00, &zero_page_data); cpu.memory.set_bytes(0x10, &program); diff --git a/examples/euclid_bytes.rs b/examples/euclid_bytes.rs index d261fa0..d2718a3 100644 --- a/examples/euclid_bytes.rs +++ b/examples/euclid_bytes.rs @@ -1,6 +1,7 @@ extern crate mos6502; use mos6502::cpu; +use mos6502::instruction::Nmos6502; use mos6502::memory::Bus; use mos6502::memory::Memory; @@ -35,7 +36,7 @@ fn main() { 0x4c, 0x10, 0x00, // Jump to .algo ]; - let mut cpu = cpu::CPU::new(Memory::new()); + let mut cpu = cpu::CPU::new(Memory::new(), Nmos6502); 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 2d0a788..1572003 100644 --- a/examples/mos6502.rs +++ b/examples/mos6502.rs @@ -29,12 +29,13 @@ extern crate mos6502; #[cfg(not(test))] use mos6502::cpu; +use mos6502::instruction::Nmos6502; use mos6502::memory::Bus; use mos6502::memory::Memory; #[cfg(not(test))] fn main() { - let mut cpu = cpu::CPU::new(Memory::new()); + let mut cpu = cpu::CPU::new(Memory::new(), Nmos6502); // "Load" a program diff --git a/src/cpu.rs b/src/cpu.rs index 5141ba5..0379f86 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -998,12 +998,13 @@ impl core::fmt::Debug for CPU { mod tests { use super::*; + use crate::instruction::Nmos6502; use crate::memory::Memory as Ram; use num::range_inclusive; #[test] fn dont_panic_for_overflow() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.add_with_carry(0x80); assert_eq!(cpu.registers.accumulator, 0x80); cpu.add_with_carry(0x80); @@ -1017,7 +1018,7 @@ mod tests { #[cfg_attr(feature = "decimal_mode", test)] fn decimal_add_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.registers.status.or(Status::PS_DECIMAL_MODE); cpu.add_with_carry(0x09); @@ -1044,7 +1045,7 @@ mod tests { #[cfg_attr(feature = "decimal_mode", test)] fn decimal_subtract_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.registers .status .or(Status::PS_DECIMAL_MODE | Status::PS_CARRY); @@ -1066,7 +1067,7 @@ mod tests { #[test] fn add_with_carry_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.add_with_carry(1); assert_eq!(cpu.registers.accumulator, 1); @@ -1089,7 +1090,7 @@ mod tests { assert!(!cpu.registers.status.contains(Status::PS_NEGATIVE)); assert!(!cpu.registers.status.contains(Status::PS_OVERFLOW)); - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); assert_eq!(cpu.registers.accumulator, 0); cpu.add_with_carry(127); @@ -1121,7 +1122,7 @@ mod tests { assert!(cpu.registers.status.contains(Status::PS_NEGATIVE)); assert!(!cpu.registers.status.contains(Status::PS_OVERFLOW)); - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.add_with_carry(127); assert_eq!(cpu.registers.accumulator, 127); @@ -1137,7 +1138,7 @@ mod tests { assert!(cpu.registers.status.contains(Status::PS_NEGATIVE)); assert!(cpu.registers.status.contains(Status::PS_OVERFLOW)); - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.registers.status.or(Status::PS_CARRY); cpu.add_with_carry(0xff); assert_eq!(cpu.registers.accumulator, 0); @@ -1146,7 +1147,7 @@ mod tests { #[test] fn solid65_adc_immediate() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); // Adding $FF plus carry should be the same as adding $00 and no carry, so these three // instructions should leave the carry flags unaffected, i.e. set. @@ -1160,7 +1161,7 @@ mod tests { #[test] fn and_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.registers.accumulator = 0; cpu.and(0xff); @@ -1189,7 +1190,7 @@ mod tests { #[test] fn subtract_with_carry_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.execute_instruction((Instruction::SEC, OpInput::UseImplied)); cpu.registers.accumulator = 0; @@ -1249,7 +1250,7 @@ mod tests { #[test] fn decrement_memory_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); let addr: u16 = 0xA1B2; cpu.memory.set_byte(addr, 5); @@ -1286,7 +1287,7 @@ mod tests { #[test] fn decrement_x_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.registers.index_x = 0x80; cpu.execute_instruction((Instruction::DEX, OpInput::UseImplied)); assert_eq!(cpu.registers.index_x, 127); @@ -1296,7 +1297,7 @@ mod tests { #[test] fn decrement_y_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.registers.index_y = 0x80; cpu.execute_instruction((Instruction::DEY, OpInput::UseImplied)); assert_eq!(cpu.registers.index_y, 127); @@ -1308,7 +1309,7 @@ mod tests { fn logical_shift_right_test() { // Testing UseImplied version (which targets the accumulator) only, for now - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.execute_instruction((Instruction::LDA, OpInput::UseImmediate(0))); cpu.execute_instruction((Instruction::LSR, OpInput::UseImplied)); assert_eq!(cpu.registers.accumulator, 0); @@ -1344,7 +1345,7 @@ mod tests { #[test] fn dec_x_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.execute_instruction((Instruction::DEX, OpInput::UseImplied)); assert_eq!(cpu.registers.index_x, 0xff); @@ -1389,7 +1390,7 @@ mod tests { #[test] fn jump_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); let addr: u16 = 0xA1B1; cpu.jump(addr); @@ -1398,7 +1399,7 @@ mod tests { #[test] fn branch_if_carry_clear_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.execute_instruction((Instruction::SEC, OpInput::UseImplied)); cpu.branch_if_carry_clear(0xABCD); @@ -1411,7 +1412,7 @@ mod tests { #[test] fn branch_if_carry_set_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.execute_instruction((Instruction::CLC, OpInput::UseImplied)); cpu.branch_if_carry_set(0xABCD); @@ -1424,7 +1425,7 @@ mod tests { #[test] fn branch_if_equal_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.branch_if_equal(0xABCD); assert_eq!(cpu.registers.program_counter, (0)); @@ -1437,7 +1438,7 @@ mod tests { #[test] fn branch_if_minus_test() { { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); let registers_before = cpu.registers; cpu.branch_if_minus(0xABCD); @@ -1446,7 +1447,7 @@ mod tests { } { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.registers.status.or(Status::PS_NEGATIVE); let registers_before = cpu.registers; @@ -1459,7 +1460,7 @@ mod tests { #[test] fn branch_if_positive_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.registers.status.insert(Status::PS_NEGATIVE); cpu.branch_if_positive(0xABCD); @@ -1472,7 +1473,7 @@ mod tests { #[test] fn branch_if_overflow_clear_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.registers.status.insert(Status::PS_OVERFLOW); cpu.branch_if_overflow_clear(0xABCD); @@ -1485,7 +1486,7 @@ mod tests { #[test] fn branch_across_end_of_address_space() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.registers.program_counter = 0xffff; cpu.registers.status.insert(Status::PS_OVERFLOW); @@ -1495,7 +1496,7 @@ mod tests { #[test] fn branch_if_overflow_set_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.branch_if_overflow_set(0xABCD); assert_eq!(cpu.registers.program_counter, (0)); @@ -1508,9 +1509,9 @@ mod tests { #[cfg(test)] 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(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); cpu.execute_instruction((load_instruction, OpInput::UseImmediate(127))); @@ -1558,7 +1559,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, @@ -1568,7 +1569,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, @@ -1578,7 +1579,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, @@ -1587,7 +1588,7 @@ mod tests { #[test] fn exclusive_or_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); for a_before in range_inclusive(0u8, 255u8) { for val in range_inclusive(0u8, 255u8) { @@ -1615,7 +1616,7 @@ mod tests { #[test] fn inclusive_or_test() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); for a_before in range_inclusive(0u8, 255u8) { for val in range_inclusive(0u8, 255u8) { @@ -1643,7 +1644,7 @@ mod tests { #[test] fn stack_underflow() { - let mut cpu = CPU::new(Ram::new()); + let mut cpu = CPU::new(Ram::new(), Nmos6502); let _val: u8 = cpu.pull_from_stack(); } }