From db82579d3c023c441c895d26d32de3fa039eafa4 Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Fri, 11 Nov 2022 16:28:55 -0800 Subject: Starting to write a real memory subsystem for biggolf. --- memory/dram.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'memory/dram.h') diff --git a/memory/dram.h b/memory/dram.h index f59c7a6..31aea12 100644 --- a/memory/dram.h +++ b/memory/dram.h @@ -21,6 +21,8 @@ namespace memory { typedef std::array page; + const unsigned int latency; + std::map image; struct response { @@ -38,11 +40,26 @@ namespace memory { infra::port *responsep = nullptr; }; - infra::port commandp; + infra::port *commandp = nullptr; + + unsigned int progress = 0; + std::optional current_command; + + dram(unsigned int latency=0) + : latency(latency) + { } void clock() { - if (commandp.can_read()) { - const auto &c = commandp.peek(); + if (!current_command.has_value() && commandp->can_read()) { + current_command = commandp->read(); + progress = latency; + } + if (current_command.has_value()) { + if (progress) { + --progress; + return; + } + const auto &c = *current_command; if (!c.responsep || c.responsep->can_write()) { auto page_address = c.line_address >> PAGE_LINES_LOG2; auto page_line = c.line_address & PAGE_LINE_OFFSET_MASK; @@ -76,7 +93,7 @@ namespace memory { c.responsep->write(std::move(r)); } } - commandp.discard(); + current_command.reset(); } } } @@ -92,5 +109,30 @@ namespace memory { return; } } + + unsigned int fetch(std::uint64_t address) const { + auto page_address = address >> PAGE_LINES_LOG2; + if (auto i = image.find(page_address); i != image.end()) { + auto &page = i->second; + auto line_address = (address & PAGE_LINE_OFFSET_MASK) >> LINE_BYTES_LOG2; + auto &line = page[line_address]; + auto word_address = address & LINE_BYTE_OFFSET_MASK; + return line[word_address]; + } + return 0; + } + + void store(std::uint64_t address, unsigned int value) { + auto page_address = address >> PAGE_LINES_LOG2; + auto [p, emplaced] = image.try_emplace(page_address); + auto &page = p->second; + if (emplaced) + for (unsigned int i = 0; i < PAGE_LINES; ++i) + page[i].fill(0); + auto line_address = (address & PAGE_LINE_OFFSET_MASK) >> LINE_BYTES_LOG2; + auto &line = page[line_address]; + auto word_address = address & LINE_BYTE_OFFSET_MASK; + line[word_address] = value; + } }; } -- cgit v1.2.3