2017-05-14 21:46:41 +00:00
|
|
|
|
//
|
|
|
|
|
// Z80.hpp
|
|
|
|
|
// Clock Signal
|
|
|
|
|
//
|
|
|
|
|
// Created by Thomas Harte on 14/05/2017.
|
|
|
|
|
// Copyright © 2017 Thomas Harte. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#ifndef Z80_hpp
|
|
|
|
|
#define Z80_hpp
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
2017-05-21 23:15:52 +00:00
|
|
|
|
#include <cstring>
|
2017-05-16 02:25:52 +00:00
|
|
|
|
#include <cstdio>
|
2017-05-30 01:44:33 +00:00
|
|
|
|
#include <vector>
|
2017-05-14 21:46:41 +00:00
|
|
|
|
|
2017-05-15 02:15:16 +00:00
|
|
|
|
#include "../RegisterSizes.hpp"
|
|
|
|
|
|
|
|
|
|
namespace CPU {
|
2017-05-14 21:46:41 +00:00
|
|
|
|
namespace Z80 {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
The list of registers that can be accessed via @c set_value_of_register and @c set_value_of_register.
|
|
|
|
|
*/
|
|
|
|
|
enum Register {
|
|
|
|
|
ProgramCounter,
|
|
|
|
|
StackPointer,
|
|
|
|
|
|
|
|
|
|
A, Flags, AF,
|
|
|
|
|
B, C, BC,
|
|
|
|
|
D, E, DE,
|
|
|
|
|
H, L, HL,
|
|
|
|
|
|
|
|
|
|
ADash, FlagsDash, AFDash,
|
|
|
|
|
BDash, CDash, BCDash,
|
|
|
|
|
DDash, EDash, DEDash,
|
|
|
|
|
HDash, LDash, HLDash,
|
|
|
|
|
|
|
|
|
|
IXh, IXl, IX,
|
|
|
|
|
IYh, IYl, IY,
|
2017-06-04 22:32:23 +00:00
|
|
|
|
R, I, Refresh,
|
2017-05-22 23:14:46 +00:00
|
|
|
|
|
|
|
|
|
IFF1, IFF2, IM
|
2017-05-14 21:46:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Flags as defined on the Z80; can be used to decode the result of @c get_flags or to form a value for @c set_flags.
|
|
|
|
|
*/
|
|
|
|
|
enum Flag: uint8_t {
|
|
|
|
|
Sign = 0x80,
|
|
|
|
|
Zero = 0x40,
|
|
|
|
|
Bit5 = 0x20,
|
|
|
|
|
HalfCarry = 0x10,
|
|
|
|
|
Bit3 = 0x08,
|
|
|
|
|
Parity = 0x04,
|
|
|
|
|
Overflow = 0x04,
|
|
|
|
|
Subtract = 0x02,
|
|
|
|
|
Carry = 0x01
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Subclasses will be given the task of performing bus operations, allowing them to provide whatever interface they like
|
|
|
|
|
between a Z80 and the rest of the system. @c BusOperation lists the types of bus operation that may be requested.
|
|
|
|
|
*/
|
|
|
|
|
enum BusOperation {
|
2017-05-16 02:25:52 +00:00
|
|
|
|
ReadOpcode = 0,
|
2017-05-15 02:08:15 +00:00
|
|
|
|
Read, Write,
|
|
|
|
|
Input, Output,
|
|
|
|
|
Interrupt,
|
2017-06-03 21:07:05 +00:00
|
|
|
|
BusAcknowledge,
|
2017-05-19 02:53:43 +00:00
|
|
|
|
Internal
|
2017-05-15 02:08:15 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-16 02:25:52 +00:00
|
|
|
|
struct MachineCycle {
|
2017-05-21 23:15:52 +00:00
|
|
|
|
BusOperation operation;
|
|
|
|
|
int length;
|
|
|
|
|
uint16_t *address;
|
|
|
|
|
uint8_t *value;
|
2017-05-16 02:25:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-15 02:08:15 +00:00
|
|
|
|
/*!
|
2017-05-30 23:24:58 +00:00
|
|
|
|
@abstact An abstract base class for emulation of a Z80 processor via the curiously recurring template pattern/f-bounded polymorphism.
|
2017-05-15 02:08:15 +00:00
|
|
|
|
|
2017-05-30 23:24:58 +00:00
|
|
|
|
@discussion Subclasses should implement @c perform_machine_cycle in
|
|
|
|
|
order to provide the bus on which the Z80 operates and @c flush(), which is called upon completion of a continuous run
|
2017-05-15 02:08:15 +00:00
|
|
|
|
of cycles to allow a subclass to bring any on-demand activities up to date.
|
|
|
|
|
*/
|
2017-06-03 23:17:34 +00:00
|
|
|
|
template <class T> class Processor {
|
2017-05-15 02:08:15 +00:00
|
|
|
|
private:
|
2017-05-15 11:55:53 +00:00
|
|
|
|
uint8_t a_, i_, r_;
|
|
|
|
|
RegisterPair bc_, de_, hl_;
|
|
|
|
|
RegisterPair afDash_, bcDash_, deDash_, hlDash_;
|
|
|
|
|
RegisterPair ix_, iy_, pc_, sp_;
|
2017-05-21 16:53:17 +00:00
|
|
|
|
bool iff1_, iff2_;
|
2017-05-22 23:14:46 +00:00
|
|
|
|
int interrupt_mode_;
|
2017-06-03 21:07:05 +00:00
|
|
|
|
uint16_t pc_increment_;
|
2017-05-30 02:23:19 +00:00
|
|
|
|
uint8_t sign_result_; // the sign flag is set if the value in sign_result_ is negative
|
|
|
|
|
uint8_t zero_result_; // the zero flag is set if the value in zero_result_ is zero
|
|
|
|
|
uint8_t half_carry_result_; // the half-carry flag is set if bit 4 of half_carry_result_ is set
|
|
|
|
|
uint8_t bit53_result_; // the bit 3 and 5 flags are set if the corresponding bits of bit53_result_ are set
|
|
|
|
|
uint8_t parity_overflow_result_; // the parity/overflow flag is set if the corresponding bit of parity_overflow_result_ is set
|
|
|
|
|
uint8_t subtract_flag_; // contains a copy of the subtract flag in isolation
|
|
|
|
|
uint8_t carry_result_; // the carry flag is set if bit 0 of carry_result_ is set
|
2017-05-31 02:15:43 +00:00
|
|
|
|
uint8_t halt_mask_;
|
2017-05-15 11:55:53 +00:00
|
|
|
|
|
2017-05-16 02:25:52 +00:00
|
|
|
|
int number_of_cycles_;
|
|
|
|
|
|
2017-06-02 00:34:52 +00:00
|
|
|
|
enum Interrupt: uint8_t {
|
|
|
|
|
IRQ = 0x01,
|
|
|
|
|
NMI = 0x02,
|
2017-06-03 23:09:47 +00:00
|
|
|
|
Reset = 0x04,
|
|
|
|
|
PowerOn = 0x08
|
2017-06-02 00:34:52 +00:00
|
|
|
|
};
|
|
|
|
|
uint8_t request_status_;
|
2017-06-02 01:40:08 +00:00
|
|
|
|
uint8_t last_request_status_;
|
2017-06-02 00:34:52 +00:00
|
|
|
|
bool irq_line_;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
bool bus_request_line_;
|
2017-06-02 00:34:52 +00:00
|
|
|
|
|
2017-05-16 02:25:52 +00:00
|
|
|
|
uint8_t operation_;
|
2017-06-04 19:07:07 +00:00
|
|
|
|
RegisterPair temp16_, memptr_;
|
2017-05-21 13:17:30 +00:00
|
|
|
|
uint8_t temp8_;
|
2017-05-16 02:25:52 +00:00
|
|
|
|
|
2017-06-03 23:17:34 +00:00
|
|
|
|
struct MicroOp {
|
|
|
|
|
enum Type {
|
|
|
|
|
BusOperation,
|
|
|
|
|
DecodeOperation,
|
|
|
|
|
DecodeOperationNoRChange,
|
|
|
|
|
MoveToNextProgram,
|
|
|
|
|
|
|
|
|
|
Increment8,
|
|
|
|
|
Increment16,
|
|
|
|
|
Decrement8,
|
|
|
|
|
Decrement16,
|
|
|
|
|
Move8,
|
|
|
|
|
Move16,
|
|
|
|
|
|
|
|
|
|
IncrementPC,
|
|
|
|
|
|
|
|
|
|
AssembleAF,
|
|
|
|
|
DisassembleAF,
|
|
|
|
|
|
|
|
|
|
And,
|
|
|
|
|
Or,
|
|
|
|
|
Xor,
|
|
|
|
|
|
|
|
|
|
TestNZ,
|
|
|
|
|
TestZ,
|
|
|
|
|
TestNC,
|
|
|
|
|
TestC,
|
|
|
|
|
TestPO,
|
|
|
|
|
TestPE,
|
|
|
|
|
TestP,
|
|
|
|
|
TestM,
|
|
|
|
|
|
|
|
|
|
ADD16, ADC16, SBC16,
|
|
|
|
|
CP8, SUB8, SBC8, ADD8, ADC8,
|
|
|
|
|
NEG,
|
|
|
|
|
|
|
|
|
|
ExDEHL, ExAFAFDash, EXX,
|
|
|
|
|
|
|
|
|
|
EI, DI, IM,
|
|
|
|
|
|
|
|
|
|
LDI, LDIR, LDD, LDDR,
|
|
|
|
|
CPI, CPIR, CPD, CPDR,
|
|
|
|
|
INI, INIR, IND, INDR,
|
|
|
|
|
OUTI, OUTD, OUT_R,
|
|
|
|
|
|
|
|
|
|
RLA, RLCA, RRA, RRCA,
|
|
|
|
|
RLC, RRC, RL, RR,
|
|
|
|
|
SLA, SRA, SLL, SRL,
|
|
|
|
|
RLD, RRD,
|
|
|
|
|
|
|
|
|
|
SetInstructionPage,
|
|
|
|
|
CalculateIndexAddress,
|
|
|
|
|
|
|
|
|
|
BeginNMI,
|
|
|
|
|
BeginIRQ,
|
|
|
|
|
BeginIRQMode0,
|
|
|
|
|
RETN,
|
|
|
|
|
JumpTo66,
|
|
|
|
|
HALT,
|
|
|
|
|
|
|
|
|
|
DJNZ,
|
|
|
|
|
DAA,
|
|
|
|
|
CPL,
|
|
|
|
|
SCF,
|
|
|
|
|
CCF,
|
|
|
|
|
|
|
|
|
|
RES,
|
|
|
|
|
BIT,
|
|
|
|
|
SET,
|
|
|
|
|
|
|
|
|
|
CalculateRSTDestination,
|
|
|
|
|
|
|
|
|
|
SetAFlags,
|
|
|
|
|
SetInFlags,
|
|
|
|
|
SetZero,
|
|
|
|
|
|
|
|
|
|
IndexedPlaceHolder,
|
|
|
|
|
|
|
|
|
|
Reset
|
|
|
|
|
};
|
|
|
|
|
Type type;
|
|
|
|
|
void *source;
|
|
|
|
|
void *destination;
|
|
|
|
|
MachineCycle machine_cycle;
|
|
|
|
|
};
|
|
|
|
|
const MicroOp *scheduled_program_counter_;
|
|
|
|
|
|
|
|
|
|
|
2017-05-21 23:15:52 +00:00
|
|
|
|
struct InstructionPage {
|
2017-05-30 01:44:33 +00:00
|
|
|
|
std::vector<MicroOp *> instructions;
|
|
|
|
|
std::vector<MicroOp> all_operations;
|
|
|
|
|
std::vector<MicroOp> fetch_decode_execute;
|
2017-06-01 22:28:04 +00:00
|
|
|
|
MicroOp *fetch_decode_execute_data;
|
2017-06-04 18:13:38 +00:00
|
|
|
|
uint8_t r_step;
|
|
|
|
|
bool is_indexed;
|
2017-05-30 02:23:19 +00:00
|
|
|
|
|
2017-06-04 18:13:38 +00:00
|
|
|
|
InstructionPage() : r_step(1), is_indexed(false) {}
|
2017-05-21 23:15:52 +00:00
|
|
|
|
};
|
2017-06-02 02:16:22 +00:00
|
|
|
|
std::vector<MicroOp> reset_program_;
|
|
|
|
|
std::vector<MicroOp> irq_program_[3];
|
|
|
|
|
std::vector<MicroOp> nmi_program_;
|
2017-05-28 01:06:56 +00:00
|
|
|
|
InstructionPage *current_instruction_page_;
|
|
|
|
|
|
2017-05-21 23:15:52 +00:00
|
|
|
|
InstructionPage base_page_;
|
|
|
|
|
InstructionPage ed_page_;
|
|
|
|
|
InstructionPage fd_page_;
|
2017-05-21 23:26:40 +00:00
|
|
|
|
InstructionPage dd_page_;
|
2017-05-20 21:04:25 +00:00
|
|
|
|
|
2017-05-27 19:39:22 +00:00
|
|
|
|
InstructionPage cb_page_;
|
|
|
|
|
InstructionPage fdcb_page_;
|
|
|
|
|
InstructionPage ddcb_page_;
|
|
|
|
|
|
2017-05-29 16:25:10 +00:00
|
|
|
|
#define NOP {MicroOp::MoveToNextProgram}
|
2017-05-19 23:18:35 +00:00
|
|
|
|
|
2017-05-27 03:23:33 +00:00
|
|
|
|
#define INDEX() {MicroOp::IndexedPlaceHolder}, FETCH(temp8_, pc_), WAIT(5), {MicroOp::CalculateIndexAddress, &index}
|
2017-05-28 01:06:56 +00:00
|
|
|
|
#define FINDEX() {MicroOp::IndexedPlaceHolder}, FETCH(temp8_, pc_), {MicroOp::CalculateIndexAddress, &index}
|
2017-06-04 19:07:07 +00:00
|
|
|
|
#define INDEX_ADDR() (add_offsets ? memptr_ : index)
|
2017-05-27 03:23:33 +00:00
|
|
|
|
|
2017-06-03 16:16:21 +00:00
|
|
|
|
#define INC16(r) {(&r == &pc_) ? MicroOp::IncrementPC : MicroOp::Increment16, &r.full}
|
|
|
|
|
|
2017-05-21 17:13:21 +00:00
|
|
|
|
/// Fetches into x from address y, and then increments y.
|
2017-06-03 16:16:21 +00:00
|
|
|
|
#define FETCH(x, y) {MicroOp::BusOperation, nullptr, nullptr, {Read, 3, &y.full, &x}}, INC16(y)
|
2017-05-21 17:13:21 +00:00
|
|
|
|
/// Fetches into x from address y.
|
2017-05-19 23:18:35 +00:00
|
|
|
|
#define FETCHL(x, y) {MicroOp::BusOperation, nullptr, nullptr, {Read, 3, &y.full, &x}}
|
|
|
|
|
|
2017-05-21 17:13:21 +00:00
|
|
|
|
/// Stores x to address y, and then increments y.
|
|
|
|
|
#define STORE(x, y) {MicroOp::BusOperation, nullptr, nullptr, {Write, 3, &y.full, &x}}, {MicroOp::Increment16, &y.full}
|
|
|
|
|
/// Stores x to address y.
|
2017-05-19 23:18:35 +00:00
|
|
|
|
#define STOREL(x, y) {MicroOp::BusOperation, nullptr, nullptr, {Write, 3, &y.full, &x}}
|
|
|
|
|
|
2017-05-21 17:13:21 +00:00
|
|
|
|
/// Fetches the 16-bit quantity x from address y, incrementing y twice.
|
2017-05-19 23:18:35 +00:00
|
|
|
|
#define FETCH16(x, y) FETCH(x.bytes.low, y), FETCH(x.bytes.high, y)
|
2017-05-21 17:13:21 +00:00
|
|
|
|
/// Fetches the 16-bit quantity x from address y, incrementing y once.
|
2017-05-19 23:18:35 +00:00
|
|
|
|
#define FETCH16L(x, y) FETCH(x.bytes.low, y), FETCHL(x.bytes.high, y)
|
|
|
|
|
|
2017-05-21 17:13:21 +00:00
|
|
|
|
/// Stores the 16-bit quantity x to address y, incrementing y once.
|
2017-05-21 14:18:43 +00:00
|
|
|
|
#define STORE16L(x, y) STORE(x.bytes.low, y), STOREL(x.bytes.high, y)
|
|
|
|
|
|
2017-05-28 18:20:05 +00:00
|
|
|
|
/// Outputs the 8-bit value to the 16-bit port
|
|
|
|
|
#define OUT(port, value) {MicroOp::BusOperation, nullptr, nullptr, {Output, 4, &port.full, &value}}
|
|
|
|
|
|
|
|
|
|
/// Inputs the 8-bit value from the 16-bit port
|
|
|
|
|
#define IN(port, value) {MicroOp::BusOperation, nullptr, nullptr, {Input, 4, &port.full, &value}}
|
|
|
|
|
|
2017-05-20 01:59:45 +00:00
|
|
|
|
#define PUSH(x) {MicroOp::Decrement16, &sp_.full}, STOREL(x.bytes.high, sp_), {MicroOp::Decrement16, &sp_.full}, STOREL(x.bytes.low, sp_)
|
|
|
|
|
#define POP(x) FETCHL(x.bytes.low, sp_), {MicroOp::Increment16, &sp_.full}, FETCHL(x.bytes.high, sp_), {MicroOp::Increment16, &sp_.full}
|
2017-05-19 23:18:35 +00:00
|
|
|
|
|
2017-05-21 13:17:30 +00:00
|
|
|
|
#define JP(cc) Program(FETCH16(temp16_, pc_), {MicroOp::cc}, {MicroOp::Move16, &temp16_.full, &pc_.full})
|
2017-05-21 14:03:46 +00:00
|
|
|
|
#define CALL(cc) Program(FETCH16(temp16_, pc_), {MicroOp::cc}, WAIT(1), PUSH(pc_), {MicroOp::Move16, &temp16_.full, &pc_.full})
|
2017-06-04 19:39:37 +00:00
|
|
|
|
#define RET(cc) Program(WAIT(1), {MicroOp::cc}, POP(memptr_), {MicroOp::Move16, &memptr_.full, &pc_.full})
|
2017-06-04 19:07:07 +00:00
|
|
|
|
#define JR(cc) Program(FETCH(temp8_, pc_), {MicroOp::cc}, WAIT(5), {MicroOp::CalculateIndexAddress, &pc_.full}, {MicroOp::Move16, &memptr_.full, &pc_.full})
|
2017-06-04 19:39:37 +00:00
|
|
|
|
#define RST() Program(WAIT(1), {MicroOp::CalculateRSTDestination}, PUSH(pc_), {MicroOp::Move16, &memptr_.full, &pc_.full})
|
2017-05-20 02:57:43 +00:00
|
|
|
|
#define LD(a, b) Program({MicroOp::Move8, &b, &a})
|
|
|
|
|
|
2017-05-27 03:37:17 +00:00
|
|
|
|
#define LD_GROUP(r, ri) \
|
|
|
|
|
LD(r, bc_.bytes.high), LD(r, bc_.bytes.low), LD(r, de_.bytes.high), LD(r, de_.bytes.low), \
|
|
|
|
|
LD(r, index.bytes.high), LD(r, index.bytes.low), Program(INDEX(), FETCHL(ri, INDEX_ADDR())), LD(r, a_)
|
2017-05-20 02:57:43 +00:00
|
|
|
|
|
2017-05-27 19:54:24 +00:00
|
|
|
|
#define READ_OP_GROUP(op) \
|
2017-05-20 03:03:34 +00:00
|
|
|
|
Program({MicroOp::op, &bc_.bytes.high}), Program({MicroOp::op, &bc_.bytes.low}), \
|
|
|
|
|
Program({MicroOp::op, &de_.bytes.high}), Program({MicroOp::op, &de_.bytes.low}), \
|
2017-05-21 23:26:40 +00:00
|
|
|
|
Program({MicroOp::op, &index.bytes.high}), Program({MicroOp::op, &index.bytes.low}), \
|
2017-05-27 03:37:17 +00:00
|
|
|
|
Program(INDEX(), FETCHL(temp8_, INDEX_ADDR()), {MicroOp::op, &temp8_}), \
|
2017-05-20 03:03:34 +00:00
|
|
|
|
Program({MicroOp::op, &a_})
|
|
|
|
|
|
2017-06-13 02:22:00 +00:00
|
|
|
|
#define READ_OP_GROUP_D(op) \
|
|
|
|
|
Program({MicroOp::op, &bc_.bytes.high}), Program({MicroOp::op, &bc_.bytes.low}), \
|
|
|
|
|
Program({MicroOp::op, &de_.bytes.high}), Program({MicroOp::op, &de_.bytes.low}), \
|
|
|
|
|
Program({MicroOp::op, &index.bytes.high}), Program({MicroOp::op, &index.bytes.low}), \
|
|
|
|
|
Program(INDEX(), FETCHL(temp8_, INDEX_ADDR()), WAIT(1), {MicroOp::op, &temp8_}), \
|
|
|
|
|
Program({MicroOp::op, &a_})
|
|
|
|
|
|
2017-05-28 01:06:56 +00:00
|
|
|
|
#define RMW(x, op, ...) Program(INDEX(), FETCHL(x, INDEX_ADDR()), {MicroOp::op, &x}, WAIT(1), STOREL(x, INDEX_ADDR()))
|
2017-05-28 03:54:06 +00:00
|
|
|
|
#define RMWI(x, op, ...) Program(WAIT(2), FETCHL(x, INDEX_ADDR()), {MicroOp::op, &x}, WAIT(1), STOREL(x, INDEX_ADDR()))
|
2017-05-28 01:06:56 +00:00
|
|
|
|
|
2017-05-27 19:54:24 +00:00
|
|
|
|
#define MODIFY_OP_GROUP(op) \
|
|
|
|
|
Program({MicroOp::op, &bc_.bytes.high}), Program({MicroOp::op, &bc_.bytes.low}), \
|
|
|
|
|
Program({MicroOp::op, &de_.bytes.high}), Program({MicroOp::op, &de_.bytes.low}), \
|
|
|
|
|
Program({MicroOp::op, &index.bytes.high}), Program({MicroOp::op, &index.bytes.low}), \
|
2017-05-28 01:06:56 +00:00
|
|
|
|
RMW(temp8_, op), \
|
2017-05-27 19:54:24 +00:00
|
|
|
|
Program({MicroOp::op, &a_})
|
|
|
|
|
|
2017-05-28 03:54:06 +00:00
|
|
|
|
#define IX_MODIFY_OP_GROUP(op) \
|
2017-05-28 01:06:56 +00:00
|
|
|
|
RMWI(bc_.bytes.high, op), \
|
|
|
|
|
RMWI(bc_.bytes.low, op), \
|
|
|
|
|
RMWI(de_.bytes.high, op), \
|
|
|
|
|
RMWI(de_.bytes.low, op), \
|
|
|
|
|
RMWI(hl_.bytes.high, op), \
|
|
|
|
|
RMWI(hl_.bytes.low, op), \
|
|
|
|
|
RMWI(temp8_, op), \
|
|
|
|
|
RMWI(a_, op)
|
|
|
|
|
|
2017-05-28 03:54:06 +00:00
|
|
|
|
#define IX_READ_OP_GROUP(op) \
|
|
|
|
|
Program(WAIT(2), FETCHL(temp8_, INDEX_ADDR()), {MicroOp::op, &temp8_}, WAIT(1)), \
|
|
|
|
|
Program(WAIT(2), FETCHL(temp8_, INDEX_ADDR()), {MicroOp::op, &temp8_}, WAIT(1)), \
|
|
|
|
|
Program(WAIT(2), FETCHL(temp8_, INDEX_ADDR()), {MicroOp::op, &temp8_}, WAIT(1)), \
|
|
|
|
|
Program(WAIT(2), FETCHL(temp8_, INDEX_ADDR()), {MicroOp::op, &temp8_}, WAIT(1)), \
|
|
|
|
|
Program(WAIT(2), FETCHL(temp8_, INDEX_ADDR()), {MicroOp::op, &temp8_}, WAIT(1)), \
|
|
|
|
|
Program(WAIT(2), FETCHL(temp8_, INDEX_ADDR()), {MicroOp::op, &temp8_}, WAIT(1)), \
|
|
|
|
|
Program(WAIT(2), FETCHL(temp8_, INDEX_ADDR()), {MicroOp::op, &temp8_}, WAIT(1)), \
|
|
|
|
|
Program(WAIT(2), FETCHL(temp8_, INDEX_ADDR()), {MicroOp::op, &temp8_}, WAIT(1))
|
|
|
|
|
|
2017-05-21 13:43:17 +00:00
|
|
|
|
#define ADD16(d, s) Program(WAIT(4), WAIT(3), {MicroOp::ADD16, &s.full, &d.full})
|
|
|
|
|
#define ADC16(d, s) Program(WAIT(4), WAIT(3), {MicroOp::ADC16, &s.full, &d.full})
|
|
|
|
|
#define SBC16(d, s) Program(WAIT(4), WAIT(3), {MicroOp::SBC16, &s.full, &d.full})
|
2017-05-20 21:04:25 +00:00
|
|
|
|
|
2017-05-19 02:53:43 +00:00
|
|
|
|
#define WAIT(n) {MicroOp::BusOperation, nullptr, nullptr, {Internal, n} }
|
2017-05-18 02:42:30 +00:00
|
|
|
|
#define Program(...) { __VA_ARGS__, {MicroOp::MoveToNextProgram} }
|
2017-05-18 02:31:41 +00:00
|
|
|
|
|
2017-06-03 21:07:05 +00:00
|
|
|
|
#define isTerminal(n) (n == MicroOp::MoveToNextProgram || n == MicroOp::DecodeOperation || n == MicroOp::DecodeOperationNoRChange)
|
2017-06-03 16:19:25 +00:00
|
|
|
|
|
2017-05-20 21:04:25 +00:00
|
|
|
|
typedef MicroOp InstructionTable[256][20];
|
2017-05-19 23:18:35 +00:00
|
|
|
|
|
2017-05-27 03:23:33 +00:00
|
|
|
|
void assemble_page(InstructionPage &target, InstructionTable &table, bool add_offsets) {
|
2017-05-21 23:15:52 +00:00
|
|
|
|
size_t number_of_micro_ops = 0;
|
|
|
|
|
size_t lengths[256];
|
|
|
|
|
|
|
|
|
|
// Count number of micro-ops required.
|
|
|
|
|
for(int c = 0; c < 256; c++) {
|
|
|
|
|
size_t length = 0;
|
2017-06-03 16:19:25 +00:00
|
|
|
|
while(!isTerminal(table[c][length].type)) length++;
|
2017-05-21 23:15:52 +00:00
|
|
|
|
length++;
|
|
|
|
|
lengths[c] = length;
|
|
|
|
|
number_of_micro_ops += length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allocate a landing area.
|
2017-05-30 01:44:33 +00:00
|
|
|
|
target.all_operations.resize(number_of_micro_ops);
|
|
|
|
|
target.instructions.resize(256, nullptr);
|
2017-05-21 23:15:52 +00:00
|
|
|
|
|
|
|
|
|
// Copy in all programs and set pointers.
|
|
|
|
|
size_t destination = 0;
|
2017-06-04 21:55:19 +00:00
|
|
|
|
for(size_t c = 0; c < 256; c++) {
|
2017-05-21 23:15:52 +00:00
|
|
|
|
target.instructions[c] = &target.all_operations[destination];
|
2017-05-27 03:23:33 +00:00
|
|
|
|
for(int t = 0; t < lengths[c];) {
|
2017-05-29 15:40:56 +00:00
|
|
|
|
// Skip zero-length bus cycles.
|
|
|
|
|
if(table[c][t].type == MicroOp::BusOperation && table[c][t].machine_cycle.length == 0) {
|
|
|
|
|
t++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 03:23:33 +00:00
|
|
|
|
// If an index placeholder is hit then drop it, and if offsets aren't being added,
|
2017-05-28 01:06:56 +00:00
|
|
|
|
// then also drop the indexing that follows, which is assumed to be everything
|
|
|
|
|
// up to and including the next ::CalculateIndexAddress. Coupled to the INDEX() macro.
|
2017-05-27 03:23:33 +00:00
|
|
|
|
if(table[c][t].type == MicroOp::IndexedPlaceHolder) {
|
|
|
|
|
t++;
|
2017-05-28 01:06:56 +00:00
|
|
|
|
if(!add_offsets) {
|
|
|
|
|
while(table[c][t].type != MicroOp::CalculateIndexAddress) t++;
|
|
|
|
|
t++;
|
|
|
|
|
}
|
2017-05-27 03:23:33 +00:00
|
|
|
|
}
|
|
|
|
|
target.all_operations[destination] = table[c][t];
|
|
|
|
|
destination++;
|
|
|
|
|
t++;
|
|
|
|
|
}
|
2017-05-20 21:04:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 23:15:52 +00:00
|
|
|
|
void assemble_ed_page(InstructionPage &target) {
|
2017-05-28 18:50:51 +00:00
|
|
|
|
#define IN_C(r) Program(IN(bc_, r), {MicroOp::SetInFlags, &r})
|
|
|
|
|
#define OUT_C(r) Program(OUT(bc_, r))
|
|
|
|
|
#define IN_OUT(r) IN_C(r), OUT_C(r)
|
|
|
|
|
|
2017-05-29 16:25:10 +00:00
|
|
|
|
#define NOP_ROW() NOP, NOP, NOP, NOP, NOP, NOP, NOP, NOP, NOP, NOP, NOP, NOP, NOP, NOP, NOP, NOP
|
2017-05-21 23:15:52 +00:00
|
|
|
|
InstructionTable ed_program_table = {
|
2017-05-20 21:29:30 +00:00
|
|
|
|
NOP_ROW(), /* 0x00 */
|
|
|
|
|
NOP_ROW(), /* 0x10 */
|
|
|
|
|
NOP_ROW(), /* 0x20 */
|
|
|
|
|
NOP_ROW(), /* 0x30 */
|
2017-05-28 18:50:51 +00:00
|
|
|
|
/* 0x40 IN B, (C); 0x41 OUT (C), B */ IN_OUT(bc_.bytes.high),
|
2017-05-28 19:47:48 +00:00
|
|
|
|
/* 0x42 SBC HL, BC */ SBC16(hl_, bc_), /* 0x43 LD (nn), BC */ Program(FETCH16(temp16_, pc_), STORE16L(bc_, temp16_)),
|
2017-05-28 20:07:25 +00:00
|
|
|
|
/* 0x44 NEG */ Program({MicroOp::NEG}), /* 0x45 RETN */ Program(POP(pc_), {MicroOp::RETN}),
|
2017-06-12 22:55:04 +00:00
|
|
|
|
/* 0x46 IM 0 */ Program({MicroOp::IM}), /* 0x47 LD I, A */ Program(WAIT(1), {MicroOp::Move8, &a_, &i_}),
|
2017-05-28 18:50:51 +00:00
|
|
|
|
/* 0x40 IN B, (C); 0x41 OUT (C), B */ IN_OUT(bc_.bytes.low),
|
2017-05-28 19:47:48 +00:00
|
|
|
|
/* 0x4a ADC HL, BC */ ADC16(hl_, bc_), /* 0x4b LD BC, (nn) */ Program(FETCH16(temp16_, pc_), FETCH16L(bc_, temp16_)),
|
2017-05-28 20:07:25 +00:00
|
|
|
|
/* 0x4c NEG */ Program({MicroOp::NEG}), /* 0x4d RETI */ Program(POP(pc_), {MicroOp::RETN}),
|
2017-06-12 22:55:04 +00:00
|
|
|
|
/* 0x4e IM 0/1 */ Program({MicroOp::IM}), /* 0x4f LD R, A */ Program(WAIT(1), {MicroOp::Move8, &a_, &r_}),
|
2017-05-28 18:50:51 +00:00
|
|
|
|
/* 0x40 IN B, (C); 0x41 OUT (C), B */ IN_OUT(de_.bytes.high),
|
2017-05-28 19:47:48 +00:00
|
|
|
|
/* 0x52 SBC HL, DE */ SBC16(hl_, de_), /* 0x53 LD (nn), DE */ Program(FETCH16(temp16_, pc_), STORE16L(de_, temp16_)),
|
2017-05-28 20:07:25 +00:00
|
|
|
|
/* 0x54 NEG */ Program({MicroOp::NEG}), /* 0x55 RETN */ Program(POP(pc_), {MicroOp::RETN}),
|
2017-06-12 22:55:04 +00:00
|
|
|
|
/* 0x56 IM 1 */ Program({MicroOp::IM}), /* 0x57 LD A, I */ Program(WAIT(1), {MicroOp::Move8, &i_, &a_}, {MicroOp::SetAFlags}),
|
2017-05-28 18:50:51 +00:00
|
|
|
|
/* 0x40 IN B, (C); 0x41 OUT (C), B */ IN_OUT(de_.bytes.low),
|
2017-05-28 20:32:10 +00:00
|
|
|
|
/* 0x5a ADC HL, DE */ ADC16(hl_, de_), /* 0x5b LD DE, (nn) */ Program(FETCH16(temp16_, pc_), FETCH16L(de_, temp16_)),
|
2017-05-28 20:07:25 +00:00
|
|
|
|
/* 0x5c NEG */ Program({MicroOp::NEG}), /* 0x5d RETN */ Program(POP(pc_), {MicroOp::RETN}),
|
2017-06-12 22:55:04 +00:00
|
|
|
|
/* 0x5e IM 2 */ Program({MicroOp::IM}), /* 0x5f LD A, R */ Program(WAIT(1), {MicroOp::Move8, &r_, &a_}, {MicroOp::SetAFlags}),
|
2017-05-28 18:50:51 +00:00
|
|
|
|
/* 0x40 IN B, (C); 0x41 OUT (C), B */ IN_OUT(hl_.bytes.high),
|
2017-05-28 19:47:48 +00:00
|
|
|
|
/* 0x62 SBC HL, HL */ SBC16(hl_, hl_), /* 0x63 LD (nn), HL */ Program(FETCH16(temp16_, pc_), STORE16L(hl_, temp16_)),
|
2017-05-28 20:07:25 +00:00
|
|
|
|
/* 0x64 NEG */ Program({MicroOp::NEG}), /* 0x65 RETN */ Program(POP(pc_), {MicroOp::RETN}),
|
2017-05-28 20:46:27 +00:00
|
|
|
|
/* 0x66 IM 0 */ Program({MicroOp::IM}), /* 0x67 RRD */ Program(FETCHL(temp8_, hl_), WAIT(4), {MicroOp::RRD}, STOREL(temp8_, hl_)),
|
2017-05-28 18:50:51 +00:00
|
|
|
|
/* 0x40 IN B, (C); 0x41 OUT (C), B */ IN_OUT(hl_.bytes.low),
|
2017-05-28 19:47:48 +00:00
|
|
|
|
/* 0x6a ADC HL, HL */ ADC16(hl_, hl_), /* 0x6b LD HL, (nn) */ Program(FETCH16(temp16_, pc_), FETCH16L(hl_, temp16_)),
|
2017-05-28 20:07:25 +00:00
|
|
|
|
/* 0x6c NEG */ Program({MicroOp::NEG}), /* 0x6d RETN */ Program(POP(pc_), {MicroOp::RETN}),
|
2017-05-28 20:46:27 +00:00
|
|
|
|
/* 0x6e IM 0/1 */ Program({MicroOp::IM}), /* 0x6f RLD */ Program(FETCHL(temp8_, hl_), WAIT(4), {MicroOp::RLD}, STOREL(temp8_, hl_)),
|
2017-05-29 16:25:10 +00:00
|
|
|
|
/* 0x70 IN (C) */ IN_C(temp8_), /* 0x71 OUT (C), 0 */ Program({MicroOp::SetZero}, OUT(bc_, temp8_)),
|
2017-05-22 00:43:36 +00:00
|
|
|
|
/* 0x72 SBC HL, SP */ SBC16(hl_, sp_), /* 0x73 LD (nn), SP */ Program(FETCH16(temp16_, pc_), STORE16L(sp_, temp16_)),
|
2017-05-28 20:07:25 +00:00
|
|
|
|
/* 0x74 NEG */ Program({MicroOp::NEG}), /* 0x75 RETN */ Program(POP(pc_), {MicroOp::RETN}),
|
2017-05-29 16:25:10 +00:00
|
|
|
|
/* 0x76 IM 1 */ Program({MicroOp::IM}), /* 0x77 XX */ NOP,
|
2017-05-28 18:50:51 +00:00
|
|
|
|
/* 0x40 IN B, (C); 0x41 OUT (C), B */ IN_OUT(a_),
|
2017-05-22 00:43:36 +00:00
|
|
|
|
/* 0x7a ADC HL, SP */ ADC16(hl_, sp_), /* 0x7b LD SP, (nn) */ Program(FETCH16(temp16_, pc_), FETCH16L(sp_, temp16_)),
|
2017-05-28 20:07:25 +00:00
|
|
|
|
/* 0x7c NEG */ Program({MicroOp::NEG}), /* 0x7d RETN */ Program(POP(pc_), {MicroOp::RETN}),
|
2017-05-29 16:25:10 +00:00
|
|
|
|
/* 0x7e IM 2 */ Program({MicroOp::IM}), /* 0x7f XX */ NOP,
|
2017-05-20 21:29:30 +00:00
|
|
|
|
NOP_ROW(), /* 0x80 */
|
|
|
|
|
NOP_ROW(), /* 0x90 */
|
2017-05-29 03:55:54 +00:00
|
|
|
|
/* 0xa0 LDI */ Program(FETCHL(temp8_, hl_), STOREL(temp8_, de_), WAIT(2), {MicroOp::LDI}),
|
|
|
|
|
/* 0xa1 CPI */ Program(FETCHL(temp8_, hl_), WAIT(5), {MicroOp::CPI}),
|
2017-05-29 20:50:52 +00:00
|
|
|
|
/* 0xa2 INI */ Program(WAIT(1), IN(bc_, temp8_), STOREL(temp8_, hl_), {MicroOp::INI}),
|
2017-05-29 21:13:24 +00:00
|
|
|
|
/* 0xa3 OTI */ Program(WAIT(1), FETCHL(temp8_, hl_), {MicroOp::OUTI}, OUT(bc_, temp8_)),
|
2017-05-29 16:25:10 +00:00
|
|
|
|
NOP, NOP, NOP, NOP,
|
2017-05-29 03:55:54 +00:00
|
|
|
|
/* 0xa8 LDD */ Program(FETCHL(temp8_, hl_), STOREL(temp8_, de_), WAIT(2), {MicroOp::LDD}),
|
|
|
|
|
/* 0xa9 CPD */ Program(FETCHL(temp8_, hl_), WAIT(5), {MicroOp::CPD}),
|
2017-05-29 20:50:52 +00:00
|
|
|
|
/* 0xaa IND */ Program(WAIT(1), IN(bc_, temp8_), STOREL(temp8_, hl_), {MicroOp::IND}),
|
2017-05-29 21:13:24 +00:00
|
|
|
|
/* 0xab OTD */ Program(WAIT(1), FETCHL(temp8_, hl_), {MicroOp::OUTD}, OUT(bc_, temp8_)),
|
2017-05-29 16:25:10 +00:00
|
|
|
|
NOP, NOP, NOP, NOP,
|
2017-05-21 17:20:28 +00:00
|
|
|
|
/* 0xb0 LDIR */ Program(FETCHL(temp8_, hl_), STOREL(temp8_, de_), WAIT(2), {MicroOp::LDIR}, WAIT(5)),
|
2017-05-29 03:55:54 +00:00
|
|
|
|
/* 0xb1 CPIR */ Program(FETCHL(temp8_, hl_), WAIT(5), {MicroOp::CPIR}, WAIT(5)),
|
2017-05-29 20:50:52 +00:00
|
|
|
|
/* 0xb2 INIR */ Program(WAIT(1), IN(bc_, temp8_), STOREL(temp8_, hl_), {MicroOp::INIR}, WAIT(5)),
|
2017-05-29 21:13:24 +00:00
|
|
|
|
/* 0xb3 OTIR */ Program(WAIT(1), FETCHL(temp8_, hl_), {MicroOp::OUTI}, OUT(bc_, temp8_), {MicroOp::OUT_R}, WAIT(5)),
|
2017-05-29 16:25:10 +00:00
|
|
|
|
NOP, NOP, NOP, NOP,
|
2017-05-29 03:55:54 +00:00
|
|
|
|
/* 0xb8 LDDR */ Program(FETCHL(temp8_, hl_), STOREL(temp8_, de_), WAIT(2), {MicroOp::LDDR}, WAIT(5)),
|
|
|
|
|
/* 0xb9 CPDR */ Program(FETCHL(temp8_, hl_), WAIT(5), {MicroOp::CPDR}, WAIT(5)),
|
2017-05-29 20:50:52 +00:00
|
|
|
|
/* 0xba INDR */ Program(WAIT(1), IN(bc_, temp8_), STOREL(temp8_, hl_), {MicroOp::INDR}, WAIT(5)),
|
2017-05-29 21:13:24 +00:00
|
|
|
|
/* 0xbb OTDR */ Program(WAIT(1), FETCHL(temp8_, hl_), {MicroOp::OUTD}, OUT(bc_, temp8_), {MicroOp::OUT_R}, WAIT(5)),
|
2017-05-29 16:25:10 +00:00
|
|
|
|
NOP, NOP, NOP, NOP,
|
2017-05-20 21:29:30 +00:00
|
|
|
|
NOP_ROW(), /* 0xc0 */
|
|
|
|
|
NOP_ROW(), /* 0xd0 */
|
|
|
|
|
NOP_ROW(), /* 0xe0 */
|
|
|
|
|
NOP_ROW(), /* 0xf0 */
|
|
|
|
|
};
|
2017-05-27 03:23:33 +00:00
|
|
|
|
assemble_page(target, ed_program_table, false);
|
2017-05-20 21:29:30 +00:00
|
|
|
|
#undef NOP_ROW
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 19:41:26 +00:00
|
|
|
|
void assemble_cb_page(InstructionPage &target, RegisterPair &index, bool add_offsets) {
|
2017-05-28 01:06:56 +00:00
|
|
|
|
#define OCTO_OP_GROUP(m, x) m(x), m(x), m(x), m(x), m(x), m(x), m(x), m(x)
|
2017-05-28 03:54:06 +00:00
|
|
|
|
#define CB_PAGE(m, p) m(RLC), m(RRC), m(RL), m(RR), m(SLA), m(SRA), m(SLL), m(SRL), OCTO_OP_GROUP(p, BIT), OCTO_OP_GROUP(m, RES), OCTO_OP_GROUP(m, SET)
|
2017-05-28 01:06:56 +00:00
|
|
|
|
|
2017-05-27 19:39:22 +00:00
|
|
|
|
InstructionTable cb_program_table = {
|
|
|
|
|
/* 0x00 RLC B; 0x01 RLC C; 0x02 RLC D; 0x03 RLC E; 0x04 RLC H; 0x05 RLC L; 0x06 RLC (HL); 0x07 RLC A */
|
|
|
|
|
/* 0x08 RRC B; 0x09 RRC C; 0x0a RRC D; 0x0b RRC E; 0x0c RRC H; 0x0d RRC L; 0x0e RRC (HL); 0x0f RRC A */
|
|
|
|
|
/* 0x10 RL B; 0x11 RL C; 0x12 RL D; 0x13 RL E; 0x14 RL H; 0x15 RL L; 0x16 RL (HL); 0x17 RL A */
|
|
|
|
|
/* 0x18 RR B; 0x99 RR C; 0x1a RR D; 0x1b RR E; 0x1c RR H; 0x1d RR L; 0x1e RR (HL); 0x1f RR A */
|
|
|
|
|
/* 0x20 SLA B; 0x21 SLA C; 0x22 SLA D; 0x23 SLA E; 0x24 SLA H; 0x25 SLA L; 0x26 SLA (HL); 0x27 SLA A */
|
|
|
|
|
/* 0x28 SRA B; 0x29 SRA C; 0x2a SRA D; 0x2b SRA E; 0x2c SRA H; 0x2d SRA L; 0x2e SRA (HL); 0x2f SRA A */
|
|
|
|
|
/* 0x30 SLL B; 0x31 SLL C; 0x32 SLL D; 0x33 SLL E; 0x34 SLL H; 0x35 SLL L; 0x36 SLL (HL); 0x37 SLL A */
|
|
|
|
|
/* 0x38 SRL B; 0x39 SRL C; 0x3a SRL D; 0x3b SRL E; 0x3c SRL H; 0x3d SRL L; 0x3e SRL (HL); 0x3f SRL A */
|
|
|
|
|
/* 0x40 – 0x7f: BIT */
|
|
|
|
|
/* 0x80 – 0xcf: RES */
|
|
|
|
|
/* 0xd0 – 0xdf: SET */
|
2017-06-13 02:22:00 +00:00
|
|
|
|
CB_PAGE(MODIFY_OP_GROUP, READ_OP_GROUP_D)
|
2017-05-28 01:06:56 +00:00
|
|
|
|
};
|
|
|
|
|
InstructionTable offsets_cb_program_table = {
|
2017-05-28 03:54:06 +00:00
|
|
|
|
CB_PAGE(IX_MODIFY_OP_GROUP, IX_READ_OP_GROUP)
|
2017-05-27 19:39:22 +00:00
|
|
|
|
};
|
2017-05-28 01:06:56 +00:00
|
|
|
|
assemble_page(target, add_offsets ? offsets_cb_program_table : cb_program_table, add_offsets);
|
|
|
|
|
|
|
|
|
|
#undef OCTO_OP_GROUP
|
|
|
|
|
#undef CB_PAGE
|
2017-05-27 19:39:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void assemble_base_page(InstructionPage &target, RegisterPair &index, bool add_offsets, InstructionPage &cb_page) {
|
2017-05-21 13:17:30 +00:00
|
|
|
|
#define INC_DEC_LD(r) \
|
|
|
|
|
Program({MicroOp::Increment8, &r}), \
|
|
|
|
|
Program({MicroOp::Decrement8, &r}), \
|
|
|
|
|
Program(FETCH(r, pc_))
|
|
|
|
|
|
|
|
|
|
#define INC_INC_DEC_LD(rf, r) \
|
|
|
|
|
Program(WAIT(2), {MicroOp::Increment16, &rf.full}), INC_DEC_LD(r)
|
|
|
|
|
|
|
|
|
|
#define DEC_INC_DEC_LD(rf, r) \
|
|
|
|
|
Program(WAIT(2), {MicroOp::Decrement16, &rf.full}), INC_DEC_LD(r)
|
|
|
|
|
|
2017-05-21 23:15:52 +00:00
|
|
|
|
InstructionTable base_program_table = {
|
2017-05-29 16:25:10 +00:00
|
|
|
|
/* 0x00 NOP */ NOP, /* 0x01 LD BC, nn */ Program(FETCH16(bc_, pc_)),
|
2017-06-04 19:39:37 +00:00
|
|
|
|
/* 0x02 LD (BC), A */ Program({MicroOp::Move16, &bc_.full, &memptr_.full}, STORE(a_, memptr_)),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x03 INC BC; 0x04 INC B; 0x05 DEC B; 0x06 LD B, n */
|
|
|
|
|
INC_INC_DEC_LD(bc_, bc_.bytes.high),
|
|
|
|
|
|
|
|
|
|
/* 0x07 RLCA */ Program({MicroOp::RLCA}),
|
2017-05-26 01:26:32 +00:00
|
|
|
|
/* 0x08 EX AF, AF' */ Program({MicroOp::ExAFAFDash}), /* 0x09 ADD HL, BC */ ADD16(index, bc_),
|
2017-06-04 19:39:37 +00:00
|
|
|
|
/* 0x0a LD A, (BC) */ Program({MicroOp::Move16, &bc_.full, &memptr_.full}, FETCH(a_, memptr_)),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x0b DEC BC; 0x0c INC C; 0x0d DEC C; 0x0e LD C, n */
|
|
|
|
|
DEC_INC_DEC_LD(bc_, bc_.bytes.low),
|
|
|
|
|
|
2017-05-21 13:52:19 +00:00
|
|
|
|
/* 0x0f RRCA */ Program({MicroOp::RRCA}),
|
2017-06-04 19:07:07 +00:00
|
|
|
|
/* 0x10 DJNZ */ Program(WAIT(1), FETCH(temp8_, pc_), {MicroOp::DJNZ}, WAIT(5), {MicroOp::CalculateIndexAddress, &pc_.full}, {MicroOp::Move16, &memptr_.full, &pc_.full}),
|
2017-05-26 01:44:24 +00:00
|
|
|
|
/* 0x11 LD DE, nn */ Program(FETCH16(de_, pc_)),
|
2017-06-04 19:39:37 +00:00
|
|
|
|
/* 0x12 LD (DE), A */ Program({MicroOp::Move16, &de_.full, &memptr_.full}, STORE(a_, memptr_)),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x13 INC DE; 0x14 INC D; 0x15 DEC D; 0x16 LD D, n */
|
|
|
|
|
INC_INC_DEC_LD(de_, de_.bytes.high),
|
|
|
|
|
|
|
|
|
|
/* 0x17 RLA */ Program({MicroOp::RLA}),
|
2017-06-04 19:07:07 +00:00
|
|
|
|
/* 0x18 JR */ Program(FETCH(temp8_, pc_), WAIT(5), {MicroOp::CalculateIndexAddress, &pc_.full}, {MicroOp::Move16, &memptr_.full, &pc_.full}),
|
2017-05-26 01:48:28 +00:00
|
|
|
|
/* 0x19 ADD HL, DE */ ADD16(index, de_),
|
2017-06-04 19:39:37 +00:00
|
|
|
|
/* 0x1a LD A, (DE) */ Program({MicroOp::Move16, &de_.full, &memptr_.full}, FETCH(a_, memptr_)),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x1b DEC DE; 0x1c INC E; 0x1d DEC E; 0x1e LD E, n */
|
|
|
|
|
DEC_INC_DEC_LD(de_, de_.bytes.low),
|
|
|
|
|
|
2017-05-21 13:52:19 +00:00
|
|
|
|
/* 0x1f RRA */ Program({MicroOp::RRA}),
|
2017-05-26 01:51:30 +00:00
|
|
|
|
/* 0x20 JR NZ */ JR(TestNZ), /* 0x21 LD HL, nn */ Program(FETCH16(index, pc_)),
|
2017-05-21 23:26:40 +00:00
|
|
|
|
/* 0x22 LD (nn), HL */ Program(FETCH16(temp16_, pc_), STORE16L(index, temp16_)),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x23 INC HL; 0x24 INC H; 0x25 DEC H; 0x26 LD H, n */
|
2017-05-21 23:26:40 +00:00
|
|
|
|
INC_INC_DEC_LD(index, index.bytes.high),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
2017-05-26 02:17:48 +00:00
|
|
|
|
/* 0x27 DAA */ Program({MicroOp::DAA}),
|
2017-05-26 01:51:30 +00:00
|
|
|
|
/* 0x28 JR Z */ JR(TestZ), /* 0x29 ADD HL, HL */ ADD16(index, index),
|
2017-05-21 23:26:40 +00:00
|
|
|
|
/* 0x2a LD HL, (nn) */ Program(FETCH16(temp16_, pc_), FETCH16L(index, temp16_)),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x2b DEC HL; 0x2c INC L; 0x2d DEC L; 0x2e LD L, n */
|
2017-05-21 23:26:40 +00:00
|
|
|
|
DEC_INC_DEC_LD(index, index.bytes.low),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
2017-05-26 02:51:08 +00:00
|
|
|
|
/* 0x2f CPL */ Program({MicroOp::CPL}),
|
2017-05-26 01:51:30 +00:00
|
|
|
|
/* 0x30 JR NC */ JR(TestNC), /* 0x31 LD SP, nn */ Program(FETCH16(sp_, pc_)),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
/* 0x32 LD (nn), A */ Program(FETCH16(temp16_, pc_), STOREL(a_, temp16_)),
|
|
|
|
|
/* 0x33 INC SP */ Program(WAIT(2), {MicroOp::Increment16, &sp_.full}),
|
2017-05-27 03:37:17 +00:00
|
|
|
|
/* 0x34 INC (HL) */ Program(INDEX(), FETCHL(temp8_, INDEX_ADDR()), WAIT(1), {MicroOp::Increment8, &temp8_}, STOREL(temp8_, INDEX_ADDR())),
|
|
|
|
|
/* 0x35 DEC (HL) */ Program(INDEX(), FETCHL(temp8_, INDEX_ADDR()), WAIT(1), {MicroOp::Decrement8, &temp8_}, STOREL(temp8_, INDEX_ADDR())),
|
2017-05-29 15:40:56 +00:00
|
|
|
|
/* 0x36 LD (HL), n */ Program({MicroOp::IndexedPlaceHolder}, FETCH(temp8_, pc_), {MicroOp::CalculateIndexAddress, &index}, FETCH(temp8_, pc_), WAIT(add_offsets ? 2 : 0), STOREL(temp8_, INDEX_ADDR())),
|
2017-05-26 02:51:08 +00:00
|
|
|
|
/* 0x37 SCF */ Program({MicroOp::SCF}),
|
2017-05-26 01:51:30 +00:00
|
|
|
|
/* 0x38 JR C */ JR(TestC),
|
2017-05-21 23:26:40 +00:00
|
|
|
|
/* 0x39 ADD HL, SP */ ADD16(index, sp_),
|
2017-06-04 19:39:37 +00:00
|
|
|
|
/* 0x3a LD A, (nn) */ Program(FETCH16(memptr_, pc_), FETCH(a_, memptr_)),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
/* 0x3b DEC SP */ Program(WAIT(2), {MicroOp::Decrement16, &sp_.full}),
|
|
|
|
|
|
|
|
|
|
/* 0x3c INC A; 0x3d DEC A; 0x3e LD A, n */
|
|
|
|
|
INC_DEC_LD(a_),
|
|
|
|
|
|
2017-05-26 02:51:08 +00:00
|
|
|
|
/* 0x3f CCF */ Program({MicroOp::CCF}),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x40 LD B, B; 0x41 LD B, C; 0x42 LD B, D; 0x43 LD B, E; 0x44 LD B, H; 0x45 LD B, L; 0x46 LD B, (HL); 0x47 LD B, A */
|
2017-05-27 03:37:17 +00:00
|
|
|
|
LD_GROUP(bc_.bytes.high, bc_.bytes.high),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x48 LD C, B; 0x49 LD C, C; 0x4a LD C, D; 0x4b LD C, E; 0x4c LD C, H; 0x4d LD C, L; 0x4e LD C, (HL); 0x4f LD C, A */
|
2017-05-27 03:37:17 +00:00
|
|
|
|
LD_GROUP(bc_.bytes.low, bc_.bytes.low),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x50 LD D, B; 0x51 LD D, C; 0x52 LD D, D; 0x53 LD D, E; 0x54 LD D, H; 0x55 LD D, L; 0x56 LD D, (HL); 0x57 LD D, A */
|
2017-05-27 03:37:17 +00:00
|
|
|
|
LD_GROUP(de_.bytes.high, de_.bytes.high),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x58 LD E, B; 0x59 LD E, C; 0x5a LD E, D; 0x5b LD E, E; 0x5c LD E, H; 0x5d LD E, L; 0x5e LD E, (HL); 0x5f LD E, A */
|
2017-05-27 03:37:17 +00:00
|
|
|
|
LD_GROUP(de_.bytes.low, de_.bytes.low),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x60 LD H, B; 0x61 LD H, C; 0x62 LD H, D; 0x63 LD H, E; 0x64 LD H, H; 0x65 LD H, L; 0x66 LD H, (HL); 0x67 LD H, A */
|
2017-05-27 03:37:17 +00:00
|
|
|
|
LD_GROUP(index.bytes.high, hl_.bytes.high),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
|
|
|
|
/* 0x68 LD L, B; 0x69 LD L, C; 0x6a LD L, D; 0x6b LD L, E; 0x6c LD L, H; 0x6d LD H, L; 0x6e LD L, (HL); 0x6f LD L, A */
|
2017-05-27 03:37:17 +00:00
|
|
|
|
LD_GROUP(index.bytes.low, hl_.bytes.low),
|
2017-05-21 13:17:30 +00:00
|
|
|
|
|
2017-05-29 15:44:26 +00:00
|
|
|
|
/* 0x70 LD (HL), B */ Program(INDEX(), STOREL(bc_.bytes.high, INDEX_ADDR())),
|
2017-05-27 03:37:17 +00:00
|
|
|
|
/* 0x71 LD (HL), C */ Program(INDEX(), STOREL(bc_.bytes.low, INDEX_ADDR())),
|
2017-05-29 15:44:26 +00:00
|
|
|
|
/* 0x72 LD (HL), D */ Program(INDEX(), STOREL(de_.bytes.high, INDEX_ADDR())),
|
2017-05-27 03:37:17 +00:00
|
|
|
|
/* 0x73 LD (HL), E */ Program(INDEX(), STOREL(de_.bytes.low, INDEX_ADDR())),
|
2017-05-29 15:44:26 +00:00
|
|
|
|
/* 0x74 LD (HL), H */ Program(INDEX(), STOREL(hl_.bytes.high, INDEX_ADDR())), // neither of these stores parts of the index register;
|
|
|
|
|
/* 0x75 LD (HL), L */ Program(INDEX(), STOREL(hl_.bytes.low, INDEX_ADDR())), // they always store exactly H and L.
|
2017-05-29 15:54:27 +00:00
|
|
|
|
/* 0x76 HALT */ Program({MicroOp::HALT}),
|
2017-05-27 03:37:17 +00:00
|
|
|
|
/* 0x77 LD (HL), A */ Program(INDEX(), STOREL(a_, INDEX_ADDR())),
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
|
|
|
|
/* 0x78 LD A, B; 0x79 LD A, C; 0x7a LD A, D; 0x7b LD A, E; 0x7c LD A, H; 0x7d LD A, L; 0x7e LD A, (HL); 0x7f LD A, A */
|
2017-05-27 03:37:17 +00:00
|
|
|
|
LD_GROUP(a_, a_),
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
|
|
|
|
/* 0x80 ADD B; 0x81 ADD C; 0x82 ADD D; 0x83 ADD E; 0x84 ADD H; 0x85 ADD L; 0x86 ADD (HL); 0x87 ADD A */
|
2017-05-27 19:54:24 +00:00
|
|
|
|
READ_OP_GROUP(ADD8),
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
|
|
|
|
/* 0x88 ADC B; 0x89 ADC C; 0x8a ADC D; 0x8b ADC E; 0x8c ADC H; 0x8d ADC L; 0x8e ADC (HL); 0x8f ADC A */
|
2017-05-27 19:54:24 +00:00
|
|
|
|
READ_OP_GROUP(ADC8),
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
|
|
|
|
/* 0x90 SUB B; 0x91 SUB C; 0x92 SUB D; 0x93 SUB E; 0x94 SUB H; 0x95 SUB L; 0x96 SUB (HL); 0x97 SUB A */
|
2017-05-27 19:54:24 +00:00
|
|
|
|
READ_OP_GROUP(SUB8),
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
|
|
|
|
/* 0x98 SBC B; 0x99 SBC C; 0x9a SBC D; 0x9b SBC E; 0x9c SBC H; 0x9d SBC L; 0x9e SBC (HL); 0x9f SBC A */
|
2017-05-27 19:54:24 +00:00
|
|
|
|
READ_OP_GROUP(SBC8),
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
|
|
|
|
/* 0xa0 AND B; 0xa1 AND C; 0xa2 AND D; 0xa3 AND E; 0xa4 AND H; 0xa5 AND L; 0xa6 AND (HL); 0xa7 AND A */
|
2017-05-27 19:54:24 +00:00
|
|
|
|
READ_OP_GROUP(And),
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
|
|
|
|
/* 0xa8 XOR B; 0xa9 XOR C; 0xaa XOR D; 0xab XOR E; 0xac XOR H; 0xad XOR L; 0xae XOR (HL); 0xaf XOR A */
|
2017-05-27 19:54:24 +00:00
|
|
|
|
READ_OP_GROUP(Xor),
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
|
|
|
|
/* 0xb0 OR B; 0xb1 OR C; 0xb2 OR D; 0xb3 OR E; 0xb4 OR H; 0xb5 OR L; 0xb6 OR (HL); 0xb7 OR A */
|
2017-05-27 19:54:24 +00:00
|
|
|
|
READ_OP_GROUP(Or),
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
|
|
|
|
/* 0xb8 CP B; 0xb9 CP C; 0xba CP D; 0xbb CP E; 0xbc CP H; 0xbd CP L; 0xbe CP (HL); 0xbf CP A */
|
2017-05-27 19:54:24 +00:00
|
|
|
|
READ_OP_GROUP(CP8),
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xc0 RET NZ */ RET(TestNZ), /* 0xc1 POP BC */ Program(POP(bc_)),
|
|
|
|
|
/* 0xc2 JP NZ */ JP(TestNZ), /* 0xc3 JP nn */ Program(FETCH16L(temp16_, pc_), {MicroOp::Move16, &temp16_.full, &pc_.full}),
|
|
|
|
|
/* 0xc4 CALL NZ */ CALL(TestNZ), /* 0xc5 PUSH BC */ Program(WAIT(1), PUSH(bc_)),
|
|
|
|
|
/* 0xc6 ADD A, n */ Program(FETCH(temp8_, pc_), {MicroOp::ADD8, &temp8_}),
|
2017-05-26 11:29:19 +00:00
|
|
|
|
/* 0xc7 RST 00h */ RST(),
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xc8 RET Z */ RET(TestZ), /* 0xc9 RET */ Program(POP(pc_)),
|
2017-06-01 00:15:56 +00:00
|
|
|
|
/* 0xca JP Z */ JP(TestZ), /* 0xcb [CB page] */Program(FINDEX(), {MicroOp::SetInstructionPage, &cb_page}),
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xcc CALL Z */ CALL(TestZ), /* 0xcd CALL */ Program(FETCH16(temp16_, pc_), WAIT(1), PUSH(pc_), {MicroOp::Move16, &temp16_.full, &pc_.full}),
|
|
|
|
|
/* 0xce ADC A, n */ Program(FETCH(temp8_, pc_), {MicroOp::ADC8, &temp8_}),
|
2017-05-26 11:29:19 +00:00
|
|
|
|
/* 0xcf RST 08h */ RST(),
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xd0 RET NC */ RET(TestNC), /* 0xd1 POP DE */ Program(POP(de_)),
|
2017-05-28 18:20:05 +00:00
|
|
|
|
/* 0xd2 JP NC */ JP(TestNC), /* 0xd3 OUT (n), A */Program(FETCH(temp16_.bytes.low, pc_), {MicroOp::Move8, &a_, &temp16_.bytes.high}, OUT(temp16_, a_)),
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xd4 CALL NC */ CALL(TestNC), /* 0xd5 PUSH DE */ Program(WAIT(1), PUSH(de_)),
|
|
|
|
|
/* 0xd6 SUB n */ Program(FETCH(temp8_, pc_), {MicroOp::SUB8, &temp8_}),
|
2017-05-26 11:29:19 +00:00
|
|
|
|
/* 0xd7 RST 10h */ RST(),
|
2017-05-28 18:55:14 +00:00
|
|
|
|
/* 0xd8 RET C */ RET(TestC), /* 0xd9 EXX */ Program({MicroOp::EXX}),
|
2017-05-28 18:20:05 +00:00
|
|
|
|
/* 0xda JP C */ JP(TestC), /* 0xdb IN A, (n) */Program(FETCH(temp16_.bytes.low, pc_), {MicroOp::Move8, &a_, &temp16_.bytes.high}, IN(temp16_, a_)),
|
2017-05-21 23:26:40 +00:00
|
|
|
|
/* 0xdc CALL C */ CALL(TestC), /* 0xdd [DD page] */Program({MicroOp::SetInstructionPage, &dd_page_}),
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xde SBC A, n */ Program(FETCH(temp8_, pc_), {MicroOp::SBC8, &temp8_}),
|
2017-05-26 11:29:19 +00:00
|
|
|
|
/* 0xdf RST 18h */ RST(),
|
2017-05-21 23:26:40 +00:00
|
|
|
|
/* 0xe0 RET PO */ RET(TestPO), /* 0xe1 POP HL */ Program(POP(index)),
|
2017-06-04 19:39:37 +00:00
|
|
|
|
/* 0xe2 JP PO */ JP(TestPO), /* 0xe3 EX (SP), HL */Program(POP(memptr_), WAIT(1), PUSH(index), WAIT(2), {MicroOp::Move16, &memptr_.full, &index.full}),
|
2017-05-21 23:26:40 +00:00
|
|
|
|
/* 0xe4 CALL PO */ CALL(TestPO), /* 0xe5 PUSH HL */ Program(WAIT(1), PUSH(index)),
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xe6 AND n */ Program(FETCH(temp8_, pc_), {MicroOp::And, &temp8_}),
|
2017-05-26 11:29:19 +00:00
|
|
|
|
/* 0xe7 RST 20h */ RST(),
|
2017-05-21 23:26:40 +00:00
|
|
|
|
/* 0xe8 RET PE */ RET(TestPE), /* 0xe9 JP (HL) */ Program({MicroOp::Move16, &index.full, &pc_.full}),
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xea JP PE */ JP(TestPE), /* 0xeb EX DE, HL */Program({MicroOp::ExDEHL}),
|
2017-05-21 23:15:52 +00:00
|
|
|
|
/* 0xec CALL PE */ CALL(TestPE), /* 0xed [ED page] */Program({MicroOp::SetInstructionPage, &ed_page_}),
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xee XOR n */ Program(FETCH(temp8_, pc_), {MicroOp::Xor, &temp8_}),
|
2017-05-26 11:29:19 +00:00
|
|
|
|
/* 0xef RST 28h */ RST(),
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xf0 RET p */ RET(TestP), /* 0xf1 POP AF */ Program(POP(temp16_), {MicroOp::DisassembleAF}),
|
2017-05-21 16:53:17 +00:00
|
|
|
|
/* 0xf2 JP P */ JP(TestP), /* 0xf3 DI */ Program({MicroOp::DI}),
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xf4 CALL P */ CALL(TestP), /* 0xf5 PUSH AF */ Program(WAIT(1), {MicroOp::AssembleAF}, PUSH(temp16_)),
|
|
|
|
|
/* 0xf6 OR n */ Program(FETCH(temp8_, pc_), {MicroOp::Or, &temp8_}),
|
2017-05-26 11:29:19 +00:00
|
|
|
|
/* 0xf7 RST 30h */ RST(),
|
2017-05-21 23:26:40 +00:00
|
|
|
|
/* 0xf8 RET M */ RET(TestM), /* 0xf9 LD SP, HL */Program(WAIT(2), {MicroOp::Move16, &index.full, &sp_.full}),
|
2017-05-21 16:53:17 +00:00
|
|
|
|
/* 0xfa JP M */ JP(TestM), /* 0xfb EI */ Program({MicroOp::EI}),
|
2017-05-21 23:15:52 +00:00
|
|
|
|
/* 0xfc CALL M */ CALL(TestM), /* 0xfd [FD page] */Program({MicroOp::SetInstructionPage, &fd_page_}),
|
2017-05-21 14:13:59 +00:00
|
|
|
|
/* 0xfe CP n */ Program(FETCH(temp8_, pc_), {MicroOp::CP8, &temp8_}),
|
2017-05-26 11:29:19 +00:00
|
|
|
|
/* 0xff RST 38h */ RST(),
|
2017-05-18 01:45:23 +00:00
|
|
|
|
};
|
2017-05-27 19:39:22 +00:00
|
|
|
|
assemble_cb_page(cb_page, index, add_offsets);
|
2017-05-27 03:23:33 +00:00
|
|
|
|
assemble_page(target, base_program_table, add_offsets);
|
2017-05-20 21:04:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-28 03:54:06 +00:00
|
|
|
|
void assemble_fetch_decode_execute(InstructionPage &target, int length) {
|
2017-05-23 01:50:34 +00:00
|
|
|
|
const MicroOp fetch_decode_execute[] = {
|
2017-05-28 03:54:06 +00:00
|
|
|
|
{ MicroOp::BusOperation, nullptr, nullptr, {(length == 4) ? ReadOpcode : Read, length, &pc_.full, &operation_}},
|
2017-06-03 16:19:25 +00:00
|
|
|
|
{ MicroOp::DecodeOperation }
|
2017-05-23 01:50:34 +00:00
|
|
|
|
};
|
2017-06-03 16:16:21 +00:00
|
|
|
|
copy_program(fetch_decode_execute, target.fetch_decode_execute);
|
2017-06-01 22:28:04 +00:00
|
|
|
|
target.fetch_decode_execute_data = target.fetch_decode_execute.data();
|
2017-05-23 01:50:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 16:16:21 +00:00
|
|
|
|
void copy_program(const MicroOp *source, std::vector<MicroOp> &destination) {
|
2017-06-03 15:30:12 +00:00
|
|
|
|
size_t length = 0;
|
2017-06-03 16:19:25 +00:00
|
|
|
|
while(!isTerminal(source[length].type)) length++;
|
2017-06-03 15:30:12 +00:00
|
|
|
|
destination.resize(length + 1);
|
|
|
|
|
size_t pointer = 0;
|
|
|
|
|
while(true) {
|
|
|
|
|
destination[pointer] = source[pointer];
|
2017-06-03 16:19:25 +00:00
|
|
|
|
if(isTerminal(source[pointer].type)) break;
|
2017-06-03 15:30:12 +00:00
|
|
|
|
pointer++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 11:59:21 +00:00
|
|
|
|
public:
|
2017-06-03 23:17:34 +00:00
|
|
|
|
Processor() :
|
2017-05-31 02:15:43 +00:00
|
|
|
|
halt_mask_(0xff),
|
2017-06-02 01:40:08 +00:00
|
|
|
|
number_of_cycles_(0),
|
|
|
|
|
request_status_(Interrupt::PowerOn),
|
2017-06-02 02:31:04 +00:00
|
|
|
|
last_request_status_(Interrupt::PowerOn),
|
2017-06-03 21:07:05 +00:00
|
|
|
|
irq_line_(false),
|
2017-06-03 23:09:47 +00:00
|
|
|
|
bus_request_line_(false),
|
2017-06-03 23:17:34 +00:00
|
|
|
|
pc_increment_(1),
|
|
|
|
|
scheduled_program_counter_(nullptr) {
|
2017-05-29 23:25:08 +00:00
|
|
|
|
set_flags(0xff);
|
|
|
|
|
|
2017-05-27 19:39:22 +00:00
|
|
|
|
assemble_base_page(base_page_, hl_, false, cb_page_);
|
|
|
|
|
assemble_base_page(dd_page_, ix_, true, ddcb_page_);
|
|
|
|
|
assemble_base_page(fd_page_, iy_, true, fdcb_page_);
|
2017-05-20 21:29:30 +00:00
|
|
|
|
assemble_ed_page(ed_page_);
|
2017-05-28 01:06:56 +00:00
|
|
|
|
|
2017-06-04 18:13:38 +00:00
|
|
|
|
fdcb_page_.r_step = 0;
|
|
|
|
|
fd_page_.is_indexed = true;
|
|
|
|
|
fdcb_page_.is_indexed = true;
|
|
|
|
|
|
|
|
|
|
ddcb_page_.r_step = 0;
|
|
|
|
|
dd_page_.is_indexed = true;
|
|
|
|
|
ddcb_page_.is_indexed = true;
|
2017-05-28 03:54:06 +00:00
|
|
|
|
|
|
|
|
|
assemble_fetch_decode_execute(base_page_, 4);
|
|
|
|
|
assemble_fetch_decode_execute(dd_page_, 4);
|
|
|
|
|
assemble_fetch_decode_execute(fd_page_, 4);
|
|
|
|
|
assemble_fetch_decode_execute(ed_page_, 4);
|
|
|
|
|
assemble_fetch_decode_execute(cb_page_, 4);
|
|
|
|
|
|
|
|
|
|
assemble_fetch_decode_execute(fdcb_page_, 3);
|
|
|
|
|
assemble_fetch_decode_execute(ddcb_page_, 3);
|
2017-06-02 02:31:04 +00:00
|
|
|
|
|
|
|
|
|
MicroOp reset_program[] = Program(WAIT(3), {MicroOp::Reset});
|
2017-06-03 15:30:12 +00:00
|
|
|
|
MicroOp nmi_program[] = {
|
|
|
|
|
{ MicroOp::BeginNMI },
|
|
|
|
|
{ MicroOp::BusOperation, nullptr, nullptr, {ReadOpcode, 5, &pc_.full, &operation_}},
|
|
|
|
|
PUSH(pc_),
|
2017-06-03 21:41:45 +00:00
|
|
|
|
{ MicroOp::JumpTo66, nullptr, nullptr},
|
2017-06-03 15:30:12 +00:00
|
|
|
|
{ MicroOp::MoveToNextProgram }
|
|
|
|
|
};
|
2017-06-03 21:07:05 +00:00
|
|
|
|
MicroOp irq_mode0_program[] = {
|
|
|
|
|
{ MicroOp::BeginIRQMode0 },
|
2017-06-03 22:42:54 +00:00
|
|
|
|
{ MicroOp::BusOperation, nullptr, nullptr, {BusOperation::Interrupt, 6, nullptr, &operation_}},
|
2017-06-03 21:07:05 +00:00
|
|
|
|
{ MicroOp::DecodeOperationNoRChange }
|
|
|
|
|
};
|
|
|
|
|
MicroOp irq_mode1_program[] = {
|
|
|
|
|
{ MicroOp::BeginIRQ },
|
|
|
|
|
{ MicroOp::BusOperation, nullptr, nullptr, {BusOperation::Interrupt, 7, nullptr, &operation_}},
|
|
|
|
|
PUSH(pc_),
|
|
|
|
|
{ MicroOp::Move16, &temp16_.full, &pc_.full },
|
|
|
|
|
{ MicroOp::MoveToNextProgram }
|
|
|
|
|
};
|
|
|
|
|
MicroOp irq_mode2_program[] = {
|
|
|
|
|
{ MicroOp::BeginIRQ },
|
|
|
|
|
{ MicroOp::BusOperation, nullptr, nullptr, {BusOperation::Interrupt, 7, nullptr, &temp16_.bytes.low}},
|
|
|
|
|
PUSH(pc_),
|
|
|
|
|
{ MicroOp::Move8, &i_, &temp16_.bytes.high },
|
|
|
|
|
FETCH16L(pc_, temp16_),
|
|
|
|
|
{ MicroOp::MoveToNextProgram }
|
|
|
|
|
};
|
2017-06-03 15:30:12 +00:00
|
|
|
|
|
|
|
|
|
copy_program(reset_program, reset_program_);
|
|
|
|
|
copy_program(nmi_program, nmi_program_);
|
2017-06-03 21:07:05 +00:00
|
|
|
|
copy_program(irq_mode0_program, irq_program_[0]);
|
|
|
|
|
copy_program(irq_mode1_program, irq_program_[1]);
|
|
|
|
|
copy_program(irq_mode2_program, irq_program_[2]);
|
2017-05-18 01:45:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 11:59:21 +00:00
|
|
|
|
/*!
|
|
|
|
|
Runs the Z80 for a supplied number of cycles.
|
|
|
|
|
|
2017-05-30 23:24:58 +00:00
|
|
|
|
@discussion Subclasses must implement @c perform_machine_cycle(const MachineCycle &cycle) .
|
2017-05-15 11:59:21 +00:00
|
|
|
|
|
|
|
|
|
If it is a read operation then @c value will be seeded with the value 0xff.
|
|
|
|
|
|
|
|
|
|
@param number_of_cycles The number of cycles to run the Z80 for.
|
|
|
|
|
*/
|
|
|
|
|
void run_for_cycles(int number_of_cycles) {
|
2017-05-16 02:25:52 +00:00
|
|
|
|
|
2017-06-01 00:15:56 +00:00
|
|
|
|
#define advance_operation() \
|
2017-06-03 21:07:05 +00:00
|
|
|
|
pc_increment_ = 1; \
|
2017-06-02 02:16:22 +00:00
|
|
|
|
if(last_request_status_) { \
|
|
|
|
|
halt_mask_ = 0xff; \
|
|
|
|
|
if(last_request_status_ & (Interrupt::PowerOn | Interrupt::Reset)) { \
|
|
|
|
|
request_status_ &= ~Interrupt::PowerOn; \
|
|
|
|
|
scheduled_program_counter_ = reset_program_.data(); \
|
|
|
|
|
} else if(last_request_status_ & Interrupt::NMI) { \
|
|
|
|
|
request_status_ &= ~Interrupt::NMI; \
|
|
|
|
|
scheduled_program_counter_ = nmi_program_.data(); \
|
|
|
|
|
} else if(last_request_status_ & Interrupt::IRQ) { \
|
|
|
|
|
scheduled_program_counter_ = irq_program_[interrupt_mode_].data(); \
|
|
|
|
|
} \
|
|
|
|
|
} else { \
|
|
|
|
|
current_instruction_page_ = &base_page_; \
|
|
|
|
|
scheduled_program_counter_ = base_page_.fetch_decode_execute_data; \
|
|
|
|
|
}
|
2017-05-18 01:45:23 +00:00
|
|
|
|
|
2017-05-16 02:25:52 +00:00
|
|
|
|
number_of_cycles_ += number_of_cycles;
|
2017-06-01 00:15:56 +00:00
|
|
|
|
if(!scheduled_program_counter_) {
|
|
|
|
|
advance_operation();
|
|
|
|
|
}
|
2017-05-18 01:45:23 +00:00
|
|
|
|
|
2017-05-16 02:25:52 +00:00
|
|
|
|
while(1) {
|
2017-06-03 23:09:47 +00:00
|
|
|
|
|
|
|
|
|
while(bus_request_line_) {
|
|
|
|
|
static MachineCycle bus_acknowledge_cycle = {BusOperation::BusAcknowledge, 1};
|
|
|
|
|
number_of_cycles_ -= static_cast<T *>(this)->perform_machine_cycle(bus_acknowledge_cycle) + 1;
|
2017-06-04 22:08:35 +00:00
|
|
|
|
if(!number_of_cycles_) {
|
2017-06-06 21:52:44 +00:00
|
|
|
|
static_cast<T *>(this)->flush();
|
2017-06-04 22:08:35 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-06-03 23:09:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while(!bus_request_line_) {
|
|
|
|
|
const MicroOp *operation = scheduled_program_counter_;
|
|
|
|
|
scheduled_program_counter_++;
|
2017-05-18 01:45:23 +00:00
|
|
|
|
|
2017-05-26 02:41:05 +00:00
|
|
|
|
#define set_parity(v) \
|
2017-06-04 22:40:59 +00:00
|
|
|
|
parity_overflow_result_ = (uint8_t)(v^1);\
|
2017-05-30 02:23:19 +00:00
|
|
|
|
parity_overflow_result_ ^= parity_overflow_result_ >> 4;\
|
|
|
|
|
parity_overflow_result_ ^= parity_overflow_result_ << 2;\
|
|
|
|
|
parity_overflow_result_ ^= parity_overflow_result_ >> 1;
|
2017-05-26 02:41:05 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
switch(operation->type) {
|
|
|
|
|
case MicroOp::BusOperation:
|
2017-06-06 21:52:44 +00:00
|
|
|
|
if(number_of_cycles_ < operation->machine_cycle.length) {
|
|
|
|
|
scheduled_program_counter_--;
|
|
|
|
|
static_cast<T *>(this)->flush();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-06-03 23:09:47 +00:00
|
|
|
|
number_of_cycles_ -= operation->machine_cycle.length;
|
|
|
|
|
last_request_status_ = request_status_;
|
|
|
|
|
number_of_cycles_ -= static_cast<T *>(this)->perform_machine_cycle(operation->machine_cycle);
|
|
|
|
|
break;
|
|
|
|
|
case MicroOp::MoveToNextProgram:
|
|
|
|
|
advance_operation();
|
|
|
|
|
break;
|
|
|
|
|
case MicroOp::DecodeOperation:
|
2017-06-04 18:13:38 +00:00
|
|
|
|
r_ = (r_ & 0x80) | ((r_ + current_instruction_page_->r_step) & 0x7f);
|
2017-06-05 14:35:03 +00:00
|
|
|
|
pc_.full += pc_increment_ & (uint16_t)halt_mask_;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::DecodeOperationNoRChange:
|
|
|
|
|
scheduled_program_counter_ = current_instruction_page_->instructions[operation_ & halt_mask_];
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::Increment16: (*(uint16_t *)operation->source)++; break;
|
|
|
|
|
case MicroOp::IncrementPC: pc_.full += pc_increment_; break;
|
|
|
|
|
case MicroOp::Decrement16: (*(uint16_t *)operation->source)--; break;
|
|
|
|
|
case MicroOp::Move8: *(uint8_t *)operation->destination = *(uint8_t *)operation->source; break;
|
|
|
|
|
case MicroOp::Move16: *(uint16_t *)operation->destination = *(uint16_t *)operation->source; break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::AssembleAF:
|
|
|
|
|
temp16_.bytes.high = a_;
|
|
|
|
|
temp16_.bytes.low = get_flags();
|
|
|
|
|
break;
|
|
|
|
|
case MicroOp::DisassembleAF:
|
|
|
|
|
a_ = temp16_.bytes.high;
|
|
|
|
|
set_flags(temp16_.bytes.low);
|
|
|
|
|
break;
|
2017-05-19 23:28:38 +00:00
|
|
|
|
|
2017-05-21 13:43:17 +00:00
|
|
|
|
#pragma mark - Logical
|
|
|
|
|
|
2017-05-28 19:09:58 +00:00
|
|
|
|
#define set_logical_flags(hf) \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
sign_result_ = zero_result_ = bit53_result_ = a_; \
|
2017-05-28 19:09:58 +00:00
|
|
|
|
set_parity(a_); \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
half_carry_result_ = hf; \
|
2017-05-28 19:09:58 +00:00
|
|
|
|
subtract_flag_ = 0; \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
carry_result_ = 0;
|
2017-05-28 19:09:58 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::And:
|
|
|
|
|
a_ &= *(uint8_t *)operation->source;
|
|
|
|
|
set_logical_flags(Flag::HalfCarry);
|
|
|
|
|
break;
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::Or:
|
|
|
|
|
a_ |= *(uint8_t *)operation->source;
|
|
|
|
|
set_logical_flags(0);
|
|
|
|
|
break;
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::Xor:
|
|
|
|
|
a_ ^= *(uint8_t *)operation->source;
|
|
|
|
|
set_logical_flags(0);
|
|
|
|
|
break;
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-05-28 19:09:58 +00:00
|
|
|
|
#undef set_logical_flags
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::CPL:
|
|
|
|
|
a_ ^= 0xff;
|
|
|
|
|
subtract_flag_ = Flag::Subtract;
|
|
|
|
|
half_carry_result_ = Flag::HalfCarry;
|
|
|
|
|
bit53_result_ = a_;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::CCF:
|
2017-06-04 22:40:59 +00:00
|
|
|
|
half_carry_result_ = (uint8_t)(carry_result_ << 4);
|
2017-06-03 23:09:47 +00:00
|
|
|
|
carry_result_ ^= Flag::Carry;
|
|
|
|
|
subtract_flag_ = 0;
|
|
|
|
|
bit53_result_ = a_;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::SCF:
|
|
|
|
|
carry_result_ = Flag::Carry;
|
|
|
|
|
half_carry_result_ = 0;
|
|
|
|
|
subtract_flag_ = 0;
|
|
|
|
|
bit53_result_ = a_;
|
|
|
|
|
break;
|
2017-05-26 02:51:08 +00:00
|
|
|
|
|
2017-05-26 11:29:19 +00:00
|
|
|
|
#pragma mark - Flow control
|
2017-05-26 01:44:24 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::DJNZ:
|
|
|
|
|
bc_.bytes.high--;
|
|
|
|
|
if(!bc_.bytes.high) {
|
|
|
|
|
advance_operation();
|
|
|
|
|
}
|
|
|
|
|
break;
|
2017-05-26 01:44:24 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::CalculateRSTDestination:
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full = operation_ & 0x38;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
break;
|
2017-05-26 11:29:19 +00:00
|
|
|
|
|
2017-05-21 13:43:17 +00:00
|
|
|
|
#pragma mark - 8-bit arithmetic
|
|
|
|
|
|
2017-06-01 02:51:32 +00:00
|
|
|
|
#define set_arithmetic_flags(sub, b53) \
|
|
|
|
|
sign_result_ = zero_result_ = (uint8_t)result; \
|
2017-06-04 22:40:59 +00:00
|
|
|
|
carry_result_ = (uint8_t)(result >> 8); \
|
|
|
|
|
half_carry_result_ = (uint8_t)half_result; \
|
|
|
|
|
parity_overflow_result_ = (uint8_t)(overflow >> 5); \
|
2017-06-01 02:51:32 +00:00
|
|
|
|
subtract_flag_ = sub; \
|
|
|
|
|
bit53_result_ = (uint8_t)b53;
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::CP8: {
|
|
|
|
|
uint8_t value = *(uint8_t *)operation->source;
|
|
|
|
|
int result = a_ - value;
|
|
|
|
|
int half_result = (a_&0xf) - (value&0xf);
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
// overflow for a subtraction is when the signs were originally
|
|
|
|
|
// different and the result is different again
|
|
|
|
|
int overflow = (value^a_) & (result^a_);
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
// the 5 and 3 flags come from the operand, atypically
|
|
|
|
|
set_arithmetic_flags(Flag::Subtract, value);
|
|
|
|
|
} break;
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::SUB8: {
|
|
|
|
|
uint8_t value = *(uint8_t *)operation->source;
|
|
|
|
|
int result = a_ - value;
|
|
|
|
|
int half_result = (a_&0xf) - (value&0xf);
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
// overflow for a subtraction is when the signs were originally
|
|
|
|
|
// different and the result is different again
|
|
|
|
|
int overflow = (value^a_) & (result^a_);
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
a_ = (uint8_t)result;
|
|
|
|
|
set_arithmetic_flags(Flag::Subtract, result);
|
|
|
|
|
} break;
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::SBC8: {
|
|
|
|
|
uint8_t value = *(uint8_t *)operation->source;
|
|
|
|
|
int result = a_ - value - (carry_result_ & Flag::Carry);
|
|
|
|
|
int half_result = (a_&0xf) - (value&0xf) - (carry_result_ & Flag::Carry);
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
// overflow for a subtraction is when the signs were originally
|
|
|
|
|
// different and the result is different again
|
|
|
|
|
int overflow = (value^a_) & (result^a_);
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
a_ = (uint8_t)result;
|
|
|
|
|
set_arithmetic_flags(Flag::Subtract, result);
|
|
|
|
|
} break;
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::ADD8: {
|
|
|
|
|
uint8_t value = *(uint8_t *)operation->source;
|
|
|
|
|
int result = a_ + value;
|
|
|
|
|
int half_result = (a_&0xf) + (value&0xf);
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
// overflow for addition is when the signs were originally
|
|
|
|
|
// the same and the result is different
|
|
|
|
|
int overflow = ~(value^a_) & (result^a_);
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
a_ = (uint8_t)result;
|
|
|
|
|
set_arithmetic_flags(0, result);
|
|
|
|
|
} break;
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::ADC8: {
|
|
|
|
|
uint8_t value = *(uint8_t *)operation->source;
|
|
|
|
|
int result = a_ + value + (carry_result_ & Flag::Carry);
|
|
|
|
|
int half_result = (a_&0xf) + (value&0xf) + (carry_result_ & Flag::Carry);
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
// overflow for addition is when the signs were originally
|
|
|
|
|
// the same and the result is different
|
|
|
|
|
int overflow = ~(value^a_) & (result^a_);
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
a_ = (uint8_t)result;
|
|
|
|
|
set_arithmetic_flags(0, result);
|
|
|
|
|
} break;
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-06-01 02:51:32 +00:00
|
|
|
|
#undef set_arithmetic_flags
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::NEG: {
|
|
|
|
|
int overflow = (a_ == 0x80);
|
|
|
|
|
int result = -a_;
|
|
|
|
|
int halfResult = -(a_&0xf);
|
|
|
|
|
|
|
|
|
|
a_ = (uint8_t)result;
|
|
|
|
|
bit53_result_ = sign_result_ = zero_result_ = a_;
|
|
|
|
|
parity_overflow_result_ = overflow ? Flag::Overflow : 0;
|
|
|
|
|
subtract_flag_ = Flag::Subtract;
|
2017-06-04 22:40:59 +00:00
|
|
|
|
carry_result_ = (uint8_t)(result >> 8);
|
|
|
|
|
half_carry_result_ = (uint8_t)halfResult;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::Increment8: {
|
|
|
|
|
uint8_t value = *(uint8_t *)operation->source;
|
|
|
|
|
int result = value + 1;
|
|
|
|
|
|
|
|
|
|
// with an increment, overflow occurs if the sign changes from
|
|
|
|
|
// positive to negative
|
|
|
|
|
int overflow = (value ^ result) & ~value;
|
|
|
|
|
int half_result = (value&0xf) + 1;
|
|
|
|
|
|
|
|
|
|
*(uint8_t *)operation->source = (uint8_t)result;
|
|
|
|
|
|
|
|
|
|
// sign, zero and 5 & 3 are set directly from the result
|
|
|
|
|
bit53_result_ = sign_result_ = zero_result_ = (uint8_t)result;
|
2017-06-04 22:40:59 +00:00
|
|
|
|
half_carry_result_ = (uint8_t)half_result;
|
|
|
|
|
parity_overflow_result_ = (uint8_t)(overflow >> 5);
|
2017-06-03 23:09:47 +00:00
|
|
|
|
subtract_flag_ = 0;
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::Decrement8: {
|
|
|
|
|
uint8_t value = *(uint8_t *)operation->source;
|
|
|
|
|
int result = value - 1;
|
|
|
|
|
|
|
|
|
|
// with a decrement, overflow occurs if the sign changes from
|
|
|
|
|
// negative to positive
|
|
|
|
|
int overflow = (value ^ result) & value;
|
|
|
|
|
int half_result = (value&0xf) - 1;
|
|
|
|
|
|
|
|
|
|
*(uint8_t *)operation->source = (uint8_t)result;
|
|
|
|
|
|
|
|
|
|
// sign, zero and 5 & 3 are set directly from the result
|
|
|
|
|
bit53_result_ = sign_result_ = zero_result_ = (uint8_t)result;
|
2017-06-04 22:40:59 +00:00
|
|
|
|
half_carry_result_ = (uint8_t)half_result;
|
|
|
|
|
parity_overflow_result_ = (uint8_t)(overflow >> 5);
|
2017-06-03 23:09:47 +00:00
|
|
|
|
subtract_flag_ = Flag::Subtract;
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::DAA: {
|
|
|
|
|
int lowNibble = a_ & 0xf;
|
|
|
|
|
int highNibble = a_ >> 4;
|
|
|
|
|
int amountToAdd = 0;
|
|
|
|
|
|
|
|
|
|
if(carry_result_ & Flag::Carry)
|
2017-05-26 02:17:48 +00:00
|
|
|
|
{
|
2017-06-03 23:09:47 +00:00
|
|
|
|
amountToAdd = (lowNibble > 0x9 || (half_carry_result_ & Flag::HalfCarry)) ? 0x66 : 0x60;
|
2017-05-26 02:17:48 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2017-06-03 23:09:47 +00:00
|
|
|
|
{
|
|
|
|
|
if(half_carry_result_ & Flag::HalfCarry)
|
|
|
|
|
{
|
|
|
|
|
if(lowNibble > 0x9)
|
|
|
|
|
amountToAdd = (highNibble > 0x8) ? 0x66 : 0x06;
|
|
|
|
|
else
|
|
|
|
|
amountToAdd = (highNibble > 0x9) ? 0x66 : 0x06;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(lowNibble > 0x9)
|
|
|
|
|
amountToAdd = (highNibble > 0x8) ? 0x66 : 0x06;
|
|
|
|
|
else
|
|
|
|
|
amountToAdd = (highNibble > 0x9) ? 0x60 : 0x00;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!(carry_result_ & Flag::Carry))
|
2017-05-26 02:17:48 +00:00
|
|
|
|
{
|
|
|
|
|
if(lowNibble > 0x9)
|
2017-06-03 23:09:47 +00:00
|
|
|
|
{
|
|
|
|
|
if(highNibble > 0x8) carry_result_ = Flag::Carry;
|
|
|
|
|
}
|
2017-05-26 02:17:48 +00:00
|
|
|
|
else
|
2017-06-03 23:09:47 +00:00
|
|
|
|
{
|
|
|
|
|
if(highNibble > 0x9) carry_result_ = Flag::Carry;
|
|
|
|
|
}
|
2017-05-26 02:17:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
if(subtract_flag_)
|
2017-05-26 02:17:48 +00:00
|
|
|
|
{
|
2017-06-03 23:09:47 +00:00
|
|
|
|
a_ -= amountToAdd;
|
|
|
|
|
half_carry_result_ = ((half_carry_result_ & Flag::HalfCarry) && lowNibble < 0x6) ? Flag::HalfCarry : 0;
|
2017-05-26 02:17:48 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-06-03 23:09:47 +00:00
|
|
|
|
a_ += amountToAdd;
|
|
|
|
|
half_carry_result_ = (lowNibble > 0x9) ? Flag::HalfCarry : 0;
|
2017-05-26 02:17:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
sign_result_ = zero_result_ = bit53_result_ = a_;
|
2017-05-26 02:17:48 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
set_parity(a_);
|
|
|
|
|
} break;
|
2017-05-20 02:57:43 +00:00
|
|
|
|
|
2017-05-21 13:43:17 +00:00
|
|
|
|
#pragma mark - 16-bit arithmetic
|
2017-05-20 02:57:43 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::ADD16: {
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full = *(uint16_t *)operation->source;
|
|
|
|
|
uint16_t sourceValue = memptr_.full;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
uint16_t destinationValue = *(uint16_t *)operation->destination;
|
|
|
|
|
int result = sourceValue + destinationValue;
|
|
|
|
|
int halfResult = (sourceValue&0xfff) + (destinationValue&0xfff);
|
|
|
|
|
|
|
|
|
|
bit53_result_ = (uint8_t)(result >> 8);
|
2017-06-04 22:40:59 +00:00
|
|
|
|
carry_result_ = (uint8_t)(result >> 16);
|
|
|
|
|
half_carry_result_ = (uint8_t)(halfResult >> 8);
|
2017-06-03 23:09:47 +00:00
|
|
|
|
subtract_flag_ = 0;
|
|
|
|
|
|
|
|
|
|
*(uint16_t *)operation->destination = (uint16_t)result;
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::ADC16: {
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full = *(uint16_t *)operation->source;
|
|
|
|
|
uint16_t sourceValue = memptr_.full;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
uint16_t destinationValue = *(uint16_t *)operation->destination;
|
|
|
|
|
int result = sourceValue + destinationValue + (carry_result_ & Flag::Carry);
|
|
|
|
|
int halfResult = (sourceValue&0xfff) + (destinationValue&0xfff) + (carry_result_ & Flag::Carry);
|
|
|
|
|
|
|
|
|
|
int overflow = (result ^ destinationValue) & ~(destinationValue ^ sourceValue);
|
|
|
|
|
|
|
|
|
|
bit53_result_ =
|
|
|
|
|
sign_result_ = (uint8_t)(result >> 8);
|
|
|
|
|
zero_result_ = (uint8_t)(result | sign_result_);
|
|
|
|
|
subtract_flag_ = 0;
|
2017-06-04 22:40:59 +00:00
|
|
|
|
carry_result_ = (uint8_t)(result >> 16);
|
|
|
|
|
half_carry_result_ = (uint8_t)(halfResult >> 8);
|
|
|
|
|
parity_overflow_result_ = (uint8_t)(overflow >> 13);
|
2017-06-03 23:09:47 +00:00
|
|
|
|
|
|
|
|
|
*(uint16_t *)operation->destination = (uint16_t)result;
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::SBC16: {
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full = *(uint16_t *)operation->source;
|
|
|
|
|
uint16_t sourceValue = memptr_.full;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
uint16_t destinationValue = *(uint16_t *)operation->destination;
|
|
|
|
|
int result = destinationValue - sourceValue - (carry_result_ & Flag::Carry);
|
|
|
|
|
int halfResult = (destinationValue&0xfff) - (sourceValue&0xfff) - (carry_result_ & Flag::Carry);
|
|
|
|
|
|
|
|
|
|
// subtraction, so parity rules are:
|
|
|
|
|
// signs of operands were different,
|
|
|
|
|
// sign of result is different
|
|
|
|
|
int overflow = (result ^ destinationValue) & (sourceValue ^ destinationValue);
|
|
|
|
|
|
|
|
|
|
bit53_result_ =
|
|
|
|
|
sign_result_ = (uint8_t)(result >> 8);
|
|
|
|
|
zero_result_ = (uint8_t)(result | sign_result_);
|
|
|
|
|
subtract_flag_ = Flag::Subtract;
|
2017-06-04 22:40:59 +00:00
|
|
|
|
carry_result_ = (uint8_t)(result >> 16);
|
|
|
|
|
half_carry_result_ = (uint8_t)(halfResult >> 8);
|
|
|
|
|
parity_overflow_result_ = (uint8_t)(overflow >> 13);
|
2017-06-03 23:09:47 +00:00
|
|
|
|
|
|
|
|
|
*(uint16_t *)operation->destination = (uint16_t)result;
|
|
|
|
|
} break;
|
2017-05-21 13:43:17 +00:00
|
|
|
|
|
2017-05-28 18:55:14 +00:00
|
|
|
|
#pragma mark - Conditionals
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::TestNZ: if(!zero_result_) { advance_operation(); } break;
|
|
|
|
|
case MicroOp::TestZ: if(zero_result_) { advance_operation(); } break;
|
|
|
|
|
case MicroOp::TestNC: if(carry_result_ & Flag::Carry) { advance_operation(); } break;
|
|
|
|
|
case MicroOp::TestC: if(!(carry_result_ & Flag::Carry)) { advance_operation(); } break;
|
|
|
|
|
case MicroOp::TestPO: if(parity_overflow_result_ & Flag::Parity) { advance_operation(); } break;
|
|
|
|
|
case MicroOp::TestPE: if(!(parity_overflow_result_ & Flag::Parity)) { advance_operation(); } break;
|
|
|
|
|
case MicroOp::TestP: if(sign_result_ & Flag::Sign) { advance_operation(); } break;
|
|
|
|
|
case MicroOp::TestM: if(!(sign_result_ & Flag::Sign)) { advance_operation(); } break;
|
2017-05-20 02:57:43 +00:00
|
|
|
|
|
2017-05-28 18:55:14 +00:00
|
|
|
|
#pragma mark - Exchange
|
|
|
|
|
|
|
|
|
|
#define swap(a, b) temp = a.full; a.full = b.full; b.full = temp;
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::ExDEHL: {
|
|
|
|
|
uint16_t temp;
|
|
|
|
|
swap(de_, hl_);
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::ExAFAFDash: {
|
|
|
|
|
uint8_t a = a_;
|
|
|
|
|
uint8_t f = get_flags();
|
|
|
|
|
set_flags(afDash_.bytes.low);
|
|
|
|
|
a_ = afDash_.bytes.high;
|
|
|
|
|
afDash_.bytes.high = a;
|
|
|
|
|
afDash_.bytes.low = f;
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::EXX: {
|
|
|
|
|
uint16_t temp;
|
|
|
|
|
swap(de_, deDash_);
|
|
|
|
|
swap(bc_, bcDash_);
|
|
|
|
|
swap(hl_, hlDash_);
|
|
|
|
|
} break;
|
2017-05-28 18:55:14 +00:00
|
|
|
|
|
|
|
|
|
#undef swap
|
|
|
|
|
|
2017-05-27 19:39:22 +00:00
|
|
|
|
#pragma mark - Repetition
|
2017-05-21 13:52:19 +00:00
|
|
|
|
|
2017-05-29 14:12:33 +00:00
|
|
|
|
#define REPEAT(test) \
|
|
|
|
|
if(test) { \
|
2017-05-29 12:54:00 +00:00
|
|
|
|
pc_.full -= 2; \
|
|
|
|
|
} else { \
|
2017-06-01 00:15:56 +00:00
|
|
|
|
advance_operation(); \
|
2017-05-29 12:54:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-31 02:12:10 +00:00
|
|
|
|
#define LDxR_STEP(dir) \
|
2017-05-29 12:24:10 +00:00
|
|
|
|
bc_.full--; \
|
2017-05-31 02:12:10 +00:00
|
|
|
|
de_.full += dir; \
|
|
|
|
|
hl_.full += dir; \
|
2017-06-04 18:18:04 +00:00
|
|
|
|
uint8_t sum = a_ + temp8_; \
|
2017-06-04 22:40:59 +00:00
|
|
|
|
bit53_result_ = (uint8_t)((sum&0x8) | ((sum & 0x02) << 4)); \
|
2017-05-29 12:24:10 +00:00
|
|
|
|
subtract_flag_ = 0; \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
half_carry_result_ = 0; \
|
|
|
|
|
parity_overflow_result_ = bc_.full ? Flag::Parity : 0;
|
2017-05-29 12:24:10 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::LDDR: {
|
|
|
|
|
LDxR_STEP(-1);
|
|
|
|
|
REPEAT(bc_.full);
|
|
|
|
|
} break;
|
2017-05-31 02:12:10 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::LDIR: {
|
|
|
|
|
LDxR_STEP(1);
|
|
|
|
|
REPEAT(bc_.full);
|
|
|
|
|
} break;
|
2017-05-21 01:58:24 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::LDD: {
|
|
|
|
|
LDxR_STEP(-1);
|
|
|
|
|
} break;
|
2017-05-31 02:12:10 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::LDI: {
|
|
|
|
|
LDxR_STEP(1);
|
|
|
|
|
} break;
|
2017-05-29 03:55:54 +00:00
|
|
|
|
|
2017-05-29 12:24:10 +00:00
|
|
|
|
#undef LDxR_STEP
|
|
|
|
|
|
2017-05-31 02:12:10 +00:00
|
|
|
|
#define CPxR_STEP(dir) \
|
|
|
|
|
hl_.full += dir; \
|
2017-05-29 12:54:00 +00:00
|
|
|
|
bc_.full--; \
|
|
|
|
|
\
|
|
|
|
|
uint8_t result = a_ - temp8_; \
|
|
|
|
|
uint8_t halfResult = (a_&0xf) - (temp8_&0xf); \
|
|
|
|
|
\
|
2017-05-30 02:23:19 +00:00
|
|
|
|
parity_overflow_result_ = bc_.full ? Flag::Parity : 0; \
|
|
|
|
|
half_carry_result_ = halfResult; \
|
2017-05-29 12:54:00 +00:00
|
|
|
|
subtract_flag_ = Flag::Subtract; \
|
2017-06-04 18:59:18 +00:00
|
|
|
|
sign_result_ = zero_result_ = result; \
|
|
|
|
|
\
|
|
|
|
|
result -= (halfResult >> 4)&1; \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
bit53_result_ = (uint8_t)((result&0x8) | ((result&0x2) << 4)); \
|
2017-05-29 12:54:00 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::CPDR: {
|
|
|
|
|
CPxR_STEP(-1);
|
|
|
|
|
REPEAT(bc_.full && sign_result_);
|
|
|
|
|
} break;
|
2017-05-31 02:12:10 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::CPIR: {
|
|
|
|
|
CPxR_STEP(1);
|
|
|
|
|
REPEAT(bc_.full && sign_result_);
|
|
|
|
|
} break;
|
2017-05-29 03:55:54 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::CPD: {
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full--;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
CPxR_STEP(-1);
|
|
|
|
|
} break;
|
2017-05-31 02:12:10 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::CPI: {
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full++;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
CPxR_STEP(1);
|
|
|
|
|
} break;
|
2017-05-29 03:55:54 +00:00
|
|
|
|
|
2017-05-29 12:54:00 +00:00
|
|
|
|
#undef CPxR_STEP
|
2017-05-29 03:55:54 +00:00
|
|
|
|
|
2017-05-31 02:12:10 +00:00
|
|
|
|
#define INxR_STEP(dir) \
|
2017-05-29 14:12:33 +00:00
|
|
|
|
bc_.bytes.high--; \
|
2017-05-31 02:12:10 +00:00
|
|
|
|
hl_.full += dir; \
|
2017-05-29 14:12:33 +00:00
|
|
|
|
\
|
2017-05-30 02:23:19 +00:00
|
|
|
|
sign_result_ = zero_result_ = bit53_result_ = bc_.bytes.high; \
|
2017-05-29 14:12:33 +00:00
|
|
|
|
subtract_flag_ = (temp8_ >> 6) & Flag::Subtract; \
|
|
|
|
|
\
|
2017-05-31 02:12:10 +00:00
|
|
|
|
int next_bc = bc_.bytes.low + dir; \
|
2017-05-29 14:12:33 +00:00
|
|
|
|
int summation = temp8_ + (next_bc&0xff); \
|
|
|
|
|
\
|
|
|
|
|
if(summation > 0xff) { \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
carry_result_ = Flag::Carry; \
|
|
|
|
|
half_carry_result_ = Flag::HalfCarry; \
|
2017-05-29 14:12:33 +00:00
|
|
|
|
} else { \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
carry_result_ = 0; \
|
|
|
|
|
half_carry_result_ = 0; \
|
2017-05-29 14:12:33 +00:00
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
summation = (summation&7) ^ bc_.bytes.high; \
|
|
|
|
|
set_parity(summation);
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::INDR: {
|
|
|
|
|
INxR_STEP(-1);
|
|
|
|
|
REPEAT(bc_.bytes.high);
|
|
|
|
|
} break;
|
2017-05-31 02:12:10 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::INIR: {
|
|
|
|
|
INxR_STEP(1);
|
|
|
|
|
REPEAT(bc_.bytes.high);
|
|
|
|
|
} break;
|
2017-05-29 14:12:33 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::IND: {
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full = bc_.full - 1;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
INxR_STEP(-1);
|
|
|
|
|
} break;
|
2017-05-31 02:12:10 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::INI: {
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full = bc_.full + 1;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
INxR_STEP(1);
|
|
|
|
|
} break;
|
2017-05-29 14:12:33 +00:00
|
|
|
|
|
|
|
|
|
#undef INxR_STEP
|
|
|
|
|
|
2017-05-31 02:12:10 +00:00
|
|
|
|
#define OUTxR_STEP(dir) \
|
2017-05-29 14:28:04 +00:00
|
|
|
|
bc_.bytes.high--; \
|
2017-05-31 02:12:10 +00:00
|
|
|
|
hl_.full += dir; \
|
2017-05-29 14:28:04 +00:00
|
|
|
|
\
|
2017-05-30 02:23:19 +00:00
|
|
|
|
sign_result_ = zero_result_ = bit53_result_ = bc_.bytes.high; \
|
2017-05-29 14:28:04 +00:00
|
|
|
|
subtract_flag_ = (temp8_ >> 6) & Flag::Subtract; \
|
|
|
|
|
\
|
|
|
|
|
int summation = temp8_ + hl_.bytes.low; \
|
|
|
|
|
if(summation > 0xff) { \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
carry_result_ = Flag::Carry; \
|
|
|
|
|
half_carry_result_ = Flag::HalfCarry; \
|
2017-05-29 14:28:04 +00:00
|
|
|
|
} else { \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
carry_result_ = half_carry_result_ = 0; \
|
2017-05-29 14:28:04 +00:00
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
summation = (summation&7) ^ bc_.bytes.high; \
|
|
|
|
|
set_parity(summation);
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::OUT_R:
|
|
|
|
|
REPEAT(bc_.bytes.high);
|
|
|
|
|
break;
|
2017-05-29 14:28:04 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::OUTD: {
|
|
|
|
|
OUTxR_STEP(-1);
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full = bc_.full - 1;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
} break;
|
2017-05-31 02:12:10 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::OUTI: {
|
|
|
|
|
OUTxR_STEP(1);
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full = bc_.full + 1;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
} break;
|
2017-05-29 14:28:04 +00:00
|
|
|
|
|
|
|
|
|
#undef OUTxR_STEP
|
|
|
|
|
|
2017-05-27 19:39:22 +00:00
|
|
|
|
#pragma mark - Bit Manipulation
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::BIT: {
|
|
|
|
|
uint8_t result = *(uint8_t *)operation->source & (1 << ((operation_ >> 3)&7));
|
2017-05-27 19:39:22 +00:00
|
|
|
|
|
2017-06-04 19:07:07 +00:00
|
|
|
|
if(current_instruction_page_->is_indexed || ((operation_&0x08) == 7)) {
|
|
|
|
|
bit53_result_ = memptr_.bytes.high;
|
2017-06-04 18:13:38 +00:00
|
|
|
|
} else {
|
|
|
|
|
bit53_result_ = *(uint8_t *)operation->source;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
sign_result_ = zero_result_ = result;
|
|
|
|
|
half_carry_result_ = Flag::HalfCarry;
|
|
|
|
|
subtract_flag_ = 0;
|
|
|
|
|
parity_overflow_result_ = result ? 0 : Flag::Parity;
|
|
|
|
|
} break;
|
2017-05-27 19:39:22 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::RES:
|
|
|
|
|
*(uint8_t *)operation->source &= ~(1 << ((operation_ >> 3)&7));
|
|
|
|
|
break;
|
2017-05-27 19:39:22 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::SET:
|
|
|
|
|
*(uint8_t *)operation->source |= (1 << ((operation_ >> 3)&7));
|
|
|
|
|
break;
|
2017-05-27 19:39:22 +00:00
|
|
|
|
|
|
|
|
|
#pragma mark - Rotation and shifting
|
2017-05-21 13:52:19 +00:00
|
|
|
|
|
2017-05-28 04:12:42 +00:00
|
|
|
|
#define set_rotate_flags() \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
bit53_result_ = a_; \
|
|
|
|
|
carry_result_ = new_carry; \
|
|
|
|
|
subtract_flag_ = half_carry_result_ = 0;
|
2017-05-28 04:12:42 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::RLA: {
|
|
|
|
|
uint8_t new_carry = a_ >> 7;
|
|
|
|
|
a_ = (uint8_t)((a_ << 1) | (carry_result_ & Flag::Carry));
|
|
|
|
|
set_rotate_flags();
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::RRA: {
|
|
|
|
|
uint8_t new_carry = a_ & 1;
|
|
|
|
|
a_ = (uint8_t)((a_ >> 1) | (carry_result_ << 7));
|
|
|
|
|
set_rotate_flags();
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::RLCA: {
|
|
|
|
|
uint8_t new_carry = a_ >> 7;
|
|
|
|
|
a_ = (uint8_t)((a_ << 1) | new_carry);
|
|
|
|
|
set_rotate_flags();
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::RRCA: {
|
|
|
|
|
uint8_t new_carry = a_ & 1;
|
|
|
|
|
a_ = (uint8_t)((a_ >> 1) | (new_carry << 7));
|
|
|
|
|
set_rotate_flags();
|
|
|
|
|
} break;
|
2017-05-21 13:52:19 +00:00
|
|
|
|
|
2017-05-28 04:12:42 +00:00
|
|
|
|
#undef set_rotate_flags
|
|
|
|
|
|
|
|
|
|
#define set_shift_flags() \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
sign_result_ = zero_result_ = bit53_result_ = *(uint8_t *)operation->source; \
|
2017-05-28 04:12:42 +00:00
|
|
|
|
set_parity(sign_result_); \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
half_carry_result_ = 0; \
|
2017-05-28 04:12:42 +00:00
|
|
|
|
subtract_flag_ = 0;
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::RLC:
|
|
|
|
|
carry_result_ = *(uint8_t *)operation->source >> 7;
|
|
|
|
|
*(uint8_t *)operation->source = (uint8_t)((*(uint8_t *)operation->source << 1) | carry_result_);
|
|
|
|
|
set_shift_flags();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::RRC:
|
|
|
|
|
carry_result_ = *(uint8_t *)operation->source;
|
|
|
|
|
*(uint8_t *)operation->source = (uint8_t)((*(uint8_t *)operation->source >> 1) | (carry_result_ << 7));
|
|
|
|
|
set_shift_flags();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::RL: {
|
|
|
|
|
uint8_t next_carry = *(uint8_t *)operation->source >> 7;
|
|
|
|
|
*(uint8_t *)operation->source = (uint8_t)((*(uint8_t *)operation->source << 1) | (carry_result_ & Flag::Carry));
|
|
|
|
|
carry_result_ = next_carry;
|
|
|
|
|
set_shift_flags();
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::RR: {
|
|
|
|
|
uint8_t next_carry = *(uint8_t *)operation->source;
|
|
|
|
|
*(uint8_t *)operation->source = (uint8_t)((*(uint8_t *)operation->source >> 1) | (carry_result_ << 7));
|
|
|
|
|
carry_result_ = next_carry;
|
|
|
|
|
set_shift_flags();
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::SLA:
|
|
|
|
|
carry_result_ = *(uint8_t *)operation->source >> 7;
|
|
|
|
|
*(uint8_t *)operation->source = (uint8_t)(*(uint8_t *)operation->source << 1);
|
|
|
|
|
set_shift_flags();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::SRA:
|
|
|
|
|
carry_result_ = *(uint8_t *)operation->source;
|
|
|
|
|
*(uint8_t *)operation->source = (uint8_t)((*(uint8_t *)operation->source >> 1) | (*(uint8_t *)operation->source & 0x80));
|
|
|
|
|
set_shift_flags();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::SLL:
|
|
|
|
|
carry_result_ = *(uint8_t *)operation->source >> 7;
|
|
|
|
|
*(uint8_t *)operation->source = (uint8_t)(*(uint8_t *)operation->source << 1) | 1;
|
|
|
|
|
set_shift_flags();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::SRL:
|
|
|
|
|
carry_result_ = *(uint8_t *)operation->source;
|
|
|
|
|
*(uint8_t *)operation->source = (uint8_t)((*(uint8_t *)operation->source >> 1));
|
|
|
|
|
set_shift_flags();
|
|
|
|
|
break;
|
2017-05-27 19:39:22 +00:00
|
|
|
|
|
2017-05-28 04:12:42 +00:00
|
|
|
|
#undef set_shift_flags
|
2017-05-27 19:39:22 +00:00
|
|
|
|
|
2017-05-28 20:46:27 +00:00
|
|
|
|
#define set_decimal_rotate_flags() \
|
|
|
|
|
subtract_flag_ = 0; \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
half_carry_result_ = 0; \
|
2017-05-28 20:46:27 +00:00
|
|
|
|
set_parity(a_); \
|
2017-05-30 02:23:19 +00:00
|
|
|
|
bit53_result_ = zero_result_ = sign_result_ = a_;
|
2017-05-28 20:46:27 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::RRD: {
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full = hl_.full + 1;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
uint8_t low_nibble = a_ & 0xf;
|
|
|
|
|
a_ = (a_ & 0xf0) | (temp8_ & 0xf);
|
2017-06-04 22:40:59 +00:00
|
|
|
|
temp8_ = (uint8_t)((temp8_ >> 4) | (low_nibble << 4));
|
2017-06-03 23:09:47 +00:00
|
|
|
|
set_decimal_rotate_flags();
|
|
|
|
|
} break;
|
2017-05-28 20:46:27 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::RLD: {
|
2017-06-04 19:39:37 +00:00
|
|
|
|
memptr_.full = hl_.full + 1;
|
2017-06-03 23:09:47 +00:00
|
|
|
|
uint8_t low_nibble = a_ & 0xf;
|
|
|
|
|
a_ = (a_ & 0xf0) | (temp8_ >> 4);
|
2017-06-04 22:40:59 +00:00
|
|
|
|
temp8_ = (uint8_t)((temp8_ << 4) | low_nibble);
|
2017-06-03 23:09:47 +00:00
|
|
|
|
set_decimal_rotate_flags();
|
|
|
|
|
} break;
|
2017-05-28 20:46:27 +00:00
|
|
|
|
|
|
|
|
|
#undef set_decimal_rotate_flags
|
|
|
|
|
|
|
|
|
|
|
2017-05-21 16:53:17 +00:00
|
|
|
|
#pragma mark - Interrupt state
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::EI:
|
|
|
|
|
iff1_ = iff2_ = true;
|
|
|
|
|
if(irq_line_) request_status_ |= Interrupt::IRQ;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::DI:
|
|
|
|
|
iff1_ = iff2_ = false;
|
|
|
|
|
request_status_ &= ~Interrupt::IRQ;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::IM:
|
|
|
|
|
switch(operation_ & 0x18) {
|
|
|
|
|
case 0x00: interrupt_mode_ = 0; break;
|
|
|
|
|
case 0x08: interrupt_mode_ = 0; break; // IM 0/1
|
|
|
|
|
case 0x10: interrupt_mode_ = 1; break;
|
|
|
|
|
case 0x18: interrupt_mode_ = 2; break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2017-05-28 19:55:21 +00:00
|
|
|
|
|
2017-05-28 18:50:51 +00:00
|
|
|
|
#pragma mark - Input
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::SetInFlags:
|
|
|
|
|
subtract_flag_ = half_carry_result_ = 0;
|
|
|
|
|
sign_result_ = zero_result_ = bit53_result_ = *(uint8_t *)operation->source;
|
|
|
|
|
set_parity(sign_result_);
|
|
|
|
|
break;
|
2017-05-28 18:50:51 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::SetAFlags:
|
|
|
|
|
subtract_flag_ = half_carry_result_ = 0;
|
|
|
|
|
parity_overflow_result_ = iff2_ ? Flag::Parity : 0;
|
|
|
|
|
sign_result_ = zero_result_ = bit53_result_ = a_;
|
|
|
|
|
break;
|
2017-05-28 20:32:10 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::SetZero:
|
|
|
|
|
temp8_ = 0;
|
|
|
|
|
break;
|
2017-05-28 18:50:51 +00:00
|
|
|
|
|
2017-05-29 15:54:27 +00:00
|
|
|
|
#pragma mark - Special-case Flow
|
2017-05-28 20:07:25 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::BeginIRQMode0:
|
|
|
|
|
pc_increment_ = 0; // deliberate fallthrough
|
|
|
|
|
case MicroOp::BeginIRQ:
|
|
|
|
|
iff2_ = iff1_ = false;
|
|
|
|
|
request_status_ &= ~Interrupt::IRQ;
|
|
|
|
|
temp16_.full = 0x38;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::BeginNMI:
|
|
|
|
|
iff2_ = iff1_;
|
|
|
|
|
iff1_ = false;
|
|
|
|
|
request_status_ &= ~Interrupt::IRQ;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::JumpTo66:
|
|
|
|
|
pc_.full = 0x66;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::RETN:
|
|
|
|
|
iff1_ = iff2_;
|
|
|
|
|
if(irq_line_ && iff1_) request_status_ |= Interrupt::IRQ;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MicroOp::HALT:
|
|
|
|
|
halt_mask_ = 0x00;
|
|
|
|
|
break;
|
2017-05-29 15:54:27 +00:00
|
|
|
|
|
2017-06-02 02:31:04 +00:00
|
|
|
|
#pragma mark - Interrupt handling
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::Reset:
|
|
|
|
|
iff1_ = iff2_ = false;
|
|
|
|
|
interrupt_mode_ = 0;
|
|
|
|
|
pc_.full = 0;
|
|
|
|
|
sp_.full = 0xffff;
|
|
|
|
|
a_ = 0xff;
|
|
|
|
|
set_flags(0xff);
|
|
|
|
|
i_ = r_ = 0;
|
|
|
|
|
break;
|
2017-06-02 02:31:04 +00:00
|
|
|
|
|
2017-05-21 13:52:19 +00:00
|
|
|
|
#pragma mark - Internal bookkeeping
|
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::SetInstructionPage:
|
|
|
|
|
current_instruction_page_ = (InstructionPage *)operation->source;
|
|
|
|
|
scheduled_program_counter_ = current_instruction_page_->fetch_decode_execute_data;
|
|
|
|
|
break;
|
2017-05-20 21:04:25 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::CalculateIndexAddress:
|
2017-06-04 22:40:59 +00:00
|
|
|
|
memptr_.full = (uint16_t)(*(uint16_t *)operation->source + (int8_t)temp8_);
|
2017-06-03 23:09:47 +00:00
|
|
|
|
break;
|
2017-05-21 23:26:40 +00:00
|
|
|
|
|
2017-06-03 23:09:47 +00:00
|
|
|
|
case MicroOp::IndexedPlaceHolder:
|
|
|
|
|
printf("Hit placeholder!!!\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-26 02:41:05 +00:00
|
|
|
|
#undef set_parity
|
2017-06-03 23:09:47 +00:00
|
|
|
|
}
|
2017-05-26 02:41:05 +00:00
|
|
|
|
|
2017-05-16 02:25:52 +00:00
|
|
|
|
}
|
2017-05-15 11:59:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 11:55:53 +00:00
|
|
|
|
/*!
|
|
|
|
|
Called to announce the end of a run_for_cycles period, allowing deferred work to take place.
|
|
|
|
|
|
2017-05-15 11:59:21 +00:00
|
|
|
|
Users of the Z80 template may override this.
|
2017-05-15 11:55:53 +00:00
|
|
|
|
*/
|
|
|
|
|
void flush() {}
|
|
|
|
|
|
2017-05-30 23:24:58 +00:00
|
|
|
|
int perform_machine_cycle(const MachineCycle &cycle) {
|
2017-05-17 02:19:40 +00:00
|
|
|
|
return 0;
|
2017-05-16 02:25:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 11:55:53 +00:00
|
|
|
|
/*!
|
|
|
|
|
Gets the flags register.
|
|
|
|
|
|
|
|
|
|
@see set_flags
|
|
|
|
|
|
|
|
|
|
@returns The current value of the flags register.
|
|
|
|
|
*/
|
|
|
|
|
uint8_t get_flags() {
|
2017-05-26 01:23:38 +00:00
|
|
|
|
uint8_t result =
|
2017-05-15 11:55:53 +00:00
|
|
|
|
(sign_result_ & Flag::Sign) |
|
2017-05-20 02:57:43 +00:00
|
|
|
|
(zero_result_ ? 0 : Flag::Zero) |
|
2017-05-30 02:23:19 +00:00
|
|
|
|
(bit53_result_ & (Flag::Bit5 | Flag::Bit3)) |
|
|
|
|
|
(half_carry_result_ & Flag::HalfCarry) |
|
|
|
|
|
(parity_overflow_result_ & Flag::Parity) |
|
2017-05-20 02:57:43 +00:00
|
|
|
|
subtract_flag_ |
|
2017-05-30 02:23:19 +00:00
|
|
|
|
(carry_result_ & Flag::Carry);
|
2017-05-26 01:23:38 +00:00
|
|
|
|
return result;
|
2017-05-15 11:55:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the flags register.
|
|
|
|
|
|
|
|
|
|
@see set_flags
|
|
|
|
|
|
|
|
|
|
@param flags The new value of the flags register.
|
|
|
|
|
*/
|
|
|
|
|
void set_flags(uint8_t flags) {
|
|
|
|
|
sign_result_ = flags;
|
2017-05-20 02:57:43 +00:00
|
|
|
|
zero_result_ = (flags & Flag::Zero) ^ Flag::Zero;
|
2017-05-30 02:23:19 +00:00
|
|
|
|
bit53_result_ = flags;
|
|
|
|
|
half_carry_result_ = flags;
|
|
|
|
|
parity_overflow_result_ = flags;
|
2017-05-15 11:55:53 +00:00
|
|
|
|
subtract_flag_ = flags & Flag::Subtract;
|
2017-05-30 02:23:19 +00:00
|
|
|
|
carry_result_ = flags;
|
2017-05-15 11:55:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Gets the value of a register.
|
|
|
|
|
|
|
|
|
|
@see set_value_of_register
|
|
|
|
|
|
|
|
|
|
@param r The register to set.
|
|
|
|
|
@returns The value of the register. 8-bit registers will be returned as unsigned.
|
|
|
|
|
*/
|
|
|
|
|
uint16_t get_value_of_register(Register r) {
|
|
|
|
|
switch (r) {
|
|
|
|
|
case Register::ProgramCounter: return pc_.full;
|
|
|
|
|
case Register::StackPointer: return sp_.full;
|
|
|
|
|
|
|
|
|
|
case Register::A: return a_;
|
|
|
|
|
case Register::Flags: return get_flags();
|
|
|
|
|
case Register::AF: return (uint16_t)((a_ << 8) | get_flags());
|
|
|
|
|
case Register::B: return bc_.bytes.high;
|
|
|
|
|
case Register::C: return bc_.bytes.low;
|
|
|
|
|
case Register::BC: return bc_.full;
|
|
|
|
|
case Register::D: return de_.bytes.high;
|
|
|
|
|
case Register::E: return de_.bytes.low;
|
|
|
|
|
case Register::DE: return de_.full;
|
|
|
|
|
case Register::H: return hl_.bytes.high;
|
|
|
|
|
case Register::L: return hl_.bytes.low;
|
|
|
|
|
case Register::HL: return hl_.full;
|
|
|
|
|
|
|
|
|
|
case Register::ADash: return afDash_.bytes.high;
|
|
|
|
|
case Register::FlagsDash: return afDash_.bytes.low;
|
|
|
|
|
case Register::AFDash: return afDash_.full;
|
|
|
|
|
case Register::BDash: return bcDash_.bytes.high;
|
|
|
|
|
case Register::CDash: return bcDash_.bytes.low;
|
|
|
|
|
case Register::BCDash: return bcDash_.full;
|
|
|
|
|
case Register::DDash: return deDash_.bytes.high;
|
|
|
|
|
case Register::EDash: return deDash_.bytes.low;
|
|
|
|
|
case Register::DEDash: return deDash_.full;
|
|
|
|
|
case Register::HDash: return hlDash_.bytes.high;
|
|
|
|
|
case Register::LDash: return hlDash_.bytes.low;
|
|
|
|
|
case Register::HLDash: return hlDash_.full;
|
|
|
|
|
|
|
|
|
|
case Register::IXh: return ix_.bytes.high;
|
|
|
|
|
case Register::IXl: return ix_.bytes.low;
|
|
|
|
|
case Register::IX: return ix_.full;
|
|
|
|
|
case Register::IYh: return iy_.bytes.high;
|
|
|
|
|
case Register::IYl: return iy_.bytes.low;
|
|
|
|
|
case Register::IY: return iy_.full;
|
|
|
|
|
|
|
|
|
|
case Register::R: return r_;
|
|
|
|
|
case Register::I: return i_;
|
2017-06-04 22:32:23 +00:00
|
|
|
|
case Register::Refresh: return (uint16_t)(r_ | (i_ << 8));
|
2017-05-15 11:55:53 +00:00
|
|
|
|
|
2017-05-22 23:14:46 +00:00
|
|
|
|
case Register::IFF1: return iff1_ ? 1 : 0;
|
|
|
|
|
case Register::IFF2: return iff2_ ? 1 : 0;
|
2017-06-04 22:32:23 +00:00
|
|
|
|
case Register::IM: return (uint16_t)interrupt_mode_;
|
2017-05-22 23:14:46 +00:00
|
|
|
|
|
2017-05-15 11:55:53 +00:00
|
|
|
|
default: return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the value of a register.
|
|
|
|
|
|
|
|
|
|
@see get_value_of_register
|
|
|
|
|
|
|
|
|
|
@param r The register to set.
|
|
|
|
|
@param value The value to set. If the register is only 8 bit, the value will be truncated.
|
|
|
|
|
*/
|
|
|
|
|
void set_value_of_register(Register r, uint16_t value) {
|
|
|
|
|
switch (r) {
|
|
|
|
|
case Register::ProgramCounter: pc_.full = value; break;
|
|
|
|
|
case Register::StackPointer: sp_.full = value; break;
|
|
|
|
|
|
|
|
|
|
case Register::A: a_ = (uint8_t)value; break;
|
|
|
|
|
case Register::AF: a_ = (uint8_t)(value >> 8); // deliberate fallthrough...
|
|
|
|
|
case Register::Flags: set_flags((uint8_t)value); break;
|
|
|
|
|
|
|
|
|
|
case Register::B: bc_.bytes.high = (uint8_t)value; break;
|
|
|
|
|
case Register::C: bc_.bytes.low = (uint8_t)value; break;
|
|
|
|
|
case Register::BC: bc_.full = value; break;
|
|
|
|
|
case Register::D: de_.bytes.high = (uint8_t)value; break;
|
|
|
|
|
case Register::E: de_.bytes.low = (uint8_t)value; break;
|
|
|
|
|
case Register::DE: de_.full = value; break;
|
|
|
|
|
case Register::H: hl_.bytes.high = (uint8_t)value; break;
|
|
|
|
|
case Register::L: hl_.bytes.low = (uint8_t)value; break;
|
|
|
|
|
case Register::HL: hl_.full = value; break;
|
|
|
|
|
|
|
|
|
|
case Register::ADash: afDash_.bytes.high = (uint8_t)value; break;
|
|
|
|
|
case Register::FlagsDash: afDash_.bytes.low = (uint8_t)value; break;
|
|
|
|
|
case Register::AFDash: afDash_.full = value; break;
|
|
|
|
|
case Register::BDash: bcDash_.bytes.high = (uint8_t)value; break;
|
|
|
|
|
case Register::CDash: bcDash_.bytes.low = (uint8_t)value; break;
|
|
|
|
|
case Register::BCDash: bcDash_.full = value; break;
|
|
|
|
|
case Register::DDash: deDash_.bytes.high = (uint8_t)value; break;
|
|
|
|
|
case Register::EDash: deDash_.bytes.low = (uint8_t)value; break;
|
|
|
|
|
case Register::DEDash: deDash_.full = value; break;
|
|
|
|
|
case Register::HDash: hlDash_.bytes.high = (uint8_t)value; break;
|
|
|
|
|
case Register::LDash: hlDash_.bytes.low = (uint8_t)value; break;
|
|
|
|
|
case Register::HLDash: hlDash_.full = value; break;
|
|
|
|
|
|
|
|
|
|
case Register::IXh: ix_.bytes.high = (uint8_t)value; break;
|
|
|
|
|
case Register::IXl: ix_.bytes.low = (uint8_t)value; break;
|
|
|
|
|
case Register::IX: ix_.full = value; break;
|
|
|
|
|
case Register::IYh: iy_.bytes.high = (uint8_t)value; break;
|
|
|
|
|
case Register::IYl: iy_.bytes.low = (uint8_t)value; break;
|
|
|
|
|
case Register::IY: iy_.full = value; break;
|
|
|
|
|
|
|
|
|
|
case Register::R: r_ = (uint8_t)value; break;
|
|
|
|
|
case Register::I: i_ = (uint8_t)value; break;
|
2017-06-04 22:32:23 +00:00
|
|
|
|
case Register::Refresh: r_ = (uint8_t)value; i_ = (uint8_t)(value >> 8); break;
|
2017-05-15 11:55:53 +00:00
|
|
|
|
|
2017-05-22 23:14:46 +00:00
|
|
|
|
case Register::IFF1: iff1_ = !!value; break;
|
|
|
|
|
case Register::IFF2: iff2_ = !!value; break;
|
2017-06-03 22:58:13 +00:00
|
|
|
|
case Register::IM: interrupt_mode_ = value % 3; break;
|
2017-05-22 23:14:46 +00:00
|
|
|
|
|
2017-05-15 11:55:53 +00:00
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-29 15:54:27 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Gets the value of the HALT output line.
|
|
|
|
|
*/
|
|
|
|
|
bool get_halt_line() {
|
2017-05-31 02:15:43 +00:00
|
|
|
|
return halt_mask_ == 0x00;
|
2017-05-29 15:54:27 +00:00
|
|
|
|
}
|
2017-06-01 02:51:32 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
2017-06-02 00:34:52 +00:00
|
|
|
|
Sets the logical value of the interrupt line.
|
2017-06-11 17:31:02 +00:00
|
|
|
|
|
|
|
|
|
@param offset If called while within perform_machine_cycle this may be a value indicating
|
|
|
|
|
how many cycles before now the line changed state. The value may not be longer than the
|
|
|
|
|
current machine cycle. If called at any other time, this must be zero.
|
2017-06-01 02:51:32 +00:00
|
|
|
|
*/
|
2017-06-11 17:31:02 +00:00
|
|
|
|
void set_interrupt_line(bool value, int offset = 0) {
|
2017-06-05 13:37:19 +00:00
|
|
|
|
if(irq_line_ == value) return;
|
|
|
|
|
|
2017-06-02 00:34:52 +00:00
|
|
|
|
// IRQ requests are level triggered and masked.
|
|
|
|
|
irq_line_ = value;
|
|
|
|
|
if(irq_line_ && iff1_) {
|
|
|
|
|
request_status_ |= Interrupt::IRQ;
|
|
|
|
|
} else {
|
|
|
|
|
request_status_ &= ~Interrupt::IRQ;
|
|
|
|
|
}
|
2017-06-11 17:31:02 +00:00
|
|
|
|
|
|
|
|
|
// If this change happened at least one cycle ago then: (i) we're promised that this is a machine
|
|
|
|
|
// cycle per the contract on supplying an offset; and (ii) that means it happened before the lines
|
|
|
|
|
// were sampled. So adjust the most recent sample.
|
2017-06-11 20:41:34 +00:00
|
|
|
|
if(offset < 0) {
|
2017-06-11 17:31:02 +00:00
|
|
|
|
last_request_status_ = (last_request_status_ & ~Interrupt::IRQ) | (request_status_ & Interrupt::IRQ);
|
|
|
|
|
}
|
2017-06-01 02:51:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
2017-06-02 00:34:52 +00:00
|
|
|
|
Sets the logical value of the non-maskable interrupt line.
|
2017-06-11 17:31:02 +00:00
|
|
|
|
|
|
|
|
|
@param offset See discussion in set_interrupt_line.
|
2017-06-01 02:51:32 +00:00
|
|
|
|
*/
|
2017-06-11 17:31:02 +00:00
|
|
|
|
void set_non_maskable_interrupt_line(bool value, int offset = 0) {
|
2017-06-02 00:34:52 +00:00
|
|
|
|
// NMIs are edge triggered and cannot be masked.
|
2017-06-11 17:31:02 +00:00
|
|
|
|
if(value) {
|
|
|
|
|
request_status_ |= Interrupt::NMI;
|
2017-06-11 20:41:34 +00:00
|
|
|
|
if(offset < 0) {
|
2017-06-11 17:31:02 +00:00
|
|
|
|
last_request_status_ |= Interrupt::NMI;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-01 02:51:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
2017-06-02 00:34:52 +00:00
|
|
|
|
Sets the logical value of the bus request line.
|
2017-06-01 02:51:32 +00:00
|
|
|
|
*/
|
2017-06-02 00:34:52 +00:00
|
|
|
|
void set_bus_request_line(bool value) {
|
2017-06-03 23:09:47 +00:00
|
|
|
|
bus_request_line_ = value;
|
2017-06-01 02:51:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 01:40:08 +00:00
|
|
|
|
/*!
|
|
|
|
|
Sets the logical value of the reset line.
|
|
|
|
|
*/
|
|
|
|
|
void set_reset_line(bool value) {
|
|
|
|
|
// Reset requests are level triggered and cannot be masked.
|
|
|
|
|
if(value) request_status_ |= Interrupt::Reset;
|
|
|
|
|
else request_status_ &= ~Interrupt::Reset;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 02:31:04 +00:00
|
|
|
|
/*!
|
|
|
|
|
This emulation automatically sets itself up in power-on state at creation, which has the effect of triggering a
|
|
|
|
|
reset at the first opportunity. Use @c reset_power_on to disable that behaviour.
|
|
|
|
|
*/
|
|
|
|
|
inline void reset_power_on() {
|
|
|
|
|
request_status_ &= ~Interrupt::PowerOn;
|
|
|
|
|
last_request_status_ &= ~Interrupt::PowerOn;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 02:51:32 +00:00
|
|
|
|
/*!
|
|
|
|
|
For receivers of perform_machine_cycle only. Temporarily rejects the current machine
|
|
|
|
|
cycle, causing time to be rewinded to its beginning.
|
|
|
|
|
|
|
|
|
|
Behaviour will be to cause the Z80 to repeat this machine cycle, having adjusted total
|
|
|
|
|
running time appropriately. This method is intended for use if a subclass receives
|
|
|
|
|
perform_machine_cycle, indicating that the cycle should be completed, and discovers
|
|
|
|
|
that it should have signalled IRQ, NMI or BUSREQ during the cycle. In that case it
|
|
|
|
|
can rewind time and post the signal with a proper time offset.
|
|
|
|
|
*/
|
2017-06-03 23:10:23 +00:00
|
|
|
|
// void reject_machine_cycle() {
|
|
|
|
|
// }
|
2017-06-01 02:51:32 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Returns the bus cycle that the Z80 is currently in the process of performing.
|
|
|
|
|
*/
|
2017-06-03 23:10:23 +00:00
|
|
|
|
// const MachineCycle &get_current_bus_cycle(int &cycles_since_start) {
|
|
|
|
|
// }
|
2017-05-14 21:46:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-15 02:15:16 +00:00
|
|
|
|
}
|
2017-05-14 21:46:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* Z80_hpp */
|