2017-06-11 08:45:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-06-19 17:08:13 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <array>
|
|
|
|
|
2017-06-11 08:45:34 +00:00
|
|
|
#include "Processor.h"
|
|
|
|
|
|
|
|
namespace EightBit {
|
|
|
|
class IntelProcessor : public Processor
|
|
|
|
{
|
|
|
|
public:
|
2017-07-21 12:33:17 +00:00
|
|
|
struct opcode_decoded_t {
|
|
|
|
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int z;
|
|
|
|
int p;
|
|
|
|
int q;
|
|
|
|
|
|
|
|
opcode_decoded_t() {
|
|
|
|
x = y = z = p = q = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
opcode_decoded_t(uint8_t opcode) {
|
|
|
|
x = (opcode & 0b11000000) >> 6; // 0 - 3
|
|
|
|
y = (opcode & 0b00111000) >> 3; // 0 - 7
|
|
|
|
z = (opcode & 0b00000111); // 0 - 7
|
|
|
|
p = (y & 0b110) >> 1; // 0 - 3
|
|
|
|
q = (y & 1); // 0 - 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const opcode_decoded_t& getDecodedOpcode(const int i) const {
|
|
|
|
return m_decodedOpcodes[i];
|
|
|
|
}
|
|
|
|
|
2017-06-11 08:45:34 +00:00
|
|
|
register16_t& MEMPTR() { return m_memptr; }
|
|
|
|
|
|
|
|
virtual void initialise();
|
|
|
|
|
2017-07-07 08:27:06 +00:00
|
|
|
register16_t& SP() { return sp; }
|
|
|
|
|
2017-06-16 12:52:10 +00:00
|
|
|
virtual register16_t& AF() = 0;
|
|
|
|
uint8_t& A() { return AF().high; }
|
|
|
|
uint8_t& F() { return AF().low; }
|
|
|
|
|
|
|
|
virtual register16_t& BC() = 0;
|
|
|
|
uint8_t& B() { return BC().high; }
|
|
|
|
uint8_t& C() { return BC().low; }
|
|
|
|
|
|
|
|
virtual register16_t& DE() = 0;
|
|
|
|
uint8_t& D() { return DE().high; }
|
|
|
|
uint8_t& E() { return DE().low; }
|
|
|
|
|
|
|
|
virtual register16_t& HL() = 0;
|
|
|
|
uint8_t& H() { return HL().high; }
|
|
|
|
uint8_t& L() { return HL().low; }
|
|
|
|
|
2017-06-11 08:45:34 +00:00
|
|
|
protected:
|
|
|
|
IntelProcessor(Memory& memory);
|
|
|
|
|
2017-06-22 15:57:38 +00:00
|
|
|
template<class T> static void adjustSign(uint8_t& f, uint8_t value) {
|
|
|
|
setFlag(f, T::SF, value & T::SF);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T> static void adjustZero(uint8_t& f, uint8_t value) {
|
|
|
|
clearFlag(f, T::ZF, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T> static void adjustParity(uint8_t& f, uint8_t value) {
|
2017-07-02 16:38:19 +00:00
|
|
|
static const int lookup[0x10] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
|
2017-06-22 15:57:38 +00:00
|
|
|
auto set = lookup[highNibble(value)] + lookup[lowNibble(value)];
|
|
|
|
clearFlag(f, T::PF, set % 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T> static void adjustSZ(uint8_t& f, uint8_t value) {
|
|
|
|
adjustSign<T>(f, value);
|
|
|
|
adjustZero<T>(f, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T> static void adjustSZP(uint8_t& f, uint8_t value) {
|
|
|
|
adjustSZ<T>(f, value);
|
|
|
|
adjustParity<T>(f, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T> static void adjustXY(uint8_t& f, uint8_t value) {
|
|
|
|
setFlag(f, T::XF, value & T::XF);
|
|
|
|
setFlag(f, T::YF, value & T::YF);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T> static void adjustSZPXY(uint8_t& f, uint8_t value) {
|
|
|
|
adjustSZP<T>(f, value);
|
|
|
|
adjustXY<T>(f, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T> static void adjustSZXY(uint8_t& f, uint8_t value) {
|
|
|
|
adjustSZ<T>(f, value);
|
|
|
|
adjustXY<T>(f, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2017-06-19 12:53:00 +00:00
|
|
|
static int buildHalfCarryIndex(uint8_t before, uint8_t value, int calculation) {
|
2017-07-02 16:38:19 +00:00
|
|
|
__assume(calculation < 0x1ffff);
|
2017-06-11 08:45:34 +00:00
|
|
|
return ((before & 0x88) >> 1) | ((value & 0x88) >> 2) | ((calculation & 0x88) >> 3);
|
|
|
|
}
|
|
|
|
|
2017-06-19 12:53:00 +00:00
|
|
|
static bool calculateHalfCarryAdd(uint8_t before, uint8_t value, int calculation) {
|
2017-07-02 16:38:19 +00:00
|
|
|
__assume(calculation < 0x1ffff);
|
2017-06-19 12:53:00 +00:00
|
|
|
static std::array<bool, 8> m_halfCarryTableAdd = { { false, false, true, false, true, false, true, true } };
|
2017-06-11 08:45:34 +00:00
|
|
|
auto index = buildHalfCarryIndex(before, value, calculation);
|
2017-06-13 22:43:21 +00:00
|
|
|
return m_halfCarryTableAdd[index & Mask3];
|
2017-06-11 08:45:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 12:53:00 +00:00
|
|
|
static bool calculateHalfCarrySub(uint8_t before, uint8_t value, int calculation) {
|
2017-07-02 16:38:19 +00:00
|
|
|
__assume(calculation < 0x1ffff);
|
2017-06-19 12:53:00 +00:00
|
|
|
std::array<bool, 8> m_halfCarryTableSub = { { false, true, true, true, false, false, false, true } };
|
2017-06-11 08:45:34 +00:00
|
|
|
auto index = buildHalfCarryIndex(before, value, calculation);
|
2017-06-13 22:43:21 +00:00
|
|
|
return m_halfCarryTableSub[index & Mask3];
|
2017-06-11 08:45:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 23:13:41 +00:00
|
|
|
virtual uint8_t fetchByte() {
|
2017-07-07 08:27:06 +00:00
|
|
|
m_memory.ADDRESS().word = PC().word++;
|
|
|
|
return m_memory.reference();
|
|
|
|
}
|
|
|
|
|
|
|
|
void fetchWord(register16_t& output) {
|
|
|
|
output.low = fetchByte();
|
|
|
|
output.high = fetchByte();
|
|
|
|
}
|
|
|
|
|
2017-07-17 23:13:41 +00:00
|
|
|
virtual void push(uint8_t value) {
|
2017-06-19 12:53:00 +00:00
|
|
|
m_memory.ADDRESS().word = --SP().word;
|
2017-06-18 17:14:39 +00:00
|
|
|
m_memory.reference() = value;
|
|
|
|
}
|
|
|
|
|
2017-06-26 22:22:32 +00:00
|
|
|
void pushWord(const register16_t& value) {
|
2017-06-18 17:14:39 +00:00
|
|
|
push(value.high);
|
|
|
|
push(value.low);
|
|
|
|
}
|
2017-06-11 08:45:34 +00:00
|
|
|
|
2017-07-17 23:13:41 +00:00
|
|
|
virtual uint8_t pop() {
|
2017-06-19 12:53:00 +00:00
|
|
|
m_memory.ADDRESS().word = SP().word++;
|
2017-06-18 17:14:39 +00:00
|
|
|
return m_memory.reference();
|
|
|
|
}
|
|
|
|
|
|
|
|
void popWord(register16_t& output) {
|
|
|
|
output.low = pop();
|
|
|
|
output.high = pop();
|
|
|
|
}
|
2017-06-11 08:45:34 +00:00
|
|
|
|
|
|
|
void fetchWord() {
|
2017-07-07 08:27:06 +00:00
|
|
|
fetchWord(MEMPTR());
|
2017-06-11 08:45:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 20:08:40 +00:00
|
|
|
//
|
|
|
|
|
2017-06-15 21:21:26 +00:00
|
|
|
uint8_t& memptrReference() {
|
2017-06-12 13:33:00 +00:00
|
|
|
m_memory.ADDRESS() = MEMPTR();
|
|
|
|
MEMPTR().word++;
|
|
|
|
return m_memory.reference();
|
|
|
|
}
|
|
|
|
|
2017-07-17 23:13:41 +00:00
|
|
|
virtual void getWordViaMemptr(register16_t& value) {
|
2017-06-15 21:21:26 +00:00
|
|
|
value.low = memptrReference();
|
2017-06-12 13:33:00 +00:00
|
|
|
m_memory.ADDRESS().word++;
|
|
|
|
value.high = m_memory.reference();
|
|
|
|
}
|
|
|
|
|
2017-07-17 23:13:41 +00:00
|
|
|
virtual void setWordViaMemptr(register16_t value) {
|
2017-06-15 21:21:26 +00:00
|
|
|
memptrReference() = value.low;
|
2017-06-12 13:33:00 +00:00
|
|
|
m_memory.ADDRESS().word++;
|
|
|
|
m_memory.reference() = value.high;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2017-06-11 20:08:40 +00:00
|
|
|
void jump() {
|
2017-06-19 12:53:00 +00:00
|
|
|
PC() = MEMPTR();
|
2017-06-11 08:45:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 20:08:40 +00:00
|
|
|
void call() {
|
2017-06-19 12:53:00 +00:00
|
|
|
pushWord(PC());
|
2017-06-11 20:08:40 +00:00
|
|
|
jump();
|
|
|
|
}
|
|
|
|
|
|
|
|
void restart(uint8_t address) {
|
|
|
|
MEMPTR().low = address;
|
|
|
|
MEMPTR().high = 0;
|
|
|
|
call();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool callConditional(int condition) {
|
|
|
|
fetchWord();
|
|
|
|
if (condition)
|
|
|
|
call();
|
|
|
|
return condition != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool jumpConditional(int conditional) {
|
|
|
|
fetchWord();
|
|
|
|
if (conditional)
|
|
|
|
jump();
|
|
|
|
return conditional != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ret() {
|
|
|
|
popWord(MEMPTR());
|
|
|
|
jump();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool returnConditional(int condition) {
|
|
|
|
if (condition)
|
|
|
|
ret();
|
|
|
|
return condition != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void jr(int8_t offset) {
|
2017-06-19 12:53:00 +00:00
|
|
|
MEMPTR().word = PC().word + offset;
|
2017-06-11 20:08:40 +00:00
|
|
|
jump();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool jrConditional(int conditional) {
|
|
|
|
auto offset = fetchByte();
|
|
|
|
if (conditional)
|
|
|
|
jr(offset);
|
|
|
|
return conditional != 0;
|
|
|
|
}
|
|
|
|
|
2017-06-11 08:45:34 +00:00
|
|
|
private:
|
2017-07-21 12:33:17 +00:00
|
|
|
std::array<opcode_decoded_t, 0x100> m_decodedOpcodes;
|
|
|
|
|
2017-06-11 08:45:34 +00:00
|
|
|
register16_t m_memptr;
|
2017-07-07 08:27:06 +00:00
|
|
|
register16_t sp;
|
2017-06-11 08:45:34 +00:00
|
|
|
};
|
|
|
|
}
|