Refactor the ram_t class to be a container of byte_t

Signed-off-by: Adrian Conlon <adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2021-10-19 09:28:13 +01:00
parent 2be190af19
commit 710c8fb883
13 changed files with 171 additions and 57 deletions

View File

@ -161,6 +161,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="byte_t.cpp" />
<ClCompile Include="cycles_t.cpp" />
<ClCompile Include="cycle_t.cpp" />
<ClCompile Include="opcode_test_suite_t.cpp" />
@ -183,6 +184,7 @@
<ClCompile Include="test_t.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="byte_t.h" />
<ClInclude Include="cycles_t.h" />
<ClInclude Include="cycle_t.h" />
<ClInclude Include="nlohmann\json.hpp" />
@ -225,7 +227,6 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="rapidjson\rapidjson-master.zip" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -50,6 +50,9 @@
<ClCompile Include="ram_t.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="byte_t.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">
@ -145,11 +148,11 @@
<ClInclude Include="rapidjson\writer.h">
<Filter>Header Files\rapidjson</Filter>
</ClInclude>
<ClInclude Include="byte_t.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="rapidjson\rapidjson-master.zip">
<Filter>Header Files\rapidjson</Filter>
</None>
</ItemGroup>
</Project>

View File

@ -130,10 +130,8 @@ void TestRunner::initialiseState() {
CPU().Y() = starting.y();
CPU().P() = starting.p();
const auto& ram = starting.ram();
for (const auto& entry : ram) {
const auto [address, value] = entry;
RAM().poke(address, value);
}
for (const auto& entry : ram)
RAM().poke(entry.address(), entry.value());
m_actualCycles.clear();
}
@ -166,8 +164,7 @@ bool TestRunner::checkState() {
const auto& ram = finished.ram();
bool ram_problem = false;
for (const auto& entry : ram) {
const auto [address, value] = entry;
const auto ram_good = check("RAM", address, value, RAM().peek(address));
const auto ram_good = check("RAM", entry.address(), entry.value(), RAM().peek(entry.address()));
if (!ram_good && !ram_problem)
ram_problem = true;
}

View File

@ -0,0 +1,66 @@
#include "stdafx.h"
#include "byte_t.h"
#include <cassert>
#include <stdexcept>
byte_t::byte_t(uint16_t address, uint8_t value) noexcept
: m_address(address),
m_value(value) {}
#ifdef USE_SIMDJSON_JSON
byte_t::byte_t(simdjson::dom::element input) noexcept
: byte_t(input.get_array()) {}
byte_t::byte_t(simdjson::dom::array input) noexcept
: m_iterator(input.begin()),
m_address((uint16_t)(uint64_t)*m_iterator),
m_value((uint8_t)(uint64_t)* ++m_iterator) {
assert(input.size() == 2);
}
#endif
#ifdef USE_RAPIDJSON_JSON
byte_t::byte_t(const rapidjson::Value& input)
: m_address((uint8_t)input[0].GetInt64()),
m_value((uint8_t)input[1].GetInt64()) {
assert(input.Size() == 2);
}
#endif
#ifdef USE_BOOST_JSON
byte_t::byte_t(const boost::json::value& input) noexcept
: byte_t(input.get_array()) {}
byte_t::byte_t(const boost::json::array& input) noexcept
: m_address((uint16_t)input[0].get_int64()),
m_value((uint8_t)input[1].get_int64()) {
assert(input.size() == 2);
};
#endif
#ifdef USE_NLOHMANN_JSON
byte_t::byte_t(const nlohmann::json& input) noexcept
: m_address(input[0].get<uint16_t>()),
m_value(input[1].get<uint8_t>()) {
assert(input.size() == 2);
}
#endif
#ifdef USE_JSONCPP_JSON
byte_t::byte_t(const Json::Value& input)
: m_address(input[0].asUInt()),
m_value(input[1].asUInt()) {
assert(input.size() == 2);
}
#endif

View File

@ -0,0 +1,61 @@
#pragma once
#include <cstdint>
#ifdef USE_SIMDJSON_JSON
# include "simdjson/simdjson.h"
#endif
#ifdef USE_RAPIDJSON_JSON
# include "rapidjson/document.h"
#endif
#ifdef USE_BOOST_JSON
# include <boost/json.hpp>
#endif
#ifdef USE_NLOHMANN_JSON
# include "nlohmann/json.hpp"
#endif
#ifdef USE_JSONCPP_JSON
# include <json/json.h>
#endif
class byte_t final {
private:
#ifdef USE_SIMDJSON_JSON
simdjson::dom::array::iterator m_iterator;
#endif
uint16_t m_address = 0xffff;
uint8_t m_value = 0xff;
public:
byte_t(uint16_t address, uint8_t value) noexcept;
#ifdef USE_SIMDJSON_JSON
byte_t(simdjson::dom::element input) noexcept;
byte_t(simdjson::dom::array input) noexcept;
#endif
#ifdef USE_RAPIDJSON_JSON
byte_t(const rapidjson::Value& input);
#endif
#ifdef USE_BOOST_JSON
byte_t(const boost::json::value& input) noexcept;
byte_t(const boost::json::array& input) noexcept;
#endif
#ifdef USE_NLOHMANN_JSON
byte_t(const nlohmann::json& input) noexcept;
#endif
#ifdef USE_JSONCPP_JSON
byte_t(const Json::Value& input);
#endif
[[nodiscard]] constexpr auto address() const noexcept { return m_address; }
[[nodiscard]] constexpr auto value() const noexcept { return m_value; }
};

View File

@ -8,7 +8,7 @@
#endif
#ifdef USE_RAPIDJSON_JSON
# include "rapidjson/rapidjson.h"
# include "rapidjson/document.h"
#endif
#ifdef USE_BOOST_JSON

View File

@ -7,7 +7,7 @@
#endif
#ifdef USE_RAPIDJSON_JSON
# include "rapidjson/rapidjson.h"
# include "rapidjson/document.h"
#endif
#ifdef USE_BOOST_JSON

View File

@ -7,7 +7,6 @@
#endif
#ifdef USE_RAPIDJSON_JSON
# include "rapidjson/rapidjson.h"
# include "rapidjson/document.h"
#endif

View File

@ -1,19 +1,18 @@
#include "stdafx.h"
#include "ram_t.h"
void ram_t::add(const byte_t& byte) {
assert(m_bytes.capacity() >= (m_bytes.size() + 1));
m_bytes.push_back(byte);
}
#ifdef USE_SIMDJSON_JSON
ram_t::ram_t(simdjson::dom::array input) {
assert(m_bytes.empty());
m_bytes.reserve(input.size());
for (const auto byte : input) {
assert(byte.is_array());
const auto ram_entry_array = byte.get_array();
assert(byte.size() == 2);
auto iterator = byte.begin();
const auto address = (uint16_t)(uint64_t)*iterator;
const auto value = (uint8_t)(uint64_t)*++iterator;
m_bytes.push_back({ address, value });
}
for (const auto& entry : input)
add(entry);
}
#endif
@ -21,27 +20,21 @@ ram_t::ram_t(simdjson::dom::array input) {
#ifdef USE_RAPIDJSON_JSON
ram_t::ram_t(const rapidjson::Value& input) {
assert(m_bytes.empty());
m_bytes.reserve(input.Size());
for (const auto& byte : input.GetArray()) {
assert(byte.Size() == 2);
const auto address = (uint16_t)byte[0].GetInt64();
const auto value = (uint8_t)byte[1].GetInt64();
m_bytes.push_back({ address, value });
}
for (const auto& entry : input.GetArray())
add(entry);
}
#endif
#ifdef USE_BOOST_JSON
ram_t::ram_t(const boost::json::array& input) {
assert(m_bytes.empty());
m_bytes.reserve(input.size());
for (const auto& byte : input) {
const auto& ram_entry_array = byte.get_array();
assert(ram_entry_array.size() == 2);
const auto address = (uint16_t)ram_entry_array[0].get_int64();
const auto value = (uint8_t)ram_entry_array[1].get_int64();
m_bytes.push_back({ address, value });
}
for (const auto& entry : input)
add(entry);
}
ram_t::ram_t(const boost::json::value& input)
@ -52,15 +45,10 @@ ram_t::ram_t(const boost::json::value& input)
#ifdef USE_NLOHMANN_JSON
ram_t::ram_t(const nlohmann::json& input) {
assert(input.is_array());
assert(m_bytes.empty());
m_bytes.reserve(input.size());
for (const auto& byte : input) {
assert(byte.is_array());
assert(byte.size() == 2);
const auto address = byte[0].get<uint16_t>();
const auto value = byte[1].get<uint8_t>();
m_bytes.push_back({ address, value });
}
for (const auto& entry : input)
add(entry);
}
#endif
@ -68,15 +56,10 @@ ram_t::ram_t(const nlohmann::json& input) {
#ifdef USE_JSONCPP_JSON
ram_t::ram_t(const Json::Value& input) {
assert(input.isArray());
assert(m_bytes.empty());
m_bytes.reserve(input.size());
for (const auto& byte : input) {
assert(byte.isArray());
assert(byte.size() == 2);
const auto address = byte[0].asUInt();
const auto value = byte[1].asUInt();
m_bytes.push_back({ address, value });
}
for (const auto& entry : input)
add(entry);
}
#endif

View File

@ -9,7 +9,7 @@
#endif
#ifdef USE_RAPIDJSON_JSON
# include "rapidjson/rapidjson.h"
# include "rapidjson/document.h"
#endif
#ifdef USE_BOOST_JSON
@ -24,9 +24,11 @@
# include <json/json.h>
#endif
class ram_t {
#include "byte_t.h"
class ram_t final {
private:
std::vector<std::pair<uint16_t, uint8_t>> m_bytes;
std::vector<byte_t> m_bytes;
public:
#ifdef USE_SIMDJSON_JSON
@ -50,6 +52,8 @@ public:
ram_t(const Json::Value& input);
#endif
void add(const byte_t& byte);
[[nodiscard]] auto begin() const noexcept { return m_bytes.begin(); }
[[nodiscard]] auto end() const noexcept { return m_bytes.end(); }

View File

@ -7,7 +7,7 @@
#endif
#ifdef USE_RAPIDJSON_JSON
# include "rapidjson/rapidjson.h"
# include "rapidjson/document.h"
#endif
#ifdef USE_BOOST_JSON

View File

@ -26,7 +26,8 @@
#endif
#ifdef USE_RAPIDJSON_JSON
# include "rapidjson/rapidjson.h"
# define RAPIDJSON_HAS_STDSTRING 1
# define RAPIDJSON_SSE42
# include "rapidjson/document.h"
#endif

View File

@ -10,7 +10,6 @@
#endif
#ifdef USE_RAPIDJSON_JSON
# include "rapidjson/rapidjson.h"
# include "rapidjson/document.h"
#endif