forked from MEGA65/m65dbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.c
1426 lines (1183 loc) · 27.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
#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;
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" };
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[] =
{
{ "help", cmdHelp, NULL, "Shows help information on m65dbg commands" },
{ "dump", cmdDump, "<addr> [<count>]", "Dumps memory (CPU context) at given address (with character representation in right-column" },
{ "mdump", cmdMDump, "<addr> [<count>]", "Dumps memory (28-bit addresses) at given address (with character representation in right-column" },
{ "dis", cmdDisassemble, "[<addr> [<count>]]", "Disassembles the instruction at <addr> or at PC. If <count> exists, it will dissembly that many instructions onwards" },
{ "step", cmdStep, NULL, "Step into next instruction" }, // equate to pressing 'enter' in raw monitor
{ "n", cmdNext, NULL, "Step over to next instruction" },
{ "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" },
{ "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" },
{ 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_symmap_entry* lstSymMap = NULL;
type_watch_entry* lstWatches = NULL;
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 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));
lstWatches->type = we.type;
lstWatches->name = strdup(we.name);
lstWatches->next = NULL;
return;
}
while (iter != NULL)
{
// add to end?
if (iter->next == NULL)
{
type_watch_entry* wenew = malloc(sizeof(type_watch_entry));
wenew->type = we.type;
wenew->name = strdup(we.name);
wenew->next = NULL;
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_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(char* name)
{
type_watch_entry* iter = lstWatches;
while (iter != NULL)
{
if (strcmp(iter->name, name) == 0)
return iter;
iter = iter->next;
}
return NULL;
}
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(iter->name);
free(iter);
if (outputFlag)
printf("watch#%d deleted!\n", wnum);
return true;
}
else
{
prev->next = iter->next;
free(iter->name);
free(iter);
if (outputFlag)
printf("watch#%d deleted!\n", wnum);
return true;
}
}
prev = iter;
iter = iter->next;
}
return false;
}
// loads the *.map file corresponding to the provided *.list file (if one exists)
void load_map(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");
while (!feof(f))
{
char line[1024];
char sval[256];
fgets(line, 1024, f);
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);
}
}
}
// loads the given *.list file
void load_list(char* fname)
{
FILE* f = fopen(fname, "rt");
char line[1024];
while (!feof(f))
{
fgets(line, 1024, f);
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);
}
}
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)
{
mem_data mem = { 0 };
char str[100];
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;
}
mem_data get_mem28(int addr)
{
mem_data mem = { 0 };
char str[100];
sprintf(str, "m%04X\n", addr);
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 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 cmdDump(void)
{
char* strAddr = strtok(NULL, " ");
if (strAddr == NULL)
{
printf("Missing <addr> parameter!\n");
return;
}
int addr = get_sym_value(strAddr);
int total = 16;
char* strTotal = strtok(NULL, " ");
if (strTotal != NULL)
{
sscanf(strTotal, "%X", &total);
}
int cnt = 0;
while (cnt < total)
{
// get memory at current pc
mem_data mem = get_mem(addr + cnt);
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
printf(".");
}
printf("\n");
cnt+=16;
if (ctrlcflag)
break;
}
}
void cmdMDump(void)
{
char* strAddr = strtok(NULL, " ");
if (strAddr == NULL)
{
printf("Missing <addr> parameter!\n");
return;
}
int addr = get_sym_value(strAddr);
int total = 16;
char* strTotal = strtok(NULL, " ");
if (strTotal != NULL)
{
sscanf(strTotal, "%X", &total);
}
int cnt = 0;
while (cnt < total)
{
// get memory at current pc
mem_data mem = get_mem28(addr + cnt);
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
printf(".");
}
printf("\n");
cnt+=16;
if (ctrlcflag)
break;
}
}
// return the last byte count
int disassemble_addr_into_string(char* str, int addr)
{
int last_bytecount = 0;
char s[32] = { 0 };
// get memory at current pc
mem_data mem = get_mem(addr);
// now, try to disassemble it
// Program counter
sprintf(str, "$%04X ", addr & 0xffff);
type_opcode_mode mode = opcode_mode[mode_lut[mem.b[0]]];
sprintf(s, " %10s:%d ", mode.name, mode.val);
strcat(str, s);
// Opcode and arguments
sprintf(s, "%02X ", mem.b[0]);
strcat(str, s);
last_bytecount = mode.val + 1;
if (last_bytecount == 1)
{
strcat(str, " ");
}
if (last_bytecount == 2)
{
sprintf(s, "%02X ", mem.b[1]);
strcat(str, s);
}
if (last_bytecount == 3)
{
sprintf(s, "%02X %02X ", mem.b[1], mem.b[2]);
strcat(str, s);
}
// Instruction name
strcat(str, instruction_lut[mem.b[0]]);
switch(mode_lut[mem.b[0]])
{
case M_impl: break;
case M_InnX:
sprintf(s, " ($%02X,X)", mem.b[1]);
strcat(str, s);
break;
case M_nn:
sprintf(s, " $%02X", mem.b[1]);
strcat(str, s);
break;
case M_immnn:
sprintf(s, " #$%02X", mem.b[1]);
strcat(str, s);
break;
case M_A: break;
case M_nnnn:
sprintf(s, " $%02X%02X", mem.b[2], mem.b[1]);
strcat(str, s);
break;
case M_nnrr:
sprintf(s, " $%02X,$%04X", mem.b[1], (addr + 3 + mem.b[2]) );
strcat(str, s);
break;
case M_rr:
if (mem.b[1] & 0x80)
sprintf(s, " $%04X", (addr + 2 - 256 + mem.b[1]) );
else
sprintf(s, " $%04X", (addr + 2 + mem.b[1]) );
strcat(str, s);
break;
case M_InnY:
sprintf(s, " ($%02X),Y", mem.b[1]);
strcat(str, s);
break;
case M_InnZ:
sprintf(s, " ($%02X),Z", mem.b[1]);
strcat(str, s);
break;
case M_rrrr:
sprintf(s, " $%04X", (addr + 2 + (mem.b[2] << 8) + mem.b[1]) & 0xffff );
strcat(str, s);
break;
case M_nnX:
sprintf(s, " $%02X,X", mem.b[1]);
strcat(str, s);
break;
case M_nnnnY:
sprintf(s, " $%02X%02X,Y", mem.b[2], mem.b[1]);
strcat(str, s);
break;
case M_nnnnX:
sprintf(s, " $%02X%02X,X", mem.b[2], mem.b[1]);
strcat(str, s);
break;
case M_Innnn:
sprintf(s, " ($%02X%02X)", mem.b[2], mem.b[1]);
strcat(str, s);
break;
case M_InnnnX:
sprintf(s, " ($%02X%02X,X)", mem.b[2], mem.b[1]);
strcat(str, s);
break;
case M_InnSPY:
sprintf(s, " ($%02X,SP),Y", mem.b[1]);
strcat(str, s);
break;
case M_nnY:
sprintf(s, " $%02X,Y", mem.b[1]);
strcat(str, s);
break;
case M_immnnnn:
sprintf(s, " #$%02X%02X", mem.b[2], mem.b[1]);
strcat(str, s);
break;
}
return last_bytecount;
}
int* get_backtrace_addresses(void)
{
// get current register values
reg_data reg = get_regs();
static int addresses[8];
// get memory at current pc
mem_data mem = get_mem(reg.sp+1);
for (int k = 0; k < 8; k++)
{
int addr = mem.b[k*2] + (mem.b[k*2+1] << 8);
addr -= 2;
addresses[k] = addr;
}
return addresses;
}
void cmdDisassemble(void)
{
char str[128] = { 0 };
int last_bytecount = 0;
if (autowatch)
cmdWatches();
int addr;
int cnt = 1; // number of lines to disassemble
// get current register values
reg_data reg = get_regs();
// get address from parameter?
char* token = strtok(NULL, " ");
if (token != NULL)
{
if (strcmp(token, "-") == 0) // '-' equates to current pc
{
// get current register values
addr = reg.pc;
}
else
addr = get_sym_value(token);
token = strtok(NULL, " ");
if (token != NULL)
{
cnt = get_sym_value(token);
}
}
// default to current pc
else
{
addr = reg.pc;
}
// are we in a different frame?
if (addr == reg.pc && traceframe != 0)
{
int* addresses = get_backtrace_addresses();
addr = addresses[traceframe-1];
printf("<<< FRAME#: %d >>>\n", traceframe);
}
int idx = 0;
while (idx < cnt)
{
last_bytecount = disassemble_addr_into_string(str, addr);
// print from .list ref? (i.e., find source in .a65 file?)
if (idx == 0)
{
type_fileloc *found = find_in_list(addr);
if (found)
{
printf("> %s:%d\n", found->file, found->lineno);
show_location(found);
printf("---------------------------------------\n");
}
}
// just print the raw disassembly line
if (cnt != 1 && idx == 0)
printf("%s%s%s\n", KINV, str, KNRM);
else
printf("%s\n", str);
if (ctrlcflag)
break;
addr += last_bytecount;
idx++;
} // end while
}
void cmdStep(void)
{
traceframe = 0;
// just send an enter command
serialWrite("\n");
serialRead(inbuf, BUFSIZE);
if (outputFlag)
{
if (autocls)
cmdClearScreen();
printf("%s", inbuf);
cmdDisassemble();
}
}
void cmdNext(void)
{
traceframe = 0;
if (autocls)
cmdClearScreen();
// check if this is a JSR command
reg_data reg = get_regs();
mem_data mem = get_mem(reg.pc);
// if not, then just do a normal step
if (strcmp(instruction_lut[mem.b[0]], "JSR") != 0)
{
cmdStep();
}
else
{
// if it is JSR, then keep doing step into until it returns to the next command after the JSR
type_opcode_mode mode = opcode_mode[mode_lut[mem.b[0]]];
int last_bytecount = mode.val + 1;
int next_addr = reg.pc + last_bytecount;
while (reg.pc != next_addr)
{
// just send an enter command
serialWrite("\n");
serialRead(inbuf, BUFSIZE);
reg = get_regs();
if (ctrlcflag)
break;
}
// show disassembly of current position
serialWrite("r\n");
serialRead(inbuf, BUFSIZE);
if (outputFlag)
{
if (autocls)
cmdClearScreen();
printf("%s", inbuf);
cmdDisassemble();
}
}
}
void cmdFinish(void)
{
traceframe = 0;
reg_data reg = get_regs();
int cur_sp = reg.sp;
bool function_returning = false;
//outputFlag = false;
while (!function_returning)
{
reg = get_regs();
mem_data mem = get_mem(reg.pc);
if (strcmp(instruction_lut[mem.b[0]], "RTS") == 0
&& reg.sp == cur_sp)
function_returning = true;
cmdClearScreen();
cmdNext();
if (ctrlcflag)
break;
}
//outputFlag = true;
cmdDisassemble();
}
// check symbol-map for value. If not found there, just return
// the hex-value of the string
int get_sym_value(char* token)
{
int addr = 0;
type_symmap_entry* sme = find_in_symmap(token);
if (sme != NULL)
{
return sme->addr;
}
else
{
sscanf(token, "%X", &addr);
return addr;
}
}
void print_byte(char *token)