Add cnst-instruction test

This commit is contained in:
shylie 2025-07-07 13:17:42 -04:00
parent 9e2604855e
commit 5b43e71dcc
8 changed files with 94 additions and 9 deletions

View File

@ -1,4 +1,4 @@
name: Build
name: Test
on: push
@ -12,3 +12,5 @@ jobs:
run: cmake -S emulator -B emulator/build
- name: Build
run: cmake --build emulator/build
- name: Test
ctest emulator/build

View File

@ -1,5 +1,14 @@
cmake_minimum_required(VERSION 3.20)
project(foot-emu)
project(foot-emulator)
add_executable(foot-emu main.cpp foot-emulator.cpp foot-emulator.h)
add_library(foot-emulator src/foot-emulator.cpp include/foot-emulator.h)
target_include_directories(foot-emulator PUBLIC include)
add_executable(foot-emulator-cli src/main.cpp)
target_link_libraries(foot-emulator-cli PUBLIC foot-emulator)
if(PROJECT_IS_TOP_LEVEL)
enable_testing()
add_subdirectory(tests)
endif()

View File

@ -59,6 +59,9 @@ public:
uint16_t& memory_at(uint16_t address);
uint16_t& decode_operand(Operand operand);
uint16_t& register_at(uint8_t index);
void run_instruction();
private:
std::array<uint16_t, 1 << 16> memory;
@ -76,7 +79,6 @@ private:
uint16_t mapped_base;
void read_instruction();
void run_instruction();
void CNST();
void CMPR();

View File

@ -91,12 +91,9 @@ uint16_t& Emulator::decode_operand(Operand operand)
}
}
void Emulator::read_instruction()
uint16_t& Emulator::register_at(uint8_t index)
{
uint16_t low = memory[registers[PC]++];
uint16_t high = memory[registers[PC]++];
instruction = (uint32_t(high) << 16) | uint32_t(low);
return registers[index];
}
void Emulator::run_instruction()
@ -197,6 +194,14 @@ void Emulator::run_instruction()
if (rep) { registers[LC] = loop_count; }
}
void Emulator::read_instruction()
{
uint16_t low = memory[registers[PC]++];
uint16_t high = memory[registers[PC]++];
instruction = (uint32_t(high) << 16) | uint32_t(low);
}
void Emulator::CNST()
{
decode_operand((instruction & 0x00FF0000) >> 16) = instruction & 0x0000FFFF;

View File

@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.20)
include(CTest)
function(test name)
add_executable(foot-emulator-test-${name} ${name}.cpp test-common.h)
target_link_libraries(foot-emulator-test-${name} PUBLIC foot-emulator)
add_test(NAME ${name} COMMAND foot-emulator-test-${name})
endfunction()
test(cnst-instruction)

View File

@ -0,0 +1,27 @@
#include <foot-emulator.h>
#include "test-common.h"
int main(int argc, char** argv)
{
// CNST
// dst - 0, Direct
// imm - 0xFFFF
{
foot::Emulator emu = run_instruction(0x0020FFFF);
if (emu.register_at(0) != 0xFFFF) { return 1; }
}
// CNST
// dst - 0, Direct
// imm - 0xFFFF
//
// CNST
// dst - 0, Indirect
// imm = 0x7777
{
foot::Emulator emu = run_instructions({ 0x0020FFFF, 0x00807777 });
if (emu.memory_at(0xFFFF) != 0x7777) { return 1; }
}
return 0;
}

View File

@ -0,0 +1,29 @@
#include <foot-emulator.h>
inline foot::Emulator run_instruction(uint32_t instruction)
{
foot::Emulator emu;
emu.memory_at(0) = instruction & 0xFFFF;
emu.memory_at(1) = (instruction >> 16) & 0xFFFF;
emu.run_instruction();
return emu;
}
inline foot::Emulator run_instructions(const std::vector<uint32_t>& instructions)
{
foot::Emulator emu;
for (int i = 0; i < instructions.size(); i++)
{
emu.memory_at(i * 2) = instructions[i] & 0xFFFF;
emu.memory_at(i * 2 + 1) = (instructions[i] >> 16) & 0xFFFF;
}
for (int i = 0; i < instructions.size(); i++)
{
emu.run_instruction();
}
return emu;
}