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; }