-
Notifications
You must be signed in to change notification settings - Fork 0
/
asm.js
936 lines (917 loc) · 24.7 KB
/
asm.js
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
"use strict";
function toHexN(n, d) {
var s = n.toString(16);
while (s.length < d) {
s = '0' + s;
}
return s; // .toUpperCase();
}
function toHex2(n) {
return toHexN(n & 0xff, 2);
}
function toHex4(n) {
return toHexN(n & 0xffff, 4);
}
function toHexA(a) {
var s = '0x'; // '$';
var i = 0;
for (i; i < a.length - 1; i++)
s += toHex2(a[i]) + ',0x';
s += toHex2(a[i]);
return s;
}
function toHexE(a,tag,rowlen) {
var s = '__'+tag+'__\n'; // '$';
var i = 0;
for (i; i < a.length - 1; i++) {
if ((i > 0) && (i % rowlen) == 0) s += '\n';
s += toHex2(a[i]);
}
s += toHex2(a[i]) + '\n';
return s;
}
function hextoval(h) {
return parseInt(String(h), 16);
}
function hextobyte(a, b) {
return parseInt(String(a) + String(b), 16);
}
function asm(s) {
var out = [];
var arr = [];
var label = [];
var variableAdress = 0;
var debugVarStart = 0;
var variable = [];
var registers = ["R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"];
function addDebugInformation(n) {
for (var i = 0; i < numberDebugString.length; i++)
if (numberDebugString[i][0] == n) {
numberDebugString[i][2] = out.length;
}
}
function strToNum(s) {
s = s.trim().toUpperCase();
if (s[s.length - 1] == 'H') {
if (!isNaN(parseInt(s, 16)))
return parseInt(s, 16);
}
if (s[s.length - 1] == 'B') {
if (!isNaN(parseInt(s, 2)))
return parseInt(s, 2);
}
if (isNaN(parseInt(s, 10)))
return s;
return parseInt(s, 10) & 0xffff;
}
function pushInt(s) {
s = s.toUpperCase();
var n = strToNum(s);
if (typeof n === 'string') {
out.push(n);
out.push(0x0);
return;
}
out.push(0xff & n);
out.push((0xff00 & n) >> 8);
}
function pushChar(s) {
s = s.toUpperCase();
var n = strToNum(s);
if (typeof n === 'string') {
out.push(n);
return;
}
out.push(0xff & n);
}
function parse(s) {
var tokens = [''];
var thisToken = 0;
for (var i = 0; i <= 10; i++)
tokens[i] = '';
tokens[0] = '';
for (i = 0; i < s.length; i++) {
switch (s[i]) {
case ' ':
if (tokens[thisToken] != '') {
thisToken++;
tokens[thisToken] = '';
}
break;
case '(':
case ')':
case ',':
case ':':
case '+':
if (tokens[thisToken] != '')
thisToken++;
tokens[thisToken] = s[i];
thisToken++;
tokens[thisToken] = '';
break;
case '"':
tokens[thisToken] += s[i++];
while (i < s.length && s[i] != '"')
tokens[thisToken] += s[i++];
tokens[thisToken] += s[i];
break;
default:
tokens[thisToken] += s[i];
}
}
compile(tokens);
}
function getRegister(r) {
return registers.indexOf(r);
}
function dbparse(s) {
var buffer = '';
var c;
for (var i = 2; i < s.length; i++) {
switch (s[i]) {
case ',':
if (buffer.length > 0)
pushChar(buffer);
buffer = '';
break;
case '\'':
i++;
for (i; i < s.length; i++) {
if (s[i] != '\'') {
out.push(s.charCodeAt(i));
} else
break;
}
break;
case '"':
i++;
for (i; i < s.length; i++) {
if (s[i] != '"') {
c = s.charCodeAt(i);
if(c > 127)
c = c - 848;
if(c < 255)
out.push(c);
} else
break;
}
break;
case ' ':
break;
default:
buffer += s[i];
}
}
if (buffer != '') {
pushChar(buffer);
}
}
function dwparse(s) {
var buffer = '';
var n;
for (var i = 2; i < s.length; i++) {
switch (s[i]) {
case ',':
if (buffer.length > 0) {
pushInt(buffer);
}
buffer = '';
break;
case '\'':
i++;
for (i; i < s.length; i++) {
if (s[i] != '\'') {
n = s.charCodeAt(i);
out.push(0xff & n);
out.push((0xff00 & n) >> 8);
} else
break;
}
break;
case '"':
i++;
for (i; i < s.length; i++) {
if (s[i] != '"') {
n = s.charCodeAt(i);
out.push(0xff & n);
out.push((0xff00 & n) >> 8);
} else
break;
}
break;
case ' ':
break;
default:
buffer += s[i];
}
}
if (buffer != '') {
pushInt(buffer);
}
}
function compile(a) {
for (var i = 0; i < a.length; i++){
if(a[i][0] != '"')
a[i] = a[i].toUpperCase();
}
for (var i = 0; i < a.length; i++) {
switch (a[i]) {
case ':':
if (i > 0) {
label.push(a[i - 1]);
label.push(out.length);
}
break;
case 'WORD':
if (a[i + 2] && a[i + 2].toUpperCase() == 'DUP') {
variable.push(a[i - 1], variableAdress);
variableAdress += strToNum(a[i + 1]) * 2;
} else {
variable.push(a[i - 1], variableAdress);
variableAdress += 2;
}
return
case 'BYTE':
if (a[i + 2] && a[i + 2].toUpperCase() == 'DUP') {
variable.push(a[i - 1], variableAdress);
variableAdress += strToNum(a[i + 1]);
} else {
variable.push(a[i - 1], variableAdress);
variableAdress += 1;
}
return
case 'NOP':
out.push(0x00);
out.push(0x00);
return;
case 'LDI':
if (a[i + 3] != '(') { //LDI R,int 01 0R XXXX
out.push(0x01);
out.push(0x00 + getRegister(a[i + 1]));
pushInt(a[i + 3]);
return;
} else {
if (getRegister(a[i + 4]) == -1 && a[i + 5] == '+') { //LDI R,(int+R) 04 RR XXXX
out.push(0x04);
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 6]));
pushInt(a[i + 4]);
return;
} else if (getRegister(a[i + 4]) > -1) {
out.push(0x02); //LDI R,(R) 02 RR
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 4]));
return;
} else {
out.push(0x03); //LDI R,(adr) 03 0R XXXX
out.push(getRegister(a[i + 1]));
pushInt(a[i + 4]);
return;
}
}
break;
case 'LDIAL':
if (getRegister(a[i + 4]) == -1 && a[i + 5] == '+') { //LDIAL R,(int+R*2) 08 RR XXXX
out.push(0x08);
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 6]));
pushInt(a[i + 4]);
} else if (getRegister(a[i + 4]) > -1 && a[i + 5] == '+') { //LDIAL R,(R+R*2) 6R RR
out.push(0x60 + getRegister(a[i + 1]));
out.push((getRegister(a[i + 4]) << 4) + getRegister(a[i + 6]));
return;
}
break;
case 'STI':
if (getRegister(a[i + 2]) == -1 && a[i + 3] == '+') { //STI (adr+R),R 06 RR XXXX
out.push(0x06);
out.push((getRegister(a[i + 4]) << 4) + getRegister(a[i + 7]));
pushInt(a[i + 2]);
return;
} else if (getRegister(a[i + 2]) > -1) {
out.push(0x05); //STI (R),R 05 RR
out.push((getRegister(a[i + 2]) << 4) + getRegister(a[i + 5]));
return;
} else {
out.push(0x06); //STI (adr),R 06 R0 XXXX
out.push(getRegister(a[i + 5]) << 4);
pushInt(a[i + 2]);
return;
}
break;
case 'STIAL':
if (getRegister(a[i + 2]) == -1 && a[i + 3] == '+') { //STIAL (adr+R*2),R 09 RR XXXX
out.push(0x09);
out.push((getRegister(a[i + 4]) << 4) + getRegister(a[i + 7]));
pushInt(a[i + 2]);
} else if (getRegister(a[i + 2]) > -1 && a[i + 3] == '+') { //STIAL (R+R*2),R 7R RR
out.push(0x70 + getRegister(a[i + 2]));
out.push((getRegister(a[i + 4]) << 4) + getRegister(a[i + 7]));
return;
}
break;
case 'LDC':
if (a[i + 3] != '(') { //LDC R,char 1R XX
out.push(0x10 + getRegister(a[i + 1]));
pushChar(a[i + 3]);
return;
} else {
if (getRegister(a[i + 4]) == -1 && a[i + 5] == '+') { //LDC R,(int+R) 30 RR XXXX
out.push(0x30);
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 6]));
pushInt(a[i + 4]);
return;
} else if (getRegister(a[i + 4]) > -1 && a[i + 5] == '+') { //LDC R,(R+R) 2R RR
out.push(0x20 + getRegister(a[i + 1]));
out.push((getRegister(a[i + 4]) << 4) + getRegister(a[i + 6]));
return;
} else if (getRegister(a[i + 4]) > -1) {
out.push(0x20); //LDC R,(R) 20 RR
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 4]));
return;
} else {
out.push(0x31); //LDC R,(adr) 31 0R XXXX
out.push(getRegister(a[i + 1]));
pushInt(a[i + 4]);
return;
}
}
break;
case 'STC':
if (getRegister(a[i + 2]) == -1 && a[i + 3] == '+') { //STC (int+R),R 33 RR XXXX
out.push(0x33);
out.push((getRegister(a[i + 4]) << 4) + getRegister(a[i + 7]));
pushInt(a[i + 2]);
return;
} else if (getRegister(a[i + 2]) > -1 && a[i + 3] == '+') { //STC (R+R),R 4R RR
out.push(0x40 + getRegister(a[i + 2]));
out.push((getRegister(a[i + 4]) << 4) + getRegister(a[i + 7]));
return;
} else if (getRegister(a[i + 2]) > -1) {
out.push(0x40); //STC (R),R 40 RR
out.push((getRegister(a[i + 2]) << 4) + getRegister(a[i + 5]));
return;
} else {
out.push(0x32); // STC (adr),R 32 0R XXXX
out.push(getRegister(a[i + 5]));
pushInt(a[i + 2]);
return;
}
break;
case 'MOV':
out.push(0x07); //MOV R,R 07 RR
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 3]));
return;
case 'PUSH':
if (getRegister(a[i + 1]) > -1) {
out.push(0x82); //PUSH R 82 0R
out.push((getRegister(a[i + 1])));
} else {
out.push(0x84);
out.push(0); //PUSH INT 84 00 XXXX
pushInt(a[i + 1]);
}
return;
case 'POP':
out.push(0x80); //POP R 80 0R
if (getRegister(a[i + 1]) > 0)
out.push(0x00 + getRegister(a[i + 1]));
return;
case 'PUSHN':
out.push(0x83); //PUSHN R 83 0R
out.push(getRegister(a[i + 1]));
return;
case 'POPN':
out.push(0x81); //POPN R 81 0R
out.push(getRegister(a[i + 1]));
return;
case 'MEMSET':
out.push(0x88); //MEMSET R 88 0R
out.push(0x00 + getRegister(a[i + 1]));
return;
case 'MEMCPY':
out.push(0x88); //MEMCPY R 88 1R
out.push(0x10 + getRegister(a[i + 1]));
return;
case 'MEMCONV':
out.push(0x8C); //MEMCONV R 8C 0R
out.push(0x00 + getRegister(a[i + 1]));
return;
case 'JMP':
out.push(0x90); //JMP adr 90 00 XXXX
out.push(0x00);
pushInt(a[i + 1]);
return;
case 'JNZ':
out.push(0x91); //JNZ adr 91 00 XXXX
out.push(0x00);
pushInt(a[i + 1]);
return;
case 'JZ':
out.push(0x92); //JZ adr 92 00 XXXX
out.push(0x00);
pushInt(a[i + 1]);
return;
case 'JNP':
out.push(0x93); //JNP adr 93 00 XXXX
out.push(0x00);
pushInt(a[i + 1]);
return;
case 'JP':
out.push(0x94); //JP adr 94 00 XXXX
out.push(0x00);
pushInt(a[i + 1]);
return;
case 'JNC':
out.push(0x95); //JNC adr 95 00 XXXX
out.push(0x00);
pushInt(a[i + 1]);
return;
case 'JC':
out.push(0x96); //JC adr 96 00 XXXX
out.push(0x00);
pushInt(a[i + 1]);
return;
case 'ADD':
out.push(0xA0); //ADD R,R A0 RR
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 3]));
return;
case 'ADC':
out.push(0xA1); //ADC R,R A1 RR
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 3]));
return;
case 'SUB':
out.push(0xA2); //SUB R,R A2 RR
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 3]));
return;
case 'SBC':
out.push(0xA3); //SBC R,R A3 RR
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 3]));
return;
case 'MUL':
out.push(0xA4); //MUL R,R A4 RR
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 3]));
return;
case 'DIV':
out.push(0xA5); //DIV R,R A5 RR
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 3]));
return;
case 'AND':
out.push(0xA6); //AND R,R A6 RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'OR':
out.push(0xA7); //OR R,R A7 RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'ANDL':
out.push(0xAE); //AND R,R A6 RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'ORL':
out.push(0xAF); //OR R,R A7 RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'XOR':
out.push(0xAA); //XOR R,R AA RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'SHL':
out.push(0xAB); //SHL R,R AB RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'SHR':
out.push(0xAC); //SHR R,R AC RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'RAND':
out.push(0xAD); //RAND R AD 0R
out.push(getRegister(a[i + 1]));
return;
case 'CMP':
if (getRegister(a[i + 3]) > -1) {
out.push(0xC1); //CMP R,R C1 RR
out.push((getRegister(a[i + 1]) << 4) + getRegister(a[i + 3]));
} else {
if (parseInt(a[i + 3]) < 255) {
out.push(0xB0 + getRegister(a[i + 1])); //CMP R,CHR BR XX
pushChar(a[i + 3]);
} else {
out.push(0xC0);
out.push(getRegister(a[i + 1]) << 4); //CMP R,INT C0 R0 XXXX
pushInt(a[i + 3]);
}
}
return;
case 'INC':
if (getRegister(a[i + 1]) > -1) {
if (a[i + 2] == ',') {
out.push(0xA8);
out.push(getRegister(a[i + 1]) + ((strToNum(a[i + 3]) & 0xf) << 4)); //INC R,n A8 nR
} else {
out.push(0xA8);
out.push(getRegister(a[i + 1])); //INC R A8 0R
}
} else {
out.push(0xA8); //INC adr A8 10 XXXX
out.push(0x10);
pushInt(a[i + 1]);
}
return;
case 'DEC':
if (getRegister(a[i + 1]) > -1) {
if (a[i + 2] == ',') {
out.push(0xA9);
out.push(getRegister(a[i + 1]) + ((strToNum(a[i + 3]) & 0xf) << 4)); //DEC R,n A9 nR
} else {
out.push(0xA9);
out.push(getRegister(a[i + 1])); //DEC R A9 0R
}
} else {
out.push(0xA9); //DEC adr A9 10 XXXX
out.push(0x10);
pushInt(a[i + 1]);
}
return;
case 'SQRT':
out.push(0xAD); //SQRT R AD 1R
out.push(0x10 + (getRegister(a[i + 1]) & 0xf));
return;
case 'COS':
out.push(0xAD); //COS R AD 2R
out.push(0x20 + (getRegister(a[i + 1]) & 0xf));
return;
case 'SIN':
out.push(0xAD); //SIN R AD 3R
out.push(0x30 + (getRegister(a[i + 1]) & 0xf));
return;
case 'ABS':
out.push(0xAD); //ABS R AD 4R
out.push(0x40 + (getRegister(a[i + 1]) & 0xf));
return;
case 'NOTL':
out.push(0xAD); //NOTL R AD 5R
out.push(0x50 + (getRegister(a[i + 1]) & 0xf));
return;
case 'NOT':
out.push(0xAD); //NOT R AD 6R
out.push(0x60 + (getRegister(a[i + 1]) & 0xf));
return;
case 'CLS':
out.push(0xD0); // CLS D000
out.push(0x00);
return;
case 'DRECT':
out.push(0xD0); // DRECT D0 1R
out.push(0x10 + getRegister(a[i + 1]));
return;
case 'FRECT':
out.push(0xD0); // FRECT D0 2R
out.push(0x20 + getRegister(a[i + 1]));
return;
case 'DCIRC':
out.push(0xD0); // DCIRC D0 3R
out.push(0x30 + getRegister(a[i + 1]));
return;
case 'FCIRC':
out.push(0xD0); // FCIRC D0 4R
out.push(0x40 + getRegister(a[i + 1]));
return;
case 'PUTC':
out.push(0xD1); // PUTC R D10R
out.push(getRegister(a[i + 1]));
return;
case 'PUTS':
out.push(0xD1); // PUTS R D11R
out.push(0x10 + getRegister(a[i + 1]));
return;
case 'PUTN':
out.push(0xD1); // PUTN R D12R
out.push(0x20 + getRegister(a[i + 1]));
return;
case 'PUTRES':
out.push(0xD1); // PUTRES R D1BR
out.push(0xB0 + strToNum(a[i + 1]));
return;
case 'SETX':
out.push(0xD1); // SETX R D13R
out.push(0x30 + getRegister(a[i + 1]));
return;
case 'SETY':
out.push(0xD1); // SETY R D14R
out.push(0x40 + getRegister(a[i + 1]));
return;
case 'DRWADR':
out.push(0xD1); //DRWADR R D1AR
out.push(0xA0 + getRegister(a[i + 1]));
return;
case 'RSTDAD':
out.push(0xD1); // RSTDAD D1A0
out.push(0xA0);
return;
case 'CLIP':
out.push(0xD1); // CLIP R D15R
out.push(0x50 + getRegister(a[i + 1]));
return;
case 'GETK':
out.push(0xD2); // GETK R D20R
out.push(0x00 + getRegister(a[i + 1]));
return;
case 'GETJ':
out.push(0xD2); // GETJ R D21R
out.push(0x10 + getRegister(a[i + 1]));
return;
case 'BTN':
out.push(0xD2); // BTN R D22R
out.push(0x20 + getRegister(a[i + 1]));
return;
case 'BTNP':
out.push(0xD2); // BTNP R D23R
out.push(0x30 + getRegister(a[i + 1]));
return;
case 'PPIX':
out.push(0xD3); // PPIX R,R D3RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'DRWIM':
out.push(0xD4); // DRWIM R D40R
out.push(0x00 + getRegister(a[i + 1]));
return;
case 'SFCLR':
out.push(0xD4); // SFCLR R D41R
out.push(0x10 + getRegister(a[i + 1]));
return;
case 'SBCLR':
out.push(0xD4); // SBCLR R D42R
out.push(0x20 + getRegister(a[i + 1]));
return;
case 'GFCLR':
out.push(0xD4); // GFCLR R D43R
out.push(0x30 + getRegister(a[i + 1]));
return;
case 'GBCLR':
out.push(0xD4); // GBCLR R D44R
out.push(0x40 + getRegister(a[i + 1]));
return;
case 'IMOPTS':
out.push(0xD4); // IMOPTS D45R
out.push(0x50 + getRegister(a[i + 1]));
return;
case 'DLINE':
out.push(0xD4); // DLINE D46R
out.push(0x60 + getRegister(a[i + 1]));
return;
case 'DRWSPR':
out.push(0xD4); // DRWSPR R D47R
out.push(0x70 + getRegister(a[i + 1]));
return;
case 'SMAPXY':
out.push(0xD4); // STILE R D48R
out.push(0x80 + getRegister(a[i + 1]));
return;
case 'ACTDS':
out.push(0xD4); // ACTDS R*2 D49R
out.push(0x90 + getRegister(a[i + 1]));
return;
case 'DRWBIT':
out.push(0xD4); // DRWBIT R D4AR
out.push(0xA0 + getRegister(a[i + 1]));
return;
case 'DRWMAP':
out.push(0xD4); // DRWMAP R D4BR
out.push(0xB0 + getRegister(a[i + 1]));
return;
case 'MVACT':
out.push(0xD4); // MVACT R D4CR
out.push(0xC0 + getRegister(a[i + 1]));
return;
case 'DRWACT':
out.push(0xD4); // DRWACT R D4DR
out.push(0xD0 + getRegister(a[i + 1]));
return;
case 'TACTC':
out.push(0xD4); // TACTC D4E0
out.push(0xE0);
return;
case 'TACTM':
out.push(0xD4); // TACTM R D4FR
out.push(0xF0 + getRegister(a[i + 1]));
return;
case 'FSET':
out.push(0xD5); // FSET R,R D5RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'FGET':
out.push(0xD5); // FGET R D50R
out.push(0x00 + (getRegister(a[i + 1])));
return;
case 'RPALET':
out.push(0xD6); // RPALET R,R D600
out.push(0x00);
return;
case 'PALT':
out.push(0xD6); // PALT R D60R
out.push(0x00 + getRegister(a[i + 1]));
return;
case 'SPALET':
out.push(0xD6); // SPALET R,R D6RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'SPART':
out.push(0xD7); // SPART R D7 0R
out.push(0x00 + getRegister(a[i + 1]));
return;
case 'SEMIT':
out.push(0xD7); // SEMIT R D7 1R
out.push(0x10 + getRegister(a[i + 1]));
return;
case 'DPART':
out.push(0xD7); // DPART R D7 2R
out.push(0x20 + getRegister(a[i + 1]));
return;
case 'DISTSS':
out.push(0xD7); // DISTSS D7 3R
out.push(0x30 + getRegister(a[i + 1]));
return;
case 'DISTSP':
out.push(0xD7); // DISTSP D7 4R
out.push(0x40 + getRegister(a[i + 1]));
return;
case 'DISTPP':
out.push(0xD7); // DISTPP D7 5R
out.push(0x50 + getRegister(a[i + 1]));
return;
case 'APART':
out.push(0xD7); // APART D7 60
out.push(0x60);
return;
case 'MPARTC':
out.push(0xD7); // MPARTC D7 7R
out.push(0x70 + getRegister(a[i + 1]));
return;
case 'SGET':
out.push(0xD8); // SGET R,R D8RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'GETPIX':
out.push(0xD9); // GETPIX R,R D9RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'SSET':
out.push(0xDA); // SSET R,R DARR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'GACTXY':
out.push(0xDB); // GACTXY R,R DB RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'ACTGET':
out.push(0xDC); // ACTGET R,X DC RX
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'AGBACT':
out.push(0xDE); // AGBSPR R,R DE RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'GMAPXY':
out.push(0xDF); // GTILEXY R,R DF RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'LOADRT':
out.push(0xE1); // LOADRT R,R E1RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'PLAYRT':
out.push(0xE0); // PLAYRT E000
out.push(0x00);
return;
case 'PAUSERT':
out.push(0xE0); // PAUSERT E001
out.push(0x01);
return;
case 'STOPRT':
out.push(0xE0); // STOPRT E002
out.push(0x02);
return;
case 'PLAYTN':
out.push(0xE2); // PLAYTN R,R E2RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'ACTSET':
out.push(0xF0 + getRegister(a[i + 1])); // ACTSET R,R,R FRRR
out.push((getRegister(a[i + 3]) << 4) + (getRegister(a[i + 5])));
return;
case 'CALL':
if (a[i + 1] != '(') {
// CALL adr 99 00 XXXX
out.push(0x99);
out.push(0x00);
pushInt(a[i + 1]);
} else if (getRegister(a[i + 2]) == -1 && a[i + 3] == '+') {
// CALL (adr+R) 99 2R XXXX
out.push(0x99);
out.push(0x20 + getRegister(a[i + 4]));
pushInt(a[i + 2]);
} else {
// CALL (adr) 99 10 XXXX
out.push(0x99);
out.push(0x10);
pushInt(a[i + 2]);
}
return;
case 'RET':
out.push(0x9A);
out.push(0x00);
return;
case 'HLT':
out.push(0x50);
out.push(0x00);
return;
case 'FLIP':
out.push(0x50);
out.push(0x50);
return;
case 'STIMER':
out.push(0x51); // STIMER R,R 51RR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'GTIMER':
out.push(0x52); // GTIMER R 520R
out.push(getRegister(a[i + 1]));
return;
case 'ESPICO':
out.push(0x55); // ESPICO X,R 55XR
out.push((strToNum(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
case 'DB':
dbparse(a.join(''));
return;
case 'DW':
dwparse(a.join(''));
return;
case 'LDF': //load flag
out.push(0xC2);
out.push((getRegister(a[i + 1]) << 4) + (strToNum(a[i + 3]) & 0xf));
return;
case 'LDRES': //load result
out.push(0xC3);
out.push(((strToNum(a[i + 1])) << 4) + (getRegister(a[i + 3])));
return;
case 'MULRES': //multiply result
out.push(0xC4);
out.push(((strToNum(a[i + 1])) << 4) + (getRegister(a[i + 3])));
return;
case 'DIVRES': //divide result
out.push(0xC5);
out.push(((strToNum(a[i + 1])) << 4) + (getRegister(a[i + 3])));
return;
case 'ATAN2':
out.push(0xCA); // ATAN2 R,R CARR
out.push((getRegister(a[i + 1]) << 4) + (getRegister(a[i + 3])));
return;
}
}
}
s = s.replace(/;.*/g, ""); //remove comment
s = s.replace(/'(.)'/g, function (a, b) {
return b.charCodeAt(0)
}); //replace single char
arr = s.split('\n');
for (var i = 0; i < arr.length; i++) {
addDebugInformation(i);
parse(arr[i]);
}
variable.push('#END', variableAdress);//переменная указывает на конец используемой глобальными переменными памяти
debugVarStart = out.length;
for (var i = 0; i < out.length; i++) {
if (typeof out[i] === 'string') {
var n;
var nparts = out[i].split('.');
if (label.indexOf(out[i]) > -1) {
n = label[label.indexOf(nparts[0]) + 1];
if (nparts[1] != null) n += Number(nparts[1]);
out[i] = n & 0xff;
i++;
out[i] = (n & 0xff00) >> 8;
} else {
n = variable[variable.indexOf(nparts[0]) + 1];
if (nparts[1] != null) n += Number(nparts[1]);
n += out.length;
out[i] = n & 0xff;
i++;
out[i] = (n & 0xff00) >> 8;
}
}
}
info("program size "+ out.length +" bytes");
info("variables occupy "+ variableAdress +" bytes");
info("total occupied memory " + (out.length + variableAdress) +" bytes");
display.reset();
cpu.init();
cpu.load(out);
debugVar = [];
for (var i = 0; i < variable.length; i++) {
debugVar.push({
variable: variable[i++],
adress: (variable[i] + debugVarStart)
});
}
return out;
}