2017-06-04 21:38:34 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
2019-01-10 22:23:51 +00:00
|
|
|
#include "ClockedChip.h"
|
2017-09-06 13:22:23 +01:00
|
|
|
#include "Bus.h"
|
|
|
|
#include "Register.h"
|
2017-06-04 21:38:34 +01:00
|
|
|
|
2017-11-20 19:17:49 +00:00
|
|
|
#include "EightBitCompilerDefinitions.h"
|
2017-09-03 12:11:14 +01:00
|
|
|
|
2017-06-04 21:38:34 +01:00
|
|
|
namespace EightBit {
|
2019-01-10 22:23:51 +00:00
|
|
|
class Processor : public ClockedChip {
|
2017-06-04 21:38:34 +01:00
|
|
|
public:
|
2021-05-29 10:31:32 +01:00
|
|
|
// http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend
|
2018-08-27 12:57:44 +01:00
|
|
|
// b: number of bits representing the number in x
|
|
|
|
// x: sign extend this b-bit number to r
|
2021-05-29 10:31:32 +01:00
|
|
|
[[nodiscard]] static constexpr int8_t signExtend(int b, uint8_t x) noexcept {
|
|
|
|
const uint8_t m = bit(b - 1); // mask can be pre-computed if b is fixed
|
|
|
|
x = x & (bit(b) - 1); // (Skip this if bits in x above position b are already zero.)
|
|
|
|
const auto result = (x ^ m) - m;
|
|
|
|
return result;
|
|
|
|
}
|
2018-08-27 12:57:44 +01:00
|
|
|
|
2021-12-27 22:07:30 +00:00
|
|
|
Processor(const Processor& rhs);
|
2021-12-27 14:24:38 +00:00
|
|
|
bool operator==(const Processor& rhs) const;
|
|
|
|
|
2021-07-18 14:28:40 +01:00
|
|
|
[[nodiscard]] constexpr auto& PC() noexcept { return m_pc; }
|
2021-12-27 14:24:38 +00:00
|
|
|
[[nodiscard]] constexpr const auto& PC() const noexcept { return m_pc; }
|
2017-06-04 21:38:34 +01:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
int run(int limit) noexcept;
|
|
|
|
virtual int step() noexcept = 0;
|
|
|
|
virtual int execute() noexcept = 0;
|
|
|
|
int execute(uint8_t value) noexcept;
|
2017-07-25 18:56:43 +01:00
|
|
|
|
2021-07-18 14:28:40 +01:00
|
|
|
[[nodiscard]] virtual register16_t peekWord(register16_t address) noexcept = 0;
|
|
|
|
virtual void pokeWord(register16_t address, register16_t value) noexcept = 0;
|
2018-08-27 11:27:33 +01:00
|
|
|
|
2019-01-14 23:17:54 +00:00
|
|
|
DECLARE_PIN_INPUT(RESET)
|
|
|
|
DECLARE_PIN_INPUT(INT)
|
|
|
|
|
2017-06-04 21:38:34 +01:00
|
|
|
protected:
|
2021-07-18 14:28:40 +01:00
|
|
|
Processor(Bus& memory) noexcept;
|
2017-06-11 09:45:34 +01:00
|
|
|
|
2021-07-18 14:28:40 +01:00
|
|
|
[[nodiscard]] constexpr auto& opcode() noexcept { return m_opcode; }
|
|
|
|
[[nodiscard]] constexpr auto& BUS() noexcept { return m_bus; }
|
2018-10-20 20:52:41 +01:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
virtual void handleRESET() noexcept;
|
|
|
|
virtual void handleINT() noexcept;
|
2018-08-25 01:34:30 +01:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
void memoryWrite(register16_t address, uint8_t data) noexcept;
|
|
|
|
void memoryWrite(register16_t address) noexcept;
|
|
|
|
void memoryWrite(uint8_t data) noexcept;
|
|
|
|
virtual void memoryWrite() noexcept;
|
|
|
|
virtual void busWrite() noexcept;
|
2018-12-29 19:17:36 +00:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
uint8_t memoryRead(register16_t address) noexcept;
|
|
|
|
virtual uint8_t memoryRead() noexcept;
|
|
|
|
virtual uint8_t busRead() noexcept;
|
2018-12-29 19:17:36 +00:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
uint8_t getBytePaged(uint8_t page, uint8_t offset) noexcept;
|
|
|
|
void setBytePaged(uint8_t page, uint8_t offset, uint8_t value) noexcept;
|
2018-08-17 21:53:49 +01:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
uint8_t fetchByte() noexcept;
|
2018-01-06 17:13:02 +00:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
[[nodiscard]] virtual register16_t getWord() noexcept = 0;
|
|
|
|
virtual void setWord(register16_t value) noexcept = 0;
|
2018-08-17 21:53:49 +01:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
[[nodiscard]] virtual register16_t getWordPaged(uint8_t page, uint8_t offset) noexcept = 0;
|
|
|
|
virtual void setWordPaged(uint8_t page, uint8_t offset, register16_t value) noexcept = 0;
|
2018-08-17 21:53:49 +01:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
[[nodiscard]] virtual register16_t fetchWord() noexcept = 0;
|
2017-08-28 21:18:08 +01:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
virtual void push(uint8_t value) noexcept = 0;
|
|
|
|
[[nodiscard]] virtual uint8_t pop() noexcept = 0;
|
2017-08-28 13:19:17 +01:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
virtual void pushWord(register16_t value) noexcept = 0;
|
|
|
|
[[nodiscard]] virtual register16_t popWord() noexcept = 0;
|
2017-08-28 13:19:17 +01:00
|
|
|
|
2022-01-17 19:10:15 +00:00
|
|
|
[[nodiscard]] register16_t getWord(register16_t address) noexcept;
|
|
|
|
void setWord(register16_t address, register16_t value) noexcept;
|
2017-08-28 21:18:08 +01:00
|
|
|
|
2021-01-09 08:41:48 +00:00
|
|
|
void jump(const register16_t destination) noexcept;
|
2022-01-17 19:10:15 +00:00
|
|
|
virtual void call(register16_t destination) noexcept;
|
|
|
|
virtual void ret() noexcept;
|
2017-08-28 21:18:08 +01:00
|
|
|
|
2017-06-19 13:53:00 +01:00
|
|
|
private:
|
2017-11-03 22:05:01 +00:00
|
|
|
Bus& m_bus;
|
2018-12-29 19:17:36 +00:00
|
|
|
uint8_t m_opcode = Mask8;
|
2018-06-16 00:55:32 +01:00
|
|
|
register16_t m_pc;
|
2017-06-04 21:38:34 +01:00
|
|
|
};
|
2017-11-30 23:19:17 +00:00
|
|
|
}
|