diff options
Diffstat (limited to 'eater_cpu/cpp')
| -rw-r--r-- | eater_cpu/cpp/CMakeLists.txt | 51 | ||||
| -rw-r--r-- | eater_cpu/cpp/Veater_computer__main.cpp | 83 | ||||
| -rw-r--r-- | eater_cpu/cpp/simpc_config.h | 4 | ||||
| -rw-r--r-- | eater_cpu/cpp/simpc_term.cpp | 96 | ||||
| -rw-r--r-- | eater_cpu/cpp/simpc_types.h | 1 | ||||
| -rw-r--r-- | eater_cpu/cpp/simpc_ui.h | 13 |
6 files changed, 248 insertions, 0 deletions
diff --git a/eater_cpu/cpp/CMakeLists.txt b/eater_cpu/cpp/CMakeLists.txt new file mode 100644 index 0000000..771db9d --- /dev/null +++ b/eater_cpu/cpp/CMakeLists.txt @@ -0,0 +1,51 @@ +cmake_minimum_required(VERSION 3.20) + +project(sim_eater_pc) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +find_package(verilator REQUIRED HINTS $ENV{VERILATOR_ROOT}) + +find_package(Curses) +find_package(FLTK) + +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() + +if (FLTK_FOUND) + option(USE_FLTK "Whether to build FLTK as UI" TRUE) +else() + message(STATUS "FLTK library was not found.") +endif() + +add_executable(Veater_computer Veater_computer__main.cpp ) +verilate(Veater_computer + SOURCES ../eater_computer.sv + TOP_MODULE eater_computer + TRACE_FST + INCLUDE_DIRS .. ../../nandgame ../../playground + VERILATOR_ARGS -CFLAGS -I${CMAKE_CURRENT_SOURCE_DIR} +) + +# if (USE_NCURSES) +# target_compile_definitions(Veater_computer PRIVATE NCUR) +# target_link_libraries(Veater_computer PRIVATE Curses::Curses) +# target_sources(Veater_computer PUBLIC simpc_ui.cpp) +# else() +# target_sources(Veater_computer PUBLIC simpc_term.cpp) +# endif() + +target_sources(Veater_computer PUBLIC simpc_term.cpp) diff --git a/eater_cpu/cpp/Veater_computer__main.cpp b/eater_cpu/cpp/Veater_computer__main.cpp new file mode 100644 index 0000000..df6b65e --- /dev/null +++ b/eater_cpu/cpp/Veater_computer__main.cpp @@ -0,0 +1,83 @@ +// Verilated -*- C++ -*- +// DESCRIPTION: Verilator output: main() simulation loop, created with --main + +#include "Veater_computer_eater_computer.h" +#include "Veater_computer_my_mem__DB10.h" +#include "verilated.h" +#include "Veater_computer.h" +#include "verilatedos.h" + +#include "verilated_fst_c.h" + +#include "simpc_ui.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}; + contextp->traceEverOn(true); + contextp->threads(1); + contextp->commandArgs(argc, argv); + + // Construct the Verilated model, from Vtop.h generated from Verilating + const std::unique_ptr<Veater_computer> topp{new Veater_computer{contextp.get(), ""}}; + + // waveforms!!! + VerilatedFstC* tfp = new VerilatedFstC; + topp->trace(tfp, 100); + tfp->open("eater.vvp"); + + topp->clk_in = 0; + topp->auto_run_in = 1; + + // init RAM + // LDA 14 + topp->eater_computer->RAM->r_datastore[0] = 0x0e; + // ADD 15 + topp->eater_computer->RAM->r_datastore[1] = 0x1f; + // OUT + topp->eater_computer->RAM->r_datastore[2] = 0xe0; + // Data @ 14 + topp->eater_computer->RAM->r_datastore[14] = 14; + // Data @ 15 + topp->eater_computer->RAM->r_datastore[15] = 28; + simpc_ui_init(); + + // Simulate until $finish + while (VL_LIKELY(!contextp->gotFinish()) && VL_LIKELY(contextp->time() < 100)) { + // Evaluate model + topp->eval(); + + tfp->dump(contextp->time()); + simpc_ui_write(topp, contextp->time()); + + // Advance time + contextp->timeInc(1); + + topp->clk_in = !topp->clk_in; + + if (topp->eater_computer->PC_out == 3 && topp->eater_computer->__PVT__decoder__DOT__internal_state == 0x01) + break; + } + contextp->timeInc(10); + tfp->dump(contextp->time()); + + tfp->close(); + simpc_ui_finish_message(contextp, topp); + + if (VL_LIKELY(!contextp->gotFinish())) { + VL_DEBUG_IF(VL_PRINTF("+ Exiting without $finish; no events left\n");); + } + + // Execute 'final' processes + topp->final(); + + // Print statistical summary report + contextp->statsPrintSummary(); + + simpc_ui_cleanup(); + + return 0; +} diff --git a/eater_cpu/cpp/simpc_config.h b/eater_cpu/cpp/simpc_config.h new file mode 100644 index 0000000..56c9ad8 --- /dev/null +++ b/eater_cpu/cpp/simpc_config.h @@ -0,0 +1,4 @@ +#pragma once + +// #define TICKS_PER_CLOCK_PERIOD 1 +// #define NCUR_DELAY_MS 10 diff --git a/eater_cpu/cpp/simpc_term.cpp b/eater_cpu/cpp/simpc_term.cpp new file mode 100644 index 0000000..601a8cd --- /dev/null +++ b/eater_cpu/cpp/simpc_term.cpp @@ -0,0 +1,96 @@ +#include "Veater_computer_eater_register.h" +#include "simpc_ui.h" + +#include "disas.h" + +#define NCUR_X 5 +#define attroff(...) +#define attron(...) +#define PRINT_ME(y, x, ...) \ + { \ + printf("%*c", x, ' '); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } +#define PRINT_ME_W(w, y, x, ...) \ + { \ + printf("%*c", x, ' '); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } +#define PRINT_NEXT() \ + { puts("-----"); } + +#include "Veater_computer.h" +#include "Veater_computer_eater_computer.h" +#include "Veater_computer_eater_alu.h" +#include "verilated.h" +#include <cstdint> +#include <cstdio> +#include <sched.h> + +bool paused = false; + +static void handle_key(); + +void simpc_ui_write(const std::unique_ptr<Veater_computer> &topp, uint64_t i) { + // uint16_t opcode = topp->eater_computer->; + uint8_t opcode = topp->eater_computer->INS->r_datastore; + // topp->halt + uint8_t halt = 0; + + PRINT_ME_W(status_top, 0, 0, "Step: %10lu", i); + PRINT_ME_W(status_top, 1, 0, "%-20s", paused ? "Paused" : "Running"); + + PRINT_ME(3, NCUR_X, "CLK1: %4d\tState: 0x%02X\tHLT: %d", + topp->clk_in, + topp->eater_computer->__PVT__decoder__DOT__internal_state, + halt); + + PRINT_ME(4, NCUR_X, "BUS: %02X\tPC: @0x%02X\tINS: 0x%02X", + topp->eater_computer->bus, + topp->eater_computer->PC_out, + opcode + ); + + std::string insline{"???"}; //print_decoded(opcode, true); + PRINT_ME(5, NCUR_X, "%-80s", insline.c_str()); + + PRINT_ME(7, NCUR_X, "A: 0x%02X\tB: 0x%02X\tALU: 0x%02X\tOUT: 0x%02X", + topp->eater_computer->A->r_datastore, topp->eater_computer->B->r_datastore, + topp->eater_computer->alu->result, topp->eater_computer->OUT->r_datastore); + // 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); + + PRINT_NEXT(); +} + +void simpc_ui_finish_message(const std::unique_ptr<VerilatedContext> &contextp, + const std::unique_ptr<Veater_computer> &topp) { + attron(A_BOLD); + auto xpos = 20; + PRINT_ME_W(status_top, 0, xpos, "Simulation finished."); + const char *msg; + // topp->halt + bool halt = false; + if (halt) { + msg = "Halt encountered."; + } else if (!contextp->gotFinish()) { + msg = "Step count exceeded."; + } else { + msg = "Regular finish."; + } + PRINT_ME_W(status_top, 1, xpos, "%s", msg); + + attroff(A_BOLD); +} + +void simpc_ui_init(void) {} + +void simpc_ui_cleanup(void) {} + +void simpc_ui_confirm_finish(void) {} diff --git a/eater_cpu/cpp/simpc_types.h b/eater_cpu/cpp/simpc_types.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/eater_cpu/cpp/simpc_types.h @@ -0,0 +1 @@ +#pragma once diff --git a/eater_cpu/cpp/simpc_ui.h b/eater_cpu/cpp/simpc_ui.h new file mode 100644 index 0000000..d840506 --- /dev/null +++ b/eater_cpu/cpp/simpc_ui.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Veater_computer.h" +#include "simpc_types.h" + +#include <memory> + +void simpc_ui_init(void); +void simpc_ui_write(const std::unique_ptr<Veater_computer> &topp, uint64_t i); +void simpc_ui_finish_message(const std::unique_ptr<VerilatedContext> &contextp, + const std::unique_ptr<Veater_computer> &topp); +void simpc_ui_confirm_finish(void); +void simpc_ui_cleanup(void); |
