1
0
mirror of https://github.com/mre/mos6502.git synced 2024-11-24 11:31:00 +00:00

less verbosity, more expliciteness

This commit is contained in:
Sam M W 2023-04-03 21:04:34 +01:00
parent 7222388c5e
commit d2043dc6fb

View File

@ -937,11 +937,12 @@ impl<M: Bus> core::fmt::Debug for CPU<M> {
mod tests { mod tests {
use super::*; use super::*;
use crate::memory::Memory as Ram;
use num::range_inclusive; use num::range_inclusive;
#[test] #[test]
fn dont_panic_for_overflow() { fn dont_panic_for_overflow() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.add_with_carry(-128); cpu.add_with_carry(-128);
assert_eq!(cpu.registers.accumulator, -128); assert_eq!(cpu.registers.accumulator, -128);
cpu.add_with_carry(-128); cpu.add_with_carry(-128);
@ -955,7 +956,7 @@ mod tests {
#[cfg_attr(feature = "decimal_mode", test)] #[cfg_attr(feature = "decimal_mode", test)]
fn decimal_add_test() { fn decimal_add_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.registers.status.or(Status::PS_DECIMAL_MODE); cpu.registers.status.or(Status::PS_DECIMAL_MODE);
cpu.add_with_carry(0x09); cpu.add_with_carry(0x09);
@ -982,7 +983,7 @@ mod tests {
#[cfg_attr(feature = "decimal_mode", test)] #[cfg_attr(feature = "decimal_mode", test)]
fn decimal_subtract_test() { fn decimal_subtract_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.registers cpu.registers
.status .status
.or(Status::PS_DECIMAL_MODE | Status::PS_CARRY); .or(Status::PS_DECIMAL_MODE | Status::PS_CARRY);
@ -1004,7 +1005,7 @@ mod tests {
#[test] #[test]
fn add_with_carry_test() { fn add_with_carry_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.add_with_carry(1); cpu.add_with_carry(1);
assert_eq!(cpu.registers.accumulator, 1); assert_eq!(cpu.registers.accumulator, 1);
@ -1027,7 +1028,7 @@ mod tests {
assert!(!cpu.registers.status.contains(Status::PS_NEGATIVE)); assert!(!cpu.registers.status.contains(Status::PS_NEGATIVE));
assert!(!cpu.registers.status.contains(Status::PS_OVERFLOW)); assert!(!cpu.registers.status.contains(Status::PS_OVERFLOW));
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.add_with_carry(127); cpu.add_with_carry(127);
assert_eq!(cpu.registers.accumulator, 127); assert_eq!(cpu.registers.accumulator, 127);
@ -1058,7 +1059,7 @@ mod tests {
assert!(cpu.registers.status.contains(Status::PS_NEGATIVE)); assert!(cpu.registers.status.contains(Status::PS_NEGATIVE));
assert!(!cpu.registers.status.contains(Status::PS_OVERFLOW)); assert!(!cpu.registers.status.contains(Status::PS_OVERFLOW));
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.add_with_carry(127); cpu.add_with_carry(127);
assert_eq!(cpu.registers.accumulator, 127); assert_eq!(cpu.registers.accumulator, 127);
@ -1074,7 +1075,7 @@ mod tests {
assert!(cpu.registers.status.contains(Status::PS_NEGATIVE)); assert!(cpu.registers.status.contains(Status::PS_NEGATIVE));
assert!(cpu.registers.status.contains(Status::PS_OVERFLOW)); assert!(cpu.registers.status.contains(Status::PS_OVERFLOW));
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.registers.status.or(Status::PS_CARRY); cpu.registers.status.or(Status::PS_CARRY);
cpu.add_with_carry(-1); cpu.add_with_carry(-1);
assert_eq!(cpu.registers.accumulator, 0); assert_eq!(cpu.registers.accumulator, 0);
@ -1083,7 +1084,7 @@ mod tests {
#[test] #[test]
fn and_test() { fn and_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.registers.accumulator = 0; cpu.registers.accumulator = 0;
cpu.and(-1); cpu.and(-1);
@ -1112,7 +1113,7 @@ mod tests {
#[test] #[test]
fn subtract_with_carry_test() { fn subtract_with_carry_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.execute_instruction((Instruction::SEC, OpInput::UseImplied)); cpu.execute_instruction((Instruction::SEC, OpInput::UseImplied));
cpu.registers.accumulator = 0; cpu.registers.accumulator = 0;
@ -1172,7 +1173,7 @@ mod tests {
#[test] #[test]
fn decrement_memory_test() { fn decrement_memory_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
let addr: u16 = 0xA1B2; let addr: u16 = 0xA1B2;
cpu.memory.set_byte(addr, 5); cpu.memory.set_byte(addr, 5);
@ -1209,7 +1210,7 @@ mod tests {
#[test] #[test]
fn decrement_x_test() { fn decrement_x_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.registers.index_x = 0x80; cpu.registers.index_x = 0x80;
cpu.execute_instruction((Instruction::DEX, OpInput::UseImplied)); cpu.execute_instruction((Instruction::DEX, OpInput::UseImplied));
assert_eq!(cpu.registers.index_x, 127); assert_eq!(cpu.registers.index_x, 127);
@ -1219,7 +1220,7 @@ mod tests {
#[test] #[test]
fn decrement_y_test() { fn decrement_y_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.registers.index_y = 0x80; cpu.registers.index_y = 0x80;
cpu.execute_instruction((Instruction::DEY, OpInput::UseImplied)); cpu.execute_instruction((Instruction::DEY, OpInput::UseImplied));
assert_eq!(cpu.registers.index_y, 127); assert_eq!(cpu.registers.index_y, 127);
@ -1231,7 +1232,7 @@ mod tests {
fn logical_shift_right_test() { fn logical_shift_right_test() {
// Testing UseImplied version (which targets the accumulator) only, for now // Testing UseImplied version (which targets the accumulator) only, for now
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.execute_instruction((Instruction::LDA, OpInput::UseImmediate(0))); cpu.execute_instruction((Instruction::LDA, OpInput::UseImmediate(0)));
cpu.execute_instruction((Instruction::LSR, OpInput::UseImplied)); cpu.execute_instruction((Instruction::LSR, OpInput::UseImplied));
assert_eq!(cpu.registers.accumulator, 0); assert_eq!(cpu.registers.accumulator, 0);
@ -1267,7 +1268,7 @@ mod tests {
#[test] #[test]
fn dec_x_test() { fn dec_x_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.execute_instruction((Instruction::DEX, OpInput::UseImplied)); cpu.execute_instruction((Instruction::DEX, OpInput::UseImplied));
assert_eq!(cpu.registers.index_x, 0xff); assert_eq!(cpu.registers.index_x, 0xff);
@ -1312,7 +1313,7 @@ mod tests {
#[test] #[test]
fn jump_test() { fn jump_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
let addr: u16 = 0xA1B1; let addr: u16 = 0xA1B1;
cpu.jump(addr); cpu.jump(addr);
@ -1321,7 +1322,7 @@ mod tests {
#[test] #[test]
fn branch_if_carry_clear_test() { fn branch_if_carry_clear_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.execute_instruction((Instruction::SEC, OpInput::UseImplied)); cpu.execute_instruction((Instruction::SEC, OpInput::UseImplied));
cpu.branch_if_carry_clear(0xABCD); cpu.branch_if_carry_clear(0xABCD);
@ -1334,7 +1335,7 @@ mod tests {
#[test] #[test]
fn branch_if_carry_set_test() { fn branch_if_carry_set_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.execute_instruction((Instruction::CLC, OpInput::UseImplied)); cpu.execute_instruction((Instruction::CLC, OpInput::UseImplied));
cpu.branch_if_carry_set(0xABCD); cpu.branch_if_carry_set(0xABCD);
@ -1347,7 +1348,7 @@ mod tests {
#[test] #[test]
fn branch_if_equal_test() { fn branch_if_equal_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.branch_if_equal(0xABCD); cpu.branch_if_equal(0xABCD);
assert_eq!(cpu.registers.program_counter, (0)); assert_eq!(cpu.registers.program_counter, (0));
@ -1360,7 +1361,7 @@ mod tests {
#[test] #[test]
fn branch_if_minus_test() { fn branch_if_minus_test() {
{ {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
let registers_before = cpu.registers; let registers_before = cpu.registers;
cpu.branch_if_minus(0xABCD); cpu.branch_if_minus(0xABCD);
@ -1369,7 +1370,7 @@ mod tests {
} }
{ {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.registers.status.or(Status::PS_NEGATIVE); cpu.registers.status.or(Status::PS_NEGATIVE);
let registers_before = cpu.registers; let registers_before = cpu.registers;
@ -1382,7 +1383,7 @@ mod tests {
#[test] #[test]
fn branch_if_positive_test() { fn branch_if_positive_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.registers.status.insert(Status::PS_NEGATIVE); cpu.registers.status.insert(Status::PS_NEGATIVE);
cpu.branch_if_positive(0xABCD); cpu.branch_if_positive(0xABCD);
@ -1395,7 +1396,7 @@ mod tests {
#[test] #[test]
fn branch_if_overflow_clear_test() { fn branch_if_overflow_clear_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.registers.status.insert(Status::PS_OVERFLOW); cpu.registers.status.insert(Status::PS_OVERFLOW);
cpu.branch_if_overflow_clear(0xABCD); cpu.branch_if_overflow_clear(0xABCD);
@ -1408,7 +1409,7 @@ mod tests {
#[test] #[test]
fn branch_across_end_of_address_space() { fn branch_across_end_of_address_space() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.registers.program_counter = 0xffff; cpu.registers.program_counter = 0xffff;
cpu.registers.status.insert(Status::PS_OVERFLOW); cpu.registers.status.insert(Status::PS_OVERFLOW);
@ -1418,7 +1419,7 @@ mod tests {
#[test] #[test]
fn branch_if_overflow_set_test() { fn branch_if_overflow_set_test() {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.branch_if_overflow_set(0xABCD); cpu.branch_if_overflow_set(0xABCD);
assert_eq!(cpu.registers.program_counter, (0)); assert_eq!(cpu.registers.program_counter, (0));
@ -1431,9 +1432,9 @@ mod tests {
#[cfg(test)] #[cfg(test)]
fn compare_test_helper<F>(compare: &mut F, load_instruction: Instruction) fn compare_test_helper<F>(compare: &mut F, load_instruction: Instruction)
where where
F: FnMut(&mut CPU<crate::memory::Memory>, u8), F: FnMut(&mut CPU<Ram>, u8),
{ {
let mut cpu = CPU::new(crate::memory::Memory::new()); let mut cpu = CPU::new(Ram::new());
cpu.execute_instruction((load_instruction, OpInput::UseImmediate(127))); cpu.execute_instruction((load_instruction, OpInput::UseImmediate(127)));
@ -1481,7 +1482,7 @@ mod tests {
#[test] #[test]
fn compare_with_a_register_test() { fn compare_with_a_register_test() {
compare_test_helper( compare_test_helper(
&mut |cpu: &mut CPU<crate::memory::Memory>, val: u8| { &mut |cpu: &mut CPU<Ram>, val: u8| {
cpu.compare_with_a_register(val); cpu.compare_with_a_register(val);
}, },
Instruction::LDA, Instruction::LDA,
@ -1491,7 +1492,7 @@ mod tests {
#[test] #[test]
fn compare_with_x_register_test() { fn compare_with_x_register_test() {
compare_test_helper( compare_test_helper(
&mut |cpu: &mut CPU<crate::memory::Memory>, val: u8| { &mut |cpu: &mut CPU<Ram>, val: u8| {
cpu.compare_with_x_register(val); cpu.compare_with_x_register(val);
}, },
Instruction::LDX, Instruction::LDX,
@ -1501,7 +1502,7 @@ mod tests {
#[test] #[test]
fn compare_with_y_register_test() { fn compare_with_y_register_test() {
compare_test_helper( compare_test_helper(
&mut |cpu: &mut CPU<crate::memory::Memory>, val: u8| { &mut |cpu: &mut CPU<Ram>, val: u8| {
cpu.compare_with_y_register(val); cpu.compare_with_y_register(val);
}, },
Instruction::LDY, Instruction::LDY,
@ -1510,8 +1511,7 @@ mod tests {
#[test] #[test]
fn exclusive_or_test() { fn exclusive_or_test() {
use crate::memory::Memory; let mut cpu = CPU::new(Ram::new());
let mut cpu = CPU::new(Memory::new());
for a_before in range_inclusive(0u8, 255u8) { for a_before in range_inclusive(0u8, 255u8) {
for val in range_inclusive(0u8, 255u8) { for val in range_inclusive(0u8, 255u8) {
@ -1539,8 +1539,7 @@ mod tests {
#[test] #[test]
fn inclusive_or_test() { fn inclusive_or_test() {
use crate::memory::Memory; let mut cpu = CPU::new(Ram::new());
let mut cpu = CPU::new(Memory::new());
for a_before in range_inclusive(0u8, 255u8) { for a_before in range_inclusive(0u8, 255u8) {
for val in range_inclusive(0u8, 255u8) { for val in range_inclusive(0u8, 255u8) {
@ -1568,8 +1567,7 @@ mod tests {
#[test] #[test]
fn stack_underflow() { fn stack_underflow() {
use crate::memory::Memory; let mut cpu = CPU::new(Ram::new());
let mut cpu = CPU::new(Memory::new());
let _val: u8 = cpu.pull_from_stack(); let _val: u8 = cpu.pull_from_stack();
} }
} }