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
|
`timescale 1ns / 1ps
module tb_wrapper;
//
// Test Vectors
//
`include "modexp_fpga_model_vectors.v";
/*
* Settings
*/
localparam USE_OPERAND_ADDR_WIDTH = 7;
localparam USE_SYSTOLIC_ARRAY_POWER = 1;
/*
* Clock (100 MHz)
*/
reg clk;
initial clk = 1'b0;
always #5 clk = ~clk;
/*
* Reset
*/
reg rst_n;
/*
* Access Bus
*/
reg bus_cs;
reg bus_we;
reg [USE_OPERAND_ADDR_WIDTH+3:0] bus_addr;
reg [ 32-1:0] bus_wr_data;
wire [ 32-1:0] bus_rd_data;
modexpa7_wrapper #
(
.OPERAND_ADDR_WIDTH (USE_OPERAND_ADDR_WIDTH),
.SYSTOLIC_ARRAY_POWER (USE_SYSTOLIC_ARRAY_POWER)
)
uut
(
.clk (clk),
.rst_n (rst_n),
.cs (bus_cs),
.we (bus_we),
.address (bus_addr),
.write_data (bus_wr_data),
.read_data (bus_rd_data)
);
integer i;
reg [31: 0] tmp;
reg [383:0] shreg;
reg poll;
initial begin
//
rst_n = 0;
//
bus_cs = 0;
bus_we = 0;
bus_addr = 'bX;
bus_wr_data = 'bX;
//
#200;
//
rst_n = 1;
//
// read common registers to make sure core header reads out ok
//
read_reg('h00, tmp); // NAME0
read_reg('h01, tmp); // NAME1
read_reg('h02, tmp); // VERSION
//
read_reg('h13, tmp); // BUFFER_BITS
read_reg('h14, tmp); // ARRAY_BITS
//
write_reg('h12, 32'd384); // EXPONENT_BITS
read_reg ('h12, tmp);
//
write_reg('h11, 32'd384); // MODULUS_BITS
read_reg ('h11, tmp);
//
write_reg('h10, 32'd0); // MODE
read_reg ('h10, tmp);
//
// fill in 384-bit modulus
//
shreg = N_384;
for (i=0; i<384/32; i=i+1) begin
write_bank(3'b000, i[USE_OPERAND_ADDR_WIDTH-1:0], shreg[31:0]);
shreg = shreg >> 32;
end
//
// start precomputation
//
write_reg('h08, 32'd0); // CONTROL.init = 0
write_reg('h08, 32'd1); // CONTROL.init = 1
//
// wait for precomputation to complete
//
poll = 1;
while (poll) begin
#10;
read_reg('h09, tmp); // tmp = STATUS
poll = ~tmp[0]; // poll = STATUS.ready
end
//
// move modulus-dependent coefficient and Montgomery factor
// from "output" to "input" banks
//
for (i=0; i<384/32; i=i+1) begin
read_bank (3'b100, i[USE_OPERAND_ADDR_WIDTH-1:0], tmp);
write_bank(3'b101, i[USE_OPERAND_ADDR_WIDTH-1:0], tmp);
read_bank (3'b110, i[USE_OPERAND_ADDR_WIDTH-1:0], tmp);
write_bank(3'b111, i[USE_OPERAND_ADDR_WIDTH-1:0], tmp);
end
//
// fill in 384-bit message
//
shreg = M_384;
for (i=0; i<384/32; i=i+1) begin
write_bank(3'b001, i[USE_OPERAND_ADDR_WIDTH-1:0], shreg[31:0]);
shreg = shreg >> 32;
end
//
// fill in 384-bit exponent
//
shreg = D_384;
for (i=0; i<384/32; i=i+1) begin
write_bank(3'b010, i[USE_OPERAND_ADDR_WIDTH-1:0], shreg[31:0]);
shreg = shreg >> 32;
end
//
// start exponentiation
//
write_reg('h08, 32'd0); // CONTROL.next = 0
write_reg('h08, 32'd2); // CONTROL.next = 1
//
// wait for exponentiation to complete
//
poll = 1;
while (poll) begin
#10;
read_reg('h09, tmp); // tmp = STATUS
poll = ~tmp[1]; // poll = STATUS.valid
end
//
// read result
//
for (i=0; i<384/32; i=i+1) begin
read_bank(3'b011, i[USE_OPERAND_ADDR_WIDTH-1:0], tmp);
shreg = {tmp, shreg[383:32]};
end
//
end
task read_reg;
input [USE_OPERAND_ADDR_WIDTH+2:0] addr;
output [ 32-1:0] data;
begin
bus_cs = 1;
bus_addr = {1'b0, addr};
#10;
bus_cs = 0;
bus_addr = 'bX;
data = bus_rd_data;
end
endtask
task read_bank;
input [ 2:0] bank;
input [USE_OPERAND_ADDR_WIDTH-1:0] addr;
output [ 32-1:0] data;
begin
bus_cs = 1;
bus_addr = {1'b1, bank, addr};
#10;
bus_cs = 0;
bus_addr = 'bX;
data = bus_rd_data;
end
endtask
task write_reg;
input [USE_OPERAND_ADDR_WIDTH+2:0] addr;
input [ 32-1:0] data;
begin
bus_cs = 1;
bus_we = 1;
bus_addr = {1'b0, addr};
bus_wr_data = data;
#10;
bus_cs = 0;
bus_we = 0;
bus_addr = 'bX;
end
endtask
task write_bank;
input [ 2:0] bank;
input [USE_OPERAND_ADDR_WIDTH-1:0] addr;
input [ 32-1:0] data;
begin
bus_cs = 1;
bus_we = 1;
bus_addr = {1'b1, bank, addr};
bus_wr_data = data;
#10;
bus_cs = 0;
bus_we = 0;
bus_addr = 'bX;
end
endtask
endmodule
|