2017-06-04 20:38:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
2017-09-06 12:22:23 +00:00
|
|
|
#include "Bus.h"
|
|
|
|
#include "Register.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 {
|
|
|
|
class Processor {
|
|
|
|
public:
|
2017-08-28 20:18:08 +00:00
|
|
|
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,
|
2017-11-30 23:15:40 +00:00
|
|
|
Bit16 = Bit15 << 1
|
2017-06-04 20:38:34 +00:00
|
|
|
};
|
|
|
|
|
2017-08-28 20:18:08 +00:00
|
|
|
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,
|
2017-08-28 22:04:25 +00:00
|
|
|
Mask9 = Bit9 - 1,
|
|
|
|
Mask10 = Bit10 - 1,
|
|
|
|
Mask11 = Bit11 - 1,
|
|
|
|
Mask12 = Bit12 - 1,
|
|
|
|
Mask13 = Bit13 - 1,
|
|
|
|
Mask14 = Bit14 - 1,
|
|
|
|
Mask15 = Bit15 - 1,
|
2017-11-30 23:15:40 +00:00
|
|
|
Mask16 = Bit16 - 1
|
2017-06-04 20:38:34 +00:00
|
|
|
};
|
|
|
|
|
2017-12-10 21:41:48 +00:00
|
|
|
enum PinLevel {
|
|
|
|
Low, High
|
|
|
|
};
|
|
|
|
|
2018-04-14 08:39:06 +00:00
|
|
|
static bool raised(const PinLevel line) { return line == High; }
|
2017-12-10 21:41:48 +00:00
|
|
|
static void raise(PinLevel& line) { line = High; }
|
2018-04-14 08:39:06 +00:00
|
|
|
static bool lowered(const PinLevel line) { return line == Low; }
|
2017-12-10 21:41:48 +00:00
|
|
|
static void lower(PinLevel& line) { line = Low; }
|
|
|
|
|
2018-04-14 08:39:06 +00:00
|
|
|
static int highNibble(const int value) { return value >> 4; }
|
|
|
|
static int lowNibble(const int value) { return value & Mask4; }
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-05-25 21:36:10 +00:00
|
|
|
static int higherNibble(const int value) { return value & 0xf0; }
|
|
|
|
static int lowerNibble(const int value) { return lowNibble(value); }
|
|
|
|
|
2018-04-14 08:39:06 +00:00
|
|
|
static int promoteNibble(const int value) { return value << 4; }
|
|
|
|
static int demoteNibble(const int value) { return highNibble(value); }
|
2017-06-04 20:38:34 +00:00
|
|
|
|
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
|
|
|
|
static int8_t signExtend(int b, uint8_t x);
|
|
|
|
|
2017-09-06 12:22:23 +00:00
|
|
|
Bus& BUS() { return m_bus; }
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2017-11-03 22:05:01 +00:00
|
|
|
register16_t& PC() { return m_pc; }
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-09 23:40:56 +00:00
|
|
|
PinLevel& RESET() { return m_resetLine; }
|
|
|
|
PinLevel& HALT() { return m_haltLine; }
|
|
|
|
PinLevel& INT() { return m_intLine; }
|
2018-08-25 11:09:26 +00:00
|
|
|
PinLevel& IRQ() { return INT(); } // Synonym
|
2018-06-09 23:40:56 +00:00
|
|
|
PinLevel& POWER() { return m_powerLine; }
|
|
|
|
|
2018-06-24 21:33:02 +00:00
|
|
|
bool powered() { return raised(POWER()); }
|
2018-08-25 00:34:30 +00:00
|
|
|
virtual void powerOn();
|
2017-12-10 21:41:48 +00:00
|
|
|
void powerOff() { lower(POWER()); }
|
2018-08-25 00:34:30 +00:00
|
|
|
void reset() { 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;
|
2017-07-25 17:56:43 +00:00
|
|
|
virtual int execute(uint8_t opcode) = 0;
|
|
|
|
|
2018-08-25 11:09:26 +00:00
|
|
|
int cycles() const { return m_cycles; }
|
|
|
|
|
2018-08-27 10:27:33 +00:00
|
|
|
virtual register16_t peekWord(register16_t address) = 0;
|
|
|
|
|
2017-06-04 20:38:34 +00:00
|
|
|
protected:
|
2018-04-14 08:39:06 +00:00
|
|
|
static void clearFlag(uint8_t& f, const int flag) { f &= ~flag; }
|
|
|
|
static void setFlag(uint8_t& f, const int flag) { f |= flag; }
|
2017-07-11 20:34:01 +00:00
|
|
|
|
2018-08-17 12:59:59 +00:00
|
|
|
static void setFlag(uint8_t& f, const int flag, const int condition) { setFlag(f, flag, !!condition); }
|
|
|
|
static void setFlag(uint8_t& f, const int flag, const uint32_t condition) { setFlag(f, flag, !!condition); }
|
2018-04-14 08:39:06 +00:00
|
|
|
static void setFlag(uint8_t& f, const int flag, const bool condition) { condition ? setFlag(f, flag) : clearFlag(f, flag); }
|
2017-07-11 20:34:01 +00:00
|
|
|
|
2018-08-17 12:59:59 +00:00
|
|
|
static void clearFlag(uint8_t& f, const int flag, const int condition) { clearFlag(f, flag, !!condition); }
|
|
|
|
static void clearFlag(uint8_t& f, const int flag, const uint32_t condition) { clearFlag(f, flag, !!condition); }
|
2018-08-17 20:53:49 +00:00
|
|
|
static void clearFlag(uint8_t& f, const int flag, const bool condition) { setFlag(f, flag, !condition); }
|
2017-07-11 20:34:01 +00:00
|
|
|
|
2017-09-06 12:22:23 +00:00
|
|
|
Processor(Bus& memory);
|
2017-11-30 23:15:40 +00:00
|
|
|
virtual ~Processor() = default;
|
2017-06-11 08:45:34 +00:00
|
|
|
|
2018-06-24 21:33:02 +00:00
|
|
|
bool halted() { return lowered(HALT()); }
|
2018-08-11 20:19:19 +00:00
|
|
|
void halt() { --PC(); lower(HALT()); }
|
|
|
|
void proceed() { ++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-08-17 20:53:49 +00:00
|
|
|
uint8_t getBytePaged(uint8_t page, uint8_t offset) {
|
|
|
|
return BUS().read(register16_t(offset, page));
|
|
|
|
}
|
|
|
|
|
|
|
|
void setBytePaged(uint8_t page, uint8_t offset, uint8_t value) {
|
|
|
|
BUS().write(register16_t(offset, page), value);
|
|
|
|
}
|
|
|
|
|
2018-01-06 17:13:02 +00:00
|
|
|
uint8_t fetchByte() {
|
2018-08-11 20:19:19 +00:00
|
|
|
return BUS().read(PC()++);
|
2018-01-06 17:13:02 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 20:53:49 +00: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 20:18:08 +00:00
|
|
|
|
2017-08-28 12:19:17 +00:00
|
|
|
virtual void push(uint8_t value) = 0;
|
|
|
|
virtual uint8_t pop() = 0;
|
|
|
|
|
2018-08-17 20:53:49 +00:00
|
|
|
virtual void pushWord(const register16_t value) = 0;
|
|
|
|
virtual register16_t popWord() = 0;
|
2017-08-28 12:19:17 +00:00
|
|
|
|
2018-08-21 17:10:38 +00:00
|
|
|
register16_t getWord(register16_t address) {
|
|
|
|
BUS().ADDRESS() = address;
|
|
|
|
return getWord();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setWord(register16_t address, register16_t value) {
|
|
|
|
BUS().ADDRESS() = address;
|
|
|
|
return setWord(value);
|
|
|
|
}
|
|
|
|
|
2018-04-14 08:39:06 +00:00
|
|
|
void jump(const register16_t destination) {
|
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-08-17 12:59:59 +00:00
|
|
|
virtual void ret() {
|
2018-03-18 22:40:23 +00:00
|
|
|
jump(popWord());
|
2017-08-28 20:18:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:05:01 +00:00
|
|
|
void resetCycles() { m_cycles = 0; }
|
2018-04-14 08:39:06 +00:00
|
|
|
void addCycles(const int extra) { m_cycles += extra; }
|
2017-11-03 22:05:01 +00:00
|
|
|
void addCycle() { ++m_cycles; }
|
|
|
|
|
2017-06-19 12:53:00 +00: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-15 23:55:32 +00: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;
|
|
|
|
PinLevel m_powerLine = Low;
|
2017-06-04 20:38:34 +00:00
|
|
|
};
|
2017-11-30 23:19:17 +00:00
|
|
|
}
|