-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnga.lua
238 lines (195 loc) · 4.97 KB
/
nga.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
local class = require('middleclass')
local utils = require('utils')
local OPMixin = require('op_mixin')
local ops = require('ops')
-- default values
local IMAGE_SIZE = 52488 * 16
local ADDRESSES = 2048
local STACK_DEPTH = 512
local NUM_DEVICES = 0
local ADDR_START = 1
-- data - stack
-- address - address
-- memory - image
local NgaVM = class('NgaVM')
function NgaVM:initialize(arg)
self.state = {}
local conf = {}
conf.image_size = arg.image_size or IMAGE_SIZE
conf.addresses = arg.addresses or ADDRESSES
conf.stack_depth = arg.stack_depth or STACK_DEPTH
conf.num_devices = arg.num_devices or NUM_DEVICES
conf.addr_start = arg.addr_start or ADDR_START
conf.packed_opcode_num = arg.packed_opcode_num or 4
self.conf = conf
local init_memory = arg.init_memory
if init_memory == nil then
init_memory = true
end
self.io = {}
self.io.device_handlers = arg.io_device_handlers or {}
self.io.query_handlers = arg.io_query_handlers or {}
-- set vm up
self:reset(init_memory)
end
-- after instances are created and every time state/conf/io table is
-- replaced, call this
function NgaVM:setup()
self:setup_alias()
self:setup_proxy()
end
function NgaVM:setup_alias()
-- tos, nos, tors
local alias_mt = {}
local state = self.state
local data = state.data
local address = state.address
function alias_mt.__index(_, k)
if k == 'tos' then
return data[state.sp]
elseif k == 'nos' then
return data[state.sp - 1]
elseif k == 'tors' then
return address[state.rp]
end
end
function alias_mt.__newindex(_, k, v)
if k == 'tos' then
data[state.sp] = v
elseif k == 'nos' then
data[state.sp - 1] = v
elseif k == 'tors' then
address[state.rp] = v
end
end
self.alias = {}
setmetatable(self.alias, alias_mt)
end
function NgaVM:setup_proxy()
local proxy = {}
proxy.conf = self.conf
proxy.alias = self.alias
proxy.state = self.state
proxy.io = self.io
self._vm_proxy = proxy
end
-- reset/prepare
function NgaVM:reset(init_memory)
if init_memory == nil then
init_memory = true
end
local state = self.state
state.memory = {}
state.data = {}
state.address = {}
state.ip = self.conf.addr_start
state.sp = self.conf.addr_start
state.rp = self.conf.addr_start
if init_memory then
self:init_mem()
end
end
function NgaVM:init_mem()
local start = self.conf.addr_start
local nop = 0
for i=start, self.conf.image_size + start - 1 do
self.state.memory[i] = nop
end
for i=start, self.conf.stack_depth + start - 1 do
self.state.data[i] = nop
end
for i=start, self.conf.addresses + start - 1 do
self.state.address[i] = nop
end
end
function NgaVM:print_stacks()
io.stdout:write("data stack: ( ")
for i=self.conf.addr_start,self.state.sp do
io.stdout:write(tostring(self.state.data[i]))
io.stdout:write(" ")
end
io.stdout:write(")\n")
io.stdout:write("address stack: ( ")
for i=self.conf.addr_start,self.state.rp do
io.stdout:write(tostring(self.state.address[i]))
io.stdout:write(" ")
end
io.stdout:write(")\n")
end
-- debug heap
function NgaVM:print_new_heap(previous_heap)
local heap_addr = self.conf.addr_start + 3
local current_heap = self.state.memory[heap_addr]
if current_heap > previous_heap then
io.stdout:write("heap starts from " .. previous_heap .. ": ( ")
for i=previous_heap, current_heap - 1 do
io.stdout:write(self.state.memory[i] .. " ")
end
io.stdout:write(")\n")
end
return current_heap
end
function NgaVM:load_image(path)
-- each line is a cell
local f = assert(io.open(path))
local idx = self.conf.addr_start
for line in io.lines(path) do
local cell = tonumber(line)
self.state.memory[idx] = cell
idx = idx + 1
end
assert(f:close())
end
function NgaVM:dump_image(path, memory, start, end_, keep_trailing_zeros)
local cell
if not memory then
memory = self.state.memory
end
if not start then
start = self.conf.addr_start
end
if not end_ then
end_ = self.conf.image_size + self.conf.addr_start - 1
end
if not keep_trailing_zeros then
local last_non_zero_idx = start
for i=start, end_ do
cell = memory[i]
if cell and cell ~= 0 then
last_non_zero_idx = i
end
end
end_ = last_non_zero_idx
end
local f = assert(io.open(path, 'w'))
for i=start, end_ do
cell = memory[i]
f:write(cell, '\n')
end
f:close()
end
function NgaVM:exec_packed_opcodes(raw_code)
local current
local valid = true
local packed_num = self.conf.packed_opcode_num
local opcodes = utils.unpack_opcodes(raw_code, packed_num)
for i=1,packed_num do
current = opcodes[i]
if not self:is_valid_opcode(current) then
valid = false
break
end
end
if valid then
for i=1,packed_num do
current = opcodes[i]
self:exec_opcode(current)
end
end
return valid
end
NgaVM.static.OPS = ops.ops
NgaVM.static.OP2CODE = ops.op2code
NgaVM.static.OP2INST = ops.insts
NgaVM:include(OPMixin)
return NgaVM