2023-10-05 18:37:58 +00:00
|
|
|
|
//
|
|
|
|
|
// PerformImplementation.hpp
|
|
|
|
|
// Clock Signal
|
|
|
|
|
//
|
|
|
|
|
// Created by Thomas Harte on 05/10/2023.
|
|
|
|
|
// Copyright © 2023 Thomas Harte. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#ifndef PerformImplementation_h
|
|
|
|
|
#define PerformImplementation_h
|
|
|
|
|
|
2023-10-05 19:49:07 +00:00
|
|
|
|
#include "../../../Numeric/Carry.hpp"
|
|
|
|
|
|
2023-10-05 18:37:58 +00:00
|
|
|
|
namespace InstructionSet::x86 {
|
|
|
|
|
|
|
|
|
|
namespace Primitive {
|
|
|
|
|
|
2023-10-05 19:49:07 +00:00
|
|
|
|
//
|
|
|
|
|
// BEGIN TEMPORARY COPY AND PASTE SECTION.
|
|
|
|
|
//
|
|
|
|
|
// The following are largely excised from the M68k PerformImplementation.hpp; if there proves to be no
|
|
|
|
|
// reason further to specialise them, there'll be a factoring out. In some cases I've tightened the documentation.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/// @returns An int of type @c IntT with only the most-significant bit set.
|
|
|
|
|
template <typename IntT> constexpr IntT top_bit() {
|
|
|
|
|
static_assert(!std::numeric_limits<IntT>::is_signed);
|
|
|
|
|
constexpr IntT max = std::numeric_limits<IntT>::max();
|
|
|
|
|
return max - (max >> 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @returns The number of bits in @c IntT.
|
|
|
|
|
template <typename IntT> constexpr int bit_size() {
|
|
|
|
|
return sizeof(IntT) * 8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @returns An int with the top bit indicating whether overflow occurred during the calculation of
|
|
|
|
|
/// • @c lhs + @c rhs (if @c is_add is true); or
|
|
|
|
|
/// • @c lhs - @c rhs (if @c is_add is false)
|
|
|
|
|
/// and the result was @c result. All other bits will be clear.
|
|
|
|
|
template <bool is_add, typename IntT>
|
|
|
|
|
IntT overflow(IntT lhs, IntT rhs, IntT result) {
|
|
|
|
|
const IntT output_changed = result ^ rhs;
|
|
|
|
|
const IntT input_differed = lhs ^ rhs;
|
|
|
|
|
|
|
|
|
|
if constexpr (is_add) {
|
|
|
|
|
return top_bit<IntT>() & output_changed & ~input_differed;
|
|
|
|
|
} else {
|
|
|
|
|
return top_bit<IntT>() & output_changed & input_differed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// END COPY AND PASTE SECTION.
|
|
|
|
|
//
|
|
|
|
|
|
2023-10-05 18:37:58 +00:00
|
|
|
|
void aaa(CPU::RegisterPair16 &ax, Status &status) {
|
|
|
|
|
/*
|
|
|
|
|
IF ((AL AND 0FH) > 9) OR (AF = 1)
|
|
|
|
|
THEN
|
|
|
|
|
AL ← (AL + 6);
|
|
|
|
|
AH ← AH + 1;
|
|
|
|
|
AF ← 1;
|
|
|
|
|
CF ← 1;
|
|
|
|
|
ELSE
|
|
|
|
|
AF ← 0;
|
|
|
|
|
CF ← 0;
|
|
|
|
|
FI;
|
|
|
|
|
AL ← AL AND 0FH;
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
The AF and CF flags are set to 1 if the adjustment results in a decimal carry;
|
|
|
|
|
otherwise they are cleared to 0. The OF, SF, ZF, and PF flags are undefined.
|
|
|
|
|
*/
|
|
|
|
|
if((ax.halves.low & 0x0f) > 9 || status.auxiliary_carry) {
|
|
|
|
|
ax.halves.low += 6;
|
|
|
|
|
++ax.halves.high;
|
|
|
|
|
status.auxiliary_carry = status.carry = 1;
|
|
|
|
|
} else {
|
|
|
|
|
status.auxiliary_carry = status.carry = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void aad(CPU::RegisterPair16 &ax, uint8_t imm, Status &status) {
|
|
|
|
|
/*
|
|
|
|
|
tempAL ← AL;
|
|
|
|
|
tempAH ← AH;
|
|
|
|
|
AL ← (tempAL + (tempAH * imm8)) AND FFH; (* imm8 is set to 0AH for the AAD mnemonic *)
|
|
|
|
|
AH ← 0
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
The SF, ZF, and PF flags are set according to the result;
|
|
|
|
|
the OF, AF, and CF flags are undefined.
|
|
|
|
|
*/
|
|
|
|
|
ax.halves.low = ax.halves.low + (ax.halves.high * imm);
|
|
|
|
|
ax.halves.high = 0;
|
|
|
|
|
status.sign = ax.halves.low & 0x80;
|
|
|
|
|
status.parity = status.zero = ax.halves.low;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 18:52:24 +00:00
|
|
|
|
void aam(CPU::RegisterPair16 &ax, uint8_t imm, Status &status) {
|
|
|
|
|
/*
|
|
|
|
|
tempAL ← AL;
|
|
|
|
|
AH ← tempAL / imm8; (* imm8 is set to 0AH for the AAD mnemonic *)
|
|
|
|
|
AL ← tempAL MOD imm8;
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
The SF, ZF, and PF flags are set according to the result.
|
|
|
|
|
The OF, AF, and CF flags are undefined.
|
|
|
|
|
*/
|
|
|
|
|
ax.halves.high = ax.halves.low / imm;
|
|
|
|
|
ax.halves.low = ax.halves.low % imm;
|
|
|
|
|
status.sign = ax.halves.low & 0x80;
|
|
|
|
|
status.parity = status.zero = ax.halves.low;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void aas(CPU::RegisterPair16 &ax, Status &status) {
|
|
|
|
|
/*
|
|
|
|
|
IF ((AL AND 0FH) > 9) OR (AF = 1)
|
|
|
|
|
THEN
|
|
|
|
|
AL ← AL – 6;
|
|
|
|
|
AH ← AH – 1;
|
|
|
|
|
AF ← 1;
|
|
|
|
|
CF ← 1;
|
|
|
|
|
ELSE
|
|
|
|
|
CF ← 0;
|
|
|
|
|
AF ← 0;
|
|
|
|
|
FI;
|
|
|
|
|
AL ← AL AND 0FH;
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
The AF and CF flags are set to 1 if there is a decimal borrow;
|
|
|
|
|
otherwise, they are cleared to 0. The OF, SF, ZF, and PF flags are undefined.
|
|
|
|
|
*/
|
|
|
|
|
if((ax.halves.low & 0x0f) > 9 || status.auxiliary_carry) {
|
|
|
|
|
ax.halves.low -= 6;
|
|
|
|
|
--ax.halves.high;
|
|
|
|
|
status.auxiliary_carry = status.carry = 1;
|
|
|
|
|
} else {
|
|
|
|
|
status.auxiliary_carry = status.carry = 0;
|
|
|
|
|
}
|
|
|
|
|
ax.halves.low &= 0x0f;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 19:49:07 +00:00
|
|
|
|
template <typename IntT>
|
|
|
|
|
void adc(IntT &destination, IntT source, Status &status) {
|
|
|
|
|
/*
|
|
|
|
|
DEST ← DEST + SRC + CF;
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
The OF, SF, ZF, AF, CF, and PF flags are set according to the result.
|
|
|
|
|
*/
|
|
|
|
|
const IntT result = destination + source + status.carry_bit<IntT>();
|
|
|
|
|
|
|
|
|
|
status.carry = Numeric::carried_out<bit_size<IntT>() - 1>(destination, source, result);
|
|
|
|
|
status.auxiliary_carry = Numeric::carried_in<4>(destination, source, result);
|
|
|
|
|
status.sign = status.zero = status.parity = result;
|
|
|
|
|
status.overflow = overflow<true, IntT>(destination, source, result);
|
|
|
|
|
|
|
|
|
|
destination = result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename IntT>
|
|
|
|
|
void add(IntT &destination, IntT source, Status &status) {
|
|
|
|
|
/*
|
|
|
|
|
DEST ← DEST + SRC;
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
The OF, SF, ZF, AF, CF, and PF flags are set according to the result.
|
|
|
|
|
*/
|
|
|
|
|
const IntT result = destination + source;
|
|
|
|
|
|
|
|
|
|
status.carry = Numeric::carried_out<bit_size<IntT>() - 1>(destination, source, result);
|
|
|
|
|
status.auxiliary_carry = Numeric::carried_in<4>(destination, source, result);
|
|
|
|
|
status.sign = status.zero = status.parity = result;
|
|
|
|
|
status.overflow = overflow<true, IntT>(destination, source, result);
|
|
|
|
|
|
|
|
|
|
destination = result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 18:37:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <
|
|
|
|
|
Model model,
|
|
|
|
|
Operation operation,
|
|
|
|
|
DataSize data_size,
|
|
|
|
|
typename FlowControllerT
|
|
|
|
|
> void perform(
|
|
|
|
|
CPU::RegisterPair16 &destination,
|
|
|
|
|
CPU::RegisterPair16 &source,
|
|
|
|
|
Status &status,
|
|
|
|
|
FlowControllerT &flow_controller
|
|
|
|
|
) {
|
|
|
|
|
switch(operation) {
|
|
|
|
|
case Operation::AAA: Primitive::aaa(destination, status); break;
|
|
|
|
|
case Operation::AAD: Primitive::aad(destination, source.halves.low, status); break;
|
2023-10-05 18:52:24 +00:00
|
|
|
|
case Operation::AAM: Primitive::aam(destination, source.halves.low, status); break;
|
|
|
|
|
case Operation::AAS: Primitive::aas(destination, status); break;
|
2023-10-05 19:49:07 +00:00
|
|
|
|
|
|
|
|
|
case Operation::ADC:
|
|
|
|
|
static_assert(operation != Operation::ADC || data_size == DataSize::Byte || data_size == DataSize::Word);
|
|
|
|
|
switch(data_size) {
|
|
|
|
|
case DataSize::Byte: Primitive::adc(destination.halves.low, source.halves.low, status); break;
|
|
|
|
|
case DataSize::Word: Primitive::adc(destination.full, source.full, status); break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Operation::ADD:
|
|
|
|
|
static_assert(operation != Operation::ADD || data_size == DataSize::Byte || data_size == DataSize::Word);
|
|
|
|
|
switch(data_size) {
|
|
|
|
|
case DataSize::Byte: Primitive::add(destination.halves.low, source.halves.low, status); break;
|
|
|
|
|
case DataSize::Word: Primitive::add(destination.full, source.full, status); break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2023-10-05 18:37:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(void)flow_controller;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*template <
|
|
|
|
|
Model model,
|
|
|
|
|
typename InstructionT,
|
|
|
|
|
typename FlowControllerT,
|
|
|
|
|
typename DataPointerResolverT,
|
|
|
|
|
typename RegistersT,
|
|
|
|
|
typename MemoryT,
|
|
|
|
|
typename IOT,
|
|
|
|
|
Operation operation
|
|
|
|
|
> void perform(
|
|
|
|
|
const InstructionT &instruction,
|
|
|
|
|
Status &status,
|
|
|
|
|
FlowControllerT &flow_controller,
|
|
|
|
|
DataPointerResolverT &resolver,
|
|
|
|
|
RegistersT ®isters,
|
|
|
|
|
MemoryT &memory,
|
|
|
|
|
IOT &io
|
|
|
|
|
) {
|
|
|
|
|
switch((operation != Operation::Invalid) ? operation : instruction.operation) {
|
|
|
|
|
default:
|
|
|
|
|
assert(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* PerformImplementation_h */
|