-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.lua
1122 lines (944 loc) · 27.4 KB
/
machine.lua
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 bitfield = require "bitfield"
local device = require "device"
local util = require 'util'
local socket = desire 'socket'
local opcodes = require 'opcodes'
local _M = {_NAME="machine", number=0}
local _MT = {__index=_M}
-------------------------------------------------------------------------
-- KEY VALUES
local maxmem = 256 -- maximum bytes in a memory segment
local maxport = 256 -- maximum number of device ports
local maxsegments = 256 -- maximum number of memory segments
-- speeds in cycles-per-second (hertz)
local HERTZ = 1
local KILOHERTZ = 1000*HERTZ
local MEGAHERTZ = 1000*KILOHERTZ
local GIGAHERTZ = 1000*MEGAHERTZ -- who would ever need such a ridiculous figure?
-------------------------------------------------------------------------
-- SIGNALS
local SIG_NONE = 0x00 -- Nothing doing.
local SIG_DIV0 = 0x01 -- Divide by zero
local SIG_ILLEGAL_INSTRUCTION = 0x02 -- Illegal Instruction (aka, unknown opcode)
local SIG_DEVICE_NOT_READY = 0x03 -- attempt to use a device or port that is not in a valid state.
local SIG_DOUBLE_FAULT = 0x0f -- Double-fault (eg, fault before clearing a fault)
local SIG_TRIPLE_FAULT = 0xff -- Triple (halting) fault.
-- signal number -> freindly name mappings
local signals = {
[SIG_NONE] = "Signal Normal";
[SIG_DIV0] = "Divide by zero";
[SIG_ILLEGAL_INSTRUCTION] = "Illegal Instruction";
[SIG_DEVICE_NOT_READY] = "Device Not Ready";
[SIG_DOUBLE_FAULT] = "Double Fault";
[SIG_TRIPLE_FAULT] = "Triple Fault (halting fault)";
}
-------------------------------------------------------------------------
-- FLAGS
local CF = 1 -- carry flag
local SF = 2 -- sign (negative) flag
local OF = 3 -- overflow/underflow flag
local ZF = 4 -- zero flag
local PF = 5 -- parity flag
--local U1 = 6 -- unused
--local U2 = 7 -- unused
--local U3 = 8 -- unused
-- nybble parity (odd) mappings
local parity = {
[0x0] = true; [0x1] = false; -- 0000 0001
[0x2] = false; [0x3] = true; -- 0010 0011
[0x4] = false; [0x5] = true; -- 0100 0101
[0x6] = true; [0x7] = false; -- 0110 0111
[0x8] = false; [0x9] = true; -- 1000 1001
[0xa] = true; [0xb] = false; -- 1010 1011
[0xc] = true; [0xd] = false; -- 1100 1101
[0xe] = false; [0xf] = true; -- 1110 1111
}
-------------------------------------------------------------------------
-- MISC
-- number to register mappings
local cr_tbl = {[0x0]='A', [0x1]='B', [0x2]='C', [0x3]='D', [0x4]='ACC', [0x5]='RET',
[0xa]='SS', [0xb]='SP', [0xc]='FLG',[0xd]='SIG', [0xe]='SEG', [0xf]='IP' }
-------------------------------------------------------------------------
-- private helper functions
-- printf, as per C
local function printf(...)
print(string.format(...))
end
-- a variant of io.write that include printf-like formatting.
local function writef(...)
io.write(string.format(...))
end
-- non-erroring assert-like function.
local function alert(c, m)
if not c then print(m) end
end
-- takes a formatted free-register byte,and returns a pair of register names
local function convreg(self, n)
local mf=math.floor
local a, b = mf(n/16), mf(n%16)
a, b =cr_tbl[a], cr_tbl[b]
if not (a and b) then
self:signal(SIG_ILLEGAL_INSTRUCTION)
alert(a, ("Illegal High Register in byte: %02x"):format(n))
alert(b, ("Illegal Low Register in byte: %02x"):format(n))
end
return a, b
end
-- a probably flawed rounding function.
local function round(n)
if n > 0 then return math.floor(n)
else return math.ceil(n) end
end
-- converts a string of bytes into the standard byte-tables used elsewhere
local function stringtodata(str)
local t = {}
for i=1,#str do
t[i] = str:sub(i, i):byte()
end
return t
end
local wait
-- a coarse wait/"sleep" function.
-- involves busy-waiting
if not socket then -- if socket isn't availible, we fall back to a busy-wait.
function wait(n)
local clk = os.clock
local start, dt = clk()
repeat dt = clk()-start until dt >= n
return dt
end
else -- but use sleep if we can.
function wait(n)
local clk = os.clock
local t = socket.sleep(n) -- ISSUE: seems like sleep does not return the time that passed...
if t then
return t
else
return os.clock()-clk
end
end
end
-- converts an seg-offset pair into a linear address.
local function adrshift(m, adr, seg)
assert(m and adr, "adrshift requires a segment (or machine), and an address")
seg = (type(m)=='number' and m) or (seg or m.SEG)
return adr+(seg*256)
end
-- returns the value stored in a given segment and offset
local function adrget(m, adr, seg)
assert(m and adr, "adrget requires a segment (or machine), and an address")
seg = seg or m.SEG
return m.memory[adr+(seg*256)]
end
-- stores a value at a given segment and offset.
local function adrset(m, adr, value, seg)
assert(m and adr, "adrset requires a segment (or machine), and an address")
assert(value, "adrset requires a value")
seg = seg or m.SEG
m.memory[adr+(seg*256)] = value
end
-- sets a given CPU flag bit.
local function setflag(self, flag, bool)
self.FLG = bitfield.SET(self.FLG, flag, bool)
return self.FLG
end
-- Gets a given CPU flag bit.
local function getflag(self, flag, bool)
return bitfield.GET(self.FLG, flag)
end
-- based on given unormalised value, sets and resets the flags likely
-- to have been effected by the operations leading to that value.
local function doflags(self, value)
if value == 0 then
setflag(self, CF, false) -- carry/barrow
setflag(self, SF, false) -- sign (negative)
setflag(self, OF, false) -- overflow
setflag(self, ZF, true) -- zero
setflag(self, PF, true) -- parity
else
setflag(self, ZF, false) -- zero
end
if value ~= (value % 256) then
setflag(self, OF, true) -- overflow
setflag(self, CF, true) -- carry/barrow
else
setflag(self, OF, false) -- overflow
setflag(self, CF, false) -- carry/barrow
end
if value < 0x00 then
setflag(self, SF, true) -- sign (negative)
setflag(self, CF, true) -- carry/barrow
else
setflag(self, SF, false) -- sign (negative)
setflag(self, CF, true) -- carry/barrow
end
do -- Parity bit assignment
local a = math.floor(value % 16)
local b = math.floor(value / 16)
a = parity[a]
b = parity[b]
setflag(self, PF, (a or b) and not (a and b)) -- XOR, for those of you following along at home
end
return value
end
-- does nothing.
local function nop() end
-------------------------------------------------------------------------
-- opcode Function
-- the actual functions that do the heavy-lifting.
local ins = {}
--generals...
local function store(self, reg, seg, adr)
assert(reg, "must be given a valid register")
assert(type(reg) == 'string', "must be given a valid register name")
assert(seg and adr, "must be given a valid address")
adrset(self, adr, self[reg], seg)
end
local function load(self, reg, seg, adr)
assert(reg, "must be given a valid register")
assert(type(reg) == 'string', "must be given a valid register name")
assert(seg and adr, "must be given a valid address")
self[reg] = adrget(self, adr, seg)
end
-- specifics...
-- NOP
-- No OPeration, do nothing, etc.
function ins.NOP(self)
return 1;
end;
-- INC A
-- Fixed register Increment
function ins.INC_1dA(self)
self.A = doflags(self,(self.A+1)) % 256
return 1
end;
-- INC B
-- Fixed register Increment
function ins.INC_1dB(self)
self.B = doflags(self,(self.B+1)) % 256
return 1
end;
-- INC C
-- Fixed register Increment
function ins.INC_1dC(self)
self.C = doflags(self,(self.C+1)) % 256
return 1
end;
-- INC D
-- Fixed register Increment
function ins.INC_1dD(self)
self.D = doflags(self,(self.D+1)) % 256
return 1
end;
-- INC ACC
-- Fixed register Increment
function ins.INC_1dACC(self)
self.ACC = doflags(self,(self.ACC+1)) % 256
return 1
end;
-- DEC A
-- Fixed register Decrement
function ins.DEC_1dACC(self)
self.A = doflags(self,(self.A-1)) % 256
return 1
end;
-- DEC B
-- Fixed register Decrement
function ins.DEC_1dACC(self)
self.B = doflags(self,(self.B-1)) % 256
return 1
end;
-- DEC C
-- Fixed register Decrement
function ins.DEC_1dC(self)
self.C = doflags(self,(self.C-1)) % 256
return 1
end;
-- DEC D
-- Fixed register Decrement
function ins.DEC_1dD(self)
self.D = doflags(self,(self.D-1)) % 256
return 1
end;
-- DEC ACC
-- Fixed register Decrement
function ins.DEC_1dACC(self)
self.ACC = doflags(self,(self.ACC-1)) % 256
return 1
end;
-- MOV &R:&R
-- indirect free-register move
function ins.MOV_1iR_2iR(self)
local a, b = convreg(self, adrget(self, self.IP+1))
adrset(self, self[b], adrget(self, self[a]))
return 2;
end;
-- MOV .A:nn
-- Move A to addresss.
function ins.MOV_1dA_2dN(self)
store(self, 'A', self.SEG, adrget(self, self.IP+1))
return 2;
end;
-- MOV .B:nn
-- Move B to addresss.
function ins.MOV_1dB_2dN(self)
store(self, 'B', self.SEG, adrget(self, self.IP+1))
return 2;
end;
-- MOV .C:nn
-- Move C to addresss.
function ins.MOV_1dC_2dN(self)
store(self, 'C', self.SEG, adrget(self, self.IP+1))
return 2;
end;
-- MOV .D:nn
-- Move D to addresss.
function ins.MOV_1dD_2dN(self)
store(self, 'D', self.SEG, adrget(self, self.IP+1))
return 2;
end;
-- MOV &nn:.A
-- Put address contents into A
function ins.MOV_1iN_2dA(self)
local point = adrget(self, self.IP+1)
self.A = adrget(self, point)
return 2;
end;
-- MOV nn:.A
-- put literal into A
function ins.MOV_1dN_2dA(self)
self.A = adrget(self, self.IP+1)
return 2;
end;
-- MOV nn, B
-- Fixed-register literal MOV to B
function ins.MOV_1dN_2dB(self)
self.B = adrget(self, self.IP+1)
return 2;
end;
-- MOV R:R
-- free-register move
function ins.MOV_1dR_2dR(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self[b] = self[a]
return 2;
end;
-- MOV &nn, B
-- Fixed-register indirect MOV to B
function ins.MOV_1iN_2dB(self)
self.B = adrget(self, adrget(self, self.IP+1))
return 2;
end;
-- MOV B, &nn
-- Fixed-register indirect MOV from B
function ins.MOV_1dB_2iN(self)
adrset(self,adrget(self, self.IP+1), self.B)
return 2;
end;
-- ADD .A:.B -> .RET
-- fixed-register AB ADDition, results in RET
function ins.ADD_1dA_2dB_3dRET(self)
local r = (self.A + self.B)
self.RET = doflags(self, r) % 256
return 1;
end;
-- ADD .A:.B -> .ACC
-- fixed-register AB ADDition, results in ACC
function ins.ADD_1dA_2dB_3dACC(self)
local r = (self.A + self.B)
self.ACC = doflags(self, r) % 256
return 1;
end;
-- ADD R:R -> .ACC
-- free-register ADDition, results in ACC
function ins.ADD_1dR_2dR_3dACC(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local r = (self[a] + self[b])
self.ACC = doflags(self, r) % 256
return 2;
end;
-- ADD R:R -> .RET
-- free-register ADDition, results in ACC
function ins.ADD_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local r = (self[a] + self[b])
self.RET = doflags(self, r) % 256
return 2;
end;
-- SWP R:R
-- Free-register swap
function ins.SWP_1dR_2dR(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self[a], self[b] = self[b], self[a]
return 2;
end;
-- MNZ R, R, nn
-- free-register conditional move to literal address
function ins.MNZ_1dR_2dR_3dN(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local c = adrget(self, self.IP+2)
if self[a] ~= 0 then
adrset(self, c, self[b])
end
return 3;
end;
-- SWP .A:.B (or SWP .B:.A ...)
-- fixed register AB swap
function ins.SWP_1dA_2dB(self)
self.A, self.B = self.B, self.A
return 1;
end;
-- ADD .A:.B -> .ACC
-- fixed-register AB ADDition, results in ACC
function ins.ADD_1dA_2dB(self)
local r = (self.A + self.B)
self.ACC = doflags(self, r) % 256
return 1;
end;
-- SHW .A
-- fixed register Show A
function ins.SHW_1dA(self)
print("A: "..self.A)
return 1;
end;
-- SHW R
-- Free-register show
function ins.SHW_1dR(self)
local a, b = convreg(self, adrget(self, self.IP+1))
print(a..": "..self[a])
return 2;
end;
-- JMP nn (or MOV nn:.IP
-- unconditional jump with literal address
function ins.JMP_1dN(self)
self.IP = adrget(self, self.IP+1)
return 0;
end;
-- JNZ .RET, nn (or MNZ .RET, nn:.IP)
-- fixed-register RET conditional jump with literal address
function ins.JNZ_1dRET_2dN(self)
if self.RET ~= 0 then
self.IP = adrget(self, self.IP+1)
return 0
end
return 2;
end;
-- NOT R -> R
-- free-register Bitwise NOT in:out
function ins.NOT_1dR_2dR_1dR(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self[b] = bitfield:new(self[a], 8):NOT()
return 2;
end;
-- AND R:R -> .RET
-- free-register bitwise AND
function ins.AND_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self.RET = bitfield:new(self[a], 8):AND(self[b])
return 2;
end;
-- OR R:R -> .RET
-- free-register bitwise OR
function ins.OR_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self.RET = bitfield:new(self[a], 8):OR(self[b])
return 2;
end;
-- XOR R:R -> .RET
-- free-register bitwise XOR
function ins.XOR_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self.RET = bitfield:new(self[a], 8):XOR(self[b])
return 2;
end;
-- SHL R:R -> .RET
-- free-register shift left
function ins.SHL_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self.RET = bitfield:new(self[a], 8):shift(self[b])
return 2;
end;
-- SHR R:R -> .RET
-- free-register shift right
function ins.SHR_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self.RET = bitfield:new(self[a], 8):shift(-self[b])
return 2;
end;
-- SRE R:R -> .RET
-- free-register shift right w/ sign extension
function ins.SRE_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self.RET = bitfield:new(self[a], 8):shift(-self[b], true)
return 2;
end;
-- ROL R:R -> .RET
-- free-register roll left
function ins.ROL_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self.RET = bitfield:new(self[a], 8):roll(self[b])
return 2;
end;
-- ROR R:R -> .RET
-- free-register roll right
function ins.ROR_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self.RET = bitfield:new(self[a], 8):roll(-self[b])
return 2;
end;
-- IN R1 -> R2
-- free-register I/O port read
function ins.IN_1dR_2dR(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local ret, err = self:deviceRead(self[a])
if not ret then
self:signal(SIG_DEVICE_NOT_READY)
else
self[b] = ret
end
return 2;
end;
-- OUT R1, R2
-- free-register I/O port write
function ins.OUT_1dR_2dR(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local ret, err = self:deviceWrite(self[a], self[b])
if not ret then
self:signal(SIG_DEVICE_NOT_READY)
end
return 2;
end;
-- MUL R, R -> RET
-- Free-register Multiply
function ins.MUL_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
if a == 'PRM' or b == 'PRM' then
self:signal(SIG_ILLEGAL_INSTRUCTION) end
local r = round(self[a] * self[b])
self.RET = doflags(self, r) % 256
return 2;
end;
-- SUB .A:.B -> .ACC
-- fixed-register AB ADDition, results in ACC
function ins.SUB_1dA_2dB_3dACC(self)
local r = (self.A - self.B)
self.ACC = doflags(self, r) % 256
return 1;
end;
-- SUB R1:.R2 -> .ACC
-- free-register ADDition, results in ACC
function ins.SUB_1dR_2dR_3dACC(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local r = (self[a] - self[b])
self.ACC = doflags(self, r) % 256
return 2;
end;
-- SUB .A:.B -> .RET
-- fixed-register AB ADDition, results in ACC
function ins.SUB_1dA_2dB_3dRET(self)
local r = (self.A - self.B)
self.ACC = doflags(self, r) % 256
return 1;
end;
-- SUB R1:.R2 -> .RET
-- free-register SUBition, results in RET
function ins.SUB_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local r = (self[a] - self[b])
self.RET = doflags(self, r) % 256
return 2;
end;
-- MOD R, R -> RET
-- Free-register Modulo
function ins.MOD_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local r =round(self[a] % self[b])
self.RET = doflags(self, r) % 256
return 2;
end;
-- MOD R, R -> ACC
-- Free-register Modulo
function ins.MOD_1dR_2dR_3dACC(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local r = round(self[a] % self[b])
self.ACC = doflags(self, r) % 256
return 2;
end;
-- DIV R, R -> ACC
-- Free-register divide
function ins.DIV_1dR_2dR_3dACC(self)
local a, b = convreg(self, adrget(self, self.IP+1))
if self.b == 0 then self:signal(SIG_DIV0) end
local r = round(self[a] / self[b])
self.ACC = doflags(self, r) % 256
return 2;
end;
-- DIV R, R -> RET
-- Free-register divide
function ins.DIV_1dR_2dR_3dRET(self)
local a, b = convreg(self, adrget(self, self.IP+1))
if self.b == 0 then self:signal(SIG_DIV0) end
local r = round(self[a] / self[b])
self.RET = doflags(self, r) % 256
return 2;
end;
-- MUL R, R -> ACC
-- Free-register Multiply
function ins.MUL_1dR_2dR_3dACC(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local r = round(self[a] * self[b])
self.ACC = doflags(self, r) % 256
return 2;
end;
-- LMOV R1(seg),R2(off),R3(destseg), R4(destoff)
-- free-register direct Long-move
function ins.LMOV_1dR_2dR_3dR_4dR(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local c, d = convreg(self, adrget(self, self.IP+2))
adrset(self, self[d], adrget(self, self[b], self[a]), self[c])
return 3;
end;
-- LJMP R1(seg), R2(off)
-- free-register unconditional jump
function ins.LJMP_1dR_2dR(self)
local a, b = convreg(self, adrget(self, self.IP+1))
self.SEG = self[a]
self.IP = self[b]
return 0;
end;
-- EQL .A:.B -> .RET
-- fixed-register AB equals, results in RET
function ins.EQL_1dA_2dB_3dRET(self)
self.RET = ({[true]=1,[false]=0})[self.A == self.B]
return 1;
end;
-- GTR .A:.B -> .RET
-- Fixed-register AB greater-than, results in RET
function ins.GTR_1dA_2dB_3dRET(self)
self.RET = ({[true]=1,[false]=0})[self.A > self.B]
return 1;
end;
-- LES .A:.B -> .RET
-- Fixed-register AB less-than, results in RET
function ins.LES_1dA_2dB_3dRET(self)
self.RET = ({[true]=1,[false]=0})[self.A < self.B]
return 1;
end;
-- LOAD &R: R
-- Put address contents into register
function ins.LOAD_1iR_2dR(self)
local _, b = convreg(self, adrget(self, self.IP+1))
self[b] = adrget(self, self.a)
return 2;
end;
-- LOAD &nn: R
-- Put address contents into register
function ins.LOAD_1iN_2dR(self)
local _, b = convreg(self, adrget(self, self.IP+1))
local point = adrget(self, self.IP+2)
self[b] = adrget(self, point)
return 3;
end;
-- STOR R: &R
-- Put register contents into address
function ins.STOR_1dR_2iR(self)
local a, b = convreg(self, adrget(self, self.IP+1))
adrset(self, self.b, self.a)
return 2;
end;
-- STOR R: &nn
-- Put register contents into address
function ins.STOR_1dR_2iN(self)
local a, b = convreg(self, adrget(self, self.IP+1))
local point = adrget(self, self.IP+2)
adrset(self, point, self[a])
return 3;
end;
-- HLT
-- halts the machine
function ins.HLT(self)
self.state = 'halt';
return 0;
end;
-------------------------------------------------------------------------
-- Instruction set
--
_M.iset = {}
local remap = opcodes.map
local map
for i=0, 0xff do
map = (remap[i] and remap[i][1]) or false
if map then
if not ins[map] then error(string.format("illegal mapping %02x to '%s'", i, tostring(map))) end
_M.iset[i] = ins[map]
end
end
_M.speed = 1*MEGAHERTZ -- set the default speed.
-----
-- With parm it returns the current SIGnal
-- given SIG_NONE it clears the current signal
-- given any other it acts according to the signal rules as follows:
-- if the current signal is NONE, set the given signal,
-- if the given signal is NONE, set the signal to NONE (ie, clear the signal)
-- if the current signal is nither NONE, nor DOUBLE_FAULT, signal DOUBLE_FAULT and LJMP 0,0 (aka, reset)
-- if the current signal is DOUBLE_FAULT, signal a TRIPLE_FAULT, and halt the machine.
function _M:signal(sig)
-- if we weren't given any signal to set/trigger. then its a request
-- for the corrent signal state.
-- oblige by returning the current signal
if sig == nil then return self.SIG, signals[self.SIG] end
if sig == SIG_NONE then -- if sig is none, clear all signals.
self.SIG = SIG_NONE
return self.SIG
elseif self.SIG == SIG_TRIPLE_FAULT then -- you can't go worse that 3fault
return self.SIG
end
if self.SIG == SIG_DOUBLE_FAULT then
-- signals on 2fault is a 3fault
self.SIG = SIG_TRIPLE_FAULT
self.state = 'halt'
elseif self.SIG ~= SIG_NONE then
-- signals on an unresolved signal is a 2fault, which triggers a reset
self.SIG = SIG_DOUBLE_FAULT
self.IP = 0
self.SEG = 0
else
-- otherwise nothign special, just set the sig register and move on
self.SIG = sig
end
-- regardless, return the end result
return self.SIG
end
-----
-- Creates a new machine with the given name.
-- if no name is given, a default name is chosen
function _M:new(name)
_M.number = _M.number+1 -- keep a count of the machines created
-- create a nme if we weren't given one to use.
name = name or ("%s %02x"):format(device:generatename(device.DEV_TYPE_MACHINE),_M.number)
-- raw table that makes up the machine. lots of feilds to make.
local m = {name=name, time = 0, speed=_M.speed, memory={}, devices={}, portmap={};
IP=0, -- Instruction Pointer
SEG=0, -- SEGment Pointer
SIG=0, -- SIGnal register
FLG=0, -- Flags register
A=0, -- Register A
B=0, -- Register B
C=0, -- Register A
D=0, -- Register B
ACC=0, -- ACCumulator
RET=0 -- RETurn, or result
}
setmetatable(m, _MT) -- give it wondrous fantabulous machine metatable!
-- make memory non-nil, and possibly set some default values.
for i=0,(maxmem*maxsegments) do
m.memory[i] = 0--math.random(256)-1
end
-- install the default load-out of devices
local t, err, d
-- null port...
d = device:new{type=device.DEV_TYPE_NONE}
t, err = m:deviceInstall(d)
assert(t, err)
-- default terminal...
d = device:new{type=device.DEV_TYPE_TERMINAL}
t, err = m:deviceInstall(d)
assert(t, err)
assert(device:load('store'), "could not load")
assert(device:load('clock'), "could not load")
-- Real-time clock
d = device:new{type=device.DEV_TYPE_CLOCK}
t, err = m:deviceInstall(d)
assert(t, err)
-- persistant store (eg. HDD or FDD)
d, err = device:new{type=device.DEV_TYPE_STORE}
assert(d, err)
t, err = m:deviceInstall(d)
assert(t, err)
return m
end
-----
-- Loads the given chunk or bytestring into memory.
-- as one parm (data) it load the data into 0:0
-- as two parms (start, data) it loads the data into the given address
function _M:load(start, data)
-- starting offset is optional, but we have to shuffle things around
-- a bit if its not given.
if data == nil then data, start = start, 0 end
assert(type(data)=='table' or type(data)=='string', "Invalid loadable data parm: '" ..type(data).."'")
if type(data)=='string' then data=stringtodata(data) end
for i=0, #data-1 do
assert(type(data[i+1]) == 'number' and data[i+1] == data[i+1]%256,
"data can only contain numerical data in the range 0..255")
self.memory[i+start] = data[i+1]
end
end
-- creates a device and 'installs' it into the machine.
-- should creation fail, it returns nil.
--
-- Returns: dev-id
function _M:deviceInstall(dev)
assert(dev, "Must be given a Device to install!")
local idx, _ = #self.devices + 1
-- try to find a valid portmapping
local p_map, err = dev:findports(self.portmap)
-- register the ports, and additionally check that it really
-- is a valid mapping
if p_map then
for i, p in pairs(p_map) do
if self.portmap[p] then
return false, "port conflict" end
self.portmap[p]=dev:registerport(p, i)
end
else
return false, err or "unknown install error"
end
-- start the device and ready it for use.
local t, err = dev:start(self, idx)
if t then
self.devices[idx] = dev
else
return false, tostring(err)
end
return dev
end
-- attempt to use port io to write val to device id
--
-- returns: status-code
function _M:deviceWrite(adr, val)
if not self.portmap[adr] then
return false, device.DEV_STATUS_FAULTED end
return self.portmap[adr]:writeport(adr, val)
end
-- attempt to use port io to write val to device id
--
-- returns: status-code
function _M:deviceRead(adr)
if not self.portmap[adr] then
return false, device.DEV_STATUS_FAULTED end
return self.portmap[adr]:readport(adr)
end
-- Causes the field-circus to services the indicated device,
-- rendering it inoperable
--
-- returns: (nothing)
function _M:deviceBreak(id)
local dev = self.devices[id]
if not dev then return end
if dev.type == DEV_TERMINAL or dev.type == DEV_STREAM then
dev.stream:close()
self.devices[id] = false
end
end
-----
-- Executes the given number of cycles.
-- if no number if given, it executes a single cycle.
function _M:cycle(n)
local n = n or 1
local ins, adv
for i=1,n do
ins = adrget(self, self.IP)
if not self.iset[ins] then
alert(false, ("Invalid instruction at address 0x%02x : %02x"):format(self.IP, ins))
self:signal(SIG_ILLEGAL_INSTRUCTION)
else
-- Note: failure to update the segment is not an oversight.
self.IP = self.iset[ins](self) + self.IP -- order is important here.