-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclick42ppm.lua
157 lines (138 loc) · 4.3 KB
/
click42ppm.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
require"src.color"
local function readAll(filename)
local f = io.open(filename, "rb")
local content = f:read("*all")
f:close()
return content
end
local function split(s, delimter)
local result = {}
for match in (s..delimter):gmatch("(.-)"..delimter) do
table.insert(result, match)
end
return result
end
local filename = arg[1]
assert(filename~=nil,"Missing filename")
local raw = readAll(filename)
local lines = split(raw,"\n")
local symbols = {}
for _,line in pairs(lines) do
if string.sub(line,1,1) ~= "#" then
for i,v in pairs( split(line," ") ) do
if v ~= "" then
table.insert(symbols,v)
end
end
end
end
function position_to_jmp_args(pos)
local x = (pos/64)%1*64
local y = math.floor(pos/64)
local x1 = math.floor(x/16)
local x2 = (x/16)%1*16
local y1 = math.floor(y/16)
local y2 = (y/16)%1*16
--[[
io.stderr:write(
pos.."\t"..
"[x:"..x..",y:"..y.."]\t"..
"JMP "..x1.." "..x2.." "..y1.." "..y2.."\n"
)
--]]
return x1,x2,y1,y2
end
local symbols_clean = {}
local labels = {}
local ops = require "src.ops"
for _,op in pairs(symbols) do
local valid = false
for index,testop in pairs(ops) do
if index-1 == tonumber(op) then
table.insert(symbols_clean, {symbol=op,value=index-1})
valid = true
break
elseif testop.label == op then
table.insert(symbols_clean, {symbol=op,value=index-1})
valid = true
break
elseif testop.sound == op then
table.insert(symbols_clean, {symbol=op,value=index-1})
valid = true
break
end
end
if string.sub(op,1,1) == "@" then
local label = string.sub(op,2)
local label_pos = #symbols_clean
local x1,x2,y1,y2 = position_to_jmp_args(label_pos)
if labels[label] then
io.stderr:write("Warning: skipping already defined label `"..label.."` at symbol position "..label_pos.."\n")
else
--io.stderr:write("Defining label `"..label.."` at symbol position "..label_pos.."\n")
labels[label] = label_pos
end
valid = true
elseif string.sub(op,1,2) == "!!" then
local label = string.sub(op,3)
--io.stderr:write("rjmp to label `"..label.."`\n")
table.insert(symbols_clean, {rjmp=label})--RJMP
table.insert(symbols_clean, {})--arg1
valid = true
elseif string.sub(op,1,1) == "!" then
local label = string.sub(op,2)
--io.stderr:write("jmp to label `"..label.."`\n")
table.insert(symbols_clean, {jmp=label})--JMP
table.insert(symbols_clean, {})--arg1
table.insert(symbols_clean, {})--arg2
table.insert(symbols_clean, {})--arg3
table.insert(symbols_clean, {})--arg4
valid = true
end
if valid then
if #symbols_clean >= 64*64 then
io.stderr:write("Warning: symbol `"..op.."` outside of cart size at symbol position "..#symbols_clean.."\n")
end
else
io.stderr:write("Warning: skipping unknown symbol `"..op.."` at symbol position "..#symbols_clean.."\n")
end
end
-- Update JMP/RJMP targets
for symbol_index,symbol in pairs(symbols_clean) do
if symbol.rjmp then
local offset = 0
if labels[symbol.rjmp] then
offset = labels[symbol.rjmp] - symbol_index - 2
if offset > 15 then
io.stderr:write("Warning: rjmp offset is greater than 15 ("..offset..")`\n")
offset = 0
end
else
io.stderr:write("Warning: skipping unknown rjmp `"..symbol.rjmp.." at symbol position "..symbol_index.."`\n")
end
symbols_clean[symbol_index+0] = {symbol="RJMP",value=8}
symbols_clean[symbol_index+1] = {symbol=offset,value=offset}
end
if symbol.jmp then
local x1,x2,y1,y2 = 0,0,0,0
if labels[symbol.jmp] then
x1,x2,y1,y2 = position_to_jmp_args(labels[symbol.jmp])
else
io.stderr:write("Warning: skipping unknown jmp `"..symbol.jmp.." at symbol position "..symbol_index.."`\n")
end
symbols_clean[symbol_index+0] = {symbol="JMP",value=7}
symbols_clean[symbol_index+1] = {symbol=x1,value=x1}
symbols_clean[symbol_index+2] = {symbol=x2,value=x2}
symbols_clean[symbol_index+3] = {symbol=y1,value=y1}
symbols_clean[symbol_index+4] = {symbol=y2,value=y2}
end
end
print("P3")
print("# click4 raw ppm")
print("64 64")
print("255")
for i = 1,64*64 do
local v = symbols_clean[i] or {symbol="0",value=0}
local c = color(v.value+1)
print(c[1],c[2],c[3],"# ["..(i-1).."] "..v.symbol.."("..v.value..")")
end