summaryrefslogtreecommitdiff
path: root/nandgame/cpp/simpc_ui.cpp
blob: a17cbb941907dc4f60476ef146144aa2267b2d40 (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
#include "simpc_ui.h"

#define NCUR 1
#include "disas.h"

#define NCUR_OFFSET 3
#define NCUR_X 5

#if NCUR
#include <ncurses.h>
#define NCUR_DELAY_MS 100

#define PRINT_ME(y, x, ...)                                                    \
  { mvprintw(y, x, __VA_ARGS__); }
#define PRINT_NEXT()                                                           \
  {                                                                            \
    refresh();                                                                 \
    napms(NCUR_DELAY_MS);                                                      \
  }

#else

#define PRINT_ME(y, x, ...)                                                    \
  {                                                                            \
    printf("%*c", x, ' ');                                                     \
    printf(__VA_ARGS__);                                                       \
    printf("\n");                                                              \
  }
#define PRINT_NEXT()                                                           \
  { puts("-----"); }

#endif

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

bool paused = false;

void simpc_ui_write(const std::unique_ptr<Vcomputer> &topp, uint64_t &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(2, 1, "%-20s", paused ? "Paused" : "Running");
  PRINT_ME(3, 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(5, NCUR_X, "%-80s", insline.c_str());

  PRINT_ME(7, 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(
      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_ME(10, NCUR_X, "--- ROM ---");
  for (int i = -1; i <= 1; i++) {
    const char *prefix = i == 0 ? "> " : "  ";
    uint16_t curadr = topp->computer->PC_addr_int + i;
    const uint16_t p = curadr;
    const uint16_t program_op_code = topp->computer->ROM->r_datastore[p];
    auto disas_code = print_decoded(program_op_code, true);
    PRINT_ME(12 + i, NCUR_X, "%04X  %s%04X  %-30s", curadr, prefix,
             program_op_code, disas_code.c_str());
  }
  // PRINT_ME(7 + NCUR_OFFSET, NCUR_X, "ALU");
  // PRINT_ME(8 + NCUR_OFFSET, NCUR_X, "X: %5d\tY: %5d",
  //          topp->computer->CPU->my_alu->int_op_x,
  //          topp->computer->CPU->my_alu->int_op_y);

  PRINT_ME(getmaxy(stdscr) - 1, 1,
           "q - Quit; p - (Un)pause; any key in pause - step");

  PRINT_NEXT();

  int ch = getch();
  if (ch == 'p') {
    nodelay(stdscr, paused);
    paused = !paused;
  } else if (ch == 'q') {
    simpc_ui_cleanup();
    exit(0);
  }
}

void simpc_ui_finish_message(const std::unique_ptr<VerilatedContext> &contextp,
                             const std::unique_ptr<Vcomputer> &topp) {
  PRINT_ME(1, 30, "Simulation finished.");
  const char *msg;
  if (topp->halt) {
    msg = "Halt encountered.";
  } else if (!contextp->gotFinish()) {
    msg = "Step count exceeded.";
  } else {
    msg = "Regular finish.";
  }
  PRINT_ME(2, 30, "%s", msg);
#if NCUR
  refresh();
#endif
}

void simpc_ui_init(void) {
#if NCUR
  initscr();
  curs_set(0);
  nodelay(stdscr, TRUE);
  noecho();
  cbreak();
#endif
}

void simpc_ui_cleanup(void) {
#if NCUR
  nocbreak();
  echo();
  endwin();
#endif
}

void simpc_ui_confirm_finish(void) {
#if NCUR
  nodelay(stdscr, FALSE);
  getch();
#endif
}