#pragma once #include #include "Memory.h" namespace EightBit { class Processor { public: enum Masks { Mask1 = 0x01, Mask2 = 0x03, Mask3 = 0x07, Mask4 = 0x0f, Mask5 = 0x1f, Mask6 = 0x3f, Mask7 = 0x7f, Mask8 = 0xff, }; enum Bits { Bit16 = 0x10000, Bit15 = 0x8000, Bit14 = 0x4000, Bit13 = 0x2000, Bit12 = 0x1000, Bit11 = 0x800, Bit10 = 0x400, Bit9 = 0x200, Bit8 = 0x100, Bit7 = 0x80, Bit6 = 0x40, Bit5 = 0x20, Bit4 = 0x10, Bit3 = 0x8, Bit2 = 0x4, Bit1 = 0x2, Bit0 = 0x1, }; static uint8_t highNibble(uint8_t value) { return value >> 4; } static uint8_t lowNibble(uint8_t value) { return value & Mask4; } static uint8_t promoteNibble(uint8_t value) { return value << 4; } static uint8_t demoteNibble(uint8_t value) { return highNibble(value); } const Memory& getMemory() const { return m_memory; } register16_t& PC() { return pc; } register16_t& SP() { return sp; } bool isHalted() const { return m_halted; } void halt() { --PC().word; m_halted = true; } virtual void initialise(); void reset(); protected: Processor(Memory& memory); Memory& m_memory; int cycles; uint8_t fetchByte() { m_memory.ADDRESS().word = PC().word++; return m_memory.reference(); } void fetchWord(register16_t& output) { output.low = fetchByte(); output.high = fetchByte(); } private: register16_t pc; register16_t sp; bool m_halted; }; }