From acd85c5766ae4fd4c7a961369540b3b5b4b4f146 Mon Sep 17 00:00:00 2001 From: uvok Date: Sun, 11 Jan 2026 10:45:59 +0100 Subject: Move around stuff, split UI --- nandgame/cpp/CMakeLists.txt | 2 +- nandgame/cpp/Vcomputer__main.cpp | 152 +++++-------------- nandgame/cpp/disas.cpp | 310 +++++++++++++++++++++------------------ nandgame/cpp/disas.h | 4 +- nandgame/cpp/simpc_config.h | 3 + nandgame/cpp/simpc_types.h | 3 + nandgame/cpp/simpc_ui.cpp | 100 +++++++++++++ nandgame/cpp/simpc_ui.h | 11 ++ 8 files changed, 324 insertions(+), 261 deletions(-) create mode 100644 nandgame/cpp/simpc_config.h create mode 100644 nandgame/cpp/simpc_types.h create mode 100644 nandgame/cpp/simpc_ui.cpp create mode 100644 nandgame/cpp/simpc_ui.h (limited to 'nandgame') diff --git a/nandgame/cpp/CMakeLists.txt b/nandgame/cpp/CMakeLists.txt index 002bfcb..4da5c03 100644 --- a/nandgame/cpp/CMakeLists.txt +++ b/nandgame/cpp/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.20) project(simpc) find_package(verilator HINTS $ENV{VERILATOR_ROOT}) -add_executable(Vcomputer Vcomputer__main.cpp disas.cpp) +add_executable(Vcomputer Vcomputer__main.cpp disas.cpp simpc_ui.cpp) verilate(Vcomputer SOURCES ../computer.sv TOP_MODULE computer diff --git a/nandgame/cpp/Vcomputer__main.cpp b/nandgame/cpp/Vcomputer__main.cpp index f857be1..50f7c1b 100644 --- a/nandgame/cpp/Vcomputer__main.cpp +++ b/nandgame/cpp/Vcomputer__main.cpp @@ -1,81 +1,20 @@ // 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_alu.h" #include "Vcomputer_computer.h" -#include "Vcomputer_instruction_decode.h" #include "Vcomputer_my_mem__D10_DB10000.h" #include "verilated.h" -#include "Vcomputer.h" -#include -#include #include #include -#define TICKS_PER_INS 2 -#define NCUR 1 -#include "disas.h" - -#define NCUR_OFFSET 3 - -#if NCUR -#include -#define NCUR_DELAY_MS 100 -#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 - //====================== -enum class StepPosition_t { - BEFORE_EVAL, - AFTER_EVAL -}; - -void draw_ui(const std::unique_ptr &topp, int &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(1 + NCUR_OFFSET, 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(3 + NCUR_OFFSET, NCUR_X, "%-80s", insline.c_str()); - - // keep old state - if (topp->halt) - return; - - 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(); -} - int main(int argc, char **argv, char **) { // Setup context, defaults, and parse command line @@ -112,66 +51,53 @@ int main(int argc, char **argv, char **) { exit(-1); } -#if NCUR - initscr(); - curs_set(0); -#endif + draw_init(); - while (VL_LIKELY(!contextp->gotFinish()) && i < 500) { - // doesn't work. - // topp->computer->clk_in = ~topp->computer->clk_in; + while (VL_LIKELY(!contextp->gotFinish()) && i < 500) { + // doesn't work. + // topp->computer->clk_in = ~topp->computer->clk_in; - if (i != 0 && (i % TICKS_PER_INS) == 0) { - topp->clk_in = !topp->clk_in; - } + if (i != 0 && (i % TICKS_PER_INS) == 0) { + topp->clk_in = !topp->clk_in; + } - draw_ui(topp, i, StepPosition_t::BEFORE_EVAL); + draw_ui(topp, i, StepPosition_t::BEFORE_EVAL); - // Evaluate model - topp->eval(); + // Evaluate model + topp->eval(); - // both bits 14 and 15 need to be set - if (topp->halt) - break; + // both bits 14 and 15 need to be set + if (topp->halt) + break; - draw_ui(topp, i, StepPosition_t::AFTER_EVAL); + draw_ui(topp, i, StepPosition_t::AFTER_EVAL); - // Advance time - contextp->timeInc(1); - i++; - } - -#if NCUR - refresh(); -#endif + // Advance time + contextp->timeInc(1); + i++; + } - PRINT_ME(10 + NCUR_OFFSET, 10, "Simulation finished."); - if (topp->halt) { - PRINT_ME(11 + NCUR_OFFSET, 10, "Halt encountered."); - } else if (!contextp->gotFinish()) { - PRINT_ME(11 + NCUR_OFFSET, 10, "Step count exceeded."); - } else { - PRINT_ME(11 + NCUR_OFFSET, 10, "Regular finish."); - } + draw_finish(contextp, topp); - if (VL_LIKELY(!contextp->gotFinish())) { - VL_DEBUG_IF(VL_PRINTF("+ Exiting without $finish; no events left\n");); - } + 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(); + // 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(); + // Print statistical summary report + // contextp->statsPrintSummary(); #if NCUR - getch(); endwin(); + getch(); + endwin(); #endif - return 0; + return 0; } diff --git a/nandgame/cpp/disas.cpp b/nandgame/cpp/disas.cpp index ac7ca0c..f6c3cf7 100644 --- a/nandgame/cpp/disas.cpp +++ b/nandgame/cpp/disas.cpp @@ -2,193 +2,211 @@ #include "disas.h" +#include +#include #include #include -#include -#include -static const std::string ZERO = "#0"; +static const std::string ZERO = "#0"; static const std::string DEST_NONE = "_"; static const std::string JUMP_NONE = ""; -static const std::vector JUMPS_IF_NZERO = {"jgt", "jlt", "jne", "jmp"}; -static const std::vector JUMPS_IF_ZERO = {"jge", "jle", "jeq", "jmp"}; +static const std::vector JUMPS_IF_NZERO = {"jgt", "jlt", "jne", + "jmp"}; +static const std::vector JUMPS_IF_ZERO = {"jge", "jle", "jeq", + "jmp"}; struct Decoded { - std::string mnemonic; - std::string dest; - std::string op1; - std::string op2; - std::string jumpdest; + 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 &v) { - for (const auto &x : v) { - if (x == s) return true; - } - return false; + 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 ""; + 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 ""; } static std::pair 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}; - } + 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"; + 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; + bool use_mem = (ins & (1 << 12)) != 0; + bool sw = (ins & (1 << 6)) != 0; - if (sw) return "D"; - return use_mem ? "M" : "A"; + 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; + 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}; - } + 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}; - } + 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 == "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 == "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", "", "", "", ""}; - } - } + if (mnemonic == "not") { + if (dest == DEST_NONE) { + if (jumpdest == "jeq" || jumpdest == "jgt" || jumpdest == "jge" || + jumpdest == JUMP_NONE) { + return {"nop", "", "", "", ""}; + } else { + return {"jmp", "", "", "", ""}; } + } } + } - return d; + 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(); + // 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 index 1f04268..d075fce 100644 --- a/nandgame/cpp/disas.h +++ b/nandgame/cpp/disas.h @@ -1,4 +1,6 @@ -#include +#pragma once + #include +#include 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..2fce3b8 --- /dev/null +++ b/nandgame/cpp/simpc_ui.cpp @@ -0,0 +1,100 @@ +#include "simpc_ui.h" + +#define NCUR 1 +#include "disas.h" + +#define NCUR_OFFSET 3 + +#if NCUR +#include +#define NCUR_DELAY_MS 100 +#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 + +#include "Vcomputer.h" +#include "Vcomputer___024root.h" +#include "Vcomputer_alu.h" +#include "Vcomputer_computer.h" +#include "Vcomputer_instruction_decode.h" +#include "verilated.h" +#include +#include +#include +#include + +void draw_ui(const std::unique_ptr &topp, int &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(1 + NCUR_OFFSET, 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(3 + NCUR_OFFSET, NCUR_X, "%-80s", insline.c_str()); + + // keep old state + if (topp->halt) + return; + + 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(); +} + +void draw_finish(const std::unique_ptr &contextp, + const std::unique_ptr &topp) { +#if NCUR + refresh(); +#endif + PRINT_ME(10 + NCUR_OFFSET, 10, "Simulation finished."); + if (topp->halt) { + PRINT_ME(11 + NCUR_OFFSET, 10, "Halt encountered."); + } else if (!contextp->gotFinish()) { + PRINT_ME(11 + NCUR_OFFSET, 10, "Step count exceeded."); + } else { + PRINT_ME(11 + NCUR_OFFSET, 10, "Regular finish."); + } +} + +void draw_init(void) { +#if NCUR + initscr(); + curs_set(0); +#endif +} diff --git a/nandgame/cpp/simpc_ui.h b/nandgame/cpp/simpc_ui.h new file mode 100644 index 0000000..4f03d4a --- /dev/null +++ b/nandgame/cpp/simpc_ui.h @@ -0,0 +1,11 @@ +#pragma once + +#include "Vcomputer.h" +#include "simpc_types.h" + +#include + +void draw_init(void); +void draw_ui(const std::unique_ptr &topp, int &i, StepPosition_t sp); +void draw_finish(const std::unique_ptr &contextp, + const std::unique_ptr &topp); \ No newline at end of file -- cgit v1.2.3