From 101f0facf8002907ca6e19faabfdcf472c0c3152 Mon Sep 17 00:00:00 2001 From: bd Date: Fri, 11 Apr 2025 21:14:25 -0400 Subject: Move source files to top-level src directory --- src/storage/cache.cc | 180 ------------------------------------------------- src/storage/dram.cc | 130 ----------------------------------- src/storage/storage.cc | 16 ----- 3 files changed, 326 deletions(-) delete mode 100644 src/storage/cache.cc delete mode 100644 src/storage/dram.cc delete mode 100644 src/storage/storage.cc (limited to 'src/storage') diff --git a/src/storage/cache.cc b/src/storage/cache.cc deleted file mode 100644 index 80f59ef..0000000 --- a/src/storage/cache.cc +++ /dev/null @@ -1,180 +0,0 @@ -#include "cache.h" -#include "definitions.h" -#include "response.h" -#include "utils.h" -#include -#include -#include - -Cache::Cache(Storage *lower, int delay) -{ - this->data = new std::vector>; - this->data->resize(L1_CACHE_LINES); - this->delay = delay; - this->is_waiting = false; - this->lower = lower; - this->meta.fill({-1, -1}); - this->requester = IDLE; - this->wait_time = this->delay; -} - -Cache::~Cache() -{ - delete this->lower; - delete this->data; -} - -Response Cache::write_word(Accessor accessor, signed int data, int address) -{ - return process(accessor, address, [&](int index, int offset) { - this->data->at(index).at(offset) = data; - this->meta[index].at(1) = 1; - }); -} - -Response Cache::write_line( - Accessor accessor, std::array data_line, int address) -{ - return process(accessor, address, [&](int index, int offset) { - (void)offset; - this->data->at(index) = data_line; - this->meta[index].at(1) = 1; - }); -} - -// TODO: tests for multi level cache -Response Cache::read_line( - Accessor accessor, - int address, - std::array &data_line) -{ - return process(accessor, address, [&](int index, int offset) { - (void)offset; - data_line = this->data->at(index); - }); -} - -Response Cache::read_word(Accessor accessor, int address, signed int &data) -{ - return process(accessor, address, [&](int index, int offset) { - data = this->data->at(index).at(offset); - }); -} - -Response Cache::process( - Accessor accessor, - int address, - std::function request_handler) -{ - Response r = this->is_access_cleared(accessor, address); - if (r == OK) { - int tag, index, offset; - get_cache_fields(address, &tag, &index, &offset); - request_handler(index, offset); - } - return r; -} - -Response Cache::is_access_cleared(Accessor accessor, int address) -{ - Response r; - r = WAIT; - /* Do this first--then process the first cycle immediately. */ - if (this->requester == IDLE) - this->requester = accessor; - if (this->requester == accessor) { - handle_miss(address); - if (this->is_waiting) - r = BLOCKED; - else if (this->wait_time == 0) { - this->requester = IDLE; - this->wait_time = delay; - r = OK; - } else { - --this->wait_time; - } - } - return r; -} - -void Cache::handle_miss(int expected) -{ - Response r, q; - int tag, index, offset; - std::array *actual; - std::array *meta; - - get_cache_fields(expected, &tag, &index, &offset); - r = OK; - meta = &this->meta.at(index); - actual = &this->data->at(index); - - if (meta->at(0) != tag) { - r = WAIT; - // address not in cache - if (meta->at(1) >= 0) { - // occupant is dirty - // writing line to DRam in case of dirty cache eviction - q = this->lower->write_line( - L1CACHE, *actual, - ((index << LINE_SPEC) + - (meta->at(0) << (L1_CACHE_LINE_SPEC + LINE_SPEC)))); - if (q == OK) { - meta->at(1) = -1; - } - } else { - q = this->lower->read_line(L1CACHE, expected, *actual); - if (q == OK) { - meta->at(0) = tag; - } - } - } - - this->is_waiting = (r == OK) ? false : true; -} - -std::array, L1_CACHE_LINES> Cache::get_meta() const -{ - std::array, L1_CACHE_LINES> ret; - std::copy(std::begin(this->meta), std::end(this->meta), std::begin(ret)); - return ret; -} - -std::ostream &operator<<(std::ostream &os, const Cache &c) -{ - const auto default_flags = std::cout.flags(); - const auto default_fill = std::cout.fill(); - - std::vector> data = - c.view(0, L1_CACHE_LINES); - std::array, L1_CACHE_LINES> meta = c.get_meta(); - - os << " " << std::setfill(' ') << std::setw(L1_CACHE_LINE_SPEC + 2) - << "INDEX" - << " | " << std::setfill(' ') << std::setw((8 + 3) * 4 - 1) << "DATA" - << " | " << std::setfill(' ') - << std::setw(MEM_LINE_SPEC - L1_CACHE_LINE_SPEC + 2) << "TAG" - << " | D" << std::endl; - for (int i = 0; i < L1_CACHE_LINES; ++i) { - os << " 0b" << std::setw(L1_CACHE_LINE_SPEC) - << std::bitset(i) << " | "; - for (int j = 0; j < LINE_SIZE; ++j) { - os << "0x" << std::setfill('0') << std::setw(8) << std::hex - << data.at(i).at(j) << " "; - } - os << "| 0b" << std::setfill(' '); - - if (meta.at(i)[0] < 0) - os << std::setfill('?') - << std::setw(MEM_LINE_SPEC - L1_CACHE_LINE_SPEC) << ""; - else - os << std::bitset( - meta.at(i)[0]); - - os << " | " << (int)(meta.at(i)[0] >= 0) << std::endl; - } - - std::cout.flags(default_flags); - std::cout.fill(default_fill); - return os; -} diff --git a/src/storage/dram.cc b/src/storage/dram.cc deleted file mode 100644 index f90f8db..0000000 --- a/src/storage/dram.cc +++ /dev/null @@ -1,130 +0,0 @@ -#include "dram.h" -#include "definitions.h" -#include "response.h" -#include -#include -#include -#include -#include -#include - -Dram::Dram(int delay) -{ - this->data = new std::vector>; - this->data->resize(MEM_LINES); - this->delay = delay; - this->is_waiting = false; - this->lower = nullptr; - this->requester = IDLE; - this->wait_time = this->delay; -} - -Dram::~Dram() { delete this->data; } - -Response Dram::write_line( - Accessor accessor, std::array data_line, int address) -{ - return process(accessor, address, [&](int line, int word) { - (void)word; - this->data->at(line) = data_line; - }); -} - -Response Dram::write_word(Accessor accessor, signed int data, int address) -{ - return process(accessor, address, [&](int line, int word) { - this->data->at(line).at(word) = data; - }); -} - -// TODO requires testing -Response Dram::read_line( - Accessor accessor, - int address, - std::array &data_line) -{ - return process(accessor, address, [&](int line, int word) { - (void)word; - data_line = this->data->at(line); - }); -} - -Response Dram::read_word(Accessor accessor, int address, signed int &data) -{ - return process(accessor, address, [&](int line, int word) { - data = this->data->at(line).at(word); - }); -} - -// TODO load a file instead and test this method -void Dram::load(std::vector program) { - unsigned long i; - for (i = 0; i < program.size(); ++i) { - int line, word; - get_memory_index(i, line, word); - this->data->at(line).at(word) = program[i]; - } -} - -Response Dram::process( - Accessor accessor, - int address, - std::function request_handler) -{ - Response r = this->is_access_cleared(accessor); - if (r == OK) { - int line, word; - get_memory_index(address, line, word); - request_handler(line, word); - } - return r; -} - -Response Dram::is_access_cleared(Accessor accessor) -{ - Response r; - r = WAIT; - /* Do this first--then process the first cycle immediately. */ - if (accessor == SIDE) - r = OK; - else { - if (this->requester == IDLE) - this->requester = accessor; - if (this->requester == accessor) { - if (this->wait_time == 0) { - this->requester = IDLE; - this->wait_time = delay; - r = OK; - } else { - --this->wait_time; - } - } - } - return r; -} - -std::ostream &operator<<(std::ostream &os, const Dram &d) -{ - const auto default_flags = std::cout.flags(); - const auto default_fill = std::cout.fill(); - - std::vector> data = d.view(0, MEM_LINES); - - os << " " << std::setfill(' ') << std::setw(MEM_LINE_SPEC + 2 + LINE_SPEC) - << "ADDRESS" - << " | " << std::setfill(' ') << std::setw((8 + 3) * 4 - 1) << "DATA" - << std::endl; - for (int i = 0; i < MEM_LINES; ++i) { - os << " 0b" << std::setw(MEM_LINE_SPEC + LINE_SPEC) << left - << std::bitset(i) << " | "; - for (int j = 0; j < LINE_SIZE; ++j) { - os << "0x" << std::setfill('0') << std::setw(8) << std::hex - << data.at(i).at(j) << ' '; - } - os << std::endl; - } - - std::cout.flags(default_flags); - std::cout.fill(default_fill); - return os; -} diff --git a/src/storage/storage.cc b/src/storage/storage.cc deleted file mode 100644 index fed607b..0000000 --- a/src/storage/storage.cc +++ /dev/null @@ -1,16 +0,0 @@ -#include "storage.h" -#include "definitions.h" -#include - -std::vector> -Storage::view(int base, int lines) const -{ - base = (base / LINE_SIZE) * LINE_SIZE; - std::vector> ret(lines + 1); - std::copy( - this->data->begin() + base, this->data->begin() + base + lines, - ret.begin()); - return ret; -} - -Storage *Storage::get_lower() { return this->lower; } -- cgit v1.2.3