Verilog generator for RNS mod 2^n-1 adder
Choose n for module 2
n
-1 (from 3 to 128):
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
Simple implementation with double addition and comparison:
// Sum modulo (2^3 - 1) = 7 module sum_modulo_7 (in1, in2, out); input [2:0] in1; input [2:0] in2; output reg [2:0] out; wire [3:0] data; wire [3:0] data2; assign data = in1 + in2; assign data2 = in1 + in2 + 1; always @(*) begin if (data2[3] == 1) out <= data2[2:0]; else out <= data[2:0]; end endmodule
Test Bench:
module atest_bench(); reg [2:0] in1; reg [2:0] in2; wire [2:0] out; integer i, j, l, m, t; reg dummy; wire complete; integer fori, forj; sum_modulo_7 sm1(in1, in2, out); initial begin for (fori = 0; fori < 7; fori = fori + 1) begin for (forj = 0; forj < 7; forj = forj + 1) begin in1 = fori; in2 = forj; m = (fori + forj) % 7; #1 dummy = 1; $display ("!!! IN1=(%d) IN2=(%d) Res=(%d) Expect=(%d)", fori, forj, out, m); l = out; if (l != m) begin $display ("!!! Error (%d, %d)!!!", l, m); end #1 dummy = 1; end end end endmodule
Main page