-
Notifications
You must be signed in to change notification settings - Fork 93
/
picsimlab1.cc
2474 lines (2213 loc) · 83.9 KB
/
picsimlab1.cc
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
/* ########################################################################
PICSimLab - Programmable IC Simulator Laboratory
########################################################################
Copyright (c) : 2010-2024 Luis Claudio Gambôa Lopes <[email protected]>
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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
For e-mail suggestions : [email protected]
######################################################################## */
// main window
// #define CONVERTER_MODE
// print timer debug info
// #define TDEBUG
#include "picsimlab1.h"
#include "picsimlab1_d.cc"
CPWindow1 Window1;
// Implementation
#include "picsimlab2.h"
#include "picsimlab3.h"
#include "picsimlab4.h"
#include "picsimlab5.h"
#include "lib/oscilloscope.h"
#include "lib/picsimlab.h"
#include "lib/spareparts.h"
#include "lib/rcontrol.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#else
#ifdef _WIN_
#include <imagehlp.h>
#include <windows.h>
#else
#include <err.h>
#include <execinfo.h>
#include <signal.h>
#endif
#endif
#ifdef _USE_PICSTARTP_
// picstart plus
int prog_init(void);
int prog_loop(_pic* pic);
int prog_end(void);
#endif
#ifdef CONVERTER_MODE
static std::string cvt_fname;
#endif
#ifdef _WIN_
double cpuTime() {
FILETIME a, b, c, d;
if (GetProcessTimes(GetCurrentProcess(), &a, &b, &c, &d) != 0) {
// Returns total user time.
// Can be tweaked to include kernel times as well.
return (double)(d.dwLowDateTime | ((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
} else {
// Handle error
return 0;
}
}
#else
#include <sys/time.h>
#include <time.h>
double cpuTime() {
return (double)clock() / CLOCKS_PER_SEC;
}
#endif
extern "C" {
void file_ready(const char* fname, const char* dir = NULL);
}
void CPWindow1::timer1_EvOnTime(CControl* control) {
// avoid run again before terminate previous
if (PICSimLab.status & (ST_T1 | ST_DI))
return;
PICSimLab.SetSync(1);
PICSimLab.status |= ST_T1;
#ifdef _NOTHREAD
// printf ("overtimer = %i \n", timer1.GetOverTime ());
if (timer1.GetOverTime() < BASETIMER)
#else
if ((!PICSimLab.tgo) && (timer1.GetTime() == BASETIMER))
#endif
{
if (crt) {
label2.SetColor(SystemColor(lxCOLOR_WINDOWTEXT));
label2.Draw();
}
crt = 0;
} else {
if (!crt) {
label2.SetColor(255, 0, 0);
label2.Draw();
}
crt = 1;
}
if (!PICSimLab.tgo) {
zerocount++;
if (zerocount > 3) {
zerocount = 0;
if (timer1.GetTime() > BASETIMER) {
timer1.SetTime(timer1.GetTime() - 5);
}
}
} else {
zerocount = 0;
}
PICSimLab.tgo++;
#ifndef _NOTHREAD
{
std::unique_lock<std::mutex> lk(cpu_mutex);
cpu_cond.notify_one();
}
#endif
if (PICSimLab.tgo > 3) {
if (timer1.GetTime() < 330) {
timer1.SetTime(timer1.GetTime() + 5);
}
PICSimLab.tgo = 1;
}
DrawBoard();
PICSimLab.status &= ~ST_T1;
}
CPWindow1::~CPWindow1(void) {}
void CPWindow1::DrawBoard(void) {
if (PICSimLab.GetNeedResize()) {
double scalex, scaley, scale_temp;
scalex = ((Window1.GetClientWidth() - 175) * 1.0) / PICSimLab.plWidth;
scaley = ((Window1.GetClientHeight() - 10) * 1.0) / PICSimLab.plHeight;
if (scalex < 0.1)
scalex = 0.1;
if (scaley < 0.1)
scaley = 0.1;
if (scalex > 4)
scalex = 4;
if (scaley > 4)
scaley = 4;
if (scalex < scaley)
scale_temp = scalex;
else
scale_temp = scaley;
if (PICSimLab.GetScale() != scale_temp) {
PICSimLab.SetScale(scale_temp);
int nw = (PICSimLab.plWidth * PICSimLab.GetScale());
if (nw == 0)
nw = 1;
int nh = (PICSimLab.plHeight * PICSimLab.GetScale());
if (nh == 0)
nh = 1;
PICSimLab.SetScale(((double)nw) / PICSimLab.plWidth);
draw1.SetWidth(nw);
draw1.SetHeight(nh);
draw1.SetVisible(0);
draw1.SetImgFileName(
GetLocalFile(PICSimLab.GetSharePath() + "boards/" + PICSimLab.GetBoard()->GetPictureFileName()),
PICSimLab.GetScale(), PICSimLab.GetScale());
}
if (PICSimLab.GetBoard()) {
PICSimLab.GetBoard()->SetScale(PICSimLab.GetScale());
PICSimLab.GetBoard()->EvOnShow();
PICSimLab.GetBoard()->Draw();
}
draw1.SetVisible(1);
if (PICSimLab.GetBoard()->GetUseOscilloscope()) {
menu1_Modules_Oscilloscope_EvMenuActive(this);
}
if (PICSimLab.GetBoard()->GetUseSpareParts()) {
Window5.Show();
Window5.timer1.SetRunState(1);
}
PICSimLab.SetNeedResize(0);
statusbar1.Draw();
} else if (PICSimLab.GetBoard()) {
PICSimLab.GetBoard()->Draw();
}
#ifndef _WIN_
Draw();
#endif
}
void CPWindow1::thread1_EvThreadRun(CControl*) {
double t0, t1, etime;
do {
if (PICSimLab.tgo) {
t0 = cpuTime();
PICSimLab.status |= ST_TH;
PICSimLab.GetBoard()->Run_CPU();
if (PICSimLab.GetDebugStatus())
PICSimLab.GetBoard()->DebugLoop();
PICSimLab.tgo--;
PICSimLab.status &= ~ST_TH;
t1 = cpuTime();
#if defined(_NOTHREAD)
/*
if ((t1 - t0) / (Window1.timer1.GetTime ()*1e-5) > 110)
{
tgo++;
}
else
{
tgo = 0;
}
*/
PICSimLab.tgo = 0;
#endif
etime = t1 - t0;
PICSimLab.SetIdleMs((PICSimLab.GetIdleMs() * 0.9) + ((Window1.timer1.GetTime() - etime * 1000) * 0.1));
#ifdef TDEBUG
float ld = (etime) / (Window1.timer1.GetTime() * 1e-5);
printf("PTime= %lf tgo= %2i zeroc= %2i Timer= %3u Perc.= %5.1lf Idle= %5.1lf\n", etime, tgo, zerocount,
Window1.timer1.GetTime(), ld, PICSimLab.GetIdleMs());
#endif
if (PICSimLab.GetIdleMs() < 0)
PICSimLab.SetIdleMs(0);
} else {
#ifndef _NOTHREAD
{
std::unique_lock<std::mutex> lk(cpu_mutex);
cpu_cond.wait(lk);
}
#endif
}
} while (!thread1.TestDestroy());
}
void CPWindow1::thread2_EvThreadRun(CControl*) {
do {
usleep(1000);
if (rcontrol_loop()) {
usleep(100000);
}
} while (!thread2.TestDestroy());
}
void CPWindow1::thread3_EvThreadRun(CControl*) {
PICSimLab.GetBoard()->EvThreadRun();
}
void CPWindow1::timer2_EvOnTime(CControl* control) {
// avoid run again before terminate previous
if (PICSimLab.status & (ST_T2 | ST_DI))
return;
PICSimLab.status |= ST_T2;
if (PICSimLab.GetBoard() != NULL) {
PICSimLab.GetBoard()->RefreshStatus();
switch (PICSimLab.GetCpuState()) {
case CPU_RUNNING:
statusbar1.SetField(0, "Running...");
break;
case CPU_STEPPING:
statusbar1.SetField(0, "Stepping...");
break;
case CPU_HALTED:
statusbar1.SetField(0, "Halted!");
break;
case CPU_BREAKPOINT:
statusbar1.SetField(0, "BreakPoint!");
break;
case CPU_POWER_OFF:
statusbar1.SetField(0, "Power Off!");
break;
case CPU_ERROR:
statusbar1.SetField(0, "Error!");
break;
case CPU_WAITING_GDB:
statusbar1.SetField(0, "Waiting for GDB...");
break;
}
}
label2.SetText(FloatStrFormat("Spd: %3.2fx", ((float)BASETIMER) / timer1.GetTime()));
if (PICSimLab.GetErrorCount()) {
#ifndef __EMSCRIPTEN__
Message_sz(lxString::FromUTF8(PICSimLab.GetError(0).c_str()), 600, 240);
#else
printf("Error: %s\n", PICSimLab.GetError(0).c_str());
#endif
PICSimLab.DeleteError(0);
}
PICSimLab.status &= ~ST_T2;
#ifdef CONVERTER_MODE
if (cvt_fname.Length() > 3) {
SaveWorkspace(cvt_fname);
}
#endif
if (GetNeedClkUpdate()) {
PICSimLab.SetClock(PICSimLab.GetClock());
}
int reason = PICSimLab.GetToDestroy();
switch (reason) {
case RC_EXIT:
WDestroy();
break;
case RC_LOAD: {
PICSimLab.LoadHexFile(rcontrol_get_file_to_load());
WDestroy();
} break;
default:
break;
}
}
void CPWindow1::draw1_EvMouseMove(CControl* control, unsigned int button, unsigned int x, unsigned int y,
unsigned int state) {
x = x / PICSimLab.GetScale();
y = y / PICSimLab.GetScale();
PICSimLab.GetBoard()->EvMouseMove(button, x, y, state);
}
void CPWindow1::draw1_EvMouseButtonPress(CControl* control, unsigned int button, unsigned int x, unsigned int y,
unsigned int state) {
x = x / PICSimLab.GetScale();
y = y / PICSimLab.GetScale();
PICSimLab.GetBoard()->EvMouseButtonPress(button, x, y, state);
}
void CPWindow1::draw1_EvMouseButtonRelease(CControl* control, unsigned int button, unsigned int x, unsigned int y,
unsigned int state) {
x = x / PICSimLab.GetScale();
y = y / PICSimLab.GetScale();
PICSimLab.GetBoard()->EvMouseButtonRelease(button, x, y, state);
}
void CPWindow1::draw1_EvKeyboardPress(CControl* control, const unsigned int key, const unsigned int hkey,
const unsigned int mask) {
PICSimLab.GetBoard()->EvKeyPress(key, mask);
}
void CPWindow1::draw1_EvKeyboardRelease(CControl* control, const unsigned int key, const unsigned int hkey,
const unsigned int mask) {
PICSimLab.GetBoard()->EvKeyRelease(key, mask);
}
#ifndef __EMSCRIPTEN__
// https://www.gamedev.net/forums/topic/457984-walking-the-stack-in-c-with-mingw32/
#ifdef _WIN_
static void windows_print_stacktrace(CONTEXT* context) {
SymInitialize(GetCurrentProcess(), 0, true);
STACKFRAME frame = {0};
#if defined(_M_AMD64)
const DWORD machine = IMAGE_FILE_MACHINE_AMD64;
/* setup initial stack frame */
frame.AddrPC.Offset = context->Rip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrStack.Offset = context->Rsp;
frame.AddrStack.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context->Rbp;
frame.AddrFrame.Mode = AddrModeFlat;
#else
const DWORD machine = IMAGE_FILE_MACHINE_I386;
/* setup initial stack frame */
frame.AddrPC.Offset = context->Eip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrStack.Offset = context->Esp;
frame.AddrStack.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context->Ebp;
frame.AddrFrame.Mode = AddrModeFlat;
#endif
while (StackWalk(machine, GetCurrentProcess(), GetCurrentThread(), &frame, context, 0, SymFunctionTableAccess,
SymGetModuleBase, 0)) {
printf("PICSimLab stack: %p\n", (void*)frame.AddrPC.Offset);
}
SymCleanup(GetCurrentProcess());
}
static LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS* ExceptionInfo) {
switch (ExceptionInfo->ExceptionRecord->ExceptionCode) {
case EXCEPTION_ACCESS_VIOLATION:
fputs("PICSimLab Error: EXCEPTION_ACCESS_VIOLATION\n", stderr);
break;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
fputs("PICSimLab Error: EXCEPTION_ARRAY_BOUNDS_EXCEEDED\n", stderr);
break;
case EXCEPTION_BREAKPOINT:
fputs("PICSimLab Error: EXCEPTION_BREAKPOINT\n", stderr);
break;
case EXCEPTION_DATATYPE_MISALIGNMENT:
fputs("PICSimLab Error: EXCEPTION_DATATYPE_MISALIGNMENT\n", stderr);
break;
case EXCEPTION_FLT_DENORMAL_OPERAND:
fputs("PICSimLab Error: EXCEPTION_FLT_DENORMAL_OPERAND\n", stderr);
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
fputs("PICSimLab Error: EXCEPTION_FLT_DIVIDE_BY_ZERO\n", stderr);
break;
case EXCEPTION_FLT_INEXACT_RESULT:
fputs("PICSimLab Error: EXCEPTION_FLT_INEXACT_RESULT\n", stderr);
break;
case EXCEPTION_FLT_INVALID_OPERATION:
fputs("PICSimLab Error: EXCEPTION_FLT_INVALID_OPERATION\n", stderr);
break;
case EXCEPTION_FLT_OVERFLOW:
fputs("PICSimLab Error: EXCEPTION_FLT_OVERFLOW\n", stderr);
break;
case EXCEPTION_FLT_STACK_CHECK:
fputs("PICSimLab Error: EXCEPTION_FLT_STACK_CHECK\n", stderr);
break;
case EXCEPTION_FLT_UNDERFLOW:
fputs("PICSimLab Error: EXCEPTION_FLT_UNDERFLOW\n", stderr);
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
fputs("PICSimLab Error: EXCEPTION_ILLEGAL_INSTRUCTION\n", stderr);
break;
case EXCEPTION_IN_PAGE_ERROR:
fputs("PICSimLab Error: EXCEPTION_IN_PAGE_ERROR\n", stderr);
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
fputs("PICSimLab Error: EXCEPTION_INT_DIVIDE_BY_ZERO\n", stderr);
break;
case EXCEPTION_INT_OVERFLOW:
fputs("PICSimLab Error: EXCEPTION_INT_OVERFLOW\n", stderr);
break;
case EXCEPTION_INVALID_DISPOSITION:
fputs("PICSimLab Error: EXCEPTION_INVALID_DISPOSITION\n", stderr);
break;
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
fputs("PICSimLab Error: EXCEPTION_NONCONTINUABLE_EXCEPTION\n", stderr);
break;
case EXCEPTION_PRIV_INSTRUCTION:
fputs("PICSimLab Error: EXCEPTION_PRIV_INSTRUCTION\n", stderr);
break;
case EXCEPTION_SINGLE_STEP:
fputs("PICSimLab Error: EXCEPTION_SINGLE_STEP\n", stderr);
break;
case EXCEPTION_STACK_OVERFLOW:
fputs("PICSimLab Error: EXCEPTION_STACK_OVERFLOW\n", stderr);
break;
default:
fputs("PICSimLab Error: Unrecognized Exception\n", stderr);
break;
}
fflush(stderr);
/* If this is a stack overflow then we can't walk the stack, so just show
where the error happened */
if (EXCEPTION_STACK_OVERFLOW != ExceptionInfo->ExceptionRecord->ExceptionCode) {
windows_print_stacktrace(ExceptionInfo->ContextRecord);
} else {
#if defined(_M_AMD64)
printf("PICSimLab Error: %p\n", (void*)ExceptionInfo->ContextRecord->Rip);
#else
printf("PICSimLab Error: %p\n", (void*)ExceptionInfo->ContextRecord->Eip);
#endif
}
return EXCEPTION_EXECUTE_HANDLER;
}
static void set_signal_handler(void) {
SetUnhandledExceptionFilter(windows_exception_handler);
}
#else
#define MAX_STACK_FRAMES 64
static void* stack_traces[MAX_STACK_FRAMES];
static void posix_print_stack_trace() {
int i, trace_size = 0;
char** messages = (char**)NULL;
trace_size = backtrace(stack_traces, MAX_STACK_FRAMES);
messages = backtrace_symbols(stack_traces, trace_size);
/* skip the first couple stack frames (as they are this function and
our handler) and also skip the last frame as it's (always?) junk. */
for (i = 3; i < (trace_size - 1); ++i) {
printf("PICSimLab Stack[%02i]: %s\n", i - 3, messages[i]);
}
if (messages) {
free(messages);
}
}
static void posix_signal_handler(int sig, siginfo_t* siginfo, void* context) {
(void)context;
switch (sig) {
case SIGSEGV:
fputs("PICSimLab Caught SIGSEGV: Segmentation Fault\n", stderr);
break;
case SIGINT:
fputs("PICSimLab Caught SIGINT: Interactive attention signal, (usually ctrl+c)\n", stderr);
break;
case SIGFPE:
switch (siginfo->si_code) {
case FPE_INTDIV:
fputs("PICSimLab Caught SIGFPE: (integer divide by zero)\n", stderr);
break;
case FPE_INTOVF:
fputs("PICSimLab Caught SIGFPE: (integer overflow)\n", stderr);
break;
case FPE_FLTDIV:
fputs("PICSimLab Caught SIGFPE: (floating-point divide by zero)\n", stderr);
break;
case FPE_FLTOVF:
fputs("PICSimLab Caught SIGFPE: (floating-point overflow)\n", stderr);
break;
case FPE_FLTUND:
fputs("PICSimLab Caught SIGFPE: (floating-point underflow)\n", stderr);
break;
case FPE_FLTRES:
fputs("PICSimLab Caught SIGFPE: (floating-point inexact result)\n", stderr);
break;
case FPE_FLTINV:
fputs("PICSimLab Caught SIGFPE: (floating-point invalid operation)\n", stderr);
break;
case FPE_FLTSUB:
fputs("PICSimLab Caught SIGFPE: (subscript out of range)\n", stderr);
break;
default:
fputs("PICSimLab Caught SIGFPE: Arithmetic Exception\n", stderr);
break;
}
case SIGILL:
switch (siginfo->si_code) {
case ILL_ILLOPC:
fputs("PICSimLab Caught SIGILL: (illegal opcode)\n", stderr);
break;
case ILL_ILLOPN:
fputs("PICSimLab Caught SIGILL: (illegal operand)\n", stderr);
break;
case ILL_ILLADR:
fputs("PICSimLab Caught SIGILL: (illegal addressing mode)\n", stderr);
break;
case ILL_ILLTRP:
fputs("PICSimLab Caught SIGILL: (illegal trap)\n", stderr);
break;
case ILL_PRVOPC:
fputs("PICSimLab Caught SIGILL: (privileged opcode)\n", stderr);
break;
case ILL_PRVREG:
fputs("PICSimLab Caught SIGILL: (privileged register)\n", stderr);
break;
case ILL_COPROC:
fputs("PICSimLab Caught SIGILL: (coprocessor error)\n", stderr);
break;
case ILL_BADSTK:
fputs("PICSimLab Caught SIGILL: (internal stack error)\n", stderr);
break;
default:
fputs("PICSimLab Caught SIGILL: Illegal Instruction\n", stderr);
break;
}
break;
case SIGTERM:
fputs("PICSimLab Caught SIGTERM: a termination request was sent to the program\n", stderr);
break;
case SIGABRT:
fputs("PICSimLab Caught SIGABRT: usually caused by an abort() or assert()\n", stderr);
break;
default:
break;
}
posix_print_stack_trace();
_Exit(1);
}
static uint8_t* alternate_stack;
static void set_signal_handler(void) {
/* setup alternate stack */
{
alternate_stack = (uint8_t*)malloc(SIGSTKSZ);
stack_t ss = {};
/* malloc is usually used here, I'm not 100% sure my static allocation
is valid but it seems to work just fine. */
ss.ss_sp = (void*)alternate_stack;
ss.ss_size = SIGSTKSZ;
ss.ss_flags = 0;
if (sigaltstack(&ss, NULL) != 0) {
err(1, "sigaltstack");
}
}
/* register our signal handlers */
{
struct sigaction sig_action = {};
sig_action.sa_sigaction = posix_signal_handler;
sigemptyset(&sig_action.sa_mask);
#ifdef __APPLE__
/* for some reason we backtrace() doesn't work on osx
when we use an alternate stack */
sig_action.sa_flags = SA_SIGINFO;
#else
sig_action.sa_flags = SA_SIGINFO | SA_ONSTACK;
#endif
if (sigaction(SIGSEGV, &sig_action, NULL) != 0) {
err(1, "sigaction");
}
if (sigaction(SIGFPE, &sig_action, NULL) != 0) {
err(1, "sigaction");
}
if (sigaction(SIGINT, &sig_action, NULL) != 0) {
err(1, "sigaction");
}
if (sigaction(SIGILL, &sig_action, NULL) != 0) {
err(1, "sigaction");
}
if (sigaction(SIGTERM, &sig_action, NULL) != 0) {
err(1, "sigaction");
}
if (sigaction(SIGABRT, &sig_action, NULL) != 0) {
err(1, "sigaction");
}
}
}
#endif
#else
static void set_signal_handler(void) {};
#endif
void CPWindow1::_EvOnCreate(CControl* control) {
char home[1024];
lxFileName fn;
lxFileName fn_spare;
char fname[1200];
char fname_error[1200];
int close_error = 0;
set_signal_handler();
strncpy(home, (const char*)lxGetUserDataDir("picsimlab").c_str(), 1023);
PICSimLab.SetWorkspaceFileName("");
PICSimLab.SetHomePath(home);
PICSimLab.SetPath((const char*)lxGetCwd().c_str());
PICSimLab.OnUpdateStatus = &CPWindow1::OnUpdateStatus;
PICSimLab.OnConfigure = &CPWindow1::OnConfigure;
PICSimLab.OnClockSet = &CPWindow1::OnClockSet;
PICSimLab.OnReadPreferences = &CPWindow1::OnReadPreferences;
PICSimLab.OnSavePrefs = &CPWindow1::OnSavePrefs;
PICSimLab.OnLoadHexFile = &CPWindow1::OnLoadHexFile;
PICSimLab.OnOpenLoadHexFileDialog = &CPWindow1::OnOpenLoadHexFileDialog;
PICSimLab.OnEndSimulation = &CPWindow1::OnEndSimulation;
PICSimLab.OnUpdateGUI = &CPWindow1::OnUpdateGUI;
PICSimLab.OnConfigMenuGUI = &CPWindow1::OnConfigMenuGUI;
PICSimLab.OnCanvasCmd = &CPWindow1::OnCanvasCmd;
PICSimLab.OnWindowCmd = &CPWindow1::OnWindowCmd;
PICSimLab.OnSystemCmd = &CPWindow1::OnSystemCmd;
SpareParts.OnCanvasCmd = &CPWindow5::OnCanvasCmd;
SpareParts.OnWindowCmd = &CPWindow5::OnWindowCmd;
Oscilloscope.OnWindowCmd = &CPWindow4::OnWindowCmd;
PICSimLab.Init();
// board menu
for (int i = 0; i < BOARDS_LAST; i++) {
MBoard[i].SetFOwner(this);
MBoard[i].SetName(std::to_string(i));
MBoard[i].SetText(boards_list[i].name);
MBoard[i].EvMenuActive = EVMENUACTIVE & CPWindow1::menu1_EvBoard;
menu1_Board.CreateChild(&MBoard[i]);
}
Oscilloscope.Init();
SpareParts.Init();
#ifndef _SHARE_
#error Define the _SHARE_ path is necessary
#endif
if (std::string(_SHARE_).find("http") != std::string::npos) {
PICSimLab.SetSharePath(std::string(_SHARE_));
} else {
PICSimLab.SetSharePath((const char*)(dirname(lxGetExecutablePath()) + "/" + std::string(_SHARE_)).c_str());
}
fn.Assign(PICSimLab.GetSharePath());
fn.MakeAbsolute();
PICSimLab.SetSharePath((const char*)(fn.GetFullPath() + "/").c_str());
#ifndef _LIB_
#error Define the _LIB_ path is necessary
#endif
#ifndef _VERSION_
#error Define the _VERSION_ path is necessary
#endif
#ifndef _DATE_
#error Define the _DATE_ path is necessary
#endif
#ifndef _ARCH_
#error Define the _ARCH_ path is necessary
#endif
#ifndef _PKG_
#error Define the _PKG_ path is necessary
#endif
PICSimLab.SetLibPath((const char*)(dirname(lxGetExecutablePath()) + "/" + std::string(_LIB_)).c_str());
fn.Assign(PICSimLab.GetLibPath());
fn.MakeAbsolute();
PICSimLab.SetLibPath((const char*)(fn.GetFullPath() + "/").c_str());
#if !defined(__EMSCRIPTEN__) && !defined(_CONSOLE_LOG_)
snprintf(fname, 1199, "%s/picsimlab_log%i.txt", home, PICSimLab.GetInstanceNumber());
printf("PICSimLab: Console output redirected to file: \"%s\"\n", fname);
if (PICSimLab.SystemCmd(PSC_FILEEXISTS, fname)) {
FILE* flog = fopen_UTF8(fname, "r");
if (flog) {
char line[1024];
int finishok = 0;
while (fgets(line, 1023, flog)) {
line[1023] = 0;
if (!strncmp(line, "PICSimLab: Finish Ok", 20)) {
finishok++;
}
}
fclose(flog);
if (finishok != 1) {
close_error = 1;
snprintf(fname_error, 1199, "%s/picsimlab_error%i.txt", home, PICSimLab.GetInstanceNumber());
lxRenameFile(fname, fname_error);
}
}
}
#ifdef _WIN_
if (AllocConsole()) {
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);
}
#endif
lxCreateDir(home);
if (freopen(fname, "w", stdout) == NULL) {
printf("PICSimLab: stdout redirect error [%i] %s \n", errno, strerror(errno));
}
if (dup2(fileno(stdout), fileno(stderr)) == -1) {
printf("PICSimLab: stderr redirect error [%i] %s \n", errno, strerror(errno));
}
printf("PICSimLab: Console output redirected to file: \"%s\"\n", fname);
#endif
printf("PICSimLab: Version \"%s %s %s %s\"\n", _VERSION_, _DATE_, _ARCH_, _PKG_);
printf("PICSimLab: Command Line: ");
for (int i = 0; i < Application->Aargc; i++) {
#ifdef wxUSE_UNICODE
printf("%s ", (const char*)lxString(Application->Aargvw[i]).utf8_str());
#else
printf("%s ", Application->Aargv[i]);
#endif
}
printf("\n");
fflush(stdout);
if (Application->Aargc == 2) { // only .pzw file
#ifdef wxUSE_UNICODE
fn.Assign(Application->Aargvw[1]);
#else
fn.Assign(Application->Aargv[1]);
#endif
fn.MakeAbsolute();
// load options
PICSimLab.Configure(home, 1, 1);
// check if it is a demonstration
std::string fns = (const char*)fn.GetFullPath().utf8_str();
lxFileName fn_dir;
fn_dir.Assign(PICSimLab.GetSharePath() + "boards/");
fn_dir.MakeAbsolute();
if ((fns.find(fn_dir.GetFullPath()) != std::string::npos) && (fns.find("demo.pzw") != std::string::npos)) {
PICSimLab.LoadWorkspace((const char*)fn.GetFullPath().utf8_str(), 0);
PICSimLab.SetWorkspaceFileName("");
} else {
PICSimLab.LoadWorkspace((const char*)fn.GetFullPath().utf8_str());
}
} else if ((Application->Aargc >= 3) && (Application->Aargc <= 5)) {
// arguments: Board Processor File.hex(.bin) file.pcf
if (Application->Aargc >= 4) {
#ifdef wxUSE_UNICODE
fn.Assign(Application->Aargvw[3]);
#else
fn.Assign(Application->Aargv[3]);
#endif
fn.MakeAbsolute();
}
if (Application->Aargc == 5) {
#ifdef wxUSE_UNICODE
fn_spare.Assign(Application->Aargvw[4]);
#else
fn_spare.Assign(Application->Aargv[4]);
#endif
fn_spare.MakeAbsolute();
}
PICSimLab.SetLabs(-1, PICSimLab.GetLab_());
for (int i = 0; i < BOARDS_LAST; i++) {
if (!strcmp(boards_list[i].name_, Application->Aargv[1])) {
PICSimLab.SetLabs(i, PICSimLab.GetLab_());
break;
}
}
if (PICSimLab.GetLab() != -1) {
if (PICSimLab.GetInstanceNumber() && !PICSimLab.GetHomePath().compare(home)) {
snprintf(fname, 1100, "%s/picsimlab_%i.ini", home, PICSimLab.GetInstanceNumber());
} else {
snprintf(fname, 1100, "%s/picsimlab.ini", home);
}
PICSimLab.PrefsClear();
if (PICSimLab.SystemCmd(PSC_FILEEXISTS, fname)) {
if (PICSimLab.PrefsLoadFromFile(fname)) {
PICSimLab.SavePrefs("picsimlab_lab", boards_list[PICSimLab.GetLab()].name_);
PICSimLab.SavePrefs(std::string(boards_list[PICSimLab.GetLab()].name_) + "_proc",
Application->Aargv[2]);
if (Application->Aargc == 5) {
PICSimLab.SavePrefs("spare_on", "1");
}
PICSimLab.PrefsSaveToFile(fname);
}
}
} else {
Application->Aargc = 1;
printf("PICSimLab: Unknown board %s !\n", Application->Aargv[1]);
}
// search for file name
if (Application->Aargc >= 4) {
// load options
PICSimLab.Configure(home, 0, 1, (const char*)fn.GetFullPath().utf8_str());
if (Application->Aargc == 5) {
SpareParts.LoadConfig((const char*)fn_spare.GetFullPath().utf8_str());
}
} else {
// load options
PICSimLab.Configure(home, 0, 1);
}
} else if (close_error) { // no arguments with error
printf(
"PICSimLab: Error closing PICSimLab in last time! \nUsing default mode.\n Erro log file: %s\n If the "
"problem persists, please consider opening an issue on github..",
fname_error);
FILE* ferror;
ferror = fopen(fname_error, "a");
if (ferror) {
char btdir[256];
PICSimLab.SystemCmd(PSC_GETTEMPDIR, "PICSimLab", btdir);
fprintf(ferror,
"\n\nPICSimLab: Error closing PICSimLab in last time! \nUsing default mode.\n Erro log file: %s\n "
"If the problem persists, please consider opening an issue on github :\n"
"https://github.com/lcgamboa/picsimlab/issues\n\n"
"A backup was made of your project that presented a problem with the name following the template:\n"
"%s/backup_<boardname>_<timestamp>.pzw\n\n",
fname_error, btdir);
fclose(ferror);
}
lxLaunchDefaultApplication(fname_error);
// force use demo
PICSimLab.Configure(home, 2, 1);
PICSimLab.RegisterError("Error closing PICSimLab in last time!\n Using default mode.\n Error log file: " +
std::string(fname_error) +
"\n If the problem persists, please consider opening an issue on github.\n ");
} else { // no arguments
// load options
PICSimLab.Configure(home, 0, 1);
}
label1.SetText(PICSimLab.GetBoard()->GetClkLabel());
}
void CPWindow1::OnConfigure(void) {
Window1.Configure();
}
void CPWindow1::OnClockSet(const float clk, const int update) {
if (update) {
if (clk < 1) {
Window1.combo1.SetText(FloatStrFormat("%2.1f", clk));
} else {
Window1.combo1.SetText(FloatStrFormat("%2.0f", clk));
}
Window1.SetNeedClkUpdate(0);
} else {
Window1.SetNeedClkUpdate(1);
}
}
void CPWindow1::OnReadPreferences(const char* name, const char* value, const int create) {
if (!strcmp(name, "picsimlab_debug")) {
#ifndef NO_DEBUG
Window1.togglebutton1.SetCheck(PICSimLab.GetDebugStatus());
#endif
}
if (!strcmp(name, "picsimlab_position")) {
int i, j;
sscanf(value, "%i,%i", &i, &j);
Window1.SetX(i);
Window1.SetY(j);
printf("PICSimLab: Window position x=%i y=%i\n", i, j);
}
if (!strcmp(name, "picsimlab_scale")) {
if (create) {
Window1.draw1.SetWidth(PICSimLab.plWidth * PICSimLab.GetScale());
Window1.SetWidth(185 + PICSimLab.plWidth * PICSimLab.GetScale());
Window1.draw1.SetHeight(PICSimLab.plHeight * PICSimLab.GetScale());
Window1.SetHeight(90 + PICSimLab.plHeight * PICSimLab.GetScale());
}
}
if (!strcmp(name, "picsimlab_lfile")) {
if (PICSimLab.GetFNAME().length() > 1)
Window1.menu1_File_ReloadLast.SetEnable(1);
else
Window1.menu1_File_ReloadLast.SetEnable(1);
}
}
void CPWindow1::OnSavePrefs(void) {
PICSimLab.SavePrefs("picsimlab_position", std::to_string(Window1.GetX()) + "," + std::to_string(Window1.GetY()));
}
void CPWindow1::OnLoadHexFile(const std::string fname) {
if (PICSimLab.GetMcuRun())