forked from gurcei/m65dbg
-
Notifications
You must be signed in to change notification settings - Fork 16
/
commands.c
5529 lines (4655 loc) · 130 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
/* vim: set expandtab shiftwidth=2 tabstop=2: */
#define _BSD_SOURCE _BSD_SOURCE
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <math.h>
#include "commands.h"
#include "serial.h"
#include "gs4510.h"
#include "screen_shot.h"
#include "m65.h"
int get_sym_value(char* token);
void print_char(int c);
typedef struct
{
int pc;
int a;
int x;
int y;
int z;
int b;
int sp;
int mapl;
int maph;
int lastop;
int odd1; // what is this?
int odd2; // what is this?
char flags[16];
} reg_data;
typedef struct
{
int addr;
unsigned int b[16];
} mem_data;
typedef struct
{
int addr;
char name[256];
} rom_chunk;
rom_chunk rom_chunks[] = {
{ 0x20000, "(C65 DOS chunk 1)" },
{ 0x22000, "(C65 DOS chunk 2)" },
{ 0x24000, "(C65 BASIC ALT chunk 1)" },
{ 0x26000, "(C65 BASIC ALT chunk 2)" },
{ 0x28000, "(CHARSET-A)" },
{ 0x2a000, "(C64 BASIC)" },
{ 0x2c000, "(C65 DOS chunk 3 + C65 KERNAL chunk 1 + CHARSET-C)" },
{ 0x2e000, "(C64 KERNAL)" },
{ 0x30000, "(C65 MONITOR)" },
{ 0x32000, "(C65 BASIC chunk 1)" },
{ 0x34000, "(C65 BASIC chunk 2)" },
{ 0x36000, "(C65 BASIC chunk 3)" },
{ 0x38000, "(C65 BASIC chunk 4)" },
{ 0x3a000, "(C65 BASIC chunk 5)" },
{ 0x3c000, "(C65 KERNAL chunk 2 + CHARSET-B)" },
{ 0x3e000, "(C65 EDITOR + C65 KERNAL chunk 3)" },
{ 0, "" }
};
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 ", "QWORD ", "STRING ", "DUMP ",
"MBYTE ", "MWORD ", "MDWORD ", "MQWORD ", "MSTRING", "MDUMP ", "MFLOAT " };
bool autocls = false; // auto-clearscreen flag
bool autowatch = false; // auto-watch flag
bool autolocals = false; // auto-locals flag
bool romw = false; // rom writable flag
bool petscii = false; // show chars in dumps based on petscii
bool fastmode = false; // switch for slow (2,000,000bps) and fast (4,000,000bps) modes
bool ctrlcflag = false; // a flag to keep track of whether ctrl-c was caught
int traceframe = 0; // tracks which frame within the backtrace
int dis_offs = 0;
int dis_scope = 10;
int softbrkaddr = 0;
unsigned char softbrkmem[3] = { 0 };
type_command_details command_details[] =
{
{ "?", cmdRawHelp, NULL, "Shows help information for raw/native monitor commands" },
{ "help", cmdHelp, "[<cmdname>]", "Shows help information for m65dbg commands. If optional <cmdname> given, show help for that command only." },
{ "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)" },
{ "a", cmdAssemble, "<addr28>", "Assembles instructions at the given <addr28> location." },
{ "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)"},
{ "sc", cmdSoftContinue, "[<addr>]", "soft 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" },
{ "pq", cmdPrintQWord, "<addr>", "Prints the qword-value of the given address" },
{ "ps", cmdPrintString, "<addr>", "Prints the null-terminated string-value found at the given address" },
{ "pbas", cmdPrintBasicVar, "[<varname>]", "Print the value of specified basic var. If none given, print value of all vars." },
{ "pmb", cmdPrintMByte, "<addr28>", "Prints the byte-value of the given 28-bit address" },
{ "pmw", cmdPrintMWord, "<addr28>", "Prints the word-value of the given 28-bit address" },
{ "pmd", cmdPrintMDWord, "<addr28>", "Prints the dword-value of the given 28-bit address" },
{ "pmq", cmdPrintMQWord, "<addr28>", "Prints the qword-value of the given 28-bit address" },
{ "pms", cmdPrintMString, "<addr28>", "Prints the null-terminated string-value found at the given 28-bit address" },
{ "pmf", cmdPrintMFloat, "<addr28>", "Prints the BASIC float value at the given 28-bit address" },
{ "cls", cmdClearScreen, NULL, "Clears the screen" },
{ "autocls", cmdAutoClearScreen, "0/1", "If set to 1, clears the screen prior to every step/next command" },
{ "romw", cmdRomW, "0/1", "If set to 1, rom is writable. If set to 0, rom is read-only. If no parameter, it toggles" },
{ "break", cmdSetBreakpoint, "<addr>", "Sets the hardware breakpoint to the desired address" },
{ "sbreak", cmdSetSoftwareBreakpoint, "<addr>", "Sets the software 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" },
{ "wq", cmdWatchQWord, "<addr>", "Watches the qword-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" },
{ "wmb", cmdWatchMByte, "<addr28>", "Watches the byte-value of the given 28-bit address" },
{ "wmw", cmdWatchMWord, "<addr28>", "Watches the word-value of the given 28-bit address" },
{ "wmd", cmdWatchMDWord, "<addr28>", "Watches the dword-value of the given 28-bit address" },
{ "wmq", cmdWatchMQWord, "<addr28>", "Watches the qword-value of the given 28-bit address" },
{ "wms", cmdWatchMString, "<addr28>", "Watches the null-terminated string-value found at the given 28-bit address" },
{ "wmf", cmdWatchMFloat, "<addr28>", "Watches a BASIC float value at the given 28-bit 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|$hex>", "retrieves the value of the symbol from the .map file. Alternatively, can find symbol name/s matching given $hex address. If two $hex values are given, it finds all symbols within this range" },
{ "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>" },
{ "poke", cmdPoke, "<addr16> <byte/s>", "pokes byte value/s into <addr16> (and beyond, if multiple values)" },
{ "pokew", cmdPokeW, "<addr16> <word/s>", "pokes word value/s into <addr16> (and beyond, if multiple values)" },
{ "poked", cmdPokeD, "<addr16> <dword/s>", "pokes dword value/s into <addr16> (and beyond, if multiple values)" },
{ "pokeq", cmdPokeQ, "<addr16> <qword/s>", "pokes qword value/s into <addr16> (and beyond, if multiple values)" },
{ "mpoke", cmdMPoke, "<addr28> <byte/s>", "pokes byte value/s into <addr28> (and beyond, if multiple values)" },
{ "mpokew", cmdMPokeW, "<addr28> <word/s>", "pokes word value/s into <addr28> (and beyond, if multiple values)" },
{ "mpoked", cmdMPokeD, "<addr28> <dword/s>", "pokes dword value/s into <addr28> (and beyond, if multiple values)" },
{ "mpokeq", cmdMPokeQ, "<addr28> <qword/s>", "pokes qword value/s into <addr28> (and beyond, if multiple values)" },
{ "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\""},
{ "ss", cmdScreenshot, NULL, "Takes an ascii screenshot of the mega65's screen" },
{ "ty", cmdType, "[<string>]", "Remote keyboard mode (if optional string provided, acts as one-shot message with carriage-return)" },
{ "ftp", cmdFtp, NULL, "FTP access to SD-card" },
{ "petscii", cmdPetscii, "0/1", "In dump commands, respect petscii screen codes" },
{ "fastmode", cmdFastMode, "0/1", "Used to quickly switch between 2,000,000bps (slow-mode: default) or 4,000,000bps (fast-mode: used in ftp-mode)" },
{ "scope", cmdScope, "<int>", "the scope-size of the listing to show alongside the disassembly" },
{ "offs", cmdOffs, "<int>", "the offset of the listing to show alongside the disassembly" },
{ "val", cmdPrintValue, "<$hex/dec/\%bin/>", "print the given value in hex, decimal and binary" },
{ "=", cmdForwardDis, "[<count>]", "move forward in disassembly of pc history from 'z' command" },
{ "-", cmdBackwardDis, "[<count>]", "move backward in disassembly of pc history from 'z' command" },
{ "mcopy", cmdMCopy, "<src_addr> <dest_addr> <count>", "copy data from source location to destination (28-bit addresses)" },
{ "locals", cmdLocals, NULL, "Print out the values of any local variables within the current c-function (needs gurce's cc65 .list file)" },
{ "autolocals", cmdAutoLocals, "0/1", "If set to 1, shows all locals prior to every step/next/dis command" },
{ "mapping", cmdMapping, NULL, "Summarise the current $D030/MAP/$01 mapping of the system" },
{ "seam", cmdSeam, "[row][col]", "display attributes of selected SEAM character" },
{ "blist", cmdBasicList, NULL, "list the current basic program" },
{ "sprite", cmdSprite, "<spridx>", "print out the bits of the sprite at the given index\n"
" (based on currently selected vicii bank at $dd00)\n"
" If the index is in the form $xxxx, it is treated as an absolute memory address." },
{ "char", cmdChar, "<charidx> [<count>]", "print out the bits of the char at the given index\n"
" (based on currently selected vicii bank at $dd00)\n"
" If the index is in the form $xxxx, it is treated as an absolute memory address." },
{ "set", cmdSet, "<addr> <string|bytes>", "set bytes at the given address to the desired string or bytes" },
{ "reload", cmdReload, NULL, "reloads any list and map files (in-case you've rebuilt them recently)" },
{ "go", cmdGo, "<addr>", "sets the PC to the desired address." },
{ NULL, NULL, NULL, NULL }
};
// a few function prototypes
mem_data get_mem(int addr, bool useAddr28);
void print_byte_at_addr(char* token, int addr, bool useAddr28, bool show_decimal, bool show_char);
void print_word_at_address(char* token, int addr, bool useAddr28, bool show_decimal);
void print_dword_at_address(char* token, int addr, bool useAddr28, bool show_decimal);
void print_qword_at_address(char* token, int addr, bool useAddr28, bool show_decimal);
char* toBinaryString(int val);
char* get_extension(char* fname)
{
return strrchr(fname, '.');
}
int prior_offset = 0; // to help keep track of stack offsets of local variables within functions
typedef struct tli
{
char* name;
int offset;
int size;
struct tli *next;
} type_localinfo;
typedef struct tfi
{
char* name;
int addr;
type_localinfo* locals;
int paramsize;
struct tfi *next;
} type_funcinfo;
type_funcinfo* lstFuncInfo = NULL;
type_funcinfo* cur_func_info = NULL;
typedef struct tfl
{
int addr;
int lastaddr;
char* file;
char* module;
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 clearSoftBreak(void);
int isCpuStopped(void);
void setSoftBreakpoint(int addr);
void step(void);
void add_to_offsets_list(type_offsets mo)
{
type_offsets* iter = lstModuleOffsets;
mo.enabled = 1;
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_locals(type_funcinfo *fi, type_localinfo *li)
{
type_localinfo* previter = 0;
type_localinfo* iter = fi->locals;
if (fi->locals == NULL)
{
fi->locals = li;
return;
}
while (iter != NULL)
{
// insert entry?
if (strcmp(iter->name, fi->name) < 0) {
if (previter == 0) {
fi->locals = li;
fi->locals->next = iter;
return;
}
else {
previter->next = li;
li->next = iter;
return;
}
}
if (iter->next == NULL) {
iter->next = li;
return;
}
previter = iter;
iter = iter->next;
}
}
void add_to_func_list(type_funcinfo *fi)
{
type_funcinfo* previter = 0;
type_funcinfo* iter = lstFuncInfo;
cur_func_info = fi;
if (lstFuncInfo == NULL)
{
lstFuncInfo = fi;
return;
}
while (iter != NULL)
{
// insert entry?
if (iter->addr > fi->addr)
{
if (previter == 0) {
lstFuncInfo = fi;
lstFuncInfo->next = iter;
return;
}
else {
previter->next = fi;
fi->next = iter;
return;
}
}
if (iter->next == NULL) {
iter->next = fi;
return;
}
previter = iter;
iter = iter->next;
}
}
type_fileloc* 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->lastaddr = fl.lastaddr;
lstFileLoc->file = strdup(fl.file);
lstFileLoc->lineno = fl.lineno;
lstFileLoc->module = fl.module;
lstFileLoc->next = NULL;
return lstFileLoc;
}
while (iter != NULL)
{
// replace existing?
if (iter->addr == fl.addr)
{
iter->file = strdup(fl.file);
iter->lineno = fl.lineno;
return iter;
}
// insert entry?
if (iter->addr > fl.addr)
{
type_fileloc* flcpy = malloc(sizeof(type_fileloc));
flcpy->addr = iter->addr;
flcpy->lastaddr = iter->lastaddr;
flcpy->file = iter->file;
flcpy->lineno = iter->lineno;
flcpy->module = iter->module;
flcpy->next = iter->next;
iter->addr = fl.addr;
iter->lastaddr = fl.lastaddr;
iter->file = strdup(fl.file);
iter->lineno = fl.lineno;
iter->module = fl.module;
iter->next = flcpy;
return iter;
}
// add to end?
if (iter->next == NULL)
{
type_fileloc* flnew = malloc(sizeof(type_fileloc));
flnew->addr = fl.addr;
flnew->lastaddr = fl.lastaddr;
flnew->file = strdup(fl.file);
flnew->lineno = fl.lineno;
flnew->module = fl.module;
flnew->next = NULL;
iter->next = flnew;
return flnew;
}
iter = iter->next;
}
return NULL;
}
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->show_decimal = src->show_decimal;
dest->show_char = src->show_char;
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)
{
//printf("%s : addr=$%04X : line=%d\n", iter->file, iter->addr, iter->lineno);
if (iter->addr == addr)
return iter;
if (iter->lastaddr != 0 && (iter->addr < addr && addr <= iter->lastaddr))
return iter;
iter = iter->next;
}
return NULL;
}
int find_addr_in_list(char* file, int line)
{
type_fileloc* iter = lstFileLoc;
while (iter != NULL)
{
if (!strcmp(file, iter->file) && iter->lineno == line)
return iter->addr;
iter = iter->next;
}
return -1;
}
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++;
//printf("ca65 module: %s : %s - offs = $%04X\n", mo.modulename, name, val);
}
}
}
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;
}
}
}
void load_lbl(const char* fname)
{
// load the map file
FILE* f = fopen(fname, "rt");
while (!feof(f))
{
char line[1024];
fgets(line, 1024, f);
char saddr[128];
char sym[1024];
sscanf(line, "al %s %s", saddr, sym);
type_symmap_entry sme;
sme.addr = get_sym_value(saddr);
sme.sval = saddr;
sme.symbol = sym;
add_to_symmap(sme);
// printf("%s : %s\n", sme.sval, sym);
}
fclose(f);
}
// 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, "$%X | %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;
}
char* get_module_string(const char* current_module)
{
type_offsets* iter = lstModuleOffsets;
while (iter != NULL)
{
if (strcmp(current_module, iter->modulename) == 0)
{
return iter->modulename;
}
iter = iter->next;
}
return 0;
}
void parse_function(char* line, int addr)
{
char *p = get_nth_token(line, 2);
if (p != NULL && strcasecmp(p, ".proc") == 0)
{
char* p = get_nth_token(line, 3);
p[strlen(p)-1] = '\0';
type_funcinfo* fi = malloc(sizeof(type_funcinfo));
fi->name = strdup(p);
fi->addr = addr;
fi->locals = NULL;
fi->paramsize = 0;
fi->next = NULL;
add_to_func_list(fi);
prior_offset = 0;
}
}
void parse_debug(char* line)
{
if (cur_func_info == NULL)
return;
char *p1 = get_nth_token(line, 2);
if (p1 == NULL)
return;
if (strcasecmp(p1, ".dbg") == 0)
{
char *p2 = get_nth_token(line, 3);
if (p2 == NULL)
return;