Repository navigation
Expand file tree
/
Copy pathcode.txt
More file actions
3574 lines (3383 loc) · 93.6 KB
/
Copy pathcode.txt
File metadata and controls
3574 lines (3383 loc) · 93.6 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
--------Function main--------
----------------main.c----------------
#include "./../include/mainHeader.h"
extern int accumulator;
extern int instructionPointer;
extern int writeUsed;
extern int writeValue;
extern int bigChars[];
extern int SCANPRINTRADIX;
extern int position;
extern int cursorX;
extern int cursorY;
struct itimerval val, oval;
int main(int argc, char** argv){
sc_memoryInit();
sc_regInit();
setSignals();
rk_mytermsave();
sc_regSet(FLAG_INTERRUPT, 1);
int position = 0, fd = 0, cn = 0, tmp = 0, exit = 0, refreshFlg = 0, regis = 0, regisProg = 0;
enum keys key = KEY_other;
if ((fd = open("bigchars.bin", O_RDONLY)) == -1) {
writeChar(2,"Cannot open ascibig\n");
return -1;
}
bc_bigcharread(fd, bigChars, 17, &cn);
close(fd);
mt_clrscr();
refreshGuiSt(position);
while (!exit) {
if (!refreshFlg) {
refreshGui(position);
refreshFlg = 1;
}
rk_readkey(&key);
sc_regGet(FLAG_INTERRUPT, ®isProg);
if(regisProg) {
switch (key) {
case KEY_up:
if (cursorY != 0) cursorY--;
else cursorY = 9;
refreshFlg = 0;
key = KEY_other;
break;
case KEY_down:
cursorY = (cursorY + 1) % 10;
refreshFlg = 0;
key = KEY_other;
break;
case KEY_left:
if (cursorX != 0) cursorX--;
else cursorX = 9;
refreshFlg = 0;
key = KEY_other;
break;
case KEY_right:
cursorX = (cursorX + 1) % 10;
refreshFlg = 0;
key = KEY_other;
break;
case KEY_f5:
refreshFlg = changeAccumulator(position);
key = KEY_other;
break;
case KEY_f6:
refreshFlg = changeInstructionPointer(position);
key = KEY_other;
break;
case KEY_enter:
refreshFlg = changeCell(position);
key = KEY_other;
break;
case KEY_t:
CU();
refreshGui(instructionPointer);
position = instructionPointer;
cursorX = instructionPointer / 10;
cursorY = instructionPointer % 10;
key = KEY_other;
break;
case KEY_s:
memorySave(position);
refreshFlg = 1;
key = KEY_other;
break;
case KEY_l:
memoryLoad(position);
refreshFlg = 1;
key = KEY_other;
break;
case KEY_i:
raise(SIGUSR1);
refreshFlg = 0;
cursorX = 0;
cursorY = 0;
key = KEY_other;
break;
case KEY_q:
exit = 1;
key = KEY_other;
break;
}
}
if (key == KEY_c) {
position = instructionPointer;
cursorX = instructionPointer / 10;
cursorY = instructionPointer % 10;
refreshFlg = 0;
raise(SIGUSR2);
key = KEY_other;
} else if (key == KEY_esc) {
raise(SIGTERM);
key = KEY_other;
} else if (key == KEY_r) {
key = KEY_other;
sc_regGet(FLAG_INTERRUPT, ®is);
if (regis) {
sc_regInit();
frequencyGenerator(START);
position = instructionPointer;
cursorX = instructionPointer / 10;
cursorY = instructionPointer % 10;
refreshFlg = 0;
continue;
} else {
frequencyGenerator(STOP);
sc_regSet(FLAG_INTERRUPT, 1);
position = instructionPointer;
cursorX = instructionPointer / 10 ;
cursorY = instructionPointer % 10;
refreshFlg = 0;
continue;
}
}
position = cursorY + cursorX * 10;
}
frequencyGenerator(STOP);
rk_mytermrestore();
signalsRestore();
system("rm -f termsettings");
return 0;
}
--------libSimpleComputer.a--------
----------------global.c----------------
#include "./../include/sc.h"
int localRAM[sizeRAM];
short int sc_register;
int correctCommands[countCmd] = { 0x10, 0x11, 0x20, 0x21, 0x30, 0x31, 0x32,
0x33, 0x40, 0x41, 0x42, 0x43, 0x51, 0x52,
0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73,
0x74, 0x75, 0x76 };
----------------sc_memory.c----------------
#include "./../include/sc_memory.h"
#include "./../include/sc_register.h"
extern int localRAM[];
int sc_memoryInit(void){
for (int i = 0; i < sizeRAM; i++) localRAM[i] = 0;
return 0;
}
int sc_memorySet(int addres, int value){
if ((addres < sizeRAM) & (addres >= 0)) {
localRAM[addres] = value;
return 0;
} else {
sc_regSet(FLAG_OUTMEM, 1);
return ERR_WRONG_ADDR;
}
}
int sc_memoryGet(int addres, int* value){
if ((addres < sizeRAM) & (addres >=0) & (value != NULL)) {
*value = localRAM[addres];
return 0;
} else {
sc_regSet(FLAG_OUTMEM, 1);
return ERR_WRONG_ADDR;
}
}
volatile int sc_memorySave(char* filename){
int data = 0, writecount = 0;
if ((data = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1)
return ERR_OPEN_FILE;
writecount = write(data, localRAM, sizeof(int) * sizeRAM);
close(data);
if (writecount != sizeof(int) * sizeRAM) return ERR_FILE;
else return 0;
}
volatile int sc_memoryLoad(char* filename){
int data = 0, readcount = 0, i = 0;
int ram[sizeRAM] = {0};
if ((data = open(filename, O_RDONLY)) == -1) return ERR_OPEN_FILE;
readcount = read(data, ram, sizeof(int) * sizeRAM);
close(data);
for (i = 0; i < sizeRAM; i++) ram[i] &= 0x7FFF;
if (readcount != sizeof(int) * sizeRAM) return ERR_FILE;
else for (i = 0; i < sizeRAM; i++) localRAM[i] = ram[i];
return 0;
}
----------------sc_register.c----------------
#include "./../include/sc_register.h"
extern short int sc_register;
int sc_regInit(void){
sc_register = 0;
return 0;
}
int sc_regSet(int scRegister, int value){
if ((scRegister >= 0) & (scRegister <=5))
if (value == 1) BITSET(sc_register, scRegister);
else if (value == 0) BITCLEAR(sc_register, scRegister);
else return ERR_WRONG_FLAG;
else return ERR_WRONG_FLAG;
return 0;
}
int sc_regGet(int scRegister, int* value){
if ((scRegister >= 0) & (scRegister <=5))
*value = BITCHECK(sc_register, scRegister);
else return ERR_WRONG_FLAG;
return 0;
}
----------------sc_command.c----------------
#include "./../include/sc_command.h"
#include "./../include/sc_register.h"
extern int correctCommands[];
int sc_commandEncode(int command, int operand, int* value){
int i = 0, *commandPtr = NULL;
for (i = 0; i < countCmd; i++)
if (command == correctCommands[i]) commandPtr = &(correctCommands[i]);
if (commandPtr != NULL) {
if (operand > 0x7F) {
operand &= 0x7F;
sc_regSet(FLAG_OVERFLOW,1);
}
if (value != NULL) {
*value = ((command << 7) | operand) & 0x3FFF;
return 0;
}
} else {
sc_regSet(FLAG_COMMAND, 1);
return ERR_UNCORRECT_COMMAND;
}
return ERR_WRONG_ADDR;
}
int sc_commandDecode(int value, int* command, int* operand){
int *correctCommand = NULL;
int attribute = (value >> 14) & 1, tmpCommand = 0, tmpOperand = 0, i = 0;
if (value == 0) {
*command = 0;
*operand = 0;
return 2;
}
if (command != NULL & operand != NULL) {
if (attribute == 0) {
tmpCommand = (value >> 7) & 0x7F;
tmpOperand = value & 0x7F;
for (i = 0; i < countCmd; i ++) {
if (tmpCommand == correctCommands[i]) {
correctCommand = &(correctCommands[i]);
}
}
if (!(correctCommand == NULL)) {
*command = tmpCommand;
*operand = tmpOperand;
} else {
sc_regSet(FLAG_COMMAND, 1);
return ERR_UNCORRECT_COMMAND;
}
return 0;
} else {
return ERR_ATTRIBUTE_BIT;
}
}
return 0;
}
----------------sc.h----------------
#ifndef SC_H
#define SC_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#define sizeRAM 100
#define countCmd 38
#define FLAG_OVERFLOW 0
#define FLAG_DIVISION 1
#define FLAG_OUTMEM 2
#define FLAG_INTERRUPT 3
#define FLAG_COMMAND 4
#define FLAG_ODD 5
#define ERR_WRONG_ADDR -1
#define ERR_OPEN_FILE -2
#define ERR_FILE -3
#define ERR_UNCORRECT_COMMAND -4
#define ERR_ATTRIBUTE_BIT -5
#define ERR_WRONG_FLAG -6
#define BITSET(A, B) (A =((A) | (1 << (B))))
#define BITCLEAR(A, B) (A = ((A) & ~(1 << (B))))
#define BITCHECK(A, B) (((A) >> B) & 1)
#endif
----------------sc_memory.h----------------
#ifndef SC_MEMORY_H
#define SC_MEMORY_H
#include "./sc.h"
int sc_memoryInit(void);
int sc_memorySet(int addres, int value);
int sc_memoryGet(int addres, int* value);
volatile int sc_memorySave(char* filename);
volatile int sc_memoryLoad(char* filename);
#endif
----------------sc_register.h----------------
#ifndef SC_REGISTER_H
#define SC_REGISTER_H
#include "./sc.h"
int sc_regInit(void);
int sc_regSet(int sc_register, int value);
int sc_regGet(int sc_register, int* value);
#endif
----------------sc_command.h----------------
#ifndef SC_COMMAND_H
#define SC_COMMAND_H
#include "./sc.h"
int sc_commandEncode(int command, int operand, int* value);
int sc_commandDecode(int value, int* command, int* operand);
#endif
--------libTerm.a--------
----------------term.c----------------
#include "./../include/term.h"
int mt_clrscr(){
writeChar(1, "\E[H\E[2J");
return 0;
}
int mt_gotoXY(int x, int y){
int rows = 0, cols = 0;
mt_getscreensize(&rows, &cols);
if (((y < rows) & (y >= 0)) & ((x < cols) & (x >= 0))) {
writeChar(1, "\E[");
writeInt(1, y, 10, -1);
writeChar(1, ";");
writeInt(1, x, 10, -1);
writeChar(1, "H");
return 0;
} else return -1;
}
int mt_getscreensize(int *rows, int *cols){
struct winsize w;
if (!ioctl(STDOUT_FILENO, TIOCGWINSZ, &w)) {
*rows = w.ws_row;
*cols = w.ws_col;
return 0;
} else return -1;
}
int mt_setfgcolor(enum colors color){
if (color >= 0 & color <= 9) {
writeChar(1, "\E[");
writeInt(1, 30 + color, 10, -1);
writeChar(1, "m");
return 0;
} else return -1;
}
int mt_setbgcolor(enum colors color){
if (color >= 0 & color <= 9) {
writeChar(1, "\E[");
writeInt(1, 40 + color, 10, -1);
writeChar(1, "m");
return 0;
} else return -1;
}
----------------term.h----------------
#ifndef CONSOLE_SC_H
#define CONSOLE_SC_H
#include <sys/ioctl.h>
#include "./../../print/include/write.h"
enum colors {
clr_black = 0,
clr_red = 1,
clr_green = 2,
clr_yellow = 3,
clr_blue = 4,
clr_purple = 5,
clr_cyan = 6,
clr_light_blue = 7,
clr_default = 9
};
int mt_clrscr();
int mt_gotoXY(int x, int y);
int mt_getscreensize(int *rows, int *cols);
int mt_setfgcolor(enum colors color);
int mt_setbgcolor(enum colors color);
#endif
--------libBigChars.a--------
----------------bc.c----------------
#include "./../include/bc.h"
static void swap(int* a, int* b){
int tmp = 0;
tmp = *a;
*a = *b;
*b = tmp;
}
int bc_printA(char *str){
writeChar(1, "\E(0");
write(1, str, strlen(str));
writeChar(1, "\E(B");
return 0;
}
int bc_box(int x1, int y1, int x2, int y2){
int maxx = 0, maxy = 0, i = 0, j = 0;
if (x1 > x2) swap(&x1, &x2);
if (y1 > y2) swap(&y1, &y2);
mt_getscreensize(&maxy, &maxx);
if ((x1 < 0) | (y1 < 0) | (x2 > maxx) | (y2 > maxy) | (x2 - x1 < 2) |
(y2 - y1 < 2))
return -1;
mt_gotoXY(x1, y1);
bc_printA(BOXCHAR_TL);
for (i = x1 + 1; i < x2; i++) bc_printA(BOXCHAR_HOR);
bc_printA(BOXCHAR_TR);
for (i = y1 +1; i < y2; i++) {
mt_gotoXY(x1, i);
bc_printA(BOXCHAR_VERT);
mt_gotoXY(x2, i);
bc_printA(BOXCHAR_VERT);
}
mt_gotoXY(x1, y2);
bc_printA(BOXCHAR_BL);
for (i = x1 + 1; i < x2; i++) bc_printA(BOXCHAR_HOR);
bc_printA(BOXCHAR_BR);
return 0;
}
int bc_printbigchar(int *big, int x, int y, enum colors fg, enum colors bg){
int pos = 0, bit = 0, maxx = 0, maxy = 0, i = 0, j = 0;
char row[9] = {0};
mt_getscreensize(&maxy, &maxx);
if ((x < 0) | (y < 0) | (x + 8 > maxx) | (y + 8 > maxy)) return -1;
row[8] = '\0';
mt_setfgcolor(fg);
mt_setbgcolor(bg);
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
pos = i >> 2;
bit = (big[pos] >> ((i % 4) * 8 + j)) & 1;
if (bit == 0) row[j] = ' ';
else row[j] = BOXCHAR_REC;
}
mt_gotoXY(x, y + i);
bc_printA(row);
}
mt_setfgcolor(clr_default);
mt_setbgcolor(clr_default);
return 0;
}
int bc_setbigcharpos(int *big, int x, int y, int value){
int pos = 0;
if ((x < 0) | (y < 0) | (x > 7) | (y > 7) | (value < 0) | (value > 1)) return -1;
if (y <= 3) pos = 0;
else pos = 1;
y = y % 4;
if (value == 0) big[pos] &= ~(1 << (y*8 + x));
else big[pos] |= 1 << (y*8 + x);
return 0;
}
int bc_getbigcharpos(int *big, int x, int y, int *value){
int pos = 0;
if ((x < 0) | (y < 0) | (x > 7) | (y > 7)) return -1;
if (y <= 3) pos = 0;
else pos = 1;
y = y % 4;
*value = (big[pos] >> (y*8 + x)) & 1;
return 0;
}
int bc_bigcharwrite(int fd, int *big, int count){
int err = 0;
err = write(fd, &count, sizeof(count));
if (err == -1) return -1;
err = write(fd, big, count * sizeof(int) * 2);
if (err == -1) return -1;
return 0;
}
int bc_bigcharread(int fd, int *big, int need_count, int *count){
int n, cnt, err;
err = read(fd, &n, sizeof(n));
if ((err == -1) | (err != sizeof(n))) return -1;
cnt = read(fd, big, need_count * sizeof(int) * 2);
if (cnt == -1) return -1;
*count = cnt / (sizeof(int) * 2);
return 0;
}
----------------bc.h----------------
#ifndef BC_H
#define BC_H
#include "./../../consoleSC/include/term.h"
#include "./../../print/include/write.h"
#define BOXCHAR_REC 'a'
#define BOXCHAR_BR "j"
#define BOXCHAR_BL "m"
#define BOXCHAR_TR "k"
#define BOXCHAR_TL "l"
#define BOXCHAR_VERT "x"
#define BOXCHAR_HOR "q"
static void swap(int* a, int* b);
int bc_printA(char *str);
int bc_box(int x1, int y1, int x2, int y2);
int bc_printbigchar(int *big, int x, int y, enum colors fg, enum colors bg);
int bc_setbigcharpos(int *big, int x, int y, int value);
int bc_getbigcharpos(int *big, int x, int y, int *value);
int bc_bigcharwrite(int fd, int *big, int count);
int bc_bigcharread(int fd, int *big, int need_count, int *count);
#endif
--------libReadKey.a--------
----------------readkey.c----------------
#include "./../include/readkey.h"
/*
tcflag_t c_iflag; // режимы ввода
tcflag_t c_oflag; // режимы вывода
tcflag_t c_cflag; // режимы управления
tcflag_t c_lflag; // режимы локали
cc_t c_cc[NCCS]; // управляющие символы
c_iflag - флаги констант:
*/
int rk_readkey(enum keys *key){
struct termios orig_options;
char buf[16] = {0};
int readNum = 0;
if (tcgetattr(STDIN_FILENO, &orig_options) != 0) return -1;
if (rk_mytermregime(0, 0, 1, 0, 1) != 0) return -1;
readNum = read(STDIN_FILENO, buf, 5);
if (readNum <= 0) return -1;
buf[readNum] = '\0';
if ((strcmp(buf, "l") == 0) | (strcmp(buf, "L") == 0)) {
*key = KEY_l;
} else if ((strcmp(buf, "s") == 0) | (strcmp(buf, "S") == 0)) {
*key = KEY_s;
} else if ((strcmp(buf, "r") == 0) | (strcmp(buf, "R") == 0)) {
*key = KEY_r;
} else if ((strcmp(buf, "t") == 0) | (strcmp(buf, "T") == 0)) {
*key = KEY_t;
} else if ((strcmp(buf, "i") == 0) | (strcmp(buf, "I") == 0)) {
*key = KEY_i;
} else if ((strcmp(buf, "q") == 0) | (strcmp(buf, "Q") == 0)) {
*key = KEY_q;
} else if ((strcmp(buf, "x") == 0) | (strcmp(buf, "X") == 0)) {
*key = KEY_x;
} else if ((strcmp(buf, "d") == 0) | (strcmp(buf, "D") == 0)) {
*key = KEY_d;
} else if ((strcmp(buf, "c") == 0) | (strcmp(buf, "C") == 0)) {
*key = KEY_c;
} else if ((strcmp(buf, "\n")) == 0) {
*key = KEY_enter;
} else if ((strcmp(buf, "\E[15~")) == 0) {
*key = KEY_f5;
} else if ((strcmp(buf, "\E[17~")) == 0) {
*key = KEY_f6;
} else if ((strcmp(buf, "\E[A")) == 0) {
*key = KEY_up;
} else if ((strcmp(buf, "\E[B")) == 0) {
*key = KEY_down;
} else if ((strcmp(buf, "\E[C")) == 0) {
*key = KEY_right;
} else if ((strcmp(buf, "\E[D")) == 0) {
*key = KEY_left;
} else if ((strcmp(buf, "\E")) == 0) {
*key = KEY_esc;
} else {
*key = KEY_other;
}
if (tcsetattr(STDIN_FILENO, TCSANOW, &orig_options) != 0) return -1;
return 0;
}
int rk_mytermsave(void){
struct termios options;
FILE *save = NULL;
if (tcgetattr(STDIN_FILENO, &options) != 0) return -1;
if ((save = fopen("termsettings", "wb")) == NULL) return -1;
fwrite(&options, sizeof(options), 1, save);
fclose(save);
return 0;
}
int rk_mytermrestore(void){
struct termios options;
FILE *data = NULL;;
if ((data = fopen("termsettings", "rb")) == NULL) return -1;
else {
if (fread(&options, sizeof(options), 1, data) > 0) {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &options) != 0) {
fclose(data);
return -1;
} else {
fclose(data);
return -1;
}
}
fclose(data);
}
return 0;
}
int rk_mytermregime(int regime, int vtime, int vmin, int echo, int sigint){
struct termios options;
if (tcgetattr(STDIN_FILENO, &options) != 0) return -1;
if (regime == 1) options.c_lflag |= ICANON;
else if (regime == 0) options.c_lflag &= ~ICANON;
else return -1;
if (regime == 0) {
options.c_cc[VTIME] = vtime;
options.c_cc[VMIN] = vmin;
if (echo == 1) options.c_lflag |= ECHO;
else if (echo == 0) options.c_lflag &= ~ECHO;
else return -1;
if (sigint == 1) options.c_lflag |= ISIG;
else if (sigint == 0) options.c_lflag &= ~ISIG;
else return -1;
}
if (tcsetattr(STDIN_FILENO, TCSANOW, &options) != 0) return -1;
return 0;
}
----------------readkey.h----------------
#ifndef READ_KEY_H
#define READ_KEY_H
#include "./../../simpleComputer/include/sc_register.h"
#include "./../../simpleComputer/include/sc_command.h"
#include "./../../simpleComputer/include/sc_memory.h"
#include "./../../bc/include/bc.h"
#include "./../../consoleSC/include/term.h"
#include <termios.h>
#include <string.h>
enum keys {
KEY_l = 0,
KEY_s,
KEY_r,
KEY_t,
KEY_i,
KEY_x,
KEY_d,
KEY_c,
KEY_q,
KEY_f5,
KEY_f6,
KEY_up,
KEY_down,
KEY_left,
KEY_right,
KEY_enter,
KEY_esc,
KEY_other
};
int rk_readkey(enum keys *key);
int rk_mytermsave(void);
int rk_mytermrestore(void);
int rk_mytermregime(int regime, int vtime, int vmin, int echo, int sigint);
#endif
--------libCPU.a--------
----------------cpu.c----------------
#include "./../include/cpu.h"
extern int accumulator;
extern int instructionPointer;
void CU(){
int command, operand, mem;
if (instructionPointer >= sizeRAM) {
sc_regSet(FLAG_OUTMEM, 1);
sc_regSet(FLAG_INTERRUPT, 1);
refreshGui(instructionPointer);
instructionPointer = 0;
return;
}
sc_memoryGet(instructionPointer, &mem);
if (!(sc_commandDecode(mem, &command, &operand)
== 0)) {
sc_regSet(FLAG_COMMAND, 1);
sc_regSet(FLAG_INTERRUPT, 1);
return;
}
if ((operand < 0) | (operand >= sizeRAM)) {
sc_regSet(FLAG_COMMAND, 1);
sc_regSet(FLAG_INTERRUPT, 1);
return;
}
if ((command >= 0x30) & (command <= 0x33) |
(command >= 0x51) & (command <= 0x54) |
(command >= 0x60) & (command <= 0x70) |
(command >= 0x75) & (command <= 0x76)) {
if (ALU(command, operand) != 0)
sc_regSet(FLAG_INTERRUPT, 1);
} else commandHandler(command, operand);
instructionPointer++;
}
int ALU(int command, int operand){
if ((operand < 0) | (operand >= sizeRAM)) {
sc_regSet(FLAG_OUTMEM,1);
return ERR_WRONG_ADDR;
} else {
if (commandHandler(command, operand) != 0) return -1;
if ((accumulator > 0x3FFF)) {
accumulator &= 0x3FFF;
sc_regSet(FLAG_OVERFLOW, 1);
} else sc_regSet(FLAG_OVERFLOW, 0);
sc_regSet(FLAG_ODD, accumulator & 1);
return 0;
}
}
----------------print.c----------------
#include "./../include/cpu.h"
extern int accumulator;
extern int instructionPointer;
extern int bigChars[];
extern int writeValue;
extern int writeUse;
extern int SCANPRINTRADIX;
static int counterRefresh = 0;
static void printWriteValue(){
int command = 0, opcode = 0, operand = 0;
mt_gotoXY(1, 23);
printLine(3);
mt_gotoXY(1, 23);
command = (writeValue >> 14) & 1;
if (writeUse != 0) {
writeChar(1,"Last write: ");
if (command == 0) {
sc_commandDecode(writeValue, &opcode, &operand);
writeChar(1,"+");
writeInt(1, opcode, 16, 2);
writeInt(1, operand, 16, 2);
} else {
writeChar(1," ");
writeInt(1, writeValue & 0x3FFF, SCANPRINTRADIX, 4);
}
}
}
void printLine(int ctr){
int x = 0, y = 0;
mt_getscreensize(&y, &x);
for (int j = 0; j < ctr; j++) {
for (int i = 0; i < x; i++)
writeChar(1," ");
writeChar(1,"\n");
}
}
void refreshGuiSt(int position){
mt_clrscr();
printBoxes();
printLabels();
printKeys(48, 14);
printPointer();
printAccum();
printOperation(position);
printMemory(2, 2, position);
printFlags(66, 11);
printWriteValue();
mt_gotoXY(1, 23);
}
void refreshGui(int position){
counterRefresh++;
if (counterRefresh % REFRESH == 0) {
refreshGuiSt(position);
counterRefresh = 0;
} else {
printPointer();
printAccum();
printOperation(position);
printMemory(2, 2, position);
printFlags(66, 11);
printWriteValue();
mt_gotoXY(1, 23);
}
}
void printAccum(){
mt_setfgcolor(ACCUMCOLORFG);
mt_gotoXY(68, 2);
writeChar(1, " ");
mt_gotoXY(68, 2);
if (SCANPRINTRADIX == 16)writeInt(1, accumulator & 0x3FFF, SCANPRINTRADIX, 4);
else if (SCANPRINTRADIX == 10) writeInt(1, accumulator & 0x3FFF, SCANPRINTRADIX, 5);
mt_setfgcolor(clr_default);
}
void printBoxes(){
bc_box(1, 1, 61, 12);
bc_box(62, 1, 79, 3);
bc_box(62, 4, 79, 6);
bc_box(62, 7, 79, 9);
bc_box(62, 10, 79, 12);
bc_box(1, 13, 46, 22);
bc_box(47, 13, 79, 22);
}
void printPointer(){
mt_gotoXY(68, 5);
mt_setfgcolor(INSTREGCOLORFG);
writeInt(1, instructionPointer, SCANPRINTRADIX, 4);
mt_setfgcolor(clr_default);
}
void printKeys(int x, int y){
mt_setfgcolor(KEYCOLORFG);
mt_gotoXY(x, y);
writeChar(1, "l - load | c - stop");
mt_gotoXY(x, y + 1);
writeChar(1, "s - save");
mt_gotoXY(x, y + 2);
writeChar(1, "r - run ");
mt_gotoXY(x, y + 3);
writeChar(1, "t - step");
mt_gotoXY(x, y + 4);
writeChar(1, "i - reset");
mt_gotoXY(x, y + 5);
writeChar(1, "F5 - accumulator");
mt_gotoXY(x, y + 6);
writeChar(1, "F6 - instruction Pointer");
mt_gotoXY(x, y + 7);
writeChar(1, "q - quit");
mt_setfgcolor(clr_default);
}
void printLabels(){
mt_setfgcolor(KEYCOLORFG);
mt_gotoXY(30, 1);
writeChar(1, " Memory ");
mt_gotoXY(64, 1);
writeChar(1, " accumulator ");
mt_gotoXY(65, 4);
writeChar(1, " instPointer ");
mt_gotoXY(65, 7);
writeChar(1, " Operation ");
mt_gotoXY(67, 10);
writeChar(1, " Flags ");
mt_gotoXY(48, 13);
writeChar(1, " Keys: ");
mt_gotoXY(2, 22);
writeChar(1, " Input/Output ");
mt_setfgcolor(clr_default);
}
void printOperation(int position){
int mem = 0, command = 0, operand = 0, isCommand;
char c = '+';
if ((position >= sizeRAM) & (position <= 0)) {
sc_regSet(FLAG_OUTMEM, 1);
return;
}
sc_memoryGet(position, &mem);
isCommand = (mem >> 14) & 1;
mt_gotoXY(68, 8);
if ((position >= 0) & (position < sizeRAM)) {
if (isCommand == 0) {
sc_commandDecode(mem, &command, &operand);
writeChar(1, " ");
mt_gotoXY(68, 8);
mt_setfgcolor(COMMANDCOLORFG);
write(1, &c, 1);
writeInt(1, command, 16, 2);
writeChar(1, " : ");
mt_setfgcolor(OPERANDCOLORFG);
writeInt(1, operand, 16, 2);
mt_setfgcolor(clr_default);
} else {
mt_setfgcolor(COMMANDCOLORFG);
writeChar(1, " ");
mt_gotoXY(68, 8);
writeInt(1, mem & 0x3FFF, 16, 4);
mt_setfgcolor(clr_default);
}
}
}
void printFlags(int x, int y){
int reg = 0;
mt_gotoXY(x, y);
sc_regGet(FLAG_OVERFLOW, ®);
if (reg == 1) {
mt_setfgcolor(REGCOLORFG);
writeChar(1, "O");
mt_setfgcolor(clr_default);
} else writeChar(1, " ");
writeChar(1, " ");
sc_regGet(FLAG_COMMAND, ®);
if (reg == 1) {
mt_setfgcolor(REGCOLORFG);
writeChar(1, "E");
mt_setfgcolor(clr_default);
} else writeChar(1, " ");
writeChar(1, " ");
sc_regGet(FLAG_INTERRUPT, ®);
if (reg == 1) {
mt_setfgcolor(REGCOLORFG);
writeChar(1, "T");
mt_setfgcolor(clr_default);
} else writeChar(1, " ");
writeChar(1, " ");
sc_regGet(FLAG_OUTMEM, ®);
if (reg == 1) {
mt_setfgcolor(REGCOLORFG);
writeChar(1, "M");
mt_setfgcolor(clr_default);
} else writeChar(1, " ");
writeChar(1, " ");
sc_regGet(FLAG_DIVISION, ®);
if (reg == 1) {
mt_setfgcolor(REGCOLORFG);
writeChar(1, "Z");
mt_setfgcolor(clr_default);
} else writeChar(1, " ");
}
int printMcell(int *bigchars, int pos){
int command = 0, mem = 0, opcode = 0, operand = 0, indexBigChar = 0;
char str[10] = {0}, c = 0;
if ((pos >= sizeRAM) & (pos < 0)) {
sc_regSet(FLAG_OUTMEM, 1);
return 1;
}
sc_memoryGet(pos, &mem);
command = (mem >> 14) & 1;
mem &= 0x3FFF;
if (command == 0) {
sc_commandDecode(mem, &opcode, &operand);
str[0] = '+';
swriteInt(str + 1, opcode, 16, 2);
swriteInt(str + 3, operand, 16, 2);
} else {
str[0] = ' ';
swriteInt(str + 1, mem, 16, 4);
}
for (int i = 0; i < 5; i++) {
mt_gotoXY(2 + 9 * i, 14);
indexBigChar = (str[i] == '+') ? 0 : (((str[i] >= '0') && (str[i] <= '9'))
? (str[i] - '0' + 1)
: ((str[i] >= 'A') &&
(str[i] <= 'F'))
? (str[i] - 'A' + 11) : -1);
bc_printbigchar(bigchars + (indexBigChar * 2), 2 + i*9, 14, BIGCHARSCOLORFG,
BIGCHARSCOLORBG);
}
return 0;