-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp.save
785 lines (564 loc) · 16.6 KB
/
main.cpp.save
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
extern "C" {
#include "cpu.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
}
#define RAM_SIZE 16*1024
#define ROM_SIZE 32*1024
#define EXIT_DEBUGGER -1
#define QUIT_APPLICATION -2
#define INVALID_COMMAND 0
#define DBG_CMD_PROCEED 1
#define DBG_CMD_REMAIN 2
struct termios oldit, newit, oldot, newot;
unsigned char* REN_rombuf;
unsigned char* REN_rambuf;
int romsize;
byte input_register;
byte output_register;
byte test_value = 0;
int tx_requested = 0;
byte spi_in = 0;
byte spi_out = 0;
int shifter = 0;
word32 last_time = 0;
word32 last_poll = 0;
int debugger_is_on = 0;
int debugger_is_enabled = 0;
int interrupts_enabled = 1;
void (*on_spi_complete)(void) = 0;
void EMUL_handleWDM(byte opcode, word32 timestamp) {
return;
}
class IOWrapper {
public static int keySt
}
int kbhit();
int getch();
void spi_print();
void add_IRQ(unsigned int mask) {
if(interrupts_enabled)
CPU_addIRQ(mask);
}
int read_line(char* buffer, unsigned int buf_size) {
char temp_char = 0;
unsigned int char_count = 0;
while(1) {
while(!kbhit());
temp_char = getch() & 0xFF;
buffer[char_count] = temp_char;
printf("%c", temp_char); fflush(stdout);
if(temp_char == '\n')
break;
char_count++;
if(char_count == (buf_size - 1)) {
char_count++;
break;
}
}
buffer[char_count] = 0;
return char_count;
}
typedef int (*DebugCommandFunction)(int, char**);
int debug_command_step(int argc, char* argv[]) {
return DBG_CMD_PROCEED;
}
int debug_command_run(int argc, char* argv[]) {
debugger_is_on = 0;
return DBG_CMD_PROCEED;
}
int hex24_to_int(char* hex_string) {
char* ptr;
if(strlen(hex_string) != 6)
return -1;
return strtol(hex_string, &ptr, 16);
}
#define BP_MODE_CHECK 0
#define BP_MODE_INSERT 1
#define BP_MODE_REMOVE -1
#define BP_MODE_COUNT 2
#define BP_MODE_GET 3
#define BP_MODE_CLEAR_ALL 4
int breakpoint_check(int address, int mode) {
static unsigned int bp_count = 0;
static int* bp_entry = (int*)0;
int found_index = -1;
int i;
if(mode == 2) { //Mode 2 == get entry count
return (int)bp_count;
}
if(address > 0xFFFFFF || address < 0)
return -1; //Bad address
if(mode == 3) { //Mode 3 == get entry at index
if(address >= bp_count)
return 0; //Address not found
return (int)bp_entry[address];
}
if(mode == 4) {
bp_count = 0;
if(bp_entry)
free(bp_entry);
bp_entry = (int*)0;
}
for(i = 0; i < bp_count; i++)
if(bp_entry[i] == address) {
found_index = i;
break;
}
if(found_index == -1 && mode == 1) { //mode 1 == insert breakpoint
bp_entry = (int*)realloc((void*)bp_entry, sizeof(int) * (bp_count + 1));
bp_entry[bp_count++] = address;
}
if(found_index != -1 && mode == -1) { //mode -1 == delete breakpoint
for(i = found_index; i < bp_count - 1; i++)
bp_entry[found_index] = bp_entry[found_index + 1];
bp_count--;
}
return found_index != -1;
}
int debug_command_break(int argc, char* argv[]) {
if(argc != 2 && argc != 1) {
printf("Wrong number of arguments.\nUsage: break <24-bit-hex-address | clear>\n");
fflush(stdout);
return DBG_CMD_REMAIN;
}
if(argc == 1) {
int i;
for(i = 0; i < breakpoint_check(0, BP_MODE_COUNT); i++) {
printf(" [%d] 0x%06X\n", i, breakpoint_check(i, BP_MODE_GET));
fflush(stdout);
}
} else if(!strcmp(argv[1], "clear")) {
breakpoint_check(0, BP_MODE_CLEAR_ALL);
printf("Breakpoints cleared\n");
fflush(stdout);
} else {
int address = hex24_to_int(argv[1]);
if(address < 0) {
printf("Could not parse the given address '%s'\n", argv[1]);
fflush(stdout);
return DBG_CMD_REMAIN;
}
switch(breakpoint_check(address, BP_MODE_INSERT)) {
case 0:
printf("Breakpoint added at %06X\n", address);
fflush(stdout);
break;
case -1:
printf("Bad breakpoint address\n");
fflush(stdout);
break;
default:
printf("Breakpoint already exists\n");
fflush(stdout);
break;
}
}
return DBG_CMD_REMAIN;
}
int debug_command_toggleint(int argc, char* argv[]) {
if(argc != 1) {
printf("toggleint takes no arguments\n");
fflush(stdout);
return DBG_CMD_REMAIN;
}
interrupts_enabled = !interrupts_enabled;
printf("interrupts are now %s\n", interrupts_enabled ? "ON" : "OFF");
fflush(stdout);
return DBG_CMD_REMAIN;
}
int debug_command_args(int argc, char* argv[]) {
int i;
for(i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
fflush(stdout);
}
return DBG_CMD_REMAIN;
}
int debug_command_exam(int argc, char* argv[]) {
int count = 16;
int address = 0;
int i;
if(argc != 2 && argc != 3) {
printf("Usage: exam [base-address] <count>\n");
fflush(stdout);
return DBG_CMD_REMAIN;
}
if(argc == 3)
count = atoi(argv[2]);
address = hex24_to_int(argv[1]);
if(address < 0) {
printf("Bad address.\n");
fflush(stdout);
return DBG_CMD_REMAIN;
}
printf("%06X: ", address);
fflush(stdout);
for(i = 0; i < count; i++) {
printf("%02X", MEM_readMem((int)address + i, 0, 0));
if(i == count - 1)
printf("\n");
else
printf(" ");
}
return DBG_CMD_REMAIN;
}
char** string_to_argv(char* input_buffer, int* argc) {
char** return_array = (char**)0;
int building = 0;
char read_buffer[256];
int chars_read;
*argc = 0;
while(1) {
if(building) {
if(*input_buffer <= ' ') {
return_array[(*argc) - 1] = (char*)malloc(sizeof(char) * (chars_read + 1));
memcpy((void*)return_array[(*argc) - 1], (void*)read_buffer, chars_read);
return_array[(*argc) - 1][chars_read] = 0;
building = 0;
if(*input_buffer == 0)
break;
continue;
}
read_buffer[chars_read++] = *input_buffer++;
} else {
if(*input_buffer <= ' ') {
if(*input_buffer == 0)
break;
input_buffer++;
continue;
}
return_array = (char**)realloc((void*)return_array, sizeof(char*) * (++(*argc))); //This can fail. That would be bad.
building = 1;
chars_read = 0;
}
}
return return_array;
}
void free_argv_memory(int argc, char* argv[]) {
int i;
for(i = 0; i < argc; i++)
free((void*)argv[i]);
free((void*)argv);
}
int parse_debug_command(char* command_buffer) {
#define COMMAND_COUNT 6
char* command_entry[COMMAND_COUNT] = {
"step",
"run",
"break",
"args",
"exam",
"toggleint"
};
DebugCommandFunction command_function[COMMAND_COUNT] = {
debug_command_step,
debug_command_run,
debug_command_break,
debug_command_args,
debug_command_exam,
debug_command_toggleint
};
int argc;
int retval = 0;
char** argv = string_to_argv(command_buffer, &argc);
if(argc == 0)
return 0;
int i;
for(i = 0; i < COMMAND_COUNT; i++)
if(!strcmp(command_entry[i], argv[0])) {
retval = command_function[i](argc, argv); //TODO: Split command buffer into argvals
break;
}
free_argv_memory(argc, argv);
return retval;
}
void do_debugger(void) {
static char in_buf[256] = {0};
int parse_result = 0;
int execution_paused = 1;
CPU_debug();
while(execution_paused) {
printf("dbg>"); fflush(stdout);
read_line(in_buf, 256);
switch(parse_debug_command(in_buf)) {
case DBG_CMD_PROCEED:
execution_paused = 0;
break;
case DBG_CMD_REMAIN:
break;
default:
printf("Unknown debugger command.\n"); fflush(stdout);
break;
}
}
}
void EMUL_hardwareUpdate(word32 timestamp) {
static int oldE = 0;
static double old_timestamp = 0;
static double old_time = 0;
static double timef = 0;
static byte tx_buffer, rx_buffer;
struct timespec t;
double clocks_elapsed;
double time_elapsed;
static long old_nsecs, new_nsecs, nsec_diff;
static int i_count = 0;
if(breakpoint_check(PC.A, BP_MODE_CHECK) > 0) {
printf("[Breakpoint encountered at %06X]\n", PC.A);
fflush(stdout);
debugger_is_on = 1;
}
if(debugger_is_on)
do_debugger();
//I want to do this based on time and not instruction count in the future
//May even be possible to put this into a timer thread so that the interrupt
//in the virtualized system is functionally mapped to an interrupt in the
//host machine
//if(i_count++ > 5000000) {
//i_count = 0;
//printf("[Interrupt fired]\n");
//fflush(stdout);
//CPU_addIRQ(1);
//}
//CPU emulation throttling
while(1) {
clock_gettime(CLOCK_REALTIME, &t);
new_nsecs = t.tv_nsec;
if(new_nsecs < old_nsecs)
nsec_diff = (new_nsecs + 1000000000) - old_nsecs;
else
nsec_diff = new_nsecs - old_nsecs;
if(nsec_diff >= 200) {
break;
}
}
old_nsecs = new_nsecs;
/*
clocks_elapsed = timestamp - old_timestamp;
if(clocks_elapsed >= 12000000) {
timef = ((double)t.tv_sec * 1000) + ((double)t.tv_nsec / 1.0e6);
time_elapsed = timef - old_time;
printf("IPS: %f/%f = %f\n", clocks_elapsed, time_elapsed, (1000 * clocks_elapsed)/time_elapsed);
old_time = timef;
old_timestamp = (double)timestamp;
}
*/
if(oldE != E) {
int i;
if(!E) printf("[Emulation mode off]\n");
//for(i = 0; i < 256; i++)
//printf("%02X ", REN_rambuf[i]);
oldE = E;
}
if(tx_requested) {
if(last_time == 0) {
//Init new transfer
rx_buffer = 0;
tx_buffer = spi_out;
shifter = 0;
last_time = timestamp;
input_register = 0;
add_IRQ(1);
} else {
//Wait 100 cycles between steps
if(timestamp >= last_time + 100) {
last_time = timestamp;
if(input_register & 0x01) {
//^clock was high last step
//Get the current input value and then clock it low
rx_buffer = (((output_register & 0x1) * 0x80) | ((rx_buffer >> 1 )& 0x7F));
input_register = input_register & 0xFE;
//Increase the clock count, check for cycle completion, write any received value
shifter++;
if(shifter == 8) {
tx_requested = 0;
spi_in = rx_buffer;
//if(spi_out || spi_in)
// printf("[SPI: S-%02X R-%02X]\n", spi_out, spi_in);
if(on_spi_complete)
on_spi_complete();
}
} else {
//^clock was low last step
//Clock it high and set the next data out value
input_register = ((tx_buffer & 0x80) >> 6) | 0x01;
tx_buffer = tx_buffer << 1;
}
}
}
} else {
if(kbhit()) {
//Queue a transfer (should have a helper function to do all of this)
last_poll = timestamp;
spi_out = (byte)(getch() & 0xFF);
tx_requested = 1;
on_spi_complete = spi_print;
last_time = 0;
add_IRQ(1);
} else if(timestamp >= last_poll + 10000) {
last_poll = timestamp;
spi_out = 0;
tx_requested = 1;
on_spi_complete = spi_print;
last_time = 0;
}
}
}
byte MEM_readMem(word32 address, word32 timestamp, word32 emulFlags) {
byte b = 0;
if(address & 0x8000)
b = REN_rombuf[address & 0x7FFF];
else if(address & 0x4000)
{ b = input_register; }
else
b = REN_rambuf[address & 0x3FFF];
return b;
}
void MEM_writeMem(word32 address, byte b, word32 timestamp) {
if(address & 0x8000)
output_register = b;
else if(!(address & 0x4000)) {
if(address == 0x0000FF) {
//We'll set ACTUAL breakpoints after this
//debugger_is_on = debugger_is_on ? 0 : breakpoints_are_enabled;
//CPU_setTrace(debugger_is_on);
}
REN_rambuf[address & 0x3FFF] = b;
/*
if(address >= 0x0200 && address < 0x0300) {
int i;
for(i = 0; i < 10; i++)
printf("%02X ", REN_rambuf[0x200 + i]);
printf("\n");
}
*/
}
//Anything we try to write to 0x4000 - 0x7FFF should be dumped to space
}
int kbhit() {
struct timeval tv = { 0L, 0L };
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv);
}
int getch() {
int r;
unsigned char c;
if((r = read(0, &c, sizeof(c))) < 0)
return r;
else
return c;
}
//TODO
int input(char* inbuf, int bufsz) {
while(!getch());
}
void int_handler(int value) {
if(debugger_is_on) {
system("reset");
exit(0);
}
debugger_is_on = 1;
}
void spi_print(void) {
static byte rx_count = 0;
static int printing = 0;
if(printing) {
printf("%c", spi_in);
fflush(stdout);
rx_count--;
} else {
if(spi_in) {
//printf("[Found 0x%02X pending chars]\n", spi_in);
rx_count = spi_in;
printing = 1;
}
}
if(rx_count) {
spi_out = 0x00;
tx_requested = 1;
on_spi_complete = spi_print;
last_time = 0;
} else {
printing = 0;
}
}
int main(int argc, char* argv[]) {
//static struct termios oldit, newit, oldot, newot;
//Get args
if(argc > 1) {
int i;
for(i = 1; i < argc; i++) {
if(!strcmp(argv[i], "--debug"))
debugger_is_on = 1;
}
}
//debugger_is_on = debugger_is_enabled;
CPU_reset();
//CPU_setTrace(0)
CPUEvent_initialize();
CPU_setUpdatePeriod(1);
//Set stdin to non-blocking
fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);
//Turn off line-at-a-time
tcgetattr(STDIN_FILENO, &oldit);
newit = oldit;
newit.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &newit);
tcgetattr(STDOUT_FILENO, &oldot);
newot = oldot;
newot.c_lflag &= ~(ICANON);
tcsetattr(STDOUT_FILENO, TCSANOW, &newot);
FILE* romfile = fopen("boot.rom", "rb");
if(!romfile) {
printf("Couldn't find ROM image 'boot.rom'.\n");
return 0;
}
fseek(romfile, 0, SEEK_END);
romsize = ftell(romfile);
rewind(romfile);
if(romsize > ROM_SIZE) {
printf("System rom image size greater than 32k.\n");
fclose(romfile);
return 0;
}
REN_rombuf = (unsigned char*)malloc(ROM_SIZE);
if(!REN_rombuf) {
printf("Couldn't allocate ROM memory.\n");
fclose(romfile);
return 0;
}
if(fread(REN_rombuf, 1, romsize, romfile) != romsize) {
printf("Couldn't read ROM image.\n");
fclose(romfile);
free(REN_rombuf);
return 0;
}
//fclose(romfile);
REN_rambuf = (unsigned char*)malloc(RAM_SIZE);
if(!REN_rambuf) {
printf("Couldn't allocate RAM memory.\n");
free(REN_rombuf);
return 0;
}
input_register = 0;
printf("Starting execution\n");
signal(SIGINT, int_handler);
CPU_run();
fclose(romfile);
//Reset console mode
fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~O_NONBLOCK);
oldit.c_lflag |= ICANON | ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &oldit);
tcsetattr(STDOUT_FILENO, TCSANOW, &oldot);
return 0;
}