Skip to content

Commit

Permalink
micro opt
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertvanheusden committed Jun 23, 2024
1 parent b17129b commit b84359c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
24 changes: 16 additions & 8 deletions bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ bus *bus::deserialize(const JsonDocument j, console *const cnsl, std::atomic_uin
if (j.containsKey("tty"))
b->add_tty(tty::deserialize(j["tty"], b, cnsl));

if (j.containsKey("mmu"))
b->add_mmu(mmu::deserialize(j["mmu"], m));
cpu *c = nullptr;
if (j.containsKey("cpu")) {
c = cpu::deserialize(j["cpu"], b, event);
b->add_cpu(c);
}

if (j.containsKey("cpu"))
b->add_cpu(cpu::deserialize(j["cpu"], b, event));
if (j.containsKey("mmu"))
b->add_mmu(mmu::deserialize(j["mmu"], m, c));

if (j.containsKey("rl02"))
b->add_rl02(rl02::deserialize(j["rl02"], b));
Expand Down Expand Up @@ -135,7 +138,7 @@ void bus::set_memory_size(const int n_pages)
delete m;
m = new memory(n_bytes);

mmu_->begin(m);
mmu_->begin(m, c);

TRACE("Memory is now %u kB in size", n_bytes / 1024);
}
Expand Down Expand Up @@ -181,19 +184,24 @@ void bus::add_ram(memory *const m)
delete this->m;
this->m = m;

mmu_->begin(m);
mmu_->begin(m, c);
}

void bus::add_mmu(mmu *const mmu_)
{
delete this->mmu_;
this->mmu_ = mmu_;

mmu_->begin(m, c);
}

void bus::add_cpu(cpu *const c)
{
delete this->c;
this->c = c;

if (mmu_)
mmu_->begin(m, c);
}

void bus::add_tm11(tm_11 *const tm11)
Expand Down Expand Up @@ -242,7 +250,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 = mmu_->calculate_physical_address(c, run_mode, addr_in, false, space);
uint32_t m_offset = mmu_->calculate_physical_address(run_mode, addr_in, false, space);

uint32_t io_base = mmu_->get_io_base();
bool is_io = m_offset >= io_base;
Expand Down Expand Up @@ -552,7 +560,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 = mmu_->calculate_physical_address(c, run_mode, addr_in, true, space);
uint32_t m_offset = mmu_->calculate_physical_address(run_mode, addr_in, true, space);

uint32_t io_base = mmu_->get_io_base();
bool is_io = m_offset >= io_base;
Expand Down
21 changes: 11 additions & 10 deletions mmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ mmu::~mmu()
{
}

void mmu::begin(memory *const m)
void mmu::begin(memory *const m, cpu *const c)
{
this->m = m;
this->c = c;

reset();
}
Expand Down Expand Up @@ -354,7 +355,7 @@ void mmu::mmudebug(const uint16_t a)
}
}

void mmu::verify_page_access(cpu *const c, const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write)
void mmu::verify_page_access(const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write)
{
const auto [ trap_action, access_control ] = get_trap_action(run_mode, d, apf, is_write);
if (trap_action == T_PROCEED)
Expand Down Expand Up @@ -403,7 +404,7 @@ void mmu::verify_page_access(cpu *const c, const uint16_t virt_addr, const int r
}
}

void mmu::verify_access_valid(cpu *const c, const uint32_t m_offset, const int run_mode, const bool d, const int apf, const bool is_io, const bool is_write)
void mmu::verify_access_valid(const uint32_t m_offset, const int run_mode, const bool d, const int apf, const bool is_io, const bool is_write)
{
if (m_offset >= m->get_memory_size() && !is_io) [[unlikely]] {
TRACE("TRAP(04) (throw 6) on address %08o", m_offset);
Expand Down Expand Up @@ -432,7 +433,7 @@ void mmu::verify_access_valid(cpu *const c, const uint32_t m_offset, const int r
}
}

void mmu::verify_page_length(cpu *const c, const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write)
void mmu::verify_page_length(const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write)
{
uint16_t pdr_len = get_pdr_len(run_mode, d, apf);
if (pdr_len == 127)
Expand Down Expand Up @@ -473,7 +474,7 @@ void mmu::verify_page_length(cpu *const c, const uint16_t virt_addr, const int r
}
}

uint32_t mmu::calculate_physical_address(cpu *const c, const int run_mode, const uint16_t a, const bool is_write, const d_i_space_t space)
uint32_t mmu::calculate_physical_address(const int run_mode, const uint16_t a, const bool is_write, const d_i_space_t space)
{
uint32_t m_offset = a;

Expand All @@ -490,15 +491,15 @@ uint32_t mmu::calculate_physical_address(cpu *const c, const int run_mode, const
if ((getMMR3() & 16) == 0) // off is 18bit
m_offset &= 0x3ffff;

verify_page_access(c, a, run_mode, d, apf, is_write);
verify_page_access(a, run_mode, d, apf, is_write);

// e.g. ram or i/o, not unmapped
uint32_t io_base = get_io_base();
bool is_io = m_offset >= io_base;

verify_access_valid(c, m_offset, run_mode, d, apf, is_io, is_write);
verify_access_valid(m_offset, run_mode, d, apf, is_io, is_write);

verify_page_length(c, a, run_mode, d, apf, is_write);
verify_page_length(a, run_mode, d, apf, is_write);
}

return m_offset;
Expand Down Expand Up @@ -559,10 +560,10 @@ void mmu::set_par_pdr(const JsonVariantConst j_in, const int run_mode, const boo
pages[run_mode][is_d][i_pdr++].pdr = v;
}

mmu *mmu::deserialize(const JsonVariantConst j, memory *const mem)
mmu *mmu::deserialize(const JsonVariantConst j, memory *const mem, cpu *const c)
{
mmu *m = new mmu();
m->begin(mem);
m->begin(mem, c);

for(int run_mode=0; run_mode<4; run_mode++) {
if (run_mode == 2)
Expand Down
13 changes: 7 additions & 6 deletions mmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,23 @@ class mmu : public device
uint16_t CSR { 0 };

memory *m { nullptr };
cpu *c { nullptr };

JsonDocument add_par_pdr(const int run_mode, const bool is_d) const;
void set_par_pdr(const JsonVariantConst j_in, const int run_mode, const bool is_d);

void verify_page_access (cpu *const c, const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write);
void verify_access_valid(cpu *const c, const uint32_t m_offset, const int run_mode, const bool d, const int apf, const bool is_io, const bool is_write);
void verify_page_length (cpu *const c, const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write);
void verify_page_access (const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write);
void verify_access_valid(const uint32_t m_offset, const int run_mode, const bool d, const int apf, const bool is_io, const bool is_write);
void verify_page_length (const uint16_t virt_addr, const int run_mode, const bool d, const int apf, const bool is_write);

public:
mmu();
virtual ~mmu();

void begin(memory *const m);
void begin(memory *const m, cpu *const c);

JsonDocument serialize() const;
static mmu *deserialize(const JsonVariantConst j, memory *const m);
static mmu *deserialize(const JsonVariantConst j, memory *const m, cpu *const c);

void mmudebug(const uint16_t a);

Expand All @@ -94,7 +95,7 @@ class mmu : public device

memory_addresses_t calculate_physical_address(const int run_mode, const uint16_t a) const;
std::pair<trap_action_t, int> 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 is_write, const d_i_space_t space);
uint32_t calculate_physical_address(const int run_mode, const uint16_t a, const bool is_write, const d_i_space_t space);

uint16_t getMMR0() const { return MMR0; }
uint16_t getMMR1() const { return MMR1; }
Expand Down

0 comments on commit b84359c

Please sign in to comment.