Skip to content

Commit

Permalink
fixes for serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertvanheusden committed May 16, 2024
1 parent f2ac06b commit bf610e1
Show file tree
Hide file tree
Showing 23 changed files with 71 additions and 76 deletions.
28 changes: 7 additions & 21 deletions bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonVariant>();
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)
Expand Down
11 changes: 7 additions & 4 deletions cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2434,9 +2434,9 @@ void cpu::step()
}
}

void cpu::serialize(JsonVariant j_in)
JsonDocument cpu::serialize()
{
JsonVariant j = j_in["cpu"].to<JsonVariant>();
JsonDocument j;

for(int set=0; set<2; set++) {
for(int regnr=0; regnr<6; regnr++)
Expand All @@ -2463,16 +2463,19 @@ 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<JsonArray>();
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;
}

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)
Expand Down
2 changes: 1 addition & 1 deletion cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> check_breakpoint();
Expand Down
2 changes: 1 addition & 1 deletion debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions disk_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonArray>();

for(size_t i=0; i<id.second.size(); i++)
j_data.add(id.second.at(i));
j_data_work.add(id.second.at(i));

out[format("%lu", id.first)] = j_data;
}
Expand Down
4 changes: 2 additions & 2 deletions disk_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class disk_backend
std::optional<std::vector<uint8_t> > get_object_from_overlay(const off_t id);
std::optional<std::vector<uint8_t> > 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;
Expand Down
4 changes: 2 additions & 2 deletions disk_backend_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion disk_backend_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
4 changes: 2 additions & 2 deletions disk_backend_nbd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion disk_backend_nbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down
2 changes: 0 additions & 2 deletions gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions kw11-l.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion kw11-l.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonArray>();
for(size_t i=0; i<size; i++)
ja.add(m[i]);
ja_work.add(m[i]);
j["contents"] = ja;

return j;
Expand Down
2 changes: 1 addition & 1 deletion memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class memory

void reset();

JsonVariant serialize() const;
JsonDocument serialize() const;
static memory *deserialize(const JsonVariantConst j);

uint16_t read_byte(const uint32_t a) const { return m[a]; }
Expand Down
18 changes: 10 additions & 8 deletions mmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,26 +505,28 @@ uint32_t mmu::calculate_physical_address(cpu *const c, const int run_mode, const
return m_offset;
}

JsonVariant mmu::add_par_pdr(const int run_mode, const bool is_d) const
JsonDocument mmu::add_par_pdr(const int run_mode, const bool is_d) const
{
JsonVariant j;
JsonDocument j;

JsonArray ja_par;
JsonDocument ja_par;
JsonArray ja_par_work = ja_par.to<JsonArray>();
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<JsonArray>();
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)
Expand Down
4 changes: 2 additions & 2 deletions mmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions rk05.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonArray>();
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++)
Expand Down
2 changes: 1 addition & 1 deletion rk05.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions rl02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonArray>();
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++)
Expand Down
2 changes: 1 addition & 1 deletion rl02.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 8 additions & 6 deletions tty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonArray>();
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<JsonArray>();
for(auto c: chars)
ja_buf.add(static_cast<signed char>(c));
ja_buf_work.add(static_cast<signed char>(c));
j["input-buffer"] = ja_buf;

return j;
Expand Down
2 changes: 1 addition & 1 deletion tty.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit bf610e1

Please sign in to comment.