-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
103 lines (82 loc) · 2.3 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
-- The CHIP-8 emulator.
local CHIP8 = require("chip8")
-- The dimensions of the screen.
local DISPLAY_W = 64
local DISPLAY_H = 32
-- Side length for one pixel.
local PIXEL_SIZE = require("settings").PIXEL_SIZE
-- Mapping of keyboard key to CHIP-8 input.
local KEY_MAPPING = require("settings").KEY_MAPPING
-- The CHIP-8 emulator object.
local cpu
-- RGB value for being fully opaque.
local OPAQUE = 255
-- Load the emulator.
function love.load(args)
local file = love.filesystem.newFile("ROM/"..require("settings").ROM)
local status, result = file:open("r")
if (status) then
-- The game instructions.
local game = {}
-- Load each byte from the ROM into game.
while (not file:isEOF()) do
game[#game + 1] = file:read(1):byte()
if (not game[#game]) then
print("Failed to read ROM! ("..(game[#game] or "nil")..")")
return
end
end
file:close()
-- Start the game.
cpu = CHIP8()
cpu:reset()
cpu:load(game)
else
print(result)
file:close()
end
end
-- Run the CPU.
function love.update()
if (cpu) then
local result = cpu:cycle()
if (result) then
print("ERROR: "..result)
end
if (cpu.ST > 0) then
print("BEEP!")
end
end
end
-- Draw the results on the screen.
function love.draw()
-- Black background.
love.graphics.clear(0, 0, 0, OPAQUE)
if (not cpu) then
return
end
-- Make each screen pixel white.
love.graphics.setColor(OPAQUE, OPAQUE, OPAQUE, OPAQUE)
-- Draw each screen pixel if active.
for y = 0, DISPLAY_H - 1 do
for x = 0, DISPLAY_W - 1 do
if (cpu.display[x + (y * DISPLAY_W)] > 0) then
love.graphics.rectangle("fill",
x * PIXEL_SIZE, y * PIXEL_SIZE,
PIXEL_SIZE, PIXEL_SIZE)
end
end
end
end
-- Handle keyboard press.
function love.keypressed(key)
if (cpu and KEY_MAPPING[key]) then
cpu:setKeyDown(KEY_MAPPING[key], true)
end
end
-- Handle keyboard release.
function love.keyreleased(key)
if (cpu and KEY_MAPPING[key]) then
cpu:setKeyDown(KEY_MAPPING[key], false)
end
end