From 5dc185866ec06cf1249aedef4c24b6a94e465d30 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Sun, 7 Oct 2018 09:14:30 +0100 Subject: [PATCH] Add a test for ADCA immediate. Half carry and overflow flags incorrect! Signed-off-by: Adrian Conlon --- MC6809/inc/mc6809.h | 10 ++++++++-- MC6809/unittest/mc6809_tests.cpp | 26 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/MC6809/inc/mc6809.h b/MC6809/inc/mc6809.h index bd722e8..41de2a4 100644 --- a/MC6809/inc/mc6809.h +++ b/MC6809/inc/mc6809.h @@ -206,21 +206,27 @@ namespace EightBit { void adjustBorrow(register16_t datum) { adjustBorrow(datum.word); } void adjustOverflow(uint8_t before, uint8_t data, uint8_t after) { - setFlag(CC(), VF, (before ^ data) & (before ^ after) & Bit7); + setFlag(CC(), VF, !!((before ^ data ^ after ^ (after >> 1)) & Bit7)); } void adjustOverflow(uint16_t before, uint16_t data, uint16_t after) { - setFlag(CC(), VF, (before ^ data) & (before ^ after) & Bit15); + setFlag(CC(), VF, !!((before ^ data ^ after ^ (after >> 1)) & Bit15)); } + void adjustOverflow(register16_t before, register16_t data, register16_t after) { adjustOverflow(before.word, data.word, after.word); } + void adjustHalfCarry(uint8_t before, uint8_t data, uint8_t after) { + setFlag(CC(), HF, !!((before ^ data ^ after) & Bit4)); + } + void adjustAddition(uint8_t before, uint8_t data, register16_t after) { const auto result = after.low; adjustNZ(result); adjustCarry(after); adjustOverflow(before, data, result); + adjustHalfCarry(before, data, result); } void adjustAddition(uint16_t before, uint16_t data, uint32_t after) { diff --git a/MC6809/unittest/mc6809_tests.cpp b/MC6809/unittest/mc6809_tests.cpp index 10a23f5..6d2a16d 100644 --- a/MC6809/unittest/mc6809_tests.cpp +++ b/MC6809/unittest/mc6809_tests.cpp @@ -4,7 +4,7 @@ // Using examples from 6809 Assembly Language Programming, by Lance A. Leventhal // Just test the basics... -TEST_CASE("Add Accumulator B to Index Register X Unsigned ", "[ABX]") { +TEST_CASE("Add Accumulator B to Index Register X Unsigned", "[ABX]") { Board board; board.initialise(); @@ -17,5 +17,29 @@ TEST_CASE("Add Accumulator B to Index Register X Unsigned ", "[ABX]") { cpu.X() = 0x1097; cpu.step(); REQUIRE(cpu.X() == 0x111b); + REQUIRE(cpu.cycles() == 3); + } +} + +TEST_CASE("Add Memory Plus Carry to Accumulator A", "[ADC][ADCA]") { + + Board board; + board.initialise(); + auto& cpu = board.CPU(); + cpu.step(); // Step over the reset + + SECTION("Immediate (byte)") { + board.poke(0, 0x89); + board.poke(1, 0x7c); + EightBit::Chip::setFlag(cpu.CC(), EightBit::mc6809::CF); + cpu.A() = 0x3a; + cpu.step(); + REQUIRE(cpu.A() == 0xb7); + REQUIRE((cpu.CC() & EightBit::mc6809::ZF) == 0); + REQUIRE((cpu.CC() & EightBit::mc6809::HF) != 0); + REQUIRE((cpu.CC() & EightBit::mc6809::VF) != 0); + REQUIRE((cpu.CC() & EightBit::mc6809::NF) != 0); + REQUIRE((cpu.CC() & EightBit::mc6809::CF) == 0); + REQUIRE(cpu.cycles() == 2); } }