-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy patharbiter_6.py
More file actions
211 lines (174 loc) · 6.16 KB
/
arbiter_6.py
File metadata and controls
211 lines (174 loc) · 6.16 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
# pylint: disable=import-error
import calyx.builder as cb
def add_wrap2(prog):
"""Inserts the component `wrap2` into the program.
It has:
- two inputs, `i` and `j`.
- six ref memories, `mem1` through `mem6`, of size 4 each.
- one ref memory, `ans`.
The invoker wants to pretend that there are actually
_two_ memories of size _12_ each.
The invoker wants to index the memories while living in this fiction.
This component will return mem[i][j], but indexed according to the fiction.
Accordingly, we assume 0 <= i < 2 and 0 <= j < 12.
"""
wrap: cb.ComponentBuilder = prog.component("wrap2")
i = wrap.input("i", 32)
j = wrap.input("j", 32)
# Six memory cells, plus an answer cell.
mems = [wrap.comb_mem_d1(f"mem{i}", 32, 4, 32, is_ref=True) for i in range(6)]
ans = wrap.comb_mem_d1("ans", 32, 1, 32, is_ref=True)
# We will need j % 4, so we'll store it in a cell.
j_mod_4 = wrap.reg(32)
# Additional cells and groups to compute equality and lt
i_eq_0 = wrap.eq_use(i, 0)
i_eq_1 = wrap.eq_use(i, 1)
j_lt_4 = wrap.lt_use(j, 4)
j_lt_8 = wrap.lt_use(j, 8)
# Load `j` unchanged into `j_mod_4`.
unchanged = wrap.reg_store(j_mod_4, j, "j_unchanged")
# Wiring to perform j-4 and j-8. Either of these will store the result in `j_mod_4`.
j_minus_4, j_mod_4 = wrap.sub_store_in_reg(j, 4, j_mod_4)
j_minus_8, j_mod_4 = wrap.sub_store_in_reg(j, 8, j_mod_4)
load_from_mems = [
# Add wiring to load the value `j_mod_4` from all of the memory cells.
# We'll have to invoke the correct one of these groups later on.
wrap.mem_load_to_mem(mems[i], j_mod_4.out, ans, 0, f"load_from_mem{i}")
for i in range(6)
]
wrap.control += [
cb.if_with(
j_lt_4,
unchanged,
cb.if_with(j_lt_8, j_minus_4, j_minus_8),
),
cb.par(
cb.if_with(
i_eq_0,
cb.if_with(
j_lt_4,
load_from_mems[0],
cb.if_with(
j_lt_8,
load_from_mems[1],
load_from_mems[2],
),
),
),
cb.if_with(
i_eq_1,
cb.if_with(
j_lt_4,
load_from_mems[3],
cb.if_with(
j_lt_8,
load_from_mems[4],
load_from_mems[5],
),
),
),
),
]
return wrap
def add_wrap3(prog):
"""Inserts the component `wrap2` into the program.
It has:
- two inputs, `i` and `j`.
- six ref memories, `mem1` through `mem6`, of size 4 each.
- one ref memory, `ans`.
The invoker wants to pretend that there are actually
_two_ memories of size _12_ each.
The invoker wants to index the memories while living in this fiction.
This component will return mem[i][j], but indexed according to the fiction.
Accordingly, we assume 0 <= i < 2 and 0 <= j < 12.
"""
wrap: cb.ComponentBuilder = prog.component("wrap3")
i = wrap.input("i", 32)
j = wrap.input("j", 32)
# Six memory cells, plus an answer cell.
mems = [wrap.comb_mem_d1(f"mem{i}", 32, 4, 32, is_ref=True) for i in range(6)]
ans = wrap.comb_mem_d1("ans", 32, 1, 32, is_ref=True)
# We will need j % 4, so we'll store it in a cell.
j_mod_4 = wrap.reg(32)
# Additional cells to compute equality, and lt
i_eq_0 = wrap.eq_use(i, 0)
i_eq_1 = wrap.eq_use(i, 1)
i_eq_2 = wrap.eq_use(i, 2)
j_lt_4 = wrap.lt_use(j, 4)
# Load `j` unchanged into `j_mod_4`.
unchanged = wrap.reg_store(j_mod_4, j, "j_unchanged")
# Wiring to perform j-4 and store the result in `j_mod_4`.
subcell, j_mod_4 = wrap.sub_store_in_reg(j, 4, j_mod_4)
emit_from_mems = [
wrap.mem_load_to_mem(mems[i], j_mod_4.out, ans, 0, f"load_from_mem{i}")
for i in range(6)
]
wrap.control += [
cb.if_with(j_lt_4, unchanged, subcell),
cb.par(
cb.if_with(
i_eq_0,
cb.if_with(j_lt_4, emit_from_mems[0], emit_from_mems[1]),
),
cb.if_with(
i_eq_1,
cb.if_with(j_lt_4, emit_from_mems[2], emit_from_mems[3]),
),
cb.if_with(
i_eq_2,
cb.if_with(j_lt_4, emit_from_mems[4], emit_from_mems[5]),
),
),
]
return wrap
def add_main(prog, wrap2, wrap3):
"""Inserts the component `main` into the program.
This will be used to `invoke` the component `wrap`.
For now, I'd like to pass it memory cells `A` and `B` by reference,
along with the inputs i = 1, j = 3.
"""
main: cb.ComponentBuilder = prog.component("main")
# Six memory cells, plus an two answer cells.
[mem_a, mem_b, mem_c, mem_d, mem_e, mem_f] = [
main.comb_mem_d1(name, 32, 4, 32, is_external=True)
for name in ["A", "B", "C", "D", "E", "F"]
]
out2 = main.comb_mem_d1("out2", 32, 1, 32, is_external=True)
out3 = main.comb_mem_d1("out3", 32, 1, 32, is_external=True)
together2 = main.cell("together2", wrap2)
together3 = main.cell("together3", wrap3)
main.control += [
cb.invoke(
together2,
in_i=cb.const(32, 1),
in_j=cb.const(32, 11),
ref_mem0=mem_a,
ref_mem1=mem_b,
ref_mem2=mem_c,
ref_mem3=mem_d,
ref_mem4=mem_e,
ref_mem5=mem_f,
ref_ans=out2,
),
cb.invoke(
together3,
in_i=cb.const(32, 2),
in_j=cb.const(32, 7),
ref_mem0=mem_a,
ref_mem1=mem_b,
ref_mem2=mem_c,
ref_mem3=mem_d,
ref_mem4=mem_e,
ref_mem5=mem_f,
ref_ans=out3,
),
]
def build():
"""Top-level function to build the program."""
prog = cb.Builder()
wrap2 = add_wrap2(prog)
wrap3 = add_wrap3(prog)
add_main(prog, wrap2, wrap3)
return prog.program
if __name__ == "__main__":
build().emit()