-
Notifications
You must be signed in to change notification settings - Fork 0
/
rggen_mux.veryl
61 lines (53 loc) · 1.33 KB
/
rggen_mux.veryl
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
pub module rggen_mux #(
param TYPE: type = logic,
param N: u32 = 1
)(
i_select: input logic<N>,
i_data: input TYPE<N>,
o_data: output TYPE
) {
const WIDTH: u32 = $bits(TYPE);
const DEPTH: u32 = $clog2(N);
function reduce_or(
data: input logic<N, WIDTH>
) -> logic<WIDTH> {
var current_n: u32;
var current_data: logic<N, WIDTH>;
var next_n: u32;
var next_data: logic<N, WIDTH>;
next_n = N;
next_data = data;
for _i: u32 in 0..DEPTH {
current_n = next_n;
current_data = next_data;
next_n = (current_n / 2) + (current_n % 2);
for j: u32 in 0..next_n {
if ((j + 1) == next_n) && ((current_n % 2) == 1) {
next_data[j] = current_data[2*j+0];
} else {
next_data[j] = current_data[2*j+0] | current_data[2*j+1];
}
}
}
return next_data[0];
}
function mux(
select: input logic<N>,
data: input TYPE<N>
) -> TYPE {
if N >= 2 {
var masked_data: logic<N, WIDTH>;
var result: logic<WIDTH>;
for i: u32 in 0..N {
masked_data[i] = {select[i] repeat WIDTH} & data[i];
}
result = reduce_or(masked_data);
return result as TYPE;
} else {
return data[0];
}
}
always_comb {
o_data = mux(i_select, i_data);
}
}