4 Commits

Author SHA1 Message Date
6822e1255a meson: make disassembler feature true by default
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2023-09-27 01:33:51 +05:30
bd91112509 refactor: make disassembler optional
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2023-09-27 01:31:32 +05:30
1baebd72f6 refactor: make cpu-impl private when not testing
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2023-09-27 01:25:47 +05:30
b55f6ee16b refactor: replace fmt ostreams with stringify
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2023-09-27 01:24:32 +05:30
19 changed files with 399 additions and 353 deletions

View File

@@ -26,7 +26,7 @@
".hh" ".hh"
".cc" ".cc"
".build" ".build"
"meson_options.txt" ".options"
]; ];
in in
rec { rec {

2
meson.options Normal file
View File

@@ -0,0 +1,2 @@
option('tests', type : 'boolean', value : true, description: 'enable tests')
option('disassembler', type: 'boolean', value: true, description: 'enable disassembler')

View File

@@ -1 +0,0 @@
option('tests', type : 'boolean', value : true, description: 'enable tests')

View File

@@ -48,24 +48,4 @@ eval_shift(ShiftType shift_type, uint32_t value, uint8_t amount, bool& carry) {
return eval; return eval;
} }
std::ostream&
operator<<(std::ostream& os, const ShiftType shift_type) {
#define CASE(type) \
case ShiftType::type: \
os << #type; \
break;
switch (shift_type) {
CASE(LSL)
CASE(LSR)
CASE(ASR)
CASE(ROR)
}
#undef CASE
return os;
}
} }

View File

@@ -10,6 +10,24 @@ enum class ShiftType {
ROR = 0b11 ROR = 0b11
}; };
constexpr auto
stringify(ShiftType shift_type) {
#define CASE(type) \
case ShiftType::type: \
return #type;
switch (shift_type) {
CASE(LSL)
CASE(LSR)
CASE(ASR)
CASE(ROR)
}
#undef CASE
return "";
}
struct ShiftData { struct ShiftData {
ShiftType type; ShiftType type;
bool immediate; bool immediate;
@@ -23,13 +41,4 @@ struct Shift {
uint32_t uint32_t
eval_shift(ShiftType shift_type, uint32_t value, uint8_t amount, bool& carry); eval_shift(ShiftType shift_type, uint32_t value, uint8_t amount, bool& carry);
// https://fmt.dev/dev/api.html#std-ostream-support
std::ostream&
operator<<(std::ostream& os, const ShiftType cond);
}
namespace fmt {
template<>
struct formatter<matar::ShiftType> : ostream_formatter {};
} }

235
src/cpu/arm/disassembler.cc Normal file
View File

@@ -0,0 +1,235 @@
#include "instruction.hh"
#include "util/bits.hh"
namespace matar {
namespace arm {
std::string
Instruction::disassemble() {
auto condition = stringify(this->condition);
return std::visit(
overloaded{
[condition](BranchAndExchange& data) {
return fmt::format("BX{} R{:d}", condition, data.rn);
},
[condition](Branch& data) {
return fmt::format(
"B{}{} 0x{:06X}", (data.link ? "L" : ""), condition, data.offset);
},
[condition](Multiply& data) {
if (data.acc) {
return fmt::format("MLA{}{} R{:d},R{:d},R{:d},R{:d}",
condition,
(data.set ? "S" : ""),
data.rd,
data.rm,
data.rs,
data.rn);
} else {
return fmt::format("MUL{}{} R{:d},R{:d},R{:d}",
condition,
(data.set ? "S" : ""),
data.rd,
data.rm,
data.rs);
}
},
[condition](MultiplyLong& data) {
return fmt::format("{}{}{}{} R{:d},R{:d},R{:d},R{:d}",
(data.uns ? 'U' : 'S'),
(data.acc ? "MLAL" : "MULL"),
condition,
(data.set ? "S" : ""),
data.rdlo,
data.rdhi,
data.rm,
data.rs);
},
[](Undefined) { return std::string("UND"); },
[condition](SingleDataSwap& data) {
return fmt::format("SWP{}{} R{:d},R{:d},[R{:d}]",
condition,
(data.byte ? "B" : ""),
data.rd,
data.rm,
data.rn);
},
[condition](SingleDataTransfer& data) {
std::string expression;
std::string address;
if (const uint16_t* offset = std::get_if<uint16_t>(&data.offset)) {
if (*offset == 0) {
expression = "";
} else {
expression =
fmt::format(",{}#{:d}", (data.up ? '+' : '-'), *offset);
}
} else if (const Shift* shift = std::get_if<Shift>(&data.offset)) {
// Shifts are always immediate in single data transfer
expression = fmt::format(",{}R{:d},{} #{:d}",
(data.up ? '+' : '-'),
shift->rm,
stringify(shift->data.type),
shift->data.operand);
}
return fmt::format(
"{}{}{}{} R{:d},[R{:d}{}]{}",
(data.load ? "LDR" : "STR"),
condition,
(data.byte ? "B" : ""),
(!data.pre && data.write ? "T" : ""),
data.rd,
data.rn,
(data.pre ? expression : ""),
(data.pre ? (data.write ? "!" : "") : expression));
},
[condition](HalfwordTransfer& data) {
std::string expression;
if (data.imm) {
if (data.offset == 0) {
expression = "";
} else {
expression = fmt::format(
",{}#{:d}", (data.up ? '+' : '-'), data.offset);
}
} else {
expression =
fmt::format(",{}R{:d}", (data.up ? '+' : '-'), data.offset);
}
return fmt::format(
"{}{}{}{} R{:d},[R{:d}{}]{}",
(data.load ? "LDR" : "STR"),
condition,
(data.sign ? "S" : ""),
(data.half ? 'H' : 'B'),
data.rd,
data.rn,
(data.pre ? expression : ""),
(data.pre ? (data.write ? "!" : "") : expression));
},
[condition](BlockDataTransfer& data) {
std::string regs;
for (uint8_t i = 0; i < 16; i++) {
if (get_bit(data.regs, i))
fmt::format_to(std::back_inserter(regs), "R{:d},", i);
};
regs.pop_back();
return fmt::format("{}{}{}{} R{:d}{},{{{}}}{}",
(data.load ? "LDM" : "STM"),
condition,
(data.up ? 'I' : 'D'),
(data.pre ? 'B' : 'A'),
data.rn,
(data.write ? "!" : ""),
regs,
(data.s ? "^" : ""));
},
[condition](PsrTransfer& data) {
if (data.type == PsrTransfer::Type::Mrs) {
return fmt::format("MRS{} R{:d},{}",
condition,
data.operand,
(data.spsr ? "SPSR_all" : "CPSR_all"));
} else {
return fmt::format(
"MSR{} {}_{},{}{}",
condition,
(data.spsr ? "SPSR" : "CPSR"),
(data.type == PsrTransfer::Type::Msr_flg ? "flg" : "all"),
(data.imm ? '#' : 'R'),
data.operand);
}
},
[condition](DataProcessing& data) {
using OpCode = DataProcessing::OpCode;
std::string op_2;
if (const uint32_t* operand =
std::get_if<uint32_t>(&data.operand)) {
op_2 = fmt::format("#{:d}", *operand);
} else if (const Shift* shift = std::get_if<Shift>(&data.operand)) {
op_2 = fmt::format("R{:d},{} {}{:d}",
shift->rm,
stringify(shift->data.type),
(shift->data.immediate ? '#' : 'R'),
shift->data.operand);
}
switch (data.opcode) {
case OpCode::MOV:
case OpCode::MVN:
return fmt::format("{}{}{} R{:d},{}",
stringify(data.opcode),
condition,
(data.set ? "S" : ""),
data.rd,
op_2);
case OpCode::TST:
case OpCode::TEQ:
case OpCode::CMP:
case OpCode::CMN:
return fmt::format("{}{} R{:d},{}",
stringify(data.opcode),
condition,
data.rn,
op_2);
default:
return fmt::format("{}{}{} R{:d},R{:d},{}",
stringify(data.opcode),
condition,
(data.set ? "S" : ""),
data.rd,
data.rn,
op_2);
}
},
[condition](SoftwareInterrupt) {
return fmt::format("SWI{}", condition);
},
[condition](CoprocessorDataTransfer& data) {
std::string expression = fmt::format(",#{:d}", data.offset);
return fmt::format(
"{}{}{} p{:d},c{:d},[R{:d}{}]{}",
(data.load ? "LDC" : "STC"),
condition,
(data.len ? "L" : ""),
data.cpn,
data.crd,
data.rn,
(data.pre ? expression : ""),
(data.pre ? (data.write ? "!" : "") : expression));
},
[condition](CoprocessorDataOperation& data) {
return fmt::format("CDP{} p{},{},c{},c{},c{},{}",
condition,
data.cpn,
data.cp_opc,
data.crd,
data.crn,
data.crm,
data.cp);
},
[condition](CoprocessorRegisterTransfer& data) {
return fmt::format("{}{} p{},{},R{},c{},c{},{}",
(data.load ? "MRC" : "MCR"),
condition,
data.cpn,
data.cp_opc,
data.rd,
data.crn,
data.crm,
data.cp);
},
[](auto) { return std::string("unknown instruction"); } },
data);
}
}
}

View File

@@ -4,7 +4,7 @@
namespace matar { namespace matar {
void void
CpuImpl::exec_arm(const arm::Instruction instruction) { CpuImpl::exec(const arm::Instruction instruction) {
Condition cond = instruction.condition; Condition cond = instruction.condition;
arm::InstructionData data = instruction.data; arm::InstructionData data = instruction.data;

View File

@@ -4,7 +4,6 @@
namespace matar { namespace matar {
namespace arm { namespace arm {
Instruction::Instruction(uint32_t insn) Instruction::Instruction(uint32_t insn)
: condition(static_cast<Condition>(bit_range(insn, 28, 31))) { : condition(static_cast<Condition>(bit_range(insn, 28, 31))) {
// Branch and exhcange // Branch and exhcange
@@ -275,261 +274,5 @@ Instruction::Instruction(uint32_t insn)
data = Undefined{}; data = Undefined{};
} }
} }
std::string
Instruction::disassemble() {
// goddamn this is gore
// TODO: make this less ugly
return std::visit(
overloaded{
[this](BranchAndExchange& data) {
return fmt::format("BX{} R{:d}", condition, data.rn);
},
[this](Branch& data) {
return fmt::format(
"B{}{} 0x{:06X}", (data.link ? "L" : ""), condition, data.offset);
},
[this](Multiply& data) {
if (data.acc) {
return fmt::format("MLA{}{} R{:d},R{:d},R{:d},R{:d}",
condition,
(data.set ? "S" : ""),
data.rd,
data.rm,
data.rs,
data.rn);
} else {
return fmt::format("MUL{}{} R{:d},R{:d},R{:d}",
condition,
(data.set ? "S" : ""),
data.rd,
data.rm,
data.rs);
}
},
[this](MultiplyLong& data) {
return fmt::format("{}{}{}{} R{:d},R{:d},R{:d},R{:d}",
(data.uns ? 'U' : 'S'),
(data.acc ? "MLAL" : "MULL"),
condition,
(data.set ? "S" : ""),
data.rdlo,
data.rdhi,
data.rm,
data.rs);
},
[](Undefined) { return std::string("UND"); },
[this](SingleDataSwap& data) {
return fmt::format("SWP{}{} R{:d},R{:d},[R{:d}]",
condition,
(data.byte ? "B" : ""),
data.rd,
data.rm,
data.rn);
},
[this](SingleDataTransfer& data) {
std::string expression;
std::string address;
if (const uint16_t* offset = std::get_if<uint16_t>(&data.offset)) {
if (*offset == 0) {
expression = "";
} else {
expression =
fmt::format(",{}#{:d}", (data.up ? '+' : '-'), *offset);
}
} else if (const Shift* shift = std::get_if<Shift>(&data.offset)) {
// Shifts are always immediate in single data transfer
expression = fmt::format(",{}R{:d},{} #{:d}",
(data.up ? '+' : '-'),
shift->rm,
shift->data.type,
shift->data.operand);
}
return fmt::format(
"{}{}{}{} R{:d},[R{:d}{}]{}",
(data.load ? "LDR" : "STR"),
condition,
(data.byte ? "B" : ""),
(!data.pre && data.write ? "T" : ""),
data.rd,
data.rn,
(data.pre ? expression : ""),
(data.pre ? (data.write ? "!" : "") : expression));
},
[this](HalfwordTransfer& data) {
std::string expression;
if (data.imm) {
if (data.offset == 0) {
expression = "";
} else {
expression = fmt::format(
",{}#{:d}", (data.up ? '+' : '-'), data.offset);
}
} else {
expression =
fmt::format(",{}R{:d}", (data.up ? '+' : '-'), data.offset);
}
return fmt::format(
"{}{}{}{} R{:d},[R{:d}{}]{}",
(data.load ? "LDR" : "STR"),
condition,
(data.sign ? "S" : ""),
(data.half ? 'H' : 'B'),
data.rd,
data.rn,
(data.pre ? expression : ""),
(data.pre ? (data.write ? "!" : "") : expression));
},
[this](BlockDataTransfer& data) {
std::string regs;
for (uint8_t i = 0; i < 16; i++) {
if (get_bit(data.regs, i))
fmt::format_to(std::back_inserter(regs), "R{:d},", i);
};
regs.pop_back();
return fmt::format("{}{}{}{} R{:d}{},{{{}}}{}",
(data.load ? "LDM" : "STM"),
condition,
(data.up ? 'I' : 'D'),
(data.pre ? 'B' : 'A'),
data.rn,
(data.write ? "!" : ""),
regs,
(data.s ? "^" : ""));
},
[this](PsrTransfer& data) {
if (data.type == PsrTransfer::Type::Mrs) {
return fmt::format("MRS{} R{:d},{}",
condition,
data.operand,
(data.spsr ? "SPSR_all" : "CPSR_all"));
} else {
return fmt::format(
"MSR{} {}_{},{}{}",
condition,
(data.spsr ? "SPSR" : "CPSR"),
(data.type == PsrTransfer::Type::Msr_flg ? "flg" : "all"),
(data.imm ? '#' : 'R'),
data.operand);
}
},
[this](DataProcessing& data) {
using OpCode = DataProcessing::OpCode;
std::string op_2;
if (const uint32_t* operand =
std::get_if<uint32_t>(&data.operand)) {
op_2 = fmt::format("#{:d}", *operand);
} else if (const Shift* shift = std::get_if<Shift>(&data.operand)) {
op_2 = fmt::format("R{:d},{} {}{:d}",
shift->rm,
shift->data.type,
(shift->data.immediate ? '#' : 'R'),
shift->data.operand);
}
switch (data.opcode) {
case OpCode::MOV:
case OpCode::MVN:
return fmt::format("{}{}{} R{:d},{}",
data.opcode,
condition,
(data.set ? "S" : ""),
data.rd,
op_2);
case OpCode::TST:
case OpCode::TEQ:
case OpCode::CMP:
case OpCode::CMN:
return fmt::format(
"{}{} R{:d},{}", data.opcode, condition, data.rn, op_2);
default:
return fmt::format("{}{}{} R{:d},R{:d},{}",
data.opcode,
condition,
(data.set ? "S" : ""),
data.rd,
data.rn,
op_2);
}
},
[this](SoftwareInterrupt) { return fmt::format("SWI{}", condition); },
[this](CoprocessorDataTransfer& data) {
std::string expression = fmt::format(",#{:d}", data.offset);
return fmt::format(
"{}{}{} p{:d},c{:d},[R{:d}{}]{}",
(data.load ? "LDC" : "STC"),
condition,
(data.len ? "L" : ""),
data.cpn,
data.crd,
data.rn,
(data.pre ? expression : ""),
(data.pre ? (data.write ? "!" : "") : expression));
},
[this](CoprocessorDataOperation& data) {
return fmt::format("CDP{} p{},{},c{},c{},c{},{}",
condition,
data.cpn,
data.cp_opc,
data.crd,
data.crn,
data.crm,
data.cp);
},
[this](CoprocessorRegisterTransfer& data) {
return fmt::format("{}{} p{},{},R{},c{},c{},{}",
(data.load ? "MRC" : "MCR"),
condition,
data.cpn,
data.cp_opc,
data.rd,
data.crn,
data.crm,
data.cp);
},
[](auto) { return std::string("unknown instruction"); } },
data);
}
std::ostream&
operator<<(std::ostream& os, const DataProcessing::OpCode opcode) {
#define CASE(opcode) \
case DataProcessing::OpCode::opcode: \
os << #opcode; \
break;
switch (opcode) {
CASE(AND)
CASE(EOR)
CASE(SUB)
CASE(RSB)
CASE(ADD)
CASE(ADC)
CASE(SBC)
CASE(RSC)
CASE(TST)
CASE(TEQ)
CASE(CMP)
CASE(CMN)
CASE(ORR)
CASE(MOV)
CASE(BIC)
CASE(MVN)
}
#undef CASE
return os;
}
} }
} }

View File

@@ -8,6 +8,7 @@
namespace matar { namespace matar {
namespace arm { namespace arm {
// https://en.cppreference.com/w/cpp/utility/variant/visit
template<class... Ts> template<class... Ts>
struct overloaded : Ts... { struct overloaded : Ts... {
using Ts::operator()...; using Ts::operator()...;
@@ -113,6 +114,37 @@ struct DataProcessing {
OpCode opcode; OpCode opcode;
}; };
constexpr auto
stringify(DataProcessing::OpCode opcode) {
#define CASE(opcode) \
case DataProcessing::OpCode::opcode: \
return #opcode;
switch (opcode) {
CASE(AND)
CASE(EOR)
CASE(SUB)
CASE(RSB)
CASE(ADD)
CASE(ADC)
CASE(SBC)
CASE(RSC)
CASE(TST)
CASE(TEQ)
CASE(CMP)
CASE(CMN)
CASE(ORR)
CASE(MOV)
CASE(BIC)
CASE(MVN)
}
#undef CASE
return "";
}
struct PsrTransfer { struct PsrTransfer {
enum class Type { enum class Type {
Mrs, Mrs,
@@ -186,15 +218,9 @@ struct Instruction {
: condition(condition) : condition(condition)
, data(data){}; , data(data){};
#ifdef DISASSEMBLER
std::string disassemble(); std::string disassemble();
#endif
}; };
std::ostream&
operator<<(std::ostream& os, const DataProcessing::OpCode cond);
} }
} }
namespace fmt {
template<>
struct formatter<matar::arm::DataProcessing::OpCode> : ostream_formatter {};
}

View File

@@ -1,4 +1,8 @@
lib_sources += files( lib_sources += files(
'instruction.cc', 'instruction.cc',
'exec.cc' 'exec.cc'
) )
if get_option('disassembler')
lib_sources += files('disassembler.cc')
endif

View File

@@ -121,11 +121,13 @@ CpuImpl::step() {
if (cpsr.state() == State::Arm) { if (cpsr.state() == State::Arm) {
uint32_t x = bus->read_word(cur_pc); uint32_t x = bus->read_word(cur_pc);
arm::Instruction instruction(x); arm::Instruction instruction(x);
exec(instruction);
#ifdef DISASSEMBLER
glogger.info("{:#034b}", x); glogger.info("{:#034b}", x);
exec_arm(instruction);
glogger.info("0x{:08X} : {}", cur_pc, instruction.disassemble()); glogger.info("0x{:08X} : {}", cur_pc, instruction.disassemble());
#endif
if (is_flushed) { if (is_flushed) {
// if flushed, do not increment the PC, instead set it to two // if flushed, do not increment the PC, instead set it to two

View File

@@ -13,7 +13,11 @@ class CpuImpl {
void step(); void step();
void chg_mode(const Mode to); void chg_mode(const Mode to);
void exec_arm(const arm::Instruction instruction); void exec(const arm::Instruction instruction);
#ifndef MATAR_CPU_TESTS
private:
#endif
static constexpr uint8_t GPR_COUNT = 16; static constexpr uint8_t GPR_COUNT = 16;

View File

@@ -96,37 +96,4 @@ Psr::condition(Condition cond) const {
return false; return false;
} }
std::ostream&
operator<<(std::ostream& os, const Condition cond) {
#define CASE(cond) \
case Condition::cond: \
os << #cond; \
break;
switch (cond) {
CASE(EQ)
CASE(NE)
CASE(CS)
CASE(CC)
CASE(MI)
CASE(PL)
CASE(VS)
CASE(VC)
CASE(HI)
CASE(LS)
CASE(GE)
CASE(LT)
CASE(GT)
CASE(LE)
case Condition::AL: {
// empty
}
}
#undef CASE
return os;
}
} }

View File

@@ -38,6 +38,38 @@ enum class Condition {
AL = 0b1110 AL = 0b1110
}; };
constexpr auto
stringify(Condition cond) {
#define CASE(cond) \
case Condition::cond: \
return #cond;
switch (cond) {
CASE(EQ)
CASE(NE)
CASE(CS)
CASE(CC)
CASE(MI)
CASE(PL)
CASE(VS)
CASE(VC)
CASE(HI)
CASE(LS)
CASE(GE)
CASE(LT)
CASE(GT)
CASE(LE)
case Condition::AL: {
// empty
}
}
#undef CASE
return "";
}
class Psr { class Psr {
public: public:
// clear the reserved bits i.e, [8:27] // clear the reserved bits i.e, [8:27]
@@ -88,13 +120,4 @@ class Psr {
uint32_t psr; uint32_t psr;
}; };
// https://fmt.dev/dev/api.html#std-ostream-support
std::ostream&
operator<<(std::ostream& os, const Condition cond);
}
namespace fmt {
template<>
struct formatter<matar::Condition> : ostream_formatter {};
} }

View File

@@ -11,7 +11,11 @@ lib_cpp_args = [ ]
fmt = dependency('fmt', version : '>=10.1.0', static: true) fmt = dependency('fmt', version : '>=10.1.0', static: true)
if not fmt.found() if not fmt.found()
fmt = dependency('fmt', version : '>=10.1.0', static: false) fmt = dependency('fmt', version : '>=10.1.0', static: false)
lib_cpp_args += 'DFMT_HEADER_ONLY' lib_cpp_args += '-DFMT_HEADER_ONLY'
endif
if get_option('disassembler')
lib_cpp_args += '-DDISASSEMBLER'
endif endif
lib = library( lib = library(

View File

@@ -80,4 +80,4 @@ class Logger {
extern logging::Logger glogger; extern logging::Logger glogger;
#define debug(x) glogger.debug("{} = {}", #x, x); #define dbg(x) glogger.debug("{} = {}", #x, x);

View File

@@ -1,4 +1,7 @@
#define MATAR_CPU_TESTS
#include "cpu/cpu-impl.hh" #include "cpu/cpu-impl.hh"
#undef MATAR_CPU_TESTS
#include "util/bits.hh" #include "util/bits.hh"
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <limits> #include <limits>
@@ -15,7 +18,7 @@ class CpuFixture {
protected: protected:
void exec(arm::InstructionData data, Condition condition = Condition::AL) { void exec(arm::InstructionData data, Condition condition = Condition::AL) {
arm::Instruction instruction(condition, data); arm::Instruction instruction(condition, data);
cpu.exec_arm(instruction); cpu.exec(instruction);
} }
void reset(uint32_t value = 0) { void reset(uint32_t value = 0) {
@@ -332,7 +335,7 @@ TEST_CASE_METHOD(CpuFixture, "Single Data Transfer", TAG) {
// r15 as rn // r15 as rn
{ {
data_transfer->rn = 15; data_transfer->rn = cpu.PC_INDEX;
cpu.gpr[15] = 7577; cpu.gpr[15] = 7577;
exec(data); exec(data);
@@ -348,7 +351,7 @@ TEST_CASE_METHOD(CpuFixture, "Single Data Transfer", TAG) {
// r15 as rd // r15 as rd
{ {
// 4088 // 4088
data_transfer->rd = 15; data_transfer->rd = cpu.PC_INDEX;
cpu.gpr[15] = 444444; cpu.gpr[15] = 444444;
exec(data); exec(data);
@@ -465,7 +468,7 @@ TEST_CASE_METHOD(CpuFixture, "Halfword Transfer", TAG) {
// r15 as rn // r15 as rn
{ {
hw_transfer->rn = 15; hw_transfer->rn = cpu.PC_INDEX;
cpu.gpr[15] = 399; cpu.gpr[15] = 399;
exec(data); exec(data);
@@ -481,7 +484,7 @@ TEST_CASE_METHOD(CpuFixture, "Halfword Transfer", TAG) {
// r15 as rd // r15 as rd
{ {
hw_transfer->rd = 15; hw_transfer->rd = cpu.PC_INDEX;
cpu.gpr[15] = 224; cpu.gpr[15] = 224;
exec(data); exec(data);
@@ -792,7 +795,7 @@ TEST_CASE_METHOD(CpuFixture, "Data Processing", TAG) {
// same as above but with rn (oprerand 1) = 15 // same as above but with rn (oprerand 1) = 15
{ {
processing->rn = 15; processing->rn = cpu.PC_INDEX;
cpu.gpr[15] = -2871; cpu.gpr[15] = -2871;
exec(data); exec(data);
@@ -1057,7 +1060,7 @@ TEST_CASE_METHOD(CpuFixture, "Data Processing", TAG) {
SECTION("R15 as destination") { SECTION("R15 as destination") {
processing->opcode = OpCode::MVN; processing->opcode = OpCode::MVN;
processing->rd = 15; processing->rd = cpu.PC_INDEX;
cpu.gpr[15] = 0; cpu.gpr[15] = 0;
CHECK(cpu.spsr.raw() != cpu.cpsr.raw()); CHECK(cpu.spsr.raw() != cpu.cpsr.raw());
exec(data); exec(data);

View File

@@ -16,7 +16,9 @@ TEST_CASE("Branch and Exchange", TAG) {
CHECK(bx->rn == 10); CHECK(bx->rn == 10);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "BXGT R10"); CHECK(instruction.disassemble() == "BXGT R10");
#endif
} }
TEST_CASE("Branch", TAG) { TEST_CASE("Branch", TAG) {
@@ -33,10 +35,12 @@ TEST_CASE("Branch", TAG) {
CHECK(b->offset == 0xFE15FF14); CHECK(b->offset == 0xFE15FF14);
CHECK(b->link == true); CHECK(b->link == true);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "BL 0xFE15FF14"); CHECK(instruction.disassemble() == "BL 0xFE15FF14");
b->link = false; b->link = false;
CHECK(instruction.disassemble() == "B 0xFE15FF14"); CHECK(instruction.disassemble() == "B 0xFE15FF14");
#endif
} }
TEST_CASE("Multiply", TAG) { TEST_CASE("Multiply", TAG) {
@@ -54,11 +58,13 @@ TEST_CASE("Multiply", TAG) {
CHECK(mul->acc == true); CHECK(mul->acc == true);
CHECK(mul->set == true); CHECK(mul->set == true);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "MLAEQS R10,R0,R15,R14"); CHECK(instruction.disassemble() == "MLAEQS R10,R0,R15,R14");
mul->acc = false; mul->acc = false;
mul->set = false; mul->set = false;
CHECK(instruction.disassemble() == "MULEQ R10,R0,R15"); CHECK(instruction.disassemble() == "MULEQ R10,R0,R15");
#endif
} }
TEST_CASE("Multiply Long", TAG) { TEST_CASE("Multiply Long", TAG) {
@@ -77,6 +83,7 @@ TEST_CASE("Multiply Long", TAG) {
CHECK(mull->set == true); CHECK(mull->set == true);
CHECK(mull->uns == true); CHECK(mull->uns == true);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "UMULLNES R7,R14,R2,R6"); CHECK(instruction.disassemble() == "UMULLNES R7,R14,R2,R6");
mull->acc = true; mull->acc = true;
@@ -85,6 +92,7 @@ TEST_CASE("Multiply Long", TAG) {
mull->uns = false; mull->uns = false;
mull->set = false; mull->set = false;
CHECK(instruction.disassemble() == "SMLALNE R7,R14,R2,R6"); CHECK(instruction.disassemble() == "SMLALNE R7,R14,R2,R6");
#endif
} }
TEST_CASE("Undefined", TAG) { TEST_CASE("Undefined", TAG) {
@@ -94,7 +102,10 @@ TEST_CASE("Undefined", TAG) {
Instruction instruction(raw); Instruction instruction(raw);
CHECK(instruction.condition == Condition::AL); CHECK(instruction.condition == Condition::AL);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "UND"); CHECK(instruction.disassemble() == "UND");
#endif
} }
TEST_CASE("Single Data Swap", TAG) { TEST_CASE("Single Data Swap", TAG) {
@@ -110,10 +121,12 @@ TEST_CASE("Single Data Swap", TAG) {
CHECK(swp->rn == 9); CHECK(swp->rn == 9);
CHECK(swp->byte == false); CHECK(swp->byte == false);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "SWPGE R5,R6,[R9]"); CHECK(instruction.disassemble() == "SWPGE R5,R6,[R9]");
swp->byte = true; swp->byte = true;
CHECK(instruction.disassemble() == "SWPGEB R5,R6,[R9]"); CHECK(instruction.disassemble() == "SWPGEB R5,R6,[R9]");
#endif
} }
TEST_CASE("Single Data Transfer", TAG) { TEST_CASE("Single Data Transfer", TAG) {
@@ -138,6 +151,7 @@ TEST_CASE("Single Data Transfer", TAG) {
CHECK(ldr->up == true); CHECK(ldr->up == true);
CHECK(ldr->pre == true); CHECK(ldr->pre == true);
#ifdef DISASSEMBLER
ldr->load = true; ldr->load = true;
ldr->byte = true; ldr->byte = true;
ldr->write = false; ldr->write = false;
@@ -153,6 +167,7 @@ TEST_CASE("Single Data Transfer", TAG) {
ldr->pre = true; ldr->pre = true;
CHECK(instruction.disassemble() == "LDRB R10,[R2,-#9023]"); CHECK(instruction.disassemble() == "LDRB R10,[R2,-#9023]");
#endif
} }
TEST_CASE("Halfword Transfer", TAG) { TEST_CASE("Halfword Transfer", TAG) {
@@ -176,6 +191,7 @@ TEST_CASE("Halfword Transfer", TAG) {
CHECK(ldr->up == true); CHECK(ldr->up == true);
CHECK(ldr->pre == true); CHECK(ldr->pre == true);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "STRCCH R2,[R15,+R6]!"); CHECK(instruction.disassemble() == "STRCCH R2,[R15,+R6]!");
ldr->pre = false; ldr->pre = false;
@@ -193,6 +209,7 @@ TEST_CASE("Halfword Transfer", TAG) {
ldr->imm = 1; ldr->imm = 1;
ldr->offset = 90; ldr->offset = 90;
CHECK(instruction.disassemble() == "STRCCSB R2,[R15],-#90"); CHECK(instruction.disassemble() == "STRCCSB R2,[R15],-#90");
#endif
} }
TEST_CASE("Block Data Transfer", TAG) { TEST_CASE("Block Data Transfer", TAG) {
@@ -223,6 +240,7 @@ TEST_CASE("Block Data Transfer", TAG) {
CHECK(ldm->up == false); CHECK(ldm->up == false);
CHECK(ldm->pre == true); CHECK(ldm->pre == true);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "LDMLSDB R7,{R0,R2,R3,R5,R6,R8,R14}^"); CHECK(instruction.disassemble() == "LDMLSDB R7,{R0,R2,R3,R5,R6,R8,R14}^");
ldm->write = true; ldm->write = true;
@@ -238,6 +256,7 @@ TEST_CASE("Block Data Transfer", TAG) {
ldm->pre = false; ldm->pre = false;
CHECK(instruction.disassemble() == "STMLSIA R7!,{R0,R2,R5,R14}"); CHECK(instruction.disassemble() == "STMLSIA R7!,{R0,R2,R5,R14}");
#endif
} }
TEST_CASE("PSR Transfer", TAG) { TEST_CASE("PSR Transfer", TAG) {
@@ -256,7 +275,9 @@ TEST_CASE("PSR Transfer", TAG) {
CHECK(mrs->operand == 10); CHECK(mrs->operand == 10);
CHECK(mrs->spsr == true); CHECK(mrs->spsr == true);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "MRSMI R10,SPSR_all"); CHECK(instruction.disassemble() == "MRSMI R10,SPSR_all");
#endif
} }
SECTION("MSR") { SECTION("MSR") {
@@ -272,7 +293,9 @@ TEST_CASE("PSR Transfer", TAG) {
CHECK(msr->operand == 8); CHECK(msr->operand == 8);
CHECK(msr->spsr == false); CHECK(msr->spsr == false);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "MSR CPSR_all,R8"); CHECK(instruction.disassemble() == "MSR CPSR_all,R8");
#endif
} }
SECTION("MSR_flg with register operand") { SECTION("MSR_flg with register operand") {
@@ -287,7 +310,9 @@ TEST_CASE("PSR Transfer", TAG) {
CHECK(msr->operand == 8); CHECK(msr->operand == 8);
CHECK(msr->spsr == false); CHECK(msr->spsr == false);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "MSRVS CPSR_flg,R8"); CHECK(instruction.disassemble() == "MSRVS CPSR_flg,R8");
#endif
} }
SECTION("MSR_flg with immediate operand") { SECTION("MSR_flg with immediate operand") {
@@ -304,7 +329,9 @@ TEST_CASE("PSR Transfer", TAG) {
CHECK(msr->operand == 27262976); CHECK(msr->operand == 27262976);
CHECK(msr->spsr == true); CHECK(msr->spsr == true);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "MSR SPSR_flg,#27262976"); CHECK(instruction.disassemble() == "MSR SPSR_flg,#27262976");
#endif
} }
} }
@@ -331,6 +358,7 @@ TEST_CASE("Data Processing", TAG) {
CHECK(alu->set == true); CHECK(alu->set == true);
CHECK(alu->opcode == OpCode::AND); CHECK(alu->opcode == OpCode::AND);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "ANDS R7,R14,R1,ROR #22"); CHECK(instruction.disassemble() == "ANDS R7,R14,R1,ROR #22");
shift->data.immediate = false; shift->data.immediate = false;
@@ -392,6 +420,7 @@ TEST_CASE("Data Processing", TAG) {
alu->opcode = OpCode::MVN; alu->opcode = OpCode::MVN;
CHECK(instruction.disassemble() == "MVN R7,#3300012"); CHECK(instruction.disassemble() == "MVN R7,#3300012");
} }
#endif
} }
TEST_CASE("Coprocessor Data Transfer", TAG) { TEST_CASE("Coprocessor Data Transfer", TAG) {
@@ -412,6 +441,7 @@ TEST_CASE("Coprocessor Data Transfer", TAG) {
CHECK(ldc->up == true); CHECK(ldc->up == true);
CHECK(ldc->pre == true); CHECK(ldc->pre == true);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "STCGE p1,c15,[R5,#70]!"); CHECK(instruction.disassemble() == "STCGE p1,c15,[R5,#70]!");
ldc->load = true; ldc->load = true;
@@ -420,6 +450,7 @@ TEST_CASE("Coprocessor Data Transfer", TAG) {
ldc->len = true; ldc->len = true;
CHECK(instruction.disassemble() == "LDCGEL p1,c15,[R5],#70"); CHECK(instruction.disassemble() == "LDCGEL p1,c15,[R5],#70");
#endif
} }
TEST_CASE("Coprocessor Operand Operation", TAG) { TEST_CASE("Coprocessor Operand Operation", TAG) {
@@ -437,7 +468,9 @@ TEST_CASE("Coprocessor Operand Operation", TAG) {
CHECK(cdp->crn == 5); CHECK(cdp->crn == 5);
CHECK(cdp->cp_opc == 10); CHECK(cdp->cp_opc == 10);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "CDP p1,10,c15,c5,c6,2"); CHECK(instruction.disassemble() == "CDP p1,10,c15,c5,c6,2");
#endif
} }
TEST_CASE("Coprocessor Register Transfer", TAG) { TEST_CASE("Coprocessor Register Transfer", TAG) {
@@ -457,7 +490,9 @@ TEST_CASE("Coprocessor Register Transfer", TAG) {
CHECK(mrc->load == false); CHECK(mrc->load == false);
CHECK(mrc->cp_opc == 5); CHECK(mrc->cp_opc == 5);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "MCR p1,5,R15,c5,c6,2"); CHECK(instruction.disassemble() == "MCR p1,5,R15,c5,c6,2");
#endif
} }
TEST_CASE("Software Interrupt", TAG) { TEST_CASE("Software Interrupt", TAG) {
@@ -465,5 +500,8 @@ TEST_CASE("Software Interrupt", TAG) {
Instruction instruction(raw); Instruction instruction(raw);
CHECK(instruction.condition == Condition::EQ); CHECK(instruction.condition == Condition::EQ);
#ifdef DISASSEMBLER
CHECK(instruction.disassemble() == "SWIEQ"); CHECK(instruction.disassemble() == "SWIEQ");
#endif
} }

View File

@@ -13,6 +13,12 @@ tests_sources = files(
subdir('cpu') subdir('cpu')
subdir('util') subdir('util')
tests_cpp_args = []
if get_option('disassembler')
tests_cpp_args += '-DDISASSEMBLER'
endif
catch2 = dependency('catch2', version: '>=3.4.0', static: true) catch2 = dependency('catch2', version: '>=3.4.0', static: true)
catch2_tests = executable( catch2_tests = executable(
'matar_tests', 'matar_tests',
@@ -21,6 +27,7 @@ catch2_tests = executable(
link_with: tests_deps, link_with: tests_deps,
include_directories: [inc, src], include_directories: [inc, src],
build_by_default: false, build_by_default: false,
cpp_args: tests_cpp_args
) )
test('catch2 tests', catch2_tests) test('catch2 tests', catch2_tests)