This repository was archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugger.c
More file actions
1724 lines (1466 loc) · 47.9 KB
/
Debugger.c
File metadata and controls
1724 lines (1466 loc) · 47.9 KB
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
/*
* S51D - Simple MCS51 Debugger
*
* Copyright (C) 2011, Michał Dobaczewski / mdobak@(Google mail)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "Global.h"
#include "Debugger.h"
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include "DeAsm.h"
#include "IntelHex.h"
#include "Utils.h"
#include "Keyboard.h"
const size_t MAX_LINE = 128;
const char* g_unavailable = "Unavailable when thread is running.\n";
const char* g_tooFewArguments = "Too few arguments. Type help for more details.\n";
const char* g_stoped = "Stoped at %.4Xh after execute %i instructions.\n";
pthread_t g_MCUThread;
pthread_t g_keyEventLoop;
bool g_MCUThreadRunning;
bool g_keyEventLoopRunning;
#define REQUIRED_ARGS(n, command) \
if (argc < ((n) + 1)) { fprintf(AppSettings()->errorOut, g_tooFewArguments); command; }
#define STOP_IF_THREAD_RUN(command) \
if (g_MCUThreadRunning) { fprintf(AppSettings()->errorOut, g_unavailable); command; }
struct _params {
const char* name;
void (*function)(int argc, char** argv);
const char* paramsDesc;
const char* shortDesc;
};
const char* operatorToString(BreakpointType type)
{
const char* str = "";
if (type == EQUAL)
str = "e";
else if (type == NOT_EQUAL)
str = "ne";
else if (type == LESS)
str = "l";
else if (type == LESS_EQUAL)
str = "le";
else if (type == GREATER)
str = "g";
else if (type == GREATER_EQUAL)
str = "ge";
else if (type == AND)
str = "and";
else if (type == OR)
str = "or";
else if (type == XOR)
str = "xor";
return str;
}
BreakpointType stringToOperator(const char* str)
{
BreakpointType type = 0;
if (0 == stricmp(str, "e"))
type = EQUAL;
else if (0 == stricmp(str, "ne"))
type = NOT_EQUAL;
else if (0 == stricmp(str, "l"))
type = LESS;
else if (0 == stricmp(str, "le"))
type = LESS_EQUAL;
else if (0 == stricmp(str, "g"))
type = GREATER;
else if (0 == stricmp(str, "ge"))
type = GREATER_EQUAL;
else if (0 == stricmp(str, "and"))
type = AND;
else if (0 == stricmp(str, "or"))
type = OR;
else if (0 == stricmp(str, "xor"))
type = XOR;
return type;
}
const char* memoryToString(MemoryType type)
{
const char* str = "";
if (type == IDATA)
str = "idata";
else if (type == XDATA)
str = "xdata";
else if (type == SFR)
str = "sfr";
else if (type == IROM)
str = "irom";
else if (type == XROM)
str = "xrom";
return str;
}
MemoryType stringToMemory(const char* str)
{
MemoryType type = 0;
if (0 == stricmp(str, "idata") || 0 == stricmp(str, "i") ||
0 == stricmp(str, "iram") || 0 == stricmp(str, "intram"))
type = IDATA;
else if (0 == stricmp(str, "xdata") || 0 == stricmp(str, "x") ||
0 == stricmp(str, "xram") || 0 == stricmp(str, "extram"))
type = XDATA;
else if (0 == stricmp(str, "sfr") || 0 == stricmp(str, "s") ||
0 == stricmp(str, "registers") || 0 == stricmp(str, "r"))
type = SFR;
else if (0 == stricmp(str, "irom") || 0 == stricmp(str, "icode") ||
0 == stricmp(str, "introm"))
type = IROM;
else if (0 == stricmp(str, "xrom") || 0 == stricmp(str, "xcode") ||
0 == stricmp(str, "extrom"))
type = XROM;
return type;
}
void* defputchar(char chr)
{
if (AppSettings()->out == AppSettings()->defaultOut) {
#ifdef _WIN32
vtProcessedTextOut(&chr, 1);
#else
print("%c", chr);
fflush(AppSettings()->out);
#endif
} else {
print("%c", chr); // Czemu nie ma fputchar ???
}
return NULL;
}
void* hexputchar(char chr)
{
print("%.2X ", chr);
if (AppSettings()->out == AppSettings()->defaultOut)
fflush(AppSettings()->out);
return NULL;
}
void* keyEventLoop(void* _arg)
{
g_keyEventLoopRunning = true;
#ifdef __linux__
initKeyboard();
#endif
while (1) {
if (AppSettings()->stopThread) {
AppSettings()->stopThread = false;
break;
}
if (keyboardHit()) {
AppSettings()->lastKey = getKey();
AppSettings()->keyAvailable = true;
} else {
AppSettings()->keyAvailable = false;
}
/* Daje odrobine czasu na "trafienie" na moment kiedy wczytany program
* oczekuje wejścia. */
for (int i = 0; i < 4 && AppSettings()->keyAvailable && !keyboardHit(); ++i)
msSleep(50);
}
#ifdef __linux__
closeKeyboard();
#endif
g_keyEventLoopRunning = false;
return NULL;
}
void* runMCU(void* output(char))
{
g_MCUThreadRunning = true;
unsigned long long simStart = clock();
unsigned long long lastTitleRefresh = clock();
unsigned long long cyclesFromLastRefresh = AppSettings()->mcu->cycles;
const unsigned long long titleRefreshInterval = CLOCKS_PER_SEC;
while (true) {
bool useOut = false;
bool needIn = false;
BYTE* in = AppSettings()->keyAvailable ? (BYTE*)&AppSettings()->lastKey : NULL;
BYTE out = '?';
/* Przerwij pętle gdy z innego wątku ustanowiono falgę. */
if (AppSettings()->stopThread) {
AppSettings()->stopThread = false;
break;
}
/* Czasy działania symulatora i mikrokontrolera. */
AppSettings()->mcuSec = getMCUTime(AppSettings()->mcu);
AppSettings()->simSec = (double)(clock() - simStart) / CLOCKS_PER_SEC +
AppSettings()->simSyncTimeBeforeStop;
/* Spowalnia program, aby działał z realną prędkością określoną predkością
kwarcu. */
if (AppSettings()->realtime && AppSettings()->mcuSec - AppSettings()->simSec > 0.01) {
msSleep(10);
continue;
}
/* Przerwij gdy wciśnięto magiczny klawisz (exitkey). */
if (AppSettings()->keyAvailable && AppSettings()->lastKey == AppSettings()->exitkey) {
AppSettings()->keyAvailable = false;
break;
}
if (!processMCUEx(AppSettings()->mcu, &out, in, &useOut, &needIn))
break;
if (useOut && output != NULL)
output(out);
if (needIn)
AppSettings()->keyAvailable = false;
/* Przerwij gdy program chce wyłączyć procesor... całkiem logiczne, co? */
if (checkRegister(AppSettings()->mcu, PCON_PD))
break;
/* Przerwij przy nieskończonej pętli. */
if (!checkRegister(AppSettings()->mcu, IE_EA) &&
*ROM(AppSettings()->mcu, AppSettings()->mcu->PC) == 0x80 &&
*ROM(AppSettings()->mcu, AppSettings()->mcu->PC + 1) == 0xFE)
break;
/* Tytuł okna konsoli. */
if (clock() - lastTitleRefresh > titleRefreshInterval) {
char title[128];
unsigned long long cyclesPerSec = (AppSettings()->mcu->cycles -
cyclesFromLastRefresh);
snprintf(title, 128, "%s - cycles: %lu XTAL: %.4fMHz "
"MCU: %.1fs SIM: %.1fs PC: %.4X",
APP_NAME,
(unsigned long)AppSettings()->mcu->cycles, // Nie działa %llu. :(.
(double)(cyclesPerSec * 12) / 1000000,
AppSettings()->mcuSec,
AppSettings()->simSec,
AppSettings()->mcu->PC);
setConsoleTitle(title);
lastTitleRefresh = clock();
cyclesFromLastRefresh = AppSettings()->mcu->cycles;
}
}
if (AppSettings()->mcu->errid != E_NOERRORS && AppSettings()->pauseOnError)
fprintf(AppSettings()->errorOut, getError(AppSettings()->mcu));
/* Zapaiętaj czas działania, aby go uzyć go przy ponownym uruchomieniu */
AppSettings()->simSyncTimeBeforeStop = AppSettings()->simSec;
AppSettings()->simTimeBeforeStop = AppSettings()->simSec;
g_MCUThreadRunning = false;
return NULL;
}
bool step(bool memory)
{
bool ret;
bool outSth;
BYTE outByte;
char* asmCode = disassembler(AppSettings()->mcu,
AppSettings()->mcu->PC, AppSettings()->format, NULL);
ret = processMCUEx(AppSettings()->mcu, &outByte, NULL, &outSth, NULL);
if (memory) {
print("%s"
C_FWHITE
C_NBOLD "IDATA[" C_BOLD "%.2Xh" C_NBOLD "]=" C_BOLD "%.2X "
C_NBOLD "XDATA[" C_BOLD "%.4Xh" C_NBOLD "]=" C_BOLD "%.2X "
C_NBOLD "SFR[" C_BOLD "%-4s" C_NBOLD "]=" C_BOLD "%.2X "
C_NBOLD "ACC=" C_BOLD "%.2X "
C_NBOLD "R[" C_BOLD "%i" C_NBOLD "]=" C_BOLD "%.2X "
C_NBOLD "SP=" C_BOLD "%.2X\n"
C_NBOLD "CY=" C_BOLD "%i "
C_NBOLD "AC=" C_BOLD "%i "
C_NBOLD "F0=" C_BOLD "%i "
C_NBOLD "OV=" C_BOLD "%i "
C_NBOLD "F1=" C_BOLD "%i "
C_NBOLD "P=" C_BOLD "%i "
C_NBOLD "DPTR=" C_BOLD "%.4X "
C_NBOLD "@DPTR=" C_BOLD "%.4X "
C_NBOLD "SBUF[" C_BOLD "out" C_NBOLD "]=" C_BOLD "%.2X "
C_NBOLD "SBUF[" C_BOLD "in" C_NBOLD "]=" C_BOLD "%.2X\n"
C_RESET,
asmCode,
AppSettings()->mcu->changedIntRAM,
AppSettings()->mcu->idata[AppSettings()->mcu->changedIntRAM],
AppSettings()->mcu->changedExtRAM,
AppSettings()->mcu->xdata[AppSettings()->mcu->changedExtRAM],
AppSettings()->mcu->SFRNames[AppSettings()->mcu->changedSFR],
AppSettings()->mcu->sfr[AppSettings()->mcu->changedSFR - 0x80],
*AppSettings()->mcu->ACC,
(checkRegister(AppSettings()->mcu, PSW_RS0) << 1 |
checkRegister(AppSettings()->mcu, PSW_RS1)) << 3,
*AppSettings()->mcu->R,
*AppSettings()->mcu->SP,
(*AppSettings()->mcu->PSW & 0x80) > 0 ? 1 : 0,
(*AppSettings()->mcu->PSW & 0x40) > 0 ? 1 : 0,
(*AppSettings()->mcu->PSW & 0x20) > 0 ? 1 : 0,
(*AppSettings()->mcu->PSW & 0x04) > 0 ? 1 : 0,
(*AppSettings()->mcu->PSW & 0x02) > 0 ? 1 : 0,
(*AppSettings()->mcu->PSW & 0x01) > 0 ? 1 : 0,
*AppSettings()->mcu->DPTR,
AppSettings()->mcu->xdata[*AppSettings()->mcu->DPTR],
*AppSettings()->mcu->SBUF,
AppSettings()->mcu->OUTPUT == -1 ? 0 : AppSettings()->mcu->OUTPUT);
if (outSth) {
if (outByte >= 32 && outByte <= 126)
print("Send byte: %.2X (%c)\n", outByte, outByte);
else
print("Send byte: %.2X\n", outByte);
}
} else {
print("%s", asmCode, AppSettings()->out);
}
free(asmCode);
return ret;
}
void hex(void* memory, WORD start, WORD stop, int offset)
{
int i = start & 0xFFF0;
if (i >= stop)
return;
for (; i <= stop; i += 0x10) {
int j;
print(C_BOLD C_FWHITE "0x%.4X " C_RESET, i);
for (j = 0; j <= 0xF; ++j) {
BYTE byte = *(BYTE*)(memory + (i - offset) + j);
byte > 0 ? print(C_FWHITE) : print(C_BOLD C_FRED);
print("%.2X ", byte);
print(C_RESET);
}
for (j = 0; j <= 0xF; ++j) {
BYTE byte = *(BYTE*)(memory + (i - offset) + j);
byte > 0 ? print(C_FWHITE) : print(C_BOLD C_FRED);
if (byte >= 32 && byte <= 126)
print("%c", byte);
else
print(".");
print(C_RESET);
}
print("\n");
}
}
void cmd_stop(int argc, char** argv)
{
if (g_MCUThreadRunning) {
AppSettings()->stopThread = true;
pthread_join(g_MCUThread, NULL);
}
}
void cmd_run(int argc, char** argv)
{
STOP_IF_THREAD_RUN(return);
if (argc >= 2) {
int i = 0;
int n = atoi(argv[1]);
for (i = 0; i < n; ++i)
if (!processMCUEx(AppSettings()->mcu, NULL, NULL, NULL, NULL))
break;
printf(g_stoped, AppSettings()->mcu->PC, i);
} else {
pthread_create(&g_MCUThread, NULL, (void*)(void*)runMCU, NULL);
}
}
void cmd_terminal(int argc, char** argv)
{
STOP_IF_THREAD_RUN(return);
pthread_create(&g_keyEventLoop, NULL, &keyEventLoop, NULL);
runMCU(&defputchar);
AppSettings()->stopThread = true;
pthread_join(g_keyEventLoop, NULL);
if (AppSettings()->out == AppSettings()->defaultOut)
putchar('\n');
}
void cmd_hexoutput(int argc, char** argv)
{
pthread_create(&g_keyEventLoop, NULL, &keyEventLoop, NULL);
runMCU(&hexputchar);
AppSettings()->stopThread = true;
pthread_join(g_keyEventLoop, NULL);
AppSettings()->stopThread = false;
if (AppSettings()->out == AppSettings()->defaultOut)
putchar('\n');
}
void cmd_trace(int argc, char** argv)
{
STOP_IF_THREAD_RUN(return);
REQUIRED_ARGS(1, return);
bool moreInfo = false;
if (argc >= 3)
moreInfo = boolQuestion(argv[2], "y", "n", NULL);
int i = 0;
int n = atoi(argv[1]);
for (i = 0; i < n; ++i)
if (!step(moreInfo))
break;
print(g_stoped, AppSettings()->mcu->PC, i);
}
void cmd_cycles(int argc, char** argv)
{
print("cycles = %llu\n", AppSettings()->mcu->cycles);
}
void cmd_oscillator(int argc, char** argv)
{
if (argc >= 2)
sscanf(argv[1], "%u[ ][Hz]", &AppSettings()->mcu->oscillator);
else
print("XTAL = %u Hz = %.4f MHz\n", AppSettings()->mcu->oscillator,
(double)AppSettings()->mcu->oscillator / 1000000);
AppSettings()->simSyncTimeBeforeStop = getMCUTime(AppSettings()->mcu);
}
void cmd_step(int argc, char** argv)
{
STOP_IF_THREAD_RUN(return);
step(true);
}
void cmd_reset(int argc, char** argv)
{
STOP_IF_THREAD_RUN(return);
resetMCU(AppSettings()->mcu);
AppSettings()->simTimeBeforeStop = 0;
AppSettings()->simSyncTimeBeforeStop = 0;
}
void cmd_state(int argc, char** argv)
{
print("Cycles: " C_BOLD C_FWHITE "%llu" C_RESET "\n",
AppSettings()->mcu->cycles);
print("Oscillator clocks: " C_BOLD C_FWHITE "%llu" C_RESET "\n",
AppSettings()->mcu->cycles * 12);
print("Oscillator frequency: " C_BOLD C_FWHITE "%u Hz" C_RESET "\n",
AppSettings()->mcu->oscillator);
print("Executed instructions: " C_BOLD C_FWHITE "%llu" C_RESET "\n",
AppSettings()->mcu->instructions);
print("Run time: " C_BOLD C_FWHITE "%.8f seconds" C_RESET "\n",
getMCUTime(AppSettings()->mcu));
print("Sim time: " C_BOLD C_FWHITE "%.8f seconds" C_RESET "\n",
AppSettings()->simSec);
print("Max value of stack pointer: " C_BOLD C_FWHITE "0x%X" C_RESET "\n",
AppSettings()->mcu->maxValueOfStackPointer);
print("Highest used internal ROM address: " C_BOLD C_FWHITE "0x%.4X" C_RESET "\n",
AppSettings()->mcu->maxIntRomAddress);
print("Highest used external ROM address: " C_BOLD C_FWHITE "0x%.4X" C_RESET "\n",
AppSettings()->mcu->maxExtRomAddress);
print("Highest used IDATA address: ");
if (AppSettings()->mcu->maxIntRamAddress == -1)
print(C_BOLD C_FWHITE "Not used." C_RESET "\n");
else
print(C_BOLD C_FWHITE "0x%.2X" C_RESET "\n",
AppSettings()->mcu->maxIntRamAddress);
print("Lowest used IDATA address: ");
if (AppSettings()->mcu->minIntRamAddress == -1)
print(C_BOLD C_FWHITE "Not used." C_RESET "\n");
else
print(C_BOLD C_FWHITE "0x%.2X" C_RESET "\n",
AppSettings()->mcu->minIntRamAddress);
print("Highest used XDATA address: ");
if (AppSettings()->mcu->maxExtRamAddress == -1)
print(C_BOLD C_FWHITE "Not used." C_RESET "\n");
else
print(C_BOLD C_FWHITE "0x%.4X" C_RESET "\n",
AppSettings()->mcu->maxExtRamAddress);
print("Lowest used XDATA address: ");
if (AppSettings()->mcu->minExtRamAddress == -1)
print(C_BOLD C_FWHITE "Not used." C_RESET "\n");
else
print(C_BOLD C_FWHITE "0x%.4X" C_RESET "\n",
AppSettings()->mcu->minExtRamAddress);
print("Available internal ROM memory: " C_BOLD C_FWHITE "%.2f kB" C_RESET "\n",
(double) AppSettings()->mcu->iromMemorySize / 1024);
print("Available external ROM memory: " C_BOLD C_FWHITE "%.2f kB" C_RESET "\n",
(double) AppSettings()->mcu->xromMemorySize / 1024);
print("Available IDATA memory: " C_BOLD C_FWHITE "%i bytes" C_RESET "\n",
AppSettings()->mcu->idataMemorySize);
print("Available XDATA memory: " C_BOLD C_FWHITE "%.2f kB" C_RESET "\n",
(double) AppSettings()->mcu->xdataMemorySize / 1024);
if (checkRegister(AppSettings()->mcu, PCON_IDL))
print("Microcontroller state: " C_BOLD C_FWHITE "IDLE" C_RESET "");
else if (checkRegister(AppSettings()->mcu, PCON_PD))
print("Microcontroller state: " C_BOLD C_FWHITE "POWER DOWN" C_RESET "");
else
print("Microcontroller state: " C_BOLD C_FWHITE "OK" C_RESET "");
print("\n");
}
void cmd_load(int argc, char** argv)
{
STOP_IF_THREAD_RUN(return);
REQUIRED_ARGS(2, return);
bool valid = false;
WORD highestAddress = 0;
MemoryType memType = stringToMemory(argv[1]);
if (memType == IROM) {
resetMCU(AppSettings()->mcu);
loadIntelHexFile(argv[2], AppSettings()->mcu->irom,
0, AppSettings()->mcu->iromMemorySize - 1, &valid, &highestAddress);
/* load to int ram if no memory */
if (highestAddress >= AppSettings()->mcu->iromMemorySize) {
loadIntelHexFile(argv[2], AppSettings()->mcu->xrom,
AppSettings()->mcu->iromMemorySize, AppSettings()->mcu->xromMemorySize - 1, &valid, NULL);
}
AppSettings()->simTimeBeforeStop = 0;
} else if (memType == XROM) {
resetMCU(AppSettings()->mcu);
loadIntelHexFile(argv[2], AppSettings()->mcu->xrom, 0, AppSettings()->mcu->xromMemorySize - 1, &valid, NULL);
AppSettings()->simTimeBeforeStop = 0;
} else {
fprintf(AppSettings()->errorOut, "Specify memory type. Choose `%s` or `%s`!\n",
memoryToString(IROM), memoryToString(XROM));
}
if (!valid)
fprintf(AppSettings()->errorOut, "File not loaded corectly!\n");
}
void cmd_pc(int argc, char** argv)
{
if (argc >= 2) {
bool valid;
int address = hextoi(argv[1], 0x0, 0xFFFF, 0x0, &valid);
if (!valid)
fprintf(AppSettings()->errorOut, "Address %s is invalid.\n", argv[1]);
else
AppSettings()->mcu->PC = address;
} else
print("PC = %.4X\n", (unsigned)AppSettings()->mcu->PC);
}
void cmd_deasm(int argc, char** argv)
{
REQUIRED_ARGS(1, return);
bool valid;
int from = argc >= 2 ? hextoi(argv[1], 0x0, 0xFFFF, 0x0, &valid) : 0;
int lines = argc >= 3 ? atoi(argv[2]) : 0x10000;
if (!valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.\n");
return;
}
int ip = 0;
for (ip = 0; ip < from; ip += AppSettings()->mcu->byteCount[*ROM(AppSettings()->mcu, ip)]);
if (ip != from)
printf("%.4Xh probably is not valid position.\n", from);
for (int i = 0; i < lines; ++i) {
char* asmCode = disassembler(AppSettings()->mcu, from, AppSettings()->format, &from);
print(asmCode);
free(asmCode);
}
if (AppSettings()->out == AppSettings()->defaultOut)
putchar('\n');
}
void cmd_byte(int argc, char** argv)
{
REQUIRED_ARGS(2, return);
MemoryType memType = stringToMemory(argv[1]);
bool valid;
char* memory;
BYTE* byte = NULL;
WORD address = 0;
if (memType == IDATA) {
address = hextoi(argv[2], 0x0, 0xFF, 0x0, &valid);
byte = &AppSettings()->mcu->idata[address];
memory = "IDATA";
if (!valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
} else if (memType == XDATA) {
address = hextoi(argv[2], 0x0, 0xFFFF, 0x0, &valid);
byte = &AppSettings()->mcu->xdata[address];
memory = "XDATA";
if (!valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
} else if (memType == IROM) {
address = hextoi(argv[2], 0x0, 0xFFFF, 0x0, &valid);
byte = &AppSettings()->mcu->irom[address];
memory = "IROM";
if (!valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
} else if (memType == XROM) {
address = hextoi(argv[2], 0x0, 0xFFFF, 0x0, &valid);
byte = &AppSettings()->mcu->xrom[address];
memory = "XROM";
if (!valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
} else if (memType == SFR) {
bool found = false;
for (int i = 0x80; i < 0x100; ++i) {
if (0 == stricmp(argv[2], AppSettings()->mcu->SFRNames[i])) {
address = i;
found = true;
break;
}
}
if (!found)
address = hextoi(argv[2], 0x80, 0xFF, 0x80, &valid);
if (!found && !valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
byte = &AppSettings()->mcu->sfr[address - 0x80];
memory = "SFR";
}
if (byte != NULL) {
if (argc >= 4) {
bool valid;
int value = hextoi(argv[3], 0x0, 0xFF, 0x0, &valid);
if (!valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
*byte = value;
} else {
print("%s[%.2X] = %.2X\n", memory, address, *byte);
}
}
}
void cmd_bit(int argc, char** argv)
{
REQUIRED_ARGS(3, return);
MemoryType memType = stringToMemory(argv[1]);
BYTE bit = 1 << atoi(argv[3]);
bool valid;
char* memory;
BYTE* byte = NULL;
WORD address = 0;
if (memType == IDATA) {
address = hextoi(argv[2], 0x0, 0xFF, 0x0, &valid);
byte = &AppSettings()->mcu->idata[address];
memory = "IDATA";
if (!valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
} else if (memType == XDATA) {
address = hextoi(argv[2], 0x0, 0xFFFF, 0x0, &valid);
byte = &AppSettings()->mcu->xdata[address];
memory = "XDATA";
if (!valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
} else if (memType == IROM) {
address = hextoi(argv[2], 0x0, 0xFFFF, 0x0, &valid);
byte = &AppSettings()->mcu->irom[address];
memory = "ROM";
if (!valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
} else if (memType == XROM) {
address = hextoi(argv[2], 0x0, 0xFFFF, 0x0, &valid);
byte = &AppSettings()->mcu->xrom[address];
memory = "ROM";
if (!valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
} else if (memType == SFR) {
bool found = false;
for (int i = 0x80; i < 0x100; ++i) {
if (0 == stricmp(argv[2], AppSettings()->mcu->SFRNames[i])) {
address = i;
found = true;
break;
}
}
if (!found)
address = hextoi(argv[2], 0x80, 0xFF, 0x80, &valid);
if (!found && !valid) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
byte = &AppSettings()->mcu->sfr[address - 0x80];
memory = "SFR";
}
if (byte != NULL) {
if (argc >= 5) {
bool valid;
bool answer = boolQuestion(argv[4], "1", "0", &valid);
if (!valid)
fprintf(AppSettings()->errorOut, "Invalid argument.\n");
else {
if (answer)
*byte |= bit;
else
*byte &= ~bit;
}
} else {
print("%s[%.2X.%i] = ", memory, address, atoi(argv[3]));
if(*byte & bit)
print("true\n");
else
print("false\n");
}
}
}
void cmd_hex(int argc, char** argv)
{
REQUIRED_ARGS(1, return);
MemoryType memType = stringToMemory(argv[1]);
int start = 0;
int stop = 0;
if (memType == IDATA) {
start = argc >= 3 ? hextoi(argv[2], 0x0, 0xFF, 0x0, NULL) : 0x0;
stop = argc >= 4 ? hextoi(argv[3], start, 0xFF, start, NULL) : 0xFF;
hex(AppSettings()->mcu->idata, start, stop, 0);
} else if (memType == XDATA) {
start = argc >= 3 ? hextoi(argv[2], 0x0, 0xFFFF, 0x0, NULL) : 0x0;
stop = argc >= 4 ? hextoi(argv[3], start, 0xFFFF, start, NULL) : 0xFFFF;
hex(AppSettings()->mcu->xdata, start, stop, 0);
} else if (memType == IROM) {
start = argc >= 3 ? hextoi(argv[2], 0x0, 0xFFFF, 0x0, NULL) : 0x0;
stop = argc >= 4 ? hextoi(argv[3], start, 0xFFFF, start, NULL) : 0xFFFF;
hex(AppSettings()->mcu->irom, start, stop, 0);
} else if (memType == XROM) {
start = argc >= 3 ? hextoi(argv[2], 0x0, 0xFFFF, 0x0, NULL) : 0x0;
stop = argc >= 4 ? hextoi(argv[3], start, 0xFFFF, start, NULL) : 0xFFFF;
hex(AppSettings()->mcu->xrom, start, stop, 0);
} else if (memType == SFR) {
start = argc >= 3 ? hextoi(argv[2], 0x80, 0xFF, 0x0, NULL) : 0x80;
stop = argc >= 4 ? hextoi(argv[3], start, 0xFF, start, NULL) : 0xFF;
hex(AppSettings()->mcu->sfr, start, stop, 0x80);
} else {
fputs("Undefined memory type.\n", AppSettings()->out);
}
}
void cmd_fill(int argc, char** argv)
{
REQUIRED_ARGS(2, return);
MemoryType memType = stringToMemory(argv[1]);
int start = 0;
int stop = 0;
bool valid1;
bool valid2;
bool valid3;
if (memType == IDATA) {
start = argc >= 4 ? hextoi(argv[3], 0x0, 0xFF, 0x0, &valid1) : 0x0;
stop = argc >= 5 ? hextoi(argv[4], start, 0xFF, start, &valid2) : 0xFF;
int address = hextoi(argv[2], 0x0, 0xFF, 0x0, &valid3);
if (!valid1 || !valid2 || !valid3) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
memset(AppSettings()->mcu->idata + start, address, stop - start);
} else if (memType == XDATA) {
start = argc >= 4 ? hextoi(argv[3], 0x0, 0xFFFF, 0x0, &valid1) : 0x0;
stop = argc >= 5 ? hextoi(argv[4], start, 0xFFFF, start, &valid2) : 0xFFFF;
int address = hextoi(argv[2], 0x0, 0xFF, 0x0, &valid3);
if (!valid1 || !valid2 || !valid3) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
memset(AppSettings()->mcu->xdata + start, address, stop - start);
} else if (memType == IROM) {
start = argc >= 4 ? hextoi(argv[3], 0x0, 0xFFFF, 0x0, &valid1) : 0x0;
stop = argc >= 5 ? hextoi(argv[4], start, 0xFFFF, start, &valid2) : 0xFFFF;
int address = hextoi(argv[2], 0x0, 0xFF, 0x0, &valid3);
if (!valid1 || !valid2 || !valid3) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
memset(AppSettings()->mcu->irom + start, address, stop - start);;
} else if (memType == XROM) {
start = argc >= 4 ? hextoi(argv[3], 0x0, 0xFFFF, 0x0, &valid1) : 0x0;
stop = argc >= 5 ? hextoi(argv[4], start, 0xFFFF, start, &valid2) : 0xFFFF;
int address = hextoi(argv[2], 0x0, 0xFF, 0x0, &valid3);
if (!valid1 || !valid2 || !valid3) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
memset(AppSettings()->mcu->xrom + start, address, stop - start);;
} else if (memType == SFR) {
start = argc >= 4 ? hextoi(argv[3], 0x80, 0xFF, 0x0, &valid1) : 0x80;
stop = argc >= 5 ? hextoi(argv[4], start, 0xFF, start, &valid2) : 0xFF;
int address = hextoi(argv[2], 0x80, 0xFF, 0x80, &valid3);
if (!valid1 || !valid2 || !valid3) {
fprintf(AppSettings()->errorOut, "Invalid argument.");
return;
}
start -= 0x80;
stop -= 0x80;
memset(AppSettings()->mcu->sfr + start, address, stop - start);
}
}
void cmd_break(int argc, char** argv)
{
REQUIRED_ARGS(1, return);
for (int i = 1; i < argc; ++i) {
bool valid;
int address = hextoi(argv[i], 0x0, 0xFFFF, 0x0, &valid);
if (!valid)
fprintf(AppSettings()->errorOut, "Address %s is invalid.\n", argv[i]);
else
setPCBreakpoint(AppSettings()->mcu, address);
}
}
void cmd_access(int argc, char** argv)
{
REQUIRED_ARGS(2, return);
MemoryType memType = stringToMemory(argv[1]);
for (int i = 2; i < argc; ++i) {
bool valid;
int address = hextoi(argv[i], 0x0, 0xFFFF, 0x0, &valid);
if (!valid)
fprintf(AppSettings()->errorOut, "Address %s is invalid.\n", argv[i]);
else
setAccessPause(AppSettings()->mcu, memType, address);
}
}
void cmd_cond(int argc, char** argv)
{
REQUIRED_ARGS(4, return);
MemoryType memType = stringToMemory(argv[1]);
BreakpointType operatorType = stringToOperator(argv[3]);
bool valid;
int value = hextoi(argv[2], 0x0, 0xFF, 0x0, &valid);
if (!valid)
fprintf(AppSettings()->errorOut, "Value %s is invalid.\n", argv[2]);
else {
for (int i = 4; i < argc; ++i) {
int address = hextoi(argv[i], 0x0, 0xFFFF, 0x0, &valid);
if (!valid)
fprintf(AppSettings()->errorOut, "Address %s is invalid.\n", argv[i]);
else
setCondPause(AppSettings()->mcu, memType, address, operatorType, value);
}
}
}
void cmd_clear(int argc, char** argv)
{
REQUIRED_ARGS(1, return);
for (int i = 1; i < argc; ++i) {
bool valid;