From bf610e17b29d2b8f580704cae493a62a7d301904 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Thu, 16 May 2024 10:14:48 +0200 Subject: [PATCH] fixes for serialization --- bus.cpp | 28 +++++++--------------------- cpu.cpp | 11 +++++++---- cpu.h | 2 +- debugger.cpp | 2 +- disk_backend.cpp | 9 +++++---- disk_backend.h | 4 ++-- disk_backend_file.cpp | 4 ++-- disk_backend_file.h | 2 +- disk_backend_nbd.cpp | 4 ++-- disk_backend_nbd.h | 2 +- gen.h | 2 -- kw11-l.cpp | 4 ++-- kw11-l.h | 2 +- memory.cpp | 9 +++++---- memory.h | 2 +- mmu.cpp | 18 ++++++++++-------- mmu.h | 4 ++-- rk05.cpp | 9 +++++---- rk05.h | 2 +- rl02.cpp | 9 +++++---- rl02.h | 2 +- tty.cpp | 14 ++++++++------ tty.h | 2 +- 23 files changed, 71 insertions(+), 76 deletions(-) diff --git a/bus.cpp b/bus.cpp index 952900b..1784bde 100644 --- a/bus.cpp +++ b/bus.cpp @@ -45,48 +45,34 @@ bus::~bus() delete dc11_; } -void dump(JsonVariantConst j) -{ - std::string temp; - printf("%zu\n", serializeJson(j, temp)); - - printf("%s\r\n", temp.c_str()); -} - JsonDocument bus::serialize() const { - JsonDocument doc; - JsonVariant j_out = doc.to(); + JsonDocument j_out; if (m) j_out["memory"] = m->serialize(); if (kw11_l_) j_out["kw11-l"] = kw11_l_->serialize(); - dump(kw11_l_->serialize()); if (tty_) - j_out["tty"] = tty_->serialize(); - dump(tty_->serialize()); + j_out["tty"] = tty_->serialize(); if (mmu_) - j_out["mmu"] = mmu_->serialize(); - dump(mmu_->serialize()); + j_out["mmu"] = mmu_->serialize(); if (c) - c->serialize(j_out["cpu"]); + j_out["cpu"] = c->serialize(); if (rl02_) - j_out["rl02"] = rl02_->serialize(); - dump(rl02_->serialize()); + j_out["rl02"] = rl02_->serialize(); if (rk05_) - j_out["rk05"] = rk05_->serialize(); - dump(rk05_->serialize()); + j_out["rk05"] = rk05_->serialize(); // TODO: tm11, dc11 - return doc; + return j_out; } bus *bus::deserialize(const JsonDocument j, console *const cnsl, std::atomic_uint32_t *const event) diff --git a/cpu.cpp b/cpu.cpp index ade9285..299f778 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -2434,9 +2434,9 @@ void cpu::step() } } -void cpu::serialize(JsonVariant j_in) +JsonDocument cpu::serialize() { - JsonVariant j = j_in["cpu"].to(); + JsonDocument j; for(int set=0; set<2; set++) { for(int regnr=0; regnr<6; regnr++) @@ -2463,9 +2463,10 @@ void cpu::serialize(JsonVariant j_in) JsonVariant j_queued_interrupts; for(auto & il: queued_interrupts) { - JsonArray ja_qi_level; + JsonDocument ja_qi_level; + JsonArray ja_qi_level_work = ja_qi_level.to(); for(auto v: il.second) - ja_qi_level.add(v); + ja_qi_level_work.add(v); j_queued_interrupts[format("%d", il.first)] = ja_qi_level; } @@ -2473,6 +2474,8 @@ void cpu::serialize(JsonVariant j_in) j["queued_interrupts"] = j_queued_interrupts; j["any_queued_interrupts"] = bool(any_queued_interrupts); + + return j; } cpu *cpu::deserialize(const JsonVariantConst j, bus *const b, std::atomic_uint32_t *const event) diff --git a/cpu.h b/cpu.h index ff770df..f1edcd1 100644 --- a/cpu.h +++ b/cpu.h @@ -114,7 +114,7 @@ class cpu explicit cpu(bus *const b, std::atomic_uint32_t *const event); ~cpu(); - void serialize(JsonVariant j); + JsonDocument serialize(); static cpu *deserialize(const JsonVariantConst j, bus *const b, std::atomic_uint32_t *const event); std::optional check_breakpoint(); diff --git a/debugger.cpp b/debugger.cpp index 04c2c3e..f30f487 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -557,7 +557,7 @@ void serialize_state(console *const cnsl, const bus *const b, const std::string FILE *fh = fopen(filename.c_str(), "w"); if (fh) { state_writer ws { fh }; - serializeJson(j, ws); + serializeJsonPretty(j, ws); fclose(fh); ok = true; diff --git a/disk_backend.cpp b/disk_backend.cpp index cdb1304..d3a1171 100644 --- a/disk_backend.cpp +++ b/disk_backend.cpp @@ -61,15 +61,16 @@ bool disk_backend::store_mem_range_in_overlay(const off_t offset, const size_t n return false; } -JsonVariant disk_backend::serialize_overlay() const +JsonDocument disk_backend::serialize_overlay() const { - JsonVariant out; + JsonDocument out; for(auto & id: overlay) { - JsonVariant j_data; + JsonDocument j_data; + JsonArray j_data_work = j_data.to(); for(size_t i=0; i > get_object_from_overlay(const off_t id); std::optional > get_from_overlay(const off_t offset, const size_t sector_size); - JsonVariant serialize_overlay() const; + JsonDocument serialize_overlay() const; void deserialize_overlay(const JsonVariantConst j); public: disk_backend(); virtual ~disk_backend(); - virtual JsonVariant serialize() const = 0; + virtual JsonDocument serialize() const = 0; static disk_backend *deserialize(const JsonVariantConst j); virtual std::string get_identifier() const = 0; diff --git a/disk_backend_file.cpp b/disk_backend_file.cpp index a8521d0..ca31185 100644 --- a/disk_backend_file.cpp +++ b/disk_backend_file.cpp @@ -21,9 +21,9 @@ disk_backend_file::~disk_backend_file() close(fd); } -JsonVariant disk_backend_file::serialize() const +JsonDocument disk_backend_file::serialize() const { - JsonVariant j; + JsonDocument j; j["disk-backend-type"] = "file"; diff --git a/disk_backend_file.h b/disk_backend_file.h index 6c8a4b5..35c3c86 100644 --- a/disk_backend_file.h +++ b/disk_backend_file.h @@ -19,7 +19,7 @@ class disk_backend_file : public disk_backend disk_backend_file(const std::string & filename); virtual ~disk_backend_file(); - JsonVariant serialize() const override; + JsonDocument serialize() const override; static disk_backend_file *deserialize(const JsonVariantConst j); std::string get_identifier() const override { return filename; } diff --git a/disk_backend_nbd.cpp b/disk_backend_nbd.cpp index 36f7e27..4a4258b 100644 --- a/disk_backend_nbd.cpp +++ b/disk_backend_nbd.cpp @@ -48,9 +48,9 @@ disk_backend_nbd::~disk_backend_nbd() close(fd); } -JsonVariant disk_backend_nbd::serialize() const +JsonDocument disk_backend_nbd::serialize() const { - JsonVariant j; + JsonDocument j; j["disk-backend-type"] = "nbd"; diff --git a/disk_backend_nbd.h b/disk_backend_nbd.h index 39f0219..5d05859 100644 --- a/disk_backend_nbd.h +++ b/disk_backend_nbd.h @@ -22,7 +22,7 @@ class disk_backend_nbd : public disk_backend disk_backend_nbd(const std::string & host, const unsigned port); virtual ~disk_backend_nbd(); - JsonVariant serialize() const override; + JsonDocument serialize() const override; static disk_backend_nbd *deserialize(const JsonVariantConst j); std::string get_identifier() const override { return format("%s:%d", host.c_str(), port); } diff --git a/gen.h b/gen.h index 20c26fb..caeaa23 100644 --- a/gen.h +++ b/gen.h @@ -3,8 +3,6 @@ #pragma once -#define ARDUINOJSON_DEBUG 1 - // #define TURBO typedef enum { EVENT_NONE = 0, EVENT_HALT, EVENT_INTERRUPT, EVENT_TERMINATE } stop_event_t; diff --git a/kw11-l.cpp b/kw11-l.cpp index 42c99e0..7ea882d 100644 --- a/kw11-l.cpp +++ b/kw11-l.cpp @@ -216,9 +216,9 @@ uint8_t kw11_l::get_lf_crs() return rc; } -JsonVariant kw11_l::serialize() +JsonDocument kw11_l::serialize() { - JsonVariant j; + JsonDocument j; j["CSR"] = lf_csr; diff --git a/kw11-l.h b/kw11-l.h index 5ee0f61..f921bd3 100644 --- a/kw11-l.h +++ b/kw11-l.h @@ -43,7 +43,7 @@ class kw11_l: public device void show_state(console *const cnsl) const override; - JsonVariant serialize(); + JsonDocument serialize(); static kw11_l *deserialize(const JsonVariantConst j, bus *const b, console *const cnsl); void begin(console *const cnsl); diff --git a/memory.cpp b/memory.cpp index 6140b82..9ffe8b4 100644 --- a/memory.cpp +++ b/memory.cpp @@ -40,15 +40,16 @@ void memory::reset() memset(m, 0x00, size); } -JsonVariant memory::serialize() const +JsonDocument memory::serialize() const { - JsonVariant j; + JsonDocument j; j["size"] = size; - JsonArray ja; + JsonDocument ja; + JsonArray ja_work = ja.to(); for(size_t i=0; i(); for(int i=0; i<8; i++) - ja_par.add(pages[run_mode][is_d][i].par); + ja_par_work.add(pages[run_mode][is_d][i].par); j["par"] = ja_par; - JsonArray ja_pdr; + JsonDocument ja_pdr; + JsonArray ja_pdr_work = ja_pdr.to(); for(int i=0; i<8; i++) - ja_pdr.add(pages[run_mode][is_d][i].pdr); + ja_pdr_work.add(pages[run_mode][is_d][i].pdr); j["pdr"] = ja_pdr; return j; } -JsonVariant mmu::serialize() const +JsonDocument mmu::serialize() const { - JsonVariant j; + JsonDocument j; for(int run_mode=0; run_mode<4; run_mode++) { if (run_mode == 2) diff --git a/mmu.h b/mmu.h index 591e00d..c1316d4 100644 --- a/mmu.h +++ b/mmu.h @@ -57,7 +57,7 @@ class mmu : public device memory *m { nullptr }; - JsonVariant add_par_pdr(const int run_mode, const bool is_d) const; + JsonDocument add_par_pdr(const int run_mode, const bool is_d) const; void set_par_pdr(const JsonVariantConst j_in, const int run_mode, const bool is_d); void verify_page_access (cpu *const c, const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write); @@ -70,7 +70,7 @@ class mmu : public device void begin(memory *const m); - JsonVariant serialize() const; + JsonDocument serialize() const; static mmu *deserialize(const JsonVariantConst j, memory *const m); void mmudebug(const uint16_t a); diff --git a/rk05.cpp b/rk05.cpp index 4def28f..2ed0d6b 100644 --- a/rk05.cpp +++ b/rk05.cpp @@ -281,13 +281,14 @@ void rk05::write_word(const uint16_t addr, const uint16_t v) } } -JsonVariant rk05::serialize() const +JsonDocument rk05::serialize() const { - JsonVariant j; + JsonDocument j; - JsonArray j_backends; + JsonDocument j_backends; + JsonArray j_backends_work = j_backends.to(); for(auto & dbe: fhs) - j_backends.add(dbe->serialize()); + j_backends_work.add(dbe->serialize()); j["backends"] = j_backends; for(int regnr=0; regnr<7; regnr++) diff --git a/rk05.h b/rk05.h index 4c94058..b1035ce 100644 --- a/rk05.h +++ b/rk05.h @@ -49,7 +49,7 @@ class rk05: public disk_device void show_state(console *const cnsl) const override; - JsonVariant serialize() const; + JsonDocument serialize() const; static rk05 *deserialize(const JsonVariantConst j, bus *const b); uint8_t read_byte(const uint16_t addr) override; diff --git a/rl02.cpp b/rl02.cpp index aacaa38..8c2c0ef 100644 --- a/rl02.cpp +++ b/rl02.cpp @@ -72,13 +72,14 @@ void rl02::show_state(console *const cnsl) const cnsl->put_string_lf(format("sector: %d", sector)); } -JsonVariant rl02::serialize() const +JsonDocument rl02::serialize() const { - JsonVariant j; + JsonDocument j; - JsonArray j_backends; + JsonDocument j_backends; + JsonArray j_backends_work = j_backends.to(); for(auto & dbe: fhs) - j_backends.add(dbe->serialize()); + j_backends_work.add(dbe->serialize()); j["backends"] = j_backends; for(int regnr=0; regnr<4; regnr++) diff --git a/rl02.h b/rl02.h index 1128fe2..b7bfdc3 100644 --- a/rl02.h +++ b/rl02.h @@ -56,7 +56,7 @@ class rl02: public disk_device void show_state(console *const cnsl) const override; - JsonVariant serialize() const; + JsonDocument serialize() const; static rl02 *deserialize(const JsonVariantConst j, bus *const b); uint8_t read_byte(const uint16_t addr) override; diff --git a/tty.cpp b/tty.cpp index c2361b2..36f1a3a 100644 --- a/tty.cpp +++ b/tty.cpp @@ -194,18 +194,20 @@ void tty::write_word(const uint16_t addr, uint16_t v) registers[(addr - PDP11TTY_BASE) / 2] = v; } -JsonVariant tty::serialize() +JsonDocument tty::serialize() { - JsonVariant j; + JsonDocument j; - JsonArray ja_reg; + JsonDocument ja_reg; + JsonArray ja_reg_work = ja_reg.to(); for(size_t i=0; i<4; i++) - ja_reg.add(registers[i]); + ja_reg_work.add(registers[i]); j["registers"] = ja_reg; - JsonArray ja_buf; + JsonDocument ja_buf; + JsonArray ja_buf_work = ja_buf.to(); for(auto c: chars) - ja_buf.add(static_cast(c)); + ja_buf_work.add(static_cast(c)); j["input-buffer"] = ja_buf; return j; diff --git a/tty.h b/tty.h index 50c1016..ac97681 100644 --- a/tty.h +++ b/tty.h @@ -52,7 +52,7 @@ class tty tty(console *const c, bus *const b); virtual ~tty(); - JsonVariant serialize(); + JsonDocument serialize(); static tty *deserialize(const JsonVariantConst j, bus *const b, console *const cnsl); void reset();