Skip to content

Commit

Permalink
Merge pull request #49 from cvut/csr-support
Browse files Browse the repository at this point in the history
CSR support with basic functionality tested on mscratch register.
  • Loading branch information
ppisa authored Oct 24, 2022
2 parents 4869107 + 4d5aa16 commit f225411
Show file tree
Hide file tree
Showing 29 changed files with 714 additions and 575 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Inline
AllowShortLambdasOnASingleLine: All
Expand Down
3 changes: 2 additions & 1 deletion src/assembler/simpleasm.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "simpleasm.h"

#include "machine/csr/register_desc.h"
#include "machine/memory/address.h"
#include "machine/memory/memory_utils.h"

Expand Down Expand Up @@ -254,7 +255,7 @@ bool SimpleAsm::process_line(
include_stack.removeLast();
return res;
}
if ((op == ".text") || (op == ".data") || (op == ".bss") || (op == ".globl") || (op == ".end")
if ((op == ".text") || (op == ".data") || (op == ".bss") || (op == ".globl") || (op == ".end")
|| (op == ".ent") || (op == ".option")) {
return true;
}
Expand Down
16 changes: 7 additions & 9 deletions src/cli/reporter.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "reporter.h"

#include <cinttypes>
#include <utility>

using namespace machine;
using namespace std;
Expand Down Expand Up @@ -47,7 +46,7 @@ constexpr const char *get_exception_name(ExceptionCause exception_cause) {
case EXCAUSE_OVERFLOW: return "OVERFLOW";
case EXCAUSE_TRAP: return "TRAP";
case EXCAUSE_HWBREAK: return "HWBREAK";
default: UNREACHABLE;
default: UNREACHABLE
}
}

Expand Down Expand Up @@ -89,21 +88,20 @@ void Reporter::report_regs() const {
for (unsigned i = 0; i < REGISTER_COUNT; i++) {
report_gp_reg(i, (i == REGISTER_COUNT - 1));
}
for (int i = 1; i < ControlState::CSR_REGS_CNT; i++) {
report_csr_reg(
static_cast<ControlState::CsrRegisters>(i), (i == ControlState::CSR_REGS_CNT - 1));
for (size_t i = 0; i < CSR::REGISTERS.size(); i++) {
report_csr_reg(i, (i == CSR::REGISTERS.size() - 1));
}
}

void Reporter::report_gp_reg(unsigned int i, bool last) const {
printf(
"R%u:0x%08" PRIx64 "%s", i, machine->registers()->read_gp(i).as_u64(), (last) ? "\n" : " ");
};
}

void Reporter::report_csr_reg(ControlState::CsrRegisters reg, bool last) const {
void Reporter::report_csr_reg(size_t internal_id, bool last) const {
printf(
"%s: 0x%08" PRIx64 "%s", ControlState::csr_name(reg).toLocal8Bit().data(),
machine->control_state()->read_csr(reg), (last) ? "\n" : " ");
"%s: 0x%08" PRIx64 "%s", CSR::REGISTERS[internal_id].name,
machine->control_state()->read_internal(internal_id).as_u64(), (last) ? "\n" : " ");
}

void Reporter::report_caches() const {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private slots:
void report_regs() const;
void report_caches() const;
void report_range(const DumpRange &range) const;
void report_csr_reg(machine::ControlState::CsrRegisters reg, bool last) const;
void report_csr_reg(size_t internal_id, bool last) const;
void report_gp_reg(unsigned int i, bool last) const;
static void report_cache(const char *cache_name, const machine::Cache &cache);
};
Expand Down
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_subdirectory(polyfills)

set(common_HEADERS
endian.h
string_utils.h
logging.h
logging_format_colors.h
math/bit_ops.h
Expand Down
7 changes: 6 additions & 1 deletion src/common/math/bit_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
#ifndef QTRVSIM_BIT_OPS_H
#define QTRVSIM_BIT_OPS_H

#include <cstdio>
#include <cassert>
#include <cstdint>
#include <limits>

using std::size_t;

/**
* Get value of single bit as lowest bit
Expand Down Expand Up @@ -43,6 +47,7 @@ constexpr inline T mask_bits(T val, size_t start, size_t end) {
*/
template<typename T>
constexpr inline T get_bits(T val, size_t start, size_t end) {
assert(start >= end);
return mask_bits(val >> end, start - end, 0);
}

Expand Down
13 changes: 13 additions & 0 deletions src/common/string_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef QTRVSIM_STRING_UTILS_H
#define QTRVSIM_STRING_UTILS_H

#include <QString>

namespace str {
template<typename T>
QString asHex(T number) {
return QString("0x%1").arg(number, 16);
}
} // namespace str

#endif // QTRVSIM_STRING_UTILS_H
2 changes: 1 addition & 1 deletion src/gui/coreview/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const struct {
{ QStringLiteral("alu-src1"), LENS(CoreState, pipeline.execute.internal.alu_src1) },
{ QStringLiteral("alu-src2"), LENS(CoreState, pipeline.execute.internal.alu_src2) },
{ QStringLiteral("decode-imm"), LENS(CoreState, pipeline.decode.result.immediate_val) },
{ QStringLiteral("exec-imm"), LENS(CoreState, pipeline.execute.internal.immediate) },
{ QStringLiteral("exec-imm"), LENS(CoreState, pipeline.execute.result.immediate_val) },
{ QStringLiteral("decode-inst-bus"), LENS(CoreState, pipeline.decode.internal.inst_bus) },
{ QStringLiteral("mem-write-val"),
LENS(CoreState, pipeline.memory.internal.mem_write_val) },
Expand Down
4 changes: 2 additions & 2 deletions src/gui/coreview/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ CoreViewScene::CoreViewScene(machine::Machine *machine, const QString &core_svg_
auto conn_trees = component.findAll<QGraphicsPathItem>("stroke-linecap", "round");
if (conn_trees.size() != 2) {
WARN(
"Mux2 does not have 2 connections found %d (source: \"%s\").",
"Mux2 does not have 2 connections found %lld (source: \"%s\").",
conn_trees.size(), qPrintable(source_name));
break;
}
Expand All @@ -112,7 +112,7 @@ CoreViewScene::CoreViewScene(machine::Machine *machine, const QString &core_svg_
auto conn_trees = component.findAll<QGraphicsPathItem>("stroke-linecap", "round");
if (conn_trees.size() != 3) {
WARN(
"Mux3 does not have 3 connections found %d (source: \"%s\").",
"Mux3 does not have 3 connections found %lld (source: \"%s\").",
conn_trees.size(), qPrintable(source_name));
break;
}
Expand Down
83 changes: 39 additions & 44 deletions src/gui/csrdock.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
#include "csrdock.h"

#include "controlstate.h"

CsrDock::CsrDock(QWidget *parent) : QDockWidget(parent) {
scrollarea = new QScrollArea(this);
scrollarea->setWidgetResizable(true);
widg = new StaticTable(scrollarea);

#define INIT(X, LABEL) \
do { \
(X) = new QLabel("0x00000000", widg); \
(X)->setFixedSize((X)->sizeHint()); \
(X)->setText(""); \
(X)->setTextInteractionFlags(Qt::TextSelectableByMouse); \
widg->addRow({ new QLabel(LABEL, widg), X }); \
#define INIT(X, LABEL, TOOLTIP) \
do { \
(X) = new QLabel("0x00000000", widg); \
(X)->setFixedSize((X)->sizeHint()); \
(X)->setText(""); \
(X)->setTextInteractionFlags(Qt::TextSelectableByMouse); \
auto desc_label = new QLabel(LABEL, widg); \
desc_label->setToolTip((TOOLTIP)); \
widg->addRow({ desc_label, X }); \
} while (false)

csr_view[0] = nullptr;
for (int i = 1; i < machine::ControlState::CSR_REGS_CNT; i++) {
INIT(csr_view[i], machine::ControlState::csr_name((machine::ControlState::CsrRegisters)i));
for (size_t i = 0; i < machine::CSR::REGISTERS.size(); i++) {
auto &desc = machine::CSR::REGISTERS.at(i);
INIT(
csr_view[i], QString(desc.name),
QString("%0 (0x%1)").arg(desc.description).arg(desc.address.data, 0, 16));
csr_highlighted[i] = false;
}
#undef INIT
Expand All @@ -36,7 +43,7 @@ CsrDock::CsrDock(QWidget *parent) : QDockWidget(parent) {
}

CsrDock::~CsrDock() {
for (int i = 1; i < machine::ControlState::CSR_REGS_CNT; i++) {
for (size_t i = 0; i < machine::CSR::REGISTERS.size(); i++) {
delete csr_view[i];
}
delete widg;
Expand All @@ -46,62 +53,50 @@ CsrDock::~CsrDock() {
void CsrDock::setup(machine::Machine *machine) {
if (machine == nullptr) {
// Reset data
for (int i = 1; i < machine::ControlState::CSR_REGS_CNT; i++) {
for (size_t i = 0; i < machine::CSR::REGISTERS.size(); i++) {
csr_view[i]->setText("");
}
return;
}

const machine::ControlState *controlst = machine->control_state();
const machine::CSR::ControlState *controlst = machine->control_state();

for (int i = 1; i < machine::ControlState::CSR_REGS_CNT; i++) {
labelVal(
csr_view[i],
controlst->read_csr((machine::ControlState::CsrRegisters)i));
for (size_t i = 0; i < machine::CSR::REGISTERS.size(); i++) {
labelVal(csr_view[i], controlst->read_internal(i).as_u64());
}

connect(
controlst, &machine::ControlState::csr_update, this,
&CsrDock::csr_changed);
connect(
controlst, &machine::ControlState::csr_read, this,
&CsrDock::csr_read);
connect(
machine, &machine::Machine::tick, this, &CsrDock::clear_highlights);
connect(controlst, &machine::CSR::ControlState::write_signal, this, &CsrDock::csr_changed);
connect(controlst, &machine::CSR::ControlState::read_signal, this, &CsrDock::csr_read);
connect(machine, &machine::Machine::tick, this, &CsrDock::clear_highlights);
}

void CsrDock::csr_changed(enum machine::ControlState::CsrRegisters reg,
uint64_t val) {
void CsrDock::csr_changed(size_t internal_reg_id, machine::RegisterValue val) {
// FIXME assert takes literal
SANITY_ASSERT(
(uint)reg < machine::ControlState::CSR_REGS_CNT && (uint)reg,
(uint)internal_reg_id < machine::CSR::REGISTERS.size(),
QString("CsrDock received signal with invalid CSR register: ")
+ QString::number((uint)reg));
labelVal(csr_view[(uint)reg], val);
csr_view[reg]->setPalette(pal_updated);
csr_highlighted[reg] = true;
+ QString::number((uint)internal_reg_id));
labelVal(csr_view[(uint)internal_reg_id], val.as_u64());
csr_view[internal_reg_id]->setPalette(pal_updated);
csr_highlighted[internal_reg_id] = true;
csr_highlighted_any = true;
}

void CsrDock::csr_read(
enum machine::ControlState::CsrRegisters reg,
uint64_t val) {
void CsrDock::csr_read(size_t internal_reg_id, machine::RegisterValue val) {
(void)val;
// FIXME assert takes literal
SANITY_ASSERT(
(uint)reg < machine::ControlState::CSR_REGS_CNT && (uint)reg,
(uint)internal_reg_id < machine::CSR::REGISTERS.size(),
QString("CsrDock received signal with invalid CSR register: ")
+ QString::number((uint)reg));
if (!csr_highlighted[reg]) {
csr_view[reg]->setPalette(pal_read);
}
csr_highlighted[reg] = true;
+ QString::number((uint)internal_reg_id));
if (!csr_highlighted[internal_reg_id]) { csr_view[internal_reg_id]->setPalette(pal_read); }
csr_highlighted[internal_reg_id] = true;
csr_highlighted_any = true;
}

void CsrDock::clear_highlights() {
if (!csr_highlighted_any) {
return;
}
for (int i = 1; i < machine::ControlState::CSR_REGS_CNT; i++) {
if (!csr_highlighted_any) { return; }
for (size_t i = 0; i < machine::CSR::REGISTERS.size(); i++) {
if (csr_highlighted[i]) {
csr_view[i]->setPalette(pal_normal);
csr_highlighted[i] = false;
Expand Down
9 changes: 5 additions & 4 deletions src/gui/csrdock.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef CSRDOCK_H
#define CSRDOCK_H

#include "machine/csr/register_desc.h"
#include "machine/machine.h"
#include "statictable.h"

Expand All @@ -20,16 +21,16 @@ class CsrDock : public QDockWidget {
void setup(machine::Machine *machine);

private slots:
void csr_changed(enum machine::ControlState::CsrRegisters reg, uint64_t val);
void csr_read(enum machine::ControlState::CsrRegisters reg, uint64_t val);
void csr_changed(size_t internal_reg_id, machine::RegisterValue val);
void csr_read(size_t internal_reg_id, machine::RegisterValue val);
void clear_highlights();

private:
StaticTable *widg;
QScrollArea *scrollarea;

QLabel *csr_view[machine::ControlState::CSR_REGS_CNT] {};
bool csr_highlighted[machine::ControlState::CSR_REGS_CNT] {};
QLabel *csr_view[machine::CSR::REGISTERS.size()] {};
bool csr_highlighted[machine::CSR::REGISTERS.size()] {};
bool csr_highlighted_any;

QPalette pal_normal;
Expand Down
Loading

0 comments on commit f225411

Please sign in to comment.