1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-13 00:29:14 +00:00
CLK/InstructionSets/x86/Implementation/FlowControl.hpp

266 lines
7.5 KiB
C++
Raw Normal View History

//
// FlowControl.hpp
// Clock Signal
//
// Created by Thomas Harte on 08/11/2023.
// Copyright © 2023 Thomas Harte. All rights reserved.
//
#pragma once
#include "Resolver.hpp"
#include "Stack.hpp"
#include "../AccessType.hpp"
2023-11-16 15:57:17 +00:00
#include <type_traits>
namespace InstructionSet::x86::Primitive {
template <typename IntT, typename ContextT>
void jump(
2024-12-01 22:51:20 +00:00
const bool condition,
const IntT displacement,
ContextT &context
) {
/*
IF condition
THEN
EIP EIP + SignExtend(DEST);
IF OperandSize = 16
THEN
EIP EIP AND 0000FFFFH;
FI;
FI;
*/
// TODO: proper behaviour in 32-bit.
if(condition) {
context.flow_controller.template jump<uint16_t>(uint16_t(context.registers.ip() + displacement));
}
}
template <typename IntT, typename OffsetT, typename ContextT>
void loop(
modify_t<IntT> counter,
2024-12-01 22:51:20 +00:00
const OffsetT displacement,
ContextT &context
) {
--counter;
if(counter) {
context.flow_controller.template jump<uint16_t>(context.registers.ip() + displacement);
}
}
template <typename IntT, typename OffsetT, typename ContextT>
void loope(
modify_t<IntT> counter,
2024-12-01 22:51:20 +00:00
const OffsetT displacement,
ContextT &context
) {
--counter;
if(counter && context.flags.template flag<Flag::Zero>()) {
context.flow_controller.template jump<uint16_t>(context.registers.ip() + displacement);
}
}
template <typename IntT, typename OffsetT, typename ContextT>
void loopne(
modify_t<IntT> counter,
2024-12-01 22:51:20 +00:00
const OffsetT displacement,
ContextT &context
) {
--counter;
if(counter && !context.flags.template flag<Flag::Zero>()) {
context.flow_controller.template jump<uint16_t>(context.registers.ip() + displacement);
}
}
template <typename AddressT, typename ContextT>
void call_relative(
typename std::make_signed<AddressT>::type offset,
ContextT &context
) {
if constexpr (std::is_same_v<AddressT, uint16_t>) {
push<uint16_t, false>(context.registers.ip(), context);
context.flow_controller.template jump<AddressT>(AddressT(context.registers.ip() + offset));
} else {
assert(false);
}
}
template <typename IntT, typename AddressT, typename ContextT>
void call_absolute(
read_t<IntT> target,
ContextT &context
) {
push<uint16_t, false>(context.registers.ip(), context);
context.flow_controller.template jump<AddressT>(AddressT(target));
}
template <typename IntT, typename ContextT>
void jump_absolute(
read_t<IntT> target,
ContextT &context
) {
context.flow_controller.template jump<uint16_t>(target);
}
template <typename AddressT, typename InstructionT, typename ContextT>
void call_far(
InstructionT &instruction,
ContextT &context
) {
// TODO: eliminate 16-bit assumption below.
const Source source_segment = instruction.data_segment();
context.memory.preauthorise_stack_write(sizeof(uint16_t) * 2);
uint16_t source_address;
const auto pointer = instruction.destination();
switch(pointer.source()) {
default:
case Source::Immediate:
push<uint16_t, true>(context.registers.cs(), context);
push<uint16_t, true>(context.registers.ip(), context);
context.flow_controller.template jump<uint16_t>(instruction.segment(), instruction.offset());
return;
case Source::Indirect:
2024-12-01 22:51:20 +00:00
source_address = uint16_t(
address<Source::Indirect, uint16_t, AccessType::Read>(instruction, pointer, context)
);
break;
case Source::IndirectNoBase:
2024-12-01 22:51:20 +00:00
source_address = uint16_t(
address<Source::IndirectNoBase, uint16_t, AccessType::Read>(instruction, pointer, context)
);
break;
case Source::DirectAddress:
2024-12-01 22:51:20 +00:00
source_address = uint16_t(
address<Source::DirectAddress, uint16_t, AccessType::Read>(instruction, pointer, context)
);
break;
}
context.memory.preauthorise_read(source_segment, source_address, sizeof(uint16_t) * 2);
2024-12-01 22:51:20 +00:00
const auto offset =
context.memory.template access<uint16_t, AccessType::PreauthorisedRead>(source_segment, source_address);
source_address += 2;
2024-12-01 22:51:20 +00:00
const auto segment =
context.memory.template access<uint16_t, AccessType::PreauthorisedRead>(source_segment, source_address);
// At least on an 8086, the stack writes occur after the target address read.
push<uint16_t, true>(context.registers.cs(), context);
push<uint16_t, true>(context.registers.ip(), context);
context.flow_controller.template jump<AddressT>(segment, offset);
}
template <typename InstructionT, typename ContextT>
void jump_far(
InstructionT &instruction,
ContextT &context
) {
// TODO: eliminate 16-bit assumption below.
uint16_t source_address = 0;
const auto pointer = instruction.destination();
switch(pointer.source()) {
default:
2024-12-01 22:51:20 +00:00
case Source::Immediate:
context.flow_controller.template jump<uint16_t>(instruction.segment(), instruction.offset());
return;
case Source::Indirect:
2024-12-01 22:51:20 +00:00
source_address = uint16_t(
address<Source::Indirect, uint16_t, AccessType::Read>(instruction, pointer, context)
);
break;
case Source::IndirectNoBase:
2024-12-01 22:51:20 +00:00
source_address = uint16_t(
address<Source::IndirectNoBase, uint16_t, AccessType::Read>(instruction, pointer, context)
);
break;
case Source::DirectAddress:
2024-12-01 22:51:20 +00:00
source_address = uint16_t(
address<Source::DirectAddress, uint16_t, AccessType::Read>(instruction, pointer, context)
);
break;
}
const Source source_segment = instruction.data_segment();
context.memory.preauthorise_read(source_segment, source_address, sizeof(uint16_t) * 2);
2024-12-01 22:51:20 +00:00
const auto offset =
context.memory.template access<uint16_t, AccessType::PreauthorisedRead>(source_segment, source_address);
source_address += 2;
2024-12-01 22:51:20 +00:00
const auto segment =
context.memory.template access<uint16_t, AccessType::PreauthorisedRead>(source_segment, source_address);
context.flow_controller.template jump<uint16_t>(segment, offset);
}
template <typename ContextT>
void iret(
ContextT &context
) {
// TODO: all modes other than 16-bit real mode.
context.memory.preauthorise_stack_read(sizeof(uint16_t) * 3);
const auto ip = pop<uint16_t, true>(context);
const auto cs = pop<uint16_t, true>(context);
context.flags.set(pop<uint16_t, true>(context));
context.flow_controller.template jump<uint16_t>(cs, ip);
}
template <typename InstructionT, typename ContextT>
void ret_near(
2024-12-01 22:51:20 +00:00
const InstructionT instruction,
ContextT &context
) {
const auto ip = pop<uint16_t, false>(context);
context.registers.sp() += instruction.operand();
context.flow_controller.template jump<uint16_t>(ip);
}
template <typename InstructionT, typename ContextT>
void ret_far(
2024-12-01 22:51:20 +00:00
const InstructionT instruction,
ContextT &context
) {
context.memory.preauthorise_stack_read(sizeof(uint16_t) * 2);
const auto ip = pop<uint16_t, true>(context);
const auto cs = pop<uint16_t, true>(context);
context.registers.sp() += instruction.operand();
context.flow_controller.template jump<uint16_t>(cs, ip);
}
template <typename ContextT>
void into(
ContextT &context
) {
if(context.flags.template flag<Flag::Overflow>()) {
2023-11-14 03:33:46 +00:00
interrupt(Interrupt::Overflow, context);
}
}
template <typename IntT, typename InstructionT, typename ContextT>
void bound(
const InstructionT &instruction,
read_t<IntT> destination,
read_t<IntT> source,
ContextT &context
) {
using sIntT = typename std::make_signed<IntT>::type;
const auto source_segment = instruction.data_segment();
context.memory.preauthorise_read(source_segment, source, 2*sizeof(IntT));
2024-12-01 22:51:20 +00:00
const auto lower_bound =
sIntT(context.memory.template access<uint16_t, AccessType::PreauthorisedRead>(source_segment, source));
2023-11-14 03:33:46 +00:00
source += 2;
2024-12-01 22:51:20 +00:00
const auto upper_bound =
sIntT(context.memory.template access<uint16_t, AccessType::PreauthorisedRead>(source_segment, source));
2023-11-14 03:33:46 +00:00
if(sIntT(destination) < lower_bound || sIntT(destination) > upper_bound) {
interrupt(Interrupt::BoundRangeExceeded, context);
}
}
}