diff --git a/src/r6502/instructions.rs b/src/r6502/instructions.rs index a30e8d7..5a9d77e 100644 --- a/src/r6502/instructions.rs +++ b/src/r6502/instructions.rs @@ -361,22 +361,60 @@ impl Instructions pub fn STX(cpu: &mut R6502, bus: &mut dyn Bus) { - + bus.write(cpu.working_addr, cpu.x); } pub fn LDX(cpu: &mut R6502, bus: &mut dyn Bus) { + let data = cpu.working_data as u8; + cpu.x = data; + if cpu.x == 0 + { + cpu.set_flag(Flags::Z); + } + + if cpu.x & 0x80 != 0 + { + cpu.set_flag(Flags::N); + } } pub fn DEC(cpu: &mut R6502, bus: &mut dyn Bus) { + let dec_val = bus.read(cpu.working_addr) - 1; + bus.write(cpu.working_addr, dec_val); + + cpu.clear_flag(Flags::Z); + if dec_val == 0 + { + cpu.set_flag(Flags::Z); + } + + cpu.clear_flag(Flags::N); + if dec_val & 0x80 > 0 + { + cpu.set_flag(Flags::N); + } } pub fn INC(cpu: &mut R6502, bus: &mut dyn Bus) { + let dec_val = bus.read(cpu.working_addr) + 1; + bus.write(cpu.working_addr, dec_val); + + cpu.clear_flag(Flags::Z); + if dec_val == 0 + { + cpu.set_flag(Flags::Z); + } + cpu.clear_flag(Flags::N); + if dec_val & 0x80 > 0 + { + cpu.set_flag(Flags::N); + } } /////////////////////////////////////////////////////////// diff --git a/src/tests/instructions/mod.rs b/src/tests/instructions/mod.rs index b768f41..6132451 100644 --- a/src/tests/instructions/mod.rs +++ b/src/tests/instructions/mod.rs @@ -337,4 +337,124 @@ fn ROR() assert_eq!(0, cpu.check_flag(Flags::Z), "Zero flag should not be set"); assert_eq!(1, cpu.check_flag(Flags::C), "Carry flag should be set"); assert_eq!(1, cpu.check_flag(Flags::N), "Negative flag should be set"); +} + +#[test] +fn STX() +{ + let mut cpu = R6502::new(); + let mut bus = RAMBus::new(); + + // program address + let addr = 0x0020 as u16; + + // Set the program counter address + bus.write(0xFFFC, (addr & 0x00FF) as u8); // low byte + bus.write(0xFFFD, ((addr & 0xFF00) >> 8) as u8); // high byte + + + // STX $0A + bus.write(addr, 0x86); // STX - Zero Page mode + bus.write(addr + 1, 0x0A); // Argument + + // Restart cpu + cpu.reset(&mut bus); + + // manually setup the cpu registers + cpu.debug_set_reg(Registers::X, 0x0F); + + // Clock the cpu to run the program (Clock essentially runs one full instruction) + cpu.clock(&mut bus); + + // Is 0x0F at memory address 0x0A? + assert_eq!(0x0F, bus.read(0x0A)); + +} + +#[test] +fn LDX() +{ + let mut cpu = R6502::new(); + let mut bus = RAMBus::new(); + + // program address + let addr = 0x0020 as u16; + + // Set the program counter address + bus.write(0xFFFC, (addr & 0x00FF) as u8); // low byte + bus.write(0xFFFD, ((addr & 0xFF00) >> 8) as u8); // high byte + + // Program to load 0x08 into the accumulator + bus.write(addr, 0xA2); // LDX - Immediate mode + bus.write(addr + 1, 0x08); // Argument + + // Restart cpu + cpu.reset(&mut bus); + + // Clock the cpu to run the program (Clock essentially runs one full instruction) + cpu.clock(&mut bus); + + // Is 0x08 in the X register? + assert_eq!(0x08, cpu.debug_get_reg(Registers::X)); +} + +#[test] +fn DEC() +{ + let mut cpu = R6502::new(); + let mut bus = RAMBus::new(); + + // program address + let addr = 0x0020 as u16; + + // Set the program counter address + bus.write(0xFFFC, (addr & 0x00FF) as u8); // low byte + bus.write(0xFFFD, ((addr & 0xFF00) >> 8) as u8); // high byte + + // put value to decrement into memory + + bus.write(0x08, 0x10); + + // Program to load 0x08 into the accumulator + bus.write(addr, 0xC6); // DEC - Zero Page + bus.write(addr + 1, 0x08); // Argument + + // Restart cpu + cpu.reset(&mut bus); + + // Clock the cpu to run the program (Clock essentially runs one full instruction) + cpu.clock(&mut bus); + + // Is 0x0F in memory at 0x08? + assert_eq!(0x0F, bus.read(0x08)); +} + +#[test] +fn INC() +{ + let mut cpu = R6502::new(); + let mut bus = RAMBus::new(); + + // program address + let addr = 0x0020 as u16; + + // Set the program counter address + bus.write(0xFFFC, (addr & 0x00FF) as u8); // low byte + bus.write(0xFFFD, ((addr & 0xFF00) >> 8) as u8); // high byte + + // Put value to decrement into memory + bus.write(0x08, 0x10); + + // Program to load 0x08 into the accumulator + bus.write(addr, 0xE6); // INC - Zero Page + bus.write(addr + 1, 0x08); // Argument + + // Restart cpu + cpu.reset(&mut bus); + + // Clock the cpu to run the program (Clock essentially runs one full instruction) + cpu.clock(&mut bus); + + // Is 0x11 in memory at 0x08? + assert_eq!(0x11, bus.read(0x08)); } \ No newline at end of file diff --git a/todo/todo.todo b/todo/todo.todo index e4929c5..1820346 100644 --- a/todo/todo.todo +++ b/todo/todo.todo @@ -44,10 +44,10 @@ Instructions: ✔ 001 ROL @done(23-11-30 17:49) ✔ 010 LSR @done(23-11-30 18:12) ✔ 011 ROR @done(23-11-30 18:12) - ☐ 100 STX - ☐ 101 LDX - ☐ 110 DEC - ☐ 111 INC + ✔ 100 STX @done(24-01-08 13:46) + ✔ 101 LDX @done(24-01-08 14:21) + ✔ 110 DEC @done(24-01-08 15:59) + ✔ 111 INC @done(24-01-08 16:01) GROUP THREE: ☐ 001 BIT