more serialize code

This commit is contained in:
folkert van heusden 2024-04-25 16:10:09 +02:00
parent 6ab59835e7
commit 10193fbf59
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
6 changed files with 127 additions and 13 deletions

44
bus.cpp
View file

@ -47,17 +47,45 @@ json_t *bus::serialize()
{ {
json_t *j_out = json_object(); json_t *j_out = json_object();
json_object_set(j_out, "memory", m->serialize()); if (m)
json_object_set(j_out, "memory", m->serialize());
if (kw11_l_)
json_object_set(j_out, "kw11-l", kw11_l_->serialize());
if (tty_)
json_object_set(j_out, "tty", tty_->serialize());
// TODO: mmu, cpu, rl02, rk05, tm11
return j_out; return j_out;
} }
bus *bus::deserialize(const json_t *const j) bus *bus::deserialize(const json_t *const j, console *const cnsl)
{ {
bus *b = new bus(); bus *b = new bus();
memory *m = memory::deserialize(json_object_get(j, "memory")); json_t *temp = nullptr;
b->add_ram(m);
temp = json_object_get(j, "memory");
if (temp) {
memory *m = memory::deserialize(temp);
b->add_ram(m);
}
temp = json_object_get(j, "kw11-l");
if (temp) {
kw11_l *kw11_l_ = kw11_l::deserialize(temp, b, cnsl);
b->add_KW11_L(kw11_l_);
}
temp = json_object_get(j, "tty");
if (temp) {
tty *tty_ = tty::deserialize(temp, b, cnsl);
b->add_tty(tty_);
}
// TODO: mmu, cpu, rl02, rk05, tm11
return b; return b;
} }
@ -91,6 +119,14 @@ void bus::reset()
rl02_->reset(); rl02_->reset();
if (tty_) if (tty_)
tty_->reset(); tty_->reset();
if (kw11_l_)
kw11_l_->reset();
}
void bus::add_KW11_L(kw11_l *const kw11_l_)
{
delete this->kw11_l_;
this->kw11_l_ = kw11_l_;
} }
void bus::add_ram(memory *const m) void bus::add_ram(memory *const m)

16
bus.h
View file

@ -45,6 +45,7 @@
#define ADDR_CCR 0177746 #define ADDR_CCR 0177746
#define ADDR_SYSTEM_ID 0177764 #define ADDR_SYSTEM_ID 0177764
class console;
class cpu; class cpu;
class kw11_l; class kw11_l;
class memory; class memory;
@ -91,7 +92,7 @@ public:
#if IS_POSIX #if IS_POSIX
json_t *serialize(); json_t *serialize();
static bus *deserialize(const json_t *const j); static bus *deserialize(const json_t *const j, console *const cnsl);
#endif #endif
void reset(); void reset();
@ -108,12 +109,13 @@ public:
void mmudebug(const uint16_t a); void mmudebug(const uint16_t a);
void add_ram (memory *const m ); void add_ram (memory *const m );
void add_cpu (cpu *const c ); void add_cpu (cpu *const c );
void add_tm11(tm_11 *const tm11 ); void add_tm11 (tm_11 *const tm11 );
void add_rk05(rk05 *const rk05_); void add_rk05 (rk05 *const rk05_ );
void add_rl02(rl02 *const rl02_); void add_rl02 (rl02 *const rl02_ );
void add_tty (tty *const tty_ ); void add_tty (tty *const tty_ );
void add_KW11_L(kw11_l *const kw11_l_);
memory *getRAM() { return m; } memory *getRAM() { return m; }
cpu *getCpu() { return c; } cpu *getCpu() { return c; }

View file

@ -57,6 +57,11 @@ void kw11_l::begin(console *const cnsl)
#endif #endif
} }
void kw11_l::reset()
{
lf_csr = 0;
}
void kw11_l::operator()() void kw11_l::operator()()
{ {
set_thread_name("kek:kw-11l"); set_thread_name("kek:kw-11l");
@ -154,3 +159,25 @@ uint8_t kw11_l::get_lf_crs()
return rc; return rc;
} }
#if IS_POSIX
json_t *kw11_l::serialize()
{
json_t *j = json_object();
json_object_set(j, "CSR", json_integer(lf_csr));
return j;
}
kw11_l *kw11_l::deserialize(const json_t *const j, bus *const b, console *const cnsl)
{
uint16_t CSR = json_integer_value(json_object_get(j, "CSR"));
kw11_l *out = new kw11_l(b);
out->lf_csr = CSR;
out->begin(cnsl);
return out;
}
#endif

View file

@ -6,6 +6,7 @@
#include "bus.h" #include "bus.h"
#include "console.h" #include "console.h"
#include "gen.h"
class kw11_l class kw11_l
@ -31,6 +32,13 @@ public:
kw11_l(bus *const b); kw11_l(bus *const b);
virtual ~kw11_l(); virtual ~kw11_l();
void reset();
#if IS_POSIX
json_t *serialize();
static kw11_l *deserialize(const json_t *const j, bus *const b, console *const cnsl);
#endif
void begin(console *const cnsl); void begin(console *const cnsl);
void operator()(); void operator()();

37
tty.cpp
View file

@ -1,4 +1,4 @@
// (C) 2018-2023 by Folkert van Heusden // (C) 2018-2024 by Folkert van Heusden
// Released under MIT license // Released under MIT license
#include <errno.h> #include <errno.h>
@ -193,3 +193,38 @@ void tty::writeWord(const uint16_t addr, uint16_t v)
DOLOG(debug, false, "set register %o to %o", addr, v); DOLOG(debug, false, "set register %o to %o", addr, v);
registers[(addr - PDP11TTY_BASE) / 2] = v; registers[(addr - PDP11TTY_BASE) / 2] = v;
} }
#if IS_POSIX
json_t *tty::serialize()
{
json_t *j = json_object();
json_t *ja_reg = json_array();
for(size_t i=0; i<4; i++)
json_array_append(ja_reg, json_integer(registers[i]));
json_object_set(j, "registers", ja_reg);
json_t *ja_buf = json_array();
for(auto & c: chars)
json_array_append(ja_buf, json_integer(c));
json_object_set(j, "input-buffer", ja_buf);
return j;
}
tty *tty::deserialize(const json_t *const j, bus *const b, console *const cnsl)
{
tty *out = new tty(cnsl, b);
json_t *ja_reg = json_object_get(j, "registers");
for(size_t i=0; i<4; i++)
out->registers[i] = json_integer_value(json_array_get(ja_reg, i));
json_t *ja_buf = json_object_get(j, "input-buffer");
size_t buf_size = json_array_size(ja_buf);
for(size_t i=0; i<buf_size; i++)
out->chars.push_back(json_integer_value(json_array_get(ja_buf, i)));
return out;
}
#endif

8
tty.h
View file

@ -1,4 +1,4 @@
// (C) 2018-2023 by Folkert van Heusden // (C) 2018-2024 by Folkert van Heusden
// Released under MIT license // Released under MIT license
#pragma once #pragma once
@ -13,6 +13,7 @@
#include "bus.h" #include "bus.h"
#include "console.h" #include "console.h"
#include "gen.h"
#define PDP11TTY_TKS 0177560 // reader status #define PDP11TTY_TKS 0177560 // reader status
@ -50,6 +51,11 @@ public:
tty(console *const c, bus *const b); tty(console *const c, bus *const b);
virtual ~tty(); virtual ~tty();
#if IS_POSIX
json_t *serialize();
static tty *deserialize(const json_t *const j, bus *const b, console *const cnsl);
#endif
void reset(); void reset();
uint8_t readByte(const uint16_t addr); uint8_t readByte(const uint16_t addr);