- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm.lua
More file actions
Latest commit
1024 lines (929 loc) · 36.5 KB
/
Copy pathasm.lua
File metadata and controls
1024 lines (929 loc) · 36.5 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
local_M= {_NAME="asm", number=0}
local_MT= {__index=_M}
localopcodes=require"opcodes"
localop=opcodes.map
localsymbols, len-- needed for parsing
localregnum= {A=0x0, B=0x1, C=0x2, D=0x3, ACC=0x4, RET=0x5,
SS=0xa, SP=0xb, FLG=0xc, SIG=0xd, SEG=0xe, IP=0xf}
localregs= {'A', 'B', 'C', 'D', 'ACC', 'RET', 'SS', 'SP', 'FLG', 'SIG', 'SEG', 'IP'}
-- takes two strings indicating registers, and encodes them into a
-- single free-register byte (taken by some opcodes)
localfunctionreg_encode(a, b)
a=aor'PRM'
b=bor'PRM'
assert(regnum[a] andregnum[b], "Invalid Register passed to reg_encode")
return (regnum[a]*16) +regnum[b]
end
-- trys to figure out the base of a number, and process it accordingly.
localfunctionparsenum(n)
iftype(n) =='number' thenreturnnend-- our work here is done!
assert(type(n) =='string', "Cannot parse non-text")
ifn:sub(1,1) =="$" then-- hexadecimal
returntonumber(n:sub(2), 16)
elseifn:sub(-1,-1) =='h' then-- hexadecimal again
returntonumber(n:sub(1, -2), 16)
elseifn:sub(1,1) =="%" then-- binary
returntonumber(n:sub(2), 2)
elseifn:sub(1,1) =="0" then-- Octal
returntonumber(n, 8)
elseifn:match("'(%\\?.)'") then-- char-literal
returnstring.byte(n:match("'(.)'"))
else-- doesn't seem to be anything special, decimal?
returntonumber(n, 10)
end
end
-- takes a string indicating a potential parameter, and tries to figure
-- out its type value, and directness.
--
-- returns: value_type, pDirect, value
localfunctionparm(p)
assert(p, "Can't check nil parms!")
p=string.match(p, "%S+")
localform, abs, value='unk', not (p:sub(1, 1) =='&'), p:match("\&?(.*)")
p=value
ifp=='null' thenreturn'symbol', abs, 'null' end-- null is always availible, and never set
fori=1,#regsdoform=regs[i] ==pand'register' orform
end
ifform~='register' then
value=parsenum(p)
form='literal'
ifvalue==nilthen
value=symbols[p]
ifvaluethen
returnform, abs, value
else
form='symbol'
value=p
end
end
end
returnform, abs, value
end
-- the encoder functions take a list of raw parameters and parse them
-- according to the expectations of the instruction they represent.
-- having done this, they then attempt to find an opcode that accepts that
-- combination of parms ( generally starting with the best/most specific
-- opcodes, and ending with the most generic) and finally attempt to encode
-- a string representing that combination of opcode(s) and parameters.
--
-- certain encoders represent psudo or meta instructions, which do things
-- like define symbols (LET, LBL) or include data directly in the stream
-- (BYTE, DATA)
--
-- regardless, the encoder ends by returning either the encoded chunk, or
-- false, followed by a message explaining the error.
localencoders= {
NOP=function(a, b, c, d)
-- NOP is the do-nothing instruction. it has only one form.
if (aorborcord) then
returnfalse, "NOP must be properly qualified: 'NOP'" end
returnstring.char(op.NOP)
end;
MOV=function(a, b, c)
-- MOV is the move data instruction.
-- MOV easily has the most forms of any instruction in TMVM/FUASSM.
-- and as a result this is easily the most complicated and
-- error-prone encoder in FUASSM.
ifnot (aandbandnotc) then
returnfalse, "MOV must be properly qualified: 'MOV P1,P2'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
if ((af=='literal' oraf=='symbol') and (bf=='literal' orbf=='symbol')) then
returnfalse, "All move operations must involve a register, sorry." end
ifaf=='register' andbf=='register' then-- free-register form
ifaa==trueandba==truethen-- pure register form
returnstring.char(op.MOV_1dR_2dR, reg_encode(av, bv))
elseifaa==falseandba==falsethen--pure addressed register form
returnstring.char(op.MOV_1iR_2iR, reg_encode(av, bv))
end
end
-- resolved symbols are litteral numbers, thus the literal and symbolic
-- forms are common to eachother, differing mostly in if they still require
-- 'patching', or not.
if (af=='literal' oraf=='symbol') andbf=='register' then
ifaa==trueandba==truethen
ifbv=="A" then
returnstring.char(op.MOV_1dN_2dA, (af=='literal' andtonumber(av)) or0), af=='symbol'
elseifbv=="B" then
returnstring.char(op.MOV_1dN_2dB, (af=='literal' andtonumber(av)) or0), af=='symbol'
else
returnfalse, "No encodings for mixed-mode moves not involving registers A or B"
end
elseifaa==falseandba==truethen-- mixed get form
ifbv=="A" then
returnstring.char(op.MOV_1iN_2dA, (af=='literal' andtonumber(av)) or0), af=='symbol'
elseifbv=="B" then
returnstring.char(op.MOV_1iN_2dB, (af=='literal' andtonumber(av)) or0), af=='symbol'
else
returnfalse, "No encodings for mixed-mode moves not involving registers A or B"
end
end
end
-- as above.
ifaf=='register' and (bf=='literal' orbf=='symbol') then
ifaa==trueandba==falsethen-- mixed push/set form
ifav=="A" then
returnstring.char(op.MOV_1dA_2dN, (bf=='literal' andtonumber(bv)) or0), bf=='symbol'
elseifav=="B" then
returnstring.char(op.MOV_1dB_2dN, (bf=='literal' andtonumber(bv)) or0), bf=='symbol'
else
returnfalse, "No encodings for mixed-mode moves not involving registers A or B"
end
else
returnfalse, "Impossible operation, perhaps you meant P2 to be indirect?"
end
end
end;
LMOV=function(a, b, c, d)
-- the long-move instrution, used to move data too, from, or between
-- different segments. at the moment its only defined in pure-register
-- forms.
ifnot (aandbandc) then
returnfalse, "LMOV must be properly qualified: 'LMOV R1,R2,R3,R4' (thats seg, offset, destseg, destoff)" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
localdf, da, dv=parm(d)
ifnot (af=='register' andbf=='register'
andcf=='register' anddf=='register') then
returnfalse, "LMOV only works with absolute registers" end
ifnot (aaandbaandcaandda) then
returnfalse, "LMOV only works with absolute registers" end
returnstring.char(op.LMOV_1dR_2dR_3dR_4dR, reg_encode(av, bv), reg_encode(cv, dv))
end;
INC=function(a, b, c)
-- the monopramic increment-by-one instruction., aka ADD 1, R, R
-- at the moment its only defined for ACC.
ifnot (aandnot (borc)) then
returnfalse, "INC must be properly qualified: 'INC ACC'" end
localaf, aa, av=parm(a)
ifnot (af=='register' andaaandav=='ACC') then
returnfalse, "INC only supports absolute ACC as a destination" end
returnstring.char(op.INC_1dACC)
end;
DEC=function(a, b, c)
-- the monopramic decrement-by-one instruction., aka SUB 1, R, R
-- at the moment its only defined for ACC.
ifnot (aandnot (borc)) then
returnfalse, "DEC must be properly qualified: 'DEC ACC'" end
localaf, aa, av=parm(a)
ifnot (af=='register' andaaandav=='ACC') then
returnfalse, "DEC only supports absolute ACC as a destination" end
returnstring.char(op.DEC_1dACC)
end;
ADD=function(a, b, c)
-- Addtion instruction, performs basic addition
ifnot (aandbandc) then
returnfalse, "ADD must be properly qualified: 'ADD R1,R2,ACC'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "ADD only works with registers" end
ifnot (aaandba) then
returnfalse, "ADD only works with absolute parms" end
ifnot (cf=='register' andcaand (cv=='ACC' orcv=='RET')) then
returnfalse, "ADD only supports absolute ACC or RET as a destination" end
ifcv=='ACC' then
if (av=="A" orav=="B") and (bv=="A" orbv=="B") then
returnstring.char(op.ADD_1dA_2dB_3dACC)
else-- free-register form
returnstring.char(op.ADD_1dR_2dR_3dACC, reg_encode(av, bv))
end
elseifcv=='RET' then
if (av=="A" orav=="B") and (bv=="A" orbv=="B") then
returnstring.char(op.ADD_1dA_2dB_3dRET)
else-- free-register form
returnstring.char(op.ADD_1dR_2dR_3dRET, reg_encode(av, bv))
end
end
returnfalse, "unhandled ADD form!"
end;
SUB=function(a, b, c)
-- Subtraction instruction, performs basic subtraction
ifnot (aandbandc) then
returnfalse, "SUB must be properly qualified: 'SUB R1,R2,ACC'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "SUB only works with registers" end
ifnot (aaandba) then
returnfalse, "SUB only works with absolute parms" end
ifnot (cf=='register' andcaand (cv=='ACC' orcv=='RET')) then
returnfalse, "SUB only supports absolute ACC or RET as a destination" end
ifcv=='ACC' then
if (av=="A") and (bv=="B") then
returnstring.char(op.SUB_1dA_2dB_3dACC)
else-- free-register form
returnstring.char(op.SUB_1dR_2dR_3dACC, reg_encode(av, bv))
end
elseifcv=='RET' then
if (av=="A") and (bv=="B") then
returnstring.char(op.SUB_1dA_2dB_3dRET)
else-- free-register form
returnstring.char(op.SUB_1dR_2dR_3dRET, reg_encode(av, bv))
end
end
end;
DIV=function(a, b, c)
-- Integral, unsigned, division instruction.
ifnot (aandbandc) then
returnfalse, "DIV must be properly qualified: 'DIV R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "DIV only works with registers" end
ifnot (aaandba) then
returnfalse, "DIV only works with absolute parms" end
ifnot (cf=='register' andcaand (cv=='RET' orcv=='ACC')) then
returnfalse, "DIV only supports absolute RET or ACC as a destination" end
ifcv=='RET' then
returnstring.char(op.DIV_1dR_2dR_3dRET, reg_encode(av, bv))
else-- cv == ACC
returnstring.char(op.DIV_1dR_2dR_3dACC, reg_encode(av, bv))
end
end;
MUL=function(a, b, c)
-- Integral, unsigned, Multiplication instruction.
ifnot (aandbandc) then
returnfalse, "MUL must be properly qualified: 'MUL R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "MUL only works with registers" end
ifnot (aaandba) then
returnfalse, "MUL only works with absolute parms" end
ifnot (cf=='register' andcaand (cv=='RET' orcv=='ACC')) then
returnfalse, "MUL only supports absolute RET or ACC as a destination." end
ifcv=='RET' then
returnstring.char(op.MUL_1dR_2dR_3dRET, reg_encode(av, bv))
else-- cv == ACC
returnstring.char(op.MUL_1dR_2dR_3dACC, reg_encode(av, bv))
end
end;
MOD=function(a, b, c)
-- Integral, unsigned, Modulo (aka, remainder) instruction.
ifnot (aandbandc) then
returnfalse, "MOD must be properly qualified: 'MOD R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "MOD only works with registers" end
ifnot (aaandba) then
returnfalse, "MOD only works with absolute parms" end
ifnot(cf=='register' andcaand (cv=='RET' orcv=='ACC')) then
returnfalse, "MOD only supports absolute RET or ACC as a destination" end
ifcv=='RET' then
returnstring.char(op.MOD_1dR_2dR_3dRET, reg_encode(av, bv))
else-- cv == ACC
returnstring.char(op.MOD_1dR_2dR_3dACC, reg_encode(av, bv))
end
end;
SHW=function(a, b, c)
-- princibly a debugging instruction, show prints the name of a
-- given register, followed by its value (as an unsigned byte)
ifnot (aandnot (borc)) then
returnfalse, "SHW must be properly qualified: 'SHW R'" end
localaf, aa, av=parm(a)
ifnot (af=='register' andaa) then
returnfalse, "SHW only presently works for absolute registers" end
ifav=='A' then
returnstring.char(op.SHW_1dA)
else
returnstring.char(op.SHW_1dR, reg_encode(av))
end
end;
HLT=function(a, b, c)
-- halt instruction, tells the machine to halt
ifnot (not (aorborc)) then
returnfalse, "HLT must be properly qualified: 'HLT'" end
returnstring.char(op.STOP)
end;
SWP=function(a, b, c)
-- swap, exchanges the values of parameters. only defined
-- for registers
ifnot ((aandb) andnotc) then
returnfalse, "SWP must be properly qualified: 'SWP R1,R2'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
ifnot (af=='register' andbf=='register') then
returnfalse, "SWP only works with registers" end
ifnot (aaandba) then
returnfalse, "SWP only works with absolute parms" end
if (av=="A" orav=="B") and (bv=="A" orbv=="B") then
returnstring.char(op.SWP_1dA_2dB)
else-- free-register form
returnstring.char(op.SWP_1dR_2dR, reg_encode(av, bv))
end
returnfalse, "unhandled SWP form!"
end;
LES=function(a, b, c)
-- less-than instruction, compares two values and stores the result
-- in the third
ifnot (aandbandc) then
returnfalse, "LES must be properly qualified: 'LES R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "LES only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "LES only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "LES may only place its result in RET" end
if (av=="A") and (bv=="B") then
returnstring.char(op.LES_1dA_2dB_3dRET)
else-- free-register form
returnfalse, "LES is only currently defined in the form LES .A,.B"
end
returnfalse, "unhandled LES form!"
end;
GTR=function(a, b, c)
-- greater-than instruction, compares two values and stores the
-- result in the third
ifnot (aandbandc) then
returnfalse, "GTR must be properly qualified: 'GTR R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "GTR only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "GTR only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "GTR may only place its result in RET" end
if (av=="A") and (bv=="B") then
returnstring.char(op.GTR_1dA_2dB_3dRET)
else-- free-register form
returnfalse, "GTR is only currently defined in the form GTR .A,.B"
end
end;
EQL=function(a, b, c)
-- equal-to instruction, compares two values and stores the
-- result in the third
ifnot (aandbandc) then
returnfalse, "EQL must be properly qualified: 'EQL R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "EQL only works with registers" end
ifnot (aaandba) then
returnfalse, "EQL only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "EQL may only place its result in RET" end
if (av=="A") and (bv=="B") then
returnstring.char(op.EQL_1dA_2dB_3dRET)
else-- free-register form
returnfalse, "EQL is only currently defined in the form EQL .A,.B"
end
returnfalse, "unhandled EQL form!"
end;
GTE=function(a, b, c)
-- greater-than-or-equal-to instruction, compares two values and
-- stores the result in the third
ifnot (aandbandc) then
returnfalse, "GTE must be properly qualified: 'GTE R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "GTE only works with registers" end
ifnot (aaandba) then
returnfalse, "GTE only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "GTE may only place its result in RET" end
if (av=="A") and (bv=="B") then
returnstring.char(op.SWP_1dA_2dB,op.LES_1dA_2dB_3dRET,op.SWP_1dA_2dB)
elseif (av=="B") and (bv=="A") then
returnstring.char(op.SWP_1dA_2dB,op.LES_1dA_2dB_3dRET,op.SWP_1dA_2dB)
else-- free-register form
returnfalse, "GTE is only currently defined in the forms GTE .A,.B and GTE .B .A"
end
returnfalse, "unhandled GTE form!"
end;
LTE=function(a, b, c)
-- less-than-or-equal-to instruction, compares two values and stores
-- the result in the third
ifnot (aandbandc) then
returnfalse, "LTE must be properly qualified: 'LTE R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "LTE only works with registers" end
ifnot (aaandba) then
returnfalse, "LTE only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "LTE may only place its result in RET" end
if (av=="A") and (bv=="B") then
returnstring.char(op.SWP_1dA_2dB,op.GTR_1dA_2dB_3dRET,op.SWP_1dA_2dB)
elseif (av=="B") and (bv=="A") then
returnstring.char(op.SWP_1dA_2dB,op.GTR_1dA_2dB_3dRET,op.SWP_1dA_2dB)
else-- free-register form
returnfalse, "LTE is only currently defined in the form LTE .A,.B"
end
returnfalse, "unhandled LRT form!"
end;
JNZ=function(a, b, c)
-- Jump-if-not-zero, the conditional jump instruction.
ifnot (aandband (notc)) then
returnfalse, "JNZ must be properly qualified: 'JNZ RET,nn'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
ifnot (aaandav=='RET') then
returnfalse, "JNZ may only test RET at this time." end
ifnot (bf=='literal' orbf=='symbol') then
returnfalse, "Only constant jump targets supported at this time." end
ifnot (ba) then
returnfalse, "JNZ only supports absolute (inline) jump targets at this time." end
ifbf=='literal' then
returnstring.char(op.JNZ_1dRET_2dN, bv)
elseifbf=='symbol' then
returnstring.char(op.JNZ_1dRET_2dN, 0x00), true
end
returnfalse, "unhandled JNZ form!"
end;
JMP=function(a, b, c)
-- jump instruction, transfers control to the given in-segment
-- address
ifnot (aandnot (borc)) then
returnfalse, "JMP must be properly qualified: 'JMP nn'" end
localaf, aa, av=parm(a)
ifaf=='literal' then
returnstring.char(op.JMP_1dN, av)
elseifaf=='symbol' then
returnstring.char(op.JMP_1dN, 0x00), true
elseifaf=='register' then
-- theres no actual free-register JMP, but LJMP with SEG as 1dR is equivilent.
-- (its also the same size)
returnstring.char(op.LJMP_1dR_2dR, reg_encode('SEG', av))
end
returnfalse, "unhandled JMP form!"
end;
LJMP=function(a, b, c)
-- long-jump instruction. transfers control to a given address in a
-- (potentially) different segment.
ifnot (aandbandnotc) then
returnfalse, "LJMP must be properly qualified: 'LJMP R1(seg), R2(offset)'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
ifnot (aaandba) then
returnfalse, "LJMP only supports absolute jump targets at this time." end
ifnot (af=='register' andbf=='register') then
returnfalse, "LJMP only supports register jump targets at this time." end
returnstring.char(op.LJMP_1dR_1dR, reg_encode(av, bv))
end;
LET=function(a, b, c)
-- LET psudo-instruction, defines a sybol as the given value.
ifnot ((aandb) andnotc) then
returnfalse, "LET must be properly qualified: 'LET sym,nn'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
ifnot (aaandba) then
returnfalse, "Values may only be stored in build-time symbols" end
ifnot (af=='symbol') then
returnfalse, "Values may only be stored in valid, unused, symbols" end
ifnot (bf=='literal') then
returnfalse, "only literals, or known symbols may be stored" end
symbols[a] =tonumber(b) or0
return"" -- non-encoding.
end;
LBL=function(a, b, c)
-- Label psudo-instruction, sets the named symbols to the offset and
-- segment of the current location.
ifnot (aandb) then
returnfalse, "LBL must be properly qualified: 'LBL adrsym,optsegsym" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
ifnot (aaandba) then
returnfalse, "Values may only be stored in build-time symbols" end
ifnot (af=='symbol' andbf=='symbol') then
returnfalse, "Values may only be stored in valid, unused, symbols" end
ifavthensymbols[av] =len%256end
ifbvthensymbols[bv] =math.floor(len/256) end
return"" -- non-encoding.
end;
BYTE=function(a, b, c)
-- BYTE psudo-instruction, defines the given symbols as the offset
-- and segment of the current location, and encodes the third parm as
-- a byte.
-- thus, it effectively creates a named data storage position in
-- memory.
--
-- this mess won't work properly when patching values...
ifnot (aandbandc) then
returnfalse, "BYTE must be properly qualified: 'BYTE symbol,symbolseg,initialiser" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (aaandba) then
returnfalse, "Values may only be stored in build-time symbols" end
ifnot (af=='symbol' andbf=='symbol') then
-- HAAAAACK!
ifnot (av== (len%256) andbv==math.floor(len/256)) then
returnfalse, "Values may only be stored in valid, unused, symbols" end
end
ifnot (cf=='literal' orcf=='symbol') then
returnfalse, "Only build-time values may be used as an initialiser" end
ifcf=='literal' then
ifnot (cv== (cv%256)) then
returnfalse, "a byte initialiser must be a positive value between 00..ff (0..255)" end
end
ifavandbvthen
symbols[av] =len%256
symbols[bv] =math.floor(len/256)
end
ifcf=='literal' then
returnstring.char(cv)
else
returnstring.char(00), true
end
end;
MNZ=function(a, b, c)
-- Move-not-zero: the conditional move instruction.
-- operates similarly to JNZ, only it performs a move operation
-- rather than a jump.
ifnot (aandbandc) then
returnfalse, "MNZ must be properly qualified: 'MNZ R1,R2,nn'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register') then
returnfalse, "Can only test registers at this time" end
ifnot (bf=='register') then
returnfalse, "Can only conditionally move from registers at this time." end
ifnot (cf=='literal' orcf=='symbol') then
returnfalse, "Only constant set values supported at this time." end
ifnot (aaandbaandca) then
returnfalse, "MNZ only supports absolute values at this time." end
ifbf=='register' then
returnstring.char(op.MNZ_1dR_2dR_3dN, reg_encode(av, bv), cv)
elseifbf=='symbol' then
returnstring.char(op.MNZ_1dR_2dR_3dN, reg_encode(av, bv), 0x00), true
end
returnfalse, "unhandled MNZ form!"
end;
NOT=function(a, b, c)
-- one's compliment negation instruction
ifnot (aandband (notc)) then
returnfalse, "NOT must be properly qualified: 'NOT R1, R2'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(a)
ifnot (af=='register' andbf=='register') then
returnfalse, "NOT only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "NOT only works with absolute registers" end
returnstring.char(op.NOT_1dR_2dR_1dR, reg_encode(av, bv))
end;
AND=function(a, b, c)
-- bitwise-AND instruction
ifnot (aandbandc) then
returnfalse, "AND must be properly qualified: 'AND R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "AND only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "AND only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "AND may only place its result in RET" end
returnstring.char(op.AND_1dR_2dR_3dRET, reg_encode(av, bv))
end;
OR=function(a, b, c)
-- bitwise-OR instruction
ifnot (aandbandc) then
returnfalse, "OR must be properly qualified: 'OR R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "OR only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "OR only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "OR may only place its result in RET" end
returnstring.char(op.OR_1dR_2dR_3dRET, reg_encode(av, bv))
end;
XOR=function(a, b, c)
-- bitwise-XOR instruction
ifnot (aandbandc) then
returnfalse, "XOR must be properly qualified: 'XOR R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "XOR only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "XOR only works with absolute registers" end
returnstring.char(op.XOR_1dR_2dR_3dRET, reg_encode(av, bv))
end;
SHL=function(a, b, c)
-- Shift left instruction. shifts the bitpattern of the given value a
-- number of positions given in the second parm, and stores the
-- result in the third
ifnot (aandbandc) then
returnfalse, "SHL must be properly qualified: 'SHL R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "SHL only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "SHL only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "SHL may only place its result in RET" end
returnstring.char(op.SHL_1dR_2dR_3dRET, reg_encode(av, bv))
end;
SHR=function(a, b, c)
-- Shift right instruction. shifts the bitpattern of the given value
-- a number of positions given in the second parm, and stores the
-- result in the third
ifnot (aandbandc) then
returnfalse, "SHR must be properly qualified: 'SHR R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "SHR only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "SHR only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "SHR may only place its result in RET" end
returnstring.char(op.SHR_1dR_2dR_3dRET, reg_encode(av, bv))
end;
SRE=function(a, b, c)
-- similar to SHR, save that it extends the sign-bit, thus
-- (generally) preserving the sign of numbers in two's compliment
ifnot (aandbandc) then
returnfalse, "SRE must be properly qualified: 'SRE R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "SRE only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "SRE only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "SHR may only place its result in RET" end
returnstring.char(op.SRE_1dR_2dR_3dRET, reg_encode(av, bv))
end;
ROL=function(a, b, c)
-- Roll Left instruction. Rolls are similar in effect to shifts,
-- however the bits that "fall off" the end are simply replaced at
-- the other end
ifnot (aandbandc) then
returnfalse, "ROL must be properly qualified: 'ROL R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "ROL only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "ROL only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "ROL may only place its result in RET" end
returnstring.char(op.ROL_1dR_2dR_3dRET, reg_encode(av, bv))
end;
ROR=function(a, b, c)
-- Roll Right instruction. Rolls are similar in effect to shifts,
-- however the bits that "fall off" the end are simply replaced at
-- the other end
ifnot (aandbandc) then
returnfalse, "ROR must be properly qualified: 'ROR R1,R2,RET'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
localcf, ca, cv=parm(c)
ifnot (af=='register' andbf=='register') then
returnfalse, "ROR only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "ROR only works with absolute registers" end
ifnot (caandcv=='RET') then
returnfalse, "ROR may only place its result in RET" end
returnstring.char(op.ROR_1dR_2dR_3dRET, reg_encode(av, bv))
end;
IN=function(a, b, c)
-- Device port read instruction. attempts to read a single value
-- from the indicated device port, and places it in the location
-- indicated by the second parm.
ifnot (aandbandnotc) then
returnfalse, "IN must be properly qualified: 'IN R1,R2'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
ifnot (af=='register' andbf=='register') then
returnfalse, "IN only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "IN only works with absolute registers" end
returnstring.char(op.IN_1dR_2dR, reg_encode(av, bv))
end;
OUT=function(a, b, c)
-- Device port write instruction. attempts send a given byte of data
-- to the indicated device port,
ifnot (aandbandnotc) then
returnfalse, "OUT must be properly qualified: 'OUT R1,R2'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
ifnot (af=='register' andbf=='register') then
returnfalse, "OUT only works with absolute registers" end
ifnot (aaandba) then
returnfalse, "OUT only works with absolute registers" end
returnstring.char(op.OUT_1dR_2dR, reg_encode(av, bv))
end;
LOAD=function(a, b)
-- the load instrution, used to load data from an address into a register
ifnot (aandb) then
returnfalse, "LOAD must be properly qualified: 'LOAD iN,dR' or 'LOAD iR,dR'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
ifnot (bf=='register') then
returnfalse, "LOAD only loads an indirect number or register into a direct register"
end
ifaaor (notba) then
returnfalse, "LOAD only loads an indirect number or register into a direct register"
end
ifaf=='register' then
returnstring.char(op.LOAD_1iR_2dR, reg_encode(av, bv))
elseifaf=='number' then
returnstring.char(op.LOAD_1iN_2dR, reg_encode('A', bv), av)
else-- af == 'symbol'
returnstring.char(op.LOAD_1iN_2dR, reg_encode('A', bv), 0x0)
end
end;
STOR=function(a, b)
-- the store instrution, used to store data from a register into an address.
ifnot (aandb) then
returnfalse, "STOR must be properly qualified: 'STOR dR,iN,' or 'LOAD dR,iR'" end
localaf, aa, av=parm(a)
localbf, ba, bv=parm(b)
ifnot (af=='register' and (bf=='register' orbf=='symbol' orbf=='number')) then
returnfalse, "STOR only stores a direct register into an indirect register or number" end
if (notaa) orbathen
returnfalse, "STOR only stores a direct register into an indirect register or number" end
ifbf=='register' then
returnstring.char(op.STOR_1dR_2iR, reg_encode(av, bv))
elseifbf=='number' then
returnstring.char(op.STOR_1dR_2iN, reg_encode(av, 'A'), bv)
else-- bf == 'symbol'
returnstring.char(op.STOR_1dR_2iN, reg_encode(av, 'A'), 0x0)
end
end;
}
-----
-- processes the given string into a pre-parsed chunk, suitable for
-- further parsing.
function_M.scrub(s)
localt= {}
s=s..'\n'
forlinstring.gmatch(s, "[^\n]*\n") do
s=string.match(l, "[^#\";]*")
s=string.match(s, "[\t]*(.*)[\t]*")
t[#t+1]=s
end
assert(t, "scrub is returning nothing")
returnt
end
localscrub=_M.scrub
-----
-- loads the given file and returns a normalised chunk, suitable for
-- further parsing.
function_M.load(fname)
localf, err=io.open(fname, 'rb')
ifnotfthenreturnnil, errend
localt=scrub(f:read('*a'))
f:close()
assert(t, "load is returning nothing")
returnt
end
-----
-- takes a pre-parsed chunk, and returns a fully-assembled chunk as a
-- string, suitable for saving or loading into a machine.
function_M.parse(t, verbose)
assert(t, "Parse has been given nothing")
-- if its raw-text, then scrub it and continue.
iftype(t) =='string' thent=_M.scrub(t) end
localtos=tostring
localchk,p= {}, {}
symbols= {}
-- phase I
-- parse statements into operands and parms
localop, a, b, c, d
localmatch="([&%[]?[%%$*]?[%w_']*[%]]?)[,%s]*(.*)"
fori=1,#tdo
op, a=string.match(t[i], match)
ifathen
a, b=string.match(a, match) end
ifbthen
b, c=string.match(b, match) end
ifcthen
c, d=string.match(c, match) end
ifop=="" thenop=nilend
ifa=="" thena=nilend
ifb=="" thenb=nilend
ifc=="" thenc=nilend
ifd=="" thend=nilend
chk[i]={op=op, a=a, b=b, c=c, d=d}
end
--phase II
-- encode into binary representations
-- we take the prepared statements from the last phase, the first part
-- should be the operator, for which we have a whole bunch of
-- specialised encoder functions written. each encoder function takes
-- the raw parms and attempts to parse and resolve them. if successful
-- it will then attempt to reduce it to a specific binary representation.
-- this can succeed, returning the encoded string, fail, producing false
-- and an error message, or partially succeed, producing a partially
-- encoded chunk, and the value true (indicating that that chunk should
-- be re-processed in the patching phase).
localbin= {}
localop, a, b, c, d, suc, r
len=0
fori=1,#chkdo
op, a, b, c, d=chk[i].op,chk[i].a,chk[i].b,chk[i].c, chk[i].d
ifopthen
locallne=string.format("line %d: %s %s,%s,%s", i, tos(op), tos(a), tos(b), tos(c), tos(d))
ifnotencoders[op] then
localmsg= ("[%s # Unknown instruction.]"):format(lne)
ifverbosethenprint(msg) end
returnfalse, msg
end
ifverbosethenprint(string.format("%04x: %s %s, %s, %s",
len, tos(op), tos(a), tos(b), tos(c))) end
suc, r, patch=pcall(encoders[op], a, b, c, d)
ifsucthen
ifnotrthenreturnfalse,
string.format("[line %d: %s %s,%s,%s # No valid reduction.]",
i, op, tos(a), tos(b), tos(c), tos(d))
end
else
locallne=string.format("line %d: %s %s,%s,%s", i, tos(op), tos(a), tos(b), tos(c), tos(d))
localmsg= ("[%s # %s.]"):format(lne, tostring(r))
ifverbosethenprint(msg) end
returnfalse, msg
end
ifpatchthenp[#p+1] =iend
bin[i] =ror""
len=len+#r
end
chk[i].len=len-- HAAAAACK
ifbin[i] ==nilthenbin[i] ="" end
end
--phase III
-- now (re)do patch points
-- during initial encoding some chunks were (possibly) markede for
-- patching. this means there wasn't enough info at the time to give a
-- proper encoding, so now we give them one last chance to resolve.
-- which is usually enough for properly written code.
locali
forj=1,#pdo
i=p[j]
op, a, b, c, d, len=chk[i].op,chk[i].a,chk[i].b,chk[i].c,chk[i].d,chk[i].len
ifverbosethenprint(string.format("Patching %s %s, %s", tos(op), tos(a), tos(b))) end
len=chk[i].len-- HAAAAACK
r, patch=encoders[op](a, b, c)
ifrandnotpatchthen
bin[i] =r
len=len+#r
else
returnfalse, ("[line %d: %s %s,%s # Failed to patch symbol.]"):format(i, op, tos(a), tos(b))
end
end
-- finally...
-- concatinate the encoded fragments into a single chunk, and return it.
returntable.concat(bin)
end
-- possibly clever shit
ifargandarg[0] and (arg[0]=='asm.lua' orarg[0]=='asm') then
print("Begining")
math.randomseed(os.time())
localfunctiontmpnam() returnstring.format("x%04x", math.random(0xffff+1)-1) end
localinf, outf, err=arg[1], arg[2], ""
assert(inf, "must specify a file to load")
print(string.format("Loading '%s'", inf))
localchk, err=_M.load(inf)
ifnotoutfthenoutf= (string.match(inf, "[^%.]*") ortmpnam())..".crap" end
print(string.format("Writting '%s'", tostring(outf)))
outf, err=io.open(outf, "wb")
assert(outf, err)
chk, err=_M.parse(chk, true)