-
Notifications
You must be signed in to change notification settings - Fork 2
/
svm_kernal.v
295 lines (271 loc) · 7.53 KB
/
svm_kernal.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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 18:43:06 12/04/2017
// Design Name:
// Module Name: svm_kernal
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module svm_kernal(
input clk_svm,
input rst_n,
input ddr_test_end,
output reg square_sign,
output reg [31:0] square_data,
output reg [15:0] square_cnt,
output reg busy_reverse,
output busy_flag,
output reg feature_full_falg,
input ddr_end,
input [31:0] sample_kernel,
input [15:0] num_dim,
input [31:0] data_rd_x,
input [31:0] data_rd_y,
input [31:0] data_rd_z,
input [31:0] data_rd_w,
input ker_cal_sign //calculate start sign
);
parameter IN_WIDTH = 32;
parameter OUT_WIDTH = 32;
reg [15:0] features_num;
reg [31:0] A_one;
reg [31:0] A_two;
reg [31:0] B_one;
reg [31:0] B_two;
reg [31:0] C_one;
reg [31:0] C_two;
reg [31:0] D_one;
reg [31:0] D_two;
reg start_A;
reg start_B;
reg start_C;
reg start_D;
reg [31:0] x [0:1];
reg [31:0] y [0:1];
reg [7:0] i;
reg [7:0] j;
reg [31:0] sum_reg;
reg busy_flag1;
//reg busy_reverse;
reg sum_flag;//
reg [31:0] sum_cnt;
////////////////////////exp
reg on_enable;
reg [31:0] x_one_reg;
reg [31:0] x_two_reg;
reg [31:0] x_reg;
reg enable;
reg e_busy_reverse;
reg busy_e_flag;
wire busy_e;
wire [31:0] out_k;
////////////////////////
wire [31:0] data_one;
wire busy_one;
wire [31:0] data_two;
wire busy_two;
wire [31:0] data_three;
wire busy_three;
wire [31:0] data_four;
wire busy_four;
reg svm_en;
wire svm_enable;
assign svm_enable = svm_en;//busy_reverse;//ddr_test_end;
//wire busy_flag;
assign busy_flag = sum_flag;
reg [3:0] busy_reverse_cnt;
reg ker_cal_falg;
//ddr-->x,y,z
/////////////////////////////////////////////
always @(posedge clk_svm or negedge rst_n)
begin
if(!rst_n)
begin
i <= 8'd0;
j <= 8'd1;//0
sum_reg <= 32'd0;
sum_flag <= 1'b0;
sum_cnt <= 32'd0;//
features_num <= 16'd4;
///////////////////
start_A <= 1'b0;
start_B <= 1'b0;
start_C <= 1'b0;
start_D <= 1'b0;
A_one <= 32'b0;
A_two <= 32'b0;
B_one <= 32'b0;
B_two <= 32'b0;
C_one <= 32'b0;
C_two <= 32'b0;
D_one <= 32'b0;
D_two <= 32'b0;
//////////////////
busy_flag1 <= 1'b1;//0
busy_reverse <= 1'b0;//1
busy_reverse_cnt <= 4'd0;
busy_e_flag <= 1'b0;
on_enable <= 1'b0;
enable <= 1'b0;
e_busy_reverse <= 1'b1;
x_reg <= 32'd0;
x_one_reg <= 32'd0;
x_two_reg <= 32'd0;
ker_cal_falg <= 1'b0;
svm_en <= 1'b0;
//////////////////output
square_sign <= 1'b0;
square_data <= 32'd0;
square_cnt <= 16'd0;
feature_full_falg <= 1'b0;
end
else begin
if(ddr_end == 1'b1)
begin
//--ker_cal_falg <= ker_cal_sign;
if(sum_cnt <= sample_kernel)//20,3239
begin
//if(busy_reverse == 1'b1 && ker_cal_sign == 1'b1)
ker_cal_falg <= ker_cal_sign;
if(!ker_cal_falg && ker_cal_sign)//
//if(svm_en == 1'b1)
begin
A_one <= {16'd0,data_rd_x[31:16]} - {16'd0,data_rd_z[31:16]};//{16'd0,data_inx[31:16]};//
A_two <= {16'd0,data_rd_x[31:16]} - {16'd0,data_rd_z[31:16]};//{16'd0,data_iny[31:16]};//
B_one <= {16'd0,data_rd_x[15:0]} - {16'd0,data_rd_z[15:0]};//{16'd0,data_inx[15:0]};//
B_two <= {16'd0,data_rd_x[15:0]} - {16'd0,data_rd_z[15:0]};//{16'd0,data_iny[15:0]};//
C_one <= {16'd0,data_rd_y[31:16]} - {16'd0,data_rd_w[31:16]};
C_two <= {16'd0,data_rd_y[31:16]} - {16'd0,data_rd_w[31:16]};
D_one <= {16'd0,data_rd_y[15:0]} - {16'd0,data_rd_w[15:0]};
D_two <= {16'd0,data_rd_y[15:0]} - {16'd0,data_rd_w[15:0]};
busy_reverse <= 1'b1;
busy_reverse_cnt <= 0;
svm_en <= 1'b1;
end
end
else begin
sum_flag <= 1'b1;
busy_reverse <= 1'b0;
end
if(busy_reverse == 1'b1)
begin
start_A <= 1'b1;
start_B <= 1'b1;
start_C <= 1'b1;
start_D <= 1'b1;
end
if(square_sign == 1'b1)
begin
if(busy_reverse_cnt <= 9)
begin
busy_reverse_cnt <= busy_reverse_cnt + 1;
end
else begin
square_sign <= 1'b0;
end
end
busy_flag1 <= busy_one;
if(busy_flag1 && !busy_one)
begin
sum_cnt <= sum_cnt + 1;//
start_A <= 1'b0;
start_B <= 1'b0;
start_C <= 1'b0;
start_D <= 1'b0;
on_enable <= 1'b1;////
busy_reverse <= 1'b0;
svm_en <= 1'b0;
feature_full_falg <= (features_num == num_dim)?0:1;//features_num+4 == 4
x_one_reg <= data_one + data_two;//*0.5
x_two_reg <= data_three + data_four;//*0.5
end
if(on_enable == 1'b1)
begin
if(feature_full_falg == 0)
begin
enable <= 1'b1;
on_enable <= 1'b0;
x_reg <= x_reg + ((x_one_reg + x_two_reg)>>1);
end
else begin
on_enable <= 1'b0;
features_num <= features_num + 4;
feature_full_falg <= 1'b0;
x_reg <= x_reg + ((x_one_reg + x_two_reg)>>1);
end
end
busy_e_flag <= busy_e;
if(busy_e_flag && !busy_e)//
begin
x_reg <= 0;
enable <= 1'b0;
square_data <= out_k;///.
square_cnt <= sum_cnt;///.
square_sign <= 1'b1;///.
end
end
end
end//always
/////////////////////////////////////////////////////div,mul
exp_top exp_K(
.clk(clk_svm),
.rst_n(rst_n),
.sum_a(x_reg),//input
.enable(enable),//input
.svm_enable(1'b1),//svm_enable
.out_b(out_k),//output
.busy_e(busy_e)//output
);
mul_one mul_one_data(
.clk(clk_svm),
.rst_n(rst_n),
.mul_a(A_one),
.mul_b(A_two),
.start(start_A),// start sign
.svm_enable(svm_enable),
.busy_one(busy_one),//output mul_one state
.data_one(data_one)//output result one
);
mul_two mul_two_data(
.clk(clk_svm),
.rst_n(rst_n),
.mul_a(B_one),
.mul_b(B_two),//input mul_one result
.start(start_B),//input mul_one state
.svm_enable(svm_enable),
.busy_two(busy_two),//output mul_two state
.data_two(data_two)//output SUM one
);
mul_three mul_three_data(
.clk(clk_svm),
.rst_n(rst_n),
.mul_a(C_one),
.mul_b(C_two),
.start(start_C),// start sign
.svm_enable(svm_enable),
.busy_three(busy_three),//output mul_three state
.data_three(data_three)//output result three
);
mul_four mul_four_data(
.clk(clk_svm),
.rst_n(rst_n),
.mul_a(D_one),
.mul_b(D_two),
.start(start_D),// start sign
.svm_enable(svm_enable),
.busy_four(busy_four),//output mul_three state
.data_four(data_four)//output result three
);
endmodule