2017-06-04 20:38:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
2018-09-23 12:10:58 +00:00
|
|
|
#include "Chip.h"
|
2017-09-06 12:22:23 +00:00
|
|
|
#include "Bus.h"
|
|
|
|
#include "Register.h"
|
2019-01-09 23:24:33 +00:00
|
|
|
#include "Signal.h"
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2017-11-20 19:17:49 +00:00
|
|
|
#include "EightBitCompilerDefinitions.h"
|
2017-09-03 11:11:14 +00:00
|
|
|
|
2017-06-04 20:38:34 +00:00
|
|
|
namespace EightBit {
|
2018-09-23 12:10:58 +00:00
|
|
|
class Processor : public Chip {
|
2017-06-04 20:38:34 +00:00
|
|
|
public:
|
2018-08-27 11:57:44 +00:00
|
|
|
// b: number of bits representing the number in x
|
|
|
|
// x: sign extend this b-bit number to r
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] static int8_t signExtend(int b, uint8_t x);
|
2018-08-27 11:57:44 +00:00
|
|
|
|
2018-11-27 22:36:54 +00:00
|
|
|
~Processor() {};
|
|
|
|
|
2019-01-09 23:24:33 +00:00
|
|
|
Signal<EventArgs> Ticked;
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] auto& PC() noexcept { return m_pc; }
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] auto& RESET() noexcept { return m_resetLine; }
|
|
|
|
[[nodiscard]] auto& HALT() noexcept { return m_haltLine; }
|
|
|
|
[[nodiscard]] auto& INT() noexcept { return m_intLine; }
|
|
|
|
[[nodiscard]] auto& IRQ() noexcept { return INT(); } // Synonym
|
2018-06-09 23:40:56 +00:00
|
|
|
|
2018-11-27 22:36:54 +00:00
|
|
|
void powerOn() override;
|
2018-11-28 21:27:14 +00:00
|
|
|
void reset() noexcept { lower(RESET()); }
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2017-11-11 15:50:55 +00:00
|
|
|
int run(int limit);
|
2017-08-30 22:17:34 +00:00
|
|
|
virtual int step() = 0;
|
2018-12-29 19:17:36 +00:00
|
|
|
virtual int execute() = 0;
|
|
|
|
int execute(uint8_t value);
|
2017-07-25 17:56:43 +00:00
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] auto cycles() const noexcept { return m_cycles; }
|
2018-08-25 11:09:26 +00:00
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] virtual register16_t peekWord(register16_t address) = 0;
|
2018-10-14 09:05:43 +00:00
|
|
|
virtual void pokeWord(register16_t address, register16_t value) = 0;
|
2018-08-27 10:27:33 +00:00
|
|
|
|
2017-06-04 20:38:34 +00:00
|
|
|
protected:
|
2017-09-06 12:22:23 +00:00
|
|
|
Processor(Bus& memory);
|
2017-06-11 08:45:34 +00:00
|
|
|
|
2018-12-29 19:17:36 +00:00
|
|
|
[[nodiscard]] auto& opcode() noexcept { return m_opcode; }
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] auto& BUS() noexcept { return m_bus; }
|
2018-10-20 19:52:41 +00:00
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] auto halted() noexcept { return lowered(HALT()); }
|
|
|
|
void halt() noexcept { --PC(); lower(HALT()); }
|
|
|
|
void proceed() noexcept { ++PC(); raise(HALT()); }
|
2017-12-10 21:41:48 +00:00
|
|
|
|
2018-08-25 00:34:30 +00:00
|
|
|
virtual void handleRESET();
|
|
|
|
virtual void handleINT();
|
2018-08-25 11:09:26 +00:00
|
|
|
virtual void handleIRQ();
|
2018-08-25 00:34:30 +00:00
|
|
|
|
2018-12-29 19:17:36 +00:00
|
|
|
void busWrite(register16_t address, uint8_t data);
|
|
|
|
void busWrite(uint8_t data);
|
|
|
|
virtual void busWrite();
|
|
|
|
|
2019-01-03 01:04:12 +00:00
|
|
|
uint8_t busRead(register16_t address);
|
|
|
|
virtual uint8_t busRead();
|
2018-12-29 19:17:36 +00:00
|
|
|
|
2019-01-06 11:10:05 +00:00
|
|
|
auto getBytePaged(const uint8_t page, const uint8_t offset) {
|
2018-12-29 19:17:36 +00:00
|
|
|
return busRead(register16_t(offset, page));
|
2018-08-17 20:53:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 23:16:00 +00:00
|
|
|
void setBytePaged(const uint8_t page, const uint8_t offset, const uint8_t value) {
|
2018-12-29 19:17:36 +00:00
|
|
|
busWrite(register16_t(offset, page), value);
|
2018-08-17 20:53:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 11:10:05 +00:00
|
|
|
auto fetchByte() {
|
2018-12-29 19:17:36 +00:00
|
|
|
return busRead(PC()++);
|
2018-01-06 17:13:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] virtual register16_t getWord() = 0;
|
2018-08-17 20:53:49 +00:00
|
|
|
virtual void setWord(register16_t value) = 0;
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] virtual register16_t getWordPaged(uint8_t page, uint8_t offset) = 0;
|
2018-08-17 20:53:49 +00:00
|
|
|
virtual void setWordPaged(uint8_t page, uint8_t offset, register16_t value) = 0;
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] virtual register16_t fetchWord() = 0;
|
2017-08-28 20:18:08 +00:00
|
|
|
|
2017-08-28 12:19:17 +00:00
|
|
|
virtual void push(uint8_t value) = 0;
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] virtual uint8_t pop() = 0;
|
2017-08-28 12:19:17 +00:00
|
|
|
|
2018-11-25 10:43:51 +00:00
|
|
|
virtual void pushWord(register16_t value) = 0;
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] virtual register16_t popWord() = 0;
|
2017-08-28 12:19:17 +00:00
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
[[nodiscard]] auto getWord(const register16_t address) {
|
2018-08-21 17:10:38 +00:00
|
|
|
BUS().ADDRESS() = address;
|
|
|
|
return getWord();
|
|
|
|
}
|
|
|
|
|
2018-09-20 23:16:00 +00:00
|
|
|
void setWord(const register16_t address, const register16_t value) {
|
2018-08-21 17:10:38 +00:00
|
|
|
BUS().ADDRESS() = address;
|
|
|
|
return setWord(value);
|
|
|
|
}
|
|
|
|
|
2018-11-28 21:27:14 +00:00
|
|
|
void jump(const register16_t destination) noexcept {
|
2018-03-18 22:40:23 +00:00
|
|
|
PC() = destination;
|
2017-08-28 20:18:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 08:39:06 +00:00
|
|
|
void call(const register16_t destination) {
|
2017-08-28 20:18:08 +00:00
|
|
|
pushWord(PC());
|
2018-03-18 22:40:23 +00:00
|
|
|
jump(destination);
|
2017-08-28 20:18:08 +00:00
|
|
|
}
|
|
|
|
|
2018-11-25 10:43:51 +00:00
|
|
|
virtual void ret();
|
2017-08-28 20:18:08 +00:00
|
|
|
|
2018-11-28 21:27:14 +00:00
|
|
|
void resetCycles() noexcept { m_cycles = 0; }
|
2019-01-09 23:24:33 +00:00
|
|
|
void tick(const int extra) { for (int i = 0; i < extra; ++i) tick(); }
|
|
|
|
void tick() { ++m_cycles; Ticked.fire(EventArgs::empty()); }
|
2017-11-03 22:05:01 +00:00
|
|
|
|
2017-06-19 12:53:00 +00: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;
|
2017-11-10 22:41:50 +00:00
|
|
|
int m_cycles = 0;
|
2018-06-15 23:55:32 +00:00
|
|
|
register16_t m_pc;
|
2017-12-10 21:41:48 +00:00
|
|
|
|
2018-12-01 15:24:29 +00:00
|
|
|
PinLevel m_intLine = PinLevel::Low;
|
|
|
|
PinLevel m_haltLine = PinLevel::Low;
|
|
|
|
PinLevel m_resetLine = PinLevel::Low;
|
2017-06-04 20:38:34 +00:00
|
|
|
};
|
2017-11-30 23:19:17 +00:00
|
|
|
}
|