From 754fc8e6a32e0309ec50c44de79683c5201f8dd0 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Sun, 23 Sep 2018 13:10:58 +0100 Subject: [PATCH] Refactor the processor class to give us a "Chip" class that gives up pin levels and power. Signed-off-by: Adrian Conlon --- inc/Chip.h | 78 ++++++++++++++++++++++++++++++++++++ inc/Processor.h | 64 +---------------------------- src/Chip.cpp | 6 +++ src/EightBit.vcxproj | 2 + src/EightBit.vcxproj.filters | 6 +++ src/Processor.cpp | 2 +- 6 files changed, 95 insertions(+), 63 deletions(-) create mode 100644 inc/Chip.h create mode 100644 src/Chip.cpp diff --git a/inc/Chip.h b/inc/Chip.h new file mode 100644 index 0000000..94190f0 --- /dev/null +++ b/inc/Chip.h @@ -0,0 +1,78 @@ +#pragma once + +#include + +namespace EightBit { + class Chip { + public: + enum Bits { + Bit0 = 1, + Bit1 = Bit0 << 1, + Bit2 = Bit1 << 1, + Bit3 = Bit2 << 1, + Bit4 = Bit3 << 1, + Bit5 = Bit4 << 1, + Bit6 = Bit5 << 1, + Bit7 = Bit6 << 1, + Bit8 = Bit7 << 1, + Bit9 = Bit8 << 1, + Bit10 = Bit9 << 1, + Bit11 = Bit10 << 1, + Bit12 = Bit11 << 1, + Bit13 = Bit12 << 1, + Bit14 = Bit13 << 1, + Bit15 = Bit14 << 1, + Bit16 = Bit15 << 1 + }; + + enum Masks { + Mask1 = Bit1 - 1, + Mask2 = Bit2 - 1, + Mask3 = Bit3 - 1, + Mask4 = Bit4 - 1, + Mask5 = Bit5 - 1, + Mask6 = Bit6 - 1, + Mask7 = Bit7 - 1, + Mask8 = Bit8 - 1, + Mask9 = Bit9 - 1, + Mask10 = Bit10 - 1, + Mask11 = Bit11 - 1, + Mask12 = Bit12 - 1, + Mask13 = Bit13 - 1, + Mask14 = Bit14 - 1, + Mask15 = Bit15 - 1, + Mask16 = Bit16 - 1 + }; + + enum PinLevel { + Low, High + }; + + static bool raised(const PinLevel line) { return line == High; } + static void raise(PinLevel& line) { line = High; } + static bool lowered(const PinLevel line) { return line == Low; } + static void lower(PinLevel& line) { line = Low; } + + static int highNibble(const int value) { return value >> 4; } + static int lowNibble(const int value) { return value & Mask4; } + + static int higherNibble(const int value) { return value & 0xf0; } + static int lowerNibble(const int value) { return lowNibble(value); } + + static int promoteNibble(const int value) { return value << 4; } + static int demoteNibble(const int value) { return highNibble(value); } + + PinLevel& POWER() { return m_powerLine; } + + bool powered() { return raised(POWER()); } + virtual void powerOn(); + void powerOff() { lower(POWER()); } + + protected: + Chip() = default; + virtual ~Chip() = default; + + private: + PinLevel m_powerLine = Low; + }; +} diff --git a/inc/Processor.h b/inc/Processor.h index 9384a8a..f136bec 100644 --- a/inc/Processor.h +++ b/inc/Processor.h @@ -2,71 +2,15 @@ #include +#include "Chip.h" #include "Bus.h" #include "Register.h" #include "EightBitCompilerDefinitions.h" namespace EightBit { - class Processor { + class Processor : public Chip { public: - enum Bits { - Bit0 = 1, - Bit1 = Bit0 << 1, - Bit2 = Bit1 << 1, - Bit3 = Bit2 << 1, - Bit4 = Bit3 << 1, - Bit5 = Bit4 << 1, - Bit6 = Bit5 << 1, - Bit7 = Bit6 << 1, - Bit8 = Bit7 << 1, - Bit9 = Bit8 << 1, - Bit10 = Bit9 << 1, - Bit11 = Bit10 << 1, - Bit12 = Bit11 << 1, - Bit13 = Bit12 << 1, - Bit14 = Bit13 << 1, - Bit15 = Bit14 << 1, - Bit16 = Bit15 << 1 - }; - - enum Masks { - Mask1 = Bit1 - 1, - Mask2 = Bit2 - 1, - Mask3 = Bit3 - 1, - Mask4 = Bit4 - 1, - Mask5 = Bit5 - 1, - Mask6 = Bit6 - 1, - Mask7 = Bit7 - 1, - Mask8 = Bit8 - 1, - Mask9 = Bit9 - 1, - Mask10 = Bit10 - 1, - Mask11 = Bit11 - 1, - Mask12 = Bit12 - 1, - Mask13 = Bit13 - 1, - Mask14 = Bit14 - 1, - Mask15 = Bit15 - 1, - Mask16 = Bit16 - 1 - }; - - enum PinLevel { - Low, High - }; - - static bool raised(const PinLevel line) { return line == High; } - static void raise(PinLevel& line) { line = High; } - static bool lowered(const PinLevel line) { return line == Low; } - static void lower(PinLevel& line) { line = Low; } - - static int highNibble(const int value) { return value >> 4; } - static int lowNibble(const int value) { return value & Mask4; } - - static int higherNibble(const int value) { return value & 0xf0; } - static int lowerNibble(const int value) { return lowNibble(value); } - - 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); @@ -79,11 +23,8 @@ namespace EightBit { PinLevel& HALT() { return m_haltLine; } PinLevel& INT() { return m_intLine; } PinLevel& IRQ() { return INT(); } // Synonym - PinLevel& POWER() { return m_powerLine; } - bool powered() { return raised(POWER()); } virtual void powerOn(); - void powerOff() { lower(POWER()); } void reset() { lower(RESET()); } int run(int limit); @@ -178,6 +119,5 @@ namespace EightBit { PinLevel m_intLine = Low; PinLevel m_haltLine = Low; PinLevel m_resetLine = Low; - PinLevel m_powerLine = Low; }; } diff --git a/src/Chip.cpp b/src/Chip.cpp new file mode 100644 index 0000000..0d9aa64 --- /dev/null +++ b/src/Chip.cpp @@ -0,0 +1,6 @@ +#include "stdafx.h" +#include "Chip.h" + +void EightBit::Chip::powerOn() { + raise(POWER()); +} \ No newline at end of file diff --git a/src/EightBit.vcxproj b/src/EightBit.vcxproj index 70fd85d..8407eb9 100644 --- a/src/EightBit.vcxproj +++ b/src/EightBit.vcxproj @@ -138,6 +138,7 @@ + @@ -155,6 +156,7 @@ + diff --git a/src/EightBit.vcxproj.filters b/src/EightBit.vcxproj.filters index e382f0a..03bb610 100644 --- a/src/EightBit.vcxproj.filters +++ b/src/EightBit.vcxproj.filters @@ -56,6 +56,9 @@ Header Files + + Header Files + @@ -85,5 +88,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/src/Processor.cpp b/src/Processor.cpp index 308389d..7bb58ba 100644 --- a/src/Processor.cpp +++ b/src/Processor.cpp @@ -6,10 +6,10 @@ EightBit::Processor::Processor(Bus& bus) } void EightBit::Processor::powerOn() { + Chip::powerOn(); raise(RESET()); raise(HALT()); raise(INT()); - raise(POWER()); } void EightBit::Processor::handleRESET() {