2024-02-17 02:35:49 +00:00
|
|
|
|
//
|
2024-02-21 20:32:27 +00:00
|
|
|
|
// OperationMapper.hpp
|
2024-02-17 02:35:49 +00:00
|
|
|
|
// Clock Signal
|
|
|
|
|
//
|
|
|
|
|
// Created by Thomas Harte on 16/02/2024.
|
|
|
|
|
// Copyright © 2024 Thomas Harte. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2024-02-21 19:17:01 +00:00
|
|
|
|
#include "../../Reflection/Dispatcher.hpp"
|
|
|
|
|
|
2024-02-17 02:35:49 +00:00
|
|
|
|
namespace InstructionSet::ARM {
|
|
|
|
|
|
2024-02-21 20:32:27 +00:00
|
|
|
|
enum class Model {
|
|
|
|
|
ARM2,
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-26 19:30:26 +00:00
|
|
|
|
enum class DataProcessingOperation {
|
2024-02-22 15:16:54 +00:00
|
|
|
|
AND, /// Rd = Op1 AND Op2.
|
|
|
|
|
EOR, /// Rd = Op1 EOR Op2.
|
|
|
|
|
SUB, /// Rd = Op1 - Op2.
|
|
|
|
|
RSB, /// Rd = Op2 - Op1.
|
|
|
|
|
ADD, /// Rd = Op1 + Op2.
|
|
|
|
|
ADC, /// Rd = Op1 + Ord2 + C.
|
|
|
|
|
SBC, /// Rd = Op1 - Op2 + C.
|
|
|
|
|
RSC, /// Rd = Op2 - Op1 + C.
|
|
|
|
|
TST, /// Set condition codes on Op1 AND Op2.
|
|
|
|
|
TEQ, /// Set condition codes on Op1 EOR Op2.
|
|
|
|
|
CMP, /// Set condition codes on Op1 - Op2.
|
|
|
|
|
CMN, /// Set condition codes on Op1 + Op2.
|
|
|
|
|
ORR, /// Rd = Op1 OR Op2.
|
|
|
|
|
MOV, /// Rd = Op2
|
|
|
|
|
BIC, /// Rd = Op1 AND NOT Op2.
|
|
|
|
|
MVN, /// Rd = NOT Op2.
|
2024-02-26 19:30:26 +00:00
|
|
|
|
};
|
2024-02-22 15:16:54 +00:00
|
|
|
|
|
2024-02-26 19:50:45 +00:00
|
|
|
|
constexpr bool is_logical(DataProcessingOperation operation) {
|
|
|
|
|
switch(operation) {
|
|
|
|
|
case DataProcessingOperation::AND:
|
|
|
|
|
case DataProcessingOperation::EOR:
|
|
|
|
|
case DataProcessingOperation::TST:
|
|
|
|
|
case DataProcessingOperation::TEQ:
|
|
|
|
|
case DataProcessingOperation::ORR:
|
|
|
|
|
case DataProcessingOperation::MOV:
|
|
|
|
|
case DataProcessingOperation::BIC:
|
|
|
|
|
case DataProcessingOperation::MVN:
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
default: return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 02:27:58 +00:00
|
|
|
|
constexpr bool is_comparison(DataProcessingOperation operation) {
|
|
|
|
|
switch(operation) {
|
|
|
|
|
case DataProcessingOperation::TST:
|
|
|
|
|
case DataProcessingOperation::TEQ:
|
|
|
|
|
case DataProcessingOperation::CMP:
|
|
|
|
|
case DataProcessingOperation::CMN:
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
default: return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 19:30:26 +00:00
|
|
|
|
enum class Operation {
|
2024-02-22 15:16:54 +00:00
|
|
|
|
MUL, /// Rd = Rm * Rs
|
|
|
|
|
MLA, /// Rd = Rm * Rs + Rn
|
|
|
|
|
B, /// Add offset to PC; programmer allows for PC being two words ahead.
|
|
|
|
|
BL, /// Copy PC and PSR to R14, then branch. Copied PC points to next instruction.
|
|
|
|
|
|
|
|
|
|
LDR, /// Read single byte or word from [base + offset], possibly mutating the base.
|
|
|
|
|
STR, /// Write a single byte or word to [base + offset], possibly mutating the base.
|
|
|
|
|
LDM, /// Read 1–16 words from [base], possibly mutating it.
|
|
|
|
|
STM, /// Write 1-16 words to [base], possibly mutating it.
|
|
|
|
|
SWI, /// Perform a software interrupt.
|
|
|
|
|
|
|
|
|
|
CDP, /// Coprocessor data operation.
|
|
|
|
|
MRC, /// Move from coprocessor register to ARM register.
|
|
|
|
|
MCR, /// Move from ARM register to coprocessor register.
|
|
|
|
|
LDC, /// Coprocessor data transfer load.
|
|
|
|
|
STC, /// Coprocessor data transfer store.
|
2024-02-21 20:32:27 +00:00
|
|
|
|
|
|
|
|
|
Undefined,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class Condition {
|
|
|
|
|
EQ, NE, CS, CC,
|
|
|
|
|
MI, PL, VS, VC,
|
|
|
|
|
HI, LS, GE, LT,
|
|
|
|
|
GT, LE, AL, NV,
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-21 19:17:01 +00:00
|
|
|
|
enum class ShiftType {
|
|
|
|
|
LogicalLeft = 0b00,
|
|
|
|
|
LogicalRight = 0b01,
|
|
|
|
|
ArithmeticRight = 0b10,
|
|
|
|
|
RotateRight = 0b11,
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-22 15:16:54 +00:00
|
|
|
|
//
|
|
|
|
|
// Implementation details.
|
|
|
|
|
//
|
2024-02-21 19:17:01 +00:00
|
|
|
|
static constexpr int FlagsStartBit = 20;
|
2024-02-22 15:16:54 +00:00
|
|
|
|
using Flags = uint8_t;
|
2024-02-21 19:17:01 +00:00
|
|
|
|
|
|
|
|
|
template <int position>
|
|
|
|
|
constexpr bool flag_bit(uint8_t flags) {
|
|
|
|
|
static_assert(position >= 20 && position < 28);
|
|
|
|
|
return flags & (1 << (position - FlagsStartBit));
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 16:20:22 +00:00
|
|
|
|
//
|
|
|
|
|
// Methods common to data processing and data transfer.
|
|
|
|
|
//
|
2024-02-21 19:51:51 +00:00
|
|
|
|
struct WithShiftControlBits {
|
|
|
|
|
constexpr WithShiftControlBits(uint32_t opcode) noexcept : opcode_(opcode) {}
|
|
|
|
|
|
|
|
|
|
/// The operand 2 register index if @c operand2_is_immediate() is @c false; meaningless otherwise.
|
2024-02-27 20:30:51 +00:00
|
|
|
|
int operand2() const { return opcode_ & 0xf; }
|
2024-02-21 19:51:51 +00:00
|
|
|
|
/// The type of shift to apply to operand 2 if @c operand2_is_immediate() is @c false; meaningless otherwise.
|
|
|
|
|
ShiftType shift_type() const { return ShiftType((opcode_ >> 5) & 3); }
|
|
|
|
|
/// @returns @c true if the amount to shift by should be taken from a register; @c false if it is an immediate value.
|
|
|
|
|
bool shift_count_is_register() const { return opcode_ & (1 << 4); }
|
|
|
|
|
/// The shift amount register index if @c shift_count_is_register() is @c true; meaningless otherwise.
|
|
|
|
|
int shift_register() const { return (opcode_ >> 8) & 0xf; }
|
|
|
|
|
/// The amount to shift by if @c shift_count_is_register() is @c false; meaningless otherwise.
|
|
|
|
|
int shift_amount() const { return (opcode_ >> 7) & 0x1f; }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
uint32_t opcode_;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-22 15:16:54 +00:00
|
|
|
|
//
|
|
|
|
|
// Branch (i.e. B and BL).
|
|
|
|
|
//
|
|
|
|
|
struct Branch {
|
|
|
|
|
constexpr Branch(uint32_t opcode) noexcept : opcode_(opcode) {}
|
|
|
|
|
|
|
|
|
|
/// The 26-bit offset to add to the PC.
|
|
|
|
|
int offset() const { return (opcode_ & 0xff'ffff) << 2; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint32_t opcode_;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-21 19:17:01 +00:00
|
|
|
|
//
|
|
|
|
|
// Data processing (i.e. AND to MVN).
|
|
|
|
|
//
|
|
|
|
|
struct DataProcessingFlags {
|
|
|
|
|
constexpr DataProcessingFlags(uint8_t flags) noexcept : flags_(flags) {}
|
|
|
|
|
|
2024-02-26 19:30:26 +00:00
|
|
|
|
/// @returns The operation to apply.
|
|
|
|
|
constexpr DataProcessingOperation operation() const {
|
|
|
|
|
return DataProcessingOperation((flags_ >> 21) & 0xf);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 19:17:01 +00:00
|
|
|
|
/// @returns @c true if operand 2 is defined by the @c rotate() and @c immediate() fields;
|
|
|
|
|
/// @c false if it is defined by the @c shift_*() and @c operand2() fields.
|
2024-02-26 19:30:26 +00:00
|
|
|
|
constexpr bool operand2_is_immediate() const { return flag_bit<25>(flags_); }
|
2024-02-21 19:17:01 +00:00
|
|
|
|
|
2024-02-22 16:20:22 +00:00
|
|
|
|
/// @c true if the status register should be updated; @c false otherwise.
|
2024-02-26 19:30:26 +00:00
|
|
|
|
constexpr bool set_condition_codes() const { return flag_bit<20>(flags_); }
|
2024-02-21 19:17:01 +00:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint8_t flags_;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-21 19:51:51 +00:00
|
|
|
|
struct DataProcessing: public WithShiftControlBits {
|
|
|
|
|
using WithShiftControlBits::WithShiftControlBits;
|
2024-02-21 19:17:01 +00:00
|
|
|
|
|
|
|
|
|
/// The destination register index. i.e. Rd.
|
|
|
|
|
int destination() const { return (opcode_ >> 12) & 0xf; }
|
|
|
|
|
|
|
|
|
|
/// The operand 1 register index. i.e. Rn.
|
|
|
|
|
int operand1() const { return (opcode_ >> 16) & 0xf; }
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Immediate values for operand 2.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/// An 8-bit value to rotate right @c rotate() places if @c operand2_is_immediate() is @c true; meaningless otherwise.
|
|
|
|
|
int immediate() const { return opcode_ & 0xff; }
|
|
|
|
|
/// The number of bits to rotate @c immediate() by to the right if @c operand2_is_immediate() is @c true; meaningless otherwise.
|
|
|
|
|
int rotate() const { return (opcode_ >> 7) & 0x1e; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// MUL and MLA.
|
|
|
|
|
//
|
|
|
|
|
struct MultiplyFlags {
|
|
|
|
|
constexpr MultiplyFlags(uint8_t flags) noexcept : flags_(flags) {}
|
|
|
|
|
|
2024-02-22 16:20:22 +00:00
|
|
|
|
/// @c true if the status register should be updated; @c false otherwise.
|
2024-02-21 19:17:01 +00:00
|
|
|
|
constexpr bool set_condition_codes() { return flag_bit<20>(flags_); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint8_t flags_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Multiply {
|
|
|
|
|
constexpr Multiply(uint32_t opcode) noexcept : opcode_(opcode) {}
|
|
|
|
|
|
|
|
|
|
/// The destination register index. i.e. 'Rd'.
|
|
|
|
|
int destination() const { return (opcode_ >> 16) & 0xf; }
|
|
|
|
|
|
|
|
|
|
/// The accumulator register index for multiply-add. i.e. 'Rn'.
|
|
|
|
|
int accumulator() const { return (opcode_ >> 12) & 0xf; }
|
|
|
|
|
|
|
|
|
|
/// The multiplicand register index. i.e. 'Rs'.
|
|
|
|
|
int multiplicand() const { return (opcode_ >> 8) & 0xf; }
|
|
|
|
|
|
|
|
|
|
/// The multiplier register index. i.e. 'Rm'.
|
|
|
|
|
int multiplier() const { return opcode_ & 0xf; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint32_t opcode_;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-21 19:51:51 +00:00
|
|
|
|
//
|
|
|
|
|
// Single data transfer (LDR, STR).
|
|
|
|
|
//
|
|
|
|
|
struct SingleDataTransferFlags {
|
|
|
|
|
constexpr SingleDataTransferFlags(uint8_t flags) noexcept : flags_(flags) {}
|
|
|
|
|
|
|
|
|
|
constexpr bool offset_is_immediate() { return flag_bit<25>(flags_); }
|
|
|
|
|
constexpr bool pre_index() { return flag_bit<24>(flags_); }
|
|
|
|
|
constexpr bool add_offset() { return flag_bit<23>(flags_); }
|
|
|
|
|
constexpr bool transfer_byte() { return flag_bit<22>(flags_); }
|
|
|
|
|
constexpr bool write_back_address() { return flag_bit<21>(flags_); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint8_t flags_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct SingleDataTransfer: public WithShiftControlBits {
|
|
|
|
|
using WithShiftControlBits::WithShiftControlBits;
|
|
|
|
|
|
|
|
|
|
/// The destination register index. i.e. 'Rd' for LDR.
|
|
|
|
|
int destination() const { return (opcode_ >> 12) & 0xf; }
|
|
|
|
|
|
|
|
|
|
/// The destination register index. i.e. 'Rd' for STR.
|
|
|
|
|
int source() const { return (opcode_ >> 12) & 0xf; }
|
|
|
|
|
|
|
|
|
|
/// The base register index. i.e. 'Rn'.
|
|
|
|
|
int base() const { return (opcode_ >> 16) & 0xf; }
|
|
|
|
|
|
|
|
|
|
/// The immediate offset, if @c offset_is_immediate() was @c true; meaningless otherwise.
|
|
|
|
|
int immediate() const { return opcode_ & 0xfff; }
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-21 20:25:57 +00:00
|
|
|
|
//
|
|
|
|
|
// Block data transfer (LDR, STR).
|
|
|
|
|
//
|
|
|
|
|
struct BlockDataTransferFlags {
|
|
|
|
|
constexpr BlockDataTransferFlags(uint8_t flags) noexcept : flags_(flags) {}
|
|
|
|
|
|
|
|
|
|
constexpr bool pre_index() { return flag_bit<24>(flags_); }
|
|
|
|
|
constexpr bool add_offset() { return flag_bit<23>(flags_); }
|
|
|
|
|
constexpr bool load_psr() { return flag_bit<22>(flags_); }
|
|
|
|
|
constexpr bool write_back_address() { return flag_bit<21>(flags_); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint8_t flags_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct BlockDataTransfer: public WithShiftControlBits {
|
|
|
|
|
using WithShiftControlBits::WithShiftControlBits;
|
|
|
|
|
|
|
|
|
|
/// The base register index. i.e. 'Rn'.
|
|
|
|
|
int base() const { return (opcode_ >> 16) & 0xf; }
|
|
|
|
|
|
|
|
|
|
/// A bitfield indicating which registers to load or store.
|
|
|
|
|
int register_list() const { return opcode_ & 0xffff; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//
|
2024-02-22 15:16:54 +00:00
|
|
|
|
// Coprocessor data operation.
|
2024-02-21 20:25:57 +00:00
|
|
|
|
//
|
|
|
|
|
struct CoprocessorDataOperationFlags {
|
|
|
|
|
constexpr CoprocessorDataOperationFlags(uint8_t flags) noexcept : flags_(flags) {}
|
|
|
|
|
|
|
|
|
|
constexpr int operation() const { return (flags_ >> (FlagsStartBit - 20)) & 0xf; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint8_t flags_;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-22 15:16:54 +00:00
|
|
|
|
struct CoprocessorDataOperation {
|
|
|
|
|
constexpr CoprocessorDataOperation(uint32_t opcode) noexcept : opcode_(opcode) {}
|
|
|
|
|
|
|
|
|
|
int operand1() { return (opcode_ >> 16) & 0xf; }
|
|
|
|
|
int operand2() { return opcode_ & 0xf; }
|
|
|
|
|
int destination() { return (opcode_ >> 12) & 0xf; }
|
|
|
|
|
int coprocessor() { return (opcode_ >> 8) & 0xf; }
|
|
|
|
|
int information() { return (opcode_ >> 5) & 0x7; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint32_t opcode_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Coprocessor register transfer.
|
|
|
|
|
//
|
2024-02-21 20:25:57 +00:00
|
|
|
|
struct CoprocessorRegisterTransferFlags {
|
|
|
|
|
constexpr CoprocessorRegisterTransferFlags(uint8_t flags) noexcept : flags_(flags) {}
|
|
|
|
|
|
|
|
|
|
constexpr int operation() const { return (flags_ >> (FlagsStartBit - 20)) & 0x7; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint8_t flags_;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-22 15:16:54 +00:00
|
|
|
|
struct CoprocessorRegisterTransfer {
|
|
|
|
|
constexpr CoprocessorRegisterTransfer(uint32_t opcode) noexcept : opcode_(opcode) {}
|
2024-02-21 20:25:57 +00:00
|
|
|
|
|
|
|
|
|
int operand1() { return (opcode_ >> 16) & 0xf; }
|
|
|
|
|
int operand2() { return opcode_ & 0xf; }
|
|
|
|
|
int destination() { return (opcode_ >> 12) & 0xf; }
|
|
|
|
|
int coprocessor() { return (opcode_ >> 8) & 0xf; }
|
|
|
|
|
int information() { return (opcode_ >> 5) & 0x7; }
|
|
|
|
|
|
2024-02-21 20:32:27 +00:00
|
|
|
|
private:
|
|
|
|
|
uint32_t opcode_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Coprocessor data transfer.
|
|
|
|
|
//
|
|
|
|
|
struct CoprocessorDataTransferFlags {
|
|
|
|
|
constexpr CoprocessorDataTransferFlags(uint8_t flags) noexcept : flags_(flags) {}
|
|
|
|
|
|
|
|
|
|
constexpr bool pre_index() { return flag_bit<24>(flags_); }
|
|
|
|
|
constexpr bool add_offset() { return flag_bit<23>(flags_); }
|
|
|
|
|
constexpr bool transfer_length() { return flag_bit<22>(flags_); }
|
|
|
|
|
constexpr bool write_back_address() { return flag_bit<21>(flags_); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint8_t flags_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct CoprocessorDataTransfer {
|
|
|
|
|
constexpr CoprocessorDataTransfer(uint32_t opcode) noexcept : opcode_(opcode) {}
|
|
|
|
|
|
|
|
|
|
int base() { return (opcode_ >> 16) & 0xf; }
|
|
|
|
|
|
|
|
|
|
int source() { return (opcode_ >> 12) & 0xf; }
|
|
|
|
|
int destination() { return (opcode_ >> 12) & 0xf; }
|
|
|
|
|
|
|
|
|
|
int coprocessor() { return (opcode_ >> 8) & 0xf; }
|
|
|
|
|
int offset() { return opcode_ & 0xff; }
|
|
|
|
|
|
|
|
|
|
private:
|
2024-02-21 20:25:57 +00:00
|
|
|
|
uint32_t opcode_;
|
|
|
|
|
};
|
2024-02-21 19:17:01 +00:00
|
|
|
|
|
2024-02-21 20:32:27 +00:00
|
|
|
|
/// Operation mapper; use the free function @c dispatch as defined below.
|
2024-02-21 19:17:01 +00:00
|
|
|
|
struct OperationMapper {
|
2024-02-27 02:36:23 +00:00
|
|
|
|
static Condition condition(uint32_t instruction) {
|
|
|
|
|
return Condition(instruction >> 28);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <int i, typename SchedulerT>
|
|
|
|
|
static void dispatch(uint32_t instruction, SchedulerT &scheduler) {
|
|
|
|
|
// Legacy: grab condition. This'll be eliminated.
|
|
|
|
|
// TODO: eliminate.
|
|
|
|
|
const auto condition = Condition(instruction >> 28);
|
|
|
|
|
|
|
|
|
|
// Put the 8-bit segment of instruction back into its proper place;
|
|
|
|
|
// this allows all the tests below to be written so as to coordinate
|
|
|
|
|
// properly with the data sheet, and since it's all compile-time work
|
|
|
|
|
// it doesn't cost anything.
|
2024-02-21 20:25:57 +00:00
|
|
|
|
constexpr auto partial = uint32_t(i << 20);
|
2024-02-21 19:17:01 +00:00
|
|
|
|
|
2024-02-21 19:18:41 +00:00
|
|
|
|
// Cf. the ARM2 datasheet, p.45. Tests below match its ordering
|
|
|
|
|
// other than that 'undefined' is the fallthrough case. More specific
|
|
|
|
|
// page references are provided were more detailed versions of the
|
|
|
|
|
// decoding are depicted.
|
|
|
|
|
|
2024-02-21 19:17:01 +00:00
|
|
|
|
// Data processing; cf. p.17.
|
|
|
|
|
if constexpr (((partial >> 26) & 0b11) == 0b00) {
|
2024-02-27 02:36:23 +00:00
|
|
|
|
scheduler.template perform<i>(DataProcessing(instruction));
|
2024-02-22 15:16:54 +00:00
|
|
|
|
return;
|
2024-02-21 19:17:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Multiply and multiply-accumulate (MUL, MLA); cf. p.23.
|
2024-02-21 19:51:51 +00:00
|
|
|
|
if constexpr (((partial >> 22) & 0b111'111) == 0b000'000) {
|
|
|
|
|
// This implementation provides only eight bits baked into the template parameters so
|
|
|
|
|
// an additional dynamic test is required to check whether this is really, really MUL or MLA.
|
2024-02-22 15:16:54 +00:00
|
|
|
|
if(((instruction >> 4) & 0b1111) == 0b1001) {
|
2024-02-21 19:17:01 +00:00
|
|
|
|
constexpr bool is_mla = partial & (1 << 21);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
scheduler.template perform<is_mla ? Operation::MLA : Operation::MUL, i>(
|
2024-02-21 20:25:57 +00:00
|
|
|
|
condition,
|
2024-02-21 19:17:01 +00:00
|
|
|
|
Multiply(instruction)
|
|
|
|
|
);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
return;
|
2024-02-21 19:17:01 +00:00
|
|
|
|
}
|
2024-02-21 19:51:51 +00:00
|
|
|
|
}
|
2024-02-21 19:17:01 +00:00
|
|
|
|
|
2024-02-21 19:51:51 +00:00
|
|
|
|
// Single data transfer (LDR, STR); cf. p.25.
|
|
|
|
|
if constexpr (((partial >> 26) & 0b11) == 0b01) {
|
|
|
|
|
constexpr bool is_ldr = partial & (1 << 20);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
scheduler.template perform<is_ldr ? Operation::LDR : Operation::STR, i>(
|
2024-02-21 20:25:57 +00:00
|
|
|
|
condition,
|
2024-02-21 19:51:51 +00:00
|
|
|
|
SingleDataTransfer(instruction)
|
|
|
|
|
);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
return;
|
2024-02-21 19:17:01 +00:00
|
|
|
|
}
|
2024-02-21 20:25:57 +00:00
|
|
|
|
|
|
|
|
|
// Block data transfer (LDM, STM); cf. p.29.
|
|
|
|
|
if constexpr (((partial >> 25) & 0b111) == 0b100) {
|
|
|
|
|
constexpr bool is_ldm = partial & (1 << 20);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
scheduler.template perform<is_ldm ? Operation::LDM : Operation::STM, i>(
|
2024-02-21 20:25:57 +00:00
|
|
|
|
condition,
|
|
|
|
|
BlockDataTransfer(instruction)
|
|
|
|
|
);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
return;
|
2024-02-21 20:25:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Branch and branch with link (B, BL); cf. p.15.
|
|
|
|
|
if constexpr (((partial >> 25) & 0b111) == 0b101) {
|
|
|
|
|
constexpr bool is_bl = partial & (1 << 24);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
scheduler.template perform<is_bl ? Operation::BL : Operation::B>(
|
2024-02-21 20:25:57 +00:00
|
|
|
|
condition,
|
2024-02-22 15:16:54 +00:00
|
|
|
|
Branch(instruction)
|
2024-02-21 20:25:57 +00:00
|
|
|
|
);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
return;
|
2024-02-21 20:25:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Software interreupt; cf. p.35.
|
|
|
|
|
if constexpr (((partial >> 24) & 0b1111) == 0b1111) {
|
|
|
|
|
scheduler.software_interrupt(condition);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
return;
|
2024-02-21 20:25:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Both:
|
|
|
|
|
// Coprocessor data operation; cf. p. 37; and
|
|
|
|
|
// Coprocessor register transfers; cf. p. 42.
|
|
|
|
|
if constexpr (((partial >> 24) & 0b1111) == 0b1110) {
|
|
|
|
|
if(instruction & (1 << 4)) {
|
|
|
|
|
// Register transfer.
|
2024-02-22 15:16:54 +00:00
|
|
|
|
const auto parameters = CoprocessorRegisterTransfer(instruction);
|
2024-02-21 20:25:57 +00:00
|
|
|
|
constexpr bool is_mrc = partial & (1 << 20);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
scheduler.template perform<is_mrc ? Operation::MRC : Operation::MCR, i>(
|
2024-02-21 20:25:57 +00:00
|
|
|
|
condition,
|
|
|
|
|
parameters
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
// Data operation.
|
2024-02-22 15:16:54 +00:00
|
|
|
|
const auto parameters = CoprocessorDataOperation(instruction);
|
|
|
|
|
scheduler.template perform<i>(
|
2024-02-21 20:25:57 +00:00
|
|
|
|
condition,
|
|
|
|
|
parameters
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-02-22 15:16:54 +00:00
|
|
|
|
return;
|
2024-02-21 20:25:57 +00:00
|
|
|
|
}
|
2024-02-21 20:32:27 +00:00
|
|
|
|
|
|
|
|
|
// Coprocessor data transfers; cf. p.39.
|
|
|
|
|
if constexpr (((partial >> 25) & 0b111) == 0b110) {
|
|
|
|
|
constexpr bool is_ldc = partial & (1 << 20);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
scheduler.template perform<is_ldc ? Operation::LDC : Operation::STC, i>(
|
2024-02-21 20:32:27 +00:00
|
|
|
|
condition,
|
|
|
|
|
CoprocessorDataTransfer(instruction)
|
|
|
|
|
);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
return;
|
2024-02-21 20:32:27 +00:00
|
|
|
|
}
|
2024-02-22 15:16:54 +00:00
|
|
|
|
|
|
|
|
|
// Fallback position.
|
|
|
|
|
scheduler.unknown(instruction);
|
2024-02-21 19:17:01 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-22 15:16:54 +00:00
|
|
|
|
/// A brief documentation of the interface expected by @c dispatch below; will be a concept if/when this project adopts C++20.
|
|
|
|
|
struct SampleScheduler {
|
|
|
|
|
// General template arguments:
|
|
|
|
|
//
|
|
|
|
|
// (1) Operation, telling the function which operation to perform. Will always be from the subset
|
|
|
|
|
// implied by the operation category; and
|
|
|
|
|
// (2) Flags, an opaque type which can be converted into a DataProcessingFlags, MultiplyFlags, etc,
|
|
|
|
|
// by simply construction, to provide all flags that can be baked into the template parameters.
|
|
|
|
|
//
|
|
|
|
|
// Arguments are ommitted if not relevant.
|
|
|
|
|
//
|
|
|
|
|
// Function arguments:
|
|
|
|
|
//
|
|
|
|
|
// (1) Condition, indicating the condition code associated with this operation; and
|
|
|
|
|
// (2) An operation-specific encapsulation of the operation code for decoding of fields that didn't
|
|
|
|
|
// fit into the template parameters.
|
2024-02-27 02:36:23 +00:00
|
|
|
|
template <Flags> void perform(DataProcessing);
|
2024-02-22 15:16:54 +00:00
|
|
|
|
template <Operation, Flags> void perform(Condition, Multiply);
|
|
|
|
|
template <Operation, Flags> void perform(Condition, SingleDataTransfer);
|
|
|
|
|
template <Operation, Flags> void perform(Condition, BlockDataTransfer);
|
|
|
|
|
template <Operation> void perform(Condition, Branch);
|
|
|
|
|
template <Operation, Flags> void perform(Condition, CoprocessorRegisterTransfer);
|
|
|
|
|
template <Flags> void perform(Condition, CoprocessorDataOperation);
|
|
|
|
|
template<Operation, Flags> void perform(Condition, CoprocessorDataTransfer);
|
|
|
|
|
|
|
|
|
|
// Irregular operations.
|
|
|
|
|
void software_interrupt(Condition);
|
|
|
|
|
void unknown(uint32_t opcode);
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-21 19:18:41 +00:00
|
|
|
|
/// Decodes @c instruction, making an appropriate call into @c scheduler.
|
2024-02-22 15:16:54 +00:00
|
|
|
|
///
|
|
|
|
|
/// In lieue of C++20, see the sample definition of SampleScheduler above for the expected interface.
|
2024-02-21 19:17:01 +00:00
|
|
|
|
template <typename SchedulerT> void dispatch(uint32_t instruction, SchedulerT &scheduler) {
|
|
|
|
|
OperationMapper mapper;
|
2024-02-27 02:36:23 +00:00
|
|
|
|
|
|
|
|
|
// Test condition.
|
|
|
|
|
const auto condition = mapper.condition(instruction);
|
|
|
|
|
if(!scheduler.should_schedule(condition)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Dispatch body.
|
2024-02-21 19:51:51 +00:00
|
|
|
|
Reflection::dispatch(mapper, (instruction >> FlagsStartBit) & 0xff, instruction, scheduler);
|
2024-02-21 19:17:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-17 02:35:49 +00:00
|
|
|
|
}
|