2017-06-04 20:38:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include "Memory.h"
|
|
|
|
|
|
|
|
namespace EightBit {
|
|
|
|
class Processor {
|
|
|
|
public:
|
|
|
|
enum Masks {
|
|
|
|
Mask1 = 0x01,
|
|
|
|
Mask2 = 0x03,
|
|
|
|
Mask3 = 0x07,
|
|
|
|
Mask4 = 0x0f,
|
|
|
|
Mask5 = 0x1f,
|
|
|
|
Mask6 = 0x3f,
|
|
|
|
Mask7 = 0x7f,
|
|
|
|
Mask8 = 0xff,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Bits {
|
|
|
|
Bit16 = 0x10000,
|
|
|
|
Bit15 = 0x8000,
|
|
|
|
Bit14 = 0x4000,
|
|
|
|
Bit13 = 0x2000,
|
|
|
|
Bit12 = 0x1000,
|
|
|
|
Bit11 = 0x800,
|
|
|
|
Bit10 = 0x400,
|
|
|
|
Bit9 = 0x200,
|
|
|
|
Bit8 = 0x100,
|
|
|
|
Bit7 = 0x80,
|
|
|
|
Bit6 = 0x40,
|
|
|
|
Bit5 = 0x20,
|
|
|
|
Bit4 = 0x10,
|
|
|
|
Bit3 = 0x8,
|
|
|
|
Bit2 = 0x4,
|
|
|
|
Bit1 = 0x2,
|
|
|
|
Bit0 = 0x1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint8_t highNibble(uint8_t value) { return value >> 4; }
|
|
|
|
static uint8_t lowNibble(uint8_t value) { return value & Mask4; }
|
|
|
|
|
|
|
|
static uint8_t promoteNibble(uint8_t value) { return value << 4; }
|
|
|
|
static uint8_t demoteNibble(uint8_t value) { return highNibble(value); }
|
|
|
|
|
|
|
|
const Memory& getMemory() const { return m_memory; }
|
|
|
|
|
2017-06-19 12:53:00 +00:00
|
|
|
register16_t& PC() { return pc; }
|
|
|
|
register16_t& SP() { return sp; }
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
|
|
bool isHalted() const { return m_halted; }
|
2017-06-19 12:53:00 +00:00
|
|
|
void halt() { --PC().word; m_halted = true; }
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
|
|
virtual void initialise();
|
|
|
|
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
protected:
|
2017-06-11 08:45:34 +00:00
|
|
|
Processor(Memory& memory);
|
|
|
|
|
2017-06-04 20:38:34 +00:00
|
|
|
Memory& m_memory;
|
|
|
|
int cycles;
|
|
|
|
|
|
|
|
uint8_t fetchByte() {
|
2017-06-19 12:53:00 +00:00
|
|
|
m_memory.ADDRESS().word = PC().word++;
|
2017-06-05 21:39:15 +00:00
|
|
|
return m_memory.reference();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:54:55 +00:00
|
|
|
void fetchWord(register16_t& output) {
|
|
|
|
output.low = fetchByte();
|
|
|
|
output.high = fetchByte();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-06-19 12:53:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
register16_t pc;
|
|
|
|
register16_t sp;
|
|
|
|
bool m_halted;
|
2017-06-04 20:38:34 +00:00
|
|
|
};
|
|
|
|
}
|