2017-06-04 21:38:34 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
2018-09-23 13:10:58 +01:00
|
|
|
#include "Chip.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 {
|
2018-09-23 13:10:58 +01:00
|
|
|
class Processor : public Chip {
|
2017-06-04 21:38:34 +01:00
|
|
|
public:
|
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
|
|
|
|
static int8_t signExtend(int b, uint8_t x);
|
|
|
|
|
2018-10-27 17:30:23 +01:00
|
|
|
auto& PC() { return m_pc; }
|
2017-06-04 21:38:34 +01:00
|
|
|
|
2018-10-27 17:30:23 +01:00
|
|
|
auto& RESET() { return m_resetLine; }
|
|
|
|
auto& HALT() { return m_haltLine; }
|
|
|
|
auto& INT() { return m_intLine; }
|
|
|
|
auto& IRQ() { return INT(); } // Synonym
|
2018-06-10 00:40:56 +01:00
|
|
|
|
2018-08-25 01:34:30 +01:00
|
|
|
virtual void powerOn();
|
|
|
|
void reset() { lower(RESET()); }
|
2017-06-04 21:38:34 +01:00
|
|
|
|
2017-11-11 15:50:55 +00:00
|
|
|
int run(int limit);
|
2017-08-30 23:17:34 +01:00
|
|
|
virtual int step() = 0;
|
2017-07-25 18:56:43 +01:00
|
|
|
virtual int execute(uint8_t opcode) = 0;
|
|
|
|
|
2018-10-27 17:30:23 +01:00
|
|
|
auto cycles() const { return m_cycles; }
|
2018-08-25 12:09:26 +01:00
|
|
|
|
2018-08-27 11:27:33 +01:00
|
|
|
virtual register16_t peekWord(register16_t address) = 0;
|
2018-10-14 10:05:43 +01:00
|
|
|
virtual void pokeWord(register16_t address, register16_t value) = 0;
|
2018-08-27 11:27:33 +01:00
|
|
|
|
2017-06-04 21:38:34 +01:00
|
|
|
protected:
|
2017-09-06 13:22:23 +01:00
|
|
|
Processor(Bus& memory);
|
2017-11-30 23:15:40 +00:00
|
|
|
virtual ~Processor() = default;
|
2017-06-11 09:45:34 +01:00
|
|
|
|
2018-10-27 17:30:23 +01:00
|
|
|
auto& BUS() { return m_bus; }
|
2018-10-20 20:52:41 +01:00
|
|
|
|
2018-10-27 17:30:23 +01:00
|
|
|
auto halted() { return lowered(HALT()); }
|
|
|
|
auto halt() { --PC(); lower(HALT()); }
|
|
|
|
auto proceed() { ++PC(); raise(HALT()); }
|
2017-12-10 21:41:48 +00:00
|
|
|
|
2018-08-25 01:34:30 +01:00
|
|
|
virtual void handleRESET();
|
|
|
|
virtual void handleINT();
|
2018-08-25 12:09:26 +01:00
|
|
|
virtual void handleIRQ();
|
2018-08-25 01:34:30 +01:00
|
|
|
|
2018-10-27 17:30:23 +01:00
|
|
|
auto getBytePaged(const uint8_t page, const uint8_t offset) {
|
2018-08-17 21:53:49 +01:00
|
|
|
return BUS().read(register16_t(offset, page));
|
|
|
|
}
|
|
|
|
|
2018-09-21 00:16:00 +01:00
|
|
|
void setBytePaged(const uint8_t page, const uint8_t offset, const uint8_t value) {
|
2018-08-17 21:53:49 +01:00
|
|
|
BUS().write(register16_t(offset, page), value);
|
|
|
|
}
|
|
|
|
|
2018-10-27 17:30:23 +01:00
|
|
|
auto fetchByte() {
|
2018-08-11 21:19:19 +01:00
|
|
|
return BUS().read(PC()++);
|
2018-01-06 17:13:02 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 21:53:49 +01:00
|
|
|
virtual register16_t getWord() = 0;
|
|
|
|
virtual void setWord(register16_t value) = 0;
|
|
|
|
|
|
|
|
virtual register16_t getWordPaged(uint8_t page, uint8_t offset) = 0;
|
|
|
|
virtual void setWordPaged(uint8_t page, uint8_t offset, register16_t value) = 0;
|
|
|
|
|
|
|
|
virtual register16_t fetchWord() = 0;
|
2017-08-28 21:18:08 +01:00
|
|
|
|
2017-08-28 13:19:17 +01:00
|
|
|
virtual void push(uint8_t value) = 0;
|
|
|
|
virtual uint8_t pop() = 0;
|
|
|
|
|
2018-08-17 21:53:49 +01:00
|
|
|
virtual void pushWord(const register16_t value) = 0;
|
|
|
|
virtual register16_t popWord() = 0;
|
2017-08-28 13:19:17 +01:00
|
|
|
|
2018-10-27 17:30:23 +01:00
|
|
|
auto getWord(const register16_t address) {
|
2018-08-21 18:10:38 +01:00
|
|
|
BUS().ADDRESS() = address;
|
|
|
|
return getWord();
|
|
|
|
}
|
|
|
|
|
2018-09-21 00:16:00 +01:00
|
|
|
void setWord(const register16_t address, const register16_t value) {
|
2018-08-21 18:10:38 +01:00
|
|
|
BUS().ADDRESS() = address;
|
|
|
|
return setWord(value);
|
|
|
|
}
|
|
|
|
|
2018-04-14 09:39:06 +01:00
|
|
|
void jump(const register16_t destination) {
|
2018-03-18 22:40:23 +00:00
|
|
|
PC() = destination;
|
2017-08-28 21:18:08 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 09:39:06 +01:00
|
|
|
void call(const register16_t destination) {
|
2017-08-28 21:18:08 +01:00
|
|
|
pushWord(PC());
|
2018-03-18 22:40:23 +00:00
|
|
|
jump(destination);
|
2017-08-28 21:18:08 +01:00
|
|
|
}
|
|
|
|
|
2018-08-17 13:59:59 +01:00
|
|
|
virtual void ret() {
|
2018-03-18 22:40:23 +00:00
|
|
|
jump(popWord());
|
2017-08-28 21:18:08 +01:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:05:01 +00:00
|
|
|
void resetCycles() { m_cycles = 0; }
|
2018-04-14 09:39:06 +01:00
|
|
|
void addCycles(const int extra) { m_cycles += extra; }
|
2017-11-03 22:05:01 +00:00
|
|
|
void addCycle() { ++m_cycles; }
|
|
|
|
|
2017-06-19 13:53:00 +01:00
|
|
|
private:
|
2017-11-03 22:05:01 +00:00
|
|
|
Bus& m_bus;
|
2017-11-10 22:41:50 +00:00
|
|
|
int m_cycles = 0;
|
2018-06-16 00:55:32 +01:00
|
|
|
register16_t m_pc;
|
2017-12-10 21:41:48 +00:00
|
|
|
|
|
|
|
PinLevel m_intLine = Low;
|
|
|
|
PinLevel m_haltLine = Low;
|
|
|
|
PinLevel m_resetLine = Low;
|
2017-06-04 21:38:34 +01:00
|
|
|
};
|
2017-11-30 23:19:17 +00:00
|
|
|
}
|