From b4f8c81a942423a36c18c5c899527d38bff0a96f Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Sat, 2 Jan 2021 11:49:34 +0000 Subject: [PATCH] Use nodiscard, where appropriate and try not to inline virtual methods. Signed-off-by: Adrian Conlon --- inc/BigEndianProcessor.h | 8 +++---- inc/IntelHexFile.h | 10 ++++---- inc/IntelProcessor.h | 48 ++++++------------------------------- inc/LittleEndianProcessor.h | 8 +++---- inc/Processor.h | 6 +---- src/IntelProcessor.cpp | 42 ++++++++++++++++++++++++++++++++ src/Processor.cpp | 5 ++++ 7 files changed, 68 insertions(+), 59 deletions(-) diff --git a/inc/BigEndianProcessor.h b/inc/BigEndianProcessor.h index 0279954..48017e2 100644 --- a/inc/BigEndianProcessor.h +++ b/inc/BigEndianProcessor.h @@ -16,15 +16,15 @@ namespace EightBit { protected: BigEndianProcessor(Bus& memory); - register16_t getWord() override; + [[nodiscard]] register16_t getWord() override; void setWord(register16_t value) override; - register16_t getWordPaged(uint8_t page, uint8_t offset) override; + [[nodiscard]] register16_t getWordPaged(uint8_t page, uint8_t offset) override; void setWordPaged(uint8_t page, uint8_t offset, register16_t value) override; - register16_t fetchWord() final; + [[nodiscard]] register16_t fetchWord() final; void pushWord(register16_t value) final; - register16_t popWord() final; + [[nodiscard]] register16_t popWord() final; }; } diff --git a/inc/IntelHexFile.h b/inc/IntelHexFile.h index b96c24f..693bb7f 100644 --- a/inc/IntelHexFile.h +++ b/inc/IntelHexFile.h @@ -1,14 +1,14 @@ #pragma once #include -#include +//#include #include -#include -#include +//#include +//#include #include #include #include -#include +//#include namespace EightBit { class IntelHexFile final { @@ -21,7 +21,7 @@ namespace EightBit { [[nodiscard]] std::optional>> parse(std::string line); [[nodiscard]] std::vector parseDataRecord(std::string line, uint8_t count); - template T fromHex(std::string input) { + template [[nodiscard]] T fromHex(std::string input) { std::istringstream converter(input); unsigned output; converter >> std::hex >> output; diff --git a/inc/IntelProcessor.h b/inc/IntelProcessor.h index 253683d..11f05d9 100644 --- a/inc/IntelProcessor.h +++ b/inc/IntelProcessor.h @@ -132,47 +132,13 @@ namespace EightBit { // - virtual void restart(const uint8_t address) { - call(MEMPTR() = { address, 0 }); - } - - virtual int callConditional(const int condition) { - MEMPTR() = fetchWord(); - if (condition) - call(MEMPTR()); - return condition; - } - - virtual int jumpConditional(const int condition) { - MEMPTR() = fetchWord(); - if (condition) - jump(MEMPTR()); - return condition; - } - - virtual int returnConditional(const int condition) { - if (condition) - ret(); - return condition; - } - - virtual void jr(const int8_t offset) { - jump(MEMPTR() = PC() + offset); - } - - virtual int jrConditional(const int condition) { - const auto offsetAddress = PC()++; - if (condition) { - const auto offset = memoryRead(offsetAddress); - jr(offset); - } - return condition; - } - - void ret() override { - Processor::ret(); - MEMPTR() = PC(); - } + virtual void restart(uint8_t address); + virtual int callConditional(int condition); + virtual int jumpConditional(int condition); + virtual int returnConditional(int condition); + virtual void jr(int8_t offset); + virtual int jrConditional(int condition); + void ret() override; private: std::array m_decodedOpcodes; diff --git a/inc/LittleEndianProcessor.h b/inc/LittleEndianProcessor.h index a4cc577..be5ff5f 100644 --- a/inc/LittleEndianProcessor.h +++ b/inc/LittleEndianProcessor.h @@ -16,15 +16,15 @@ namespace EightBit { protected: LittleEndianProcessor(Bus& memory); - register16_t getWord() override; + [[nodiscard]] register16_t getWord() override; void setWord(register16_t value) override; - register16_t getWordPaged(uint8_t page, uint8_t offset) override; + [[nodiscard]] register16_t getWordPaged(uint8_t page, uint8_t offset) override; void setWordPaged(uint8_t page, uint8_t offset, register16_t value) override; - register16_t fetchWord() final; + [[nodiscard]] register16_t fetchWord() final; void pushWord(register16_t value) override; - register16_t popWord() override; + [[nodiscard]] register16_t popWord() override; }; } diff --git a/inc/Processor.h b/inc/Processor.h index 5026c52..2be17fb 100644 --- a/inc/Processor.h +++ b/inc/Processor.h @@ -88,11 +88,7 @@ namespace EightBit { PC() = destination; } - virtual void call(const register16_t destination) { - pushWord(PC()); - jump(destination); - } - + virtual void call(register16_t destination); virtual void ret(); private: diff --git a/src/IntelProcessor.cpp b/src/IntelProcessor.cpp index 6154c4e..b060891 100644 --- a/src/IntelProcessor.cpp +++ b/src/IntelProcessor.cpp @@ -40,3 +40,45 @@ void EightBit::IntelProcessor::setWord(const register16_t value) { LittleEndianProcessor::setWord(value); MEMPTR() = BUS().ADDRESS(); } + +void EightBit::IntelProcessor::restart(const uint8_t address) { + call(MEMPTR() = { address, 0 }); +} + +int EightBit::IntelProcessor::callConditional(const int condition) { + MEMPTR() = fetchWord(); + if (condition) + call(MEMPTR()); + return condition; +} + +int EightBit::IntelProcessor::jumpConditional(const int condition) { + MEMPTR() = fetchWord(); + if (condition) + jump(MEMPTR()); + return condition; +} + +int EightBit::IntelProcessor::returnConditional(const int condition) { + if (condition) + ret(); + return condition; +} + +void EightBit::IntelProcessor::jr(const int8_t offset) { + jump(MEMPTR() = PC() + offset); +} + +int EightBit::IntelProcessor::jrConditional(const int condition) { + const auto offsetAddress = PC()++; + if (condition) { + const auto offset = memoryRead(offsetAddress); + jr(offset); + } + return condition; +} + +void EightBit::IntelProcessor::ret() { + Processor::ret(); + MEMPTR() = PC(); +} diff --git a/src/Processor.cpp b/src/Processor.cpp index cbfcd58..1709b0a 100644 --- a/src/Processor.cpp +++ b/src/Processor.cpp @@ -67,6 +67,11 @@ int8_t EightBit::Processor::signExtend(const int b, uint8_t x) noexcept { return result; } +void EightBit::Processor::call(const register16_t destination) { + pushWord(PC()); + jump(destination); +} + void EightBit::Processor::ret() { jump(popWord()); }