-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
285 lines (209 loc) · 5.81 KB
/
main.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
local Uxn = (require "uxn").Uxn
bit = require "bit"
local band, bor, bxor, bnot = bit.band, bit.bor, bit.bxor, bit.bnot
local arshift, rshift, lshift = bit.arshift, bit.rshift, bit.lshift
local math = require "math"
local devices = require "love-devices"
Device = require "device"
-- Debugging
--jit.off()
PROFILE = nil
-- Screen parameters
WIDTH = 300
HEIGHT = 200
PADDING = 4
SCALING = 2
function setupCPU(mem)
local cpu = Uxn:new(mem)
cpu.ip = 0x0100
-- Debugging flags
cpu.PRINT = false
cpu.memory.ERROR_ON_UNINITIALIZED_READ = false
system = cpu:addDevice(0, devices.system)
console = cpu:addDevice(1, devices.console)
screen = devices.screen(WIDTH, HEIGHT)
cpu:addDevice(2, screen)
controller = cpu:addDevice(8, devices.controller)
mouse = cpu:addDevice(9, devices.mouse)
file = cpu:addDevice(10, devices.file)
cpu:addDevice(11, devices.datetime)
return cpu
end
function love.load(arg)
love.mouse.setVisible(false)
love.graphics.setDefaultFilter("nearest", "nearest")
--love.graphics.setNewFont("mono.ttf", 14)
love.graphics.setBackgroundColor(0,0,0)
love.window.setMode((WIDTH * SCALING) + (PADDING * 2), (HEIGHT * SCALING) + (PADDING * 2))
love.keyboard.setKeyRepeat(true)
-- This is the shader that translates system colours into
-- palette colours
paletteShader = love.graphics.newShader [[
uniform vec3 palette[4];
vec4 effect( vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords ) {
vec4 pixel = Texel(tex, texture_coords);
int index = int(pixel.r * 4.0 + 0.5);
pixel.rgb = palette[index];
return pixel * color;
}
]]
local memory = {}
-- Preload the zero-page
for i = 0, 255 do
memory[i] = 0
end
-- Takes in a filename from the command line, else load boot.rom
local data, size = love.filesystem.read("data", arg[1] or "boot.rom")
for i = 1, size do
memory[i + 255] = love.data.unpack("B", data, i)
end
-- Create a new CPU
cpu = setupCPU(memory)
-- Execute the initial vector
cpu:runUntilBreak()
if PROFILE then
Device.DEBUG_NUM_CALLS.read = {}
Device.DEBUG_NUM_CALLS.write = {}
cpu.debug_profile = {}
cpu.device_triggers = {}
cpu.device_reads = {}
cpu.device_writes = {}
love.profiler = require('cigumo_profile')
love.profiler.start()
end
end
local frame = 0
function love.draw()
frame = frame + 1
-- Run the screen vector
screen:trigger()
if frame == PROFILE then
love.profiler.stop()
print(love.profiler.report())
print("device", "num_triggers")
for k, v in pairs(cpu.device_triggers) do
print(k, v)
end
print("device", "cpu_reads")
for k, v in pairs(cpu.device_reads) do
print(k, v)
end
print("device", "cpu_writes")
for k, v in pairs(cpu.device_writes) do
print(k, v)
end
print("device", "total Device reads")
for k, v in pairs(Device.DEBUG_NUM_CALLS.read) do
print(k, v)
end
print("device, total Device writes")
for k, v in pairs(Device.DEBUG_NUM_CALLS.write) do
print(k, v)
end
print(cpu:print_profile())
end
love.graphics.push()
love.graphics.translate(PADDING, PADDING)
love.graphics.scale(SCALING, SCALING)
love.graphics.setColor(1,1,1)
love.graphics.setBlendMode("replace", "premultiplied")
love.graphics.setShader(paletteShader)
love.graphics.draw(screen.back)
love.graphics.setBlendMode("alpha")
love.graphics.draw(screen.front)
love.graphics.setShader()
love.graphics.pop()
local dbg = "x: "..screen:readShort(8).." y: "..screen:readShort(10).." "
dbg = dbg.."PS: "..table.concat(cpu.program_stack, " ").." "
dbg = dbg.."frame: "..frame.." fps: "..love.timer.getFPS()
love.graphics.setColor(1,0,0)
--love.graphics.print(dbg, 10, 300)
end
-- Take in a love keyconstant and return which bit in the controller byte
keyToBit = {
["rctrl"] = 0, ["lctrl"] = 0,
["ralt"] = 2, ["lalt"] = 2,
["rshift"] = 4, ["lshift"] = 4,
["escape"] = 8,
["up"] = 16,
["down"] = 32,
["left"] = 64,
["right"] = 128,
}
function love.textinput(text)
controller[3] = string.byte(string.sub(text,1,1))
controller:trigger()
end
function love.keypressed(key)
controller[2] = bor(controller[2], keyToBit[key] or 0)
local ascii = 0
if key == "backspace" then
ascii = 0x08
elseif key == "return" then
ascii = 0x0d
elseif key == "tab" then
ascii = 0x09
elseif key == "delete" then
ascii = 0x7f
end
controller[3] = ascii
controller:trigger()
end
function love.keyreleased(key)
controller[2] = band(controller[2], bnot(keyToBit[key] or 0))
controller[3] = 0
controller:trigger()
end
--[[ TODO
function love.filedropped(file)
-- TODO: Reset devices
cpu = setupCPU()
file:open("r")
local data = file:read("data")
data = love.data.encode("string", "hex", data)
cpu.memory.hex_rom = data
cpu:runUntilBreak()
end
]]--
function normalizeMouse(x, y)
-- Move to the center to offset math.floor
x = x + 0.5
y = y + 0.5
x = x - PADDING
y = y - PADDING
x = x / SCALING
y = y / SCALING
return math.floor(x), math.floor(y)
end
local old_x, old_y
function love.mousemoved(x, y)
x, y = normalizeMouse(x, y)
if x ~= old_x or y ~= old_y then
mouse:writeShort(2, x)
mouse:writeShort(4, y)
old_x = x
old_y = y
mouse:trigger()
end
end
function love.mousepressed(x, y, button)
x, y = normalizeMouse(x, y)
mouse:writeShort(2, x)
mouse:writeShort(4, y)
mouse[6] = bor(mouse[6], button == 1 and 0x01 or 0x10)
mouse:trigger()
end
function love.mousereleased(x, y, button)
x, y = normalizeMouse(x, y)
mouse:writeShort(2, x)
mouse:writeShort(4, y)
mouse[6] = band(mouse[6], button == 1 and 0x10 or 0x01)
mouse:trigger()
end
function love.wheelmoved(_, y)
mouse[7] = y > 0 and 1 or -1
mouse:trigger()
end
function love.update(dt)
mouse[7] = 0
end