From 0f86e4c661fa3164447a77d84610146426008364 Mon Sep 17 00:00:00 2001 From: uvok Date: Sun, 11 Jan 2026 10:14:34 +0100 Subject: Reorganize cpp files --- nandgame/Makefile | 4 +- nandgame/Vcomputer__main.cpp | 177 ----------------------------------- nandgame/assembler/disas.cpp | 194 --------------------------------------- nandgame/assembler/disas.h | 4 - nandgame/cpp/Vcomputer__main.cpp | 177 +++++++++++++++++++++++++++++++++++ nandgame/cpp/disas.cpp | 194 +++++++++++++++++++++++++++++++++++++++ nandgame/cpp/disas.h | 4 + 7 files changed, 377 insertions(+), 377 deletions(-) delete mode 100644 nandgame/Vcomputer__main.cpp delete mode 100644 nandgame/assembler/disas.cpp delete mode 100644 nandgame/assembler/disas.h create mode 100644 nandgame/cpp/Vcomputer__main.cpp create mode 100644 nandgame/cpp/disas.cpp create mode 100644 nandgame/cpp/disas.h (limited to 'nandgame') diff --git a/nandgame/Makefile b/nandgame/Makefile index 4973f81..8bdb828 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 verilator \ --Mdir simpc \ --cc --exe \ - -CFLAGS "-I${PWD}/assembler" -LDFLAGS "-lncurses" \ + -CFLAGS "-I${PWD}/cpp" -LDFLAGS "-lncurses" \ -DVERILATE --trace \ $^ \ No newline at end of file diff --git a/nandgame/Vcomputer__main.cpp b/nandgame/Vcomputer__main.cpp deleted file mode 100644 index 26d8aa6..0000000 --- a/nandgame/Vcomputer__main.cpp +++ /dev/null @@ -1,177 +0,0 @@ -// Verilated -*- C++ -*- -// DESCRIPTION: Verilator output: main() simulation loop, created with --main - -#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 "../assembler/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 - Verilated::debug(0); - const std::unique_ptr 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 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 - - 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; - } - - draw_ui(topp, i, StepPosition_t::BEFORE_EVAL); - - // Evaluate model - topp->eval(); - - // both bits 14 and 15 need to be set - if (topp->halt) - break; - - draw_ui(topp, i, StepPosition_t::AFTER_EVAL); - - // Advance time - contextp->timeInc(1); - i++; - } - -#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."); - } - - 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(); - -#if NCUR - getch(); endwin(); -#endif - - return 0; -} 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 -#include -#include -#include - -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"}; - -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 &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 ""; -} - -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}; - } -} - -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/assembler/disas.h deleted file mode 100644 index 1f04268..0000000 --- a/nandgame/assembler/disas.h +++ /dev/null @@ -1,4 +0,0 @@ -#include -#include - -std::string print_decoded(uint16_t ins, bool simplify); diff --git a/nandgame/cpp/Vcomputer__main.cpp b/nandgame/cpp/Vcomputer__main.cpp new file mode 100644 index 0000000..f857be1 --- /dev/null +++ b/nandgame/cpp/Vcomputer__main.cpp @@ -0,0 +1,177 @@ +// Verilated -*- C++ -*- +// DESCRIPTION: Verilator output: main() simulation loop, created with --main + +#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 + Verilated::debug(0); + const std::unique_ptr 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 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 + + 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; + } + + draw_ui(topp, i, StepPosition_t::BEFORE_EVAL); + + // Evaluate model + topp->eval(); + + // both bits 14 and 15 need to be set + if (topp->halt) + break; + + draw_ui(topp, i, StepPosition_t::AFTER_EVAL); + + // Advance time + contextp->timeInc(1); + i++; + } + +#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."); + } + + 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(); + +#if NCUR + getch(); endwin(); +#endif + + return 0; +} diff --git a/nandgame/cpp/disas.cpp b/nandgame/cpp/disas.cpp new file mode 100644 index 0000000..ac7ca0c --- /dev/null +++ b/nandgame/cpp/disas.cpp @@ -0,0 +1,194 @@ +// LLM converted. Here be dragons. + +#include "disas.h" + +#include +#include +#include +#include + +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"}; + +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 &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 ""; +} + +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}; + } +} + +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..1f04268 --- /dev/null +++ b/nandgame/cpp/disas.h @@ -0,0 +1,4 @@ +#include +#include + +std::string print_decoded(uint16_t ins, bool simplify); -- cgit v1.2.3