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 | asm = [[
# load values to memory
label loadloop
add+I 0 IO mem
jle mem 1 not_max
add+I 0 adr 0
add+I 0 mem 1
label not_max
add+I 1 adr adr
jne+I 16 adr loadloop
# 0: max index (set while loading)
# 1: max height
# 2: water
# stack: tmp
# adr: adr
# mem: heights
# reset adr and max height
add+I+J 0 255 adr
add+I+J 0 0 1
label first_loop
add+I 1 adr adr
jeq adr 0 second_part
jgt mem 1 update_max_first
sub 1 mem stack
add 2 stack+pop 2
jeq+I+J 0 0 first_loop
label update_max_first
add+I 0 mem 1
jeq+I+J 0 0 first_loop
label second_part
add+I+J 0 16 adr
add+I+J 0 0 1
label second_loop
sub+J adr 1 adr
jeq adr 0 end
jgt mem 1 update_max_second
sub 1 mem stack
add 2 stack+pop 2
jeq+I+J 0 0 second_loop
label update_max_second
add+I 0 mem 1
jeq+I+J 0 0 second_loop
label end
add+I 0 2 IO
]]
codes = {
jge = 0x25,
jgt = 0x24,
jle = 0x23,
jlt = 0x22,
jne = 0x21,
jeq = 0x20,
shl = 7,
shr = 6,
xor = 5,
["not"] = 4,
["or"] = 3,
["and"] = 2,
sub = 1,
add = 0,
I = 0x80,
J = 0x40,
jump = 0x20,
stack = 3, pop = 8,
adr = 4,
mem = 5,
IP = 6,
IO = 7,
}
compile = function(asm)
local no_coms = ""
for l in asm:gmatch("[^\n]*") do
if l:sub(1,1) ~= "#" then
no_coms = no_coms .. " " .. l
end
end
local iter = no_coms:gmatch("(%+?)([%w_]+)")
local rom, labels, i = {}, {}, 0
local plus, com = iter()
while com do
if com == "label" then
plus, com = iter()
labels[com] = labels[com] or {}
labels[com].target = i
else
if plus ~= "+" then
rom[i] = 0
i = i + 1
end
if codes[com] then
rom[i - 1] = rom[i - 1] + codes[com]
elseif tonumber(com) then
rom[i - 1] = rom[i - 1] + com
else
labels[com] = labels[com] or {}
table.insert(labels[com], i - 1)
end
end
plus, com = iter()
end
for k, v in pairs(labels) do
for i = 1, #v do
if v.target then
rom[v[i]] = rom[v[i]] + v.target
else
print("A label without a target: "..k)
end
end
end
return rom
end
emulate = function(rom, input)
local ram, stack, out = {}, {}, {}
local ip, r0, r1, r2, adr = 0, 0, 0, 0, 0
while rom[ip + 3] do
local com, imm1, imm2, imm3 = rom[ip], rom[ip + 1], rom[ip + 2], rom[ip + 3]
ip = ip + 4
local regs = {[0] = r0, r1, r2, stack[#stack] or 0, adr, ram[adr] or 0, ip, input[1] or 0}
local arg1, arg2, flag, ans
if com & codes.I > 0 then
arg1 = imm1
else
arg1 = regs[imm1 & 7]
end
if com & codes.J > 0 then
arg2 = imm2
else
arg2 = regs[imm2 & 7]
end
if imm1 & 7 == 7 or imm2 & 7 == 7 and #input > 0
then table.remove(input, 1) end
if imm1 & 8 > 0 then table.remove(stack) end
if com & codes.jump > 0 then
if com & 7 == codes.jge & 7 then
flag = arg1 >= arg2
elseif com & 7 == codes.jgt & 7 then
flag = arg1 > arg2
elseif com & 7 == codes.jle & 7 then
flag = arg1 <= arg2
elseif com & 7 == codes.jlt & 7 then
flag = arg1 < arg2
elseif com & 7 == codes.jne & 7 then
flag = arg1 ~= arg2
elseif com & 7 == codes.jeq & 7 then
flag = arg1 == arg2
end
if flag then ip = imm3 end
else
if com & 7 == codes.shl then
ans = arg1 << arg2
elseif com & 7 == codes.shr then
ans = arg1 >> arg2
elseif com & 7 == codes.xor then
ans = arg1 ~ arg2
elseif com & 7 == codes["not"] then
ans = ~arg1
elseif com & 7 == codes["or"] then
ans = arg1 | arg2
elseif com & 7 == codes["and"] then
ans = arg1 & arg2
elseif com & 7 == codes.sub then
ans = arg1 - arg2
elseif com & 7 == codes.add then
ans = arg1 + arg2
end
ans = ans & 255
if imm3 & 7 == 0 then r0 = ans
elseif imm3 & 7 == 1 then r1 = ans
elseif imm3 & 7 == 2 then r2 = ans
elseif imm3 & 7 == 3 then table.insert(stack, ans)
elseif imm3 & 7 == 4 then adr = ans
elseif imm3 & 7 == 5 then ram[adr] = ans
elseif imm3 & 7 == 6 then ip = ans
elseif imm3 & 7 == 7 then table.insert(out, ans)
end
end
end
return out
end
entry = function(t) return emulate(compile(asm), t)[1] end
|
post a comment