diff --git a/Intel8080/inc/Intel8080.h b/Intel8080/inc/Intel8080.h index ba976b3..f34cca5 100644 --- a/Intel8080/inc/Intel8080.h +++ b/Intel8080/inc/Intel8080.h @@ -79,8 +79,7 @@ namespace EightBit { } void adjustReservedFlags() { - F() &= ~(Bit5 | Bit3); - F() |= Bit1; + F() = (F() | Bit1) & ~(Bit5 | Bit3); } static void adjustAuxiliaryCarryAdd(uint8_t& f, uint8_t before, uint8_t value, int calculation) { @@ -150,9 +149,9 @@ namespace EightBit { void dad(uint16_t value) { auto& f = F(); - uint32_t sum = HL().word + value; - setFlag(f, CF, sum > 0xffff); - HL().word = (uint16_t)sum; + auto sum = HL().word + value; + setFlag(f, CF, sum & Bit16); + HL().word = sum; } void sub(uint8_t& operand, uint8_t value, int carry = 0) { @@ -527,13 +526,11 @@ namespace EightBit { } void sbi() { - auto value = fetchByte(); - sbb(value); + sbb(fetchByte()); } void sui() { - auto value = fetchByte(); - sub(value); + sub(fetchByte()); } // logical @@ -603,35 +600,31 @@ namespace EightBit { void rlc() { auto& a = A(); auto carry = a & Bit7; - a <<= 1; - carry ? a |= Bit0 : a &= ~Bit0; + a = (a << 1) | (carry >> 7); setFlag(F(), CF, carry); } void rrc() { auto& a = A(); auto carry = a & Bit0; - a >>= 1; - carry ? a |= Bit7 : a &= ~Bit7; + a = (a >> 1) | (carry << 7); setFlag(F(), CF, carry); } void ral() { auto& a = A(); auto& f = F(); - auto carry = a & Bit7; - a <<= 1; - a |= (f & CF); - setFlag(f, CF, carry); + const auto carry = f & CF; + setFlag(f, CF, a & Bit7); + a = (a << 1) | carry; } void rar() { auto& a = A(); auto& f = F(); - auto carry = a & 1; - a >>= 1; - a |= (f & CF) << 7; - setFlag(f, CF, carry); + const auto carry = f & CF; + setFlag(f, CF, a & Bit0); + a = (a >> 1) | (carry << 7); } // specials diff --git a/Z80/inc/Z80.h b/Z80/inc/Z80.h index 5e692b1..bb14798 100644 --- a/Z80/inc/Z80.h +++ b/Z80/inc/Z80.h @@ -221,6 +221,8 @@ namespace EightBit { } register16_t& RP(int rp) { + __assume(rp < 4); + __assume(rp >= 0); switch (rp) { case 3: return SP(); @@ -241,6 +243,8 @@ namespace EightBit { } register16_t& RP2(int rp) { + __assume(rp < 4); + __assume(rp >= 0); switch (rp) { case 3: return AF(); diff --git a/Z80/src/Z80.cpp b/Z80/src/Z80.cpp index 7c6e3e3..54f4ba2 100644 --- a/Z80/src/Z80.cpp +++ b/Z80/src/Z80.cpp @@ -148,6 +148,8 @@ bool EightBit::Z80::jrConditionalFlag(uint8_t& f, int flag) { return jrConditional(!(f & SF)); case 7: // M return jrConditional(f & SF); + default: + __assume(0); } throw std::logic_error("Unhandled JR conditional"); } @@ -170,6 +172,8 @@ bool EightBit::Z80::jumpConditionalFlag(uint8_t& f, int flag) { return jumpConditional(!(f & SF)); case 7: // M return jumpConditional(f & SF); + default: + __assume(0); } throw std::logic_error("Unhandled JP conditional"); } @@ -201,6 +205,8 @@ bool EightBit::Z80::returnConditionalFlag(uint8_t& f, int flag) { return returnConditional(!(f & SF)); case 7: // M return returnConditional(f & SF); + default: + __assume(0); } throw std::logic_error("Unhandled RET conditional"); } @@ -223,6 +229,8 @@ bool EightBit::Z80::callConditionalFlag(uint8_t& f, int flag) { return callConditional(!(f & SF)); case 7: // M return callConditional(f & SF); + default: + __assume(0); } throw std::logic_error("Unhandled CALL conditional"); } @@ -435,7 +443,7 @@ uint8_t& EightBit::Z80::srl(uint8_t& f, uint8_t& operand) { uint8_t& EightBit::Z80::bit(uint8_t& f, int n, uint8_t& operand) { setFlag(f, HC); clearFlag(f, NF); - const uint8_t discarded = operand & (1 << n); + const auto discarded = operand & (1 << n); adjustSZXY(f, discarded); clearFlag(f, PF, discarded); return operand; @@ -543,8 +551,7 @@ void EightBit::Z80::blockCompare(uint8_t a, uint8_t& f) { adjustHalfCarrySub(f, a, value, result); setFlag(f, NF); - if (f & HC) - result -= 1; + result -= ((f & HC) >> 4); setFlag(f, YF, result & Bit1); setFlag(f, XF, result & Bit3); @@ -825,18 +832,18 @@ void EightBit::Z80::executeCB(int x, int y, int z) { adjustSZP(f, m_displaced ? R2(z, a) = srl(f, DISPLACED()) : srl(f, R(z, a))); break; } + cycles += 8; if (!m_displaced) { - cycles += 8; if (z == 6) cycles += 7; } else { - cycles += 23; + cycles += 15; } break; case 1: // BIT y, r[z] + cycles += 8; if (!m_displaced) { auto operand = bit(f, y, R(z, a)); - cycles += 8; if (z == 6) { adjustXY(f, MEMPTR().high); cycles += 4; @@ -846,29 +853,29 @@ void EightBit::Z80::executeCB(int x, int y, int z) { } else { bit(f, y, DISPLACED()); adjustXY(f, MEMPTR().high); - cycles += 20; + cycles += 12; } break; case 2: // RES y, r[z] + cycles += 8; if (!m_displaced) { res(y, R(z, a)); - cycles += 8; if (z == 6) cycles += 7; } else { R2(z, a) = res(y, DISPLACED()); - cycles += 23; + cycles += 15; } break; case 3: // SET y, r[z] + cycles += 8; if (!m_displaced) { set(y, R(z, a)); - cycles += 8; if (z == 6) cycles += 7; } else { R2(z, a) = set(y, DISPLACED()); - cycles += 23; + cycles += 15; } break; default: diff --git a/Z80/src/stdafx.h b/Z80/src/stdafx.h index fe79e2d..ef248a1 100644 --- a/Z80/src/stdafx.h +++ b/Z80/src/stdafx.h @@ -19,24 +19,3 @@ #include #include - -//#include -//#include -// -//#if SDL_BYTEORDER == SDL_LIL_ENDIAN -//#define HOST_LITTLE_ENDIAN -//#endif -// -//#if SDL_BYTEORDER == SDL_BIG_ENDIAN -//#define HOST_BIG_ENDIAN -//#endif -// -//#define RUN_TESTS -////#define RUN_CPM -////#define RUN_INVADERS -// -//#ifdef _MSC_VER -//#pragma comment(lib, "SDL2.lib") -//#pragma comment(lib, "SDL2main.lib") -//#pragma comment(lib, "SDL2_mixer.lib") -//#endif diff --git a/inc/IntelProcessor.h b/inc/IntelProcessor.h index 8a33626..67a1538 100644 --- a/inc/IntelProcessor.h +++ b/inc/IntelProcessor.h @@ -54,7 +54,7 @@ namespace EightBit { } template static void adjustParity(uint8_t& f, uint8_t value) { - static const uint8_t lookup[0x10] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; + static const int lookup[0x10] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; auto set = lookup[highNibble(value)] + lookup[lowNibble(value)]; clearFlag(f, T::PF, set % 2); } @@ -87,16 +87,19 @@ namespace EightBit { // static int buildHalfCarryIndex(uint8_t before, uint8_t value, int calculation) { + __assume(calculation < 0x1ffff); return ((before & 0x88) >> 1) | ((value & 0x88) >> 2) | ((calculation & 0x88) >> 3); } static bool calculateHalfCarryAdd(uint8_t before, uint8_t value, int calculation) { + __assume(calculation < 0x1ffff); static std::array m_halfCarryTableAdd = { { false, false, true, false, true, false, true, true } }; auto index = buildHalfCarryIndex(before, value, calculation); return m_halfCarryTableAdd[index & Mask3]; } static bool calculateHalfCarrySub(uint8_t before, uint8_t value, int calculation) { + __assume(calculation < 0x1ffff); std::array m_halfCarryTableSub = { { false, true, true, true, false, false, false, true } }; auto index = buildHalfCarryIndex(before, value, calculation); return m_halfCarryTableSub[index & Mask3];