Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@
build*
.vscode/
.cache/

callgrind.out.*
12 changes: 7 additions & 5 deletions cmake/comp_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,19 @@ set(GCC_WARNINGS
-flto-odr-type-merging
-fno-omit-frame-pointer)

function(apply_compiler_flags TARGET VISIBILIY)
function(apply_compiler_flags TARGET VISIBILITY)
# Add sanitizers
target_link_options(${TARGET} ${VISIBILIY} "$<$<CONFIG:Debug>:${SANITIZERS}>")
target_compile_options(${TARGET} ${VISIBILIY}
target_link_options(${TARGET} ${VISIBILITY} "$<$<CONFIG:Debug>:${SANITIZERS}>")
target_compile_options(${TARGET} ${VISIBILITY}
"$<$<CONFIG:Debug>:${SANITIZERS}>")

# Compile stuff
target_compile_options(${TARGET} ${VISIBILIY}
target_compile_options(${TARGET} ${VISIBILITY}
"$<$<CONFIG:Debug>:${COMMON_WARNINGS}>")
target_compile_options(${TARGET} ${VISIBILITY}
"$<$<CONFIG:Debug>:-g>")
target_compile_options(
${TARGET} ${VISIBILIY}
${TARGET} ${VISIBILITY}
"$<$<CXX_COMPILER_ID:GNU>:$<$<CONFIG:Debug>:${GCC_WARNINGS}>>")
endfunction()

Expand Down
7 changes: 6 additions & 1 deletion include/common/common.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ constexpr std::uint16_t kOffsetBits = 12;

constexpr std::string_view kCosimLoggerName = "cosim";

inline const auto &getCosimLogger() {
static auto coSimLogger = spdlog::get(kCosimLoggerName.data());
return coSimLogger;
}

template <typename... Args>
void cosimLog(fmt::format_string<Args...> str, Args &&...args) {
auto coSimLogger = spdlog::get(kCosimLoggerName.data());
const auto &coSimLogger = getCosimLogger();
if (coSimLogger) {
coSimLogger->info(str, std::forward<Args>(args)...);
}
Expand Down
11 changes: 11 additions & 0 deletions include/common/inst.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <string>
#include <vector>

#include <spdlog/fmt/fmt.h>

#include "common.hh"

namespace sim {
Expand All @@ -31,4 +33,13 @@ using BasicBlock = std::vector<Instruction>;

} // namespace sim

template <> struct fmt::formatter<sim::Instruction> {
constexpr auto parse(format_parse_context &ctx) { return ctx.end(); }

template <typename FormatContext>
auto format(const sim::Instruction &inst, FormatContext &ctx) const {
return fmt::format_to(ctx.out(), "{}", inst.str());
}
};

#endif // __INCLUDE_COMMON_INST_HH__
13 changes: 12 additions & 1 deletion include/common/state.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <string>
#include <vector>

#include <fmt/core.h>
#include <spdlog/fmt/fmt.h>
#include <spdlog/spdlog.h>

#include "common/common.hh"
Expand All @@ -29,7 +31,7 @@ public:
if (!regnum)
return;

cosimLog("x{}=0x{:08x}", regnum, val);
// cosimLog("x{}=0x{:08x}", regnum, val);
regs.at(regnum) = val;
}

Expand Down Expand Up @@ -60,4 +62,13 @@ struct State final {

} // namespace sim

template <> struct fmt::formatter<sim::RegFile> {
constexpr auto parse(format_parse_context &ctx) { return ctx.end(); }

template <typename FormatContext>
auto format(const sim::RegFile &regFile, FormatContext &ctx) const {
return fmt::format_to(ctx.out(), "{}", regFile.str());
}
};

#endif // __INCLUDE_COMMON_STATE_HH__
17 changes: 9 additions & 8 deletions include/executor/executor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ concept InstForwardIterator = std::input_iterator<T> &&

class Executor final {
public:
using ExecutorMap =
std::unordered_map<OpType, void (*)(const Instruction &, State &)>;

Executor() = default;
Executor(const Executor &) = delete;
Executor(Executor &&) = delete;
Expand All @@ -30,20 +33,18 @@ public:
template <InstForwardIterator It>
void execute(It begin, It end, State &state) {
std::for_each(begin, end, [this, &state](const auto &inst) {
cosimLog("-----------------------");
cosimLog("NUM={}", instrCount);
// cosimLog("-----------------------");
// cosimLog("NUM={}", instrCount);
this->execute(inst, state);
cosimLog("PC=0x{:08x}", state.pc);
spdlog::trace("Instruction:\n [0x{:08x}]{}", state.pc, inst.str());
spdlog::trace("Current regfile state:\n{}", state.regs.str());
// cosimLog("PC=0x{:08x}", state.pc);
// spdlog::trace("Instruction:\n [0x{:08x}]{}", state.pc, inst);
// spdlog::trace("Current regfile state:\n{}", state.regs);
this->instrCount += 1;
});
}

private:
static const std::unordered_map<
OpType, std::function<void(const Instruction, State &)>>
execMap_;
static const ExecutorMap execMap_;
std::size_t instrCount{1};
};

Expand Down
51 changes: 27 additions & 24 deletions include/memory/memory.hh
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,7 @@ public:
uint16_t offset{};

AddrSections(uint32_t pt, uint16_t off) : indexPt(pt), offset(off) {}
AddrSections(Addr addr) {
constexpr std::pair<uint8_t, uint8_t> of_bits{kOffsetBits - 1, 0};
constexpr std::pair<uint8_t, uint8_t> pt_bits{sizeofBits<Addr>() - 1,
kOffsetBits};
indexPt = getBits<pt_bits.first, pt_bits.second>(addr);
offset =
static_cast<uint16_t>(getBits<of_bits.first, of_bits.second>(addr));
}
AddrSections(Addr addr) : AddrSections(getPt(addr), getOffset(addr)) {}
bool operator==(const AddrSections &) const = default;
};

Expand All @@ -102,7 +95,8 @@ public:
template <MemoryOp op> PagePtr pageTableLookup(const AddrSections &sect);

template <isSimType T, PhysMemory::MemoryOp op> T *getEntity(Addr addr);
uint16_t getOffset(Addr addr);
constexpr static uint16_t getOffset(Addr addr);
constexpr static uint8_t getPt(Addr addr);

private:
PT pageTable{};
Expand Down Expand Up @@ -149,19 +143,22 @@ public:

template <isSimType T, PhysMemory::MemoryOp op>
inline T *PhysMemory::getEntity(Addr addr) {
if (addr % sizeof(T) || ((getOffset(addr) + sizeof(T)) > (1 << kOffsetBits)))
auto offset = getOffset(addr);

if (addr % sizeof(T) || ((offset + sizeof(T)) > (1 << kOffsetBits)))
throw PhysMemory::MisAlignedAddrException(
"Misaligned memory access is not supported!");
AddrSections sections(addr);
auto offset = sections.offset;

auto isInTLB = tlb.tlbLookup(addr);
PagePtr page;

if (isInTLB) {
page = isInTLB;
} else {
page = PhysMemory::pageTableLookup<op>(sections);
page = PhysMemory::pageTableLookup<op>({getPt(addr), offset});
tlb.tlbUpdate(addr, page);
}

Word *word = &page->wordStorage.at(offset / sizeof(Word));
Byte *byte = reinterpret_cast<Byte *>(word) + (offset % sizeof(Word));
return reinterpret_cast<T *>(byte);
Expand All @@ -172,16 +169,16 @@ PagePtr PhysMemory::pageTableLookup(const AddrSections &sect) {

using MemOp = PhysMemory::MemoryOp;
auto index = sect.indexPt;
auto it_PT = pageTable.find(index);
if (it_PT == pageTable.end()) {
if constexpr (op == MemOp::LOAD)
auto [it_PT, inserted] = pageTable.try_emplace(index);

if constexpr (op == MemOp::LOAD) {
if (inserted) {
throw PhysMemory::PageFaultException(
"Load on unmapped region in physical mem");
else {
pageTable[index] = Page();
}
}
return &pageTable.at(index);

return &it_PT->second;
}

template <std::forward_iterator It>
Expand All @@ -201,10 +198,15 @@ inline void Memory::printMemStats(std::ostream &ost) const {

inline const Memory::MemoryStats &Memory::getMemStats() const { return stats; }

inline uint16_t PhysMemory::getOffset(Addr addr) {
constexpr inline uint16_t PhysMemory::getOffset(Addr addr) {
return static_cast<uint16_t>(getBits<kOffsetBits - 1, 0>(addr));
}

constexpr inline uint8_t PhysMemory::getPt(Addr addr) {
return static_cast<uint8_t>(
getBits<sizeofBits<Addr>() - 1, kOffsetBits>(addr));
}

inline Word Memory::loadWord(Addr addr) {
stats.numLoads++;
Word loadedWord = *physMem.getEntity<Word, PhysMemory::MemoryOp::LOAD>(addr);
Expand All @@ -216,7 +218,7 @@ inline void Memory::storeWord(Addr addr, Word word) {
stats.numStores++;
*physMem.getEntity<Word, PhysMemory::MemoryOp::STORE>(addr) = word;
if (isProgramStored) {
cosimLog("M[0x{:08x}]=0x{:08x}", addr, word);
// cosimLog("M[0x{:08x}]=0x{:08x}", addr, word);
}
}

Expand Down Expand Up @@ -257,18 +259,19 @@ inline PagePtr TLB::tlbLookup(Addr addr) {
stats.TLBMisses++;
return nullptr;
}
if (it->second.virtualAddress != addr) {
const auto &entry = it->second;
if (entry.virtualAddress != addr) {
stats.TLBMisses++;
return nullptr;
}
stats.TLBHits++;
return it->second.physPage;
return entry.physPage;
}

inline void TLB::tlbUpdate(Addr addr, PagePtr page) {

auto idx = getTLBIndex(addr);
tlb[idx] = TLBEntry(addr, page);
tlb.insert_or_assign(idx, TLBEntry(addr, page));
}

inline const TLB::TLBStats &TLB::getTLBStats() const { return stats; }
Expand Down
Loading