2021-10-10 20:26:30 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "TestRunner.h"
|
|
|
|
|
|
|
|
#include <Disassembly.h>
|
|
|
|
|
|
|
|
TestRunner::TestRunner(const test_t& test)
|
|
|
|
: m_test(test) {}
|
|
|
|
|
|
|
|
EightBit::MemoryMapping TestRunner::mapping(uint16_t address) noexcept {
|
|
|
|
return { RAM(), 0x0000, 0xffff, EightBit::MemoryMapping::AccessLevel::ReadWrite };
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::raisePOWER() {
|
|
|
|
EightBit::Bus::raisePOWER();
|
|
|
|
CPU().raisePOWER();
|
|
|
|
CPU().raiseRESET();
|
|
|
|
CPU().raiseINT();
|
|
|
|
CPU().raiseNMI();
|
|
|
|
CPU().raiseSO();
|
|
|
|
CPU().raiseRDY();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::lowerPOWER() {
|
|
|
|
CPU().lowerPOWER();
|
|
|
|
EightBit::Bus::lowerPOWER();
|
|
|
|
}
|
|
|
|
|
2021-10-18 10:54:01 +00:00
|
|
|
void TestRunner::addActualCycle(const cycle_t& value) {
|
|
|
|
m_actualCycles.add(value);
|
2021-10-10 20:26:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 10:54:01 +00:00
|
|
|
void TestRunner::addActualCycle(uint16_t address, uint8_t value, cycle_t::action_t action) {
|
|
|
|
addActualCycle({ address, value, action });
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::addActualCycle(EightBit::register16_t address, uint8_t value, cycle_t::action_t action) {
|
|
|
|
addActualCycle(address.word, value, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::addActualReadCycle(EightBit::register16_t address, uint8_t value) {
|
|
|
|
addActualCycle(address, value, cycle_t::action_t::read);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::addActualWriteCycle(EightBit::register16_t address, uint8_t value) {
|
|
|
|
addActualCycle(address, value, cycle_t::action_t::write);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::dumpCycles(std::string which, const cycles_t& events) {
|
2021-10-11 22:56:20 +00:00
|
|
|
m_messages.push_back(which);
|
2021-10-18 10:54:01 +00:00
|
|
|
dumpCycles(events);
|
2021-10-11 22:56:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 10:54:01 +00:00
|
|
|
void TestRunner::dumpCycles(const cycles_t& cycles) {
|
2021-10-11 22:56:20 +00:00
|
|
|
os() << std::hex << std::uppercase << std::setfill('0');
|
2021-10-18 10:54:01 +00:00
|
|
|
for (const auto& cycle: cycles)
|
|
|
|
dumpCycle(cycle);
|
2021-10-11 22:56:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 10:54:01 +00:00
|
|
|
void TestRunner::dumpCycle(const cycle_t& cycle) {
|
2021-10-11 22:56:20 +00:00
|
|
|
os()
|
2021-10-18 10:54:01 +00:00
|
|
|
<< "Address: " << std::setw(4) << cycle.address()
|
|
|
|
<< ", value: " << std::setw(2) << (int)cycle.value()
|
|
|
|
<< ", action: " << cycle_t::to_string(cycle.action());
|
2021-10-11 22:56:20 +00:00
|
|
|
m_messages.push_back(os().str());
|
|
|
|
os().str("");
|
|
|
|
}
|
|
|
|
|
2021-10-10 20:26:30 +00:00
|
|
|
void TestRunner::initialise() {
|
|
|
|
|
|
|
|
ReadByte.connect([this](EightBit::EventArgs&) {
|
2021-10-18 10:54:01 +00:00
|
|
|
addActualReadCycle(ADDRESS(), DATA());
|
2021-10-10 20:26:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
WrittenByte.connect([this](EightBit::EventArgs&) {
|
2021-10-18 10:54:01 +00:00
|
|
|
addActualWriteCycle(ADDRESS(), DATA());
|
2021-10-10 20:26:30 +00:00
|
|
|
});
|
2021-10-11 10:41:50 +00:00
|
|
|
|
|
|
|
os() << std::hex << std::uppercase << std::setfill('0');
|
2021-10-10 20:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::raise(std::string what, uint16_t expected, uint16_t actual) {
|
2021-10-11 10:41:50 +00:00
|
|
|
os()
|
|
|
|
<< std::setw(4)
|
2021-10-11 09:20:18 +00:00
|
|
|
<< what
|
2021-10-11 10:41:50 +00:00
|
|
|
<< ": expected: " << (int)expected
|
|
|
|
<< ", actual: " << (int)actual;
|
|
|
|
m_messages.push_back(os().str());
|
|
|
|
os().str("");
|
2021-10-10 20:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::raise(std::string what, uint8_t expected, uint8_t actual) {
|
2021-10-11 10:41:50 +00:00
|
|
|
os()
|
|
|
|
<< std::setw(2)
|
2021-10-11 09:20:18 +00:00
|
|
|
<< what
|
2021-10-11 10:41:50 +00:00
|
|
|
<< ": expected: " << (int)expected
|
2021-10-10 20:26:30 +00:00
|
|
|
<< "(" << EightBit::Disassembly::dump_Flags(expected) << ")"
|
2021-10-11 10:41:50 +00:00
|
|
|
<< ", actual: " << (int)actual
|
2021-10-11 09:20:18 +00:00
|
|
|
<< "(" << EightBit::Disassembly::dump_Flags(actual) << ")";
|
2021-10-11 10:41:50 +00:00
|
|
|
m_messages.push_back(os().str());
|
|
|
|
os().str("");
|
2021-10-10 20:26:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 10:54:01 +00:00
|
|
|
void TestRunner::raise(std::string what, cycle_t::action_t expected, cycle_t::action_t actual) {
|
2021-10-11 10:41:50 +00:00
|
|
|
os()
|
2021-10-11 09:20:18 +00:00
|
|
|
<< what
|
2021-10-18 10:54:01 +00:00
|
|
|
<< ": expected: " << cycle_t::to_string(expected)
|
|
|
|
<< ", actual: " << cycle_t::to_string(actual);
|
2021-10-11 10:41:50 +00:00
|
|
|
m_messages.push_back(os().str());
|
|
|
|
os().str("");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestRunner::check(std::string what, uint16_t address, uint8_t expected, uint8_t actual) {
|
|
|
|
const auto success = actual == expected;
|
|
|
|
if (!success) {
|
|
|
|
os() << what << ": " << std::setw(4) << (int)address;
|
|
|
|
raise(os().str(), expected, actual);
|
|
|
|
os().str("");
|
|
|
|
}
|
|
|
|
return success;
|
2021-10-10 20:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::initialiseState() {
|
|
|
|
|
|
|
|
const auto& starting = test().initial_state();
|
|
|
|
|
|
|
|
CPU().PC().word = starting.pc();
|
|
|
|
CPU().S() = starting.s();
|
|
|
|
CPU().A() = starting.a();
|
|
|
|
CPU().X() = starting.x();
|
|
|
|
CPU().Y() = starting.y();
|
|
|
|
CPU().P() = starting.p();
|
|
|
|
const auto& ram = starting.ram();
|
2021-10-19 08:28:13 +00:00
|
|
|
for (const auto& entry : ram)
|
|
|
|
RAM().poke(entry.address(), entry.value());
|
2021-10-10 20:26:30 +00:00
|
|
|
|
2021-10-18 10:54:01 +00:00
|
|
|
m_actualCycles.clear();
|
2021-10-10 20:26:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 09:20:18 +00:00
|
|
|
bool TestRunner::checkState() {
|
2021-10-10 20:26:30 +00:00
|
|
|
|
|
|
|
const auto& finished = test().final_state();
|
|
|
|
|
2021-10-18 10:54:01 +00:00
|
|
|
const auto& expected_cycles = test().cycles();
|
|
|
|
const auto& actual_cycles = m_actualCycles;
|
|
|
|
m_cycle_count_mismatch = expected_cycles.size() != actual_cycles.size();
|
|
|
|
if (m_cycle_count_mismatch)
|
2021-10-11 09:20:18 +00:00
|
|
|
return false;
|
2021-10-10 20:26:30 +00:00
|
|
|
|
2021-10-18 10:54:01 +00:00
|
|
|
for (int i = 0; i < expected_cycles.size(); ++i) {
|
2021-10-18 16:19:28 +00:00
|
|
|
const auto& expected = expected_cycles[i];
|
|
|
|
const auto& actual = actual_cycles[i]; // actual could be less than expected
|
2021-10-18 10:54:01 +00:00
|
|
|
check("Cycle address", expected.address(), actual.address());
|
|
|
|
check("Cycle value", expected.value(), actual.value());
|
|
|
|
check("Cycle action", expected.action(), actual.action());
|
2021-10-10 20:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto pc_good = check("PC", finished.pc(), CPU().PC().word);
|
|
|
|
const auto s_good = check("S", finished.s(), CPU().S());
|
|
|
|
const auto a_good = check("A", finished.a(), CPU().A());
|
|
|
|
const auto x_good = check("X", finished.x(), CPU().X());
|
|
|
|
const auto y_good = check("Y", finished.y(), CPU().Y());
|
|
|
|
const auto p_good = check("P", finished.p(), CPU().P());
|
|
|
|
|
|
|
|
const auto& ram = finished.ram();
|
|
|
|
bool ram_problem = false;
|
|
|
|
for (const auto& entry : ram) {
|
2021-10-19 08:28:13 +00:00
|
|
|
const auto ram_good = check("RAM", entry.address(), entry.value(), RAM().peek(entry.address()));
|
2021-10-10 20:26:30 +00:00
|
|
|
if (!ram_good && !ram_problem)
|
|
|
|
ram_problem = true;
|
|
|
|
}
|
|
|
|
|
2021-10-11 09:20:18 +00:00
|
|
|
return pc_good && s_good && a_good && x_good && y_good && p_good && !ram_problem;
|
2021-10-10 20:26:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 09:20:18 +00:00
|
|
|
bool TestRunner::check() {
|
2021-10-10 20:26:30 +00:00
|
|
|
initialise();
|
|
|
|
raisePOWER();
|
|
|
|
initialiseState();
|
|
|
|
const int cycles = CPU().step();
|
2021-10-11 09:20:18 +00:00
|
|
|
const auto valid = checkState();
|
2021-10-18 10:54:01 +00:00
|
|
|
if (m_cycle_count_mismatch) {
|
2021-10-11 22:56:20 +00:00
|
|
|
if (cycles == 1) {
|
|
|
|
m_messages.push_back("Unimplemented");
|
|
|
|
} else {
|
|
|
|
|
|
|
|
os()
|
|
|
|
<< std::dec << std::setfill(' ')
|
|
|
|
<< "Stepped cycles: " << cycles
|
|
|
|
<< ", expected events: " << test().cycles().size()
|
2021-10-18 10:54:01 +00:00
|
|
|
<< ", actual events: " << m_actualCycles.size();
|
2021-10-11 22:56:20 +00:00
|
|
|
m_messages.push_back(os().str());
|
|
|
|
os().str("");
|
|
|
|
|
2021-10-18 10:54:01 +00:00
|
|
|
dumpCycles("-- Expected cycles", test().cycles());
|
|
|
|
dumpCycles("-- Actual cycles", m_actualCycles);
|
2021-10-11 22:56:20 +00:00
|
|
|
}
|
2021-10-11 09:20:18 +00:00
|
|
|
}
|
2021-10-10 20:26:30 +00:00
|
|
|
lowerPOWER();
|
2021-10-11 09:20:18 +00:00
|
|
|
return valid;
|
2021-10-10 20:26:30 +00:00
|
|
|
}
|