From dfc4c4945486d24d2d313af5bde957cf390a6ebb Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Mon, 27 Aug 2018 12:57:44 +0100 Subject: [PATCH] Correct 5-bit sign extension on the 6809 processor. Allows CLR 5-bit offset indexed disassembly to work correctly. Signed-off-by: Adrian Conlon --- MC6809/src/Disassembly.cpp | 4 ++-- MC6809/src/mc6809.cpp | 2 +- inc/Processor.h | 4 ++++ src/Processor.cpp | 8 ++++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/MC6809/src/Disassembly.cpp b/MC6809/src/Disassembly.cpp index 712f4dc..da89712 100644 --- a/MC6809/src/Disassembly.cpp +++ b/MC6809/src/Disassembly.cpp @@ -206,7 +206,7 @@ std::string EightBit::Disassembly::disassembleUnprefixed() { //case 0x0f: BUS().write(Address_direct(), clr()); break; // CLR (direct) //case 0x4f: A() = clr(); break; // CLR (CLRA implied) //case 0x5f: B() = clr(); break; // CLR (CLRB implied) - //case 0x6f: BUS().write(Address_indexed(), clr()); break; // CLR (indexed) + case 0x6f: output << Address_indexed("CLR"); break; // CLR (indexed) //case 0x7f: BUS().write(Address_extended(), clr()); break; // CLR (extended) //// CMP @@ -693,7 +693,7 @@ std::string EightBit::Disassembly::Address_indexed(std::string mnemomic) { // EA = ,R + 5-bit offset output << "\t" << mnemomic << "\t" - << dump_Byte(type & Processor::Mask5) << "," << r; + << (int)Processor::signExtend(5, type & Processor::Mask5) << "," << r; } return output.str(); diff --git a/MC6809/src/mc6809.cpp b/MC6809/src/mc6809.cpp index c849e1c..b9cb68d 100644 --- a/MC6809/src/mc6809.cpp +++ b/MC6809/src/mc6809.cpp @@ -682,7 +682,7 @@ EightBit::register16_t EightBit::mc6809::Address_indexed() { } else { // EA = ,R + 5-bit offset addCycle(); - address = r + (type & Mask5); + address = r + signExtend(5, type & Mask5); } return address; } diff --git a/inc/Processor.h b/inc/Processor.h index c83d7a1..2028ffc 100644 --- a/inc/Processor.h +++ b/inc/Processor.h @@ -67,6 +67,10 @@ namespace EightBit { static int promoteNibble(const int value) { return value << 4; } static int demoteNibble(const int value) { return highNibble(value); } + // b: number of bits representing the number in x + // x: sign extend this b-bit number to r + static int8_t signExtend(int b, uint8_t x); + Bus& BUS() { return m_bus; } register16_t& PC() { return m_pc; } diff --git a/src/Processor.cpp b/src/Processor.cpp index 4eaca27..32415eb 100644 --- a/src/Processor.cpp +++ b/src/Processor.cpp @@ -31,3 +31,11 @@ int EightBit::Processor::run(const int limit) { current += step(); return current; } + +// http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend +int8_t EightBit::Processor::signExtend(int b, uint8_t x) { + const uint8_t m = 1 << (b - 1); // mask can be pre-computed if b is fixed + x = x & ((1 << b) - 1); // (Skip this if bits in x above position b are already zero.) + const auto result = (x ^ m) - m; + return result; +}