-
Notifications
You must be signed in to change notification settings - Fork 10
/
Std.v
318 lines (266 loc) · 9.4 KB
/
Std.v
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
(*! Stdlib | Standard library !*)
Require Import Koika.Frontend.
Section Maybe.
Context (tau: type).
Definition Maybe :=
{| struct_name := "maybe_" ++ type_id tau;
struct_fields := [("valid", bits_t 1); ("data", tau)] |}.
Definition valid {reg_t fn} : UInternalFunction reg_t fn :=
{{ fun valid (x: tau) : struct_t Maybe =>
struct Maybe { valid := Ob~1; data := x } }}.
Definition invalid {reg_t fn} : UInternalFunction reg_t fn :=
{{ fun invalid () : struct_t Maybe =>
struct Maybe { valid := Ob~0 } }}.
End Maybe.
Notation maybe tau := (struct_t (Maybe tau)).
Module Type Fifo.
Parameter T:type.
End Fifo.
Module Fifo1 (f: Fifo).
Import f.
Inductive reg_t := data0 | valid0.
Definition R r :=
match r with
| data0 => T
| valid0 => bits_t 1
end.
Definition r idx : R idx :=
match idx with
| data0 => value_of_bits Bits.zero
| valid0 => Bits.zero
end.
Definition name_reg r :=
match r with
| data0 => "data0"
| valid0 => "valid0"
end.
Definition can_enq : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun can_enq () : bits_t 1 => !read1(valid0) }}.
Definition enq : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun enq (data : T) : bits_t 0 =>
guard (can_enq ());
write1(data0, data);
write1(valid0, #Ob~1) }}.
Definition can_deq : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun can_deq () : bits_t 1 => read0(valid0) }}.
Definition peek : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun peek () : maybe T =>
if can_deq () then {valid T}(read0(data0))
else {invalid T}() }}.
Definition deq : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun deq () : T =>
guard (can_deq ());
write0(valid0, Ob~0);
read0(data0) }}.
Instance FiniteType_reg_t : FiniteType reg_t := _.
End Fifo1.
Module Fifo1Bypass (f: Fifo).
Import f.
Inductive reg_t := data0 | valid0.
Definition R r :=
match r with
| data0 => T
| valid0 => bits_t 1
end.
Definition r idx : R idx :=
match idx with
| data0 => value_of_bits Bits.zero
| valid0 => Bits.zero
end.
Definition name_reg r :=
match r with
| data0 => "data0"
| valid0 => "valid0"
end.
Definition can_enq : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun can_enq () : bits_t 1 => !read0(valid0) }}.
Definition enq : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun enq (data : T) : bits_t 0 =>
guard (can_enq ());
write0(data0, data);
write0(valid0, #Ob~1) }}.
Definition can_deq : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun can_deq () : bits_t 1 => read1(valid0) }}.
Definition peek : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun peek () : maybe T =>
if can_deq () then {valid T}(read1(data0))
else {invalid T}() }}.
Definition deq : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun deq () : T =>
guard (can_deq ());
write1(valid0, Ob~0);
read1(data0) }}.
Instance FiniteType_reg_t : FiniteType reg_t := _.
End Fifo1Bypass.
Module Type RfPow2_sig.
Parameter idx_sz: nat.
Parameter T: type.
Parameter init: T.
Parameter read_style : @switch_style var_t.
Parameter write_style : @switch_style var_t.
End RfPow2_sig.
Module RfPow2 (s: RfPow2_sig).
Definition sz := pow2 s.idx_sz.
Inductive reg_t := rData (n: Vect.index sz).
Definition R r :=
match r with
| rData _ => s.T
end.
Definition r idx : R idx :=
match idx with
| rData _ => s.init
end.
Definition name_reg r :=
match r with
| rData n => String.append "rData_" (show n)
end.
Definition read_0 : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun read_0 (idx : bits_t s.idx_sz) : s.T =>
`UCompleteSwitch s.read_style s.idx_sz "idx"
(fun idx => {{ read0(rData idx) }})` }}.
Definition write_0 : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun write_0 (idx : bits_t s.idx_sz) (val: s.T) : unit_t =>
`UCompleteSwitch s.write_style s.idx_sz "idx"
(fun idx => {{ write0(rData idx, val) }})` }}.
Definition read_1 : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun read_1 (idx : bits_t s.idx_sz) : s.T =>
`UCompleteSwitch s.read_style s.idx_sz "idx"
(fun idx => {{ read1(rData idx) }})` }}.
Definition write_1 : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun write_1 (idx : bits_t s.idx_sz) (val: s.T) : unit_t =>
`UCompleteSwitch s.write_style s.idx_sz "idx"
(fun idx => {{ write1(rData idx, val) }})` }}.
End RfPow2.
Module Type Rf_sig.
Parameter lastIdx: nat.
Parameter T: type.
Parameter init: T.
End Rf_sig.
Module Rf (s: Rf_sig).
Definition lastIdx := s.lastIdx.
Definition log_sz := log2 lastIdx.
Definition sz := S lastIdx.
Inductive reg_t := rData (n: Vect.index sz).
Definition R r :=
match r with
| rData _ => s.T
end.
Definition r idx : R idx :=
match idx with
| rData _ => s.init
end.
Definition name_reg r :=
match r with
| rData n => String.append "rData_" (show n)
end.
Definition read : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun read (idx : bits_t log_sz) : s.T =>
`USugar
(USwitch
{{idx}}
{{fail(type_sz s.T)}}
(List.map
(fun idx =>
(USugar (UConstBits
(Bits.of_nat log_sz idx)),
{{ read0(rData (match (index_of_nat sz idx) with
| Some idx => idx
| _ => thisone
end)) }}))
(List.seq 0 sz))) ` }}.
Definition write : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun write (idx : bits_t log_sz) (val: s.T) : unit_t =>
`USugar
(USwitch
{{idx}}
{{fail}}
(List.map
(fun idx =>
(USugar (UConstBits
(Bits.of_nat log_sz idx)),
{{ write0(rData (match (index_of_nat sz idx) with
| Some idx => idx
| _ => thisone
end), val) }}))
(List.seq 0 sz))) ` }}.
End Rf.
Definition signExtend {reg_t} (n:nat) (m:nat) : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun signExtend (arg : bits_t n) : bits_t (m+n) => sext(arg, m + n) }}.
Module RfEhr (s: Rf_sig).
Definition lastIdx := s.lastIdx.
Definition log_sz := log2 lastIdx.
Definition sz := S lastIdx.
Inductive reg_t := rData (n: Vect.index sz).
Definition R r :=
match r with
| rData _ => s.T
end.
Definition r idx : R idx :=
match idx with
| rData _ => s.init
end.
Definition name_reg r :=
match r with
| rData n => String.append "rData_" (show n)
end.
Definition read_0 : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun read_0 (idx : bits_t log_sz) : s.T =>
`USugar
(USwitch
{{idx}}
{{fail(type_sz s.T)}}
(List.map
(fun idx =>
(USugar (UConstBits
(Bits.of_nat log_sz idx)),
{{ read0(rData (match (index_of_nat sz idx) with
| Some idx => idx
| _ => thisone
end)) }}))
(List.seq 0 sz))) ` }}.
Definition read_1 : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun read_1 (idx : bits_t log_sz) : s.T =>
`USugar
(USwitch
{{idx}}
{{fail(type_sz s.T)}}
(List.map
(fun idx =>
(USugar (UConstBits
(Bits.of_nat log_sz idx)),
{{ read1(rData (match (index_of_nat sz idx) with
| Some idx => idx
| _ => thisone
end)) }}))
(List.seq 0 sz))) ` }}.
Definition write_0 : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun write_0 (idx : bits_t log_sz) (val: s.T) : unit_t =>
`USugar
(USwitch
{{idx}}
{{fail}}
(List.map
(fun idx =>
(USugar (UConstBits
(Bits.of_nat log_sz idx)),
{{ write0(rData (match (index_of_nat sz idx) with
| Some idx => idx
| _ => thisone
end), val) }}))
(List.seq 0 sz))) ` }}.
Definition write_1 : UInternalFunction reg_t empty_ext_fn_t :=
{{ fun write_1 (idx : bits_t log_sz) (val: s.T) : unit_t =>
`USugar
(USwitch
{{idx}}
{{fail}}
(List.map
(fun idx =>
(USugar (UConstBits
(Bits.of_nat log_sz idx)),
{{ write1(rData (match (index_of_nat sz idx) with
| Some idx => idx
| _ => thisone
end), val) }}))
(List.seq 0 sz))) ` }}.
End RfEhr.