summaryrefslogtreecommitdiff
path: root/nandgame
diff options
context:
space:
mode:
Diffstat (limited to 'nandgame')
-rw-r--r--nandgame/Makefile8
-rw-r--r--nandgame/assembler/disas.cpp194
-rw-r--r--nandgame/computer.sv2
-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.h (renamed from nandgame/assembler/disas.h)4
-rw-r--r--nandgame/cpp/simpc_config.h3
-rw-r--r--nandgame/cpp/simpc_types.h3
-rw-r--r--nandgame/cpp/simpc_ui.cpp (renamed from nandgame/Vcomputer__main.cpp)161
-rw-r--r--nandgame/cpp/simpc_ui.h14
11 files changed, 430 insertions, 307 deletions
diff --git a/nandgame/Makefile b/nandgame/Makefile
index 4973f81..d6d23ea 100644
--- a/nandgame/Makefile
+++ b/nandgame/Makefile
@@ -11,10 +11,10 @@ include ../common.mk
.PHONY: simpc
simpc: simpc/Vcomputer
-simpc/Vcomputer: computer.sv Vcomputer__main.cpp assembler/disas.cpp
+simpc/Vcomputer: computer.sv cpp/Vcomputer__main.cpp cpp/disas.cpp cpp/simpc_ui.cpp
verilator \
--Mdir simpc \
--cc --exe \
- -CFLAGS "-I${PWD}/assembler" -LDFLAGS "-lncurses" \
- -DVERILATE --trace \
- $^ \ No newline at end of file
+ -CFLAGS "-I${PWD}/cpp" -LDFLAGS "-lncurses" \
+ --trace \
+ $^
diff --git a/nandgame/assembler/disas.cpp b/nandgame/assembler/disas.cpp
deleted file mode 100644
index ac7ca0c..0000000
--- a/nandgame/assembler/disas.cpp
+++ /dev/null
@@ -1,194 +0,0 @@
-// LLM converted. Here be dragons.
-
-#include "disas.h"
-
-#include <string>
-#include <vector>
-#include <sstream>
-#include <iomanip>
-
-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/computer.sv b/nandgame/computer.sv
index 258c323..5c2127f 100644
--- a/nandgame/computer.sv
+++ b/nandgame/computer.sv
@@ -83,7 +83,7 @@ counter PC (
.st_store_X_in(cpu_do_jump_int)
);
-`ifdef VERILATE
+`ifdef VERILATOR
initial begin
$dumpfile("simpc.vvp"); $dumpvars();
end
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/assembler/disas.h b/nandgame/cpp/disas.h
index 1f04268..d075fce 100644
--- a/nandgame/assembler/disas.h
+++ b/nandgame/cpp/disas.h
@@ -1,4 +1,6 @@
-#include <string>
+#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/Vcomputer__main.cpp b/nandgame/cpp/simpc_ui.cpp
index 1857f1e..b7971fb 100644
--- a/nandgame/Vcomputer__main.cpp
+++ b/nandgame/cpp/simpc_ui.cpp
@@ -1,33 +1,17 @@
-// Verilated -*- C++ -*-
-// DESCRIPTION: Verilator output: main() simulation loop, created with --main
+#include "simpc_ui.h"
-#include "Vcomputer.h"
-#include "Vcomputer___024root.h"
-#include "Vcomputer_alu.h"
-#include "Vcomputer_comb_mem.h"
-#include "Vcomputer_computer.h"
-#include "Vcomputer_instruction_decode.h"
-#include "Vcomputer_my_mem__D10_DB10000.h"
-#include "verilated.h"
-#include <cinttypes>
-#include <cstdint>
-#include <cstdio>
-#include <sched.h>
-
-#define TICKS_PER_INS 2
#define NCUR 1
-#include "../assembler/disas.h"
+#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__); \
- }
+ { mvprintw(y, x, __VA_ARGS__); }
#define PRINT_NEXT() \
{ \
refresh(); \
@@ -43,22 +27,29 @@
printf("\n"); \
}
#define PRINT_NEXT() \
- { \
- puts("-----"); \
- }
+ { 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>
-enum class StepPosition_t { BEFORE_EVAL, AFTER_EVAL };
+bool paused = false;
-void draw_ui(const std::unique_ptr<Vcomputer> &topp, uint64_t &i,
- StepPosition_t sp) {
+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
@@ -77,7 +68,6 @@ void draw_ui(const std::unique_ptr<Vcomputer> &topp, uint64_t &i,
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);
-#define MEMORY_CONTEXT 3
const int ram1_pos_offset = 35 + 1;
const int ram2_pos_offset = ram1_pos_offset + 14 + 1;
@@ -125,71 +115,23 @@ void draw_ui(const std::unique_ptr<Vcomputer> &topp, uint64_t &i,
// topp->computer->CPU->my_alu->int_op_x,
// topp->computer->CPU->my_alu->int_op_y);
- PRINT_NEXT();
-}
-
-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);
- }
+ PRINT_ME(getmaxy(stdscr) - 1, 1,
+ "q - Quit; p - (Un)pause; any key in pause - step");
-#if NCUR
- initscr();
- curs_set(0);
-#endif
-
- while (VL_LIKELY(!contextp->gotFinish()) && contextp->time() < 500 &&
- !topp->halt) {
- auto i = contextp->time();
- draw_ui(topp, i, StepPosition_t::BEFORE_EVAL);
-
- // Evaluate model
- if (i != 0 && (i % TICKS_PER_INS) == 0) {
- topp->clk_in = !topp->clk_in;
- }
- topp->eval();
-
- draw_ui(topp, i, StepPosition_t::AFTER_EVAL);
+ PRINT_NEXT();
- // Advance time
- contextp->timeInc(1);
+ int ch = getch();
+ if (ch == 'p') {
+ nodelay(stdscr, paused);
+ paused = !paused;
+ } else if (ch == 'q') {
+ simpc_ui_cleanup();
+ exit(0);
}
+}
-#if NCUR
- refresh();
-#endif
-
- // PRINT_ME(9, 0, "%80c", ' ');
+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) {
@@ -200,27 +142,32 @@ int main(int argc, char **argv, char **) {
msg = "Regular finish.";
}
PRINT_ME(2, 30, "%s", msg);
- // PRINT_ME(12, 0, "%80c", ' ');
-
- 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();
+#if NCUR
+ refresh();
+#endif
+}
- // Print statistical summary report
- // contextp->statsPrintSummary();
+void simpc_ui_init(void) {
+#if NCUR
+ initscr();
+ curs_set(0);
+ nodelay(stdscr, TRUE);
+ noecho();
+ cbreak();
+#endif
+}
+void simpc_ui_cleanup(void) {
#if NCUR
- getch();
+ nocbreak();
+ echo();
endwin();
#endif
-
- return 0;
}
+
+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);