From 1234aaf9d927cf01a8e989d4c425da658a99bda6 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Mon, 18 Oct 2021 13:12:22 +0100 Subject: [PATCH] Refactor RAM test values. Signed-off-by: Adrian Conlon --- M6502/HarteTest_6502/HarteTest_6502.vcxproj | 2 + .../HarteTest_6502.vcxproj.filters | 6 ++ M6502/HarteTest_6502/ram_t.cpp | 67 +++++++++++++++++++ M6502/HarteTest_6502/ram_t.h | 55 +++++++++++++++ M6502/HarteTest_6502/state_t.cpp | 59 +++------------- M6502/HarteTest_6502/state_t.h | 10 +-- M6502/HarteTest_6502/stdafx.h | 8 +-- 7 files changed, 145 insertions(+), 62 deletions(-) create mode 100644 M6502/HarteTest_6502/ram_t.cpp create mode 100644 M6502/HarteTest_6502/ram_t.h diff --git a/M6502/HarteTest_6502/HarteTest_6502.vcxproj b/M6502/HarteTest_6502/HarteTest_6502.vcxproj index 9dbe797..73004e0 100644 --- a/M6502/HarteTest_6502/HarteTest_6502.vcxproj +++ b/M6502/HarteTest_6502/HarteTest_6502.vcxproj @@ -164,6 +164,7 @@ + NotUsing NotUsing @@ -186,6 +187,7 @@ + diff --git a/M6502/HarteTest_6502/HarteTest_6502.vcxproj.filters b/M6502/HarteTest_6502/HarteTest_6502.vcxproj.filters index 7d3d754..dbea32b 100644 --- a/M6502/HarteTest_6502/HarteTest_6502.vcxproj.filters +++ b/M6502/HarteTest_6502/HarteTest_6502.vcxproj.filters @@ -44,6 +44,9 @@ Source Files + + Source Files + @@ -73,6 +76,9 @@ Header Files + + Header Files + diff --git a/M6502/HarteTest_6502/ram_t.cpp b/M6502/HarteTest_6502/ram_t.cpp new file mode 100644 index 0000000..d13dcb0 --- /dev/null +++ b/M6502/HarteTest_6502/ram_t.cpp @@ -0,0 +1,67 @@ +#include "stdafx.h" +#include "ram_t.h" + +#ifdef USE_SIMDJSON_JSON + +ram_t::ram_t(simdjson::dom::array input) { + 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); + const auto address = (uint16_t)(uint64_t)byte.at(0); + const auto value = (uint8_t)(uint64_t)byte.at(1); + m_bytes.push_back({ address, value }); + } +} + +#endif + +#ifdef USE_BOOST_JSON + +ram_t::ram_t(const boost::json::array& input) { + m_bytes.reserve(input.size()); + for (const auto& byte : input) { + const auto& ram_entry_array = byte.as_array(); + assert(ram_entry_array.size() == 2); + const auto address = (uint16_t)ram_entry_array.at(0).as_int64(); + const auto value = (uint8_t)ram_entry_array.at(1).as_int64(); + m_bytes.push_back({ address, value }); + } +} + +ram_t::ram_t(const boost::json::value& input) +: ram_t(input.as_array()) {} + +#endif + +#ifdef USE_NLOHMANN_JSON + +ram_t::ram_t(const nlohmann::json& input) { + assert(input.is_array()); + for (const auto& byte : input) { + assert(byte.is_array()); + assert(byte.size() == 2); + const auto address = byte[0].get(); + const auto value = byte[1].get(); + m_bytes.push_back({ address, value }); + } +} + +#endif + +#ifdef USE_JSONCPP_JSON + +ram_t::ram_t(const Json::Value& input) { + + assert(input.isArray()); + 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 }); + } +} + +#endif diff --git a/M6502/HarteTest_6502/ram_t.h b/M6502/HarteTest_6502/ram_t.h new file mode 100644 index 0000000..c9011d1 --- /dev/null +++ b/M6502/HarteTest_6502/ram_t.h @@ -0,0 +1,55 @@ +#pragma once + +#include +#include +#include + +#ifdef USE_SIMDJSON_JSON +# include "simdjson/simdjson.h" +#endif + +#ifdef USE_BOOST_JSON +# include +#endif + +#ifdef USE_NLOHMANN_JSON +# include "nlohmann/json.hpp" +#endif + +#ifdef USE_JSONCPP_JSON +# include +#endif + +class ram_t { +private: + std::vector> m_bytes; + +public: +#ifdef USE_SIMDJSON_JSON + ram_t(simdjson::dom::array input); +#endif + +#ifdef USE_BOOST_JSON + ram_t(const boost::json::value& input); + ram_t(const boost::json::array& input); +#endif + +#ifdef USE_NLOHMANN_JSON + ram_t(const nlohmann::json& input); +#endif + +#ifdef USE_JSONCPP_JSON + ram_t(const Json::Value& input); +#endif + + [[nodiscard]] auto begin() const { return m_bytes.begin(); } + [[nodiscard]] auto end() const { return m_bytes.end(); } + + [[nodiscard]] auto size() const noexcept { return m_bytes.size(); } + + void clear() { m_bytes.clear(); } + + [[nodiscard]] auto& at(size_t idx) { return m_bytes.at(idx); } + [[nodiscard]] const auto& at(size_t idx) const { return m_bytes.at(idx); } +}; + diff --git a/M6502/HarteTest_6502/state_t.cpp b/M6502/HarteTest_6502/state_t.cpp index 7b7b7da..abed83e 100644 --- a/M6502/HarteTest_6502/state_t.cpp +++ b/M6502/HarteTest_6502/state_t.cpp @@ -2,8 +2,6 @@ #include "state_t.h" #include -state_t::state_t() {} - #ifdef USE_SIMDJSON_JSON state_t::state_t(const simdjson::dom::element serialised) @@ -12,19 +10,8 @@ state_t::state_t(const simdjson::dom::element serialised) m_a((uint8_t)(uint64_t)serialised["a"]), m_x((uint8_t)(uint64_t)serialised["x"]), m_y((uint8_t)(uint64_t)serialised["y"]), - m_p((uint8_t)(uint64_t)serialised["p"]) { - - const auto ram_entries = serialised["ram"].get_array(); - m_ram.reserve(ram_entries.size()); - for (const auto ram_entry : ram_entries) { - assert(ram_entry.is_array()); - const auto ram_entry_array = ram_entry.get_array(); - assert(ram_entry_array.size() == 2); - const auto address = (uint16_t)(uint64_t)ram_entry_array.at(0); - const auto value = (uint8_t)(uint64_t)ram_entry_array.at(1); - m_ram.push_back({ address, value }); - } -} + m_p((uint8_t)(uint64_t)serialised["p"]), + m_ram(serialised["ram"].get_array()) {} #endif @@ -36,18 +23,8 @@ state_t::state_t(const boost::json::object& serialised) m_a((uint8_t)serialised.at("a").as_int64()), m_x((uint8_t)serialised.at("x").as_int64()), m_y((uint8_t)serialised.at("y").as_int64()), - m_p((uint8_t)serialised.at("p").as_int64()) { - - const auto& ram_entries = serialised.at("ram").as_array(); - m_ram.reserve(ram_entries.size()); - for (const auto& ram_entry : ram_entries) { - const auto& ram_entry_array = ram_entry.as_array(); - assert(ram_entry_array.size() == 2); - const auto address = (uint16_t)ram_entry_array.at(0).as_int64(); - const auto value = (uint8_t)ram_entry_array.at(1).as_int64(); - m_ram.push_back({ address, value }); - } -} + m_p((uint8_t)serialised.at("p").as_int64()), + m_ram(serialised.at("ram")) {} state_t::state_t(const boost::json::value& serialised) : state_t(serialised.as_object()) {} @@ -62,18 +39,8 @@ state_t::state_t(const nlohmann::json& serialised) m_a(serialised["a"].get()), m_x(serialised["x"].get()), m_y(serialised["y"].get()), - m_p(serialised["p"].get()) { - - const auto& ram_entries = serialised["ram"]; - assert(ram_entries.is_array()); - for (const auto& ram_entry : ram_entries) { - assert(ram_entry.is_array()); - assert(ram_entry.size() == 2); - const auto address = ram_entry[0].get(); - const auto value = ram_entry[1].get(); - m_ram.push_back({ address, value }); - } -} + m_p(serialised["p"].get()), + m_ram(serialised["ram"]) {} #endif @@ -85,17 +52,7 @@ state_t::state_t(const Json::Value& serialised) m_a(serialised["a"].asUInt()), m_x(serialised["x"].asUInt()), m_y(serialised["y"].asUInt()), - m_p(serialised["p"].asUInt()) { - - const auto& ram_entries = serialised["ram"]; - assert(ram_entries.isArray()); - for (const auto& ram_entry : ram_entries) { - assert(ram_entry.isArray()); - assert(ram_entry.size() == 2); - const auto address = ram_entry[0].asUInt(); - const auto value = ram_entry[1].asUInt(); - m_ram.push_back({ address, value }); - } -} + m_p(serialised["p"].asUInt()), + m_ram(serialised["ram"]) {} #endif diff --git a/M6502/HarteTest_6502/state_t.h b/M6502/HarteTest_6502/state_t.h index 1908925..f2b0933 100644 --- a/M6502/HarteTest_6502/state_t.h +++ b/M6502/HarteTest_6502/state_t.h @@ -1,8 +1,6 @@ #pragma once #include -#include -#include #ifdef USE_SIMDJSON_JSON # include "simdjson/simdjson.h" @@ -20,6 +18,8 @@ # include #endif +#include "ram_t.h" + class state_t final { private: uint16_t m_pc = 0xffff; @@ -28,11 +28,7 @@ private: uint8_t m_x = 0xff; uint8_t m_y = 0xff; uint8_t m_p = 0xff; - std::vector> m_ram; - -#ifdef USE_JSONCPP_JSON - void initialise(const Json::Value& serialised); -#endif + ram_t m_ram; public: state_t(); diff --git a/M6502/HarteTest_6502/stdafx.h b/M6502/HarteTest_6502/stdafx.h index fd42132..73743f6 100644 --- a/M6502/HarteTest_6502/stdafx.h +++ b/M6502/HarteTest_6502/stdafx.h @@ -7,16 +7,16 @@ #include #include #include -#include +#include #include #include //#define TEST_JSON_PERFORMANCE -#define USE_SIMDJSON_JSON // 14 seconds -//#define USE_BOOST_JSON // 28 seconds +#define USE_SIMDJSON_JSON // 13 seconds +//#define USE_BOOST_JSON // 26 seconds //#define USE_NLOHMANN_JSON // 56 seconds -//#define USE_JSONCPP_JSON // 97 seconds +//#define USE_JSONCPP_JSON // 89 seconds #ifdef USE_SIMDJSON_JSON # define JSON_PREFER_PASS_BY_VALUE