-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathmath.futil
79 lines (76 loc) · 1.66 KB
/
math.futil
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
import "core.futil";
import "binary_operators.futil";
extern "math.sv" {
// Fixed point square root operator, using a digit-by-digit algorithm.
// en.wikipedia.org/wiki/Methods_of_computing_square_roots#Digit-by-digit_calculation
primitive fp_sqrt[
WIDTH, INT_WIDTH, FRAC_WIDTH
](
@clk clk: 1,
@reset reset: 1,
@write_together(1) @go go: 1,
@write_together(1) in: WIDTH
) -> (
@stable out: WIDTH,
@done done: 1
);
primitive sqrt[
WIDTH
](
@clk clk: 1,
@reset reset: 1,
@write_together(1) @go go: 1,
@write_together(1) in: WIDTH
) -> (
@stable out: WIDTH,
@done done: 1
);
}
// Computes the unsigned value b^e, where
// b is the `base` and e is the `exp`.
component pow(base: 32, exp: 32) -> (out: 32) {
cells {
t = std_reg(32);
count = std_reg(32);
mul = std_mult_pipe(32);
lt = std_lt(32);
incr = std_add(32);
}
wires {
group init<"static"=1> {
t.in = 32'd1;
t.write_en = 1'd1;
count.in = 32'd0;
count.write_en = 1'd1;
init[done] = t.done & count.done ? 1'd1;
}
group do_mul {
mul.left = base;
mul.right = t.out;
mul.go = !mul.done ? 1'd1;
t.in = mul.out;
t.write_en = mul.done;
do_mul[done] = t.done;
}
group incr_count<"static"=1> {
incr.left = 32'd1;
incr.right = count.out;
count.in = incr.out;
count.write_en = 1'd1;
incr_count[done] = count.done;
}
comb group cond {
lt.right = exp;
lt.left = count.out;
}
out = t.out;
}
control {
seq {
init;
while lt.out with cond {
par { do_mul; incr_count; }
}
}
}
}