From 608eed5c16366cae251f5d12ad1ed0b988b3ce46 Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Wed, 6 Feb 2019 14:42:49 +0100 Subject: Implement simple address-space ranges registration and example peripheral. Signed-off-by: Pavel Pisa --- qtmips_machine/cache.cpp | 8 +-- qtmips_machine/cache.h | 4 +- qtmips_machine/peripheral.cpp | 62 +++++++++++++++++++ qtmips_machine/peripheral.h | 60 +++++++++++++++++++ qtmips_machine/physaddrspace.cpp | 121 ++++++++++++++++++++++++++++++++++++++ qtmips_machine/physaddrspace.h | 76 ++++++++++++++++++++++++ qtmips_machine/qtmips_machine.pro | 8 ++- qtmips_machine/qtmipsmachine.cpp | 13 +++- qtmips_machine/qtmipsmachine.h | 3 + 9 files changed, 345 insertions(+), 10 deletions(-) create mode 100644 qtmips_machine/peripheral.cpp create mode 100644 qtmips_machine/peripheral.h create mode 100644 qtmips_machine/physaddrspace.cpp create mode 100644 qtmips_machine/physaddrspace.h diff --git a/qtmips_machine/cache.cpp b/qtmips_machine/cache.cpp index 7925dc0..e358986 100644 --- a/qtmips_machine/cache.cpp +++ b/qtmips_machine/cache.cpp @@ -37,7 +37,7 @@ using namespace machine; -Cache::Cache(Memory *m, const MachineConfigCache *cc, unsigned memory_access_penalty_r, unsigned memory_access_penalty_w) : cnf(cc) { +Cache::Cache(MemoryAccess *m, const MachineConfigCache *cc, unsigned memory_access_penalty_r, unsigned memory_access_penalty_w) : cnf(cc) { mem = m; access_pen_r = memory_access_penalty_r; access_pen_w = memory_access_penalty_w; @@ -89,7 +89,7 @@ bool Cache::wword(std::uint32_t address, std::uint32_t value) { changed = access(address, &data, true, value); if (cnf.write_policy() == MachineConfigCache::WP_TROUGH) - return mem->wword(address, value); + return mem->write_word(address, value); return changed; } @@ -241,7 +241,7 @@ bool Cache::access(std::uint32_t address, std::uint32_t *data, bool write, std:: emit miss_update(miss()); update_statistics(); for (unsigned i = 0; i < cnf.blocks(); i++) - cd.data[i] = mem->rword(base_address(tag, row) + (4*i)); + cd.data[i] = mem->read_word(base_address(tag, row) + (4*i)); } // Update replc @@ -274,7 +274,7 @@ void Cache::kick(unsigned associat_indx, unsigned row) const { struct cache_data &cd = dt[associat_indx][row]; if (cd.dirty && cnf.write_policy() == MachineConfigCache::WP_BACK) for (unsigned i = 0; i < cnf.blocks(); i++) - mem->wword(base_address(cd.tag, row) + (4*i), cd.data[i]); + mem->write_word(base_address(cd.tag, row) + (4*i), cd.data[i]); cd.valid = false; cd.dirty = false; diff --git a/qtmips_machine/cache.h b/qtmips_machine/cache.h index 5e19e10..bb089fe 100644 --- a/qtmips_machine/cache.h +++ b/qtmips_machine/cache.h @@ -46,7 +46,7 @@ namespace machine { class Cache : public MemoryAccess { Q_OBJECT public: - Cache(Memory *m, const MachineConfigCache *c, unsigned memory_access_penalty_r = 1, unsigned memory_access_penalty_w = 1); + Cache(MemoryAccess *m, const MachineConfigCache *c, unsigned memory_access_penalty_r = 1, unsigned memory_access_penalty_w = 1); bool wword(std::uint32_t address, std::uint32_t value); std::uint32_t rword(std::uint32_t address) const; @@ -72,7 +72,7 @@ signals: private: MachineConfigCache cnf; - Memory *mem; + MemoryAccess *mem; unsigned access_pen_r, access_pen_w; std::uint32_t uncached_start; std::uint32_t uncached_last; diff --git a/qtmips_machine/peripheral.cpp b/qtmips_machine/peripheral.cpp new file mode 100644 index 0000000..161f760 --- /dev/null +++ b/qtmips_machine/peripheral.cpp @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#include "peripheral.h" + +using namespace machine; + +SimplePeripheral::SimplePeripheral() { + +} + +SimplePeripheral::~SimplePeripheral() { + +} + +bool SimplePeripheral::wword(std::uint32_t address, std::uint32_t value) { +#if 0 + printf("SimplePeripheral::wword address 0x%08lx data 0x%08lx\n", + (unsigned long)address, (unsigned long)value); +#endif + return true; +} + +std::uint32_t SimplePeripheral::rword(std::uint32_t address) const { +#if 0 + printf("SimplePeripheral::rword address 0x%08lx\n", + (unsigned long)address); +#endif + return 0x12345678; +} diff --git a/qtmips_machine/peripheral.h b/qtmips_machine/peripheral.h new file mode 100644 index 0000000..dfc4db7 --- /dev/null +++ b/qtmips_machine/peripheral.h @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#ifndef SIMPLEPERIPHERAL_H +#define SIMPLEPERIPHERAL_H + +#include +#include +#include +#include +#include "machinedefs.h" +#include "memory.h" + +namespace machine { + +class SimplePeripheral : public MemoryAccess { + Q_OBJECT +public: + SimplePeripheral(); + ~SimplePeripheral(); + + bool wword(std::uint32_t address, std::uint32_t value); + std::uint32_t rword(std::uint32_t address) const; +}; + +} + +#endif // SIMPLEPERIPHERAL_H diff --git a/qtmips_machine/physaddrspace.cpp b/qtmips_machine/physaddrspace.cpp new file mode 100644 index 0000000..1c96d55 --- /dev/null +++ b/qtmips_machine/physaddrspace.cpp @@ -0,0 +1,121 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#include "physaddrspace.h" + +using namespace machine; + +PhysAddrSpace::PhysAddrSpace() { + +} + +PhysAddrSpace::~PhysAddrSpace() { + while (!ranges_by_access.isEmpty()) { + RangeDesc *p_range = ranges_by_access.first(); + ranges_by_addr.remove(p_range->last_addr); + ranges_by_access.remove(p_range->mem_acces); + if (p_range->owned) + delete p_range->mem_acces; + delete p_range; + } +} + +bool PhysAddrSpace::wword(std::uint32_t address, std::uint32_t value) { + RangeDesc *p_range = find_range(address); + if (p_range == nullptr) + return false; + return p_range->mem_acces->write_word(address - p_range->start_addr, value); +} + +std::uint32_t PhysAddrSpace::rword(std::uint32_t address) const { + const RangeDesc *p_range = find_range(address); + if (p_range == nullptr) + return 0x00000000; + return p_range->mem_acces->read_word(address - p_range->start_addr); +} + +PhysAddrSpace::RangeDesc *PhysAddrSpace::find_range(std::uint32_t address) const { + PhysAddrSpace::RangeDesc *p_range; + auto i = ranges_by_addr.lowerBound(address); + if (i == ranges_by_addr.end()) + return nullptr; + p_range = i.value(); + if (address >= p_range->start_addr && address <= p_range->last_addr) + return p_range; + return nullptr; +} + +bool PhysAddrSpace::insert_range(MemoryAccess *mem_acces, std::uint32_t start_addr, std::uint32_t last_addr, bool move_ownership) { + RangeDesc *p_range = new RangeDesc(mem_acces, start_addr, last_addr, move_ownership); + auto i = ranges_by_addr.lowerBound(start_addr); + if (i != ranges_by_addr.end()) { + if (i.value()->start_addr <= last_addr && i.value()->last_addr >= start_addr) + return false; + } + ranges_by_addr.insert(last_addr, p_range); + ranges_by_access.insert(mem_acces, p_range); + return true; +} + +bool PhysAddrSpace::remove_range(MemoryAccess *mem_acces) { + RangeDesc *p_range = ranges_by_access.take(mem_acces); + if (p_range == nullptr) + return false; + ranges_by_addr.remove(p_range->last_addr); + if (p_range->owned) + delete p_range->mem_acces; + delete p_range; + return true; +} + +void PhysAddrSpace::clean_range(std::uint32_t start_addr, std::uint32_t last_addr) { + auto i = ranges_by_addr.lowerBound(start_addr); + while (i != ranges_by_addr.end()) { + RangeDesc *p_range = i.value(); + i++; + if (p_range->start_addr <= last_addr) + remove_range(p_range->mem_acces); + else + break; + } +} + +PhysAddrSpace::RangeDesc::RangeDesc(MemoryAccess *mem_acces, std::uint32_t start_addr, std::uint32_t last_addr, bool owned) { + this->mem_acces = mem_acces; + this->start_addr = start_addr; + this->last_addr = last_addr; + this->owned = owned; +} + diff --git a/qtmips_machine/physaddrspace.h b/qtmips_machine/physaddrspace.h new file mode 100644 index 0000000..b30eb05 --- /dev/null +++ b/qtmips_machine/physaddrspace.h @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#ifndef PHYSADDRSPACE_H +#define PHYSADDRSPACE_H + +#include +#include +#include +#include +#include "machinedefs.h" +#include "memory.h" + +namespace machine { + +class PhysAddrSpace : public MemoryAccess { + Q_OBJECT +public: + PhysAddrSpace(); + ~PhysAddrSpace(); + + bool wword(std::uint32_t address, std::uint32_t value); + std::uint32_t rword(std::uint32_t address) const; + + bool insert_range(MemoryAccess *mem_acces, std::uint32_t start_addr, std::uint32_t last_addr, bool move_ownership); + bool remove_range(MemoryAccess *mem_acces); + void clean_range(std::uint32_t start_addr, std::uint32_t last_addr); +private: + class RangeDesc { + public: + RangeDesc(MemoryAccess *mem_acces, std::uint32_t start_addr, std::uint32_t last_addr, bool owned); + std::uint32_t start_addr; + std::uint32_t last_addr; + MemoryAccess *mem_acces; + bool owned; + }; + QMap ranges_by_addr; + QMap ranges_by_access; + RangeDesc *find_range(std::uint32_t address) const; +}; + +} + +#endif // PHYSADDRSPACE_H diff --git a/qtmips_machine/qtmips_machine.pro b/qtmips_machine/qtmips_machine.pro index eae7b6e..286847f 100644 --- a/qtmips_machine/qtmips_machine.pro +++ b/qtmips_machine/qtmips_machine.pro @@ -23,7 +23,9 @@ SOURCES += \ cache.cpp \ alu.cpp \ machineconfig.cpp \ - utils.cpp + utils.cpp \ + physaddrspace.cpp \ + peripheral.cpp HEADERS += \ qtmipsmachine.h \ @@ -37,4 +39,6 @@ HEADERS += \ alu.h \ machineconfig.h \ utils.h \ - machinedefs.h + machinedefs.h \ + physaddrspace.h \ + peripheral.h diff --git a/qtmips_machine/qtmipsmachine.cpp b/qtmips_machine/qtmipsmachine.cpp index 51e5580..0f65609 100644 --- a/qtmips_machine/qtmipsmachine.cpp +++ b/qtmips_machine/qtmipsmachine.cpp @@ -39,6 +39,7 @@ using namespace machine; QtMipsMachine::QtMipsMachine(const MachineConfig &cc) : QObject(), mcnf(&cc) { + MemoryAccess *cpu_mem; stat = ST_READY; ProgramLoader program(cc.elf()); @@ -49,8 +50,16 @@ QtMipsMachine::QtMipsMachine(const MachineConfig &cc) : QObject(), mcnf(&cc) { if (program.get_executable_entry()) regs->pc_abs_jmp(program.get_executable_entry()); mem = new Memory(*mem_program_only); - cch_program = new Cache(mem, &cc.cache_program(), cc.memory_access_time_read(), cc.memory_access_time_write()); - cch_data = new Cache(mem, &cc.cache_data(), cc.memory_access_time_read(), cc.memory_access_time_write()); + cpu_mem = mem; +#if 1 + physaddrspace = new PhysAddrSpace(); + physaddrspace->insert_range(mem, 0x00000000, 0xefffffff, false); + MemoryAccess *periph = new SimplePeripheral(); + physaddrspace->insert_range(periph, 0xffffc000, 0xffffcfff, false); + cpu_mem = physaddrspace; +#endif + cch_program = new Cache(cpu_mem, &cc.cache_program(), cc.memory_access_time_read(), cc.memory_access_time_write()); + cch_data = new Cache(cpu_mem, &cc.cache_data(), cc.memory_access_time_read(), cc.memory_access_time_write()); if (cc.pipelined()) cr = new CorePipelined(regs, cch_program, cch_data, cc.hazard_unit()); diff --git a/qtmips_machine/qtmipsmachine.h b/qtmips_machine/qtmipsmachine.h index a3ecea7..fd7dadf 100644 --- a/qtmips_machine/qtmipsmachine.h +++ b/qtmips_machine/qtmipsmachine.h @@ -45,6 +45,8 @@ #include #include #include +#include +#include namespace machine { @@ -92,6 +94,7 @@ private: Registers *regs; Memory *mem, *mem_program_only; + PhysAddrSpace *physaddrspace; Cache *cch_program, *cch_data; Core *cr; -- cgit v1.2.3