-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogrammer.lua
More file actions
106 lines (81 loc) · 1.72 KB
/
programmer.lua
File metadata and controls
106 lines (81 loc) · 1.72 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
Error = require("Error")
local Out = {
Ctrl = "top",
Inst = "right",
Low = "left",
High = "back"
}
local MemoryMax = 64
function get(side)
return rs.getBundledOutput(side)
end
function set(side, value)
rs.setBundledOutput(side, value)
end
function commit()
os.sleep(0.6)
end
function flip(b)
set(Out.Ctrl, bit.bor(get(Out.Ctrl), b))
commit()
set(Out.Ctrl, bit.band(get(Out.Ctrl), bit.bnot(b)))
commit()
end
function flipClock()
flip(1)
end
function flipProg()
flip(2)
end
function init(arg)
if table.getn(arg) ~= 1 then
print("Usage: programmer <input_file>")
return nil
end
local input = fs.open(arg[1], "rb")
if input == nil then
Error.print("Error opening file "..arg[1])
end
return input
end
function prepare()
set(Out.Ctrl, 4)
set(Out.Inst, 0)
set(Out.Low , 0)
set(Out.High, 0)
commit()
flipClock()
set(Out.Ctrl, 0)
commit()
end
function writeOut(input)
for i = 1,MemoryMax do
local inst = input.read()
local low = input.read()
local high = input.read()
if inst == nil then
inst = 0
low = 0
high = 0
elseif low == nil or high == nil then
Error.print("Corrupted binary")
end
print(i, ":", inst, low, high)
set(Out.Inst, inst)
set(Out.Low , low )
set(Out.High, high)
flipProg()
flipClock()
end
end
function finish()
set(Out.Ctrl, 0)
set(Out.Inst, 0)
set(Out.Low , 0)
set(Out.High, 0)
end
local input = init{...}
prepare()
writeOut(input)
input.close()
finish()