Skip to content

Commit 97996f9

Browse files
committed
refactor: Update RAM.v and TM.v to fix bugs and improve code readability
1 parent aa2e38c commit 97996f9

File tree

3 files changed

+73
-34
lines changed

3 files changed

+73
-34
lines changed

code/Conv.v

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,26 @@ module Conv(
88
output reg out_st
99
);
1010

11+
////////////////////////////////Load Data////////////////////////////
1112
reg start_conv, load_data_status;
12-
reg [15:0] conv_result [0:35];
1313
reg [7:0] input_feature [0:63];
14-
reg cycle ;
15-
reg [2:0] line, row;
16-
reg [15:0] temp[0:8];
1714
reg [5:0] data_counter ;
15+
////////////////////////////////////////////////////////////////////
16+
17+
////////////////////////////////Convolution/////////////////////////
18+
reg [19:0] conv_result [0:35]; // 原來是 20 bits , 但我們只取 16 bits(6,10)fixed point
19+
reg [19:0] temp[0:8];
20+
reg [2:0] line, row;
21+
reg cycle ;
22+
////////////////////////////////////////////////////////////////////
23+
1824

25+
///////////////////////////////Output///////////////////////////////
26+
reg [5:0] output_counter ;
27+
reg output_data_status;
28+
////////////////////////////////////////////////////////////////////
1929

20-
// 3x3 fixed-point kernel
30+
////////////////////////////////Fixed-point Kernel///////////////////
2131
reg [7:0] fixed_kernel [0:8] ;
2232
initial begin
2333
fixed_kernel[0] = 8'b00001000; // 0.0625 -> 00001000
@@ -29,15 +39,22 @@ initial begin
2939
fixed_kernel[6] = 8'b00001000; // 0.0625 -> 00001000
3040
fixed_kernel[7] = 8'b00010000; // 0.125 -> 00010000
3141
fixed_kernel[8] = 8'b00001000; // 0.0625 -> 00001000
32-
33-
start_conv = 1'b0;
34-
load_data_status = 1'b0;
35-
cycle = 1'b0;
36-
line = 3'b000;
37-
row = 3'b000;
38-
data_counter = 6'b000000;
39-
out_st = 1'b0;
4042
end
43+
////////////////////////////////////////////////////////////////////
44+
45+
////////////////////////////////initial/////////////////////////////
46+
initial begin
47+
start_conv <= 1'b0;
48+
load_data_status <= 1'b0;
49+
data_counter <= 6'b000000;
50+
line <= 3'b000;
51+
row <= 3'b000;
52+
cycle <= 1'b0;
53+
out_st <= 1'b0;
54+
output_counter <= 6'b000000;
55+
output_data_status <= 1'b0;
56+
end
57+
////////////////////////////////////////////////////////////////////
4158

4259
always @(posedge clk) begin
4360

@@ -48,6 +65,16 @@ always @(posedge clk) begin
4865
data_counter <= 6'b000000;
4966
load_data_status <= 1'b0;
5067
start_conv <= 1'b1;
68+
$display("Input Feature Get From RAM: ");
69+
$display(" %b %b %b %b %b %b %b %b", input_feature[0], input_feature[1], input_feature[2], input_feature[3], input_feature[4], input_feature[5], input_feature[6], input_feature[7]);
70+
$display(" %b %b %b %b %b %b %b %b", input_feature[8], input_feature[9], input_feature[10], input_feature[11], input_feature[12], input_feature[13], input_feature[14], input_feature[15]);
71+
$display(" %b %b %b %b %b %b %b %b", input_feature[16], input_feature[17], input_feature[18], input_feature[19], input_feature[20], input_feature[21], input_feature[22], input_feature[23]);
72+
$display(" %b %b %b %b %b %b %b %b", input_feature[24], input_feature[25], input_feature[26], input_feature[27], input_feature[28], input_feature[29], input_feature[30], input_feature[31]);
73+
$display(" %b %b %b %b %b %b %b %b", input_feature[32], input_feature[33], input_feature[34], input_feature[35], input_feature[36], input_feature[37], input_feature[38], input_feature[39]);
74+
$display(" %b %b %b %b %b %b %b %b", input_feature[40], input_feature[41], input_feature[42], input_feature[43], input_feature[44], input_feature[45], input_feature[46], input_feature[47]);
75+
$display(" %b %b %b %b %b %b %b %b", input_feature[48], input_feature[49], input_feature[50], input_feature[51], input_feature[52], input_feature[53], input_feature[54], input_feature[55]);
76+
$display(" %b %b %b %b %b %b %b %b", input_feature[56], input_feature[57], input_feature[58], input_feature[59], input_feature[60], input_feature[61], input_feature[62], input_feature[63]);
77+
5178
end
5279
else begin
5380
data_counter <= data_counter + 1;
@@ -64,18 +91,19 @@ end
6491
always @ (posedge clk) begin
6592
if (start_conv == 1'b1) begin
6693
if (cycle == 1) begin
67-
if (row < 6) begin // 檢查 row 的範圍
94+
if (row < 5) begin // 檢查 row 的範圍
6895
row <= row + 1;
6996
end
7097
else begin
7198
row <= 0;
72-
if (line < 6) begin // 檢查 line 的範圍
99+
if (line < 5) begin // 檢查 line 的範圍
73100
line <= line + 1;
74101
end
75102
else begin
76103
line <= 0;
77104
start_conv <= 1'b0;
78105
out_st <= 1'b1;
106+
output_data_status <= 1'b1;
79107
end
80108
end
81109
cycle <= 0;
@@ -104,12 +132,13 @@ end
104132

105133
// Output the result 6x6 matrix
106134
always @(posedge clk) begin
107-
if ( out_st == 1'b1 ) begin
108-
dout = conv_result[data_counter] ;
109-
data_counter <= data_counter + 1;
110-
if (data_counter == 35) begin
111-
data_counter <= 6'b000000;
112-
out_st <= 0;
135+
if ( output_data_status == 1'b1 ) begin
136+
dout = conv_result[output_counter] ;
137+
output_counter <= output_counter + 1;
138+
out_st <= 1'b0;
139+
if (output_counter == 36) begin // 6x6 matrix
140+
output_counter <= 6'b000000;
141+
output_data_status <= 1'b0;
113142
end
114143
end
115144
end

code/RAM.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ always @(posedge clk) begin
1616
if(~wr) begin //read data
1717
dout <= ram_data[address];
1818
end
19-
else begin
19+
else begin
2020
dout <= 8'd0;
2121
ram_data[address] <= din;
2222
end

code/TM.v

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ Conv moduleConv(
3838
/////////////////////////////
3939

4040

41-
reg [5:0] write_ram_counter;
41+
reg [6:0] write_ram_counter;
4242
reg [15:0] conv_result[0:35];
4343
reg [5:0] output_counter ;
4444
reg [7:0] fixed_matrix [0:63];
4545

46+
reg output_data_status;
47+
4648
initial begin
4749
clk = 0;
4850
in_st = 1'bx; // reset
@@ -124,10 +126,11 @@ initial begin
124126

125127
#(CLK_PERIOD*66)
126128

127-
in_st <= 1 ;
129+
#(CLK_PERIOD) in_st <= 1 ;
130+
address <= 6'b000000;
128131

129-
#(CLK_PERIOD) address <= 6'b000000;
130132
#(CLK_PERIOD) address <= 6'b000001;
133+
in_st <= 1'b0;
131134
#(CLK_PERIOD) address <= 6'b000010;
132135
#(CLK_PERIOD) address <= 6'b000011;
133136
#(CLK_PERIOD) address <= 6'b000100;
@@ -191,28 +194,30 @@ initial begin
191194
#(CLK_PERIOD) address <= 6'b111110;
192195
#(CLK_PERIOD) address <= 6'b111111;
193196

194-
in_st <= 0 ;
195-
196197

198+
197199
#(CLK_PERIOD*300) $display("finished");
198200
$stop;
199201
end
200202

201203
always @(posedge clk) begin
202204
if ( wr == 1'b1 ) begin
203-
address = write_ram_counter;
204-
ram_din = fixed_matrix[write_ram_counter];
205-
write_ram_counter <= write_ram_counter + 1;
206-
if (write_ram_counter == 63) begin // 8x8 matrix
205+
address <= write_ram_counter;
206+
ram_din <= fixed_matrix[write_ram_counter];
207+
208+
write_ram_counter <= write_ram_counter + 1;
209+
210+
if (write_ram_counter == 64) begin // 8x8 matrix
207211
address <= 6'b000000;
208212
write_ram_counter <= 6'b000000;
209213
wr <= 1'b0;
210214
end
211215
end
212-
else if ( out_st == 1'b1 ) begin
213-
conv_result[output_counter] = dout ;
216+
217+
else if ( output_data_status == 1'b1 ) begin
218+
conv_result[output_counter] <= dout ;
214219
output_counter <= output_counter + 1;
215-
if (output_counter == 35) begin // 6x6 matrix
220+
if (output_counter == 36) begin // 6x6 matrix
216221
output_counter <= 6'b000000;
217222
$display("Conv Result:");
218223
$display(" %b %b %b %b %b %b", conv_result[0], conv_result[1], conv_result[2], conv_result[3], conv_result[4], conv_result[5]);
@@ -221,8 +226,13 @@ always @(posedge clk) begin
221226
$display(" %b %b %b %b %b %b", conv_result[18], conv_result[19], conv_result[20], conv_result[21], conv_result[22], conv_result[23]);
222227
$display(" %b %b %b %b %b %b", conv_result[24], conv_result[25], conv_result[26], conv_result[27], conv_result[28], conv_result[29]);
223228
$display(" %b %b %b %b %b %b", conv_result[30], conv_result[31], conv_result[32], conv_result[33], conv_result[34], conv_result[35]);
229+
output_data_status = 1'b0;
224230
end
225231
end
232+
233+
else if ( out_st == 1'b1 ) begin
234+
output_data_status = 1'b1;
235+
end
226236
end
227237

228238
endmodule

0 commit comments

Comments
 (0)