Merge branch 'master' into psram

This commit is contained in:
folkert van heusden 2024-04-26 20:32:10 +02:00
commit b9f8418ea0
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
45 changed files with 1253 additions and 441 deletions

View file

@ -1,4 +1,4 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <Adafruit_NeoPixel.h>
@ -13,8 +13,8 @@
#define NEOPIXELS_PIN 25
console_esp32::console_esp32(std::atomic_uint32_t *const stop_event, bus *const b, std::vector<Stream *> & io_ports, const int t_width, const int t_height) :
console(stop_event, b, t_width, t_height),
console_esp32::console_esp32(std::atomic_uint32_t *const stop_event, std::vector<Stream *> & io_ports, const int t_width, const int t_height) :
console(stop_event, t_width, t_height),
io_ports(io_ports)
{
}

View file

@ -1,4 +1,4 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <Arduino.h>
@ -18,7 +18,7 @@ protected:
void put_char_ll(const char c) override;
public:
console_esp32(std::atomic_uint32_t *const stop_event, bus *const b, std::vector<Stream *> & io_ports, const int t_width, const int t_height);
console_esp32(std::atomic_uint32_t *const stop_event, std::vector<Stream *> & io_ports, const int t_width, const int t_height);
virtual ~console_esp32();
void put_string_lf(const std::string & what) override;

View file

@ -15,8 +15,8 @@
#define COLORED 0
#define UNCOLORED 1
console_shabadge::console_shabadge(std::atomic_uint32_t *const stop_event, bus *const b, std::vector<Stream *> & io_ports) :
console_esp32(stop_event, b, io_ports, 296 / 8, 128 / 8)
console_shabadge::console_shabadge(std::atomic_uint32_t *const stop_event, std::vector<Stream *> & io_ports) :
console_esp32(stop_event, io_ports, 296 / 8, 128 / 8)
{
if (epd.Init() != 0)
Serial.println("Init of DEPG0290B01 failed");

View file

@ -22,7 +22,7 @@ private:
void put_char_ll(const char c) override;
public:
console_shabadge(std::atomic_uint32_t *const stop_event, bus *const b, std::vector<Stream *> & io_ports);
console_shabadge(std::atomic_uint32_t *const stop_event, std::vector<Stream *> & io_ports);
virtual ~console_shabadge();
void panel_update_thread() override;

View file

@ -29,7 +29,7 @@ void disk_backend_esp32::emit_error()
DOLOG(ll_error, true, "SdFat error: %d/%d", sd.sdErrorCode(), sd.sdErrorData());
}
bool disk_backend_esp32::begin()
bool disk_backend_esp32::begin(const bool dummy)
{
if (!fh->open(filename.c_str(), O_RDWR)) {
DOLOG(ll_error, true, "rk05: cannot open \"%s\"", filename.c_str());
@ -41,7 +41,7 @@ bool disk_backend_esp32::begin()
return true;
}
bool disk_backend_esp32::read(const off_t offset, const size_t n, uint8_t *const target)
bool disk_backend_esp32::read(const off_t offset, const size_t n, uint8_t *const target, const size_t sector_size)
{
DOLOG(debug, false, "disk_backend_esp32::read: read %zu bytes from offset %zu", n, offset);
@ -77,7 +77,7 @@ bool disk_backend_esp32::read(const off_t offset, const size_t n, uint8_t *const
return true;
}
bool disk_backend_esp32::write(const off_t offset, const size_t n, const uint8_t *const from)
bool disk_backend_esp32::write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size)
{
DOLOG(debug, false, "disk_backend_esp32::write: write %zu bytes to offset %zu", n, offset);

View file

@ -23,9 +23,11 @@ public:
disk_backend_esp32(const std::string & filename);
virtual ~disk_backend_esp32();
bool begin() override;
std::string get_identifier() const { return filename; }
bool read(const off_t offset, const size_t n, uint8_t *const target) override;
bool begin(const bool dummy) override;
bool write(const off_t offset, const size_t n, const uint8_t *const from) override;
bool read(const off_t offset, const size_t n, uint8_t *const target, const size_t sector_size) override;
bool write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size) override;
};

View file

@ -38,7 +38,6 @@
#include "esp32.h"
#endif
#include "gen.h"
#include "kw11-l.h"
#include "loaders.h"
#include "memory.h"
#include "tty.h"
@ -217,7 +216,7 @@ void setup() {
while(!Serial)
delay(100);
Serial.println(F("This PDP-11 emulator is called \"kek\" (reason for that is forgotten) and was written by Folkert van Heusden."));
Serial.println(F("PDP11 emulator, by Folkert van Heusden"));
Serial.print(F("GIT hash: "));
Serial.println(version_str);
Serial.println(F("Build on: " __DATE__ " " __TIME__));
@ -291,13 +290,11 @@ void setup() {
std::vector<Stream *> serial_ports { &Serial_RS232, &Serial };
#if defined(SHA2017)
cnsl = new console_shabadge(&stop_event, b, serial_ports);
cnsl = new console_shabadge(&stop_event, serial_ports);
#elif defined(ESP32) || defined(BUILD_FOR_RP2040)
cnsl = new console_esp32(&stop_event, b, serial_ports, 80, 25);
cnsl = new console_esp32(&stop_event, serial_ports, 80, 25);
#endif
Serial.println(F("Start line-frequency interrupt"));
kw11_l *lf = new kw11_l(b, cnsl);
cnsl->set_bus(b);
running = cnsl->get_running_flag();

1
ESP32/mmu.cpp Symbolic link
View file

@ -0,0 +1 @@
../mmu.cpp

1
ESP32/mmu.h Symbolic link
View file

@ -0,0 +1 @@
../mmu.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
[platformio]

173
bus.cpp
View file

@ -8,6 +8,7 @@
#include "bus.h"
#include "gen.h"
#include "cpu.h"
#include "kw11-l.h"
#include "log.h"
#include "memory.h"
#include "mmu.h"
@ -22,19 +23,16 @@
bus::bus()
{
m = new memory(n_pages * 8192l);
mmu_ = new mmu();
reset();
kw11_l_ = new kw11_l(this);
#if defined(BUILD_FOR_RP2040)
xSemaphoreGive(lf_csr_lock); // initialize
#endif
reset();
}
bus::~bus()
{
delete kw11_l_;
delete c;
delete tm11;
delete rk05_;
@ -44,6 +42,82 @@ bus::~bus()
delete m;
}
#if IS_POSIX
json_t *bus::serialize() const
{
json_t *j_out = json_object();
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());
if (mmu_)
json_object_set(j_out, "mmu", mmu_->serialize());
if (c)
json_object_set(j_out, "cpu", c->serialize());
if (rl02_)
json_object_set(j_out, "rl02", rl02_->serialize());
// TODO: rk05, tm11
return j_out;
}
bus *bus::deserialize(const json_t *const j, console *const cnsl, std::atomic_uint32_t *const event)
{
bus *b = new bus();
json_t *temp = nullptr;
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_);
}
temp = json_object_get(j, "mmu");
if (temp) {
mmu *mmu_ = mmu::deserialize(temp);
b->add_mmu(mmu_);
}
temp = json_object_get(j, "cpu");
if (temp) {
cpu *cpu_ = cpu::deserialize(temp, b, event);
b->add_cpu(cpu_);
}
temp = json_object_get(j, "rl02");
if (temp) {
rl02 *rl02_ = rl02::deserialize(temp, b);
b->add_rl02(rl02_);
}
// TODO: rk05, tm11
return b;
}
#endif
void bus::set_memory_size(const int n_pages)
{
this->n_pages = n_pages;
@ -58,10 +132,10 @@ void bus::set_memory_size(const int n_pages)
void bus::reset()
{
if (m)
m->reset();
if (mmu_)
mmu_->reset();
if (c)
c->reset();
if (tm11)
@ -72,6 +146,26 @@ void bus::reset()
rl02_->reset();
if (tty_)
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)
{
delete this->m;
this->m = m;
}
void bus::add_mmu(mmu *const mmu_)
{
delete this->mmu_;
this->mmu_ = mmu_;
}
void bus::add_cpu(cpu *const c)
@ -83,7 +177,7 @@ void bus::add_cpu(cpu *const c)
void bus::add_tm11(tm_11 *const tm11)
{
delete this->tm11;
this->tm11 = tm11;
this->tm11= tm11;
}
void bus::add_rk05(rk05 *const rk05_)
@ -220,22 +314,8 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm
return temp;
}
if (a == ADDR_LFC) { // line frequency clock and status register
#if defined(BUILD_FOR_RP2040)
xSemaphoreTake(lf_csr_lock, portMAX_DELAY);
#else
std::unique_lock<std::mutex> lck(lf_csr_lock);
#endif
uint16_t temp = lf_csr;
if (!peek_only) DOLOG(debug, false, "READ-I/O line frequency clock: %o", temp);
#if defined(BUILD_FOR_RP2040)
xSemaphoreGive(lf_csr_lock);
#endif
return temp;
}
if (a == ADDR_LFC) // line frequency clock and status register
return kw11_l_->readWord(a);
if (a == ADDR_LP11CSR) { // printer, CSR register, LP11
uint16_t temp = 0x80;
@ -848,17 +928,8 @@ write_rc_t bus::write(const uint16_t addr_in, const word_mode_t word_mode, uint1
}
if (a == ADDR_LFC) { // line frequency clock and status register
#if defined(BUILD_FOR_RP2040)
xSemaphoreTake(lf_csr_lock, portMAX_DELAY);
#else
std::unique_lock<std::mutex> lck(lf_csr_lock);
#endif
kw11_l_->writeWord(a, value);
DOLOG(debug, false, "WRITE-I/O set line frequency clock/status register: %06o", value);
lf_csr = value;
#if defined(BUILD_FOR_RP2040)
xSemaphoreGive(lf_csr_lock);
#endif
return { false };
}
@ -1023,35 +1094,3 @@ void bus::writeUnibusByte(const uint32_t a, const uint8_t v)
DOLOG(debug, false, "writeUnibusByte[%08o]=%03o", a, v);
m->writeByte(a, v);
}
void bus::set_lf_crs_b7()
{
#if defined(BUILD_FOR_RP2040)
xSemaphoreTake(lf_csr_lock, portMAX_DELAY);
#else
std::unique_lock<std::mutex> lck(lf_csr_lock);
#endif
lf_csr |= 128;
#if defined(BUILD_FOR_RP2040)
xSemaphoreGive(lf_csr_lock);
#endif
}
uint8_t bus::get_lf_crs()
{
#if defined(BUILD_FOR_RP2040)
xSemaphoreTake(lf_csr_lock, portMAX_DELAY);
#else
std::unique_lock<std::mutex> lck(lf_csr_lock);
#endif
uint8_t rc = lf_csr;
#if defined(BUILD_FOR_RP2040)
xSemaphoreGive(lf_csr_lock);
#endif
return rc;
}

51
bus.h
View file

@ -18,14 +18,6 @@
#include "rp2040.h"
#endif
#if defined(ESP32) || defined(BUILD_FOR_RP2040)
// ESP32 goes in a crash-loop when allocating 128kB
// see also https://github.com/espressif/esp-idf/issues/1934
#define DEFAULT_N_PAGES 12
#else
#define DEFAULT_N_PAGES 31
#endif
#define ADDR_MMR0 0177572
#define ADDR_MMR1 0177574
#define ADDR_MMR2 0177576
@ -53,7 +45,9 @@
#define ADDR_CCR 0177746
#define ADDR_SYSTEM_ID 0177764
class console;
class cpu;
class kw11_l;
class memory;
class tty;
@ -80,19 +74,13 @@ private:
rk05 *rk05_ { nullptr };
rl02 *rl02_ { nullptr };
tty *tty_ { nullptr };
kw11_l *kw11_l_ { nullptr };
mmu *mmu_ { nullptr };
int n_pages { DEFAULT_N_PAGES };
memory *m { nullptr };
#if defined(BUILD_FOR_RP2040)
SemaphoreHandle_t lf_csr_lock { xSemaphoreCreateBinary() };
#else
std::mutex lf_csr_lock;
#endif
uint16_t lf_csr { 0 };
uint16_t microprogram_break_register { 0 };
uint16_t console_switches { 0 };
@ -102,36 +90,41 @@ public:
bus();
~bus();
#if IS_POSIX
json_t *serialize() const;
static bus *deserialize(const json_t *const j, console *const cnsl, std::atomic_uint32_t *const event);
#endif
void reset();
void init(); // invoked by 'RESET' command
void set_console_switches(const uint16_t new_state) { console_switches = new_state; }
void set_console_switch(const int bit, const bool state) { console_switches &= ~(1 << bit); console_switches |= state << bit; }
uint16_t get_console_switches() { return console_switches; }
void set_debug_mode() { console_switches |= 128; }
uint16_t get_console_leds() { return console_leds; }
int get_memory_size() const { return n_pages; }
void set_memory_size(const int n_pages);
void mmudebug(const uint16_t a);
uint16_t get_console_leds() { return console_leds; }
void add_cpu (cpu *const c);
void add_tm11(tm_11 *const tm11);
void add_rk05(rk05 *const rk05_);
void add_rl02(rl02 *const rl02_);
void add_tty (tty *const tty_);
void add_ram (memory *const m );
void add_cpu (cpu *const c );
void add_mmu (mmu *const mmu_ );
void add_tm11 (tm_11 *const tm11 );
void add_rk05 (rk05 *const rk05_ );
void add_rl02 (rl02 *const rl02_ );
void add_tty (tty *const tty_ );
void add_KW11_L(kw11_l *const kw11_l_);
memory *getRAM() { return m; }
cpu *getCpu() { return c; }
kw11_l *getKW11_L() { return kw11_l_; }
tty *getTty() { return tty_; }
mmu *getMMU() { return mmu_; }
void init(); // invoked by 'RESET' command
void set_lf_crs_b7();
uint8_t get_lf_crs();
rk05 *getRK05() { return rk05_; }
rl02 *getRL02() { return rl02_; }
uint16_t read (const uint16_t a, const word_mode_t word_mode, const rm_selection_t mode_selection, const bool peek_only=false, const d_i_space_t s = i_space);
uint16_t readByte(const uint16_t a) { return read(a, wm_byte, rm_cur); }

View file

@ -25,9 +25,8 @@ void thread_wrapper_console(void *p)
}
#endif
console::console(std::atomic_uint32_t *const stop_event, bus *const b, const int t_width, const int t_height) :
console::console(std::atomic_uint32_t *const stop_event, const int t_width, const int t_height) :
stop_event(stop_event),
b(b),
t_width(t_width),
t_height(t_height)
{
@ -49,6 +48,8 @@ console::~console()
void console::start_thread()
{
assert(b);
stop_thread_flag = false;
#if defined(BUILD_FOR_RP2040)

View file

@ -31,7 +31,7 @@ private:
protected:
std::atomic_uint32_t *const stop_event { nullptr };
bus *const b { nullptr };
bus *b { nullptr };
#if !defined(BUILD_FOR_RP2040)
std::thread *th { nullptr };
#endif
@ -57,9 +57,11 @@ protected:
virtual void put_char_ll(const char c) = 0;
public:
console(std::atomic_uint32_t *const stop_event, bus *const b, const int t_width = 80, const int t_height = 25);
console(std::atomic_uint32_t *const stop_event, const int t_width = 80, const int t_height = 25);
virtual ~console();
void set_bus(bus *const b) { this->b = b; }
void start_thread();
void stop_thread();

View file

@ -13,8 +13,7 @@
#include "utils.h"
console_ncurses::console_ncurses(std::atomic_uint32_t *const stop_event, bus *const b) :
console(stop_event, b)
console_ncurses::console_ncurses(std::atomic_uint32_t *const stop_event): console(stop_event)
{
init_ncurses(true);

View file

@ -29,7 +29,7 @@ protected:
void put_char_ll(const char c) override;
public:
console_ncurses(std::atomic_uint32_t *const stop_event, bus *const b);
console_ncurses(std::atomic_uint32_t *const stop_event);
virtual ~console_ncurses();
void put_string_lf(const std::string & what) override;

View file

@ -16,8 +16,7 @@
#include "error.h"
console_posix::console_posix(std::atomic_uint32_t *const stop_event, bus *const b) :
console(stop_event, b)
console_posix::console_posix(std::atomic_uint32_t *const stop_event): console(stop_event)
{
#if !defined(_WIN32)
if (tcgetattr(STDIN_FILENO, &org_tty_opts) == -1)

View file

@ -21,7 +21,7 @@ protected:
void put_char_ll(const char c) override;
public:
console_posix(std::atomic_uint32_t *const stop_event, bus *const b);
console_posix(std::atomic_uint32_t *const stop_event);
virtual ~console_posix();
void resize_terminal() override;

87
cpu.cpp
View file

@ -2395,3 +2395,90 @@ void cpu::step()
DOLOG(debug, false, "bus-trap during execution of command (%d)", exception_nr);
}
}
#if IS_POSIX
json_t *cpu::serialize()
{
json_t *j = json_object();
for(int set=0; set<2; set++) {
for(int regnr=0; regnr<6; regnr++)
json_object_set(j, format("register-%d-%d", set, regnr).c_str(), json_integer(regs0_5[set][regnr]));
}
for(int spnr=0; spnr<4; spnr++)
json_object_set(j, format("sp-%d", spnr).c_str(), json_integer(sp[spnr]));
json_object_set(j, "pc", json_integer(pc));
json_object_set(j, "instruction_start", json_integer(instruction_start));
json_object_set(j, "psw", json_integer(psw));
json_object_set(j, "fpsr", json_integer(fpsr));
json_object_set(j, "stackLimitRegister", json_integer(stackLimitRegister));
json_object_set(j, "processing_trap_depth", json_integer(processing_trap_depth));
json_object_set(j, "instruction_count", json_integer(instruction_count));
json_object_set(j, "running_since", json_integer(running_since));
json_object_set(j, "wait_time", json_integer(wait_time));
json_object_set(j, "it_is_a_trap", json_boolean(it_is_a_trap));
json_object_set(j, "debug_mode", json_boolean(debug_mode));
if (trap_delay.has_value())
json_object_set(j, "trap_delay", json_integer(trap_delay.value()));
json_t *j_queued_interrupts = json_object();
for(auto & il: queued_interrupts) {
json_t *ja_qi_level = json_array();
for(auto & v: il.second)
json_array_append(ja_qi_level, json_integer(v));
json_object_set(j_queued_interrupts, format("%d", il.first).c_str(), ja_qi_level);
}
json_object_set(j, "queued_interrupts", j_queued_interrupts);
json_object_set(j, "any_queued_interrupts", json_boolean(any_queued_interrupts));
return j;
}
cpu *cpu::deserialize(const json_t *const j, bus *const b, std::atomic_uint32_t *const event)
{
cpu *c = new cpu(b, event);
for(int set=0; set<2; set++) {
for(int regnr=0; regnr<6; regnr++)
c->regs0_5[set][regnr] = json_integer_value(json_object_get(j, format("register-%d-%d", set, regnr).c_str()));
}
for(int spnr=0; spnr<4; spnr++)
c->sp[spnr] = json_integer_value(json_object_get(j, format("sp-%d", spnr).c_str()));
c->pc = json_integer_value(json_object_get(j, "pc"));
c->instruction_start = json_integer_value(json_object_get(j, "instruction_start"));
c->psw = json_integer_value(json_object_get(j, "psw"));
c->fpsr = json_integer_value(json_object_get(j, "fpsr"));
c->stackLimitRegister = json_integer_value(json_object_get(j, "stackLimitRegister"));
c->processing_trap_depth = json_integer_value(json_object_get(j, "processing_trap_depth"));
c->instruction_count = json_integer_value(json_object_get(j, "instruction_count"));
c->running_since = get_us();
c->wait_time = 0;
c->it_is_a_trap = json_boolean_value(json_object_get(j, "it_is_a_trap"));
c->debug_mode = json_boolean_value(json_object_get(j, "debug_mode"));
json_t *temp = json_object_get(j, "trap_delay");
if (temp)
c->trap_delay = json_integer_value(temp);
else
c->trap_delay.reset();
c->any_queued_interrupts = json_boolean_value(json_object_get(j, "any_queued_interrupts"));
c->init_interrupt_queue();
json_t *j_queued_interrupts = json_object_get(j, "queued_interrupts");
for(int level=0; level<8; level++) {
auto it = c->queued_interrupts.find(level);
json_t *ja_qi_level = json_object_get(j_queued_interrupts, format("%d", level).c_str());
for(size_t i=0; i<json_array_size(ja_qi_level); i++)
it->second.insert(json_integer_value(json_array_get(ja_qi_level, i)));
}
return c;
}
#endif

8
cpu.h
View file

@ -14,6 +14,7 @@
#include "breakpoint.h"
#include "bus.h"
#include "gen.h"
constexpr const int initial_trap_delay = 8;
@ -61,6 +62,7 @@ private:
// level, vector
std::map<uint8_t, std::set<uint8_t> > queued_interrupts;
std::atomic_bool any_queued_interrupts { false };
#if defined(BUILD_FOR_RP2040)
SemaphoreHandle_t qi_lock { xSemaphoreCreateBinary() };
QueueHandle_t qi_q { xQueueCreate(16, 1) };
@ -68,7 +70,6 @@ private:
std::mutex qi_lock;
std::condition_variable qi_cv;
#endif
std::atomic_bool any_queued_interrupts { false };
std::map<int, breakpoint *> breakpoints;
int bp_nr { 0 };
@ -111,6 +112,11 @@ public:
explicit cpu(bus *const b, std::atomic_uint32_t *const event);
~cpu();
#if IS_POSIX
json_t *serialize();
static cpu *deserialize(const json_t *const j, bus *const b, std::atomic_uint32_t *const event);
#endif
std::optional<std::string> check_breakpoint();
int set_breakpoint(breakpoint *const bp);
bool remove_breakpoint(const int bp_id);

View file

@ -2,7 +2,8 @@
// Released under MIT license
#include <optional>
#ifdef linux
#include "gen.h"
#if IS_POSIX
#include <dirent.h>
#include <jansson.h>
#include <sys/stat.h>
@ -18,13 +19,12 @@
#include "console.h"
#include "cpu.h"
#include "disk_backend.h"
#ifdef linux
#if IS_POSIX
#include "disk_backend_file.h"
#else
#include "disk_backend_esp32.h"
#endif
#include "disk_backend_nbd.h"
#include "gen.h"
#include "loaders.h"
#include "log.h"
#include "tty.h"
@ -38,7 +38,7 @@
#include "rp2040.h"
#endif
void setBootLoader(bus *const b);
void set_boot_loader(bus *const b);
void configure_disk(console *const c);
@ -67,7 +67,7 @@ typedef enum { BE_NETWORK, BE_SD } disk_backend_t;
#if !defined(BUILD_FOR_RP2040)
std::optional<std::tuple<std::vector<disk_backend *>, std::vector<disk_backend *>, std::string> > load_disk_configuration(console *const c)
{
#ifdef linux
#if IS_POSIX
json_error_t error;
json_t *json = json_load_file("." NET_DISK_CFG_FILE, JSON_REJECT_DUPLICATES, &error);
if (!json) {
@ -125,7 +125,7 @@ std::optional<std::tuple<std::vector<disk_backend *>, std::vector<disk_backend *
disk_backend *d = new disk_backend_nbd(nbd_host.c_str(), nbd_port);
if (d->begin() == false) {
if (d->begin(false) == false) {
c->put_string_lf("Cannot initialize NBD client from configuration file");
delete d;
return { };
@ -147,7 +147,7 @@ std::optional<std::tuple<std::vector<disk_backend *>, std::vector<disk_backend *
bool save_disk_configuration(const std::string & nbd_host, const int nbd_port, const std::optional<std::string> & tape_file, const disk_type_t dt, console *const cnsl)
{
#ifdef linux
#if IS_POSIX
json_t *json = json_object();
json_object_set(json, "NBD-host", json_string(nbd_host.c_str()));
@ -196,108 +196,37 @@ bool save_disk_configuration(const std::string & nbd_host, const int nbd_port, c
}
#endif
std::optional<disk_backend_t> select_disk_backend(console *const c)
{
#if defined(BUILD_FOR_RP2040)
return BE_SD;
#elif linux
c->put_string("1. network (NBD), 2. local filesystem, 9. abort");
#else
c->put_string("1. network (NBD), 2. local SD card, 9. abort");
#endif
int ch = -1;
while(ch == -1 && ch != '1' && ch != '2' && ch != '9') {
auto temp = c->wait_char(500);
if (temp.has_value())
ch = temp.value();
}
c->put_string_lf(format("%c", ch));
if (ch == '1')
return BE_NETWORK;
if (ch == '2')
return BE_SD;
return { };
}
std::optional<disk_type_t> select_disk_type(console *const c)
{
c->put_string("1. RK05, 2. RL02, 3. tape/BIC, 9. abort");
int ch = -1;
while(ch == -1 && ch != '1' && ch != '2' && ch != '3' && ch != '9') {
auto temp = c->wait_char(500);
if (temp.has_value())
ch = temp.value();
}
c->put_string_lf(format("%c", ch));
if (ch == '1')
return DT_RK05;
if (ch == '2')
return DT_RL02;
if (ch == '3')
return DT_TAPE;
return { };
}
#if !defined(BUILD_FOR_RP2040)
std::optional<std::tuple<std::vector<disk_backend *>, std::vector<disk_backend *>, std::string> > select_nbd_server(console *const c)
std::optional<disk_backend *> select_nbd_server(console *const cnsl)
{
c->flush_input();
cnsl->flush_input();
std::string hostname = c->read_line("Enter hostname (or empty to abort): ");
std::string hostname = cnsl->read_line("Enter hostname (or empty to abort): ");
if (hostname.empty())
return { };
std::string port_str = c->read_line("Enter port number (or empty to abort): ");
std::string port_str = cnsl->read_line("Enter port number (or empty to abort): ");
if (port_str.empty())
return { };
auto disk_type = select_disk_type(c);
if (disk_type.has_value() == false)
return { };
disk_backend *d = new disk_backend_nbd(hostname, atoi(port_str.c_str()));
if (d->begin() == false) {
c->put_string_lf("Cannot initialize NBD client");
if (d->begin(false) == false) {
cnsl->put_string_lf("Cannot initialize NBD client");
delete d;
return { };
}
if (save_disk_configuration(hostname, atoi(port_str.c_str()), { }, disk_type.value(), c))
c->put_string_lf("NBD disk configuration saved");
else
c->put_string_lf("NBD disk configuration NOT saved");
if (disk_type.value() == DT_RK05)
return { { { d }, { }, "" } };
if (disk_type.value() == DT_RL02)
return { { { }, { d }, "" } };
return { };
return d;
}
#endif
// RK05, RL02 files
std::optional<std::tuple<std::vector<disk_backend *>, std::vector<disk_backend *>, std::string> > select_disk_files(console *const c)
// disk image files
std::optional<disk_backend *> select_disk_file(console *const c)
{
#ifdef linux
#if IS_POSIX
c->put_string_lf("Files in current directory: ");
#else
c->debug("MISO: %d", int(MISO));
@ -367,17 +296,12 @@ std::optional<std::tuple<std::vector<disk_backend *>, std::vector<disk_backend *
if (selected_file.empty())
return { };
auto disk_type = select_disk_type(c);
if (disk_type.has_value() == false)
return { };
c->put_string("Opening file: ");
c->put_string_lf(selected_file.c_str());
bool can_open_file = false;
#ifdef linux
#if IS_POSIX
struct stat st { };
can_open_file = stat(selected_file.c_str(), &st) == 0;
#else
@ -388,16 +312,13 @@ std::optional<std::tuple<std::vector<disk_backend *>, std::vector<disk_backend *
#endif
if (can_open_file) {
if (disk_type.value() == DT_TAPE)
return { { { }, { }, selected_file } };
#ifdef linux
#if IS_POSIX
disk_backend *temp = new disk_backend_file(selected_file);
#else
disk_backend *temp = new disk_backend_esp32(selected_file);
#endif
if (!temp->begin()) {
if (!temp->begin(false)) {
c->put_string("Cannot use: ");
c->put_string_lf(selected_file.c_str());
@ -406,63 +327,136 @@ std::optional<std::tuple<std::vector<disk_backend *>, std::vector<disk_backend *
continue;
}
if (disk_type.value() == DT_RK05)
return { { { temp }, { }, "" } };
if (disk_type.value() == DT_RL02)
return { { { }, { temp }, "" } };
return { temp };
}
c->put_string_lf("open failed");
}
return { };
}
void set_disk_configuration(bus *const b, console *const cnsl, std::tuple<std::vector<disk_backend *>, std::vector<disk_backend *>, std::string> & disk_files)
int wait_for_key(const std::string & title, console *const cnsl, const std::vector<char> & allowed)
{
if (std::get<0>(disk_files).empty() == false)
b->add_rk05(new rk05(std::get<0>(disk_files), b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()));
cnsl->put_string_lf(title);
if (std::get<1>(disk_files).empty() == false)
b->add_rl02(new rl02(std::get<1>(disk_files), b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()));
cnsl->put_string("> ");
if (std::get<2>(disk_files).empty() == false) {
auto addr = loadTape(b, std::get<2>(disk_files));
int ch = -1;
while(ch == -1) {
auto temp = cnsl->wait_char(500);
if (addr.has_value())
b->getCpu()->setPC(addr.value());
if (temp.has_value()) {
for(auto & a: allowed) {
if (a == temp.value()) {
ch = temp.value();
break;
}
}
}
}
if (std::get<0>(disk_files).empty() == false)
setBootLoader(b, BL_RK05);
else if (std::get<1>(disk_files).empty() == false)
setBootLoader(b, BL_RL02);
cnsl->put_string_lf(format("%c", ch));
return ch;
}
std::optional<disk_backend *> select_disk_backend(console *const cnsl)
{
#if defined(BUILD_FOR_RP2040)
return select_disk_file(cnsl);
#else
int ch = wait_for_key("1. local disk, 2. network disk (NBD), 9. abort", cnsl, { '1', '2', '9' });
if (ch == '9')
return { };
if (ch == '1')
return select_disk_file(cnsl);
if (ch == '2')
return select_nbd_server(cnsl);
return { };
#endif
}
void configure_disk(bus *const b, console *const cnsl)
{
// TODO tape
int ch = wait_for_key("1. RK05, 2. RL02, 9. abort", cnsl, { '1', '2', '3', '9' });
bootloader_t bl = BL_NONE;
disk_device *dd = nullptr;
if (ch == '1') {
dd = b->getRK05();
bl = BL_RK05;
}
else if (ch == '2') {
dd = b->getRL02();
bl = BL_RL02;
}
else if (ch == '9') {
return;
}
for(;;) {
cnsl->put_string_lf("Load disk");
std::vector<char> keys_allowed { '1', '2', '9' };
auto backend = select_disk_backend(cnsl);
auto cartridge_slots = dd->access_disk_backends();
int slot_key = 'A';
for(auto & slot: *cartridge_slots) {
cnsl->put_string_lf(format(" %c. %s", slot_key, slot ? slot->get_identifier().c_str() : "-"));
keys_allowed.push_back(slot_key);
slot_key++;
}
if (backend.has_value() == false)
int ch = wait_for_key("Select cartridge to setup, 1. to add a cartridge, 2. to load a bootloader or 9. to exit", cnsl, keys_allowed);
if (ch == '9')
break;
std::optional<std::tuple<std::vector<disk_backend *>, std::vector<disk_backend *>, std::string> > files;
if (ch == '1') {
auto image_file = select_disk_backend(cnsl);
#if !defined(BUILD_FOR_RP2040)
if (backend == BE_NETWORK)
files = select_nbd_server(cnsl);
else // if (backend == BE_SD)
#endif
files = select_disk_files(cnsl);
if (image_file.has_value()) {
cartridge_slots->push_back(image_file.value());
if (files.has_value() == false)
cnsl->put_string_lf("Cartridge loaded");
}
}
else if (ch == '2') {
set_boot_loader(b, bl);
cnsl->put_string_lf("Bootloader loaded");
}
else {
int slot = ch - 'A';
for(;;) {
int ch = wait_for_key("Select cartridge action: 1. load, 2. unload, 9. exit", cnsl, { '1', '2', '9' });
if (ch == '9')
break;
set_disk_configuration(b, cnsl, files.value());
if (ch == '1') {
auto image_file = select_disk_backend(cnsl);
break;
if (image_file.has_value()) {
delete cartridge_slots->at(slot);
cartridge_slots->at(slot) = image_file.value();
cnsl->put_string_lf("Cartridge loaded");
}
}
else if (ch == '2') {
if (cartridge_slots->at(slot)) {
delete cartridge_slots->at(slot);
cartridge_slots->at(slot) = nullptr;
cnsl->put_string_lf("Cartridge unloaded");
}
}
}
}
}
}
@ -722,6 +716,27 @@ void show_queued_interrupts(console *const cnsl, cpu *const c)
}
}
#if IS_POSIX
void serialize_state(console *const cnsl, const bus *const b, const std::string & filename)
{
json_t *j = b->serialize();
bool ok = false;
FILE *fh = fopen(filename.c_str(), "w");
if (fh) {
if (json_dumpf(j, fh, JSON_INDENT(4)) == 0)
ok = true;
fclose(fh);
}
json_decref(j);
cnsl->put_string_lf(format("Serialize to %s: %s", filename.c_str(), ok ? "OK" : "failed"));
}
#endif
void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const stop_event, const bool tracing_in)
{
int32_t trace_start_addr = -1;
@ -1005,7 +1020,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
continue;
}
else if (parts[0] == "bl" && parts.size() == 2) {
setBootLoader(b, parts.at(1) == "rk05" ? BL_RK05 : BL_RL02);
set_boot_loader(b, parts.at(1) == "rk05" ? BL_RK05 : BL_RL02);
cnsl->put_string_lf("Bootloader set");
continue;
@ -1069,6 +1084,12 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
continue;
}
#if IS_POSIX
else if (parts[0] == "ser" && parts.size() == 2) {
serialize_state(cnsl, b, parts.at(1));
continue;
}
#endif
else if (parts[0] == "setsl" && parts.size() == 3) {
setloghost(parts.at(1).c_str(), parse_ll(parts[2]));
@ -1130,6 +1151,10 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
"stats - show run statistics",
"ramsize - set ram size (page count (8 kB))",
"bl - set bootload (rl02 or rk05)",
#if IS_POSIX
"ser - serialize state to a file",
// "dser - deserialize state from a file",
#endif
#if defined(ESP32)
"cfgnet - configure network (e.g. WiFi)",
"startnet - start network",

View file

@ -1,7 +1,14 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <cassert>
#include "disk_backend.h"
#if IS_POSIX
#include "disk_backend_file.h"
#include "disk_backend_nbd.h"
#endif
disk_backend::disk_backend()
{
@ -10,3 +17,102 @@ disk_backend::disk_backend()
disk_backend::~disk_backend()
{
}
#if IS_POSIX
void disk_backend::store_object_in_overlay(const off_t id, const std::vector<uint8_t> & data)
{
overlay.insert_or_assign(id, data);
}
std::optional<std::vector<uint8_t> > disk_backend::get_object_from_overlay(const off_t id)
{
auto it = overlay.find(id);
if (it != overlay.end())
return it->second;
return { };
}
std::optional<std::vector<uint8_t> > disk_backend::get_from_overlay(const off_t offset, const size_t sector_size)
{
assert((offset % sector_size) == 0);
if (use_overlay)
return get_object_from_overlay(offset / sector_size);
return { };
}
bool disk_backend::store_mem_range_in_overlay(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size)
{
assert((offset % sector_size) == 0);
assert((n % sector_size) == 0);
if (use_overlay) {
for(size_t o=0; o<n; o += sector_size)
store_object_in_overlay((offset + o) / sector_size, std::vector<uint8_t>(from + o, from + o + sector_size));
return true;
}
return false;
}
json_t *disk_backend::serialize_overlay() const
{
json_t *out = json_object();
for(auto & id: overlay) {
json_t *j_data = json_array();
for(size_t i=0; i<id.second.size(); i++)
json_array_append(j_data, json_integer(id.second.at(i)));
json_object_set(out, format("%u", id.first).c_str(), j_data);
}
return out;
}
void disk_backend::deserialize_overlay(const json_t *const j)
{
json_t *input = json_object_get(j, "overlay");
if (!input) // we can have state-dumps without overlay
return;
const char *key = nullptr;
json_t *value = nullptr;
json_object_foreach(input, key, value) {
uint32_t id = std::atoi(key);
std::vector<uint8_t> data;
for(size_t i=0; i<json_array_size(value); i++)
data.push_back(json_integer_value(json_array_get(value, i)));
store_object_in_overlay(id, data);
}
}
disk_backend *disk_backend::deserialize(const json_t *const j)
{
std::string type = json_string_value(json_object_get(j, "disk-backend-type"));
disk_backend *d = nullptr;
if (type == "file")
d = disk_backend_file::deserialize(j);
else if (type == "nbd")
d = disk_backend_nbd::deserialize(j);
// should not be reached
assert(d);
d->deserialize_overlay(j);
// assume we want snapshots (again?)
d->begin(true);
return d;
}
#endif

View file

@ -1,21 +1,48 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#pragma once
#include <map>
#include <optional>
#include <stdint.h>
#include <string>
#include <vector>
#include <sys/types.h>
#include "gen.h"
class disk_backend
{
protected:
#if IS_POSIX
bool use_overlay { false };
std::map<off_t, std::vector<uint8_t> > overlay;
void store_object_in_overlay(const off_t id, const std::vector<uint8_t> & data);
bool store_mem_range_in_overlay(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size);
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);
json_t *serialize_overlay() const;
void deserialize_overlay(const json_t *const j);
#endif
public:
disk_backend();
virtual ~disk_backend();
virtual bool begin() = 0;
#if IS_POSIX
virtual json_t *serialize() const = 0;
static disk_backend *deserialize(const json_t *const j);
#endif
virtual bool read(const off_t offset, const size_t n, uint8_t *const target) = 0;
virtual std::string get_identifier() const = 0;
virtual bool write(const off_t offset, const size_t n, const uint8_t *const from) = 0;
virtual bool begin(const bool disk_snapshots) = 0;
virtual bool read(const off_t offset, const size_t n, uint8_t *const target, const size_t sector_size) = 0;
virtual bool write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size) = 0;
};

View file

@ -1,11 +1,13 @@
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <cassert>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "disk_backend_file.h"
#include "gen.h"
#include "log.h"
@ -19,8 +21,32 @@ disk_backend_file::~disk_backend_file()
close(fd);
}
bool disk_backend_file::begin()
#if IS_POSIX
json_t *disk_backend_file::serialize() const
{
json_t *j = json_object();
json_object_set(j, "disk-backend-type", json_string("file"));
json_object_set(j, "overlay", serialize_overlay());
// TODO store checksum of backend
json_object_set(j, "filename", json_string(filename.c_str()));
return j;
}
disk_backend_file *disk_backend_file::deserialize(const json_t *const j)
{
// TODO verify checksum of backend
return new disk_backend_file(json_string_value(json_object_get(j, "filename")));
}
#endif
bool disk_backend_file::begin(const bool snapshots)
{
use_overlay = snapshots;
fd = open(filename.c_str(), O_RDWR);
if (fd == -1) {
@ -32,30 +58,48 @@ bool disk_backend_file::begin()
return true;
}
bool disk_backend_file::read(const off_t offset, const size_t n, uint8_t *const target)
bool disk_backend_file::read(const off_t offset_in, const size_t n, uint8_t *const target, const size_t sector_size)
{
DOLOG(debug, false, "disk_backend_file::read: read %zu bytes from offset %zu", n, offset);
DOLOG(debug, false, "disk_backend_file::read: read %zu bytes from offset %zu", n, offset_in);
assert((offset % sector_size) == 0);
assert((n % sector_size) == 0);
for(off_t o=0; o<off_t(n); o+=sector_size) {
off_t offset = offset_in + o;
#if IS_POSIX
auto o_rc = get_from_overlay(offset, sector_size);
if (o_rc.has_value()) {
memcpy(&target[o], o_rc.value().data(), sector_size);
continue;
}
#endif
#if defined(_WIN32) // hope for the best
if (lseek(fd, offset, SEEK_SET) == -1)
return false;
return ::read(fd, target, n) == ssize_t(n);
if (::read(fd, target, n) != ssize_t(n))
return false;
#else
ssize_t rc = pread(fd, target, n, offset);
if (rc != ssize_t(n)) {
DOLOG(warning, false, "disk_backend_file::read: read failure. expected %zu bytes, got %zd", n, rc);
return false;
}
#endif
}
return true;
#endif
}
bool disk_backend_file::write(const off_t offset, const size_t n, const uint8_t *const from)
bool disk_backend_file::write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size)
{
DOLOG(debug, false, "disk_backend_file::write: write %zu bytes to offset %zu", n, offset);
if (store_mem_range_in_overlay(offset, n, from, sector_size))
return true;
#if defined(_WIN32) // hope for the best
if (lseek(fd, offset, SEEK_SET) == -1)
return false;

View file

@ -1,4 +1,4 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <string>
@ -17,9 +17,16 @@ public:
disk_backend_file(const std::string & filename);
virtual ~disk_backend_file();
bool begin() override;
#if IS_POSIX
json_t *serialize() const override;
static disk_backend_file *deserialize(const json_t *const j);
#endif
bool read(const off_t offset, const size_t n, uint8_t *const target) override;
std::string get_identifier() const override { return filename; }
bool write(const off_t offset, const size_t n, const uint8_t *const from) override;
bool begin(const bool snapshots) override;
bool read(const off_t offset, const size_t n, uint8_t *const target, const size_t sector_size) override;
bool write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size) override;
};

View file

@ -1,6 +1,7 @@
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <cassert>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
@ -47,8 +48,35 @@ disk_backend_nbd::~disk_backend_nbd()
close(fd);
}
bool disk_backend_nbd::begin()
#if IS_POSIX
json_t *disk_backend_nbd::serialize() const
{
json_t *j = json_object();
json_object_set(j, "disk-backend-type", json_string("nbd"));
json_object_set(j, "overlay", serialize_overlay());
// TODO store checksum of backend
json_object_set(j, "host", json_string(host.c_str()));
json_object_set(j, "port", json_integer(port));
return j;
}
disk_backend_nbd *disk_backend_nbd::deserialize(const json_t *const j)
{
// TODO verify checksum of backend
return new disk_backend_nbd(json_string_value(json_object_get(j, "host")), json_integer_value(json_object_get(j, "port")));
}
#endif
bool disk_backend_nbd::begin(const bool snapshots)
{
#if IS_POSIX
use_overlay = snapshots;
#endif
if (!connect(false)) {
DOLOG(ll_error, true, "disk_backend_nbd: cannot connect to NBD server");
return false;
@ -133,14 +161,27 @@ bool disk_backend_nbd::connect(const bool retry)
return fd != -1;
}
bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const target)
bool disk_backend_nbd::read(const off_t offset_in, const size_t n, uint8_t *const target, const size_t sector_size)
{
DOLOG(debug, false, "disk_backend_nbd::read: read %zu bytes from offset %zu", n, offset);
DOLOG(debug, false, "disk_backend_nbd::read: read %zu bytes from offset %zu", n, offset_in);
if (n == 0)
return true;
do {
size_t o = 0;
off_t offset = offset_in;
while(offset < offset_in + off_t(n)) {
#if IS_POSIX
auto o_rc = get_from_overlay(offset, sector_size);
if (o_rc.has_value()) {
memcpy(&target[o], o_rc.value().data(), sector_size);
offset += sector_size;
o += sector_size;
continue;
}
#endif
if (fd == -1 && !connect(true)) {
DOLOG(warning, true, "disk_backend_nbd::read: (re-)connect");
sleep(1);
@ -158,7 +199,7 @@ bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const t
nbd_request.magic = ntohl(0x25609513);
nbd_request.type = 0; // READ
nbd_request.offset = HTONLL(uint64_t(offset));
nbd_request.length = htonl(n);
nbd_request.length = htonl(sector_size);
if (WRITE(fd, reinterpret_cast<const char *>(&nbd_request), sizeof nbd_request) != sizeof nbd_request) {
DOLOG(warning, true, "disk_backend_nbd::read: problem sending request");
@ -196,26 +237,33 @@ bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const t
return false;
}
if (READ(fd, reinterpret_cast<char *>(target), n) != ssize_t(n)) {
if (READ(fd, reinterpret_cast<char *>(target), sector_size) != ssize_t(sector_size)) {
DOLOG(warning, true, "disk_backend_nbd::read: problem receiving payload");
close(fd);
fd = -1;
sleep(1);
continue;
}
offset += sector_size;
o += sector_size;
}
while(fd == -1);
return true;
}
bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *const from)
bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size)
{
DOLOG(debug, false, "disk_backend_nbd::write: write %zu bytes to offset %zu", n, offset);
if (n == 0)
return true;
#if IS_POSIX
if (store_mem_range_in_overlay(offset, n, from, sector_size))
return true;
#endif
do {
if (!connect(true)) {
DOLOG(warning, true, "disk_backend_nbd::write: (re-)connect");
@ -229,7 +277,7 @@ bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *
uint64_t handle;
uint64_t offset;
uint32_t length;
} nbd_request { 0 };
} nbd_request { };
nbd_request.magic = ntohl(0x25609513);
nbd_request.type = 1; // WRITE

View file

@ -1,10 +1,12 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <string>
#include <sys/types.h>
#include "disk_backend.h"
#include "gen.h"
#include "utils.h"
class disk_backend_nbd : public disk_backend
@ -20,9 +22,16 @@ public:
disk_backend_nbd(const std::string & host, const unsigned port);
virtual ~disk_backend_nbd();
bool begin() override;
#if IS_POSIX
json_t *serialize() const override;
static disk_backend_nbd *deserialize(const json_t *const j);
#endif
bool read(const off_t offset, const size_t n, uint8_t *const target) override;
std::string get_identifier() const override { return format("%s:%d", host.c_str(), port); }
bool write(const off_t offset, const size_t n, const uint8_t *const from) override;
bool begin(const bool snapshots) override;
bool read(const off_t offset, const size_t n, uint8_t *const target, const size_t sector_size) override;
bool write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size) override;
};

22
disk_device.h Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include <vector>
#include "device.h"
#include "disk_backend.h"
class disk_device: public device
{
protected:
std::vector<disk_backend *> fhs;
public:
disk_device() {
}
virtual ~disk_device() {
}
std::vector<disk_backend *> * access_disk_backends() { return &fhs; }
};

18
gen.h
View file

@ -12,3 +12,21 @@ typedef enum { d_space, i_space } d_i_space_t;
typedef enum { wm_word = 0, wm_byte = 1 } word_mode_t;
typedef enum { rm_prev, rm_cur } rm_selection_t;
#if (defined(linux) || defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)))
#define IS_POSIX 1
#else
#define IS_POSIX 0
#endif
#if IS_POSIX
#include <jansson.h>
#endif
#if defined(ESP32) || defined(BUILD_FOR_RP2040)
// ESP32 goes in a crash-loop when allocating 128kB
// see also https://github.com/espressif/esp-idf/issues/1934
#define DEFAULT_N_PAGES 12
#else
#define DEFAULT_N_PAGES 31
#endif

View file

@ -25,13 +25,8 @@ void thread_wrapper_kw11(void *p)
}
#endif
kw11_l::kw11_l(bus *const b, console *const cnsl) : b(b), cnsl(cnsl)
kw11_l::kw11_l(bus *const b): b(b)
{
#if defined(ESP32) || defined(BUILD_FOR_RP2040)
xTaskCreate(&thread_wrapper_kw11, "kw11-l", 2048, this, 1, nullptr);
#else
th = new std::thread(std::ref(*this));
#endif
}
kw11_l::~kw11_l()
@ -39,12 +34,34 @@ kw11_l::~kw11_l()
stop_flag = true;
#if !defined(ESP32) && !defined(BUILD_FOR_RP2040)
if (th) {
th->join();
delete th;
}
#endif
}
void kw11_l::begin(console *const cnsl)
{
this->cnsl = cnsl;
#if defined(ESP32) || defined(BUILD_FOR_RP2040)
xTaskCreate(&thread_wrapper_kw11, "kw11-l", 2048, this, 1, nullptr);
#if defined(BUILD_FOR_RP2040)
xSemaphoreGive(lf_csr_lock); // initialize
#endif
#else
th = new std::thread(std::ref(*this));
#endif
}
void kw11_l::reset()
{
lf_csr = 0;
}
void kw11_l::operator()()
{
set_thread_name("kek:kw-11l");
@ -53,9 +70,9 @@ void kw11_l::operator()()
while(!stop_flag) {
if (*cnsl->get_running_flag()) {
b->set_lf_crs_b7();
set_lf_crs_b7();
if (b->get_lf_crs() & 64)
if (get_lf_crs() & 64)
b->getCpu()->queue_interrupt(6, 0100);
// TODO: dependant on cpu cycles processed
@ -68,3 +85,99 @@ void kw11_l::operator()()
DOLOG(debug, true, "KW11-L thread terminating");
}
uint16_t kw11_l::readWord(const uint16_t a)
{
if (a != ADDR_LFC) {
DOLOG(debug, true, "KW11-L readWord not for us (%06o)", a);
return 0;
}
#if defined(BUILD_FOR_RP2040)
xSemaphoreTake(lf_csr_lock, portMAX_DELAY);
#else
std::unique_lock<std::mutex> lck(lf_csr_lock);
#endif
uint16_t temp = lf_csr;
#if defined(BUILD_FOR_RP2040)
xSemaphoreGive(lf_csr_lock);
#endif
return temp;
}
void kw11_l::writeWord(const uint16_t a, const uint16_t value)
{
if (a != ADDR_LFC) {
DOLOG(debug, true, "KW11-L writeWord not for us (%06o to %06o)", value, a);
return;
}
#if defined(BUILD_FOR_RP2040)
xSemaphoreTake(lf_csr_lock, portMAX_DELAY);
#else
std::unique_lock<std::mutex> lck(lf_csr_lock);
#endif
DOLOG(debug, false, "WRITE-I/O set line frequency clock/status register: %06o", value);
lf_csr = value;
#if defined(BUILD_FOR_RP2040)
xSemaphoreGive(lf_csr_lock);
#endif
}
void kw11_l::set_lf_crs_b7()
{
#if defined(BUILD_FOR_RP2040)
xSemaphoreTake(lf_csr_lock, portMAX_DELAY);
#else
std::unique_lock<std::mutex> lck(lf_csr_lock);
#endif
lf_csr |= 128;
#if defined(BUILD_FOR_RP2040)
xSemaphoreGive(lf_csr_lock);
#endif
}
uint8_t kw11_l::get_lf_crs()
{
#if defined(BUILD_FOR_RP2040)
xSemaphoreTake(lf_csr_lock, portMAX_DELAY);
#else
std::unique_lock<std::mutex> lck(lf_csr_lock);
#endif
uint8_t rc = lf_csr;
#if defined(BUILD_FOR_RP2040)
xSemaphoreGive(lf_csr_lock);
#endif
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

@ -1,26 +1,47 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <atomic>
#include <thread>
#include "bus.h"
#include "console.h"
#include "gen.h"
class kw11_l
{
private:
bus *const b { nullptr };
console *const cnsl { nullptr };
console *cnsl { nullptr };
#if !defined(BUILD_FOR_RP2040)
std::thread * th { nullptr };
#if defined(BUILD_FOR_RP2040)
SemaphoreHandle_t lf_csr_lock { xSemaphoreCreateBinary() };
#else
std::thread *th { nullptr };
std::mutex lf_csr_lock;
#endif
uint16_t lf_csr { 0 };
std::atomic_bool stop_flag { false };
uint8_t get_lf_crs();
void set_lf_crs_b7();
public:
kw11_l(bus *const b, console *const cnsl);
kw11_l(bus *const b);
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 operator()();
uint16_t readWord (const uint16_t a);
void writeWord(const uint16_t a, const uint16_t v);
};

View file

@ -28,7 +28,7 @@ void loadbin(bus *const b, uint16_t base, const char *const file)
fclose(fh);
}
void setBootLoader(bus *const b, const bootloader_t which)
void set_boot_loader(bus *const b, const bootloader_t which)
{
cpu *const c = b -> getCpu();

View file

@ -11,6 +11,6 @@
typedef enum { BL_NONE, BL_RK05, BL_RL02 } bootloader_t;
void loadbin(bus *const b, uint16_t base, const char *const file);
void setBootLoader(bus *const b, const bootloader_t which);
void set_boot_loader(bus *const b, const bootloader_t which);
std::optional<uint16_t> loadTape(bus *const b, const std::string & file);
void load_p11_x11(bus *const b, const std::string & file);

185
main.cpp
View file

@ -295,9 +295,19 @@ void get_metrics(cpu *const c)
}
}
void start_disk_devices(const std::vector<disk_backend *> & backends, const bool enable_snapshots)
{
for(auto & backend: backends) {
if (backend->begin(enable_snapshots) == false)
error_exit(false, "Failed to initialize disk backend \"%s\"", backend->get_identifier().c_str());
}
}
void help()
{
printf("-h this help\n");
printf("-D x deserialize state from file\n");
printf("-P when serializing state to file (in the debugger), include an overlay: changes to disk-files are then non-persistent, they only exist in the state-dump\n");
printf("-T t.bin load file as a binary tape file (like simh \"load\" command), also for .BIC files\n");
printf("-B run tape file as a unit test (for .BIC files)\n");
printf("-R d.rk load file as a RK05 disk device\n");
@ -317,27 +327,8 @@ void help()
printf("-M log metrics\n");
}
#include "breakpoint_parser.h"
int main(int argc, char *argv[])
{
#if 0
{
bus *b = new bus();
cpu *c = new cpu(b, &event);
b->add_cpu(c);
std::pair<breakpoint *, std::optional<std::string> > rc = parse_breakpoint(b, "(pc=0123 and (r0=01456 or r2=1) and memWV[0444]=0222)");
printf("%p\n", rc.first);
if (rc.second.has_value())
printf("%s\n", rc.second.value().c_str());
delete rc.first;
delete b;
}
return 0;
#endif
//setlocale(LC_ALL, "");
std::vector<disk_backend *> rk05_files;
@ -364,7 +355,7 @@ int main(int argc, char *argv[])
std::string test;
disk_backend *temp_d = nullptr;
bool disk_snapshots = false;
std::optional<int> set_ram_size;
@ -372,14 +363,20 @@ int main(int argc, char *argv[])
bool metrics = false;
std::string deserialize;
int opt = -1;
while((opt = getopt(argc, argv, "hMT:Br:R:p:ndtL:bl:s:Q:N:J:XS:")) != -1)
while((opt = getopt(argc, argv, "hD:MT:Br:R:p:ndtL:bl:s:Q:N:J:XS:P")) != -1)
{
switch(opt) {
case 'h':
help();
return 1;
case 'D':
deserialize = optarg;
break;
case 'M':
metrics = true;
break;
@ -434,17 +431,11 @@ int main(int argc, char *argv[])
break;
case 'R':
temp_d = new disk_backend_file(optarg);
if (!temp_d->begin())
error_exit(false, "Cannot use file \"%s\" for RK05", optarg);
rk05_files.push_back(temp_d);
rk05_files.push_back(new disk_backend_file(optarg));
break;
case 'r':
temp_d = new disk_backend_file(optarg);
if (!temp_d->begin())
error_exit(false, "Cannot use file \"%s\" for RL02", optarg);
rl02_files.push_back(temp_d);
rl02_files.push_back(new disk_backend_file(optarg));
break;
case 'N': {
@ -452,7 +443,7 @@ int main(int argc, char *argv[])
if (parts.size() != 3)
error_exit(false, "-N: parameter missing");
temp_d = new disk_backend_nbd(parts.at(0), atoi(parts.at(1).c_str()));
disk_backend *temp_d = new disk_backend_nbd(parts.at(0), atoi(parts.at(1).c_str()));
if (parts.at(2) == "rk05")
rk05_files.push_back(temp_d);
@ -487,6 +478,10 @@ int main(int argc, char *argv[])
set_ram_size = std::stoi(optarg);
break;
case 'P':
disk_snapshots = true;
break;
default:
fprintf(stderr, "-%c is not understood\n", opt);
return 1;
@ -500,16 +495,80 @@ int main(int argc, char *argv[])
if (validate_json.empty() == false)
return run_cpu_validation(validate_json);
bus *b = new bus();
DOLOG(info, true, "PDP11 emulator, by Folkert van Heusden");
DOLOG(info, true, "Built on: " __DATE__ " " __TIME__);
start_disk_devices(rk05_files, disk_snapshots);
start_disk_devices(rl02_files, disk_snapshots);
#if !defined(_WIN32)
if (withUI)
cnsl = new console_ncurses(&event);
else
#endif
cnsl = new console_posix(&event);
bus *b = nullptr;
if (deserialize.empty()) {
b = new bus();
if (set_ram_size.has_value())
b->set_memory_size(set_ram_size.value());
else
b->set_memory_size(DEFAULT_N_PAGES * 8192l);
b->set_console_switches(console_switches);
cpu *c = new cpu(b, &event);
b->add_cpu(c);
if (rk05_files.empty() == false)
bootloader = BL_RK05;
if (rl02_files.empty() == false)
bootloader = BL_RL02;
if (enable_bootloader)
set_boot_loader(b, bootloader);
b->add_rk05(new rk05(rk05_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()));
b->add_rl02(new rl02(rl02_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()));
}
else {
FILE *fh = fopen(deserialize.c_str(), "r");
if (!fh)
error_exit(true, "Failed to open %s", deserialize.c_str());
json_error_t je { };
json_t *j = json_loadf(fh, 0, &je);
fclose(fh);
if (!j)
error_exit(true, "State file %s is corrupt: %s", deserialize.c_str(), je.text);
b = bus::deserialize(j, cnsl, &event);
json_decref(j);
DOLOG(warning, true, "DO NOT FORGET TO DELETE AND NOT TO RE-USE THE STATE FILE (\"%s\")! (unless updated)", deserialize.c_str());
myusleep(251000);
}
if (b->getTty() == nullptr) {
tty *tty_ = new tty(cnsl, b);
b->add_tty(tty_);
}
cnsl->set_bus(b);
running = cnsl->get_running_flag();
std::atomic_bool interrupt_emulation { false };
std::optional<uint16_t> bic_start;
@ -520,53 +579,13 @@ int main(int argc, char *argv[])
if (bic_start.has_value() == false)
return 1; // fail
c->setRegister(7, bic_start.value());
b->getCpu()->setRegister(7, bic_start.value());
}
if (sa_set)
c->setRegister(7, start_addr);
b->getCpu()->setRegister(7, start_addr);
#if !defined(_WIN32)
if (withUI)
cnsl = new console_ncurses(&event, b);
else
#endif
{
DOLOG(info, true, "This PDP-11 emulator is called \"kek\" (reason for that is forgotten) and was written by Folkert van Heusden.");
DOLOG(info, true, "Built on: " __DATE__ " " __TIME__);
cnsl = new console_posix(&event, b);
}
if (rk05_files.empty() == false) {
if (enable_bootloader == false)
DOLOG(warning, true, "Note: loading RK05 with no (RK05-) bootloader selected");
else
bootloader = BL_RK05;
b->add_rk05(new rk05(rk05_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()));
}
if (rl02_files.empty() == false) {
if (enable_bootloader == false)
DOLOG(warning, true, "Note: loading RL02 with no (RL02-) bootloader selected");
else
bootloader = BL_RL02;
b->add_rl02(new rl02(rl02_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()));
}
if (enable_bootloader)
setBootLoader(b, bootloader);
running = cnsl->get_running_flag();
tty *tty_ = new tty(cnsl, b);
b->add_tty(tty_);
DOLOG(info, true, "Start running at %06o", c->getRegister(7));
DOLOG(info, true, "Start running at %06o", b->getCpu()->getRegister(7));
#if !defined(_WIN32)
struct sigaction sa { };
@ -584,26 +603,26 @@ int main(int argc, char *argv[])
if (test.empty() == false)
load_p11_x11(b, test);
kw11_l *lf = new kw11_l(b, cnsl);
std::thread *metrics_thread = nullptr;
if (metrics)
metrics_thread = new std::thread(get_metrics, c);
metrics_thread = new std::thread(get_metrics, b->getCpu());
cnsl->start_thread();
b->getKW11_L()->begin(cnsl);
if (is_bic)
run_bic(cnsl, b, &event, tracing, bic_start.value());
else if (run_debugger || (bootloader == BL_NONE && test.empty() && tape.empty()))
debugger(cnsl, b, &event, tracing);
else {
c->emulation_start(); // for statistics
b->getCpu()->emulation_start(); // for statistics
for(;;) {
*running = true;
while(event == EVENT_NONE)
c->step();
b->getCpu()->step();
*running = false;
@ -613,7 +632,7 @@ int main(int argc, char *argv[])
break;
}
auto stats = c->get_mips_rel_speed({ }, { });
auto stats = b->getCpu()->get_mips_rel_speed({ }, { });
cnsl->put_string_lf(format("MIPS: %.2f, relative speed: %.2f%%, instructions executed: %" PRIu64 " in %.2f seconds", std::get<0>(stats), std::get<1>(stats), std::get<2>(stats), std::get<3>(stats) / 1000000.));
}
@ -624,11 +643,11 @@ int main(int argc, char *argv[])
delete metrics_thread;
}
delete lf;
delete cnsl;
cnsl->stop_thread();
delete b;
delete cnsl;
return 0;
}

View file

@ -46,3 +46,31 @@ void memory::reset()
{
memset(m, 0x00, size);
}
#if IS_POSIX
json_t *memory::serialize() const
{
json_t *j = json_object();
json_object_set(j, "size", json_integer(size));
json_t *ja = json_array();
for(size_t i=0; i<size; i++)
json_array_append(ja, json_integer(m[i]));
json_object_set(j, "contents", ja);
return j;
}
memory *memory::deserialize(const json_t *const j)
{
size_t size = json_integer_value(json_object_get(j, "size"));
memory *m = new memory(size);
json_t *ja = json_object_get(j, "contents");
for(size_t i=0; i<size; i++)
m->m[i] = json_integer_value(json_array_get(ja, i));
return m;
}
#endif

View file

@ -2,10 +2,12 @@
// Released under MIT license
#pragma once
#include <assert.h>
#include <stdint.h>
#include "gen.h"
class memory
{
private:
@ -20,6 +22,10 @@ public:
~memory();
void reset();
#if IS_POSIX
json_t *serialize() const;
static memory *deserialize(const json_t *const j);
#endif
uint16_t readByte(const uint32_t a) const { return m[a]; }
void writeByte(const uint32_t a, const uint16_t v) { assert(a < size); m[a] = v; }

77
mmu.cpp
View file

@ -220,3 +220,80 @@ void mmu::writeByte(const uint16_t a, const uint8_t value)
else if (a >= ADDR_PAR_U_START && a < ADDR_PAR_U_END)
write_par(a, 3, value, wm_byte);
}
#if IS_POSIX
void mmu::add_par_pdr(json_t *const target, const int run_mode, const bool is_d, const std::string & name) const
{
json_t *j = json_object();
json_t *ja_par = json_array();
for(int i=0; i<8; i++)
json_array_append(ja_par, json_integer(pages[run_mode][is_d][i].par));
json_object_set(j, "par", ja_par);
json_t *ja_pdr = json_array();
for(int i=0; i<8; i++)
json_array_append(ja_pdr, json_integer(pages[run_mode][is_d][i].pdr));
json_object_set(j, "pdr", ja_pdr);
json_object_set(target, name.c_str(), j);
}
json_t *mmu::serialize() const
{
json_t *j = json_object();
for(int run_mode=0; run_mode<4; run_mode++) {
if (run_mode == 2)
continue;
for(int is_d=0; is_d<2; is_d++)
add_par_pdr(j, run_mode, is_d, format("runmode_%d_d_%d", run_mode, is_d));
}
json_object_set(j, "MMR0", json_integer(MMR0));
json_object_set(j, "MMR1", json_integer(MMR1));
json_object_set(j, "MMR2", json_integer(MMR2));
json_object_set(j, "MMR3", json_integer(MMR3));
json_object_set(j, "CPUERR", json_integer(CPUERR));
json_object_set(j, "PIR", json_integer(PIR));
json_object_set(j, "CSR", json_integer(CSR));
return j;
}
void mmu::set_par_pdr(const json_t *const j_in, const int run_mode, const bool is_d, const std::string & name)
{
json_t *j = json_object_get(j_in, name.c_str());
json_t *j_par = json_object_get(j, "par");
for(int i=0; i<8; i++)
pages[run_mode][is_d][i].par = json_integer_value(json_array_get(j_par, i));
json_t *j_pdr = json_object_get(j, "pdr");
for(int i=0; i<8; i++)
pages[run_mode][is_d][i].pdr = json_integer_value(json_array_get(j_pdr, i));
}
mmu *mmu::deserialize(const json_t *const j)
{
mmu *m = new mmu();
for(int run_mode=0; run_mode<4; run_mode++) {
if (run_mode == 2)
continue;
for(int is_d=0; is_d<2; is_d++)
m->set_par_pdr(j, run_mode, is_d, format("runmode_%d_d_%d", run_mode, is_d));
}
m->MMR0 = json_integer_value(json_object_get(j, "MMR0"));
m->MMR1 = json_integer_value(json_object_get(j, "MMR1"));
m->MMR2 = json_integer_value(json_object_get(j, "MMR2"));
m->MMR3 = json_integer_value(json_object_get(j, "MMR3"));
m->CPUERR = json_integer_value(json_object_get(j, "CPUERR"));
m->PIR = json_integer_value(json_object_get(j, "PIR"));
m->CSR = json_integer_value(json_object_get(j, "CSR"));
return m;
}
#endif

12
mmu.h
View file

@ -1,8 +1,10 @@
#pragma once
#include <cstdint>
#include <string>
#include "device.h"
#include "gen.h"
#define ADDR_PDR_SV_START 0172200
#define ADDR_PDR_SV_END 0172240
@ -39,10 +41,20 @@ private:
uint16_t PIR { 0 };
uint16_t CSR { 0 };
#if IS_POSIX
void add_par_pdr(json_t *const target, const int run_mode, const bool is_d, const std::string & name) const;
void set_par_pdr(const json_t *const j_in, const int run_mode, const bool is_d, const std::string & name);
#endif
public:
mmu();
virtual ~mmu();
#if IS_POSIX
json_t *serialize() const;
static mmu *deserialize(const json_t *const j);
#endif
void reset() override;
bool is_enabled() const { return MMR0 & 1; }

View file

@ -156,7 +156,7 @@ void rk05::writeWord(const uint16_t addr, const uint16_t v)
for(size_t i=0; i<cur; i++)
xfer_buffer[i] = b->readUnibusByte(work_memoff++);
if (!fhs.at(device)->write(work_diskoffb, cur, xfer_buffer))
if (!fhs.at(device)->write(work_diskoffb, cur, xfer_buffer, 512))
DOLOG(ll_error, true, "RK05(%d) write error %s to %u len %u", device, strerror(errno), work_diskoffb, cur);
work_diskoffb += cur;
@ -191,7 +191,7 @@ void rk05::writeWord(const uint16_t addr, const uint16_t v)
while(temp > 0) {
uint32_t cur = std::min(uint32_t(sizeof xfer_buffer), temp);
if (!fhs.at(device)->read(temp_diskoffb, cur, xfer_buffer)) {
if (!fhs.at(device)->read(temp_diskoffb, cur, xfer_buffer, 512)) {
DOLOG(ll_error, true, "RK05 read error %s from %u len %u", strerror(errno), temp_diskoffb, cur);
break;
}

5
rk05.h
View file

@ -9,7 +9,7 @@
#include <string>
#include <vector>
#include "device.h"
#include "disk_device.h"
#include "disk_backend.h"
@ -25,12 +25,11 @@
class bus;
class rk05 : public device
class rk05: public disk_device
{
private:
bus *const b { nullptr };
uint16_t registers [7] { 0 };
std::vector<disk_backend *> fhs;
uint8_t xfer_buffer[512] { 0 };
std::atomic_bool *const disk_read_acitivity { nullptr };

View file

@ -31,10 +31,10 @@ static const char * const commands[] = {
"read data w/o header check"
};
rl02::rl02(const std::vector<disk_backend *> & files, bus *const b, std::atomic_bool *const disk_read_acitivity, std::atomic_bool *const disk_write_acitivity) :
rl02::rl02(const std::vector<disk_backend *> & files, bus *const b, std::atomic_bool *const disk_read_activity, std::atomic_bool *const disk_write_activity) :
b(b),
disk_read_acitivity(disk_read_acitivity),
disk_write_acitivity(disk_write_acitivity)
disk_read_activity (disk_read_activity ),
disk_write_activity(disk_write_activity)
{
fhs = files;
@ -58,6 +58,54 @@ void rl02::reset()
sector = 0;
}
#if IS_POSIX
json_t *rl02::serialize() const
{
json_t *j = json_object();
json_t *j_backends = json_array();
for(auto & dbe: fhs)
json_array_append(j_backends, dbe->serialize());
json_object_set(j, "backends", j_backends);
for(int regnr=0; regnr<4; regnr++)
json_object_set(j, format("register-%d", regnr).c_str(), json_integer(registers[regnr]));
for(int mprnr=0; mprnr<3; mprnr++)
json_object_set(j, format("mpr-%d", mprnr).c_str(), json_integer(mpr[mprnr]));
json_object_set(j, "track", json_integer(track));
json_object_set(j, "head", json_integer(head));
json_object_set(j, "sector", json_integer(sector));
return j;
}
rl02 *rl02::deserialize(const json_t *const j, bus *const b)
{
std::vector<disk_backend *> backends;
json_t *j_backends = json_object_get(j, "backends");
for(size_t i=0; i<json_array_size(j_backends); i++)
backends.push_back(disk_backend::deserialize(json_array_get(j_backends, i)));
rl02 *r = new rl02(backends, b, nullptr, nullptr);
for(int regnr=0; regnr<4; regnr++)
r->registers[regnr] = json_integer_value(json_object_get(j, format("register-%d", regnr).c_str()));
for(int mprnr=0; mprnr<3; mprnr++)
r->mpr[mprnr] = json_integer_value(json_object_get(j, format("mpr-%d", mprnr).c_str()));
r->track = json_integer_value(json_object_get(j, "track" ));
r->head = json_integer_value(json_object_get(j, "head" ));
r->sector = json_integer_value(json_object_get(j, "sector"));
return r;
}
#endif
uint8_t rl02::readByte(const uint16_t addr)
{
uint16_t v = readWord(addr & ~1);
@ -152,8 +200,6 @@ void rl02::writeWord(const uint16_t addr, uint16_t v)
bool do_int = false;
*disk_read_acitivity = true;
if (size_t(device) >= fhs.size()) {
DOLOG(info, false, "RL02: PDP11/70 is accessing a not-attached virtual disk %d", device);
@ -194,6 +240,9 @@ void rl02::writeWord(const uint16_t addr, uint16_t v)
do_int = true;
}
else if (command == 5) { // write data
if (disk_write_activity)
*disk_write_activity = true;
uint32_t memory_address = get_bus_address();
uint32_t count = (65536l - registers[(RL02_MPR - RL02_BASE) / 2]) * 2;
@ -222,7 +271,7 @@ void rl02::writeWord(const uint16_t addr, uint16_t v)
mpr[0]++;
}
if (!fhs.at(device)->write(temp_disk_offset, cur, xfer_buffer)) {
if (fhs.at(device) == nullptr || fhs.at(device)->write(temp_disk_offset, cur, xfer_buffer, 256) == false) {
DOLOG(ll_error, true, "RL02: write error, device %d, disk offset %u, read size %u, cylinder %d, head %d, sector %d", device, temp_disk_offset, cur, track, head, sector);
break;
}
@ -247,8 +296,14 @@ void rl02::writeWord(const uint16_t addr, uint16_t v)
}
do_int = true;
if (disk_write_activity)
*disk_write_activity = false;
}
else if (command == 6 || command == 7) { // read data / read data without header check
if (disk_read_activity)
*disk_read_activity = true;
uint32_t memory_address = get_bus_address();
uint32_t count = (65536l - registers[(RL02_MPR - RL02_BASE) / 2]) * 2;
@ -270,7 +325,7 @@ void rl02::writeWord(const uint16_t addr, uint16_t v)
while(count > 0) {
uint32_t cur = std::min(uint32_t(sizeof xfer_buffer), count);
if (!fhs.at(device)->read(temp_disk_offset, cur, xfer_buffer)) {
if (fhs.at(device) == nullptr || fhs.at(device)->read(temp_disk_offset, cur, xfer_buffer, 256) == false) {
DOLOG(ll_error, true, "RL02: read error, device %d, disk offset %u, read size %u, cylinder %d, head %d, sector %d", device, temp_disk_offset, cur, track, head, sector);
break;
}
@ -305,6 +360,9 @@ void rl02::writeWord(const uint16_t addr, uint16_t v)
}
do_int = true;
if (disk_read_activity)
*disk_read_activity = false;
}
else {
DOLOG(warning, false, "RL02: command %d not implemented", command);
@ -317,7 +375,5 @@ void rl02::writeWord(const uint16_t addr, uint16_t v)
b->getCpu()->queue_interrupt(5, 0160);
}
}
*disk_read_acitivity = false;
}
}

17
rl02.h
View file

@ -9,8 +9,9 @@
#include <string>
#include <vector>
#include "device.h"
#include "disk_device.h"
#include "disk_backend.h"
#include "gen.h"
#define RL02_CSR 0174400 // control status register
@ -26,7 +27,7 @@ constexpr const int rl02_bytes_per_sector = 256;
class bus;
class rl02 : public device
class rl02: public disk_device
{
private:
bus *const b;
@ -36,10 +37,9 @@ private:
uint8_t head { 0 };
uint8_t sector { 0 };
uint16_t mpr[3];
std::vector<disk_backend *> fhs;
std::atomic_bool *const disk_read_acitivity { nullptr };
std::atomic_bool *const disk_write_acitivity { nullptr };
std::atomic_bool *const disk_read_activity { nullptr };
std::atomic_bool *const disk_write_activity { nullptr };
uint32_t get_bus_address() const;
void update_bus_address(const uint32_t a);
@ -47,11 +47,16 @@ private:
uint32_t calc_offset() const;
public:
rl02(const std::vector<disk_backend *> & files, bus *const b, std::atomic_bool *const disk_read_acitivity, std::atomic_bool *const disk_write_acitivity);
rl02(const std::vector<disk_backend *> & files, bus *const b, std::atomic_bool *const disk_read_activity, std::atomic_bool *const disk_write_activity);
virtual ~rl02();
void reset() override;
#if IS_POSIX
json_t *serialize() const;
static rl02 *deserialize(const json_t *const j, bus *const b);
#endif
uint8_t readByte(const uint16_t addr) override;
uint16_t readWord(const uint16_t addr) override;

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
#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);
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
#pragma once
@ -13,6 +13,7 @@
#include "bus.h"
#include "console.h"
#include "gen.h"
#define PDP11TTY_TKS 0177560 // reader status
@ -50,6 +51,11 @@ public:
tty(console *const c, bus *const b);
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();
uint8_t readByte(const uint16_t addr);

View file

@ -1,6 +1,8 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include "gen.h"
#if defined(ESP32) || defined(BUILD_FOR_RP2040)
#include <Arduino.h>
#include "rp2040.h"
@ -165,7 +167,7 @@ void set_thread_name(std::string name)
std::string get_thread_name()
{
#ifdef linux
#if IS_POSIX
char buffer[16 + 1] { };
pthread_getname_np(pthread_self(), buffer, sizeof buffer);