summaryrefslogtreecommitdiff
path: root/nandgame/Vcomputer__main.cpp
blob: 5c3cd1ecbcb4bc716476e1b0b39fed69dcf1e26c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Verilated -*- C++ -*-
// DESCRIPTION: Verilator output: main() simulation loop, created with --main

#include "Vcomputer___024root.h"
#include "Vcomputer_computer.h"
#include "Vcomputer_my_mem__D10_DB10000.h"
#include "verilated.h"
#include "Vcomputer.h"
#include <cinttypes>
#include <cstdint>
#include <cstdio>
#include <sched.h>

#define NCUR 1
#include "../assembler/disas.h"

#if NCUR
#include <ncurses.h>
#define NCUR_DELAY_MS 300
#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(), ""}};

    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();
#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;
#if NCUR
        mvprintw(1, 1, "Step: %10d", i);
#endif

#if NCUR
        mvprintw(1 + ncur_offset, 1,
#else
        printf(
#endif
            "CLK1: %4d\tCLK2: %4d\tPC: @0x%04X\tINS: 0x%04X\n",
            topp->clk_in,
            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);
#if NCUR
            mvprintw(3 + ncur_offset, 5, "%s",
#else
            printf("        %s\n",
#endif
                insline.c_str());
        }
#endif

#if NCUR
        mvprintw(5 + ncur_offset, 5,
#else
        printf("  "
#endif
            "A: 0x%04X\tD: 0x%04X\tM: 0x%04X\tRES: 0x%04X\n",
            topp->computer->reg_A_int,
            topp->computer->reg_D_int,
            topp->computer->reg_pA_int,
            topp->computer->result_int
        );
#if NCUR
        mvprintw(6 + ncur_offset, 5,
#else
        printf("  "
#endif
            "A: %6" PRId16 "\tD: %6" PRId16 "\tM: %6" PRId16 "\tRES: %6" PRId16 "\n",
            topp->computer->reg_A_int,
            topp->computer->reg_D_int,
            topp->computer->reg_pA_int,
            topp->computer->result_int
        );
        // Evaluate model
        topp->eval();
        // Advance time
        contextp->timeInc(1);
        i++;
#if NCUR
        refresh(); napms(NCUR_DELAY_MS);
#else
        puts("-----");
#endif
    }

#if NCUR
    mvprintw(10 + ncur_offset, 10,
#else
    puts(
#endif
        "Simulation finished");

    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();

#if NCUR
    getch(); endwin();
#endif

    return 0;
}