summaryrefslogtreecommitdiff
path: root/nandgame/Vcomputer__main.cpp
diff options
context:
space:
mode:
authoruvok2026-01-09 13:05:59 +0100
committeruvok2026-01-09 13:05:59 +0100
commit678cb2d2d752bbac7625ba9b287762b3acabf116 (patch)
tree91bc1c93968d98c669c34d7c97872f989e1ee786 /nandgame/Vcomputer__main.cpp
parent6f93b00c4d133b6735725c851fb0db43478f30d8 (diff)
Extract UI drawing
Diffstat (limited to 'nandgame/Vcomputer__main.cpp')
-rw-r--r--nandgame/Vcomputer__main.cpp171
1 files changed, 70 insertions, 101 deletions
diff --git a/nandgame/Vcomputer__main.cpp b/nandgame/Vcomputer__main.cpp
index 981bf9d..fe907fb 100644
--- a/nandgame/Vcomputer__main.cpp
+++ b/nandgame/Vcomputer__main.cpp
@@ -16,141 +16,110 @@
#define NCUR 1
#include "../assembler/disas.h"
+#define NCUR_OFFSET 3
+
#if NCUR
#include <ncurses.h>
#define NCUR_DELAY_MS 300
#define NCUR_X 5
#define PRINT_ME(x, y, ...) { mvprintw(x, y, __VA_ARGS__); }
+#define PRINT_NEXT() { refresh(); napms(NCUR_DELAY_MS); }
#else
#define PRINT_ME(x, y, ...) { printf("%*s", x, ""); printf(__VA_ARGS__); printf("\n"); }
+#define PRINT_NEXT() { puts("-----"); }
#endif
//======================
-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};
- 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(), ""}};
+void draw_ui(const std::unique_ptr<Vcomputer> &topp, int &i) {
+ uint16_t opcode = topp->computer->PC_content_int;
+
+ PRINT_ME(1, 1, "Step: %10d", i);
+ PRINT_ME(1 + NCUR_OFFSET, NCUR_X, "CLK1: %4d\tPC: @0x%04X\tINS: 0x%04X",
+ topp->clk_in,
+ // wrong
+ // topp->computer->clk_in,
+ topp->computer->PC_addr_int, opcode);
+
+ auto insline = print_decoded(opcode, true);
+ PRINT_ME(3 + NCUR_OFFSET, NCUR_X, "%s", insline.c_str());
+
+ PRINT_ME(5 + NCUR_OFFSET, 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(6 + NCUR_OFFSET, NCUR_X,
+ "%9" PRId16 "\t%9" PRId16 "\t%9" PRId16 "\t%11" PRId16,
+ topp->computer->reg_A_int, topp->computer->reg_D_int,
+ topp->computer->reg_pA_int, topp->computer->result_int);
+ PRINT_ME(7 + NCUR_OFFSET, NCUR_X, "ALU");
+ PRINT_ME(8 + NCUR_OFFSET, NCUR_X, "X: %5" PRId16 "\tY: %5" PRId16,
+ topp->computer->CPU->my_alu->int_op_x,
+ topp->computer->CPU->my_alu->int_op_y)
+
+ PRINT_NEXT();
+}
- if (argc != 2) {
- fprintf(stderr, "Usage: %.20s [filename]\n\n", argv[0]);
- exit(-1);
- }
- int i = 0;
- 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);
- }
+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};
+ 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);
+ }
+ int i = 0;
+ 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);
+ }
#if NCUR
initscr();
curs_set(0);
#endif
- int ncur_offset = 3;
while (VL_LIKELY(!contextp->gotFinish()) && i < 100) {
//topp->computer->clk_in = ~topp->computer->clk_in;
topp->clk_in = !topp->clk_in;
- //uint16_t opcode = (topp->computer->PC_content_int & 0xff) << 8 | (topp->computer->PC_content_int >> 8);
- uint16_t opcode = topp->computer->PC_content_int;
-
- PRINT_ME(1, 1, "Step: %10d", i);
- PRINT_ME(1 + ncur_offset, NCUR_X,
- "CLK1: %4d\tPC: @0x%04X\tINS: 0x%04X",
- topp->clk_in,
- // wrong
- //topp->computer->clk_in,
- topp->computer->PC_addr_int,
- opcode
- );
- // This is fucking dirty
-#ifdef POPEN_PY
- {
- char command[128];
- snprintf(command, sizeof(command) - 1, "python3 ../assembler/disas_ins.py %04X", opcode);
- char buffer[256];
- FILE* pipe = popen(command, "r");
- if (!pipe) {
- perror("popen failed");
- exit(-1);
- }
-
- if (fgets(buffer, sizeof(buffer), pipe) != NULL) {
- printf("%s", buffer);
- }
-
- (void) pclose(pipe);
- }
-#else
- {
- auto insline = print_decoded(opcode, true);
- PRINT_ME(3 + ncur_offset, NCUR_X, "%s", insline.c_str());
- }
-#endif
-
- PRINT_ME(5 + ncur_offset, 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(6 + ncur_offset, NCUR_X,
- "%9" PRId16 "\t%9" PRId16 "\t%9" PRId16 "\t%11" PRId16,
- topp->computer->reg_A_int,
- topp->computer->reg_D_int,
- topp->computer->reg_pA_int,
- topp->computer->result_int
- );
- PRINT_ME(7 + ncur_offset, NCUR_X, "ALU");
- PRINT_ME(8 + ncur_offset, NCUR_X,
- "X: %5" PRId16 "\tY: %5" PRId16 ,
- topp->computer->CPU->my_alu->int_op_x,
- topp->computer->CPU->my_alu->int_op_y
- );
// Evaluate model
topp->eval();
+
+ draw_ui(topp, i);
+
// Advance time
contextp->timeInc(1);
i++;
-#if NCUR
- refresh(); napms(NCUR_DELAY_MS);
-#else
- puts("-----");
-#endif
}
- PRINT_ME(10 + ncur_offset, 10, "Simulation finished.");
+ PRINT_ME(10 + NCUR_OFFSET, 10, "Simulation finished.");
if (VL_LIKELY(!contextp->gotFinish())) {
VL_DEBUG_IF(VL_PRINTF("+ Exiting without $finish; no events left\n"););