-
Notifications
You must be signed in to change notification settings - Fork 17
/
commands.c
2215 lines (1838 loc) · 45.6 KB
/
commands.c
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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _BSD_SOURCE _BSD_SOURCE
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include "commands.h"
#include "serial.h"
#include "gs4510.h"
int get_sym_value(char* token);
typedef struct
{
int pc;
int a;
int x;
int y;
int z;
int b;
int sp;
int mapl;
int maph;
} reg_data;
typedef struct
{
int addr;
unsigned int b[16];
} mem_data;
bool outputFlag = true;
bool continue_mode = false;
char outbuf[BUFSIZE] = { 0 }; // the buffer of what command is output to the remote monitor
char inbuf[BUFSIZE] = { 0 }; // the buffer of what is read in from the remote monitor
char* type_names[] = { "BYTE ", "WORD ", "DWORD ", "STRING", "DUMP ", "MDUMP " };
bool autocls = false; // auto-clearscreen flag
bool autowatch = false; // auto-watch flag
bool ctrlcflag = false; // a flag to keep track of whether ctrl-c was caught
int traceframe = 0; // tracks which frame within the backtrace
type_command_details command_details[] =
{
{ "?", cmdRawHelp, NULL, "Shows help information for raw/native monitor commands" },
{ "help", cmdHelp, NULL, "Shows help information on m65dbg commands" },
{ "dump", cmdDump, "<addr16> [<count>]", "Dumps memory (CPU context) at given address (with character representation in right-column" },
{ "mdump", cmdMDump, "<addr28> [<count>]", "Dumps memory (28-bit addresses) at given address (with character representation in right-column" },
{ "dis", cmdDisassemble, "[<addr16> [<count>]]", "Disassembles the instruction at <addr> or at PC. If <count> exists, it will dissembly that many instructions onwards" },
{ "mdis", cmdMDisassemble, "[<addr28> [<count>]]", "Disassembles the instruction at <addr> or at PC. If <count> exists, it will dissembly that many instructions onwards" },
{ "c", cmdContinue, "[<addr>]", "continue (until optional <addr>) (equivalent to t0, but more m65dbg-friendly)"},
{ "step", cmdStep, "[<count>]", "Step into next instruction. If <count> is specified, perform that many steps" }, // equate to pressing 'enter' in raw monitor
{ "n", cmdNext, "[<count>]", "Step over to next instruction (software-based, slow). If <count> is specified, perform that many steps" },
{ "next", cmdHardNext, "[<count>]", "Step over to next instruction (hardware-based, fast, xemu-only, for now). If <count> is specified, perform that many steps" },
{ "finish", cmdFinish, NULL, "Continue running until function returns (ie, step-out-from)" },
{ "pb", cmdPrintByte, "<addr>", "Prints the byte-value of the given address" },
{ "pw", cmdPrintWord, "<addr>", "Prints the word-value of the given address" },
{ "pd", cmdPrintDWord, "<addr>", "Prints the dword-value of the given address" },
{ "ps", cmdPrintString, "<addr>", "Prints the null-terminated string-value found at the given address" },
{ "cls", cmdClearScreen, NULL, "Clears the screen" },
{ "autocls", cmdAutoClearScreen, "0/1", "If set to 1, clears the screen prior to every step/next command" },
{ "break", cmdSetBreakpoint, "<addr>", "Sets the hardware breakpoint to the desired address" },
{ "wb", cmdWatchByte, "<addr>", "Watches the byte-value of the given address" },
{ "ww", cmdWatchWord, "<addr>", "Watches the word-value of the given address" },
{ "wd", cmdWatchDWord, "<addr>", "Watches the dword-value of the given address" },
{ "ws", cmdWatchString, "<addr>", "Watches the null-terminated string-value found at the given address" },
{ "wdump", cmdWatchDump, "<addr> [<count>]", "Watches a dump of bytes at the given address" },
{ "wmdump", cmdWatchMDump, "<addr28> [<count>]", "Watches an mdump of bytes at the given 28-bit address" },
{ "watches", cmdWatches, NULL, "Lists all watches and their present values" },
{ "wdel", cmdDeleteWatch, "<watch#>/all", "Deletes the watch number specified (use 'watches' command to get a list of existing watch numbers)" },
{ "autowatch", cmdAutoWatch, "0/1", "If set to 1, shows all watches prior to every step/next/dis command" },
{ "symbol", cmdSymbolValue, "<symbol>", "retrieves the value of the symbol from the .map file" },
{ "save", cmdSave, "<binfile> <addr28> <count>", "saves out a memory dump to <binfile> starting from <addr28> and for <count> bytes" },
{ "load", cmdLoad, "<binfile> <addr28>", "loads in <binfile> to <addr28>" },
{ "back", cmdBackTrace, NULL, "produces a rough backtrace from the current contents of the stack" },
{ "up", cmdUpFrame, NULL, "The 'dis' disassembly command will disassemble one stack-level up from the current frame" },
{ "down", cmdDownFrame, NULL, "The 'dis' disassembly command will disassemble one stack-level down from the current frame" },
{ "se", cmdSearch, "<addr28> <len> <values>", "Searches the range you specify for the given values (either a list of hex bytes or a \"string\""},
{ NULL, NULL, NULL, NULL }
};
char* get_extension(char* fname)
{
return strrchr(fname, '.');
}
typedef struct tfl
{
int addr;
char* file;
int lineno;
struct tfl *next;
} type_fileloc;
type_fileloc* lstFileLoc = NULL;
type_fileloc* cur_file_loc = NULL;
type_symmap_entry* lstSymMap = NULL;
type_offsets segmentOffsets = { 0 };
type_offsets* lstModuleOffsets = NULL;
type_watch_entry* lstWatches = NULL;
void add_to_offsets_list(type_offsets mo)
{
type_offsets* iter = lstModuleOffsets;
if (iter == NULL)
{
lstModuleOffsets = malloc(sizeof(type_offsets));
memcpy(lstModuleOffsets, &mo, sizeof(type_offsets));
lstModuleOffsets->next = NULL;
return;
}
while (iter != NULL)
{
//printf("iterating %s\n", iter->modulename);
// add to end?
if (iter->next == NULL)
{
type_offsets* mo_new = malloc(sizeof(type_offsets));
memcpy(mo_new, &mo, sizeof(type_offsets));
mo_new->next = NULL;
//printf("adding %s\n\n", mo_new->modulename);
iter->next = mo_new;
return;
}
iter = iter->next;
}
}
void add_to_list(type_fileloc fl)
{
type_fileloc* iter = lstFileLoc;
// first entry in list?
if (lstFileLoc == NULL)
{
lstFileLoc = malloc(sizeof(type_fileloc));
lstFileLoc->addr = fl.addr;
lstFileLoc->file = strdup(fl.file);
lstFileLoc->lineno = fl.lineno;
lstFileLoc->next = NULL;
return;
}
while (iter != NULL)
{
// replace existing?
if (iter->addr == fl.addr)
{
iter->file = strdup(fl.file);
iter->lineno = fl.lineno;
return;
}
// insert entry?
if (iter->addr > fl.addr)
{
type_fileloc* flcpy = malloc(sizeof(type_fileloc));
flcpy->addr = iter->addr;
flcpy->file = iter->file;
flcpy->lineno = iter->lineno;
flcpy->next = iter->next;
iter->addr = fl.addr;
iter->file = strdup(fl.file);
iter->lineno = fl.lineno;
iter->next = flcpy;
return;
}
// add to end?
if (iter->next == NULL)
{
type_fileloc* flnew = malloc(sizeof(type_fileloc));
flnew->addr = fl.addr;
flnew->file = strdup(fl.file);
flnew->lineno = fl.lineno;
flnew->next = NULL;
iter->next = flnew;
return;
}
iter = iter->next;
}
}
void add_to_symmap(type_symmap_entry sme)
{
type_symmap_entry* iter = lstSymMap;
// first entry in list?
if (lstSymMap == NULL)
{
lstSymMap = malloc(sizeof(type_symmap_entry));
lstSymMap->addr = sme.addr;
lstSymMap->sval = strdup(sme.sval);
lstSymMap->symbol = strdup(sme.symbol);
lstSymMap->next = NULL;
return;
}
while (iter != NULL)
{
// insert entry?
if (iter->addr >= sme.addr)
{
type_symmap_entry* smecpy = malloc(sizeof(type_symmap_entry));
smecpy->addr = iter->addr;
smecpy->sval = iter->sval;
smecpy->symbol = iter->symbol;
smecpy->next = iter->next;
iter->addr = sme.addr;
iter->sval = strdup(sme.sval);
iter->symbol = strdup(sme.symbol);
iter->next = smecpy;
return;
}
// add to end?
if (iter->next == NULL)
{
type_symmap_entry* smenew = malloc(sizeof(type_symmap_entry));
smenew->addr = sme.addr;
smenew->sval = strdup(sme.sval);
smenew->symbol = strdup(sme.symbol);
smenew->next = NULL;
iter->next = smenew;
return;
}
iter = iter->next;
}
}
void copy_watch(type_watch_entry* dest, type_watch_entry* src)
{
dest->type = src->type;
dest->name = strdup(src->name);
dest->param1 = src->param1 ? strdup(src->param1) : NULL;
dest->next = NULL;
}
void add_to_watchlist(type_watch_entry we)
{
type_watch_entry* iter = lstWatches;
// first entry in list?
if (lstWatches == NULL)
{
lstWatches = malloc(sizeof(type_watch_entry));
copy_watch(lstWatches, &we);
return;
}
while (iter != NULL)
{
// add to end?
if (iter->next == NULL)
{
type_watch_entry* wenew = malloc(sizeof(type_watch_entry));
copy_watch(wenew, &we);
iter->next = wenew;
return;
}
iter = iter->next;
}
}
type_fileloc* find_in_list(int addr)
{
type_fileloc* iter = lstFileLoc;
while (iter != NULL)
{
if (iter->addr == addr)
return iter;
iter = iter->next;
}
return NULL;
}
type_fileloc* find_lineno_in_list(int lineno)
{
type_fileloc* iter = lstFileLoc;
if (!cur_file_loc)
return NULL;
while (iter != NULL)
{
if (strcmp(cur_file_loc->file, iter->file) == 0 && iter->lineno == lineno)
return iter;
iter = iter->next;
}
return NULL;
}
type_symmap_entry* find_in_symmap(char* sym)
{
type_symmap_entry* iter = lstSymMap;
while (iter != NULL)
{
if (strcmp(sym, iter->symbol) == 0)
return iter;
iter = iter->next;
}
return NULL;
}
type_watch_entry* find_in_watchlist(type_watch type, char* name)
{
type_watch_entry* iter = lstWatches;
while (iter != NULL)
{
if (strcmp(iter->name, name) == 0 && type == iter->type)
return iter;
iter = iter->next;
}
return NULL;
}
void free_watch(type_watch_entry* iter, int wnum)
{
free(iter->name);
if (iter->param1)
free(iter->param1);
free(iter);
if (outputFlag)
printf("watch#%d deleted!\n", wnum);
}
bool delete_from_watchlist(int wnum)
{
int cnt = 0;
type_watch_entry* iter = lstWatches;
type_watch_entry* prev = NULL;
while (iter != NULL)
{
cnt++;
// we found the item to delete?
if (cnt == wnum)
{
// first entry of list?
if (prev == NULL)
{
lstWatches = iter->next;
free_watch(iter, wnum);
return true;
}
else
{
prev->next = iter->next;
free_watch(iter, wnum);
return true;
}
}
prev = iter;
iter = iter->next;
}
return false;
}
char* get_string_token(char* p, char* name);
char* get_nth_token(char* p, int n)
{
static char token[128];
for (int k = 0; k <= n; k++)
{
p = get_string_token(p, token);
if (p == 0)
return NULL;
}
return token;
}
char* get_string_token(char* p, char* name)
{
int found_start = 0;
int idx = 0;
while (1)
{
if (!found_start)
{
if (*p == 0 || *p == '\r' || *p == '\n')
return 0;
if (*p != ' ' && *p != '\t')
{
found_start = 1;
continue;
}
p++;
}
if (found_start) // found start of token, now look for end;
{
if (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
{
name[idx] = 0;
return ++p;
}
name[idx] = *p;
p++;
idx++;
}
}
}
bool starts_with(const char *str, const char *pre)
{
return strncmp(pre, str, strlen(pre)) == 0;
}
void parse_ca65_segments(FILE* f, char* line)
{
char name[128];
char sval[128];
int val;
char *p;
memset(&segmentOffsets, 0, sizeof(segmentOffsets));
while (!feof(f))
{
fgets(line, 1024, f);
if (line[0] == '\0' || line[0] == '\r' || line[0] == '\n')
{
return;
}
p = line;
if (!(p = get_string_token(p, name)))
return;
if (!(p = get_string_token(p, sval)))
return;
val = strtol(sval, NULL, 16);
strcpy(segmentOffsets.segments[segmentOffsets.seg_cnt].name, name);
segmentOffsets.segments[segmentOffsets.seg_cnt].offset = val;
segmentOffsets.seg_cnt++;
}
}
void parse_ca65_modules(FILE* f, char* line)
{
char name[128];
char sval[128];
int val;
int state = 0;
char *p;
type_offsets mo = { 0 };
while (!feof(f))
{
fgets(line, 1024, f);
if (line[0] == '\0' || line[0] == '\r' || line[0] == '\n')
{
if (state == 1)
add_to_offsets_list(mo);
return;
}
if (strchr(line, ':') != NULL)
{
line[(int)(strchr(line, ':') - line)] = '\0';
if (state == 1)
{
add_to_offsets_list(mo);
memset(&mo, 0, sizeof(mo));
}
state = 0;
}
switch(state)
{
case 0: // get module name
strcpy(mo.modulename, line);
state = 1;
break;
case 1: // get segment offsets
p = line;
if (!(p = get_string_token(p, name)))
return;
if (!(p = get_string_token(p, sval)))
return;
if (!starts_with(sval, "Offs="))
return;
p = sval + 5;
val = strtol(p, NULL, 16);
strcpy(mo.segments[mo.seg_cnt].name, name);
mo.segments[mo.seg_cnt].offset = val;
mo.seg_cnt++;
}
}
}
void parse_ca65_symbols(FILE* f, char* line)
{
char name[128];
char sval[128];
int val;
char str[64];
while (!feof(f))
{
fgets(line, 1024, f);
//if (starts_with(line, "zerobss"))
// printf(line);
char* p = line;
for (int k = 0; k < 2; k++)
{
if (!(p = get_string_token(p, name)))
return;
p = get_string_token(p,sval);
val = strtol(sval, NULL, 16);
p = get_string_token(p,str); // ignore this 3rd one...
type_symmap_entry sme;
sme.addr = val;
sme.sval = sval;
sme.symbol = name;
add_to_symmap(sme);
}
}
}
void load_ca65_map(FILE* f)
{
char line[1024];
rewind(f);
while (!feof(f))
{
fgets(line, 1024, f);
if (starts_with(line, "Modules list:"))
{
fgets(line, 1024, f); // ignore following "----" line
parse_ca65_modules(f, line);
continue;
}
if (starts_with(line, "Segment list:"))
{
fgets(line, 1024, f); // ignore following "----" line
fgets(line, 1024, f); // ignore following "Name" line
fgets(line, 1024, f); // ignore following "----" line
parse_ca65_segments(f, line);
}
if (starts_with(line, "Exports list by name:"))
{
fgets(line, 1024, f); // ignore following "----" line
parse_ca65_symbols(f, line);
continue;
}
}
}
// loads the *.map file corresponding to the provided *.list file (if one exists)
void load_map(const char* fname)
{
char strMapFile[200];
strcpy(strMapFile, fname);
char* sdot = strrchr(strMapFile, '.');
*sdot = '\0';
strcat(strMapFile, ".map");
// check if file exists
if (access(strMapFile, F_OK) != -1)
{
printf("Loading \"%s\"...\n", strMapFile);
// load the map file
FILE* f = fopen(strMapFile, "rt");
int first_line = 1;
while (!feof(f))
{
char line[1024];
char sval[256];
fgets(line, 1024, f);
if (first_line)
{
first_line = 0;
if (starts_with(line, "Modules list:"))
{
load_ca65_map(f);
break;
}
}
int addr;
char sym[1024];
sscanf(line, "$%04X %s", &addr, sym);
sscanf(line, "%s", sval);
//printf("%s : %04X\n", sym, addr);
type_symmap_entry sme;
sme.addr = addr;
sme.sval = sval;
sme.symbol = sym;
add_to_symmap(sme);
}
fclose(f);
}
}
int get_segment_offset(const char* current_segment)
{
for (int k = 0; k < segmentOffsets.seg_cnt; k++)
{
if (strcmp(current_segment, segmentOffsets.segments[k].name) == 0)
{
return segmentOffsets.segments[k].offset;
}
}
return 0;
}
int get_module_offset(const char* current_module, const char* current_segment)
{
type_offsets* iter = lstModuleOffsets;
while (iter != NULL)
{
if (strcmp(current_module, iter->modulename) == 0)
{
for (int k = 0; k < iter->seg_cnt; k++)
{
if (strcmp(current_segment, iter->segments[k].name) == 0)
{
return iter->segments[k].offset;
}
}
}
iter = iter->next;
}
return 0;
}
void load_ca65_list(const char* fname, FILE* f)
{
static char list_file_name[256];
strcpy(list_file_name, fname); // preserve a copy of this eternally
load_map(fname); // load the ca65 map file first, as it contains details that will help us parse the list file
char line[1024];
char current_module[256] = { 0 };
char current_segment[64] = { 0 };
int lineno = 1;
while (!feof(f))
{
lineno++;
fgets(line, 1024, f);
if (starts_with(line, "Current file:"))
{
// Retrieve the current file/module that was assembled
strcpy(current_module, strchr(line, ':') + 2);
current_module[strlen(current_module)-1] = '\0';
current_module[strlen(current_module)-1] = 'o';
current_segment[0] = '\0';
}
if (line[0] == '\0' || line[0] == '\r' || line[0] == '\n')
continue;
// new .segment specified in code?
char *p = get_nth_token(line, 2);
if (p != NULL && strcmp(p, ".segment") == 0)
{
char* p = get_nth_token(line, 3);
strncpy(current_segment, p+1, strlen(p+1)-1);
current_segment[strlen(p+1)] = '\0';
}
// did we find a line with a relocatable address at the start of it
if (line[0] != ' ' && line[1] != ' ' && line[2] != ' ' && line[3] != ' ' && line[4] != ' ' && line[5] != ' '
&& line[6] == 'r' && line[7] == ' ' && line[8] != ' ')
{
char saddr[8];
int addr;
strncpy(saddr, line, 6);
saddr[7] = '\0';
addr = strtol(saddr, NULL, 16);
// convert relocatable address into absolute address
addr += get_segment_offset(current_segment);
addr += get_module_offset(current_module, current_segment);
//printf("mod=%s:seg=%s : %08X : %s", current_module, current_segment, addr, line);
type_fileloc fl;
fl.addr = addr;
fl.file = list_file_name;
fl.lineno = lineno;
add_to_list(fl);
}
}
}
// loads the given *.list file
void load_list(char* fname)
{
FILE* f = fopen(fname, "rt");
char line[1024];
int first_line = 1;
while (!feof(f))
{
fgets(line, 1024, f);
if (first_line)
{
first_line = 0;
if (starts_with(line, "ca65"))
{
load_ca65_list(fname, f);
fclose(f);
return;
}
}
if (strlen(line) == 0)
continue;
char *s = strrchr(line, '|');
if (s != NULL && *s != '\0')
{
s++;
if (strlen(s) < 5)
continue;
int addr;
char file[1024];
int lineno;
strcpy(file, &strtok(s, ":")[1]);
sscanf(strtok(NULL, ":"), "%d", &lineno);
sscanf(line, " %X", &addr);
//printf("%04X : %s:%d\n", addr, file, lineno);
type_fileloc fl;
fl.addr = addr;
fl.file = file;
fl.lineno = lineno;
add_to_list(fl);
}
}
fclose(f);
load_map(fname);
}
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"
#define KINV "\x1B[7m"
#define KCLEAR "\x1B[2J"
#define KPOS0_0 "\x1B[1;1H"
void show_location(type_fileloc* fl)
{
FILE* f = fopen(fl->file, "rt");
if (f == NULL)
return;
char line[1024];
int cnt = 1;
while (!feof(f))
{
fgets(line, 1024, f);
if (cnt >= (fl->lineno - 10) && cnt <= (fl->lineno + 10) )
{
if (cnt == fl->lineno)
{
printf("%s> %d: %s%s", KINV, cnt, line, KNRM);
}
else
printf("> %d: %s", cnt, line);
//break;
}
cnt++;
}
fclose(f);
}
// search the current directory for *.list files
void listSearch(void)
{
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
char* ext = get_extension(dir->d_name);
if (ext != NULL && strcmp(ext, ".list") == 0)
{
printf("Loading \"%s\"...\n", dir->d_name);
load_list(dir->d_name);
}
}
closedir(d);
}
}
reg_data get_regs(void)
{
reg_data reg = { 0 };
char* line;
serialWrite("r\n");
serialRead(inbuf, BUFSIZE);
line = strstr(inbuf+2, "\n") + 1;
sscanf(line,"%04X %02X %02X %02X %02X %02X %04X %04X %04X",
®.pc, ®.a, ®.x, ®.y, ®.z, ®.b, ®.sp, ®.mapl, ®.maph);
return reg;
}
mem_data get_mem(int addr, bool useAddr28)
{
mem_data mem = { 0 };
char str[100];
if (useAddr28)
sprintf(str, "m%07X\n", addr); // use 'm' (for 28-bit memory addresses)
else
sprintf(str, "d%04X\n", addr); // use 'd' instead of 'm' (for memory in cpu context)
serialWrite(str);
serialRead(inbuf, BUFSIZE);
sscanf(inbuf, " :%X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
&mem.addr, &mem.b[0], &mem.b[1], &mem.b[2], &mem.b[3], &mem.b[4], &mem.b[5], &mem.b[6], &mem.b[7], &mem.b[8], &mem.b[9], &mem.b[10], &mem.b[11], &mem.b[12], &mem.b[13], &mem.b[14], &mem.b[15]);
return mem;
}
// read all 32 lines at once (to hopefully speed things up for saving memory dumps)
mem_data* get_mem28array(int addr)
{
static mem_data multimem[32];
mem_data* mem;
char str[100];
sprintf(str, "M%04X\n", addr);
serialWrite(str);
serialRead(inbuf, BUFSIZE);
char* strLine = strtok(inbuf, "\n");
for (int k = 0; k < 32; k++)
{
mem = &multimem[k];
sscanf(strLine, " :%X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
&mem->addr, &mem->b[0], &mem->b[1], &mem->b[2], &mem->b[3], &mem->b[4], &mem->b[5], &mem->b[6], &mem->b[7], &mem->b[8], &mem->b[9], &mem->b[10], &mem->b[11], &mem->b[12], &mem->b[13], &mem->b[14], &mem->b[15]);
strLine = strtok(NULL, "\n");
}
return multimem;
}
// write buffer to client ram
void put_mem28array(int addr, unsigned char* data, int size)
{
char str[10];
sprintf(outbuf, "s%08X", addr);
int i = 0;
while(i < size)
{
sprintf(str, " %02X", data[i]);
strcat(outbuf, str);
i++;
}
strcat(outbuf, "\n");
serialWrite(outbuf);
serialRead(inbuf, BUFSIZE);
}
void cmdRawHelp(void)
{
serialWrite("?\n");
serialRead(inbuf, BUFSIZE);
printf("%s", inbuf);
printf("! - reset machine\n"
"f<low> <high> <byte> - Fill memory\n"
"g<addr> - Set PC\n"
"m<addr28> - Dump 16-bytes of memory (28bit addresses)\n"
"M<addr28> - Dump 512-bytes of memory (28bit addresses)\n"
"d<addr> - Dump 16-bytes of memory (CPU context)\n"
"D<addr> - Dump 512-bytes of memory (CPU context)\n"
"r - display CPU registers and last instruction executed\n"
"s<addr28> <value> ... - Set memory (28bit addresses)\n"
"S<addr> <value> ... - Set memory (CPU memory context)\n"
"b[<addr>] - Set or clear CPU breakpoint\n"
"t<0|1> - Enable/disable tracing\n"
"tc - Traced execution until keypress\n"
"t|BLANK LINE - Step one cpu cycle if in trace mode\n"
"w<addr> - Sets a watchpoint to trigger when specified address is modified\n"
"w - clear 'w' watchpoint\n"
"e - set a breakpoint to occur based on CPU flags\n"
);
}
void cmdHelp(void)
{
printf("m65dbg commands\n"
"===============\n");
for (int k = 0; command_details[k].name != NULL; k++)
{
type_command_details cd = command_details[k];
if (cd.params == NULL)
printf("%s = %s\n", cd.name, cd.help);
else
printf("%s %s = %s\n", cd.name, cd.params, cd.help);
}
printf(
"[ENTER] = repeat last command\n"
"q/x/exit = exit the program\n"
);
}
void dump(int addr, int total)
{
int cnt = 0;
while (cnt < total)
{
// get memory at current pc
mem_data mem = get_mem(addr + cnt, false);
printf(" :%07X ", mem.addr);
for (int k = 0; k < 16; k++)
{
if (k == 8) // add extra space prior to 8th byte
printf(" ");
printf("%02X ", mem.b[k]);
}
printf(" | ");
for (int k = 0; k < 16; k++)
{
int c = mem.b[k];
if (isprint(c))
printf("%c", c);
else