forked from RudolfGeosits/MSP430-Emulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
351 lines (316 loc) · 9.22 KB
/
main.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
MSP430 Emulator
Copyright (C) 2020 Rudolf Geosits ([email protected])
"MSP430 Emulator" is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
"MSP430 Emulator" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "main.h"
#include <stdio.h>
#include <fcntl.h>
#include "debugger/io.h"
static void printVersion()
{
printf(PROGRAM_NAME " version " VERSION_STRING "\n");
printf("Compiled @ " __DATE__ " : " __TIME__ "\n");
}
static void printHelp()
{
printVersion();
printf("The following options are supported:\n");
printf("-b NAME Load binary file\n");
printf("-v Print program version\n");
printf("-h Print this help\n");
printf("-m [web|cli] Set mode to webserver(default)/commandline\n");
printf("-p INTEGER Set webserver port\n");
printf("-i NAME Set USCI input pipe/file\n");
printf("-o NAME Set USCI output pipe/file\n");
printf("-d NAME Set DIGITAL I/O PORT 1 output pipe/file\n");
}
static bool checkEmulatorConfig(Emulator* const emu)
{
switch (emu->mode)
{
case Emulator_Mode_Web:
if (emu->port < 0)
{
printf("Need port argument\n");
return false;
}
break;
}
return true;
}
static bool parseMode(char* const modeString, Emulator_Mode* const mode)
{
if (strcmp("web", modeString) == 0)
{
*mode = Emulator_Mode_Web;
return true;
} else if (strcmp("cli", modeString) == 0)
{
*mode = Emulator_Mode_Cli;
return true;
}
return false;
}
static bool setEmulatorConfig(Emulator* const emu, int argc, char *argv[])
{
int option;
emu->do_trace = false;
emu->mode = Emulator_Mode_Web;
emu->port = -1;
emu->binary = NULL;
emu->usci_input_pipe_fd = 0;
emu->usci_output_pipe_fd = 0;
emu->usci_input_pipe_name = NULL;
emu->usci_output_pipe_name = NULL;
while ((option = getopt(argc, argv, "hvm:p:b:i:o:d:")) != -1)
{
switch (option)
{
case 'h':
printHelp();
return false;
case 'v':
printVersion();
return false;
case 'm':
if (!parseMode(optarg, &(emu->mode)))
return false;
break;
case 'p':
emu->port = strtoul(optarg, NULL, 10);
break;
case 'b':
emu->binary = optarg;
break;
case 'i':
emu->usci_input_pipe_name = optarg;
break;
case 'o':
emu->usci_output_pipe_name = optarg;
break;
case 'd':
emu->port1_output_pipe_name = optarg;
break;
default:
printf("Unknown option\n");
return false;
}
}
if (!checkEmulatorConfig(emu))
{
printHelp();
return false;
}
return true;
}
static bool startWebServer(Emulator* const emu)
{
Debugger* const deb = emu->debugger;
deb->ws_port = emu->port;
pthread_t *t = &deb->web_server_thread;
if ( pthread_create(t, NULL, web_server, (void *)emu ) )
{
fprintf(stderr, "Error creating web server thread\n");
return false;
}
while (!deb->web_server_ready)
usleep(10000);
print_console(emu, " [MSP430 Emulator]\n Copyright (C) 2020 Rudolf Geosits ([email protected])\n");
print_console(emu, " [!] Upload your firmware (ELF format only); Type 'h' for debugger options.\n\n");
while (!deb->web_firmware_uploaded)
usleep(10000);
return true;
}
static void initializeMsp430(Emulator* const emu)
{
emu->cpu = (Cpu *) calloc(1, sizeof(Cpu));
emu->cpu->bcm = (Bcm *) calloc(1, sizeof(Bcm));
emu->cpu->timer_a = (Timer_a *) calloc(1, sizeof(Timer_a));
emu->cpu->p1 = (Port_1 *) calloc(1, sizeof(Port_1));
emu->cpu->usci = (Usci *) calloc(1, sizeof(Usci));
initialize_msp_memspace();
initialize_msp_registers(emu);
setup_bcm(emu);
setup_timer_a(emu);
setup_port_1(emu);
setup_usci(emu);
}
static void deinitializeMsp430(Emulator* const emu)
{
uninitialize_msp_memspace();
Cpu* const cpu = emu->cpu;
free(cpu->timer_a);
free(cpu->bcm);
free(cpu->p1);
free(cpu->usci);
free(cpu);
}
static bool openUsciPipes(Emulator* const emu)
{
if (emu->usci_input_pipe_name != NULL)
{
emu->usci_input_pipe = fopen(emu->usci_input_pipe_name, "rb");
if (emu->usci_input_pipe == NULL)
{
print_console(emu, "Cannot open USCI input pipe\n");
return false;
}
emu->usci_input_pipe_fd = fileno(emu->usci_input_pipe);
int flags = fcntl(emu->usci_input_pipe_fd, F_GETFL, 0);
fcntl(emu->usci_input_pipe_fd, F_SETFL, flags | O_NONBLOCK);
}
if (emu->usci_output_pipe_name != NULL)
{
emu->usci_output_pipe = fopen(emu->usci_output_pipe_name, "wb");
if (emu->usci_output_pipe == NULL)
{
print_console(emu, "Cannot open USCI output pipe\n");
return false;
}
emu->usci_output_pipe_fd = fileno(emu->usci_output_pipe);
}
return true;
}
static bool closeUsciPipes(Emulator* const emu)
{
if (emu->usci_input_pipe != NULL)
{
fclose(emu->usci_input_pipe);
emu->usci_input_pipe = NULL;
emu->usci_input_pipe_fd = -1;
}
if (emu->usci_output_pipe != NULL)
{
fclose(emu->usci_output_pipe);
emu->usci_output_pipe = NULL;
emu->usci_output_pipe_fd = -1;
}
return true;
}
static bool openDioPipes(Emulator* const emu)
{
if (emu->port1_output_pipe_name != NULL)
{
emu->port1_output_pipe = fopen(emu->port1_output_pipe_name, "wb");
if (emu->port1_output_pipe == NULL)
{
print_console(emu, "Cannot open DIGITAL I.O PORT 1 output pipe\n");
return false;
}
emu->port1_output_pipe_fd = fileno(emu->port1_output_pipe);
}
return true;
}
static bool closeDioPipes(Emulator* const emu)
{
if (emu->port1_output_pipe != NULL)
{
fclose(emu->port1_output_pipe);
emu->port1_output_pipe = NULL;
emu->port1_output_pipe_fd = -1;
}
return true;
}
static void handleCommanding(Emulator* const emu)
{
Cpu* const cpu = emu->cpu;
// Handle debugger when CPU is not running
if (!cpu->running)
{
switch (emu->mode)
{
case Emulator_Mode_Web:
usleep(10000);
break;
case Emulator_Mode_Cli:
{
char* buffer = readline(NULL);
const int bufferLength = strlen(buffer);
exec_cmd(emu, buffer, bufferLength);
free(buffer);
}
break;
}
}
}
static void handleProcessingStep(Emulator* const emu)
{
Cpu* const cpu = emu->cpu;
if (!cpu->running)
return;
// Handle Breakpoints
if (handle_breakpoints(emu))
return;
// Instruction Decoder
decode(emu, fetch(emu, true), EXECUTE);
// Handle Peripherals
handle_bcm(emu);
handle_timer_a(emu);
handle_port_1(emu);
handle_usci(emu);
// Average of 4 cycles per instruction
mclk_wait_cycles(emu, 4);
}
int mainInernal(int argc, char *argv[], Emulator* const emu)
{
Debugger* const deb = emu->debugger;
if (!setEmulatorConfig(emu, argc, argv))
return 0;
initializeMsp430(emu);
Cpu* const cpu = emu->cpu;
setup_debugger(emu);
if (emu->mode == Emulator_Mode_Web)
{
if (!startWebServer(emu))
return -1;
}
if (emu->mode == Emulator_Mode_Cli)
register_signal(SIGINT); // Register Callback for CONTROL-c
if (emu->binary != NULL)
load_firmware(emu, emu->binary, 0xC000);
if (!openUsciPipes(emu))
{
closeUsciPipes(emu);
return -1;
}
if (!openDioPipes(emu))
{
closeDioPipes(emu);
return -1;
}
// display first round of registers
display_registers(emu);
disassemble(emu, cpu->pc, 1);
update_register_display(emu);
// Fetch-Decode-Execute Cycle (run machine)
while (!deb->quit)
{
handleCommanding(emu);
handleProcessingStep(emu);
}
closeUsciPipes(emu);
closeDioPipes(emu);
deinitializeMsp430(emu);
return 0;
}
int main(int argc, char *argv[])
{
Emulator *emu = (Emulator *) calloc( 1, sizeof(Emulator) );
emu->debugger = (Debugger *) calloc(1, sizeof(Debugger));
emu->debugger->server = (Server *) calloc(1, sizeof(Server));
const int result = mainInernal(argc, argv, emu);
free(emu->debugger->server);
free(emu->debugger);
free(emu);
return result;
}