summaryrefslogtreecommitdiff
path: root/nandgame/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'nandgame/cpp')
-rw-r--r--nandgame/cpp/CMakeLists.txt39
-rw-r--r--nandgame/cpp/Vcomputer__main.cpp97
-rw-r--r--nandgame/cpp/disas.cpp212
-rw-r--r--nandgame/cpp/disas.h6
-rw-r--r--nandgame/cpp/simpc_config.h3
-rw-r--r--nandgame/cpp/simpc_types.h3
-rw-r--r--nandgame/cpp/simpc_ui.cpp173
-rw-r--r--nandgame/cpp/simpc_ui.h14
8 files changed, 547 insertions, 0 deletions
diff --git a/nandgame/cpp/CMakeLists.txt b/nandgame/cpp/CMakeLists.txt
new file mode 100644
index 0000000..6e9a4ab
--- /dev/null
+++ b/nandgame/cpp/CMakeLists.txt
@@ -0,0 +1,39 @@
+cmake_minimum_required(VERSION 3.20)
+
+project(simpc)
+
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
+
+find_package(verilator HINTS $ENV{VERILATOR_ROOT})
+
+find_package(Curses REQUIRED)
+
+if(Curses_FOUND AND NOT TARGET Curses::Curses)
+ add_library(Curses::Curses INTERFACE IMPORTED)
+ set_target_properties(
+ Curses::Curses
+ PROPERTIES
+ INTERFACE_LINK_LIBRARIES "${CURSES_LIBRARIES}"
+ INTERFACE_INCLUDE_DIRECTORIES "${CURSES_INCLUDE_DIRS}"
+ )
+endif()
+
+if (Curses_FOUND)
+ option(USE_NCURSES "Whether to use ncurses as UI instead of flat terminal" TRUE)
+else()
+ message(STATUS "Curses library was not found.")
+endif()
+
+add_executable(Vcomputer Vcomputer__main.cpp disas.cpp simpc_ui.cpp)
+verilate(Vcomputer
+ SOURCES ../computer.sv
+ TOP_MODULE computer
+ TRACE_FST
+ INCLUDE_DIRS ..
+ VERILATOR_ARGS -CFLAGS -I${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+if (USE_NCURSES)
+ target_compile_definitions(Vcomputer PRIVATE NCUR)
+ target_link_libraries(Vcomputer PRIVATE Curses::Curses)
+endif()
diff --git a/nandgame/cpp/Vcomputer__main.cpp b/nandgame/cpp/Vcomputer__main.cpp
new file mode 100644
index 0000000..77e291f
--- /dev/null
+++ b/nandgame/cpp/Vcomputer__main.cpp
@@ -0,0 +1,97 @@
+// Verilated -*- C++ -*-
+// DESCRIPTION: Verilator output: main() simulation loop, created with --main
+
+#include "simpc_config.h"
+#include "simpc_types.h"
+#include "simpc_ui.h"
+
+#include "Vcomputer.h"
+#include "Vcomputer___024root.h"
+#include "Vcomputer_computer.h"
+#include "Vcomputer_my_mem__D10_DB10000.h"
+#include "verilated.h"
+#include <cstdio>
+#include <sched.h>
+
+//======================
+
+int main(int argc, char **argv, char **) {
+
+ // Setup context, defaults, and parse command line
+ Verilated::debug(0);
+ const std::unique_ptr<VerilatedContext> contextp{new VerilatedContext};
+ Verilated::traceEverOn(true);
+ contextp->threads(1);
+ contextp->commandArgs(argc, argv);
+
+ // Construct the Verilated model, from Vtop.h generated from Verilating
+ const std::unique_ptr<Vcomputer> topp{new Vcomputer{contextp.get(), ""}};
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %.20s [filename]\n\n", argv[0]);
+ exit(-1);
+ }
+
+ puts("Start simulation.");
+ // topp->computer->clk_in = 0;
+ topp->clk_in = 0;
+ FILE *f = fopen(argv[1], "rb");
+ if (!f) {
+ fprintf(stderr, "Program file %.20s not found.\n", argv[1]);
+ exit(-1);
+ }
+ fseek(f, 0, SEEK_END);
+ long fpos = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ size_t bytes_read =
+ fread(topp->computer->ROM->r_datastore.m_storage, 2, fpos / 2, f);
+
+ if (bytes_read * 2 != fpos) {
+ fputs("Couldn't read program file completely.", stderr);
+ exit(-1);
+ }
+
+ simpc_ui_init();
+
+ while (VL_LIKELY(!contextp->gotFinish()) && contextp->time() < 500 &&
+ !topp->halt) {
+ auto i = contextp->time();
+
+ if (i != 0 && (i % TICKS_PER_INS) == 0) {
+ topp->clk_in = !topp->clk_in;
+ }
+
+ simpc_ui_write(topp, i, StepPosition_t::BEFORE_EVAL);
+
+ // Evaluate model
+ topp->eval();
+
+ simpc_ui_write(topp, i, StepPosition_t::AFTER_EVAL);
+
+ // Advance time
+ contextp->timeInc(1);
+ i++;
+ }
+
+ simpc_ui_finish_message(contextp, topp);
+
+ if (VL_LIKELY(!contextp->gotFinish())) {
+ VL_DEBUG_IF(VL_PRINTF("+ Exiting without $finish; no events left\n"););
+ }
+
+ // for tracefile to properly show
+ for (int i = 0; i < 10; i++) {
+ contextp->timeInc(1);
+ topp->eval();
+ }
+ // Execute 'final' processes
+ topp->final();
+
+ // Print statistical summary report
+ // contextp->statsPrintSummary();
+
+ simpc_ui_confirm_finish();
+ simpc_ui_cleanup();
+
+ return 0;
+}
diff --git a/nandgame/cpp/disas.cpp b/nandgame/cpp/disas.cpp
new file mode 100644
index 0000000..f6c3cf7
--- /dev/null
+++ b/nandgame/cpp/disas.cpp
@@ -0,0 +1,212 @@
+// LLM converted. Here be dragons.
+
+#include "disas.h"
+
+#include <iomanip>
+#include <sstream>
+#include <string>
+#include <vector>
+
+static const std::string ZERO = "#0";
+static const std::string DEST_NONE = "_";
+static const std::string JUMP_NONE = "";
+
+static const std::vector<std::string> JUMPS_IF_NZERO = {"jgt", "jlt", "jne",
+ "jmp"};
+static const std::vector<std::string> JUMPS_IF_ZERO = {"jge", "jle", "jeq",
+ "jmp"};
+
+struct Decoded {
+ std::string mnemonic;
+ std::string dest;
+ std::string op1;
+ std::string op2;
+ std::string jumpdest;
+};
+
+static bool in_list(const std::string &s, const std::vector<std::string> &v) {
+ for (const auto &x : v) {
+ if (x == s)
+ return true;
+ }
+ return false;
+}
+
+static std::string decode_jump(uint16_t ins) {
+ if ((ins & 0x7) == 0) {
+ return JUMP_NONE;
+ }
+
+ if ((ins & 0x7) == 0x7) {
+ return "jmp";
+ }
+
+ bool jl = (ins & (1 << 2)) != 0;
+ bool je = (ins & (1 << 1)) != 0;
+ bool jg = (ins & (1 << 0)) != 0;
+
+ if (jl && je) {
+ return "jle";
+ }
+ if (jl && jg) {
+ return "jne";
+ }
+ if (je && jg) {
+ return "jge";
+ }
+
+ if (jl)
+ return "jlt";
+ if (je)
+ return "jeq";
+ if (jg)
+ return "jgt";
+
+ return "<unknown>";
+}
+
+static std::pair<std::string, bool> decode_ins(uint16_t ins) {
+ int opcode = (ins >> 8) & 0x03;
+ bool ar_n_log = (ins & (1 << 10)) != 0;
+ opcode |= (ar_n_log ? 1 : 0) << 2;
+
+ switch (opcode) {
+ case 0b000:
+ return {"and", true};
+ case 0b001:
+ return {"or", true};
+ case 0b010:
+ return {"xor", true};
+ case 0b011:
+ return {"not", false};
+ case 0b100:
+ return {"add", true};
+ case 0b101:
+ return {"inc", false};
+ case 0b110:
+ return {"sub", true};
+ case 0b111:
+ return {"dec", false};
+ default:
+ return {"<?>", false};
+ }
+}
+
+static std::string decode_arg1(uint16_t ins) {
+ bool use_mem = (ins & (1 << 12)) != 0;
+ bool zx = (ins & (1 << 7)) != 0;
+ bool sw = (ins & (1 << 6)) != 0;
+
+ if (zx)
+ return ZERO;
+ if (!sw)
+ return "D";
+ return use_mem ? "M" : "A";
+}
+
+static std::string decode_arg2(uint16_t ins) {
+ bool use_mem = (ins & (1 << 12)) != 0;
+ bool sw = (ins & (1 << 6)) != 0;
+
+ if (sw)
+ return "D";
+ return use_mem ? "M" : "A";
+}
+
+static std::string decode_dest(uint16_t ins) {
+ bool dA = (ins & (1 << 5)) != 0;
+ bool dD = (ins & (1 << 4)) != 0;
+ bool dM = (ins & (1 << 3)) != 0;
+
+ std::string dest;
+ if (dA)
+ dest += "A";
+ if (dD)
+ dest += "D";
+ if (dM)
+ dest += "M";
+
+ return dest.empty() ? DEST_NONE : dest;
+}
+
+static Decoded decode_instruction(uint16_t ins) {
+ if ((ins & 0x8000) == 0) {
+ return {"mov", "A", "#" + std::to_string(ins), "", ""};
+ } else {
+ auto [mnemonic, two_op] = decode_ins(ins);
+ std::string dest = decode_dest(ins);
+ std::string op1 = decode_arg1(ins);
+ std::string op2 = two_op ? decode_arg2(ins) : "";
+ std::string jumpdst = decode_jump(ins);
+ return {mnemonic, dest, op1, op2, jumpdst};
+ }
+}
+
+static Decoded fixup_ins(uint16_t ins) {
+ Decoded d = decode_instruction(ins);
+ std::string &mnemonic = d.mnemonic;
+ std::string &dest = d.dest;
+ std::string &op1 = d.op1;
+ std::string &op2 = d.op2;
+ std::string &jumpdest = d.jumpdest;
+
+ if (op1 == ZERO) {
+ if (mnemonic == "sub") {
+ return {"neg", dest, op2, "", jumpdest};
+ }
+
+ if (mnemonic == "and") {
+ if (dest == DEST_NONE) {
+ if (in_list(jumpdest, JUMPS_IF_ZERO)) {
+ return {"jmp", "", "", "", ""};
+ } else {
+ return {"nop", "", "", "", ""};
+ }
+ } else {
+ std::string newjmp = in_list(jumpdest, JUMPS_IF_ZERO) ? "jmp" : "";
+ return {"mov", dest, ZERO, "", newjmp};
+ }
+ }
+
+ if (mnemonic == "add" || mnemonic == "or" || mnemonic == "xor") {
+ if (dest == DEST_NONE && jumpdest == JUMP_NONE) {
+ return {"nop", "", "", "", ""};
+ } else {
+ return {"mov", dest, op2, "", jumpdest};
+ }
+ }
+
+ if (mnemonic == "not") {
+ if (dest == DEST_NONE) {
+ if (jumpdest == "jeq" || jumpdest == "jgt" || jumpdest == "jge" ||
+ jumpdest == JUMP_NONE) {
+ return {"nop", "", "", "", ""};
+ } else {
+ return {"jmp", "", "", "", ""};
+ }
+ }
+ }
+ }
+
+ return d;
+}
+
+std::string print_decoded(uint16_t ins, bool simplify) {
+ // illegal instruction
+ if ((ins & 0xC000) == 0x8000)
+ return "halt";
+
+ Decoded d = simplify ? fixup_ins(ins) : decode_instruction(ins);
+
+ std::string jumpdest_str = d.jumpdest.empty() ? "" : "." + d.jumpdest;
+ std::string opcode_str = d.mnemonic + jumpdest_str;
+ std::string dest_str = d.dest.empty() ? std::string(7, ' ') : d.dest + ", ";
+ std::string op2_str = d.op2.empty() ? "" : ", ";
+ std::string op1_str = d.op1 + op2_str;
+
+ std::ostringstream oss;
+ oss << std::left << std::setw(9) << opcode_str << std::setw(6) << dest_str
+ << std::setw(4) << op1_str << d.op2;
+
+ return oss.str();
+}
diff --git a/nandgame/cpp/disas.h b/nandgame/cpp/disas.h
new file mode 100644
index 0000000..d075fce
--- /dev/null
+++ b/nandgame/cpp/disas.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#include <cstdint>
+#include <string>
+
+std::string print_decoded(uint16_t ins, bool simplify);
diff --git a/nandgame/cpp/simpc_config.h b/nandgame/cpp/simpc_config.h
new file mode 100644
index 0000000..6e35517
--- /dev/null
+++ b/nandgame/cpp/simpc_config.h
@@ -0,0 +1,3 @@
+#pragma once
+
+#define TICKS_PER_INS 2
diff --git a/nandgame/cpp/simpc_types.h b/nandgame/cpp/simpc_types.h
new file mode 100644
index 0000000..5fc7076
--- /dev/null
+++ b/nandgame/cpp/simpc_types.h
@@ -0,0 +1,3 @@
+#pragma once
+
+enum class StepPosition_t { BEFORE_EVAL, AFTER_EVAL };
diff --git a/nandgame/cpp/simpc_ui.cpp b/nandgame/cpp/simpc_ui.cpp
new file mode 100644
index 0000000..b7971fb
--- /dev/null
+++ b/nandgame/cpp/simpc_ui.cpp
@@ -0,0 +1,173 @@
+#include "simpc_ui.h"
+
+#define NCUR 1
+#include "disas.h"
+
+#define NCUR_X 5
+#define MEMORY_CONTEXT 3
+
+#if NCUR
+#include <ncurses.h>
+#define NCUR_DELAY_MS 100
+
+#define PRINT_ME(y, x, ...) \
+ { mvprintw(y, x, __VA_ARGS__); }
+#define PRINT_NEXT() \
+ { \
+ refresh(); \
+ napms(NCUR_DELAY_MS); \
+ }
+
+#else
+
+#define PRINT_ME(y, x, ...) \
+ { \
+ printf("%*c", x, ' '); \
+ printf(__VA_ARGS__); \
+ printf("\n"); \
+ }
+#define PRINT_NEXT() \
+ { puts("-----"); }
+
+#endif
+
+#include "Vcomputer.h"
+#include "Vcomputer___024root.h"
+#include "Vcomputer_comb_mem.h"
+#include "Vcomputer_computer.h"
+#include "Vcomputer_my_mem__D10_DB10000.h"
+#include "verilated.h"
+#include <cstdint>
+#include <cstdio>
+#include <sched.h>
+
+bool paused = false;
+
+void simpc_ui_write(const std::unique_ptr<Vcomputer> &topp, uint64_t &i,
+ StepPosition_t sp) {
+ uint16_t opcode = topp->computer->PC_content_int;
+
+ PRINT_ME(1, 1, "Step: %10d \b%c", i,
+ sp == StepPosition_t::BEFORE_EVAL ? 'A' : 'B');
+ PRINT_ME(2, 1, "%-20s", paused ? "Paused" : "Running");
+ PRINT_ME(3, NCUR_X, "CLK1: %4d\tPC: @0x%04X\tINS: 0x%04X\tHLT: %d",
+ topp->clk_in,
+ // wrong
+ // topp->computer->clk_in,
+ topp->computer->PC_addr_int, opcode, topp->halt);
+
+ auto insline = print_decoded(opcode, true);
+ PRINT_ME(5, NCUR_X, "%-80s", insline.c_str());
+
+ PRINT_ME(7, NCUR_X, "A: 0x%04X\tD: 0x%04X\tM: 0x%04X\tRES: 0x%04X",
+ topp->computer->reg_A_int, topp->computer->reg_D_int,
+ topp->computer->reg_pA_int, topp->computer->result_int);
+ PRINT_ME(
+ 8, NCUR_X, "%c%8d\t%c%8d\t%c%8d\t%11d",
+ topp->computer->store_to_A_int ? '*' : ' ', topp->computer->reg_A_int,
+ topp->computer->store_to_D_int ? '*' : ' ', topp->computer->reg_D_int,
+ topp->computer->store_to_pA_int ? '*' : ' ', topp->computer->reg_pA_int,
+ topp->computer->result_int);
+
+ const int ram1_pos_offset = 35 + 1;
+ const int ram2_pos_offset = ram1_pos_offset + 14 + 1;
+ PRINT_ME(10, NCUR_X, "%-35s", "--- ROM ---");
+ PRINT_ME(10, NCUR_X + ram1_pos_offset, "%-35s", "--- RAM1 ---");
+ PRINT_ME(10, NCUR_X + ram2_pos_offset, "%-35s", "-- RAM2 --");
+ for (int i = -MEMORY_CONTEXT; i <= MEMORY_CONTEXT; i++) {
+ const int ypos_base = 10 + 1 + MEMORY_CONTEXT;
+ const char *prefix = i == 0 ? "> " : " ";
+ int32_t current_ROM_address = topp->computer->PC_addr_int + i;
+ int32_t current_RAM_address = topp->computer->reg_A_int + i;
+ if (current_ROM_address < 0) {
+ PRINT_ME(
+ ypos_base + i, NCUR_X, "%.35s",
+ "---------------------------------------------------------------");
+ } else {
+ const uint16_t p = current_ROM_address;
+ const uint16_t program_op_code = topp->computer->ROM->r_datastore[p];
+ auto disas_code = print_decoded(program_op_code, true);
+ PRINT_ME(ypos_base + i, NCUR_X, "%04X %s%04X %-30s",
+ current_ROM_address, prefix, program_op_code,
+ disas_code.c_str());
+ }
+ if (current_RAM_address < 0) {
+ PRINT_ME(
+ ypos_base + i, NCUR_X + ram1_pos_offset, "%.12s",
+ "---------------------------------------------------------------");
+ } else {
+ const uint16_t p = current_RAM_address;
+ const uint16_t mem_content =
+ topp->computer->RAM->nand_memory->r_datastore[p];
+ PRINT_ME(ypos_base + i, NCUR_X + ram1_pos_offset, "%04X %s%04X",
+ current_RAM_address, prefix, mem_content);
+ }
+ {
+ const uint16_t p = MEMORY_CONTEXT + i;
+ const uint16_t mem_content =
+ topp->computer->RAM->nand_memory->r_datastore[p];
+ PRINT_ME(ypos_base + i, NCUR_X + ram2_pos_offset, "%04X %04X%30c", p,
+ mem_content, ' ');
+ }
+ }
+ // PRINT_ME(7 + NCUR_OFFSET, NCUR_X, "ALU");
+ // PRINT_ME(8 + NCUR_OFFSET, NCUR_X, "X: %5d\tY: %5d",
+ // topp->computer->CPU->my_alu->int_op_x,
+ // topp->computer->CPU->my_alu->int_op_y);
+
+ PRINT_ME(getmaxy(stdscr) - 1, 1,
+ "q - Quit; p - (Un)pause; any key in pause - step");
+
+ PRINT_NEXT();
+
+ int ch = getch();
+ if (ch == 'p') {
+ nodelay(stdscr, paused);
+ paused = !paused;
+ } else if (ch == 'q') {
+ simpc_ui_cleanup();
+ exit(0);
+ }
+}
+
+void simpc_ui_finish_message(const std::unique_ptr<VerilatedContext> &contextp,
+ const std::unique_ptr<Vcomputer> &topp) {
+ PRINT_ME(1, 30, "Simulation finished.");
+ const char *msg;
+ if (topp->halt) {
+ msg = "Halt encountered.";
+ } else if (!contextp->gotFinish()) {
+ msg = "Step count exceeded.";
+ } else {
+ msg = "Regular finish.";
+ }
+ PRINT_ME(2, 30, "%s", msg);
+#if NCUR
+ refresh();
+#endif
+}
+
+void simpc_ui_init(void) {
+#if NCUR
+ initscr();
+ curs_set(0);
+ nodelay(stdscr, TRUE);
+ noecho();
+ cbreak();
+#endif
+}
+
+void simpc_ui_cleanup(void) {
+#if NCUR
+ nocbreak();
+ echo();
+ endwin();
+#endif
+}
+
+void simpc_ui_confirm_finish(void) {
+#if NCUR
+ nodelay(stdscr, FALSE);
+ getch();
+#endif
+} \ No newline at end of file
diff --git a/nandgame/cpp/simpc_ui.h b/nandgame/cpp/simpc_ui.h
new file mode 100644
index 0000000..71b651f
--- /dev/null
+++ b/nandgame/cpp/simpc_ui.h
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "Vcomputer.h"
+#include "simpc_types.h"
+
+#include <memory>
+
+void simpc_ui_init(void);
+void simpc_ui_write(const std::unique_ptr<Vcomputer> &topp, uint64_t &i,
+ StepPosition_t sp);
+void simpc_ui_finish_message(const std::unique_ptr<VerilatedContext> &contextp,
+ const std::unique_ptr<Vcomputer> &topp);
+void simpc_ui_confirm_finish(void);
+void simpc_ui_cleanup(void);