Add cnst-instruction test
This commit is contained in:
parent
9e2604855e
commit
5b43e71dcc
@ -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
|
@ -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()
|
||||
|
@ -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();
|
@ -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;
|
11
emulator/tests/CMakeLists.txt
Normal file
11
emulator/tests/CMakeLists.txt
Normal 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)
|
27
emulator/tests/cnst-instruction.cpp
Normal file
27
emulator/tests/cnst-instruction.cpp
Normal 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;
|
||||
}
|
29
emulator/tests/test-common.h
Normal file
29
emulator/tests/test-common.h
Normal 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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user