2017-06-11 09:45:34 +01:00
|
|
|
#pragma once
|
|
|
|
|
2017-06-19 18:08:13 +01:00
|
|
|
#include <cstdint>
|
|
|
|
#include <array>
|
|
|
|
|
2017-11-05 12:47:42 +00:00
|
|
|
#include "Bus.h"
|
2017-06-11 09:45:34 +01:00
|
|
|
#include "Processor.h"
|
2017-10-22 21:24:28 +01:00
|
|
|
#include "Register.h"
|
2017-06-11 09:45:34 +01:00
|
|
|
|
2017-11-20 19:17:49 +00:00
|
|
|
#include "EightBitCompilerDefinitions.h"
|
2017-11-04 23:15:55 +00:00
|
|
|
|
2017-06-11 09:45:34 +01:00
|
|
|
namespace EightBit {
|
|
|
|
class IntelProcessor : public Processor
|
|
|
|
{
|
|
|
|
public:
|
2017-07-21 13:33:17 +01:00
|
|
|
struct opcode_decoded_t {
|
|
|
|
|
2017-12-10 21:41:48 +00:00
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
int z = 0;
|
|
|
|
int p = 0;
|
|
|
|
int q = 0;
|
|
|
|
|
|
|
|
opcode_decoded_t() {}
|
2017-07-21 13:33:17 +01:00
|
|
|
|
|
|
|
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-11-03 22:05:01 +00:00
|
|
|
register16_t& SP() { return m_sp; }
|
2017-07-07 09:27:06 +01:00
|
|
|
|
2017-06-16 13:52:10 +01: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 09:45:34 +01:00
|
|
|
protected:
|
2017-09-06 13:22:23 +01:00
|
|
|
IntelProcessor(Bus& bus);
|
2017-11-30 23:15:40 +00:00
|
|
|
virtual ~IntelProcessor() = default;
|
2017-06-11 09:45:34 +01:00
|
|
|
|
2017-12-10 21:41:48 +00:00
|
|
|
virtual void reset() override;
|
|
|
|
|
2017-06-22 16:57:38 +01: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-12-12 23:12:45 +00:00
|
|
|
clearFlag(f, T::PF, PARITY(value));
|
2017-06-22 16:57:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 13:53:00 +01:00
|
|
|
static int buildHalfCarryIndex(uint8_t before, uint8_t value, int calculation) {
|
2017-06-11 09:45:34 +01:00
|
|
|
return ((before & 0x88) >> 1) | ((value & 0x88) >> 2) | ((calculation & 0x88) >> 3);
|
|
|
|
}
|
|
|
|
|
2017-06-19 13:53:00 +01:00
|
|
|
static bool calculateHalfCarryAdd(uint8_t before, uint8_t value, int calculation) {
|
|
|
|
static std::array<bool, 8> m_halfCarryTableAdd = { { false, false, true, false, true, false, true, true } };
|
2017-10-24 00:04:13 +01:00
|
|
|
const auto index = buildHalfCarryIndex(before, value, calculation);
|
2017-06-13 23:43:21 +01:00
|
|
|
return m_halfCarryTableAdd[index & Mask3];
|
2017-06-11 09:45:34 +01:00
|
|
|
}
|
|
|
|
|
2017-06-19 13:53:00 +01:00
|
|
|
static bool calculateHalfCarrySub(uint8_t before, uint8_t value, int calculation) {
|
|
|
|
std::array<bool, 8> m_halfCarryTableSub = { { false, true, true, true, false, false, false, true } };
|
2017-10-24 00:04:13 +01:00
|
|
|
const auto index = buildHalfCarryIndex(before, value, calculation);
|
2017-06-13 23:43:21 +01:00
|
|
|
return m_halfCarryTableSub[index & Mask3];
|
2017-06-11 09:45:34 +01:00
|
|
|
}
|
|
|
|
|
2017-11-11 15:13:26 +00:00
|
|
|
virtual void push(uint8_t value) final;
|
|
|
|
virtual uint8_t pop() final;
|
2017-06-18 18:14:39 +01:00
|
|
|
|
2017-06-11 21:08:40 +01:00
|
|
|
//
|
|
|
|
|
2018-01-10 23:08:14 +00:00
|
|
|
void getWord(register16_t& value);
|
|
|
|
void setWord(register16_t value);
|
2017-06-12 14:33:00 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
|
2017-06-11 21:08:40 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool returnConditional(int condition) {
|
|
|
|
if (condition)
|
|
|
|
ret();
|
|
|
|
return condition != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void jr(int8_t offset) {
|
2017-06-19 13:53:00 +01:00
|
|
|
MEMPTR().word = PC().word + offset;
|
2017-06-11 21:08:40 +01:00
|
|
|
jump();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool jrConditional(int conditional) {
|
2017-10-24 00:04:13 +01:00
|
|
|
const auto offset = fetchByte();
|
2017-06-11 21:08:40 +01:00
|
|
|
if (conditional)
|
|
|
|
jr(offset);
|
|
|
|
return conditional != 0;
|
|
|
|
}
|
|
|
|
|
2017-06-11 09:45:34 +01:00
|
|
|
private:
|
2017-07-21 13:33:17 +01:00
|
|
|
std::array<opcode_decoded_t, 0x100> m_decodedOpcodes;
|
2017-11-30 14:37:18 +00:00
|
|
|
register16_t m_sp = { { 0xff, 0xff } };
|
2017-06-11 09:45:34 +01:00
|
|
|
};
|
2017-11-30 23:19:17 +00:00
|
|
|
}
|