From e643b2a0bbb307347774ffe5622f99c43808316f Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Wed, 1 May 2024 22:25:22 +0200 Subject: [PATCH 01/13] load/unload tape --- bus.h | 2 + debugger.cpp | 129 ++++++++++++++++++++++++++++++++------------------- main.cpp | 3 ++ tm-11.cpp | 23 +++++++-- tm-11.h | 8 ++-- 5 files changed, 112 insertions(+), 53 deletions(-) diff --git a/bus.h b/bus.h index 6f65b6c..43138aa 100644 --- a/bus.h +++ b/bus.h @@ -49,6 +49,7 @@ class console; class cpu; class kw11_l; class memory; +class tm_11; class tty; typedef enum { T_PROCEED, T_ABORT_4, T_TRAP_250 } trap_action_t; @@ -121,6 +122,7 @@ public: mmu *getMMU() { return mmu_; } rk05 *getRK05() { return rk05_; } rl02 *getRL02() { return rl02_; } + tm_11 *getTM11() { return tm11; } 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 read_byte(const uint16_t a) { return read(a, wm_byte, rm_cur); } diff --git a/debugger.cpp b/debugger.cpp index 213b2e7..8bed3e5 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -76,35 +76,8 @@ std::optional select_nbd_server(console *const cnsl) } #endif -// disk image files -std::optional select_disk_file(console *const c) +std::optional select_host_file(console *const c) { -#if IS_POSIX - c->put_string_lf("Files in current directory: "); -#else - c->put_string_lf(format("MISO: %d", int(MISO))); - c->put_string_lf(format("MOSI: %d", int(MOSI))); - c->put_string_lf(format("SCK : %d", int(SCK ))); - c->put_string_lf(format("SS : %d", int(SS ))); - - c->put_string_lf("Files on SD-card:"); - -#if defined(SHA2017) - if (!SD.begin(21, SD_SCK_MHZ(10))) - SD.initErrorHalt(); -#elif !defined(BUILD_FOR_RP2040) - if (!SD.begin(SS, SD_SCK_MHZ(15))) { - auto err = SD.sdErrorCode(); - if (err) - c->put_string_lf(format("SDerror: 0x%x, data: 0x%x", err, SD.sdErrorData())); - else - c->put_string_lf("Failed to initialize SD card"); - - return { }; - } -#endif -#endif - for(;;) { #if defined(linux) DIR *dir = opendir("."); @@ -164,27 +137,65 @@ std::optional select_disk_file(console *const c) fh.close(); #endif - if (can_open_file) { -#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(false)) { - c->put_string("Cannot use: "); - c->put_string_lf(selected_file.c_str()); - - delete temp; - - continue; - } - - return { temp }; - } + if (can_open_file) + return selected_file; c->put_string_lf("open failed"); } +} + +// disk image files +std::optional select_disk_file(console *const c) +{ +#if IS_POSIX + c->put_string_lf("Files in current directory: "); +#else + c->put_string_lf(format("MISO: %d", int(MISO))); + c->put_string_lf(format("MOSI: %d", int(MOSI))); + c->put_string_lf(format("SCK : %d", int(SCK ))); + c->put_string_lf(format("SS : %d", int(SS ))); + + c->put_string_lf("Files on SD-card:"); + +#if defined(SHA2017) + if (!SD.begin(21, SD_SCK_MHZ(10))) + SD.initErrorHalt(); +#elif !defined(BUILD_FOR_RP2040) + if (!SD.begin(SS, SD_SCK_MHZ(15))) { + auto err = SD.sdErrorCode(); + if (err) + c->put_string_lf(format("SDerror: 0x%x, data: 0x%x", err, SD.sdErrorData())); + else + c->put_string_lf("Failed to initialize SD card"); + + return { }; + } +#endif +#endif + + for(;;) { + auto selected_file = select_host_file(c); + + if (selected_file.has_value() == false) + break; + +#if IS_POSIX + disk_backend *temp = new disk_backend_file(selected_file.value()); +#else + disk_backend *temp = new disk_backend_esp32(selected_file.value()); +#endif + + if (!temp->begin(false)) { + c->put_string("Cannot use: "); + c->put_string_lf(selected_file.value().c_str()); + + delete temp; + + continue; + } + + return { temp }; + } return { }; } @@ -235,7 +246,6 @@ std::optional select_disk_backend(console *const cnsl) void configure_disk(bus *const b, console *const cnsl) { - // TODO tape int type_ch = wait_for_key("1. RK05, 2. RL02, 9. abort", cnsl, { '1', '2', '3', '9' }); bootloader_t bl = BL_NONE; @@ -590,6 +600,19 @@ void serialize_state(console *const cnsl, const bus *const b, const std::string } #endif +void tm11_load_tape(console *const cnsl, bus *const b) +{ + auto file = select_host_file(cnsl); + + if (file.has_value()) + b->getTM11()->load(file.value()); +} + +void tm11_unload_tape(bus *const b) +{ + b->getTM11()->unload(); +} + 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; @@ -959,6 +982,16 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto continue; } + else if (cmd == "lt") { + tm11_load_tape(cnsl, b); + + continue; + } + else if (cmd == "ult") { + tm11_unload_tape(b); + + continue; + } else if (cmd == "dp") { cnsl->stop_panel_thread(); @@ -1012,6 +1045,8 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto "setmem - set memory (a=) to value (v=), both in octal, one byte", "toggle - set switch (s=, 0...15 (decimal)) of the front panel to state (t=, 0 or 1)", "cls - clear screen", + "lt - load tape (parameter is filename)", + "ult - unload tape", "stats - show run statistics", "ramsize - set ram size (page count (8 kB))", "bl - set bootload (rl02 or rk05)", diff --git a/main.cpp b/main.cpp index 229bbd2..ef08444 100644 --- a/main.cpp +++ b/main.cpp @@ -580,6 +580,9 @@ int main(int argc, char *argv[]) cnsl->set_bus(b); cnsl->begin(); + tm_11 *tm_11_ = new tm_11(b); + b->add_tm11(tm_11_); + running = cnsl->get_running_flag(); std::atomic_bool interrupt_emulation { false }; diff --git a/tm-11.cpp b/tm-11.cpp index e2c12ff..f145578 100644 --- a/tm-11.cpp +++ b/tm-11.cpp @@ -10,18 +10,35 @@ #include "memory.h" #include "utils.h" -tm_11::tm_11(const std::string & file, memory *const m): file(file), m(m) +tm_11::tm_11(bus *const b): m(b->getRAM()) { } tm_11::~tm_11() { - fclose(fh); + if (fh) + fclose(fh); } -void tm_11::begin() +void tm_11::unload() { + if (fh) { + fclose(fh); + fh = nullptr; + } + + tape_file.clear(); + + reset(); +} + +void tm_11::load(const std::string & file) +{ + if (fh) + fclose(fh); + fh = fopen(file.c_str(), "rb"); + tape_file = file; reset(); } diff --git a/tm-11.h b/tm-11.h index b65456a..fd9f4a0 100644 --- a/tm-11.h +++ b/tm-11.h @@ -7,6 +7,7 @@ #include #include +#include "bus.h" #include "device.h" #define TM_11_MTS 0172520 // status register @@ -24,18 +25,19 @@ class memory; class tm_11 : public device { private: - std::string file; memory *const m { nullptr }; uint16_t registers[6] { 0 }; uint8_t xfer_buffer[65536]; int offset { 0 }; FILE *fh { nullptr }; + std::string tape_file; public: - tm_11(const std::string & file, memory *const m); + tm_11(bus *const b); virtual ~tm_11(); - void begin(); + void load(const std::string & file); + void unload(); void reset() override; From 16b2554c90d95a95951701c3ffec0aa1446bd15e Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Wed, 1 May 2024 23:04:45 +0200 Subject: [PATCH 02/13] lt can now get a filename directly as parameter --- debugger.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/debugger.cpp b/debugger.cpp index 8bed3e5..b28fb89 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -600,12 +600,16 @@ void serialize_state(console *const cnsl, const bus *const b, const std::string } #endif -void tm11_load_tape(console *const cnsl, bus *const b) +void tm11_load_tape(console *const cnsl, bus *const b, const std::optional & file) { - auto file = select_host_file(cnsl); - if (file.has_value()) b->getTM11()->load(file.value()); + else { + auto sel_file = select_host_file(cnsl); + + if (sel_file.has_value()) + b->getTM11()->load(sel_file.value()); + } } void tm11_unload_tape(bus *const b) @@ -982,8 +986,11 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto continue; } - else if (cmd == "lt") { - tm11_load_tape(cnsl, b); + else if (parts[0] == "lt") { + if (parts.size() == 2) + tm11_load_tape(cnsl, b, parts[1]); + else + tm11_load_tape(cnsl, b, { }); continue; } From 1e0aacf7b906fa3d19e967a87ac2a0ab4e0d6793 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Wed, 1 May 2024 23:17:43 +0200 Subject: [PATCH 03/13] license date --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 9028bcc..9ce50c2 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Folkert van Heusden +Copyright (c) 2018-2024 Folkert van Heusden Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 4c9a07d937eb2bfb1ab15d7ead752361812b1a92 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Wed, 1 May 2024 23:26:24 +0200 Subject: [PATCH 04/13] list of files --- BIC/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 BIC/README.md diff --git a/BIC/README.md b/BIC/README.md new file mode 100644 index 0000000..7513d02 --- /dev/null +++ b/BIC/README.md @@ -0,0 +1,9 @@ +BKTCB0.BIC 11/40 memory management MTPI/MFPI instruction test +EKBAD0.BIC 11/70 cpu basic instructions +EKBBF0.BIC 11/70 cpu advanced instructions +EKBEE1.BIC 11/70 memory management +EKBFD1.BIC 11/70 unibus map +EMKAB0.BIC 11/70 main memory test +EQKCE1.BIC 11/70 instruction exerciser +ZRKJE0.BIC RK05 basic logic test #1 +ZRKKF2.BIC RK05 basic logic test #2 From aa7e14d8f2772e77a777816068f15280eb5140ba Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Thu, 2 May 2024 01:32:21 +0200 Subject: [PATCH 05/13] BISB/BICB: keep MSB for register operations --- cpu.cpp | 65 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/cpu.cpp b/cpu.cpp index 18854a6..deffbe8 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -641,39 +641,64 @@ bool cpu::double_operand_instructions(const uint16_t instr) } case 0b100: { // BIC/BICB Bit Clear Word/Byte - gam_rc_t g_src = getGAM(src_mode, src_reg, word_mode, rm_cur); + gam_rc_t g_src = getGAM(src_mode, src_reg, word_mode, rm_cur); - auto g_dst = getGAM(dst_mode, dst_reg, word_mode, rm_cur); + if (dst_mode == 0) { + addToMMR1(g_src); // keep here because of order of updates - addToMMR1(g_dst); - addToMMR1(g_src); + uint16_t v = getRegister(dst_reg); // need the full word + uint16_t result = v & ~g_src.value.value(); - uint16_t result = g_dst.value.value() & ~g_src.value.value(); + setRegister(dst_reg, result); - if (put_result(g_dst, result)) - setPSW_flags_nzv(result, word_mode); + setPSW_flags_nzv(result, word_mode); + } + else { + auto g_dst = getGAM(dst_mode, dst_reg, word_mode, rm_cur); - return true; + addToMMR1(g_dst); + addToMMR1(g_src); + + uint16_t result = g_dst.value.value() & ~g_src.value.value(); + + if (put_result(g_dst, result)) + setPSW_flags_nzv(result, word_mode); + } + + return true; } case 0b101: { // BIS/BISB Bit Set Word/Byte - // TODO: retain MSB for register operations? - gam_rc_t g_src = getGAM(src_mode, src_reg, word_mode, rm_cur); + gam_rc_t g_src = getGAM(src_mode, src_reg, word_mode, rm_cur); - auto g_dst = getGAM(dst_mode, dst_reg, word_mode, rm_cur); + if (dst_mode == 0) { + addToMMR1(g_src); // keep here because of order of updates - addToMMR1(g_dst); - addToMMR1(g_src); + uint16_t v = getRegister(dst_reg); // need the full word + uint16_t result = v | g_src.value.value(); - uint16_t result = g_dst.value.value() | g_src.value.value(); + setRegister(dst_reg, result); - if (put_result(g_dst, result)) { - setPSW_n(SIGN(result, word_mode)); - setPSW_z(IS_0(result, word_mode)); - setPSW_v(false); - } + setPSW_n(SIGN(result, word_mode)); + setPSW_z(IS_0(result, word_mode)); + setPSW_v(false); + } + else { + auto g_dst = getGAM(dst_mode, dst_reg, word_mode, rm_cur); - return true; + addToMMR1(g_dst); + addToMMR1(g_src); + + uint16_t result = g_dst.value.value() | g_src.value.value(); + + if (put_result(g_dst, result)) { + setPSW_n(SIGN(result, word_mode)); + setPSW_z(IS_0(result, word_mode)); + setPSW_v(false); + } + } + + return true; } case 0b110: { // ADD/SUB Add/Subtract Word From 8072fcbceed492ddb7737b1fd8e3636d470745d8 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Thu, 2 May 2024 21:02:17 +0200 Subject: [PATCH 06/13] missing set_memory_size --- main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/main.cpp b/main.cpp index ef08444..8e5fd6e 100644 --- a/main.cpp +++ b/main.cpp @@ -69,6 +69,7 @@ int run_cpu_validation(const std::string & filename) // create environment event = 0; bus *b = new bus(); + b->set_memory_size(DEFAULT_N_PAGES * 8192l); cpu *c = new cpu(b, &event); b->add_cpu(c); From 3e8a64144e034f14b9d6373e3b789d8b9de15e92 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Thu, 2 May 2024 21:32:42 +0200 Subject: [PATCH 07/13] assert --- cpu.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cpu.cpp b/cpu.cpp index deffbe8..e76be6a 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -539,11 +539,15 @@ gam_rc_t cpu::getGAM(const uint8_t mode, const uint8_t reg, const word_mode_t wo break; } + assert(g.value < 256 || word_mode == wm_word); + return g; } bool cpu::putGAM(const gam_rc_t & g, const uint16_t value) { + assert(value < 256 || g.word_mode == wm_word); + if (g.addr.has_value()) { auto rc = b->write(g.addr.value(), g.word_mode, value, g.mode_selection, g.space); From 8860b04045c2bdc6d88ee05e2e9c9467c60e4a14 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Fri, 3 May 2024 19:32:08 +0200 Subject: [PATCH 08/13] the check if odd-address access is performed is done by the mmu --- bus.cpp | 42 ++++++++++-------------------------------- bus.h | 1 - cpu.cpp | 2 +- mmu.cpp | 11 +++++++++++ mmu.h | 2 ++ 5 files changed, 24 insertions(+), 34 deletions(-) diff --git a/bus.cpp b/bus.cpp index aaa8c17..d8e6b72 100644 --- a/bus.cpp +++ b/bus.cpp @@ -212,18 +212,6 @@ void bus::init() mmu_->setMMR3(0); } -void bus::trap_odd(const uint16_t a) -{ - uint16_t temp = mmu_->getMMR0(); - - temp &= ~(7 << 1); - temp |= (a >> 13) << 1; - - mmu_->setMMR0(temp); - - c->trap(004); // invalid access -} - uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm_selection_t mode_selection, const bool peek_only, const d_i_space_t space) { int run_mode = mode_selection == rm_cur ? c->getPSW_runmode() : c->getPSW_prev_runmode(); @@ -270,9 +258,9 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm ///^ registers ^/// if (!peek_only) { - if ((a & 1) && word_mode == wm_word) { + if ((a & 1) && word_mode == wm_word) [[unlikely]] { DOLOG(debug, false, "READ-I/O odd address %06o UNHANDLED", a); - trap_odd(a); + mmu_->trap_if_odd(addr_in, run_mode, space, false); throw 0; return 0; } @@ -507,8 +495,8 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm } if (peek_only == false && word_mode == wm_word && (addr_in & 1)) { - if (!peek_only) DOLOG(debug, false, "READ from %06o - odd address!", addr_in); - trap_odd(addr_in); + DOLOG(debug, false, "READ from %06o - odd address!", addr_in); + mmu_->trap_if_odd(addr_in, run_mode, space, false); throw 2; return 0; } @@ -534,18 +522,6 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm return temp; } -void bus::check_odd_addressing(const uint16_t a, const int run_mode, const d_i_space_t space, const bool is_write) -{ - if (a & 1) { - if (is_write) - mmu_->set_page_trapped(run_mode, space == d_space, a >> 13); - - trap_odd(a); - - throw 4; - } -} - memory_addresses_t bus::calculate_physical_address(const int run_mode, const uint16_t a) const { const uint8_t apf = a >> 13; // active page field @@ -1011,10 +987,11 @@ write_rc_t bus::write(const uint16_t addr_in, const word_mode_t word_mode, uint1 DOLOG(debug, false, "WRITE-I/O UNHANDLED %08o(%c): %06o (base: %o)", m_offset, word_mode == wm_byte ? 'B' : 'W', value, get_io_base()); - if (word_mode == wm_word && (a & 1)) { + if (word_mode == wm_word && (a & 1)) [[unlikely]] { DOLOG(debug, false, "WRITE-I/O to %08o (value: %06o) - odd address!", m_offset, value); - trap_odd(a); + mmu_->trap_if_odd(a, run_mode, space, true); + throw 8; } @@ -1023,10 +1000,11 @@ write_rc_t bus::write(const uint16_t addr_in, const word_mode_t word_mode, uint1 throw 9; } - if (word_mode == wm_word && (addr_in & 1)) { + if (word_mode == wm_word && (addr_in & 1)) [[unlikely]] { DOLOG(debug, false, "WRITE to %06o (value: %06o) - odd address!", addr_in, value); - trap_odd(addr_in); + mmu_->trap_if_odd(addr_in, run_mode, space, true); + throw 10; } diff --git a/bus.h b/bus.h index 43138aa..7ea9fc1 100644 --- a/bus.h +++ b/bus.h @@ -140,7 +140,6 @@ public: void writePhysical(const uint32_t a, const uint16_t value); void check_odd_addressing(const uint16_t a, const int run_mode, const d_i_space_t space, const bool is_write); - void trap_odd(const uint16_t a); uint32_t get_io_base() const { return mmu_->getMMR0() & 1 ? (mmu_->getMMR3() & 16 ? 017760000 : 0760000) : 0160000; } bool is_psw(const uint16_t addr, const int run_mode, const d_i_space_t space) const; diff --git a/cpu.cpp b/cpu.cpp index 18854a6..d5be197 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -1361,7 +1361,7 @@ bool cpu::single_operand_instructions(const uint16_t instr) } else { auto a = getGAM(dst_mode, dst_reg, word_mode, rm_cur); - addToMMR1(a); + addToMMR1(a); uint16_t vl = a.value.value(); uint16_t v = (vl << 1) & (word_mode == wm_byte ? 0xff : 0xffff); diff --git a/mmu.cpp b/mmu.cpp index ddd376b..3e90b8d 100644 --- a/mmu.cpp +++ b/mmu.cpp @@ -213,6 +213,17 @@ void mmu::write_byte(const uint16_t a, const uint8_t value) write_par(a, 3, value, wm_byte); } +void mmu::trap_if_odd(const uint16_t a, const int run_mode, const d_i_space_t space, const bool is_write) +{ + int page = a >> 13; + + if (is_write) + set_page_trapped(run_mode, space == d_space, page); + + MMR0 &= ~(7 << 1); + MMR0 |= page << 1; +} + #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 { diff --git a/mmu.h b/mmu.h index e8edc75..f8be6e8 100644 --- a/mmu.h +++ b/mmu.h @@ -88,6 +88,8 @@ public: void setMMR0Bit(const int bit); void clearMMR0Bit(const int bit); + void trap_if_odd(const uint16_t a, const int run_mode, const d_i_space_t space, const bool is_write); + uint16_t getCPUERR() const { return CPUERR; } void setCPUERR(const uint16_t v) { CPUERR = v; } From de293967a297e74d87638c887b273f761a82f65f Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Fri, 3 May 2024 19:39:28 +0200 Subject: [PATCH 09/13] calculate_physical_address moves to mmu --- bus.cpp | 46 +++++++--------------------------------------- bus.h | 11 ----------- debugger.cpp | 2 +- mmu.cpp | 33 +++++++++++++++++++++++++++++++++ mmu.h | 11 +++++++++++ 5 files changed, 52 insertions(+), 51 deletions(-) diff --git a/bus.cpp b/bus.cpp index d8e6b72..de41cb1 100644 --- a/bus.cpp +++ b/bus.cpp @@ -218,7 +218,7 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm uint32_t m_offset = calculate_physical_address(run_mode, addr_in, !peek_only, false, peek_only, space); - uint32_t io_base = get_io_base(); + uint32_t io_base = mmu_->get_io_base(); bool is_io = m_offset >= io_base; if (is_io) { @@ -485,7 +485,7 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm } if (!peek_only) { - DOLOG(debug, false, "READ-I/O UNHANDLED read %08o (%c), (base: %o)", m_offset, word_mode == wm_byte ? 'B' : ' ', get_io_base()); + DOLOG(debug, false, "READ-I/O UNHANDLED read %08o (%c), (base: %o)", m_offset, word_mode == wm_byte ? 'B' : ' ', mmu_->get_io_base()); c->trap(004); // no such i/o throw 1; @@ -522,41 +522,9 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm return temp; } -memory_addresses_t bus::calculate_physical_address(const int run_mode, const uint16_t a) const -{ - const uint8_t apf = a >> 13; // active page field - - if (mmu_->is_enabled() == false) { - bool is_psw = a == ADDR_PSW; - return { a, apf, a, is_psw, a, is_psw }; - } - - uint32_t physical_instruction = mmu_->get_physical_memory_offset(run_mode, 0, apf); - uint32_t physical_data = mmu_->get_physical_memory_offset(run_mode, 1, apf); - - uint16_t p_offset = a & 8191; // page offset - - physical_instruction += p_offset; - physical_data += p_offset; - - if ((mmu_->getMMR3() & 16) == 0) { // offset is 18bit - physical_instruction &= 0x3ffff; - physical_data &= 0x3ffff; - } - - if (mmu_->get_use_data_space(run_mode) == false) - physical_data = physical_instruction; - - uint32_t io_base = get_io_base(); - bool physical_instruction_is_psw = (physical_instruction - io_base + 0160000) == ADDR_PSW; - bool physical_data_is_psw = (physical_data - io_base + 0160000) == ADDR_PSW; - - return { a, apf, physical_instruction, physical_instruction_is_psw, physical_data, physical_data_is_psw }; -} - bool bus::is_psw(const uint16_t addr, const int run_mode, const d_i_space_t space) const { - auto meta = calculate_physical_address(run_mode, addr); + auto meta = mmu_->calculate_physical_address(run_mode, addr); if (space == d_space && meta.physical_data_is_psw) return true; @@ -570,7 +538,7 @@ bool bus::is_psw(const uint16_t addr, const int run_mode, const d_i_space_t spac void bus::mmudebug(const uint16_t a) { for(int rm=0; rm<4; rm++) { - auto ma = calculate_physical_address(rm, a); + auto ma = mmu_->calculate_physical_address(rm, a); DOLOG(debug, false, "RM %d, a: %06o, apf: %d, PI: %08o (PSW: %d), PD: %08o (PSW: %d)", rm, ma.virtual_address, ma.apf, ma.physical_instruction, ma.physical_instruction_is_psw, ma.physical_data, ma.physical_data_is_psw); } @@ -626,7 +594,7 @@ uint32_t bus::calculate_physical_address(const int run_mode, const uint16_t a, c if ((mmu_->getMMR3() & 16) == 0) // off is 18bit m_offset &= 0x3ffff; - uint32_t io_base = get_io_base(); + uint32_t io_base = mmu_->get_io_base(); bool is_io = m_offset >= io_base; if (trap_on_failure) [[unlikely]] { @@ -769,7 +737,7 @@ write_rc_t bus::write(const uint16_t addr_in, const word_mode_t word_mode, uint1 uint32_t m_offset = calculate_physical_address(run_mode, addr_in, true, true, false, space); - uint32_t io_base = get_io_base(); + uint32_t io_base = mmu_->get_io_base(); bool is_io = m_offset >= io_base; if (is_io) { @@ -985,7 +953,7 @@ write_rc_t bus::write(const uint16_t addr_in, const word_mode_t word_mode, uint1 /////////// - DOLOG(debug, false, "WRITE-I/O UNHANDLED %08o(%c): %06o (base: %o)", m_offset, word_mode == wm_byte ? 'B' : 'W', value, get_io_base()); + DOLOG(debug, false, "WRITE-I/O UNHANDLED %08o(%c): %06o (base: %o)", m_offset, word_mode == wm_byte ? 'B' : 'W', value, mmu_->get_io_base()); if (word_mode == wm_word && (a & 1)) [[unlikely]] { DOLOG(debug, false, "WRITE-I/O to %08o (value: %06o) - odd address!", m_offset, value); diff --git a/bus.h b/bus.h index 7ea9fc1..44461dd 100644 --- a/bus.h +++ b/bus.h @@ -54,15 +54,6 @@ class tty; typedef enum { T_PROCEED, T_ABORT_4, T_TRAP_250 } trap_action_t; -typedef struct { - uint16_t virtual_address; - uint8_t apf; // active page field - uint32_t physical_instruction; - bool physical_instruction_is_psw; - uint32_t physical_data; - bool physical_data_is_psw; -} memory_addresses_t; - typedef struct { bool is_psw; } write_rc_t; @@ -141,12 +132,10 @@ public: void check_odd_addressing(const uint16_t a, const int run_mode, const d_i_space_t space, const bool is_write); - uint32_t get_io_base() const { return mmu_->getMMR0() & 1 ? (mmu_->getMMR3() & 16 ? 017760000 : 0760000) : 0160000; } bool is_psw(const uint16_t addr, const int run_mode, const d_i_space_t space) const; std::pair get_trap_action(const int run_mode, const bool d, const int apf, const bool is_write); uint32_t calculate_physical_address(const int run_mode, const uint16_t a, const bool trap_on_failure, const bool is_write, const bool peek_only, const d_i_space_t space); - memory_addresses_t calculate_physical_address(const int run_mode, const uint16_t a) const; void check_address(const bool trap_on_failure, const bool is_write, const memory_addresses_t & addr, const word_mode_t word_mode, const bool is_data, const int run_mode); }; diff --git a/debugger.cpp b/debugger.cpp index b28fb89..3c60733 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -498,7 +498,7 @@ void mmu_resolve(console *const cnsl, bus *const b, const uint16_t va) int run_mode = b->getCpu()->getPSW_runmode(); cnsl->put_string_lf(format("Run mode: %d, use data space: %d", run_mode, b->getMMU()->get_use_data_space(run_mode))); - auto data = b->calculate_physical_address(run_mode, va); + auto data = b->getMMU()->calculate_physical_address(run_mode, va); uint16_t page_offset = va & 8191; cnsl->put_string_lf(format("Active page field: %d, page offset: %o (%d)", data.apf, page_offset, page_offset)); diff --git a/mmu.cpp b/mmu.cpp index 3e90b8d..e995d2c 100644 --- a/mmu.cpp +++ b/mmu.cpp @@ -1,6 +1,7 @@ #include #include +#include "bus.h" // for (at least) ADDR_PSW #include "gen.h" #include "log.h" #include "mmu.h" @@ -224,6 +225,38 @@ void mmu::trap_if_odd(const uint16_t a, const int run_mode, const d_i_space_t sp MMR0 |= page << 1; } +memory_addresses_t mmu::calculate_physical_address(const int run_mode, const uint16_t a) const +{ + const uint8_t apf = a >> 13; // active page field + + if (is_enabled() == false) { + bool is_psw = a == ADDR_PSW; + return { a, apf, a, is_psw, a, is_psw }; + } + + uint32_t physical_instruction = get_physical_memory_offset(run_mode, 0, apf); + uint32_t physical_data = get_physical_memory_offset(run_mode, 1, apf); + + uint16_t p_offset = a & 8191; // page offset + + physical_instruction += p_offset; + physical_data += p_offset; + + if ((getMMR3() & 16) == 0) { // offset is 18bit + physical_instruction &= 0x3ffff; + physical_data &= 0x3ffff; + } + + if (get_use_data_space(run_mode) == false) + physical_data = physical_instruction; + + uint32_t io_base = get_io_base(); + bool physical_instruction_is_psw = (physical_instruction - io_base + 0160000) == ADDR_PSW; + bool physical_data_is_psw = (physical_data - io_base + 0160000) == ADDR_PSW; + + return { a, apf, physical_instruction, physical_instruction_is_psw, physical_data, physical_data_is_psw }; +} + #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 { diff --git a/mmu.h b/mmu.h index f8be6e8..1dd5ef0 100644 --- a/mmu.h +++ b/mmu.h @@ -22,6 +22,15 @@ #define ADDR_PAR_U_END 0177700 +typedef struct { + uint16_t virtual_address; + uint8_t apf; // active page field + uint32_t physical_instruction; + bool physical_instruction_is_psw; + uint32_t physical_data; + bool physical_data_is_psw; +} memory_addresses_t; + typedef struct { uint16_t par; uint16_t pdr; @@ -69,6 +78,8 @@ public: int get_pdr_direction (const int run_mode, const bool d, const int apf) { return pages[run_mode][d][apf].pdr & 8; } uint32_t get_physical_memory_offset(const int run_mode, const bool d, const int apf) const { return pages[run_mode][d][apf].par * 64; } bool get_use_data_space(const int run_mode) const; + memory_addresses_t calculate_physical_address(const int run_mode, const uint16_t a) const; + uint32_t get_io_base() const { return getMMR0() & 1 ? (getMMR3() & 16 ? 017760000 : 0760000) : 0160000; } uint16_t getMMR0() const { return MMR0; } uint16_t getMMR1() const { return MMR1; } From f319439314946ca5b8d38bfaa33bad92f2f6e4b6 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Fri, 3 May 2024 19:42:28 +0200 Subject: [PATCH 10/13] get_trap_action is an mmu action --- bus.cpp | 34 +--------------------------------- bus.h | 3 --- debugger.cpp | 4 ++-- mmu.cpp | 32 ++++++++++++++++++++++++++++++++ mmu.h | 6 +++++- 5 files changed, 40 insertions(+), 39 deletions(-) diff --git a/bus.cpp b/bus.cpp index de41cb1..783d250 100644 --- a/bus.cpp +++ b/bus.cpp @@ -544,38 +544,6 @@ void bus::mmudebug(const uint16_t a) } } -std::pair bus::get_trap_action(const int run_mode, const bool d, const int apf, const bool is_write) -{ - const int access_control = mmu_->get_access_control(run_mode, d, apf); - - trap_action_t trap_action = T_PROCEED; - - if (access_control == 0) - trap_action = T_ABORT_4; - else if (access_control == 1) - trap_action = is_write ? T_ABORT_4 : T_TRAP_250; - else if (access_control == 2) { - if (is_write) - trap_action = T_ABORT_4; - } - else if (access_control == 3) - trap_action = T_ABORT_4; - else if (access_control == 4) - trap_action = T_TRAP_250; - else if (access_control == 5) { - if (is_write) - trap_action = T_TRAP_250; - } - else if (access_control == 6) { - // proceed - } - else if (access_control == 7) { - trap_action = T_ABORT_4; - } - - return { trap_action, access_control }; -} - uint32_t bus::calculate_physical_address(const int run_mode, const uint16_t a, const bool trap_on_failure, const bool is_write, const bool peek_only, const d_i_space_t space) { uint32_t m_offset = a; @@ -599,7 +567,7 @@ uint32_t bus::calculate_physical_address(const int run_mode, const uint16_t a, c if (trap_on_failure) [[unlikely]] { { - auto rc = get_trap_action(run_mode, d, apf, is_write); + auto rc = mmu_->get_trap_action(run_mode, d, apf, is_write); auto trap_action = rc.first; int access_control = rc.second; diff --git a/bus.h b/bus.h index 44461dd..119789c 100644 --- a/bus.h +++ b/bus.h @@ -52,8 +52,6 @@ class memory; class tm_11; class tty; -typedef enum { T_PROCEED, T_ABORT_4, T_TRAP_250 } trap_action_t; - typedef struct { bool is_psw; } write_rc_t; @@ -134,7 +132,6 @@ public: bool is_psw(const uint16_t addr, const int run_mode, const d_i_space_t space) const; - std::pair get_trap_action(const int run_mode, const bool d, const int apf, const bool is_write); uint32_t calculate_physical_address(const int run_mode, const uint16_t a, const bool trap_on_failure, const bool is_write, const bool peek_only, const d_i_space_t space); void check_address(const bool trap_on_failure, const bool is_write, const memory_addresses_t & addr, const word_mode_t word_mode, const bool is_data, const int run_mode); diff --git a/debugger.cpp b/debugger.cpp index 3c60733..046babc 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -521,8 +521,8 @@ void mmu_resolve(console *const cnsl, bus *const b, const uint16_t va) } for(int i=0; i<2; i++) { - auto ta_i = b->get_trap_action(run_mode, false, data.apf, i); - auto ta_d = b->get_trap_action(run_mode, true, data.apf, i); + auto ta_i = b->getMMU()->get_trap_action(run_mode, false, data.apf, i); + auto ta_d = b->getMMU()->get_trap_action(run_mode, true, data.apf, i); cnsl->put_string_lf(format("Instruction action: %s (%s)", trap_action_to_str(ta_i.first), i ? "write" : "read")); cnsl->put_string_lf(format("Data action : %s (%s)", trap_action_to_str(ta_d.first), i ? "write" : "read")); diff --git a/mmu.cpp b/mmu.cpp index e995d2c..3f28fe2 100644 --- a/mmu.cpp +++ b/mmu.cpp @@ -257,6 +257,38 @@ memory_addresses_t mmu::calculate_physical_address(const int run_mode, const uin return { a, apf, physical_instruction, physical_instruction_is_psw, physical_data, physical_data_is_psw }; } +std::pair mmu::get_trap_action(const int run_mode, const bool d, const int apf, const bool is_write) +{ + const int access_control = get_access_control(run_mode, d, apf); + + trap_action_t trap_action = T_PROCEED; + + if (access_control == 0) + trap_action = T_ABORT_4; + else if (access_control == 1) + trap_action = is_write ? T_ABORT_4 : T_TRAP_250; + else if (access_control == 2) { + if (is_write) + trap_action = T_ABORT_4; + } + else if (access_control == 3) + trap_action = T_ABORT_4; + else if (access_control == 4) + trap_action = T_TRAP_250; + else if (access_control == 5) { + if (is_write) + trap_action = T_TRAP_250; + } + else if (access_control == 6) { + // proceed + } + else if (access_control == 7) { + trap_action = T_ABORT_4; + } + + return { trap_action, access_control }; +} + #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 { diff --git a/mmu.h b/mmu.h index 1dd5ef0..47f43d0 100644 --- a/mmu.h +++ b/mmu.h @@ -22,6 +22,8 @@ #define ADDR_PAR_U_END 0177700 +typedef enum { T_PROCEED, T_ABORT_4, T_TRAP_250 } trap_action_t; + typedef struct { uint16_t virtual_address; uint8_t apf; // active page field @@ -78,9 +80,11 @@ public: int get_pdr_direction (const int run_mode, const bool d, const int apf) { return pages[run_mode][d][apf].pdr & 8; } uint32_t get_physical_memory_offset(const int run_mode, const bool d, const int apf) const { return pages[run_mode][d][apf].par * 64; } bool get_use_data_space(const int run_mode) const; - memory_addresses_t calculate_physical_address(const int run_mode, const uint16_t a) const; uint32_t get_io_base() const { return getMMR0() & 1 ? (getMMR3() & 16 ? 017760000 : 0760000) : 0160000; } + memory_addresses_t calculate_physical_address(const int run_mode, const uint16_t a) const; + std::pair get_trap_action(const int run_mode, const bool d, const int apf, const bool is_write); + uint16_t getMMR0() const { return MMR0; } uint16_t getMMR1() const { return MMR1; } uint16_t getMMR2() const { return MMR2; } From 51f2f3b5086366fd97adc6e1b3d5fc3e41290258 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Fri, 3 May 2024 20:09:32 +0200 Subject: [PATCH 11/13] 2nd calculate_physical_address moved to mmu --- bus.cpp | 168 ++------------------------------------------ bus.h | 4 -- console_ncurses.cpp | 2 +- cpu.cpp | 5 +- cpu.h | 8 ++- mmu.cpp | 164 +++++++++++++++++++++++++++++++++++++++++- mmu.h | 14 +++- 7 files changed, 189 insertions(+), 176 deletions(-) diff --git a/bus.cpp b/bus.cpp index 783d250..c89ea3f 100644 --- a/bus.cpp +++ b/bus.cpp @@ -24,7 +24,6 @@ bus::bus() { mmu_ = new mmu(); - mmu_->begin(); kw11_l_ = new kw11_l(this); @@ -80,9 +79,10 @@ bus *bus::deserialize(const json_t *const j, console *const cnsl, std::atomic_ui json_t *temp = nullptr; + memory *m = nullptr; temp = json_object_get(j, "memory"); if (temp) { - memory *m = memory::deserialize(temp); + m = memory::deserialize(temp); b->add_ram(m); } @@ -100,7 +100,7 @@ bus *bus::deserialize(const json_t *const j, console *const cnsl, std::atomic_ui temp = json_object_get(j, "mmu"); if (temp) { - mmu *mmu_ = mmu::deserialize(temp); + mmu *mmu_ = mmu::deserialize(temp, m); b->add_mmu(mmu_); } @@ -168,6 +168,8 @@ void bus::add_ram(memory *const m) { delete this->m; this->m = m; + + mmu_->begin(m); } void bus::add_mmu(mmu *const mmu_) @@ -216,7 +218,7 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm { int run_mode = mode_selection == rm_cur ? c->getPSW_runmode() : c->getPSW_prev_runmode(); - uint32_t m_offset = calculate_physical_address(run_mode, addr_in, !peek_only, false, peek_only, space); + uint32_t m_offset = mmu_->calculate_physical_address(c, run_mode, addr_in, !peek_only, false, peek_only, space); uint32_t io_base = mmu_->get_io_base(); bool is_io = m_offset >= io_base; @@ -535,162 +537,6 @@ bool bus::is_psw(const uint16_t addr, const int run_mode, const d_i_space_t spac return false; } -void bus::mmudebug(const uint16_t a) -{ - for(int rm=0; rm<4; rm++) { - auto ma = mmu_->calculate_physical_address(rm, a); - - DOLOG(debug, false, "RM %d, a: %06o, apf: %d, PI: %08o (PSW: %d), PD: %08o (PSW: %d)", rm, ma.virtual_address, ma.apf, ma.physical_instruction, ma.physical_instruction_is_psw, ma.physical_data, ma.physical_data_is_psw); - } -} - -uint32_t bus::calculate_physical_address(const int run_mode, const uint16_t a, const bool trap_on_failure, const bool is_write, const bool peek_only, const d_i_space_t space) -{ - uint32_t m_offset = a; - - if (mmu_->is_enabled() || (is_write && (mmu_->getMMR0() & (1 << 8 /* maintenance check */)))) { - uint8_t apf = a >> 13; // active page field - - bool d = space == d_space && mmu_->get_use_data_space(run_mode); - - uint16_t p_offset = a & 8191; // page offset - - m_offset = mmu_->get_physical_memory_offset(run_mode, d, apf); - - m_offset += p_offset; - - if ((mmu_->getMMR3() & 16) == 0) // off is 18bit - m_offset &= 0x3ffff; - - uint32_t io_base = mmu_->get_io_base(); - bool is_io = m_offset >= io_base; - - if (trap_on_failure) [[unlikely]] { - { - auto rc = mmu_->get_trap_action(run_mode, d, apf, is_write); - auto trap_action = rc.first; - int access_control = rc.second; - - if (trap_action != T_PROCEED) { - if (is_write) - mmu_->set_page_trapped(run_mode, d, apf); - - if (mmu_->is_locked() == false) { - uint16_t temp = mmu_->getMMR0(); - - temp &= ~((1l << 15) | (1 << 14) | (1 << 13) | (1 << 12) | (3 << 5) | (7 << 1) | (1 << 4)); - - if (is_write && access_control != 6) - temp |= 1 << 13; // read-only - // - if (access_control == 0 || access_control == 4) - temp |= 1l << 15; // not resident - else - temp |= 1 << 13; // read-only - - temp |= run_mode << 5; // TODO: kernel-mode or user-mode when a trap occurs in user-mode? - - temp |= apf << 1; // add current page - - temp |= d << 4; - - mmu_->setMMR0(temp); - - DOLOG(debug, false, "MMR0: %06o", temp); - } - - if (trap_action == T_TRAP_250) { - DOLOG(debug, false, "Page access %d (for virtual address %06o): trap 0250", access_control, a); - - c->trap(0250); // trap - - throw 5; - } - else { // T_ABORT_4 - DOLOG(debug, false, "Page access %d (for virtual address %06o): trap 004", access_control, a); - - c->trap(004); // abort - - throw 5; - } - } - } - - if (m_offset >= m->get_memory_size() && !is_io) [[unlikely]] { - DOLOG(debug, !peek_only, "bus::calculate_physical_address %o >= %o", m_offset, m->get_memory_size()); - DOLOG(debug, false, "TRAP(04) (throw 6) on address %06o", a); - - if (mmu_->is_locked() == false) { - uint16_t temp = mmu_->getMMR0(); - - temp &= 017777; - temp |= 1l << 15; // non-resident - - temp &= ~14; // add current page - temp |= apf << 1; - - temp &= ~(3 << 5); - temp |= run_mode << 5; - - mmu_->setMMR0(temp); - } - - if (is_write) - mmu_->set_page_trapped(run_mode, d, apf); - - c->trap(04); - - throw 6; - } - - uint16_t pdr_len = mmu_->get_pdr_len(run_mode, d, apf); - uint16_t pdr_cmp = (a >> 6) & 127; - - bool direction = mmu_->get_pdr_direction(run_mode, d, apf); - - // DOLOG(debug, false, "p_offset %06o pdr_len %06o direction %d, run_mode %d, apf %d, pdr: %06o", p_offset, pdr_len, direction, run_mode, apf, pages[run_mode][d][apf].pdr); - - if ((pdr_cmp > pdr_len && direction == false) || (pdr_cmp < pdr_len && direction == true)) { - DOLOG(debug, false, "bus::calculate_physical_address::p_offset %o versus %o direction %d", pdr_cmp, pdr_len, direction); - DOLOG(debug, false, "TRAP(0250) (throw 7) on address %06o", a); - c->trap(0250); // invalid access - - if (mmu_->is_locked() == false) { - uint16_t temp = mmu_->getMMR0(); - - temp &= 017777; - temp |= 1 << 14; // length - - temp &= ~14; // add current page - temp |= apf << 1; - - temp &= ~(3 << 5); - temp |= run_mode << 5; - - temp &= ~(1 << 4); - temp |= d << 4; - - mmu_->setMMR0(temp); - } - - if (is_write) - mmu_->set_page_trapped(run_mode, d, apf); - - throw 7; - } - } - - DOLOG(debug, false, "virtual address %06o maps to physical address %08o (run_mode: %d, apf: %d, par: %08o, poff: %o, AC: %d, %s)", a, m_offset, run_mode, apf, - mmu_->get_physical_memory_offset(run_mode, d, apf), - p_offset, mmu_->get_access_control(run_mode, d, apf), d ? "D" : "I"); - } - else { - // DOLOG(debug, false, "no MMU (read physical address %08o)", m_offset); - } - - return m_offset; -} - write_rc_t bus::write(const uint16_t addr_in, const word_mode_t word_mode, uint16_t value, const rm_selection_t mode_selection, const d_i_space_t space) { int run_mode = mode_selection == rm_cur ? c->getPSW_runmode() : c->getPSW_prev_runmode(); @@ -703,7 +549,7 @@ write_rc_t bus::write(const uint16_t addr_in, const word_mode_t word_mode, uint1 if (mmu_->is_enabled() && (addr_in & 1) == 0 /* TODO remove this? */ && addr_in != ADDR_MMR0) mmu_->set_page_written_to(run_mode, d, apf); - uint32_t m_offset = calculate_physical_address(run_mode, addr_in, true, true, false, space); + uint32_t m_offset = mmu_->calculate_physical_address(c, run_mode, addr_in, true, true, false, space); uint32_t io_base = mmu_->get_io_base(); bool is_io = m_offset >= io_base; diff --git a/bus.h b/bus.h index 119789c..25c515f 100644 --- a/bus.h +++ b/bus.h @@ -93,8 +93,6 @@ public: void set_memory_size(const int n_pages); - void mmudebug(const uint16_t a); - void add_ram (memory *const m ); void add_cpu (cpu *const c ); void add_mmu (mmu *const mmu_ ); @@ -132,7 +130,5 @@ public: bool is_psw(const uint16_t addr, const int run_mode, const d_i_space_t space) const; - uint32_t calculate_physical_address(const int run_mode, const uint16_t a, const bool trap_on_failure, const bool is_write, const bool peek_only, const d_i_space_t space); - void check_address(const bool trap_on_failure, const bool is_write, const memory_addresses_t & addr, const word_mode_t word_mode, const bool is_data, const int run_mode); }; diff --git a/console_ncurses.cpp b/console_ncurses.cpp index 7c2e8e8..2ceedf4 100644 --- a/console_ncurses.cpp +++ b/console_ncurses.cpp @@ -145,7 +145,7 @@ void console_ncurses::panel_update_thread() int run_mode = current_PSW >> 14; uint16_t current_PC = c->getPC(); - uint32_t full_addr = b->calculate_physical_address(run_mode, current_PC, false, false, true, i_space); + uint32_t full_addr = b->getMMU()->calculate_physical_address(c, run_mode, current_PC, false, false, true, i_space); uint16_t current_instr = b->read_word(current_PC); diff --git a/cpu.cpp b/cpu.cpp index d5be197..3d3fb84 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -6,11 +6,14 @@ #include #include +#include "breakpoint.h" +#include "bus.h" #include "cpu.h" #include "gen.h" #include "log.h" #include "utils.h" + #define SIGN(x, wm) ((wm) == wm_byte ? (x) & 0x80 : (x) & 0x8000) #define IS_0(x, wm) ((wm) == wm_byte ? ((x) & 0xff) == 0 : (x) == 0) @@ -1412,7 +1415,7 @@ bool cpu::single_operand_instructions(const uint16_t instr) auto a = getGAMAddress(dst_mode, dst_reg, wm_word); addToMMR1(a); - b->mmudebug(a.addr.value()); + b->getMMU()->mmudebug(a.addr.value()); a.mode_selection = rm_prev; a.space = word_mode == wm_byte ? d_space : i_space; diff --git a/cpu.h b/cpu.h index fa02705..9c5c40f 100644 --- a/cpu.h +++ b/cpu.h @@ -3,7 +3,8 @@ #pragma once -#include +#include +#include #include #include #include @@ -12,11 +13,12 @@ #include #include -#include "breakpoint.h" -#include "bus.h" #include "gen.h" +class breakpoint; +class bus; + constexpr const int initial_trap_delay = 8; constexpr const int max_stacktrace_depth = 16; diff --git a/mmu.cpp b/mmu.cpp index 3f28fe2..10f3890 100644 --- a/mmu.cpp +++ b/mmu.cpp @@ -16,8 +16,10 @@ mmu::~mmu() { } -void mmu::begin() +void mmu::begin(memory *const m) { + this->m = m; + reset(); } @@ -289,6 +291,162 @@ std::pair mmu::get_trap_action(const int run_mode, const boo return { trap_action, access_control }; } +void mmu::mmudebug(const uint16_t a) +{ + for(int rm=0; rm<4; rm++) { + auto ma = calculate_physical_address(rm, a); + + DOLOG(debug, false, "RM %d, a: %06o, apf: %d, PI: %08o (PSW: %d), PD: %08o (PSW: %d)", rm, ma.virtual_address, ma.apf, ma.physical_instruction, ma.physical_instruction_is_psw, ma.physical_data, ma.physical_data_is_psw); + } +} + +uint32_t mmu::calculate_physical_address(cpu *const c, const int run_mode, const uint16_t a, const bool trap_on_failure, const bool is_write, const bool peek_only, const d_i_space_t space) +{ + uint32_t m_offset = a; + + if (is_enabled() || (is_write && (getMMR0() & (1 << 8 /* maintenance check */)))) { + uint8_t apf = a >> 13; // active page field + + bool d = space == d_space && get_use_data_space(run_mode); + + uint16_t p_offset = a & 8191; // page offset + + m_offset = get_physical_memory_offset(run_mode, d, apf); + + m_offset += p_offset; + + if ((getMMR3() & 16) == 0) // off is 18bit + m_offset &= 0x3ffff; + + uint32_t io_base = get_io_base(); + bool is_io = m_offset >= io_base; + + if (trap_on_failure) [[unlikely]] { + { + auto rc = get_trap_action(run_mode, d, apf, is_write); + auto trap_action = rc.first; + int access_control = rc.second; + + if (trap_action != T_PROCEED) { + if (is_write) + set_page_trapped(run_mode, d, apf); + + if (is_locked() == false) { + uint16_t temp = getMMR0(); + + temp &= ~((1l << 15) | (1 << 14) | (1 << 13) | (1 << 12) | (3 << 5) | (7 << 1) | (1 << 4)); + + if (is_write && access_control != 6) + temp |= 1 << 13; // read-only + // + if (access_control == 0 || access_control == 4) + temp |= 1l << 15; // not resident + else + temp |= 1 << 13; // read-only + + temp |= run_mode << 5; // TODO: kernel-mode or user-mode when a trap occurs in user-mode? + + temp |= apf << 1; // add current page + + temp |= d << 4; + + setMMR0(temp); + + DOLOG(debug, false, "MMR0: %06o", temp); + } + + if (trap_action == T_TRAP_250) { + DOLOG(debug, false, "Page access %d (for virtual address %06o): trap 0250", access_control, a); + + c->trap(0250); // trap + + throw 5; + } + else { // T_ABORT_4 + DOLOG(debug, false, "Page access %d (for virtual address %06o): trap 004", access_control, a); + + c->trap(004); // abort + + throw 5; + } + } + } + + if (m_offset >= m->get_memory_size() && !is_io) [[unlikely]] { + DOLOG(debug, !peek_only, "mmu::calculate_physical_address %o >= %o", m_offset, m->get_memory_size()); + DOLOG(debug, false, "TRAP(04) (throw 6) on address %06o", a); + + if (is_locked() == false) { + uint16_t temp = getMMR0(); + + temp &= 017777; + temp |= 1l << 15; // non-resident + + temp &= ~14; // add current page + temp |= apf << 1; + + temp &= ~(3 << 5); + temp |= run_mode << 5; + + setMMR0(temp); + } + + if (is_write) + set_page_trapped(run_mode, d, apf); + + c->trap(04); + + throw 6; + } + + uint16_t pdr_len = get_pdr_len(run_mode, d, apf); + uint16_t pdr_cmp = (a >> 6) & 127; + + bool direction = get_pdr_direction(run_mode, d, apf); + + // DOLOG(debug, false, "p_offset %06o pdr_len %06o direction %d, run_mode %d, apf %d, pdr: %06o", p_offset, pdr_len, direction, run_mode, apf, pages[run_mode][d][apf].pdr); + + if ((pdr_cmp > pdr_len && direction == false) || (pdr_cmp < pdr_len && direction == true)) { + DOLOG(debug, false, "mmu::calculate_physical_address::p_offset %o versus %o direction %d", pdr_cmp, pdr_len, direction); + DOLOG(debug, false, "TRAP(0250) (throw 7) on address %06o", a); + c->trap(0250); // invalid access + + if (is_locked() == false) { + uint16_t temp = getMMR0(); + + temp &= 017777; + temp |= 1 << 14; // length + + temp &= ~14; // add current page + temp |= apf << 1; + + temp &= ~(3 << 5); + temp |= run_mode << 5; + + temp &= ~(1 << 4); + temp |= d << 4; + + setMMR0(temp); + } + + if (is_write) + set_page_trapped(run_mode, d, apf); + + throw 7; + } + } + + DOLOG(debug, false, "virtual address %06o maps to physical address %08o (run_mode: %d, apf: %d, par: %08o, poff: %o, AC: %d, %s)", a, m_offset, run_mode, apf, + get_physical_memory_offset(run_mode, d, apf), + p_offset, get_access_control(run_mode, d, apf), d ? "D" : "I"); + } + else { + // DOLOG(debug, false, "no MMU (read physical address %08o)", m_offset); + } + + return m_offset; +} + #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 { @@ -342,10 +500,10 @@ void mmu::set_par_pdr(const json_t *const j_in, const int run_mode, const bool 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 *mmu::deserialize(const json_t *const j, memory *const mem) { mmu *m = new mmu(); - m->begin(); + m->begin(mem); for(int run_mode=0; run_mode<4; run_mode++) { if (run_mode == 2) diff --git a/mmu.h b/mmu.h index 47f43d0..9de27e5 100644 --- a/mmu.h +++ b/mmu.h @@ -3,8 +3,11 @@ #include #include -#include "device.h" #include "gen.h" +#include "cpu.h" +#include "device.h" +#include "memory.h" + #define ADDR_PDR_SV_START 0172200 #define ADDR_PDR_SV_END 0172240 @@ -52,6 +55,8 @@ private: uint16_t PIR { 0 }; uint16_t CSR { 0 }; + memory *m { nullptr }; + #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); @@ -61,13 +66,15 @@ public: mmu(); virtual ~mmu(); - void begin(); + void begin(memory *const m); #if IS_POSIX json_t *serialize() const; - static mmu *deserialize(const json_t *const j); + static mmu *deserialize(const json_t *const j, memory *const m); #endif + void mmudebug(const uint16_t a); + void reset() override; bool is_enabled() const { return MMR0 & 1; } @@ -84,6 +91,7 @@ public: memory_addresses_t calculate_physical_address(const int run_mode, const uint16_t a) const; std::pair get_trap_action(const int run_mode, const bool d, const int apf, const bool is_write); + uint32_t calculate_physical_address(cpu *const c, const int run_mode, const uint16_t a, const bool trap_on_failure, const bool is_write, const bool peek_only, const d_i_space_t space); uint16_t getMMR0() const { return MMR0; } uint16_t getMMR1() const { return MMR1; } From e0d9b0363a1907684ffc695ed2f9750f722a74c1 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Fri, 3 May 2024 20:11:05 +0200 Subject: [PATCH 12/13] code clean-up --- bus.h | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/bus.h b/bus.h index 25c515f..f9a4454 100644 --- a/bus.h +++ b/bus.h @@ -111,24 +111,18 @@ public: rl02 *getRL02() { return rl02_; } tm_11 *getTM11() { return tm11; } - 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 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 read_byte(const uint16_t a) { return read(a, wm_byte, rm_cur); } uint16_t read_word(const uint16_t a, const d_i_space_t s = i_space); uint16_t peekWord(const uint16_t a); - uint8_t readUnibusByte(const uint32_t a); - void writeUnibusByte(const uint32_t a, const uint8_t value); - - write_rc_t write (const uint16_t a, const word_mode_t word_mode, uint16_t value, const rm_selection_t mode_selection, const d_i_space_t s = i_space); - void write_byte(const uint16_t a, const uint8_t value) { write(a, wm_byte, value, rm_cur); } - void write_word(const uint16_t a, const uint16_t value, const d_i_space_t s = i_space); - uint16_t readPhysical(const uint32_t a); + + write_rc_t write(const uint16_t a, const word_mode_t word_mode, uint16_t value, const rm_selection_t mode_selection, const d_i_space_t s = i_space); + void writeUnibusByte(const uint32_t a, const uint8_t value); + void write_byte(const uint16_t a, const uint8_t value) { write(a, wm_byte, value, rm_cur); } + void write_word(const uint16_t a, const uint16_t value, const d_i_space_t s = i_space); void writePhysical(const uint32_t a, const uint16_t value); - void check_odd_addressing(const uint16_t a, const int run_mode, const d_i_space_t space, const bool is_write); - bool is_psw(const uint16_t addr, const int run_mode, const d_i_space_t space) const; - - void check_address(const bool trap_on_failure, const bool is_write, const memory_addresses_t & addr, const word_mode_t word_mode, const bool is_data, const int run_mode); }; From a545eed0adedf4359f1962312cf66d6f923e7485 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Fri, 3 May 2024 20:21:58 +0200 Subject: [PATCH 13/13] missing mmu-init --- bus.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bus.cpp b/bus.cpp index c89ea3f..b9b2dee 100644 --- a/bus.cpp +++ b/bus.cpp @@ -135,6 +135,8 @@ void bus::set_memory_size(const int n_pages) delete m; m = new memory(n_bytes); + mmu_->begin(m); + DOLOG(info, false, "Memory is now %u kB in size", n_bytes / 1024); }