-
Notifications
You must be signed in to change notification settings - Fork 8
/
spike-gui.cpp
281 lines (251 loc) · 9.94 KB
/
spike-gui.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Labarc riscv-isa-sim spike graphic user interface
// This program is free software; you can
// redistribute it and/or modify it under the terms of either the GNU
// Lesser General Public License Version 3 or the Perl Artistic License
// Version 2.1.
// Icaro Dantas de Araujo Lima and Elmar Melcher at UFCG, 2021
// This program is based in part on the work of the FLTK project (http://www.fltk.org)
#include "communicator.h"
#include <fstream>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Input.H>
using std::cerr;
using std::string;
using std::vector;
using std::ifstream;
using std::find_if;
using std::size_t;
using std::distance;
using std::begin;
#define MAX_MEMS 10 // maximum active memory positions
#define NCHRS_DA_LINE 60 // number of characters in a dissassembly line
#define DISPLAY_FONT ((Fl_Font)55)
#define DISPLAY_FONT_SIZE 13
#define DISPLAY_FONT_WIDTH 8
#define DISPLAY_FONT_HEIGHT 18
#define NSTYLES 4 // number of styles used for disassembly
#define HELP_FONT ((Fl_Font)56)
#define HELP_FONT_SIZE 10
const char *mono_fonts[] = { "Noto Mono",
"Consolas",
"Monospace",
"Droid Sans Mono",
"Lucida Console",
"DejaVu Sans Mono",
"FreeMono", "" };
// define sub-windows geometries
#define BORDER 10
#define COMMAND_WIDTH 200
#define COMMAND_HEIGHT 20
#define PTEXT_OFFSET 2*BORDER+COMMAND_HEIGHT
#define PTEXT_LINES 5
#define DISA_OFFSET PTEXT_OFFSET+DISPLAY_FONT_HEIGHT*PTEXT_LINES+BORDER
#define DISA_LINES 7
#define DISA_PC_LINE 2
#define REGS_OFFSET DISA_OFFSET+DISPLAY_FONT_HEIGHT*DISA_LINES+BORDER
#define REGS_LINES 9
#define MEMS_OFFSET REGS_OFFSET+DISPLAY_FONT_HEIGHT*REGS_LINES+BORDER
#define MEMS_LINES 5
#define WINDOW_HEIGHT MEMS_OFFSET+DISPLAY_FONT_HEIGHT*MEMS_LINES+BORDER
#define WINDOW_WIDTH 2*BORDER+((6+2+8)*4+2)*DISPLAY_FONT_WIDTH
class spike {
public:
spike(int w, int h);
communicator *sock; // socket comunicator with spike
void update(); // update register window
static void cmd_cb(Fl_Widget *, void *); // static function called by callback
Fl_Window window; // Window representing ISA simulator
private:
Fl_Input command; // input debug command
Fl_Text_Display ptext; //response display
Fl_Text_Buffer *pbuff; // response text buffer - use pointer to avoid cb remove error
Fl_Text_Display disa; // disassembly display
Fl_Text_Buffer *dbuff; // disassembly text buffer
Fl_Text_Buffer *stylebuf;
Fl_Text_Display::Style_Table_Entry
styletable[NSTYLES] = { // Style table
{ FL_BLACK, DISPLAY_FONT, DISPLAY_FONT_SIZE+1 }, // A - Plain
{ FL_DARK_RED, DISPLAY_FONT, DISPLAY_FONT_SIZE+1 }, // B - PC position
{ FL_DARK3, DISPLAY_FONT, DISPLAY_FONT_SIZE+1 }, // C - not PC position
{ FL_BLACK, DISPLAY_FONT, DISPLAY_FONT_SIZE+1 } // D - do not use
}; // +1 makes font size equal do default
Fl_Text_Display regs; //register display
Fl_Text_Buffer *rbuff; // register display text buffer
Fl_Text_Display mems; // memory display
Fl_Text_Buffer *mbuff; // memory display text buffer
Fl_Window help_window; // help window
Fl_Text_Display htext; // help text display
Fl_Text_Buffer *hbuff; // help text buffer
vector<string> disa_lines; // disassembled code, line by line
char style_str[(NCHRS_DA_LINE+1)*DISA_LINES]; // style buffer content
string memcmds[MAX_MEMS]; // memory commands to be shown in memory window
void cmd_caba(); // normal function called by callback function through cmd_cb()
void disa_file(); // read disassembled code from file
void help(); // show help window
};
spike::spike(int w, int h) :
window(w, h, "RISC-V ISA simulator"),
command(BORDER, BORDER, COMMAND_WIDTH, COMMAND_HEIGHT),
ptext(BORDER,PTEXT_OFFSET,w-2*BORDER,DISPLAY_FONT_HEIGHT*PTEXT_LINES+1),
disa(BORDER,DISA_OFFSET,w-2*BORDER,DISPLAY_FONT_HEIGHT*DISA_LINES+1),
regs(BORDER,REGS_OFFSET,w-2*BORDER,DISPLAY_FONT_HEIGHT*REGS_LINES+1),
mems(BORDER,MEMS_OFFSET,w-2*BORDER,DISPLAY_FONT_HEIGHT*MEMS_LINES+1),
help_window(w,h, "Spike Help"),
htext(BORDER,BORDER,w-2*BORDER,h-2*BORDER) {
int i=0;
do { // search for an existing mono-space font
Fl::set_font(DISPLAY_FONT, mono_fonts[i++]);
fl_font(DISPLAY_FONT, DISPLAY_FONT_SIZE);
} while( fl_width('W') != fl_width('i') && strlen(mono_fonts[i]) );
// set help font to a different font if there is another one
if (strlen(mono_fonts[i])) Fl::set_font(HELP_FONT, mono_fonts[i]);
else Fl::set_font(HELP_FONT, mono_fonts[i-1]);
// Command
command.align(FL_ALIGN_RIGHT);
command.label("h for help");
command.callback(cmd_cb, (void *)this); // static function needed here
command.when(FL_WHEN_ENTER_KEY|FL_WHEN_NOT_CHANGED);
// Response
ptext.textfont(DISPLAY_FONT);
pbuff = new Fl_Text_Buffer();
pbuff->text("Command response");
ptext.buffer(pbuff);
// Disassmbly
disa.textfont(DISPLAY_FONT);
disa_file();
dbuff = new Fl_Text_Buffer();
stylebuf = new Fl_Text_Buffer();
memset(style_str, 'C', (NCHRS_DA_LINE+1)*DISA_LINES-1);
// only line no DISA_PC_LINE will be highlighted
memset(style_str+(NCHRS_DA_LINE+1)*DISA_PC_LINE, 'B', NCHRS_DA_LINE);
stylebuf->text(style_str);
disa.buffer(dbuff);
// Registers
regs.textfont(DISPLAY_FONT);
rbuff = new Fl_Text_Buffer();
regs.buffer(rbuff);
// Memory
mems.textfont(DISPLAY_FONT);
mbuff = new Fl_Text_Buffer();
mems.buffer(mbuff);
for(int i; i<MAX_MEMS; i++) memcmds[i] = "";
window.end();
}
void spike::disa_file() { // read disassembly file into string vector
#define DISA_FILE "disa.txt"
ifstream disa_file (DISA_FILE);
if (disa_file.is_open()) {
string li;
// first lines are empty, so we can always show the line before PC
li.append(NCHRS_DA_LINE, ' ');
for(int i=0; i<DISA_PC_LINE; i++) disa_lines.push_back(li);
while (getline (disa_file, li)) {
if (li[8] == ':') // replace leading spaces of memory address by 0's
for(int i=0; i<8; i++)
if (li[i] == ' ') li[i] = '0';
li.append(NCHRS_DA_LINE - li.length(), ' ');
disa_lines.push_back(li);
}
disa_file.close();
li = ""; // append enough empty lines
li.append(NCHRS_DA_LINE, ' ');
for(int i=0; i<DISA_LINES; i++) disa_lines.push_back(li);
} else {
cerr << "Unable to open file " DISA_FILE;
}
}
void spike::update() {
string pc = sock->send_and_rec("pc 0");
// Disassembly
string pcd = pc.substr(2,8) + ":"; // search for disassmbly line starting with pc
vector<string>::iterator it = find_if(disa_lines.begin(),
disa_lines.end(),
[pcd](const string& str) {
return str.substr(0,9) == pcd;
}
);
if(it != disa_lines.end()) { // if found
size_t i = distance(begin(disa_lines), it); // i>0 always
string lis;
for(int j=-DISA_PC_LINE; j<DISA_LINES-DISA_PC_LINE; j++) { // collect lines arround pc
lis += disa_lines[i+j];
if(j<DISA_LINES-DISA_PC_LINE-1) lis += '\n';
}
dbuff->text(lis.c_str());
disa.highlight_data(stylebuf, styletable, NSTYLES, 'D', NULL, NULL);
} else dbuff->text("");
// Registers
string rg = sock->send_and_rec("reg 0");
rg.erase( rg.end()-1 );
rbuff->text((" pc: " + pc + rg).c_str());
// Memory
string m;
if (memcmds[0].c_str()[0]==0) m = "Memory - use mem command"; // hint if empty
for (int i=0; memcmds[i].c_str()[0]; i++) // for all slots which are not empty
m += memcmds[i] + ": " + sock->send_and_rec(memcmds[i]);
mbuff->text(m.c_str());
}
void spike::help() {
hbuff = new Fl_Text_Buffer();
hbuff->text(sock->send_and_rec(command.value()));
fl_font(HELP_FONT, HELP_FONT_SIZE);
htext.textfont(HELP_FONT);
htext.buffer(hbuff);
help_window.show();
}
void spike::cmd_cb(Fl_Widget *, void *ptr) { // static function needed for callback
((spike *)ptr)->cmd_caba(); // which calls a normal function
}
void spike::cmd_caba() {
if (command.value()[0] == 'h') { // help
help();
} else if ( command.value()[0] == 'q' || // quit
(command.value()[0] == 'r' && // rs without argument
command.value()[1] == 's' &&
command.value()[2] == 0 ) ) {
sock->send_and_rec(command.value());
exit(0);
} else if (strncmp(command.value(), "mem", 3)==0) {
int i;
for (i=0; memcmds[i].c_str()[0]; i++); // find first empty slot
if (i==MAX_MEMS-1) { // last position reached
for (i=0; i<MAX_MEMS-1; i++) memcmds[i] = memcmds[i+1]; // shift up
i = MAX_MEMS-2;
}
memcmds[i] = command.value();
update();
} else {
pbuff->text(sock->send_and_rec(command.value()));
update();
}
}
int main(int argc, char** argv, char** env) {
if (argc < 2)
{
cerr << "Usage: " << argv[0] << " [<host>] <port>\n";
return 1;
}
char *host;
char *port;
int argc_offset;
if(argc==2) { // default for host is localhost
host = (char *)"localhost";
port = argv[1];
argc_offset = 1;
} else {
host = argv[1];
port = argv[2];
argc_offset = 2;
}
spike *spike_ptr = new spike(WINDOW_WIDTH, WINDOW_HEIGHT);
spike_ptr->sock = new communicator(host, port);
spike_ptr->update();
spike_ptr->window.show(argc-argc_offset,argv+argc_offset); // dirty argv[0] :-(
Fl::run(); // run the graphical interface which calls cmd_cb()
spike_ptr->sock->send_and_rec("q"); // when window is closed, send quit command to spike
delete spike_ptr;
// Fin
exit(0);
}