1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-17 13:29:02 +00:00
CLK/InstructionSets/M68k/Implementation/PerformImplementation.hpp

1196 lines
34 KiB
C++
Raw Normal View History

2022-04-28 20:55:47 +00:00
//
// PerformImplementation.hpp
// Clock Signal
//
// Created by Thomas Harte on 28/04/2022.
// Copyright © 2022 Thomas Harte. All rights reserved.
//
#ifndef InstructionSets_M68k_PerformImplementation_h
#define InstructionSets_M68k_PerformImplementation_h
2022-05-11 11:00:35 +00:00
#include "../ExceptionVectors.hpp"
2022-04-28 20:55:47 +00:00
#include <cassert>
#include <cmath>
namespace InstructionSet {
namespace M68k {
#define u_extend16(x) uint32_t(int16_t(x))
#define s_extend16(x) int32_t(int16_t(x))
template <
Model model,
typename FlowController,
Operation operation = Operation::Undefined
> void perform(Preinstruction instruction, CPU::SlicedInt32 &src, CPU::SlicedInt32 &dest, Status &status, FlowController &flow_controller) {
2022-04-28 20:55:47 +00:00
#define sub_overflow() ((result ^ destination) & (destination ^ source))
#define add_overflow() ((result ^ destination) & ~(destination ^ source))
switch((operation != Operation::Undefined) ? operation : instruction.operation) {
2022-04-28 20:55:47 +00:00
/*
2022-04-29 11:57:02 +00:00
ABCD adds the lowest bytes from the source and destination using BCD arithmetic,
2022-04-28 20:55:47 +00:00
obeying the extend flag.
*/
case Operation::ABCD: {
// Pull out the two halves, for simplicity.
const uint8_t source = src.b;
const uint8_t destination = dest.b;
2022-04-28 20:55:47 +00:00
// Perform the BCD add by evaluating the two nibbles separately.
const int unadjusted_result = destination + source + (status.extend_flag_ ? 1 : 0);
int result = (destination & 0xf) + (source & 0xf) + (status.extend_flag_ ? 1 : 0);
if(result > 0x09) result += 0x06;
result += (destination & 0xf0) + (source & 0xf0);
if(result > 0x99) result += 0x60;
// Set all flags essentially as if this were normal addition.
status.zero_result_ |= result & 0xff;
status.extend_flag_ = status.carry_flag_ = uint_fast32_t(result & ~0xff);
status.negative_flag_ = result & 0x80;
status.overflow_flag_ = ~unadjusted_result & result & 0x80;
// Store the result.
dest.b = uint8_t(result);
2022-04-28 20:55:47 +00:00
} break;
#define addop(a, b, x) a + b + (x ? 1 : 0)
#define subop(a, b, x) a - b - (x ? 1 : 0)
#define z_set(a, b) a = b
#define z_or(a, b) a |= b
#define addsubb(a, b, op, overflow, x, zero_op) \
2022-04-28 20:55:47 +00:00
const int source = a; \
const int destination = b; \
const auto result = op(destination, source, x); \
\
b = uint8_t(result); \
zero_op(status.zero_result_, b); \
2022-04-28 20:55:47 +00:00
status.extend_flag_ = status.carry_flag_ = uint_fast32_t(result & ~0xff); \
status.negative_flag_ = result & 0x80; \
status.overflow_flag_ = overflow() & 0x80;
#define addsubw(a, b, op, overflow, x, zero_op) \
2022-04-28 20:55:47 +00:00
const int source = a; \
const int destination = b; \
const auto result = op(destination, source, x); \
\
b = uint16_t(result); \
zero_op(status.zero_result_, b); \
2022-04-28 20:55:47 +00:00
status.extend_flag_ = status.carry_flag_ = uint_fast32_t(result & ~0xffff); \
status.negative_flag_ = result & 0x8000; \
status.overflow_flag_ = overflow() & 0x8000;
#define addsubl(a, b, op, overflow, x, zero_op) \
2022-04-28 20:55:47 +00:00
const uint64_t source = a; \
const uint64_t destination = b; \
const auto result = op(destination, source, x); \
\
b = uint32_t(result); \
zero_op(status.zero_result_, b); \
2022-04-28 20:55:47 +00:00
status.extend_flag_ = status.carry_flag_ = uint_fast32_t(result >> 32); \
status.negative_flag_ = result & 0x80000000; \
status.overflow_flag_ = overflow() & 0x80000000;
#define addb(a, b, x, z) addsubb(a, b, addop, add_overflow, x, z)
#define subb(a, b, x, z) addsubb(a, b, subop, sub_overflow, x, z)
#define addw(a, b, x, z) addsubw(a, b, addop, add_overflow, x, z)
#define subw(a, b, x, z) addsubw(a, b, subop, sub_overflow, x, z)
#define addl(a, b, x, z) addsubl(a, b, addop, add_overflow, x, z)
#define subl(a, b, x, z) addsubl(a, b, subop, sub_overflow, x, z)
2022-04-28 20:55:47 +00:00
#define no_extend(op, a, b) op(a, b, 0, z_set)
#define extend(op, a, b) op(a, b, status.extend_flag_, z_or)
2022-04-28 20:55:47 +00:00
// ADD and ADDA add two quantities, the latter sign extending and without setting any flags;
// ADDQ and SUBQ act as ADD and SUB, but taking the second argument from the instruction code.
case Operation::ADDb: {
no_extend( addb,
src.b,
dest.b);
2022-04-28 20:55:47 +00:00
} break;
case Operation::ADDXb: {
extend( addb,
src.b,
dest.b);
2022-04-28 20:55:47 +00:00
} break;
case Operation::ADDw: {
no_extend( addw,
src.w,
dest.w);
2022-04-28 20:55:47 +00:00
} break;
case Operation::ADDXw: {
extend( addw,
src.w,
dest.w);
2022-04-28 20:55:47 +00:00
} break;
case Operation::ADDl: {
no_extend( addl,
src.l,
dest.l);
2022-04-28 20:55:47 +00:00
} break;
case Operation::ADDXl: {
extend( addl,
src.l,
dest.l);
2022-04-28 20:55:47 +00:00
} break;
case Operation::SUBb: {
no_extend( subb,
src.b,
dest.b);
2022-04-28 20:55:47 +00:00
} break;
case Operation::SUBXb: {
extend( subb,
src.b,
dest.b);
2022-04-28 20:55:47 +00:00
} break;
case Operation::SUBw: {
no_extend( subw,
src.w,
dest.w);
2022-04-28 20:55:47 +00:00
} break;
case Operation::SUBXw: {
extend( subw,
src.w,
dest.w);
2022-04-28 20:55:47 +00:00
} break;
case Operation::SUBl: {
no_extend( subl,
src.l,
dest.l);
2022-04-28 20:55:47 +00:00
} break;
case Operation::SUBXl: {
extend( subl,
src.l,
dest.l);
2022-04-28 20:55:47 +00:00
} break;
#undef addl
#undef addw
#undef addb
#undef subl
#undef subw
#undef subb
#undef addsubl
#undef addsubw
#undef addsubb
#undef z_set
#undef z_or
#undef no_extend
#undef extend
#undef addop
#undef subop
case Operation::ADDAw:
dest.l += u_extend16(src.w);
2022-04-28 20:55:47 +00:00
break;
case Operation::ADDAl:
dest.l += src.l;
2022-04-28 20:55:47 +00:00
break;
case Operation::SUBAw:
dest.l -= u_extend16(src.w);
2022-04-28 20:55:47 +00:00
break;
case Operation::SUBAl:
dest.l -= src.l;
2022-04-28 20:55:47 +00:00
break;
2022-05-05 00:57:22 +00:00
// BTST/BCLR/etc: modulo for the mask depends on whether memory or a data register is the target.
case Operation::BTST: {
const uint32_t mask = (instruction.mode<1>() == AddressingMode::DataRegisterDirect) ? 31 : 7;
status.zero_result_ = dest.l & (1 << (src.l & mask));
} break;
case Operation::BCLR: {
const uint32_t mask = (instruction.mode<1>() == AddressingMode::DataRegisterDirect) ? 31 : 7;
status.zero_result_ = dest.l & (1 << (src.l & mask));
dest.l &= ~(1 << (src.l & mask));
flow_controller.did_bit_op(src.l & mask);
2022-05-05 00:57:22 +00:00
} break;
case Operation::BCHG: {
const uint32_t mask = (instruction.mode<1>() == AddressingMode::DataRegisterDirect) ? 31 : 7;
status.zero_result_ = dest.l & (1 << (src.l & mask));
dest.l ^= 1 << (src.l & mask);
flow_controller.did_bit_op(src.l & mask);
2022-05-05 00:57:22 +00:00
} break;
case Operation::BSET: {
const uint32_t mask = (instruction.mode<1>() == AddressingMode::DataRegisterDirect) ? 31 : 7;
status.zero_result_ = dest.l & (1 << (src.l & mask));
dest.l |= 1 << (src.l & mask);
flow_controller.did_bit_op(src.l & mask);
2022-05-05 00:57:22 +00:00
} break;
2022-04-28 20:55:47 +00:00
2022-04-29 21:12:06 +00:00
case Operation::Bccb:
flow_controller.template complete_bcc<int8_t>(
status.evaluate_condition(instruction.condition()),
src.b);
break;
2022-04-29 21:12:06 +00:00
case Operation::Bccw:
flow_controller.template complete_bcc<int16_t>(
status.evaluate_condition(instruction.condition()),
src.w);
break;
2022-04-29 21:12:06 +00:00
case Operation::Bccl:
flow_controller.template complete_bcc<int32_t>(
status.evaluate_condition(instruction.condition()),
src.l);
break;
2022-04-28 20:55:47 +00:00
case Operation::BSRb:
flow_controller.bsr(int8_t(src.b) + 2);
break;
case Operation::BSRw:
flow_controller.bsr(int16_t(src.w) + 2);
break;
case Operation::BSRl:
flow_controller.bsr(src.l + 2);
break;
case Operation::DBcc: {
const bool matched_condition = status.evaluate_condition(instruction.condition());
bool overflowed = false;
2022-04-29 21:12:06 +00:00
// Classify the dbcc.
if(!matched_condition) {
-- src.w;
overflowed = src.w == 0xffff;
2022-04-29 21:12:06 +00:00
}
// Take the branch.
flow_controller.complete_dbcc(
matched_condition,
overflowed,
int16_t(dest.w));
} break;
2022-04-29 21:12:06 +00:00
case Operation::Scc:
2022-05-03 18:23:57 +00:00
src.b = status.evaluate_condition(instruction.condition()) ? 0xff : 0x00;
2022-04-29 21:12:06 +00:00
break;
2022-04-28 20:55:47 +00:00
/*
CLRs: store 0 to the destination, set the zero flag, and clear
negative, overflow and carry.
*/
case Operation::CLRb:
2022-05-06 15:33:57 +00:00
src.b = 0;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.overflow_flag_ = status.carry_flag_ = status.zero_result_ = 0;
break;
case Operation::CLRw:
2022-05-06 15:33:57 +00:00
src.w = 0;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.overflow_flag_ = status.carry_flag_ = status.zero_result_ = 0;
break;
case Operation::CLRl:
2022-05-06 15:33:57 +00:00
src.l = 0;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.overflow_flag_ = status.carry_flag_ = status.zero_result_ = 0;
break;
/*
CMP.b, CMP.l and CMP.w: sets the condition flags (other than extend) based on a subtraction
of the source from the destination; the result of the subtraction is not stored.
*/
case Operation::CMPb: {
const uint8_t source = src.b;
const uint8_t destination = dest.b;
2022-04-28 20:55:47 +00:00
const int result = destination - source;
status.zero_result_ = result & 0xff;
status.carry_flag_ = decltype(status.carry_flag_)(result & ~0xff);
status.negative_flag_ = result & 0x80;
status.overflow_flag_ = sub_overflow() & 0x80;
} break;
case Operation::CMPw: {
const uint16_t source = src.w;
2022-05-03 13:05:34 +00:00
const uint16_t destination = dest.w;
2022-04-28 20:55:47 +00:00
const int result = destination - source;
status.zero_result_ = result & 0xffff;
status.carry_flag_ = decltype(status.carry_flag_)(result & ~0xffff);
status.negative_flag_ = result & 0x8000;
status.overflow_flag_ = sub_overflow() & 0x8000;
} break;
case Operation::CMPAw: {
const auto source = uint64_t(u_extend16(src.w));
const uint64_t destination = dest.l;
2022-04-28 20:55:47 +00:00
const auto result = destination - source;
status.zero_result_ = uint32_t(result);
status.carry_flag_ = result >> 32;
status.negative_flag_ = result & 0x80000000;
status.overflow_flag_ = sub_overflow() & 0x80000000;
} break;
2022-05-03 13:20:02 +00:00
// TODO: is there any benefit to keeping both of these?
case Operation::CMPAl:
2022-04-28 20:55:47 +00:00
case Operation::CMPl: {
const auto source = uint64_t(src.l);
const auto destination = uint64_t(dest.l);
2022-04-28 20:55:47 +00:00
const auto result = destination - source;
status.zero_result_ = uint32_t(result);
status.carry_flag_ = result >> 32;
status.negative_flag_ = result & 0x80000000;
status.overflow_flag_ = sub_overflow() & 0x80000000;
} break;
// JMP: copies EA(0) to the program counter.
2022-04-29 21:12:06 +00:00
case Operation::JMP:
flow_controller.jmp(src.l);
2022-04-29 21:12:06 +00:00
break;
2022-04-28 20:55:47 +00:00
// JSR: jump to EA(0), pushing the current PC to the stack.
case Operation::JSR:
flow_controller.jsr(src.l);
break;
2022-04-28 20:55:47 +00:00
/*
MOVE.b, MOVE.l and MOVE.w: move the least significant byte or word, or the entire long word,
and set negative, zero, overflow and carry as appropriate.
*/
case Operation::MOVEb:
status.zero_result_ = dest.b = src.b;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x80;
status.overflow_flag_ = status.carry_flag_ = 0;
break;
case Operation::MOVEw:
status.zero_result_ = dest.w = src.w;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x8000;
status.overflow_flag_ = status.carry_flag_ = 0;
break;
case Operation::MOVEl:
status.zero_result_ = dest.l = src.l;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x80000000;
status.overflow_flag_ = status.carry_flag_ = 0;
break;
/*
MOVEA.l: move the entire long word;
MOVEA.w: move the least significant word and sign extend it.
Neither sets any flags.
*/
case Operation::MOVEAw:
dest.l = u_extend16(src.w);
2022-04-28 20:55:47 +00:00
break;
case Operation::MOVEAl:
dest.l = src.l;
2022-04-28 20:55:47 +00:00
break;
2022-04-29 21:12:06 +00:00
case Operation::LEA:
2022-04-30 00:30:48 +00:00
dest.l = src.l;
2022-04-29 21:12:06 +00:00
break;
case Operation::PEA:
flow_controller.pea(src.l);
break;
2022-04-28 20:55:47 +00:00
/*
Status word moves and manipulations.
*/
case Operation::MOVEtoSR:
status.set_status(src.w);
flow_controller.did_update_status();
2022-04-28 20:55:47 +00:00
break;
case Operation::MOVEfromSR:
2022-05-06 15:33:57 +00:00
src.w = status.status();
2022-04-28 20:55:47 +00:00
break;
case Operation::MOVEtoCCR:
status.set_ccr(src.w);
2022-04-28 20:55:47 +00:00
break;
2022-05-11 11:52:23 +00:00
case Operation::MOVEtoUSP:
flow_controller.move_to_usp(src.l);
break;
case Operation::MOVEfromUSP:
flow_controller.move_from_usp(src.l);
break;
2022-04-28 20:55:47 +00:00
case Operation::EXTbtow:
2022-05-04 00:17:36 +00:00
src.w = uint16_t(int8_t(src.b));
2022-04-28 20:55:47 +00:00
status.overflow_flag_ = status.carry_flag_ = 0;
2022-05-04 00:17:36 +00:00
status.zero_result_ = src.w;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x8000;
break;
case Operation::EXTwtol:
2022-05-04 00:17:36 +00:00
src.l = u_extend16(src.w);
2022-04-28 20:55:47 +00:00
status.overflow_flag_ = status.carry_flag_ = 0;
2022-05-04 00:17:36 +00:00
status.zero_result_ = src.l;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x80000000;
break;
#define and_op(a, b) a &= b
#define or_op(a, b) a |= b
#define eor_op(a, b) a ^= b
#define apply(op, func) { \
auto sr = status.status(); \
op(sr, src.w); \
func(sr); \
2022-04-28 20:55:47 +00:00
}
#define set_status(x) status.set_status(x); flow_controller.did_update_status()
#define set_ccr(x) status.set_ccr(x)
2022-04-28 20:55:47 +00:00
#define apply_op_sr(op) apply(op, set_status)
#define apply_op_ccr(op) apply(op, set_ccr)
case Operation::ANDItoSR: apply_op_sr(and_op); break;
case Operation::EORItoSR: apply_op_sr(eor_op); break;
case Operation::ORItoSR: apply_op_sr(or_op); break;
case Operation::ANDItoCCR: apply_op_ccr(and_op); break;
case Operation::EORItoCCR: apply_op_ccr(eor_op); break;
case Operation::ORItoCCR: apply_op_ccr(or_op); break;
#undef apply_op_ccr
#undef apply_op_sr
#undef set_ccr
#undef set_status
2022-04-28 20:55:47 +00:00
#undef apply
#undef eor_op
#undef or_op
#undef and_op
/*
Multiplications.
*/
case Operation::MULU:
dest.l = dest.w * src.w;
2022-04-28 20:55:47 +00:00
status.carry_flag_ = status.overflow_flag_ = 0;
status.zero_result_ = dest.l;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x80000000;
flow_controller.did_mulu(src.w);
break;
2022-04-28 20:55:47 +00:00
case Operation::MULS:
dest.l =
u_extend16(dest.w) * u_extend16(src.w);
2022-04-28 20:55:47 +00:00
status.carry_flag_ = status.overflow_flag_ = 0;
status.zero_result_ = dest.l;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x80000000;
flow_controller.did_muls(src.w);
break;
2022-04-28 20:55:47 +00:00
/*
Divisions.
*/
#define announce_divide_by_zero() \
status.negative_flag_ = status.overflow_flag_ = 0; \
status.zero_result_ = 1; \
2022-05-11 11:00:35 +00:00
flow_controller.raise_exception(Exception::IntegerDivideByZero)
2022-04-28 20:55:47 +00:00
case Operation::DIVU: {
status.carry_flag_ = 0;
// An attempt to divide by zero schedules an exception.
if(!src.w) {
2022-04-28 20:55:47 +00:00
// Schedule a divide-by-zero exception.
announce_divide_by_zero();
return;
}
uint32_t dividend = dest.l;
uint32_t divisor = src.w;
2022-04-28 20:55:47 +00:00
const auto quotient = dividend / divisor;
// If overflow would occur, appropriate flags are set and the result is not written back.
if(quotient > 65535) {
status.overflow_flag_ = status.zero_result_ = status.negative_flag_ = 1;
2022-05-10 00:58:51 +00:00
flow_controller.template did_divu<true>(dividend, divisor);
2022-04-28 20:55:47 +00:00
return;
}
const uint16_t remainder = uint16_t(dividend % divisor);
dest.l = uint32_t((remainder << 16) | uint16_t(quotient));
2022-04-28 20:55:47 +00:00
status.overflow_flag_ = 0;
status.zero_result_ = quotient;
status.negative_flag_ = status.zero_result_ & 0x8000;
2022-05-10 00:58:51 +00:00
flow_controller.template did_divu<false>(dividend, divisor);
2022-04-28 20:55:47 +00:00
} break;
case Operation::DIVS: {
status.carry_flag_ = 0;
// An attempt to divide by zero schedules an exception.
if(!src.w) {
2022-04-28 20:55:47 +00:00
// Schedule a divide-by-zero exception.
2022-05-11 11:00:35 +00:00
announce_divide_by_zero();
2022-04-28 20:55:47 +00:00
break;
}
const int32_t signed_dividend = int32_t(dest.l);
const int32_t signed_divisor = s_extend16(src.w);
2022-04-28 20:55:47 +00:00
const auto result_sign =
( (0 <= signed_dividend) - (signed_dividend < 0) ) *
( (0 <= signed_divisor) - (signed_divisor < 0) );
const uint32_t dividend = uint32_t(std::abs(signed_dividend));
const uint32_t divisor = uint32_t(std::abs(signed_divisor));
int cycles_expended = 12; // Covers the nn nnn n to get beyond the sign test.
if(signed_dividend < 0) {
cycles_expended += 2; // An additional microycle applies if the dividend is negative.
}
// Check for overflow. If it exists, work here is already done.
const auto quotient = dividend / divisor;
if(quotient > 32767) {
status.overflow_flag_ = 1;
2022-05-10 00:58:51 +00:00
flow_controller.template did_divs<true>(signed_dividend, signed_divisor);
2022-04-28 20:55:47 +00:00
break;
}
const uint16_t remainder = uint16_t(signed_dividend % signed_divisor);
const int signed_quotient = result_sign*int(quotient);
dest.l = uint32_t((remainder << 16) | uint16_t(signed_quotient));
2022-04-28 20:55:47 +00:00
status.zero_result_ = decltype(status.zero_result_)(signed_quotient);
status.negative_flag_ = status.zero_result_ & 0x8000;
status.overflow_flag_ = 0;
2022-05-10 00:58:51 +00:00
flow_controller.template did_divs<false>(signed_dividend, signed_divisor);
2022-04-28 20:55:47 +00:00
} break;
#undef announce_divide_by_zero
// TRAP, which is a nicer form of ILLEGAL.
case Operation::TRAP:
2022-05-11 11:00:35 +00:00
flow_controller.template raise_exception<false>(src.l + Exception::TrapBase);
break;
2022-04-28 20:55:47 +00:00
case Operation::TRAPV: {
if(status.overflow_flag_) {
2022-05-11 11:00:35 +00:00
flow_controller.template raise_exception<false>(Exception::TRAPV);
}
} break;
2022-04-28 20:55:47 +00:00
case Operation::CHK: {
const bool is_under = s_extend16(dest.w) < 0;
const bool is_over = s_extend16(dest.w) > s_extend16(src.w);
2022-04-28 20:55:47 +00:00
status.overflow_flag_ = status.carry_flag_ = 0;
status.zero_result_ = dest.w;
2022-04-28 20:55:47 +00:00
// Test applied for N:
//
// if Dn < 0, set negative flag;
// otherwise, if Dn > <ea>, reset negative flag.
if(is_over) status.negative_flag_ = 0;
if(is_under) status.negative_flag_ = 1;
2022-04-28 20:55:47 +00:00
// No exception is the default course of action; deviate only if an
// exception is necessary.
2022-05-10 00:58:51 +00:00
flow_controller.did_chk(is_under, is_over);
2022-04-28 20:55:47 +00:00
if(is_under || is_over) {
2022-05-11 11:00:35 +00:00
flow_controller.template raise_exception<false>(Exception::CHK);
2022-04-28 20:55:47 +00:00
}
} break;
/*
NEGs: negatives the destination, setting the zero,
negative, overflow and carry flags appropriate, and extend.
NB: since the same logic as SUB is used to calculate overflow,
and SUB calculates `destination - source`, the NEGs deliberately
label 'source' and 'destination' differently from Motorola.
*/
case Operation::NEGb: {
const int destination = 0;
2022-05-06 15:33:57 +00:00
const int source = src.b;
2022-04-28 20:55:47 +00:00
const auto result = destination - source;
2022-05-06 15:33:57 +00:00
src.b = uint8_t(result);
2022-04-28 20:55:47 +00:00
status.zero_result_ = result & 0xff;
status.extend_flag_ = status.carry_flag_ = decltype(status.carry_flag_)(result & ~0xff);
status.negative_flag_ = result & 0x80;
status.overflow_flag_ = sub_overflow() & 0x80;
} break;
case Operation::NEGw: {
const int destination = 0;
2022-05-06 15:33:57 +00:00
const int source = src.w;
2022-04-28 20:55:47 +00:00
const auto result = destination - source;
2022-05-06 15:33:57 +00:00
src.w = uint16_t(result);
2022-04-28 20:55:47 +00:00
status.zero_result_ = result & 0xffff;
status.extend_flag_ = status.carry_flag_ = decltype(status.carry_flag_)(result & ~0xffff);
status.negative_flag_ = result & 0x8000;
status.overflow_flag_ = sub_overflow() & 0x8000;
} break;
case Operation::NEGl: {
const uint64_t destination = 0;
2022-05-06 15:33:57 +00:00
const uint64_t source = src.l;
2022-04-28 20:55:47 +00:00
const auto result = destination - source;
2022-05-06 15:33:57 +00:00
src.l = uint32_t(result);
2022-04-28 20:55:47 +00:00
status.zero_result_ = uint_fast32_t(result);
status.extend_flag_ = status.carry_flag_ = result >> 32;
status.negative_flag_ = result & 0x80000000;
status.overflow_flag_ = sub_overflow() & 0x80000000;
} break;
/*
NEGXs: NEG, with extend.
*/
case Operation::NEGXb: {
2022-05-06 15:33:57 +00:00
const int source = src.b;
2022-04-28 20:55:47 +00:00
const int destination = 0;
const auto result = destination - source - (status.extend_flag_ ? 1 : 0);
2022-05-06 15:33:57 +00:00
src.b = uint8_t(result);
2022-04-28 20:55:47 +00:00
status.zero_result_ |= result & 0xff;
status.extend_flag_ = status.carry_flag_ = decltype(status.carry_flag_)(result & ~0xff);
status.negative_flag_ = result & 0x80;
status.overflow_flag_ = sub_overflow() & 0x80;
} break;
case Operation::NEGXw: {
2022-05-06 15:33:57 +00:00
const int source = src.w;
2022-04-28 20:55:47 +00:00
const int destination = 0;
const auto result = destination - source - (status.extend_flag_ ? 1 : 0);
2022-05-06 15:33:57 +00:00
src.w = uint16_t(result);
2022-04-28 20:55:47 +00:00
status.zero_result_ |= result & 0xffff;
status.extend_flag_ = status.carry_flag_ = decltype(status.carry_flag_)(result & ~0xffff);
status.negative_flag_ = result & 0x8000;
status.overflow_flag_ = sub_overflow() & 0x8000;
} break;
case Operation::NEGXl: {
2022-05-06 15:33:57 +00:00
const uint64_t source = src.l;
2022-04-28 20:55:47 +00:00
const uint64_t destination = 0;
const auto result = destination - source - (status.extend_flag_ ? 1 : 0);
2022-05-06 15:33:57 +00:00
src.l = uint32_t(result);
2022-04-28 20:55:47 +00:00
status.zero_result_ |= uint_fast32_t(result);
status.extend_flag_ = status.carry_flag_ = result >> 32;
status.negative_flag_ = result & 0x80000000;
status.overflow_flag_ = sub_overflow() & 0x80000000;
} break;
/*
The no-op.
*/
2022-04-29 21:12:06 +00:00
case Operation::NOP: break;
2022-04-28 20:55:47 +00:00
/*
LINK and UNLINK help with stack frames, allowing a certain
amount of stack space to be allocated or deallocated.
*/
2022-05-04 12:26:11 +00:00
case Operation::LINKw:
2022-05-07 12:15:26 +00:00
flow_controller.link(instruction, int16_t(dest.w));
2022-05-04 12:26:11 +00:00
break;
2022-05-04 12:26:11 +00:00
case Operation::UNLINK:
flow_controller.unlink(src.l);
break;
2022-04-28 20:55:47 +00:00
/*
TAS: sets zero and negative depending on the current value of the destination,
2022-05-06 16:18:56 +00:00
and sets the high bit, using a specialised atomic bus cycle.
2022-04-28 20:55:47 +00:00
*/
case Operation::TAS:
2022-05-06 16:23:04 +00:00
flow_controller.tas(instruction, src.l);
2022-04-28 20:55:47 +00:00
break;
/*
Bitwise operators: AND, OR and EOR. All three clear the overflow and carry flags,
and set zero and negative appropriately.
*/
#define op_and(x, y) x &= y
#define op_or(x, y) x |= y
#define op_eor(x, y) x ^= y
#define bitwise(source, dest, sign_mask, operator) \
operator(dest, source); \
status.overflow_flag_ = status.carry_flag_ = 0; \
status.zero_result_ = dest; \
status.negative_flag_ = dest & sign_mask;
#define andx(source, dest, sign_mask) bitwise(source, dest, sign_mask, op_and)
#define eorx(source, dest, sign_mask) bitwise(source, dest, sign_mask, op_eor)
#define orx(source, dest, sign_mask) bitwise(source, dest, sign_mask, op_or)
#define op_bwl(name, op) \
case Operation::name##b: op(src.b, dest.b, 0x80); break; \
case Operation::name##w: op(src.w, dest.w, 0x8000); break; \
case Operation::name##l: op(src.l, dest.l, 0x80000000); break;
2022-04-28 20:55:47 +00:00
op_bwl(AND, andx);
op_bwl(EOR, eorx);
op_bwl(OR, orx);
#undef op_bwl
#undef orx
#undef eorx
#undef andx
#undef bitwise
#undef op_eor
#undef op_or
#undef op_and
// NOTs: take the logical inverse, affecting the negative and zero flags.
case Operation::NOTb:
2022-05-06 15:33:57 +00:00
src.b ^= 0xff;
status.zero_result_ = src.b;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x80;
status.overflow_flag_ = status.carry_flag_ = 0;
break;
case Operation::NOTw:
2022-05-06 15:33:57 +00:00
src.w ^= 0xffff;
status.zero_result_ = src.w;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x8000;
status.overflow_flag_ = status.carry_flag_ = 0;
break;
case Operation::NOTl:
2022-05-06 15:33:57 +00:00
src.l ^= 0xffffffff;
status.zero_result_ = src.l;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x80000000;
status.overflow_flag_ = status.carry_flag_ = 0;
break;
2022-05-02 16:57:45 +00:00
#define sbcd(d) \
2022-04-28 20:55:47 +00:00
/* Perform the BCD arithmetic by evaluating the two nibbles separately. */ \
const int unadjusted_result = destination - source - (status.extend_flag_ ? 1 : 0); \
int result = (destination & 0xf) - (source & 0xf) - (status.extend_flag_ ? 1 : 0); \
if((result & 0x1f) > 0x09) result -= 0x06; \
result += (destination & 0xf0) - (source & 0xf0); \
status.extend_flag_ = status.carry_flag_ = decltype(status.carry_flag_)((result & 0x1ff) > 0x99); \
if(status.carry_flag_) result -= 0x60; \
\
/* Set all flags essentially as if this were normal subtraction. */ \
status.zero_result_ |= result & 0xff; \
status.negative_flag_ = result & 0x80; \
status.overflow_flag_ = unadjusted_result & ~result & 0x80; \
\
/* Store the result. */ \
2022-05-02 16:57:45 +00:00
d = uint8_t(result);
2022-04-28 20:55:47 +00:00
/*
SBCD subtracts the lowest byte of the source from that of the destination using
BCD arithmetic, obeying the extend flag.
*/
case Operation::SBCD: {
const uint8_t source = src.b;
const uint8_t destination = dest.b;
2022-05-02 16:57:45 +00:00
sbcd(dest.b);
2022-04-28 20:55:47 +00:00
} break;
/*
2022-05-02 16:57:45 +00:00
NBCD is like SBCD except that the result is 0 - source rather than
2022-04-28 20:55:47 +00:00
destination - source.
*/
case Operation::NBCD: {
2022-05-02 16:57:45 +00:00
const uint8_t source = src.b;
2022-04-28 20:55:47 +00:00
const uint8_t destination = 0;
2022-05-02 16:57:45 +00:00
sbcd(src.b);
2022-04-28 20:55:47 +00:00
} break;
#undef sbcd
2022-04-28 20:55:47 +00:00
// EXG and SWAP exchange/swap words or long words.
case Operation::EXG: {
const auto temporary = src.l;
src.l = dest.l;
dest.l = temporary;
2022-04-28 20:55:47 +00:00
} break;
case Operation::SWAP: {
2022-05-04 00:17:36 +00:00
uint16_t *const words = reinterpret_cast<uint16_t *>(&src.l);
const auto temporary = words[0];
words[0] = words[1];
words[1] = temporary;
2022-04-28 20:55:47 +00:00
2022-05-04 00:17:36 +00:00
status.zero_result_ = src.l;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = temporary & 0x8000;
status.overflow_flag_ = status.carry_flag_ = 0;
} break;
/*
Shifts and rotates.
*/
#define set_neg_zero(v, m) \
status.zero_result_ = decltype(status.zero_result_)(v); \
status.negative_flag_ = status.zero_result_ & decltype(status.negative_flag_)(m);
#define set_neg_zero_overflow(v, m) \
set_neg_zero(v, m); \
status.overflow_flag_ = (decltype(status.zero_result_)(value) ^ status.zero_result_) & decltype(status.overflow_flag_)(m);
#define decode_shift_count() \
2022-05-09 15:26:01 +00:00
int shift_count = src.l & 63; \
2022-05-10 00:58:51 +00:00
flow_controller.did_shift(shift_count);
2022-04-28 20:55:47 +00:00
2022-05-04 23:44:59 +00:00
#define set_flags_w(t) set_flags(src.w, 0x8000, t)
2022-04-28 20:55:47 +00:00
#define asl(destination, size) {\
decode_shift_count(); \
const auto value = destination; \
\
if(!shift_count) { \
status.carry_flag_ = status.overflow_flag_ = 0; \
} else { \
destination = (shift_count < size) ? decltype(destination)(value << shift_count) : 0; \
2022-05-09 15:26:01 +00:00
status.extend_flag_ = status.carry_flag_ = decltype(status.carry_flag_)(value) & decltype(status.carry_flag_)( (1u << (size - 1)) >> (shift_count - 1) ); \
2022-04-28 20:55:47 +00:00
\
if(shift_count >= size) status.overflow_flag_ = value && (value != decltype(value)(-1)); \
else { \
const auto mask = decltype(destination)(0xffffffff << (size - shift_count)); \
status.overflow_flag_ = mask & value && ((mask & value) != mask); \
} \
} \
\
set_neg_zero(destination, 1 << (size - 1)); \
}
case Operation::ASLm: {
2022-05-04 23:44:59 +00:00
const auto value = src.w;
src.w = uint16_t(value << 1);
2022-04-28 20:55:47 +00:00
status.extend_flag_ = status.carry_flag_ = value & 0x8000;
2022-05-04 23:44:59 +00:00
set_neg_zero_overflow(src.w, 0x8000);
2022-04-28 20:55:47 +00:00
} break;
2022-05-09 15:26:01 +00:00
case Operation::ASLb: asl(dest.b, 8); break;
case Operation::ASLw: asl(dest.w, 16); break;
case Operation::ASLl: asl(dest.l, 32); break;
2022-04-28 20:55:47 +00:00
#define asr(destination, size) {\
decode_shift_count(); \
const auto value = destination; \
\
if(!shift_count) { \
2022-05-09 15:26:01 +00:00
status.carry_flag_ = 0; \
2022-04-28 20:55:47 +00:00
} else { \
destination = (shift_count < size) ? \
decltype(destination)(\
(value >> shift_count) | \
((value & decltype(value)(1 << (size - 1)) ? 0xffffffff : 0x000000000) << (size - shift_count)) \
) : \
decltype(destination)( \
(value & decltype(value)(1 << (size - 1))) ? 0xffffffff : 0x000000000 \
); \
2022-05-09 15:26:01 +00:00
status.extend_flag_ = status.carry_flag_ = decltype(status.carry_flag_)(value) & decltype(status.carry_flag_)(1 << (shift_count - 1)); \
2022-04-28 20:55:47 +00:00
} \
\
set_neg_zero_overflow(destination, 1 << (size - 1)); \
}
case Operation::ASRm: {
2022-05-04 23:44:59 +00:00
const auto value = src.w;
src.w = (value&0x8000) | (value >> 1);
2022-04-28 20:55:47 +00:00
status.extend_flag_ = status.carry_flag_ = value & 1;
2022-05-04 23:44:59 +00:00
set_neg_zero_overflow(src.w, 0x8000);
2022-04-28 20:55:47 +00:00
} break;
2022-05-09 15:26:01 +00:00
case Operation::ASRb: asr(dest.b, 8); break;
case Operation::ASRw: asr(dest.w, 16); break;
case Operation::ASRl: asr(dest.l, 32); break;
2022-04-28 20:55:47 +00:00
#undef set_neg_zero_overflow
#define set_neg_zero_overflow(v, m) \
set_neg_zero(v, m); \
status.overflow_flag_ = 0;
#undef set_flags
#define set_flags(v, m, t) \
status.zero_result_ = v; \
status.negative_flag_ = status.zero_result_ & (m); \
status.overflow_flag_ = 0; \
status.carry_flag_ = value & (t);
#define lsl(destination, size) {\
decode_shift_count(); \
const auto value = destination; \
\
if(!shift_count) { \
2022-05-09 15:26:01 +00:00
status.carry_flag_ = 0; \
2022-04-28 20:55:47 +00:00
} else { \
destination = (shift_count < size) ? decltype(destination)(value << shift_count) : 0; \
status.extend_flag_ = status.carry_flag_ = decltype(status.carry_flag_)(value) & decltype(status.carry_flag_)( (1u << (size - 1)) >> (shift_count - 1) ); \
} \
\
set_neg_zero_overflow(destination, 1 << (size - 1)); \
}
case Operation::LSLm: {
2022-05-04 23:44:59 +00:00
const auto value = src.w;
src.w = uint16_t(value << 1);
2022-04-28 20:55:47 +00:00
status.extend_flag_ = status.carry_flag_ = value & 0x8000;
2022-05-04 23:44:59 +00:00
set_neg_zero_overflow(src.w, 0x8000);
2022-04-28 20:55:47 +00:00
} break;
2022-05-09 15:26:01 +00:00
case Operation::LSLb: lsl(dest.b, 8); break;
case Operation::LSLw: lsl(dest.w, 16); break;
case Operation::LSLl: lsl(dest.l, 32); break;
2022-04-28 20:55:47 +00:00
#define lsr(destination, size) {\
decode_shift_count(); \
const auto value = destination; \
\
if(!shift_count) { \
status.carry_flag_ = 0; \
} else { \
destination = (shift_count < size) ? (value >> shift_count) : 0; \
status.extend_flag_ = status.carry_flag_ = value & decltype(status.carry_flag_)(1 << (shift_count - 1)); \
} \
\
set_neg_zero_overflow(destination, 1 << (size - 1)); \
}
case Operation::LSRm: {
2022-05-04 23:44:59 +00:00
const auto value = src.w;
src.w = value >> 1;
2022-04-28 20:55:47 +00:00
status.extend_flag_ = status.carry_flag_ = value & 1;
2022-05-04 23:44:59 +00:00
set_neg_zero_overflow(src.w, 0x8000);
2022-04-28 20:55:47 +00:00
} break;
2022-05-09 15:26:01 +00:00
case Operation::LSRb: lsr(dest.b, 8); break;
case Operation::LSRw: lsr(dest.w, 16); break;
case Operation::LSRl: lsr(dest.l, 32); break;
2022-04-28 20:55:47 +00:00
#define rol(destination, size) { \
decode_shift_count(); \
const auto value = destination; \
\
if(!shift_count) { \
status.carry_flag_ = 0; \
} else { \
shift_count &= (size - 1); \
destination = decltype(destination)( \
(value << shift_count) | \
(value >> (size - shift_count)) \
); \
status.carry_flag_ = decltype(status.carry_flag_)(destination & 1); \
} \
\
set_neg_zero_overflow(destination, 1 << (size - 1)); \
}
case Operation::ROLm: {
2022-05-04 23:44:59 +00:00
const auto value = src.w;
src.w = uint16_t((value << 1) | (value >> 15));
status.carry_flag_ = src.w & 1;
set_neg_zero_overflow(src.w, 0x8000);
2022-04-28 20:55:47 +00:00
} break;
2022-05-09 15:26:01 +00:00
case Operation::ROLb: rol(dest.b, 8); break;
case Operation::ROLw: rol(dest.w, 16); break;
case Operation::ROLl: rol(dest.l, 32); break;
2022-04-28 20:55:47 +00:00
#define ror(destination, size) { \
decode_shift_count(); \
const auto value = destination; \
\
if(!shift_count) { \
status.carry_flag_ = 0; \
} else { \
shift_count &= (size - 1); \
destination = decltype(destination)(\
(value >> shift_count) | \
(value << (size - shift_count)) \
);\
status.carry_flag_ = destination & decltype(status.carry_flag_)(1 << (size - 1)); \
} \
\
set_neg_zero_overflow(destination, 1 << (size - 1)); \
}
case Operation::RORm: {
2022-05-04 23:44:59 +00:00
const auto value = src.w;
src.w = uint16_t((value >> 1) | (value << 15));
status.carry_flag_ = src.w & 0x8000;
set_neg_zero_overflow(src.w, 0x8000);
2022-04-28 20:55:47 +00:00
} break;
2022-05-09 15:26:01 +00:00
case Operation::RORb: ror(dest.b, 8); break;
case Operation::RORw: ror(dest.w, 16); break;
case Operation::RORl: ror(dest.l, 32); break;
2022-04-28 20:55:47 +00:00
#define roxl(destination, size) { \
2022-05-09 15:26:01 +00:00
decode_shift_count(); \
\
2022-04-28 20:55:47 +00:00
shift_count %= (size + 1); \
uint64_t compound = uint64_t(destination) | (status.extend_flag_ ? (1ull << size) : 0); \
compound = \
(compound << shift_count) | \
(compound >> (size + 1 - shift_count)); \
status.carry_flag_ = status.extend_flag_ = decltype(status.carry_flag_)((compound >> size) & 1); \
destination = decltype(destination)(compound); \
\
set_neg_zero_overflow(destination, 1 << (size - 1)); \
}
case Operation::ROXLm: {
2022-05-04 23:44:59 +00:00
const auto value = src.w;
src.w = uint16_t((value << 1) | (status.extend_flag_ ? 0x0001 : 0x0000));
2022-04-28 20:55:47 +00:00
status.extend_flag_ = value & 0x8000;
set_flags_w(0x8000);
} break;
2022-05-09 15:26:01 +00:00
case Operation::ROXLb: roxl(dest.b, 8); break;
case Operation::ROXLw: roxl(dest.w, 16); break;
case Operation::ROXLl: roxl(dest.l, 32); break;
2022-04-28 20:55:47 +00:00
#define roxr(destination, size) { \
decode_shift_count(); \
\
shift_count %= (size + 1); \
uint64_t compound = uint64_t(destination) | (status.extend_flag_ ? (1ull << size) : 0); \
compound = \
(compound >> shift_count) | \
(compound << (size + 1 - shift_count)); \
status.carry_flag_ = status.extend_flag_ = decltype(status.carry_flag_)((compound >> size) & 1); \
destination = decltype(destination)(compound); \
\
set_neg_zero_overflow(destination, 1 << (size - 1)); \
}
case Operation::ROXRm: {
2022-05-04 23:44:59 +00:00
const auto value = src.w;
src.w = (value >> 1) | (status.extend_flag_ ? 0x8000 : 0x0000);
2022-04-28 20:55:47 +00:00
status.extend_flag_ = value & 0x0001;
set_flags_w(0x0001);
} break;
2022-05-09 15:26:01 +00:00
case Operation::ROXRb: roxr(dest.b, 8); break;
case Operation::ROXRw: roxr(dest.w, 16); break;
case Operation::ROXRl: roxr(dest.l, 32); break;
2022-04-28 20:55:47 +00:00
#undef roxr
#undef roxl
#undef ror
#undef rol
#undef asr
#undef lsr
#undef lsl
#undef asl
#undef set_flags
#undef decode_shift_count
#undef set_flags_w
#undef set_neg_zero_overflow
#undef set_neg_zero
2022-04-28 20:55:47 +00:00
2022-05-05 13:00:33 +00:00
case Operation::MOVEPl:
2022-05-05 16:27:36 +00:00
flow_controller.template movep<uint32_t>(instruction, src.l, dest.l);
2022-05-05 13:00:33 +00:00
break;
case Operation::MOVEPw:
2022-05-05 16:27:36 +00:00
flow_controller.template movep<uint16_t>(instruction, src.l, dest.l);
2022-05-05 13:00:33 +00:00
break;
2022-05-06 13:45:06 +00:00
case Operation::MOVEMtoRl:
flow_controller.template movem_toR<uint32_t>(instruction, src.l, dest.l);
2022-05-05 16:42:57 +00:00
break;
2022-05-06 13:45:06 +00:00
case Operation::MOVEMtoMl:
flow_controller.template movem_toM<uint32_t>(instruction, src.l, dest.l);
break;
case Operation::MOVEMtoRw:
flow_controller.template movem_toR<uint16_t>(instruction, src.l, dest.l);
break;
case Operation::MOVEMtoMw:
flow_controller.template movem_toM<uint16_t>(instruction, src.l, dest.l);
2022-05-05 16:42:57 +00:00
break;
2022-04-28 20:55:47 +00:00
/*
RTE and RTR share an implementation.
*/
2022-05-06 16:30:49 +00:00
case Operation::RTR:
flow_controller.rtr();
break;
2022-04-29 21:12:06 +00:00
2022-05-06 16:30:49 +00:00
case Operation::RTE:
flow_controller.rte();
break;
case Operation::RTS:
flow_controller.rts();
break;
2022-04-28 20:55:47 +00:00
/*
TSTs: compare to zero.
*/
case Operation::TSTb:
status.carry_flag_ = status.overflow_flag_ = 0;
status.zero_result_ = src.b;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x80;
break;
case Operation::TSTw:
status.carry_flag_ = status.overflow_flag_ = 0;
status.zero_result_ = src.w;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x8000;
break;
case Operation::TSTl:
status.carry_flag_ = status.overflow_flag_ = 0;
status.zero_result_ = src.l;
2022-04-28 20:55:47 +00:00
status.negative_flag_ = status.zero_result_ & 0x80000000;
break;
case Operation::STOP:
status.set_status(src.w);
flow_controller.did_update_status();
flow_controller.stop();
break;
2022-04-28 20:55:47 +00:00
2022-05-11 11:52:23 +00:00
case Operation::RESET:
flow_controller.reset();
break;
2022-04-28 20:55:47 +00:00
/*
Development period debugging.
*/
default:
assert(false);
break;
}
#undef sub_overflow
#undef add_overflow
#undef u_extend16
#undef s_extend16
}
}
}
#endif /* InstructionSets_M68k_PerformImplementation_h */