-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.lua
More file actions
327 lines (263 loc) · 9.5 KB
/
cpu.lua
File metadata and controls
327 lines (263 loc) · 9.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
--- @class cpuClass
--- @field memory table
--- @field gpRegisters table
--- @field instructionSet instructionSetClass
--- @field stackRegister table
--- @field sleepT table
local cpu = {}
cpu.__index = cpu
local maxStackDepth = 100
local memorySize = 1000
local gpRegisterAmount = 10
UPDATE_TIME = 0.033 --one instruction per frame , false = never, number = seconds to wait
function cpu.new(instructionSet)
local self = {
memoryAddressRegister = 0, --Current memory index
memoryDataRegister = nil, --Current memory index data
memory = {}, --array of accessible addresses
programMemory = {}, --array of instructions
gpRegisters = {}, --General purpose registers
stackRegister = { stack = {}, currentPos = 0 }, --Stack register
programCounter = 1, --next instruction address
devices = {},
devBuses = {},
statusCode = 0, --0 idle / 1 active / 2 sleeping / 3 dead
statusDetails = '',
sleepT = {active = false, time = 0},
accumulator = 0,
frequency = 0,
peakfrequency = 0,
}
for i = 1, memorySize do
self.memory[i] = {data = false}
end
for i = 1, gpRegisterAmount do
self.gpRegisters['R'..i] = {data = false}
end
self.gpRegisters['RMA'] = {data = false}
self.gpRegisters['RMD'] = {data = false}
self.gpRegisters['RDBA'] = {data = false}
self.gpRegisters['RDMA'] = {data = false}
self.gpRegisters['RDMD'] = {data = false}
local c = setmetatable(self, cpu)
c.instructionSet = instructionSet.new(c)
return c
end
-- Array of sequential instructions and arguments to be executed
function cpu:load(instructions)
--Indicate that the CPU is active
self.statusCode = 1
--Point to the first instruction
self.programCounter = 1
--Reset the memory and registers to empty
self.memory = {}
for i = 1, gpRegisterAmount do
self.gpRegisters['R'..i] = {data = false}
end
self.gpRegisters['RMA'] = {data = false}
self.gpRegisters['RMD'] = {data = false}
self.gpRegisters['RDBA'] = {data = false}
self.gpRegisters['RDMA'] = {data = false}
self.gpRegisters['RDMD'] = {data = false}
--Reset any crashed text
self.statusDetails = ''
--Store the instructions in memory
self.programMemory = instructions
end
--Outwards facing update
function cpu:update(dt)
if UPDATE_TIME then
if type(UPDATE_TIME) =="number" then
self.accumulator = math.min(self.accumulator+dt, 1)
local count = 0
repeat
count = count + 1
if self.accumulator > UPDATE_TIME then
if self.statusCode == 1 then
local st = love.timer.getTime()
self:executeNext()
local ft = love.timer.getTime()-st
self.accumulator = self.accumulator - math.max(ft,UPDATE_TIME)
else
self.accumulator = 0
self.peakfrequency = 0
end
end
until self.accumulator < UPDATE_TIME
self.frequency = 1/(dt/count)
self.peakfrequency = math.max(self.peakfrequency, self.frequency)
if self.statusCode == 2 then
self:status2(dt)
end
else
if self.statusCode == 1 then
self:executeNext()
elseif self.statusCode == 2 then
self:status2(dt)
end
end
end
end
function cpu:compileAOT()
self.preCompiled = true
self.instructionMemory = {}
for i, inst in ipairs(self.programMemory) do
local s, e = self.instructionSet:preCompile(inst)
self.instructionMemory[i] = {s = s, e = e}
if s == false then
return self:crash(e)
end
end
end
--Internal update (basically :3)
function cpu:executeNext()
self.oldProgCounter = self.programCounter
self.programCounter = self.programCounter + 1
if self.preCompiled then
return self:executeAOT()
end
if not self.programMemory[self.oldProgCounter] then
self.statusCode = 0
self.statusDetails = 'Program Finished'
return
end
local s, e = self.instructionSet:exec(self.programMemory[self.oldProgCounter])
if s == false then
self:crash(e)
end
end
function cpu:executeAOT()
if not self.instructionMemory[self.oldProgCounter] then
self.statusCode = 0
self.statusDetails = 'Program Finished'
return
end
local s, e = self.instructionSet:runCompiled(self.instructionMemory[self.oldProgCounter].s, self.instructionMemory[self.oldProgCounter].e)
if s == false then
self:crash(e)
end
end
--Mostly for labels, but who knows
function cpu:findInstruction(text)
for index, instruction in ipairs(self.programMemory) do
if instruction == text then
return index
end
end
end
--Sets a gp register
function cpu:setRegister(regNum, data)
if self.gpRegisters[regNum] then
if regNum == 'RMA' then
if self.memory[data] then
self.gpRegisters['RMD'].data = self.memory[data].data
else
return self:crash('Tried to access memory out of range')
end
end
if regNum == 'RMD' then
local index = self.gpRegisters["RMA"].data
if self.memory[index] then
self.memory[index].data = data
else
return self:crash('Tried to access memory out of range')
end
end
--Device bus address
if regNum == 'RDBA' then
if not self.devices[data] then
return self:crash('Tried to set device bus register to a non-existant device '..data)
end
end
--Device memory address
if regNum == 'RDMA' then
if not self.gpRegisters.RDBA.data then
return self:crash('Set device bus first '..data)
else
if not self.devices[self.gpRegisters.RDBA.data].implements.DMI then
return self:crash(self.gpRegisters.RDBA.data..' doesnt implement memory access')
else
if not self.devices[self.gpRegisters.RDBA.data]:check(data) then
return self:crash('tried to access device memory out of bounds '..self.gpRegisters.RDBA.data..':'..data)
end
end
end
end
--Device memory data
if regNum == 'RDMD' then
if not self.gpRegisters.RDBA.data or not self.gpRegisters.RDMA.data then
return self:crash('Tried to set undefined devices memory, set RDBA and RDMA first')
end
if self.devices[self.gpRegisters.RDBA.data].implements.DMI then
self.gpRegisters[regNum].data = data
return self.devices[self.gpRegisters.RDBA.data]:set(self.gpRegisters.RDMA.data, data)
else
return self:crash(self.gpRegisters.RDBA.data..' doesnt implement memory access')
end
end
self.gpRegisters[regNum].data = data
else
self:crash('Error setting data: Register '..regNum..' doesnt exist')
end
end
--Gets data from register
function cpu:getRegister(regNum)
if self.gpRegisters[regNum] then
if regNum == 'RDMD' then
self.gpRegisters[regNum].data = self.devices[self.gpRegisters.RDBA.data]:get(self.gpRegisters.RDMA.data)
end
return self.gpRegisters[regNum].data
else
self:crash('Error getting data: Register '..regNum..' doesnt exist')
end
end
--Pushes data to stack register
function cpu:push(a,b,c,d,e,f)
if self.stackRegister.currentPos <= 0 then
self.stackRegister.currentPos = 1
elseif self.stackRegister.currentPos >= 1 then
self.stackRegister.currentPos = self.stackRegister.currentPos + 1
end
self.stackRegister.stack[self.stackRegister.currentPos] = {a,b,c,d,e,f}
if self.stackRegister.currentPos > maxStackDepth then
self:crash('Reached max stack depth')
end
end
--Pops data from stack register
function cpu:pop()
if self.stackRegister.currentPos < 1 then
return nil
end
local d = self.stackRegister.stack[self.stackRegister.currentPos]
if self.stackRegister.currentPos > 0 then
self.stackRegister.currentPos = self.stackRegister.currentPos - 1
end
return d[1],d[2],d[3],d[4],d[5],d[6]
end
function cpu:setProgramCounter(address)
self.programCounter = address
end
function cpu:addDevice(device)
self.devices[device.address] = device
end
function cpu:finish()
self.statusCode = 0
self.statusDetails = 'Finished'
end
function cpu:sleep(seconds)
self.sleepT.active = true
self.sleepT.time = seconds
self.statusCode = 2
end
function cpu:status2(dt)
self.sleepT.time = self.sleepT.time - dt
if self.sleepT.time < 0 then
self.statusCode = 1
self.sleepT.active = false
end
end
function cpu:crash(err)
self.statusCode = 3
self.statusDetails = err
end
return cpu