SAIFIINDUSTRIES commited on
Commit
a088a35
·
verified ·
1 Parent(s): ca60100

Add Repo: zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16

Browse files
Files changed (40) hide show
  1. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/AES_DEC.v +85 -0
  2. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/AES_ENC.v +85 -0
  3. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/DecCore.v +74 -0
  4. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/EncCore.v +74 -0
  5. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/InvMixColumns.v +83 -0
  6. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/InvSubBytes.v +68 -0
  7. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/MixColumns.v +73 -0
  8. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/README.md +1 -0
  9. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/SubBytes.v +68 -0
  10. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/aes.c +6 -0
  11. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/aes.h +88 -0
  12. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/aes_selftest.c +60 -0
  13. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/LeNet.c +6 -0
  14. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/LeNet.h +81 -0
  15. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/LeNet_selftest.c +60 -0
  16. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/README.md +1 -0
  17. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/complete.v +116 -0
  18. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/conv_1.v +278 -0
  19. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/conv_2.v +275 -0
  20. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/fc_1.v +272 -0
  21. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/fc_2.v +276 -0
  22. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/lenet_top.v +583 -0
  23. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/max_pool.v +39 -0
  24. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/multi_add.v +72 -0
  25. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/pool_1.v +189 -0
  26. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/pool_2.v +189 -0
  27. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/relu_1.v +31 -0
  28. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/README.md +4 -0
  29. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/README.md +1 -0
  30. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/bram_top.v +212 -0
  31. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/conv.v +555 -0
  32. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/fc.v +252 -0
  33. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/max_pool.v +38 -0
  34. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/mult_add.v +73 -0
  35. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/pool.v +198 -0
  36. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/relu.v +31 -0
  37. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/vgg16.c +6 -0
  38. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/vgg16.h +85 -0
  39. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/vgg16_selftest.c +60 -0
  40. zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/vgg16_top.v +790 -0
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/AES_DEC.v ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/05/2017 04:35:07 AM
7
+ // Design Name:
8
+ // Module Name: AES_DEC
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+
23
+ module AES_DEC(
24
+ Din, Key, Dout, Drdy, Krdy, RSTn, EN, CLK, BSY, Dvld
25
+ );
26
+ input [127:0] Din; // Data input
27
+ input [127:0] Key; // Key input
28
+ output [127:0] Dout; // Data output
29
+ input Drdy; // Data input ready
30
+ input Krdy; // Key input ready
31
+ input RSTn; // Reset (Low active)
32
+ input EN; // AES circuit enable
33
+ input CLK; // System clock
34
+ output BSY; // Busy signal
35
+ output Dvld; // Data output valid
36
+
37
+ reg [127:0] Drg; // Data register
38
+ reg [127:0] Krg; // Key register
39
+ reg [127:0] KrgX; // Temporary key Register
40
+ reg [9:0] Rrg; // Round counter
41
+ reg Dvldrg, BSYrg;
42
+ wire [127:0] Dnext, Knext;
43
+
44
+ DecCore DC (Drg, KrgX, Rrg, Dnext, Knext);
45
+
46
+ assign Dvld = Dvldrg;
47
+ assign Dout = Drg;
48
+ assign BSY = BSYrg;
49
+
50
+ always @(posedge CLK) begin
51
+ if (RSTn == 0) begin
52
+ Rrg <= 10'b1000000000;
53
+ Dvldrg <= 0;
54
+ BSYrg <= 0;
55
+ end
56
+ else if (EN == 1) begin
57
+ if (BSYrg == 0) begin
58
+ if (Krdy == 1) begin
59
+ Krg <= Key;
60
+ KrgX <= Key;
61
+ Dvldrg <= 0;
62
+ end
63
+ else if (Drdy == 1) begin
64
+ KrgX <= Knext;
65
+ Drg <= Din ^ Krg;
66
+ Rrg <= {Rrg[0], Rrg[9:1]};
67
+ Dvldrg <= 0;
68
+ BSYrg <= 1;
69
+ end
70
+ end
71
+ else begin
72
+ Drg <= Dnext;
73
+ if (Rrg[9] == 1) begin
74
+ KrgX <= Krg;
75
+ Dvldrg <= 1;
76
+ BSYrg <= 0;
77
+ end
78
+ else begin
79
+ Rrg <= {Rrg[0], Rrg[9:1]};
80
+ KrgX <= Knext;
81
+ end
82
+ end
83
+ end
84
+ end
85
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/AES_ENC.v ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/05/2017 04:34:04 AM
7
+ // Design Name:
8
+ // Module Name: AES_ENC
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+
23
+ module AES_ENC(
24
+ Din, Key, Dout, Drdy, Krdy, RSTn, EN, CLK, BSY, Dvld
25
+ );
26
+ input [127:0] Din; // Data input
27
+ input [127:0] Key; // Key input
28
+ output [127:0] Dout; // Data output
29
+ input Drdy; // Data input ready
30
+ input Krdy; // Key input ready
31
+ input RSTn; // Reset (Low active)
32
+ input EN; // AES circuit enable
33
+ input CLK; // System clock
34
+ output BSY; // Busy signal
35
+ output Dvld; // Data output valid
36
+
37
+ reg [127:0] Drg; // Data register
38
+ reg [127:0] Krg; // Key register
39
+ reg [127:0] KrgX; // Temporary key Register
40
+ reg [9:0] Rrg; // Round counter
41
+ reg Dvldrg, BSYrg;
42
+ wire [127:0] Dnext, Knext;
43
+
44
+ EncCore EC (Drg, KrgX, Rrg, Dnext, Knext);
45
+
46
+ assign Dvld = Dvldrg;
47
+ assign Dout = Drg;
48
+ assign BSY = BSYrg;
49
+
50
+ always @(posedge CLK) begin
51
+ if (RSTn == 0) begin
52
+ Rrg <= 10'b0000000001;
53
+ Dvldrg <= 0;
54
+ BSYrg <= 0;
55
+ end
56
+ else if (EN == 1) begin
57
+ if (BSYrg == 0) begin
58
+ if (Krdy == 1) begin
59
+ Krg <= Key;
60
+ KrgX <= Key;
61
+ Dvldrg <= 0;
62
+ end
63
+ else if (Drdy == 1) begin
64
+ Rrg <= {Rrg[8:0], Rrg[9]};
65
+ KrgX <= Knext;
66
+ Drg <= Din ^ Krg;
67
+ Dvldrg <= 0;
68
+ BSYrg <= 1;
69
+ end
70
+ end
71
+ else begin
72
+ Drg <= Dnext;
73
+ if (Rrg[0] == 1) begin
74
+ KrgX <= Krg;
75
+ Dvldrg <= 1;
76
+ BSYrg <= 0;
77
+ end
78
+ else begin
79
+ Rrg <= {Rrg[8:0], Rrg[9]};
80
+ KrgX <= Knext;
81
+ end
82
+ end
83
+ end
84
+ end
85
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/DecCore.v ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/05/2017 04:33:03 AM
7
+ // Design Name:
8
+ // Module Name: DecCore
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+
23
+ module DecCore(
24
+ di, ki, Rrg, do, ko
25
+ );
26
+ input [127:0] di;
27
+ input [127:0] ki;
28
+ input [9:0] Rrg;
29
+ output [127:0] do;
30
+ output [127:0] ko;
31
+
32
+ wire [127:0] sb, sr, mx, dx;
33
+ wire [31:0] so;
34
+
35
+ InvMixColumns MX3 (di[127:96], mx[127:96]);
36
+ InvMixColumns MX2 (di[ 95:64], mx[ 95:64]);
37
+ InvMixColumns MX1 (di[ 63:32], mx[ 63:32]);
38
+ InvMixColumns MX0 (di[ 31: 0], mx[ 31: 0]);
39
+
40
+ assign dx = (Rrg[8] == 1)? di: mx;
41
+ assign sr = {dx[127:120], dx[ 23: 16], dx[ 47: 40], dx[ 71: 64],
42
+ dx[ 95: 88], dx[119:112], dx[ 15: 8], dx[ 39: 32],
43
+ dx[ 63: 56], dx[ 87: 80], dx[111:104], dx[ 7: 0],
44
+ dx[ 31: 24], dx[ 55: 48], dx[ 79: 72], dx[103: 96]};
45
+
46
+ InvSubBytes SB3 (sr[127:96], sb[127:96]);
47
+ InvSubBytes SB2 (sr[ 95:64], sb[ 95:64]);
48
+ InvSubBytes SB1 (sr[ 63:32], sb[ 63:32]);
49
+ InvSubBytes SB0 (sr[ 31: 0], sb[ 31: 0]);
50
+
51
+ assign do = sb ^ ki;
52
+
53
+ function [7:0] rcon;
54
+ input [9:0] x;
55
+ casex (x)
56
+ 10'bxxxxxxxxx1: rcon = 8'h01;
57
+ 10'bxxxxxxxx1x: rcon = 8'h02;
58
+ 10'bxxxxxxx1xx: rcon = 8'h04;
59
+ 10'bxxxxxx1xxx: rcon = 8'h08;
60
+ 10'bxxxxx1xxxx: rcon = 8'h10;
61
+ 10'bxxxx1xxxxx: rcon = 8'h20;
62
+ 10'bxxx1xxxxxx: rcon = 8'h40;
63
+ 10'bxx1xxxxxxx: rcon = 8'h80;
64
+ 10'bx1xxxxxxxx: rcon = 8'h1b;
65
+ 10'b1xxxxxxxxx: rcon = 8'h36;
66
+ endcase
67
+ endfunction
68
+
69
+ SubBytes SBK ({ko[23:16], ko[15:8], ko[7:0], ko[31:24]}, so);
70
+ assign ko[127:96] = ki[127:96] ^ {so[31:24] ^ rcon(Rrg), so[23: 0]};
71
+ assign ko[ 95:64] = ki[ 95:64] ^ ki[127:96];
72
+ assign ko[ 63:32] = ki[ 63:32] ^ ki[ 95:64];
73
+ assign ko[ 31: 0] = ki[ 31: 0] ^ ki[ 63:32];
74
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/EncCore.v ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/05/2017 04:31:23 AM
7
+ // Design Name:
8
+ // Module Name: EncCore
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+
23
+ module EncCore(
24
+ di, ki, Rrg, do, ko
25
+ );
26
+ input [127:0] di;
27
+ input [127:0] ki;
28
+ input [9:0] Rrg;
29
+ output [127:0] do;
30
+ output [127:0] ko;
31
+
32
+ wire [127:0] sb, sr, mx;
33
+ wire [31:0] so;
34
+
35
+ SubBytes SB3 (di[127:96], sb[127:96]);
36
+ SubBytes SB2 (di[ 95:64], sb[ 95:64]);
37
+ SubBytes SB1 (di[ 63:32], sb[ 63:32]);
38
+ SubBytes SB0 (di[ 31: 0], sb[ 31: 0]);
39
+
40
+ assign sr = {sb[127:120], sb[ 87: 80], sb[ 47: 40], sb[ 7: 0],
41
+ sb[ 95: 88], sb[ 55: 48], sb[ 15: 8], sb[103: 96],
42
+ sb[ 63: 56], sb[ 23: 16], sb[111:104], sb[ 71: 64],
43
+ sb[ 31: 24], sb[119:112], sb[ 79: 72], sb[ 39: 32]};
44
+
45
+ MixColumns MX3 (sr[127:96], mx[127:96]);
46
+ MixColumns MX2 (sr[ 95:64], mx[ 95:64]);
47
+ MixColumns MX1 (sr[ 63:32], mx[ 63:32]);
48
+ MixColumns MX0 (sr[ 31: 0], mx[ 31: 0]);
49
+
50
+ assign do = ((Rrg[0] == 1)? sr: mx) ^ ki;
51
+
52
+ function [7:0] rcon;
53
+ input [9:0] x;
54
+ casex (x)
55
+ 10'bxxxxxxxxx1: rcon = 8'h01;
56
+ 10'bxxxxxxxx1x: rcon = 8'h02;
57
+ 10'bxxxxxxx1xx: rcon = 8'h04;
58
+ 10'bxxxxxx1xxx: rcon = 8'h08;
59
+ 10'bxxxxx1xxxx: rcon = 8'h10;
60
+ 10'bxxxx1xxxxx: rcon = 8'h20;
61
+ 10'bxxx1xxxxxx: rcon = 8'h40;
62
+ 10'bxx1xxxxxxx: rcon = 8'h80;
63
+ 10'bx1xxxxxxxx: rcon = 8'h1b;
64
+ 10'b1xxxxxxxxx: rcon = 8'h36;
65
+ endcase
66
+ endfunction
67
+
68
+ SubBytes SBK ({ki[23:16], ki[15:8], ki[7:0], ki[31:24]}, so);
69
+
70
+ assign ko[127:96] = ki[127:96] ^ {so[31:24] ^ rcon(Rrg), so[23: 0]};
71
+ assign ko[ 95:64] = ki[ 95:64] ^ ko[127:96];
72
+ assign ko[ 63:32] = ki[ 63:32] ^ ko[ 95:64];
73
+ assign ko[ 31: 0] = ki[ 31: 0] ^ ko[ 63:32];
74
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/InvMixColumns.v ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/05/2017 04:28:25 AM
7
+ // Design Name:
8
+ // Module Name: InvMixColumns
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+
23
+ module InvMixColumns(
24
+ x, y
25
+ );
26
+ input [31:0] x;
27
+ output [31:0] y;
28
+
29
+ wire [7:0] a3, a2, a1, a0, b3, b2, b1, b0;
30
+ wire [7:0] c3, c2, c1, c0, d3, d2, d1, d0;
31
+
32
+ assign a3 = x[31:24];
33
+ assign a2 = x[23:16];
34
+ assign a1 = x[15: 8];
35
+ assign a0 = x[ 7: 0];
36
+
37
+ assign b3 = a3 ^ a2;
38
+ assign b2 = a2 ^ a1;
39
+ assign b1 = a1 ^ a0;
40
+ assign b0 = a0 ^ a3;
41
+
42
+ assign c3 = {a2[7] ^ b1[7] ^ b3[6],
43
+ a2[6] ^ b1[6] ^ b3[5],
44
+ a2[5] ^ b1[5] ^ b3[4],
45
+ a2[4] ^ b1[4] ^ b3[3] ^ b3[7],
46
+ a2[3] ^ b1[3] ^ b3[2] ^ b3[7],
47
+ a2[2] ^ b1[2] ^ b3[1],
48
+ a2[1] ^ b1[1] ^ b3[0] ^ b3[7],
49
+ a2[0] ^ b1[0] ^ b3[7]};
50
+ assign c2 = {a3[7] ^ b1[7] ^ b2[6],
51
+ a3[6] ^ b1[6] ^ b2[5],
52
+ a3[5] ^ b1[5] ^ b2[4],
53
+ a3[4] ^ b1[4] ^ b2[3] ^ b2[7],
54
+ a3[3] ^ b1[3] ^ b2[2] ^ b2[7],
55
+ a3[2] ^ b1[2] ^ b2[1],
56
+ a3[1] ^ b1[1] ^ b2[0] ^ b2[7],
57
+ a3[0] ^ b1[0] ^ b2[7]};
58
+ assign c1 = {a0[7] ^ b3[7] ^ b1[6],
59
+ a0[6] ^ b3[6] ^ b1[5],
60
+ a0[5] ^ b3[5] ^ b1[4],
61
+ a0[4] ^ b3[4] ^ b1[3] ^ b1[7],
62
+ a0[3] ^ b3[3] ^ b1[2] ^ b1[7],
63
+ a0[2] ^ b3[2] ^ b1[1],
64
+ a0[1] ^ b3[1] ^ b1[0] ^ b1[7],
65
+ a0[0] ^ b3[0] ^ b1[7]};
66
+ assign c0 = {a1[7] ^ b3[7] ^ b0[6],
67
+ a1[6] ^ b3[6] ^ b0[5],
68
+ a1[5] ^ b3[5] ^ b0[4],
69
+ a1[4] ^ b3[4] ^ b0[3] ^ b0[7],
70
+ a1[3] ^ b3[3] ^ b0[2] ^ b0[7],
71
+ a1[2] ^ b3[2] ^ b0[1],
72
+ a1[1] ^ b3[1] ^ b0[0] ^ b0[7],
73
+ a1[0] ^ b3[0] ^ b0[7]};
74
+ assign d3 = {c3[5], c3[4], c3[3] ^ c3[7], c3[2] ^ c3[7] ^ c3[6],
75
+ c3[1] ^ c3[6], c3[0] ^ c3[7], c3[7] ^ c3[6], c3[6]};
76
+ assign d2 = {c2[5], c2[4], c2[3] ^ c2[7], c2[2] ^ c2[7] ^ c2[6],
77
+ c2[1] ^ c2[6], c2[0] ^ c2[7], c2[7] ^ c2[6], c2[6]};
78
+ assign d1 = {c1[5], c1[4], c1[3] ^ c1[7], c1[2] ^ c1[7] ^ c1[6],
79
+ c1[1] ^ c1[6], c1[0] ^ c1[7], c1[7] ^ c1[6], c1[6]};
80
+ assign d0 = {c0[5], c0[4], c0[3] ^ c0[7], c0[2] ^ c0[7] ^ c0[6],
81
+ c0[1] ^ c0[6], c0[0] ^ c0[7], c0[7] ^ c0[6], c0[6]};
82
+ assign y = {d3 ^ d1 ^ c3, d2 ^ d0 ^ c2, d3 ^ d1 ^ c1, d2 ^ d0 ^ c0};
83
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/InvSubBytes.v ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/05/2017 04:19:48 AM
7
+ // Design Name:
8
+ // Module Name: InvSubBytes
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+
23
+ module InvSubBytes(
24
+ x, y
25
+ );
26
+ input [31:0] x;
27
+ output [31:0] y;
28
+
29
+ function [7:0] S;
30
+ input [7:0] x;
31
+ case (x)
32
+ 0:S= 82; 1:S= 9; 2:S=106; 3:S=213; 4:S= 48; 5:S= 54; 6:S=165; 7:S= 56;
33
+ 8:S=191; 9:S= 64; 10:S=163; 11:S=158; 12:S=129; 13:S=243; 14:S=215; 15:S=251;
34
+ 16:S=124; 17:S=227; 18:S= 57; 19:S=130; 20:S=155; 21:S= 47; 22:S=255; 23:S=135;
35
+ 24:S= 52; 25:S=142; 26:S= 67; 27:S= 68; 28:S=196; 29:S=222; 30:S=233; 31:S=203;
36
+ 32:S= 84; 33:S=123; 34:S=148; 35:S= 50; 36:S=166; 37:S=194; 38:S= 35; 39:S= 61;
37
+ 40:S=238; 41:S= 76; 42:S=149; 43:S= 11; 44:S= 66; 45:S=250; 46:S=195; 47:S= 78;
38
+ 48:S= 8; 49:S= 46; 50:S=161; 51:S=102; 52:S= 40; 53:S=217; 54:S= 36; 55:S=178;
39
+ 56:S=118; 57:S= 91; 58:S=162; 59:S= 73; 60:S=109; 61:S=139; 62:S=209; 63:S= 37;
40
+ 64:S=114; 65:S=248; 66:S=246; 67:S=100; 68:S=134; 69:S=104; 70:S=152; 71:S= 22;
41
+ 72:S=212; 73:S=164; 74:S= 92; 75:S=204; 76:S= 93; 77:S=101; 78:S=182; 79:S=146;
42
+ 80:S=108; 81:S=112; 82:S= 72; 83:S= 80; 84:S=253; 85:S=237; 86:S=185; 87:S=218;
43
+ 88:S= 94; 89:S= 21; 90:S= 70; 91:S= 87; 92:S=167; 93:S=141; 94:S=157; 95:S=132;
44
+ 96:S=144; 97:S=216; 98:S=171; 99:S= 0; 100:S=140; 101:S=188; 102:S=211; 103:S= 10;
45
+ 104:S=247; 105:S=228; 106:S= 88; 107:S= 5; 108:S=184; 109:S=179; 110:S= 69; 111:S= 6;
46
+ 112:S=208; 113:S= 44; 114:S= 30; 115:S=143; 116:S=202; 117:S= 63; 118:S= 15; 119:S= 2;
47
+ 120:S=193; 121:S=175; 122:S=189; 123:S= 3; 124:S= 1; 125:S= 19; 126:S=138; 127:S=107;
48
+ 128:S= 58; 129:S=145; 130:S= 17; 131:S= 65; 132:S= 79; 133:S=103; 134:S=220; 135:S=234;
49
+ 136:S=151; 137:S=242; 138:S=207; 139:S=206; 140:S=240; 141:S=180; 142:S=230; 143:S=115;
50
+ 144:S=150; 145:S=172; 146:S=116; 147:S= 34; 148:S=231; 149:S=173; 150:S= 53; 151:S=133;
51
+ 152:S=226; 153:S=249; 154:S= 55; 155:S=232; 156:S= 28; 157:S=117; 158:S=223; 159:S=110;
52
+ 160:S= 71; 161:S=241; 162:S= 26; 163:S=113; 164:S= 29; 165:S= 41; 166:S=197; 167:S=137;
53
+ 168:S=111; 169:S=183; 170:S= 98; 171:S= 14; 172:S=170; 173:S= 24; 174:S=190; 175:S= 27;
54
+ 176:S=252; 177:S= 86; 178:S= 62; 179:S= 75; 180:S=198; 181:S=210; 182:S=121; 183:S= 32;
55
+ 184:S=154; 185:S=219; 186:S=192; 187:S=254; 188:S=120; 189:S=205; 190:S= 90; 191:S=244;
56
+ 192:S= 31; 193:S=221; 194:S=168; 195:S= 51; 196:S=136; 197:S= 7; 198:S=199; 199:S= 49;
57
+ 200:S=177; 201:S= 18; 202:S= 16; 203:S= 89; 204:S= 39; 205:S=128; 206:S=236; 207:S= 95;
58
+ 208:S= 96; 209:S= 81; 210:S=127; 211:S=169; 212:S= 25; 213:S=181; 214:S= 74; 215:S= 13;
59
+ 216:S= 45; 217:S=229; 218:S=122; 219:S=159; 220:S=147; 221:S=201; 222:S=156; 223:S=239;
60
+ 224:S=160; 225:S=224; 226:S= 59; 227:S= 77; 228:S=174; 229:S= 42; 230:S=245; 231:S=176;
61
+ 232:S=200; 233:S=235; 234:S=187; 235:S= 60; 236:S=131; 237:S= 83; 238:S=153; 239:S= 97;
62
+ 240:S= 23; 241:S= 43; 242:S= 4; 243:S=126; 244:S=186; 245:S=119; 246:S=214; 247:S= 38;
63
+ 248:S=225; 249:S=105; 250:S= 20; 251:S= 99; 252:S= 85; 253:S= 33; 254:S= 12; 255:S=125;
64
+ endcase
65
+ endfunction
66
+
67
+ assign y = {S(x[31:24]), S(x[23:16]), S(x[15: 8]), S(x[ 7: 0])};
68
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/MixColumns.v ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/05/2017 04:23:29 AM
7
+ // Design Name:
8
+ // Module Name: MixColumns
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+
23
+ module MixColumns(
24
+ x, y
25
+ );
26
+ input [31:0] x;
27
+ output [31:0] y;
28
+
29
+ wire [7:0] a3, a2, a1, a0, b3, b2, b1, b0;
30
+
31
+ assign a3 = x[31:24];
32
+ assign a2 = x[23:16];
33
+ assign a1 = x[15: 8];
34
+ assign a0 = x[ 7: 0];
35
+
36
+ assign b3 = a3 ^ a2;
37
+ assign b2 = a2 ^ a1;
38
+ assign b1 = a1 ^ a0;
39
+ assign b0 = a0 ^ a3;
40
+
41
+ assign y = {a2[7] ^ b1[7] ^ b3[6],
42
+ a2[6] ^ b1[6] ^ b3[5],
43
+ a2[5] ^ b1[5] ^ b3[4],
44
+ a2[4] ^ b1[4] ^ b3[3] ^ b3[7],
45
+ a2[3] ^ b1[3] ^ b3[2] ^ b3[7],
46
+ a2[2] ^ b1[2] ^ b3[1],
47
+ a2[1] ^ b1[1] ^ b3[0] ^ b3[7],
48
+ a2[0] ^ b1[0] ^ b3[7],
49
+ a3[7] ^ b1[7] ^ b2[6],
50
+ a3[6] ^ b1[6] ^ b2[5],
51
+ a3[5] ^ b1[5] ^ b2[4],
52
+ a3[4] ^ b1[4] ^ b2[3] ^ b2[7],
53
+ a3[3] ^ b1[3] ^ b2[2] ^ b2[7],
54
+ a3[2] ^ b1[2] ^ b2[1],
55
+ a3[1] ^ b1[1] ^ b2[0] ^ b2[7],
56
+ a3[0] ^ b1[0] ^ b2[7],
57
+ a0[7] ^ b3[7] ^ b1[6],
58
+ a0[6] ^ b3[6] ^ b1[5],
59
+ a0[5] ^ b3[5] ^ b1[4],
60
+ a0[4] ^ b3[4] ^ b1[3] ^ b1[7],
61
+ a0[3] ^ b3[3] ^ b1[2] ^ b1[7],
62
+ a0[2] ^ b3[2] ^ b1[1],
63
+ a0[1] ^ b3[1] ^ b1[0] ^ b1[7],
64
+ a0[0] ^ b3[0] ^ b1[7],
65
+ a1[7] ^ b3[7] ^ b0[6],
66
+ a1[6] ^ b3[6] ^ b0[5],
67
+ a1[5] ^ b3[5] ^ b0[4],
68
+ a1[4] ^ b3[4] ^ b0[3] ^ b0[7],
69
+ a1[3] ^ b3[3] ^ b0[2] ^ b0[7],
70
+ a1[2] ^ b3[2] ^ b0[1],
71
+ a1[1] ^ b3[1] ^ b0[0] ^ b0[7],
72
+ a1[0] ^ b3[0] ^ b0[7]};
73
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/README.md ADDED
@@ -0,0 +1 @@
 
 
1
+
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/SubBytes.v ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/05/2017 04:10:53 AM
7
+ // Design Name:
8
+ // Module Name: SubBytes
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+
23
+ module SubBytes(
24
+ x, y
25
+ );
26
+ input [31:0] x;
27
+ output [31:0] y;
28
+
29
+ function [7:0] S;
30
+ input [7:0] x;
31
+ case (x)
32
+ 0:S= 99; 1:S=124; 2:S=119; 3:S=123; 4:S=242; 5:S=107; 6:S=111; 7:S=197;
33
+ 8:S= 48; 9:S= 1; 10:S=103; 11:S= 43; 12:S=254; 13:S=215; 14:S=171; 15:S=118;
34
+ 16:S=202; 17:S=130; 18:S=201; 19:S=125; 20:S=250; 21:S= 89; 22:S= 71; 23:S=240;
35
+ 24:S=173; 25:S=212; 26:S=162; 27:S=175; 28:S=156; 29:S=164; 30:S=114; 31:S=192;
36
+ 32:S=183; 33:S=253; 34:S=147; 35:S= 38; 36:S= 54; 37:S= 63; 38:S=247; 39:S=204;
37
+ 40:S= 52; 41:S=165; 42:S=229; 43:S=241; 44:S=113; 45:S=216; 46:S= 49; 47:S= 21;
38
+ 48:S= 4; 49:S=199; 50:S= 35; 51:S=195; 52:S= 24; 53:S=150; 54:S= 5; 55:S=154;
39
+ 56:S= 7; 57:S= 18; 58:S=128; 59:S=226; 60:S=235; 61:S= 39; 62:S=178; 63:S=117;
40
+ 64:S= 9; 65:S=131; 66:S= 44; 67:S= 26; 68:S= 27; 69:S=110; 70:S= 90; 71:S=160;
41
+ 72:S= 82; 73:S= 59; 74:S=214; 75:S=179; 76:S= 41; 77:S=227; 78:S= 47; 79:S=132;
42
+ 80:S= 83; 81:S=209; 82:S= 0; 83:S=237; 84:S= 32; 85:S=252; 86:S=177; 87:S= 91;
43
+ 88:S=106; 89:S=203; 90:S=190; 91:S= 57; 92:S= 74; 93:S= 76; 94:S= 88; 95:S=207;
44
+ 96:S=208; 97:S=239; 98:S=170; 99:S=251; 100:S= 67; 101:S= 77; 102:S= 51; 103:S=133;
45
+ 104:S= 69; 105:S=249; 106:S= 2; 107:S=127; 108:S= 80; 109:S= 60; 110:S=159; 111:S=168;
46
+ 112:S= 81; 113:S=163; 114:S= 64; 115:S=143; 116:S=146; 117:S=157; 118:S= 56; 119:S=245;
47
+ 120:S=188; 121:S=182; 122:S=218; 123:S= 33; 124:S= 16; 125:S=255; 126:S=243; 127:S=210;
48
+ 128:S=205; 129:S= 12; 130:S= 19; 131:S=236; 132:S= 95; 133:S=151; 134:S= 68; 135:S= 23;
49
+ 136:S=196; 137:S=167; 138:S=126; 139:S= 61; 140:S=100; 141:S= 93; 142:S= 25; 143:S=115;
50
+ 144:S= 96; 145:S=129; 146:S= 79; 147:S=220; 148:S= 34; 149:S= 42; 150:S=144; 151:S=136;
51
+ 152:S= 70; 153:S=238; 154:S=184; 155:S= 20; 156:S=222; 157:S= 94; 158:S= 11; 159:S=219;
52
+ 160:S=224; 161:S= 50; 162:S= 58; 163:S= 10; 164:S= 73; 165:S= 6; 166:S= 36; 167:S= 92;
53
+ 168:S=194; 169:S=211; 170:S=172; 171:S= 98; 172:S=145; 173:S=149; 174:S=228; 175:S=121;
54
+ 176:S=231; 177:S=200; 178:S= 55; 179:S=109; 180:S=141; 181:S=213; 182:S= 78; 183:S=169;
55
+ 184:S=108; 185:S= 86; 186:S=244; 187:S=234; 188:S=101; 189:S=122; 190:S=174; 191:S= 8;
56
+ 192:S=186; 193:S=120; 194:S= 37; 195:S= 46; 196:S= 28; 197:S=166; 198:S=180; 199:S=198;
57
+ 200:S=232; 201:S=221; 202:S=116; 203:S= 31; 204:S= 75; 205:S=189; 206:S=139; 207:S=138;
58
+ 208:S=112; 209:S= 62; 210:S=181; 211:S=102; 212:S= 72; 213:S= 3; 214:S=246; 215:S= 14;
59
+ 216:S= 97; 217:S= 53; 218:S= 87; 219:S=185; 220:S=134; 221:S=193; 222:S= 29; 223:S=158;
60
+ 224:S=225; 225:S=248; 226:S=152; 227:S= 17; 228:S=105; 229:S=217; 230:S=142; 231:S=148;
61
+ 232:S=155; 233:S= 30; 234:S=135; 235:S=233; 236:S=206; 237:S= 85; 238:S= 40; 239:S=223;
62
+ 240:S=140; 241:S=161; 242:S=137; 243:S= 13; 244:S=191; 245:S=230; 246:S= 66; 247:S=104;
63
+ 248:S= 65; 249:S=153; 250:S= 45; 251:S= 15; 252:S=176; 253:S= 84; 254:S=187; 255:S= 22;
64
+ endcase
65
+ endfunction
66
+
67
+ assign y = {S(x[31:24]), S(x[23:16]), S(x[15: 8]), S(x[ 7: 0])};
68
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/aes.c ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+
2
+
3
+ /***************************** Include Files *******************************/
4
+ #include "aes.h"
5
+
6
+ /************************** Function Definitions ***************************/
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/aes.h ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #ifndef AES_H
3
+ #define AES_H
4
+
5
+
6
+ /****************** Include Files ********************/
7
+ #include "xil_types.h"
8
+ #include "xstatus.h"
9
+
10
+ #define AES_S00_AXI_SLV_REG0_OFFSET 0
11
+ #define AES_S00_AXI_SLV_REG1_OFFSET 4
12
+ #define AES_S00_AXI_SLV_REG2_OFFSET 8
13
+ #define AES_S00_AXI_SLV_REG3_OFFSET 12
14
+ #define AES_S00_AXI_SLV_REG4_OFFSET 16
15
+ #define AES_S00_AXI_SLV_REG5_OFFSET 20
16
+ #define AES_S00_AXI_SLV_REG6_OFFSET 24
17
+ #define AES_S00_AXI_SLV_REG7_OFFSET 28
18
+ #define AES_S00_AXI_SLV_REG8_OFFSET 32
19
+ #define AES_S00_AXI_SLV_REG9_OFFSET 36
20
+ #define AES_S00_AXI_SLV_REG10_OFFSET 40
21
+ #define AES_S00_AXI_SLV_REG11_OFFSET 44
22
+ #define AES_S00_AXI_SLV_REG12_OFFSET 48
23
+
24
+
25
+ /**************************** Type Definitions *****************************/
26
+ /**
27
+ *
28
+ * Write a value to a AES register. A 32 bit write is performed.
29
+ * If the component is implemented in a smaller width, only the least
30
+ * significant data is written.
31
+ *
32
+ * @param BaseAddress is the base address of the AESdevice.
33
+ * @param RegOffset is the register offset from the base to write to.
34
+ * @param Data is the data written to the register.
35
+ *
36
+ * @return None.
37
+ *
38
+ * @note
39
+ * C-style signature:
40
+ * void AES_mWriteReg(u32 BaseAddress, unsigned RegOffset, u32 Data)
41
+ *
42
+ */
43
+ #define AES_mWriteReg(BaseAddress, RegOffset, Data) \
44
+ Xil_Out32((BaseAddress) + (RegOffset), (u32)(Data))
45
+
46
+ /**
47
+ *
48
+ * Read a value from a AES register. A 32 bit read is performed.
49
+ * If the component is implemented in a smaller width, only the least
50
+ * significant data is read from the register. The most significant data
51
+ * will be read as 0.
52
+ *
53
+ * @param BaseAddress is the base address of the AES device.
54
+ * @param RegOffset is the register offset from the base to write to.
55
+ *
56
+ * @return Data is the data from the register.
57
+ *
58
+ * @note
59
+ * C-style signature:
60
+ * u32 AES_mReadReg(u32 BaseAddress, unsigned RegOffset)
61
+ *
62
+ */
63
+ #define AES_mReadReg(BaseAddress, RegOffset) \
64
+ Xil_In32((BaseAddress) + (RegOffset))
65
+
66
+ /************************** Function Prototypes ****************************/
67
+ /**
68
+ *
69
+ * Run a self-test on the driver/device. Note this may be a destructive test if
70
+ * resets of the device are performed.
71
+ *
72
+ * If the hardware system is not built correctly, this function may never
73
+ * return to the caller.
74
+ *
75
+ * @param baseaddr_p is the base address of the AES instance to be worked on.
76
+ *
77
+ * @return
78
+ *
79
+ * - XST_SUCCESS if all self-test code passed
80
+ * - XST_FAILURE if any self-test code failed
81
+ *
82
+ * @note Caching must be turned off for this function to work.
83
+ * @note Self test may fail if data memory and device are not on the same bus.
84
+ *
85
+ */
86
+ XStatus AES_Reg_SelfTest(void * baseaddr_p);
87
+
88
+ #endif // AES_H
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/AES/aes_selftest.c ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ /***************************** Include Files *******************************/
3
+ #include "aes.h"
4
+ #include "xparameters.h"
5
+ #include "stdio.h"
6
+ #include "xil_io.h"
7
+
8
+ /************************** Constant Definitions ***************************/
9
+ #define READ_WRITE_MUL_FACTOR 0x10
10
+
11
+ /************************** Function Definitions ***************************/
12
+ /**
13
+ *
14
+ * Run a self-test on the driver/device. Note this may be a destructive test if
15
+ * resets of the device are performed.
16
+ *
17
+ * If the hardware system is not built correctly, this function may never
18
+ * return to the caller.
19
+ *
20
+ * @param baseaddr_p is the base address of the AESinstance to be worked on.
21
+ *
22
+ * @return
23
+ *
24
+ * - XST_SUCCESS if all self-test code passed
25
+ * - XST_FAILURE if any self-test code failed
26
+ *
27
+ * @note Caching must be turned off for this function to work.
28
+ * @note Self test may fail if data memory and device are not on the same bus.
29
+ *
30
+ */
31
+ XStatus AES_Reg_SelfTest(void * baseaddr_p)
32
+ {
33
+ u32 baseaddr;
34
+ int write_loop_index;
35
+ int read_loop_index;
36
+ int Index;
37
+
38
+ baseaddr = (u32) baseaddr_p;
39
+
40
+ xil_printf("******************************\n\r");
41
+ xil_printf("* User Peripheral Self Test\n\r");
42
+ xil_printf("******************************\n\n\r");
43
+
44
+ /*
45
+ * Write to user logic slave module register(s) and read back
46
+ */
47
+ xil_printf("User logic slave module test...\n\r");
48
+
49
+ for (write_loop_index = 0 ; write_loop_index < 4; write_loop_index++)
50
+ AES_mWriteReg (baseaddr, write_loop_index*4, (write_loop_index+1)*READ_WRITE_MUL_FACTOR);
51
+ for (read_loop_index = 0 ; read_loop_index < 4; read_loop_index++)
52
+ if ( AES_mReadReg (baseaddr, read_loop_index*4) != (read_loop_index+1)*READ_WRITE_MUL_FACTOR){
53
+ xil_printf ("Error reading register value at address %x\n", (int)baseaddr + read_loop_index*4);
54
+ return XST_FAILURE;
55
+ }
56
+
57
+ xil_printf(" - slave register write/read passed\n\n\r");
58
+
59
+ return XST_SUCCESS;
60
+ }
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/LeNet.c ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+
2
+
3
+ /***************************** Include Files *******************************/
4
+ #include "LeNet.h"
5
+
6
+ /************************** Function Definitions ***************************/
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/LeNet.h ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #ifndef LENET_H
3
+ #define LENET_H
4
+
5
+
6
+ /****************** Include Files ********************/
7
+ #include "xil_types.h"
8
+ #include "xstatus.h"
9
+
10
+ #define LENET_S00_AXI_SLV_REG0_OFFSET 0
11
+ #define LENET_S00_AXI_SLV_REG1_OFFSET 4
12
+ #define LENET_S00_AXI_SLV_REG2_OFFSET 8
13
+ #define LENET_S00_AXI_SLV_REG3_OFFSET 12
14
+ #define LENET_S00_AXI_SLV_REG4_OFFSET 16
15
+ #define LENET_S00_AXI_SLV_REG5_OFFSET 20
16
+
17
+
18
+ /**************************** Type Definitions *****************************/
19
+ /**
20
+ *
21
+ * Write a value to a LENET register. A 32 bit write is performed.
22
+ * If the component is implemented in a smaller width, only the least
23
+ * significant data is written.
24
+ *
25
+ * @param BaseAddress is the base address of the LENETdevice.
26
+ * @param RegOffset is the register offset from the base to write to.
27
+ * @param Data is the data written to the register.
28
+ *
29
+ * @return None.
30
+ *
31
+ * @note
32
+ * C-style signature:
33
+ * void LENET_mWriteReg(u32 BaseAddress, unsigned RegOffset, u32 Data)
34
+ *
35
+ */
36
+ #define LENET_mWriteReg(BaseAddress, RegOffset, Data) \
37
+ Xil_Out32((BaseAddress) + (RegOffset), (u32)(Data))
38
+
39
+ /**
40
+ *
41
+ * Read a value from a LENET register. A 32 bit read is performed.
42
+ * If the component is implemented in a smaller width, only the least
43
+ * significant data is read from the register. The most significant data
44
+ * will be read as 0.
45
+ *
46
+ * @param BaseAddress is the base address of the LENET device.
47
+ * @param RegOffset is the register offset from the base to write to.
48
+ *
49
+ * @return Data is the data from the register.
50
+ *
51
+ * @note
52
+ * C-style signature:
53
+ * u32 LENET_mReadReg(u32 BaseAddress, unsigned RegOffset)
54
+ *
55
+ */
56
+ #define LENET_mReadReg(BaseAddress, RegOffset) \
57
+ Xil_In32((BaseAddress) + (RegOffset))
58
+
59
+ /************************** Function Prototypes ****************************/
60
+ /**
61
+ *
62
+ * Run a self-test on the driver/device. Note this may be a destructive test if
63
+ * resets of the device are performed.
64
+ *
65
+ * If the hardware system is not built correctly, this function may never
66
+ * return to the caller.
67
+ *
68
+ * @param baseaddr_p is the base address of the LENET instance to be worked on.
69
+ *
70
+ * @return
71
+ *
72
+ * - XST_SUCCESS if all self-test code passed
73
+ * - XST_FAILURE if any self-test code failed
74
+ *
75
+ * @note Caching must be turned off for this function to work.
76
+ * @note Self test may fail if data memory and device are not on the same bus.
77
+ *
78
+ */
79
+ XStatus LENET_Reg_SelfTest(void * baseaddr_p);
80
+
81
+ #endif // LENET_H
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/LeNet_selftest.c ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ /***************************** Include Files *******************************/
3
+ #include "LeNet.h"
4
+ #include "xparameters.h"
5
+ #include "stdio.h"
6
+ #include "xil_io.h"
7
+
8
+ /************************** Constant Definitions ***************************/
9
+ #define READ_WRITE_MUL_FACTOR 0x10
10
+
11
+ /************************** Function Definitions ***************************/
12
+ /**
13
+ *
14
+ * Run a self-test on the driver/device. Note this may be a destructive test if
15
+ * resets of the device are performed.
16
+ *
17
+ * If the hardware system is not built correctly, this function may never
18
+ * return to the caller.
19
+ *
20
+ * @param baseaddr_p is the base address of the LENETinstance to be worked on.
21
+ *
22
+ * @return
23
+ *
24
+ * - XST_SUCCESS if all self-test code passed
25
+ * - XST_FAILURE if any self-test code failed
26
+ *
27
+ * @note Caching must be turned off for this function to work.
28
+ * @note Self test may fail if data memory and device are not on the same bus.
29
+ *
30
+ */
31
+ XStatus LENET_Reg_SelfTest(void * baseaddr_p)
32
+ {
33
+ u32 baseaddr;
34
+ int write_loop_index;
35
+ int read_loop_index;
36
+ int Index;
37
+
38
+ baseaddr = (u32) baseaddr_p;
39
+
40
+ xil_printf("******************************\n\r");
41
+ xil_printf("* User Peripheral Self Test\n\r");
42
+ xil_printf("******************************\n\n\r");
43
+
44
+ /*
45
+ * Write to user logic slave module register(s) and read back
46
+ */
47
+ xil_printf("User logic slave module test...\n\r");
48
+
49
+ for (write_loop_index = 0 ; write_loop_index < 4; write_loop_index++)
50
+ LENET_mWriteReg (baseaddr, write_loop_index*4, (write_loop_index+1)*READ_WRITE_MUL_FACTOR);
51
+ for (read_loop_index = 0 ; read_loop_index < 4; read_loop_index++)
52
+ if ( LENET_mReadReg (baseaddr, read_loop_index*4) != (read_loop_index+1)*READ_WRITE_MUL_FACTOR){
53
+ xil_printf ("Error reading register value at address %x\n", (int)baseaddr + read_loop_index*4);
54
+ return XST_FAILURE;
55
+ }
56
+
57
+ xil_printf(" - slave register write/read passed\n\n\r");
58
+
59
+ return XST_SUCCESS;
60
+ }
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/README.md ADDED
@@ -0,0 +1 @@
 
 
1
+
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/complete.v ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/02/2018 02:18:44 AM
7
+ // Design Name:
8
+ // Module Name: complete
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define OUTPUT_NODE 10
23
+ `define DATA_SIZE 8
24
+
25
+ module complete(
26
+ clk, rst, complete_en, result_bram_douta,
27
+ result_bram_ena, result_bram_addra,
28
+ result,
29
+ complete_finish
30
+ );
31
+ input clk;
32
+ input rst;
33
+ input complete_en;
34
+ input [`DATA_SIZE-1:0] result_bram_douta;
35
+ output reg result_bram_ena;
36
+ output reg [14:0] result_bram_addra;
37
+ output reg [`DATA_SIZE*`OUTPUT_NODE-1:0] result;
38
+ output reg complete_finish;
39
+
40
+ parameter fc2_result_base = 18900;
41
+
42
+ integer count = 0,
43
+ circle = 0,
44
+ data_begin = 0;
45
+
46
+ reg [2:0] state;
47
+ //reg [`DATA_SIZE*`OUTPUT_NODE-1:0] tempresult;
48
+ //assign result = tempresult;
49
+
50
+ parameter S_IDLE = 3'b100,
51
+ S_CHECK = 3'b010,
52
+ S_LOAD = 3'b001;
53
+
54
+ always@(posedge clk)
55
+ begin
56
+ if(rst == 1'b1)
57
+ begin
58
+ result_bram_ena <= 1'b0;
59
+ circle <= 0;
60
+ count <= 0;
61
+ state <= S_IDLE;
62
+ end
63
+ else
64
+ begin
65
+ if(complete_en == 1'b1)
66
+ begin
67
+ case(state)
68
+ S_IDLE:
69
+ begin
70
+ circle <= 0;
71
+ count <= 0;
72
+ complete_finish <= 1'b0;
73
+ state <= S_CHECK;
74
+ end
75
+ S_CHECK:
76
+ begin
77
+ if(count == `OUTPUT_NODE)
78
+ begin
79
+ count <= 0;
80
+ circle <= 0;
81
+ complete_finish <= 1'b1;
82
+ state <= S_IDLE;
83
+ end
84
+ else
85
+ begin
86
+ circle <= 0;
87
+ state <= S_LOAD;
88
+ end
89
+ end
90
+ S_LOAD:
91
+ begin
92
+ if(circle == 0)
93
+ begin
94
+ result_bram_ena <= 1'b1;
95
+ result_bram_addra <= fc2_result_base + count;
96
+ circle <= circle + 1;
97
+ end
98
+ else if(circle == 3)
99
+ begin
100
+ data_begin = `DATA_SIZE * (`OUTPUT_NODE - count) - 1;
101
+ result[data_begin-:8] <= result_bram_douta;
102
+ count <= count + 1;
103
+ circle <= 0;
104
+ result_bram_ena <= 1'b0;
105
+ state <= S_CHECK;
106
+ end
107
+ else
108
+ begin
109
+ circle <= circle + 1;
110
+ end
111
+ end
112
+ endcase
113
+ end
114
+ end
115
+ end
116
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/conv_1.v ADDED
@@ -0,0 +1,278 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/30/2017 05:44:43 PM
7
+ // Design Name:
8
+ // Module Name: conv_1
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define INPUT_NODE 784
23
+ `define IMAGE_SIZE 28
24
+ `define NUM_CHANNELS 1
25
+ `define CONV1_DEEP 20
26
+ `define CONV1_SIZE 5
27
+ `define CONV1_OUTPUT 24
28
+ `define DATA_SIZE 8
29
+
30
+ module conv_1(
31
+ clk, rst, conv_1_en, bias_weights_bram_douta, input_bram_douta, graph,
32
+ bias_weights_bram_ena, bias_weights_bram_addra,
33
+ result_bram_ena, result_bram_wea, result_bram_addra, result_bram_dina,
34
+ input_bram_ena, input_bram_addra,
35
+ conv_1_finish
36
+ );
37
+ input clk;
38
+ input rst;
39
+ input conv_1_en;
40
+ input [`DATA_SIZE-1:0] bias_weights_bram_douta;
41
+ input [`DATA_SIZE-1:0] input_bram_douta;
42
+ input [4:0] graph;
43
+ output reg bias_weights_bram_ena;
44
+ output reg [18:0] bias_weights_bram_addra;
45
+ output reg result_bram_ena;
46
+ output reg result_bram_wea;
47
+ output reg [14:0] result_bram_addra;
48
+ output reg [`DATA_SIZE-1:0] result_bram_dina;
49
+ output reg input_bram_ena;
50
+ output reg [12:0] input_bram_addra;
51
+ output reg conv_1_finish;
52
+
53
+ //reg [4:0] filter;
54
+ //reg [4:0] row;
55
+ //reg [4:0] column;
56
+
57
+ integer filter = 0,
58
+ channel = 0,
59
+ row = 0,
60
+ column = 0,
61
+ count = 0;
62
+
63
+ reg [`DATA_SIZE*`CONV1_SIZE*`CONV1_SIZE-1:0] matrix1;
64
+ reg [`DATA_SIZE*`CONV1_SIZE*`CONV1_SIZE-1:0] matrix2;
65
+ reg [`DATA_SIZE-1:0] bias;
66
+ reg relu_1_en;
67
+ wire [`DATA_SIZE-1:0] dout;
68
+ reg [`DATA_SIZE-1:0] result;
69
+
70
+ multi_add conv_1_ma(
71
+ .matrix1(matrix1),
72
+ .matrix2(matrix2),
73
+ .bias(bias),
74
+ .relu_1_en(relu_1_en),
75
+ .dout(dout)
76
+ );
77
+
78
+ reg [6:0] state;
79
+
80
+ parameter S_IDLE = 7'b1000000,
81
+ S_CHECK = 7'b0100000,
82
+ S_LOAD_WEIGHTS = 7'b0010000,
83
+ S_LOAD_BIAS = 7'b0001000,
84
+ S_LOAD_DATA = 7'b0000100,
85
+ S_CONVOLUTE = 7'b0000010,
86
+ S_STORE_RESULT = 7'b0000001;
87
+
88
+ integer data_begin = 0;
89
+ integer circle = 0;
90
+
91
+ parameter conv1_weights_base = 0,
92
+ conv1_bias_base = 430500,
93
+ conv1_result_base = 0;
94
+
95
+ always@(posedge clk)
96
+ begin
97
+ if(rst == 1'b1)
98
+ begin
99
+ state <= S_IDLE;
100
+ bias_weights_bram_ena <= 1'b0;
101
+ result_bram_ena <= 1'b0;
102
+ result_bram_wea <= 1'b0;
103
+ input_bram_ena <= 1'b0;
104
+ end
105
+ else
106
+ begin
107
+ if(conv_1_en == 1'b1)
108
+ begin
109
+ case(state)
110
+ S_IDLE:
111
+ begin
112
+ filter <= 0;
113
+ channel <= 0;
114
+ row <= 0;
115
+ column <= 0;
116
+ matrix1 <= 0;
117
+ matrix2 <= 0;
118
+ bias <= 0;
119
+ relu_1_en <= 1'b0;
120
+ result <= 0;
121
+ conv_1_finish <= 1'b0;
122
+ state <= S_CHECK;
123
+ end
124
+ S_CHECK:
125
+ begin
126
+ if(filter == `CONV1_DEEP)
127
+ begin
128
+ bias_weights_bram_ena <= 1'b0;
129
+ result_bram_ena <= 1'b0;
130
+ result_bram_wea <= 1'b0;
131
+ input_bram_ena <= 1'b0;
132
+ conv_1_finish <= 1'b1;
133
+ state <= S_IDLE;
134
+ end
135
+ else
136
+ begin
137
+ circle <= 0;
138
+ count <= 0;
139
+ state <= S_LOAD_WEIGHTS;
140
+ end
141
+ end
142
+ S_LOAD_WEIGHTS:
143
+ begin
144
+ if(count < `CONV1_SIZE*`CONV1_SIZE)
145
+ begin
146
+ if(circle == 0)
147
+ begin
148
+ bias_weights_bram_ena <= 1'b1;
149
+ bias_weights_bram_addra <= conv1_weights_base + count;
150
+ circle <= circle + 1;
151
+ end
152
+ else if(circle == 3)
153
+ begin
154
+ data_begin = `DATA_SIZE * (`CONV1_SIZE * `CONV1_SIZE - count) - 1;
155
+ matrix2[data_begin-:8] <= bias_weights_bram_douta;
156
+ count <= count + 1;
157
+ circle <= 0;
158
+ end
159
+ else
160
+ begin
161
+ circle <= circle + 1;
162
+ end
163
+ end
164
+ else
165
+ begin
166
+ circle <= 0;
167
+ count <= 0;
168
+ bias_weights_bram_ena <= 1'b0;
169
+ state <= S_LOAD_BIAS;
170
+ end
171
+ end
172
+ S_LOAD_BIAS:
173
+ begin
174
+ if(circle == 0)
175
+ begin
176
+ bias_weights_bram_ena <= 1'b1;
177
+ bias_weights_bram_addra <= conv1_bias_base + filter;
178
+ circle <= circle + 1;
179
+ end
180
+ else if(circle == 3)
181
+ begin
182
+ bias <= bias_weights_bram_douta;
183
+ circle <= 0;
184
+ count <= 0;
185
+ bias_weights_bram_ena <= 1'b0;
186
+ state <= S_LOAD_DATA;
187
+ end
188
+ else
189
+ begin
190
+ circle <= circle + 1;
191
+ end
192
+ end
193
+ S_LOAD_DATA:
194
+ begin
195
+ if(count < `CONV1_SIZE * `CONV1_SIZE)
196
+ begin
197
+ if(circle == 0)
198
+ begin
199
+ input_bram_ena <= 1'b1;
200
+ input_bram_addra <= graph * `INPUT_NODE + (row + count / `CONV1_SIZE) * `IMAGE_SIZE + column + count % `CONV1_SIZE;
201
+ circle <= circle + 1;
202
+ end
203
+ else if(circle == 3)
204
+ begin
205
+ data_begin = `DATA_SIZE * (`CONV1_SIZE * `CONV1_SIZE - count) - 1;
206
+ matrix1[data_begin-:8] <= input_bram_douta;
207
+ count <= count + 1;
208
+ circle <= 0;
209
+ end
210
+ else
211
+ begin
212
+ circle <= circle + 1;
213
+ end
214
+ end
215
+ else
216
+ begin
217
+ circle <= 0;
218
+ count <= 0;
219
+ input_bram_ena <= 1'b0;
220
+ relu_1_en <= 1'b0;
221
+ state <= S_CONVOLUTE;
222
+ end
223
+ end
224
+ S_CONVOLUTE:
225
+ begin
226
+ result <= dout;
227
+ circle <= 0;
228
+ state <= S_STORE_RESULT;
229
+ end
230
+ S_STORE_RESULT:
231
+ begin
232
+ if(circle == 0)
233
+ begin
234
+ result_bram_ena <= 1'b1;
235
+ result_bram_wea <= 1'b1;
236
+ result_bram_addra <= conv1_result_base + filter * `CONV1_OUTPUT * `CONV1_OUTPUT + row * `CONV1_OUTPUT + column;
237
+ result_bram_dina <= result;
238
+ circle <= circle + 1;
239
+ end
240
+ else if(circle == 3)
241
+ begin
242
+ result_bram_ena <= 1'b0;
243
+ result_bram_wea <= 1'b0;
244
+ circle <= 0;
245
+ if(column == `CONV1_OUTPUT - 1)
246
+ begin
247
+ if(row == `CONV1_OUTPUT - 1)
248
+ begin
249
+ if(channel == `NUM_CHANNELS - 1)
250
+ begin
251
+ filter <= filter + 1;
252
+ end
253
+ channel <= (channel + 1) % `NUM_CHANNELS;
254
+ end
255
+ row <= (row + 1) % `CONV1_OUTPUT;
256
+ end
257
+ column <= (column + 1) % `CONV1_OUTPUT;
258
+ state <= S_CHECK;
259
+ end
260
+ else
261
+ begin
262
+ circle <= circle + 1;
263
+ end
264
+ end
265
+ default:
266
+ begin
267
+ state <= S_IDLE;
268
+ bias_weights_bram_ena <= 1'b0;
269
+ result_bram_ena <= 1'b0;
270
+ result_bram_wea <= 1'b0;
271
+ input_bram_ena <= 1'b0;
272
+ end
273
+ endcase
274
+ end
275
+ end
276
+ end
277
+
278
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/conv_2.v ADDED
@@ -0,0 +1,275 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/01/2018 08:32:31 PM
7
+ // Design Name:
8
+ // Module Name: conv_2
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define CONV1_DEEP 20
23
+ `define CONV2_DEEP 50
24
+ `define CONV2_SIZE 5
25
+ `define CONV2_INPUT 12
26
+ `define CONV2_OUTPUT 8
27
+ `define DATA_SIZE 8
28
+
29
+ module conv_2(
30
+ clk, rst, conv_2_en, bias_weights_bram_douta, result_bram_douta,
31
+ bias_weights_bram_ena, bias_weights_bram_addra,
32
+ result_bram_ena, result_bram_wea, result_bram_addra, result_bram_dina,
33
+ conv_2_finish
34
+ );
35
+ input clk;
36
+ input rst;
37
+ input conv_2_en;
38
+ input [`DATA_SIZE-1:0] bias_weights_bram_douta;
39
+ input [`DATA_SIZE-1:0] result_bram_douta;
40
+ output reg bias_weights_bram_ena;
41
+ output reg [18:0] bias_weights_bram_addra;
42
+ output reg result_bram_ena;
43
+ output reg result_bram_wea;
44
+ output reg [14:0] result_bram_addra;
45
+ output reg [`DATA_SIZE-1:0] result_bram_dina;
46
+ output reg conv_2_finish;
47
+
48
+ integer filter = 0,
49
+ channel = 0,
50
+ row = 0,
51
+ column = 0,
52
+ count = 0;
53
+
54
+ reg [`DATA_SIZE*`CONV2_SIZE*`CONV2_SIZE-1:0] matrix1;
55
+ reg [`DATA_SIZE*`CONV2_SIZE*`CONV2_SIZE-1:0] matrix2;
56
+ reg [`DATA_SIZE-1:0] bias;
57
+ reg relu_1_en;
58
+ wire [`DATA_SIZE-1:0] dout;
59
+ reg [`DATA_SIZE-1:0] result;
60
+
61
+ multi_add conv_2_ma(
62
+ .matrix1(matrix1),
63
+ .matrix2(matrix2),
64
+ .bias(bias),
65
+ .relu_1_en(relu_1_en),
66
+ .dout(dout)
67
+ );
68
+
69
+ reg [6:0] state;
70
+
71
+ parameter S_IDLE = 7'b1000000,
72
+ S_CHECK = 7'b0100000,
73
+ S_LOAD_WEIGHTS = 7'b0010000,
74
+ S_LOAD_BIAS = 7'b0001000,
75
+ S_LOAD_DATA = 7'b0000100,
76
+ S_CONVOLUTE = 7'b0000010,
77
+ S_STORE_RESULT = 7'b0000001;
78
+
79
+ integer data_begin = 0;
80
+ integer circle = 0;
81
+
82
+ parameter pool1_result_base = 11520,
83
+ conv2_weights_base = 500,
84
+ conv2_bias_base = 430520,
85
+ conv2_result_base = 14400;
86
+
87
+ always@(posedge clk)
88
+ begin
89
+ if(rst == 1'b1)
90
+ begin
91
+ state <= S_IDLE;
92
+ bias_weights_bram_ena <= 1'b0;
93
+ result_bram_ena <= 1'b0;
94
+ result_bram_wea <= 1'b0;
95
+ end
96
+ else
97
+ begin
98
+ if(conv_2_en == 1'b1)
99
+ begin
100
+ case(state)
101
+ S_IDLE:
102
+ begin
103
+ filter <= 0;
104
+ channel <= 0;
105
+ row <= 0;
106
+ column <= 0;
107
+ matrix1 <= 0;
108
+ matrix2 <= 0;
109
+ bias <= 0;
110
+ relu_1_en <= 1'b0;
111
+ result <= 0;
112
+ conv_2_finish <= 1'b0;
113
+ state <= S_CHECK;
114
+ end
115
+ S_CHECK:
116
+ begin
117
+ if(filter == `CONV2_DEEP)
118
+ begin
119
+ bias_weights_bram_ena <= 1'b0;
120
+ result_bram_ena <= 1'b0;
121
+ result_bram_wea <= 1'b0;
122
+ conv_2_finish <= 1'b1;
123
+ state <= S_IDLE;
124
+ end
125
+ else
126
+ begin
127
+ circle <= 0;
128
+ count <= 0;
129
+ state <= S_LOAD_WEIGHTS;
130
+ end
131
+ end
132
+ S_LOAD_WEIGHTS:
133
+ begin
134
+ if(count < `CONV2_SIZE*`CONV2_SIZE)
135
+ begin
136
+ if(circle == 0)
137
+ begin
138
+ bias_weights_bram_ena <= 1'b1;
139
+ bias_weights_bram_addra <= conv2_weights_base + count;
140
+ circle <= circle + 1;
141
+ end
142
+ else if(circle == 3)
143
+ begin
144
+ data_begin = `DATA_SIZE * (`CONV2_SIZE * `CONV2_SIZE - count) - 1;
145
+ matrix2[data_begin-:8] <= bias_weights_bram_douta;
146
+ count <= count + 1;
147
+ circle <= 0;
148
+ end
149
+ else
150
+ begin
151
+ circle <= circle + 1;
152
+ end
153
+ end
154
+ else
155
+ begin
156
+ circle <= 0;
157
+ count <= 0;
158
+ bias_weights_bram_ena <= 1'b0;
159
+ state <= S_LOAD_BIAS;
160
+ end
161
+ end
162
+ S_LOAD_BIAS:
163
+ begin
164
+ if(channel == 0)
165
+ begin
166
+ if(circle == 0)
167
+ begin
168
+ bias_weights_bram_ena <= 1'b1;
169
+ bias_weights_bram_addra <= conv2_bias_base + filter;
170
+ circle <= circle + 1;
171
+ end
172
+ else if(circle == 3)
173
+ begin
174
+ bias <= bias_weights_bram_douta;
175
+ circle <= 0;
176
+ count <= 0;
177
+ bias_weights_bram_ena <= 1'b0;
178
+ state <= S_LOAD_DATA;
179
+ end
180
+ else
181
+ begin
182
+ circle <= circle + 1;
183
+ end
184
+ end
185
+ else
186
+ begin
187
+ bias <= result;
188
+ state <= S_LOAD_DATA;
189
+ end
190
+ end
191
+ S_LOAD_DATA:
192
+ begin
193
+ if(count < `CONV2_SIZE * `CONV2_SIZE)
194
+ begin
195
+ if(circle == 0)
196
+ begin
197
+ result_bram_ena <= 1'b1;
198
+ result_bram_addra <= pool1_result_base + channel * `CONV2_INPUT * `CONV2_INPUT + (row + count / `CONV2_SIZE) * `CONV2_INPUT + column + count % `CONV2_SIZE;
199
+ circle <= circle + 1;
200
+ end
201
+ else if(circle == 3)
202
+ begin
203
+ data_begin = `DATA_SIZE * (`CONV2_SIZE * `CONV2_SIZE - count) - 1;
204
+ matrix1[data_begin-:8] <= result_bram_douta;
205
+ count <= count + 1;
206
+ circle <= 0;
207
+ end
208
+ else
209
+ begin
210
+ circle <= circle + 1;
211
+ end
212
+ end
213
+ else
214
+ begin
215
+ circle <= 0;
216
+ count <= 0;
217
+ result_bram_ena <= 1'b0;
218
+ relu_1_en <= 1'b0;
219
+ state <= S_CONVOLUTE;
220
+ end
221
+ end
222
+ S_CONVOLUTE:
223
+ begin
224
+ result <= dout;
225
+ circle <= 0;
226
+ state <= S_STORE_RESULT;
227
+ end
228
+ S_STORE_RESULT:
229
+ begin
230
+ if(circle == 0)
231
+ begin
232
+ result_bram_ena <= 1'b1;
233
+ result_bram_wea <= 1'b1;
234
+ result_bram_addra <= conv2_result_base + filter * `CONV2_OUTPUT * `CONV2_OUTPUT + row * `CONV2_OUTPUT + column;
235
+ result_bram_dina <= result;
236
+ circle <= circle + 1;
237
+ end
238
+ else if(circle == 3)
239
+ begin
240
+ result_bram_ena <= 1'b0;
241
+ result_bram_wea <= 1'b0;
242
+ circle <= 0;
243
+ if(column == `CONV2_OUTPUT - 1)
244
+ begin
245
+ if(row == `CONV2_OUTPUT - 1)
246
+ begin
247
+ if(channel == `CONV1_DEEP - 1)
248
+ begin
249
+ filter <= filter + 1;
250
+ end
251
+ channel <= (channel + 1) % `CONV1_DEEP;
252
+ end
253
+ row <= (row + 1) % `CONV2_OUTPUT;
254
+ end
255
+ column <= (column + 1) % `CONV2_OUTPUT;
256
+ state <= S_CHECK;
257
+ end
258
+ else
259
+ begin
260
+ circle <= circle + 1;
261
+ end
262
+ end
263
+ default:
264
+ begin
265
+ state <= S_IDLE;
266
+ bias_weights_bram_ena <= 1'b0;
267
+ result_bram_ena <= 1'b0;
268
+ result_bram_wea <= 1'b0;
269
+ end
270
+ endcase
271
+ end
272
+ end
273
+ end
274
+
275
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/fc_1.v ADDED
@@ -0,0 +1,272 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/31/2017 08:00:24 AM
7
+ // Design Name:
8
+ // Module Name: fc_1
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define FC1_SIZE 500
23
+ `define CONV2_DEEP 50
24
+ `define POOL2_OUTPUT 4
25
+ `define DATA_SIZE 8
26
+
27
+ module fc_1(
28
+ clk, rst, fc_1_en, bias_weights_bram_douta, result_bram_douta,
29
+ bias_weights_bram_ena, bias_weights_bram_addra,
30
+ result_bram_ena, result_bram_wea, result_bram_addra, result_bram_dina,
31
+ fc_1_finish
32
+ );
33
+ input clk;
34
+ input rst;
35
+ input fc_1_en;
36
+ input [`DATA_SIZE-1:0] bias_weights_bram_douta;
37
+ input [`DATA_SIZE-1:0] result_bram_douta;
38
+ output reg bias_weights_bram_ena;
39
+ output reg [18:0] bias_weights_bram_addra;
40
+ output reg result_bram_ena;
41
+ output reg result_bram_wea;
42
+ output reg [14:0] result_bram_addra;
43
+ output reg [`DATA_SIZE-1:0] result_bram_dina;
44
+ output reg fc_1_finish;
45
+
46
+ reg [`DATA_SIZE*25-1:0] matrix1;
47
+ reg [`DATA_SIZE*25-1:0] matrix2;
48
+ reg [`DATA_SIZE-1:0] bias;
49
+ reg relu_1_en;
50
+ wire [`DATA_SIZE-1:0] dout;
51
+ reg [`DATA_SIZE-1:0] result;
52
+
53
+ multi_add fc_1_ma(
54
+ .matrix1(matrix1),
55
+ .matrix2(matrix2),
56
+ .bias(bias),
57
+ .relu_1_en(relu_1_en),
58
+ .dout(dout)
59
+ );
60
+
61
+ reg [6:0] state;
62
+
63
+ parameter S_IDLE = 7'b1000000,
64
+ S_CHECK = 7'b0100000,
65
+ S_LOAD_WEIGHTS = 7'b0010000,
66
+ S_LOAD_BIAS = 7'b0001000,
67
+ S_LOAD_DATA = 7'b0000100,
68
+ S_MULTI_ADD = 7'b0000010,
69
+ S_STORE_RESULT = 7'b0000001;
70
+
71
+ integer count = 0,
72
+ row = 0,
73
+ column = 0,
74
+ circle = 0,
75
+ data_begin = 0;
76
+
77
+ parameter pool2_result_base = 17600,
78
+ fc1_weights_base = 25500,
79
+ fc1_bias_base = 430570,
80
+ fc1_result_base = 18400;
81
+
82
+ always@(posedge clk)
83
+ begin
84
+ if(rst == 1'b1)
85
+ begin
86
+ state <= S_IDLE;
87
+ bias_weights_bram_ena <= 1'b0;
88
+ result_bram_ena <= 1'b0;
89
+ result_bram_wea <= 1'b0;
90
+ end
91
+ else
92
+ begin
93
+ if(fc_1_en == 1'b1)
94
+ begin
95
+ case(state)
96
+ S_IDLE:
97
+ begin
98
+ row <= 0;
99
+ column <= 0;
100
+ matrix1 <= 0;
101
+ matrix2 <= 0;
102
+ bias <= 0;
103
+ relu_1_en <= 1'b1;
104
+ result <= 0;
105
+ fc_1_finish <= 1'b0;
106
+ state <= S_CHECK;
107
+ end
108
+ S_CHECK:
109
+ begin
110
+ if(row == `FC1_SIZE)
111
+ begin
112
+ bias_weights_bram_ena <= 1'b0;
113
+ result_bram_ena <= 1'b0;
114
+ result_bram_wea <= 1'b0;
115
+ relu_1_en <= 1'b0;
116
+ fc_1_finish <= 1'b1;
117
+ state <= S_IDLE;
118
+ end
119
+ else
120
+ begin
121
+ circle <= 0;
122
+ count <= 0;
123
+ result <= 0;
124
+ state <= S_LOAD_WEIGHTS;
125
+ end
126
+ end
127
+ S_LOAD_WEIGHTS:
128
+ begin
129
+ if(count < 25)
130
+ begin
131
+ if(circle == 0)
132
+ begin
133
+ bias_weights_bram_ena <= 1'b1;
134
+ bias_weights_bram_addra <= fc1_weights_base + row * `CONV2_DEEP * `POOL2_OUTPUT * `POOL2_OUTPUT + column;
135
+ circle <= circle + 1;
136
+ end
137
+ else if(circle == 3)
138
+ begin
139
+ data_begin = `DATA_SIZE * (25 - count) - 1;
140
+ matrix2[data_begin-:8] <= bias_weights_bram_douta;
141
+ count <= count + 1;
142
+ column <= column + 1;
143
+ circle <= 0;
144
+ end
145
+ else
146
+ begin
147
+ circle <= circle + 1;
148
+ end
149
+ end
150
+ else
151
+ begin
152
+ circle <= 0;
153
+ count <= 0;
154
+ column <= column - 25;
155
+ bias_weights_bram_ena <= 1'b0;
156
+ state <= S_LOAD_BIAS;
157
+ end
158
+ end
159
+ S_LOAD_BIAS:
160
+ begin
161
+ if(column < 25)
162
+ begin
163
+ if(circle == 0)
164
+ begin
165
+ bias_weights_bram_ena <= 1'b1;
166
+ bias_weights_bram_addra <= fc1_bias_base + row;
167
+ circle <= circle + 1;
168
+ end
169
+ else if(circle == 3)
170
+ begin
171
+ bias <= bias_weights_bram_douta;
172
+ circle <= 0;
173
+ count <= 0;
174
+ bias_weights_bram_ena <= 1'b0;
175
+ state <= S_LOAD_DATA;
176
+ end
177
+ else
178
+ begin
179
+ circle <= circle + 1;
180
+ end
181
+ end
182
+ else
183
+ begin
184
+ bias <= result;
185
+ state <= S_LOAD_DATA;
186
+ end
187
+ end
188
+ S_LOAD_DATA:
189
+ begin
190
+ if(count < 25)
191
+ begin
192
+ if(circle == 0)
193
+ begin
194
+ result_bram_ena <= 1'b1;
195
+ result_bram_addra <= pool2_result_base + column;
196
+ circle <= circle + 1;
197
+ end
198
+ else if(circle == 3)
199
+ begin
200
+ data_begin = `DATA_SIZE * (25 - count) - 1;
201
+ matrix1[data_begin-:8] <= result_bram_douta;
202
+ count <= count + 1;
203
+ column <= column + 1;
204
+ circle <= 0;
205
+ end
206
+ else
207
+ begin
208
+ circle <= circle + 1;
209
+ end
210
+ end
211
+ else
212
+ begin
213
+ circle <= 0;
214
+ count <= 0;
215
+ result_bram_ena <= 1'b0;
216
+ relu_1_en <= 1'b1;
217
+ state <= S_MULTI_ADD;
218
+ end
219
+ end
220
+ S_MULTI_ADD:
221
+ begin
222
+ result <= result + dout;
223
+ if(column == `CONV2_DEEP * `POOL2_OUTPUT * `POOL2_OUTPUT)
224
+ begin
225
+ circle <= 0;
226
+ state <= S_STORE_RESULT;
227
+ end
228
+ else
229
+ begin
230
+ circle <= 0;
231
+ count <= 0;
232
+ state <= S_LOAD_WEIGHTS;
233
+ end
234
+ end
235
+ S_STORE_RESULT:
236
+ begin
237
+ if(circle == 0)
238
+ begin
239
+ result_bram_ena <= 1'b1;
240
+ result_bram_wea <= 1'b1;
241
+ result_bram_addra <= fc1_result_base + row;
242
+ result_bram_dina <= result;
243
+ circle <= circle + 1;
244
+ end
245
+ else if(circle == 3)
246
+ begin
247
+ result_bram_ena <= 1'b0;
248
+ result_bram_wea <= 1'b0;
249
+ circle <= 0;
250
+ column <= 0;
251
+ count <= 0;
252
+ row <= row + 1;
253
+ state <= S_CHECK;
254
+ end
255
+ else
256
+ begin
257
+ circle <= circle + 1;
258
+ end
259
+ end
260
+ default:
261
+ begin
262
+ state <= S_IDLE;
263
+ bias_weights_bram_ena <= 1'b0;
264
+ result_bram_ena <= 1'b0;
265
+ result_bram_wea <= 1'b0;
266
+ end
267
+ endcase
268
+ end
269
+ end
270
+ end
271
+
272
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/fc_2.v ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/02/2018 12:22:35 AM
7
+ // Design Name:
8
+ // Module Name: fc_2
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define FC1_SIZE 500
23
+ `define FC2_SIZE 10
24
+ `define DATA_SIZE 8
25
+
26
+ module fc_2(
27
+ clk, rst, fc_2_en, bias_weights_bram_douta, result_bram_douta,
28
+ bias_weights_bram_ena, bias_weights_bram_addra,
29
+ result_bram_ena, result_bram_wea, result_bram_addra, result_bram_dina,
30
+ fc_2_finish,
31
+ out_result
32
+ );
33
+ input clk;
34
+ input rst;
35
+ input fc_2_en;
36
+ input [`DATA_SIZE-1:0] bias_weights_bram_douta;
37
+ input [`DATA_SIZE-1:0] result_bram_douta;
38
+ output reg bias_weights_bram_ena;
39
+ output reg [18:0] bias_weights_bram_addra;
40
+ output reg result_bram_ena;
41
+ output reg result_bram_wea;
42
+ output reg [14:0] result_bram_addra;
43
+ output reg [`DATA_SIZE-1:0] result_bram_dina;
44
+ output reg fc_2_finish;
45
+ output reg [79:0] out_result;
46
+
47
+ reg [`DATA_SIZE*25-1:0] matrix1;
48
+ reg [`DATA_SIZE*25-1:0] matrix2;
49
+ reg [`DATA_SIZE-1:0] bias;
50
+ reg relu_1_en;
51
+ wire [`DATA_SIZE-1:0] dout;
52
+ reg [`DATA_SIZE-1:0] result;
53
+
54
+ multi_add fc_1_ma(
55
+ .matrix1(matrix1),
56
+ .matrix2(matrix2),
57
+ .bias(bias),
58
+ .relu_1_en(relu_1_en),
59
+ .dout(dout)
60
+ );
61
+
62
+ reg [6:0] state;
63
+
64
+ parameter S_IDLE = 7'b1000000,
65
+ S_CHECK = 7'b0100000,
66
+ S_LOAD_WEIGHTS = 7'b0010000,
67
+ S_LOAD_BIAS = 7'b0001000,
68
+ S_LOAD_DATA = 7'b0000100,
69
+ S_MULTI_ADD = 7'b0000010,
70
+ S_STORE_RESULT = 7'b0000001;
71
+
72
+ integer count = 0,
73
+ row = 0,
74
+ column = 0,
75
+ circle = 0,
76
+ data_begin = 0;
77
+
78
+ parameter fc1_result_base = 18400,
79
+ fc2_weights_base = 425500,
80
+ fc2_bias_base = 431070,
81
+ fc2_result_base = 18900;
82
+
83
+ always@(posedge clk)
84
+ begin
85
+ if(rst == 1'b1)
86
+ begin
87
+ state <= S_IDLE;
88
+ bias_weights_bram_ena <= 1'b0;
89
+ result_bram_ena <= 1'b0;
90
+ result_bram_wea <= 1'b0;
91
+ end
92
+ else
93
+ begin
94
+ if(fc_2_en == 1'b1)
95
+ begin
96
+ case(state)
97
+ S_IDLE:
98
+ begin
99
+ row <= 0;
100
+ column <= 0;
101
+ matrix1 <= 0;
102
+ matrix2 <= 0;
103
+ bias <= 0;
104
+ relu_1_en <= 1'b0;
105
+ result <= 0;
106
+ fc_2_finish <= 1'b0;
107
+ state <= S_CHECK;
108
+ end
109
+ S_CHECK:
110
+ begin
111
+ if(row == `FC2_SIZE)
112
+ begin
113
+ bias_weights_bram_ena <= 1'b0;
114
+ result_bram_ena <= 1'b0;
115
+ result_bram_wea <= 1'b0;
116
+ relu_1_en <= 1'b0;
117
+ fc_2_finish <= 1'b1;
118
+ state <= S_IDLE;
119
+ end
120
+ else
121
+ begin
122
+ circle <= 0;
123
+ count <= 0;
124
+ result <= 0;
125
+ state <= S_LOAD_WEIGHTS;
126
+ end
127
+ end
128
+ S_LOAD_WEIGHTS:
129
+ begin
130
+ if(count < 25)
131
+ begin
132
+ if(circle == 0)
133
+ begin
134
+ bias_weights_bram_ena <= 1'b1;
135
+ bias_weights_bram_addra <= fc2_weights_base + row * `FC1_SIZE + column;
136
+ circle <= circle + 1;
137
+ end
138
+ else if(circle == 3)
139
+ begin
140
+ data_begin = `DATA_SIZE * (25 - count) - 1;
141
+ matrix2[data_begin-:8] <= bias_weights_bram_douta;
142
+ count <= count + 1;
143
+ column <= column + 1;
144
+ circle <= 0;
145
+ end
146
+ else
147
+ begin
148
+ circle <= circle + 1;
149
+ end
150
+ end
151
+ else
152
+ begin
153
+ circle <= 0;
154
+ count <= 0;
155
+ column <= column - 25;
156
+ bias_weights_bram_ena <= 1'b0;
157
+ state <= S_LOAD_BIAS;
158
+ end
159
+ end
160
+ S_LOAD_BIAS:
161
+ begin
162
+ if(column < 25)
163
+ begin
164
+ if(circle == 0)
165
+ begin
166
+ bias_weights_bram_ena <= 1'b1;
167
+ bias_weights_bram_addra <= fc2_bias_base + row;
168
+ circle <= circle + 1;
169
+ end
170
+ else if(circle == 3)
171
+ begin
172
+ bias <= bias_weights_bram_douta;
173
+ circle <= 0;
174
+ count <= 0;
175
+ bias_weights_bram_ena <= 1'b0;
176
+ state <= S_LOAD_DATA;
177
+ end
178
+ else
179
+ begin
180
+ circle <= circle + 1;
181
+ end
182
+ end
183
+ else
184
+ begin
185
+ bias <= result;
186
+ state <= S_LOAD_DATA;
187
+ end
188
+ end
189
+ S_LOAD_DATA:
190
+ begin
191
+ if(count < 25)
192
+ begin
193
+ if(circle == 0)
194
+ begin
195
+ result_bram_ena <= 1'b1;
196
+ result_bram_addra <= fc1_result_base + column;
197
+ circle <= circle + 1;
198
+ end
199
+ else if(circle == 3)
200
+ begin
201
+ data_begin = `DATA_SIZE * (25 - count) - 1;
202
+ matrix1[data_begin-:8] <= result_bram_douta;
203
+ count <= count + 1;
204
+ column <= column + 1;
205
+ circle <= 0;
206
+ end
207
+ else
208
+ begin
209
+ circle <= circle + 1;
210
+ end
211
+ end
212
+ else
213
+ begin
214
+ circle <= 0;
215
+ count <= 0;
216
+ result_bram_ena <= 1'b0;
217
+ relu_1_en <= 1'b0;
218
+ state <= S_MULTI_ADD;
219
+ end
220
+ end
221
+ S_MULTI_ADD:
222
+ begin
223
+ result <= result + dout;
224
+ if(column == `FC1_SIZE)
225
+ begin
226
+ circle <= 0;
227
+ state <= S_STORE_RESULT;
228
+ end
229
+ else
230
+ begin
231
+ circle <= 0;
232
+ count <= 0;
233
+ state <= S_LOAD_WEIGHTS;
234
+ end
235
+ end
236
+ S_STORE_RESULT:
237
+ begin
238
+ if(circle == 0)
239
+ begin
240
+ result_bram_ena <= 1'b1;
241
+ result_bram_wea <= 1'b1;
242
+ result_bram_addra <= fc1_result_base + row;
243
+ result_bram_dina <= result;
244
+ circle <= circle + 1;
245
+ end
246
+ else if(circle == 3)
247
+ begin
248
+ $display("%b", result);
249
+ data_begin = 8 * (10 - row) - 1;
250
+ out_result[data_begin-:8] <= result;
251
+ result_bram_ena <= 1'b0;
252
+ result_bram_wea <= 1'b0;
253
+ circle <= 0;
254
+ column <= 0;
255
+ count <= 0;
256
+ row <= row + 1;
257
+ state <= S_CHECK;
258
+ end
259
+ else
260
+ begin
261
+ circle <= circle + 1;
262
+ end
263
+ end
264
+ default:
265
+ begin
266
+ state <= S_IDLE;
267
+ bias_weights_bram_ena <= 1'b0;
268
+ result_bram_ena <= 1'b0;
269
+ result_bram_wea <= 1'b0;
270
+ end
271
+ endcase
272
+ end
273
+ end
274
+ end
275
+
276
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/lenet_top.v ADDED
@@ -0,0 +1,583 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/30/2017 01:39:54 AM
7
+ // Design Name:
8
+ // Module Name: lenet_top
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ // LeNet parameters
23
+ `define INPUT_NODE 784
24
+ `define OUTPUT_NODE 10
25
+ `define LAYER1_NODE 500
26
+ `define LAYER2_NODE 500
27
+ `define IMAGE_SIZE 28
28
+ `define NUM_CHANNELS 1
29
+ `define NUM_LABELS 10
30
+
31
+ // Conv1
32
+ `define CONV1_DEEP 20
33
+ `define CONV1_SIZE 5
34
+ `define CONV1_OUTPUT 24
35
+
36
+ // Conv2
37
+ `define CONV2_DEEP 50
38
+ `define CONV2_SIZE 5
39
+ `define CONV2_OUTPUT 8
40
+
41
+ // Fc
42
+ `define FC1_SIZE 500
43
+
44
+ // fixed point
45
+ `define DATA_SIZE 8
46
+
47
+ module lenet_top(
48
+ clk, rst, start, graph, result, lenet_finish
49
+ );
50
+ input clk;
51
+ input rst;
52
+ input start;
53
+ input [4:0] graph;
54
+ output [`DATA_SIZE*`OUTPUT_NODE-1:0] result;
55
+ output lenet_finish;
56
+
57
+ reg finish;
58
+ assign lenet_finish = finish;
59
+
60
+ reg bias_weights_bram_ena;
61
+ reg [0:0] bias_weights_bram_wea;
62
+ reg [18:0] bias_weights_bram_addra;
63
+ reg [7:0] bias_weights_bram_dina = 8'b0;
64
+ wire [7:0] bias_weights_bram_douta;
65
+
66
+ reg result_bram_ena;
67
+ reg [0:0] result_bram_wea;
68
+ reg [14:0] result_bram_addra;
69
+ reg [7:0] result_bram_dina;
70
+ wire [7:0] result_bram_douta;
71
+
72
+ reg input_bram_ena;
73
+ reg [0:0] input_bram_wea;
74
+ reg [12:0] input_bram_addra;
75
+ reg [7:0] input_bram_dina = 8'b0;
76
+ wire [7:0] input_bram_douta;
77
+
78
+ blk_mem_gen_0 bias_weights_bram (
79
+ .clka(clk), // input wire clka
80
+ .ena(bias_weights_bram_ena), // input wire ena
81
+ .wea(bias_weights_bram_wea), // input wire [0 : 0] wea
82
+ .addra(bias_weights_bram_addra), // input wire [18 : 0] addra
83
+ .dina(bias_weights_bram_dina), // input wire [7 : 0] dina
84
+ .douta(bias_weights_bram_douta) // output wire [7 : 0] douta
85
+ );
86
+
87
+ blk_mem_gen_1 result_bram (
88
+ .clka(clk), // input wire clka
89
+ .ena(result_bram_ena), // input wire ena
90
+ .wea(result_bram_wea), // input wire [0 : 0] wea
91
+ .addra(result_bram_addra), // input wire [14 : 0] addra
92
+ .dina(result_bram_dina), // input wire [7 : 0] dina
93
+ .douta(result_bram_douta) // output wire [7 : 0] douta
94
+ );
95
+
96
+ blk_mem_gen_2 input_bram (
97
+ .clka(clk), // input wire clka
98
+ .ena(input_bram_ena), // input wire ena
99
+ .wea(input_bram_wea), // input wire [0 : 0] wea
100
+ .addra(input_bram_addra), // input wire [12 : 0] addra
101
+ .dina(input_bram_dina), // input wire [7 : 0] dina
102
+ .douta(input_bram_douta) // output wire [7 : 0] douta
103
+ );
104
+
105
+ reg conv_1_en;
106
+ wire conv_1_finish;
107
+ reg pool_1_en;
108
+ wire pool_1_finish;
109
+ reg conv_2_en;
110
+ wire conv_2_finish;
111
+ reg pool_2_en;
112
+ wire pool_2_finish;
113
+ reg fc_1_en;
114
+ wire fc_1_finish;
115
+ reg relu_1_en;
116
+ reg fc_2_en;
117
+ wire fc_2_finish;
118
+ reg complete_en;
119
+ wire complete_finish;
120
+
121
+ // conv1
122
+ wire conv_1_bias_weights_bram_ena;
123
+ //wire [0:0] conv_1_bias_weights_bram_wea;
124
+ wire [18:0] conv_1_bias_weights_bram_addra;
125
+ //wire [7:0] conv_1_bias_weights_bram_dina;
126
+ reg [7:0] conv_1_bias_weights_bram_douta;
127
+ wire conv_1_result_bram_ena;
128
+ wire [0:0] conv_1_result_bram_wea;
129
+ wire [14:0] conv_1_result_bram_addra;
130
+ wire [7:0] conv_1_result_bram_dina;
131
+ //reg [7:0] conv_1_result_bram_douta;
132
+ wire conv_1_input_bram_ena;
133
+ //wire [0:0] conv_1_input_bram_wea;
134
+ wire [12:0] conv_1_input_bram_addra;
135
+ //wire [7:0] conv_1_input_bram_dina;
136
+ reg [7:0] conv_1_input_bram_douta;
137
+
138
+ conv_1 u_conv_1(
139
+ .clk(clk),
140
+ .rst(rst),
141
+ .conv_1_en(conv_1_en),
142
+ .bias_weights_bram_douta(conv_1_bias_weights_bram_douta),
143
+ .input_bram_douta(conv_1_input_bram_douta),
144
+ .graph(graph),
145
+ .bias_weights_bram_ena(conv_1_bias_weights_bram_ena),
146
+ .bias_weights_bram_addra(conv_1_bias_weights_bram_addra),
147
+ .result_bram_ena(conv_1_result_bram_ena),
148
+ .result_bram_wea(conv_1_result_bram_wea),
149
+ .result_bram_addra(conv_1_result_bram_addra),
150
+ .result_bram_dina(conv_1_result_bram_dina),
151
+ .input_bram_ena(conv_1_input_bram_ena),
152
+ .input_bram_addra(conv_1_input_bram_addra),
153
+ .conv_1_finish(conv_1_finish)
154
+ );
155
+
156
+ // pool1
157
+ //wire pool_1_bias_weights_bram_ena;
158
+ //wire [0:0] pool_1_bias_weights_bram_wea;
159
+ //wire [18:0] pool_1_bias_weights_bram_addra;
160
+ //wire [7:0] pool_1_bias_weights_bram_dina;
161
+ //wire [7:0] pool_1_bias_weights_bram_douta;
162
+ wire pool_1_result_bram_ena;
163
+ wire [0:0] pool_1_result_bram_wea;
164
+ wire [14:0] pool_1_result_bram_addra;
165
+ wire [7:0] pool_1_result_bram_dina;
166
+ reg [7:0] pool_1_result_bram_douta;
167
+
168
+ pool_1 u_pool_1(
169
+ .clk(clk),
170
+ .rst(rst),
171
+ .pool_1_en(pool_1_en),
172
+ .result_bram_douta(pool_1_result_bram_douta),
173
+ .result_bram_ena(pool_1_result_bram_ena),
174
+ .result_bram_wea(pool_1_result_bram_wea),
175
+ .result_bram_addra(pool_1_result_bram_addra),
176
+ .result_bram_dina(pool_1_result_bram_dina),
177
+ .pool_1_finish(pool_1_finish)
178
+ );
179
+
180
+ // conv2
181
+ wire conv_2_bias_weights_bram_ena;
182
+ //wire [0:0] conv_2_bias_weights_bram_wea;
183
+ wire [18:0] conv_2_bias_weights_bram_addra;
184
+ //wire [7:0] conv_2_bias_weights_bram_dina;
185
+ reg [7:0] conv_2_bias_weights_bram_douta;
186
+ wire conv_2_result_bram_ena;
187
+ wire [0:0] conv_2_result_bram_wea;
188
+ wire [14:0] conv_2_result_bram_addra;
189
+ wire [7:0] conv_2_result_bram_dina;
190
+ reg [7:0] conv_2_result_bram_douta;
191
+
192
+ conv_2 u_conv_2(
193
+ .clk(clk),
194
+ .rst(rst),
195
+ .conv_2_en(conv_2_en),
196
+ .bias_weights_bram_douta(conv_2_bias_weights_bram_douta),
197
+ .result_bram_douta(conv_2_result_bram_douta),
198
+ .bias_weights_bram_ena(conv_2_bias_weights_bram_ena),
199
+ .bias_weights_bram_addra(conv_2_bias_weights_bram_addra),
200
+ .result_bram_ena(conv_2_result_bram_ena),
201
+ .result_bram_wea(conv_2_result_bram_wea),
202
+ .result_bram_addra(conv_2_result_bram_addra),
203
+ .result_bram_dina(conv_2_result_bram_dina),
204
+ .conv_2_finish(conv_2_finish)
205
+ );
206
+
207
+ // pool2
208
+ //wire pool_2_bias_weights_bram_ena;
209
+ //wire [0:0] pool_2_bias_weights_bram_wea;
210
+ //wire [18:0] pool_2_bias_weights_bram_addra;
211
+ //wire [7:0] pool_2_bias_weights_bram_dina;
212
+ //wire [7:0] pool_2_bias_weights_bram_douta;
213
+ wire pool_2_result_bram_ena;
214
+ wire [0:0] pool_2_result_bram_wea;
215
+ wire [14:0] pool_2_result_bram_addra;
216
+ wire [7:0] pool_2_result_bram_dina;
217
+ reg [7:0] pool_2_result_bram_douta;
218
+
219
+ pool_2 u_pool_2(
220
+ .clk(clk),
221
+ .rst(rst),
222
+ .pool_2_en(pool_2_en),
223
+ .result_bram_douta(pool_2_result_bram_douta),
224
+ .result_bram_ena(pool_2_result_bram_ena),
225
+ .result_bram_wea(pool_2_result_bram_wea),
226
+ .result_bram_addra(pool_2_result_bram_addra),
227
+ .result_bram_dina(pool_2_result_bram_dina),
228
+ .pool_2_finish(pool_2_finish)
229
+ );
230
+
231
+ // fc1
232
+ wire fc_1_bias_weights_bram_ena;
233
+ //wire [0:0] fc_1_bias_weights_bram_wea;
234
+ wire [18:0] fc_1_bias_weights_bram_addra;
235
+ //wire [7:0] fc_1_bias_weights_bram_dina;
236
+ reg [7:0] fc_1_bias_weights_bram_douta;
237
+ wire fc_1_result_bram_ena;
238
+ wire [0:0] fc_1_result_bram_wea;
239
+ wire [14:0] fc_1_result_bram_addra;
240
+ wire [7:0] fc_1_result_bram_dina;
241
+ reg [7:0] fc_1_result_bram_douta;
242
+
243
+ fc_1 u_fc_1(
244
+ .clk(clk),
245
+ .rst(rst),
246
+ .fc_1_en(fc_1_en),
247
+ .bias_weights_bram_douta(fc_1_bias_weights_bram_douta),
248
+ .result_bram_douta(fc_1_result_bram_douta),
249
+ .bias_weights_bram_ena(fc_1_bias_weights_bram_ena),
250
+ .bias_weights_bram_addra(fc_1_bias_weights_bram_addra),
251
+ .result_bram_ena(fc_1_result_bram_ena),
252
+ .result_bram_wea(fc_1_result_bram_wea),
253
+ .result_bram_addra(fc_1_result_bram_addra),
254
+ .result_bram_dina(fc_1_result_bram_dina),
255
+ .fc_1_finish(fc_1_finish)
256
+ );
257
+
258
+ // fc2
259
+ wire fc_2_bias_weights_bram_ena;
260
+ //wire [0:0] fc_2_bias_weights_bram_wea;
261
+ wire [18:0] fc_2_bias_weights_bram_addra;
262
+ //wire [7:0] fc_2_bias_weights_bram_dina;
263
+ reg [7:0] fc_2_bias_weights_bram_douta;
264
+ wire fc_2_result_bram_ena;
265
+ wire [0:0] fc_2_result_bram_wea;
266
+ wire [14:0] fc_2_result_bram_addra;
267
+ wire [7:0] fc_2_result_bram_dina;
268
+ reg [7:0] fc_2_result_bram_douta;
269
+
270
+ fc_2 u_fc_2(
271
+ .clk(clk),
272
+ .rst(rst),
273
+ .fc_2_en(fc_2_en),
274
+ .bias_weights_bram_douta(fc_2_bias_weights_bram_douta),
275
+ .result_bram_douta(fc_2_result_bram_douta),
276
+ .bias_weights_bram_ena(fc_2_bias_weights_bram_ena),
277
+ .bias_weights_bram_addra(fc_2_bias_weights_bram_addra),
278
+ .result_bram_ena(fc_2_result_bram_ena),
279
+ .result_bram_wea(fc_2_result_bram_wea),
280
+ .result_bram_addra(fc_2_result_bram_addra),
281
+ .result_bram_dina(fc_2_result_bram_dina),
282
+ .fc_2_finish(fc_2_finish),
283
+ .out_result(result)
284
+ );
285
+
286
+ // complete
287
+ wire complete_result_bram_ena;
288
+ wire [14:0] complete_result_bram_addra;
289
+ reg [7:0] complete_result_bram_douta;
290
+ wire [`DATA_SIZE*`OUTPUT_NODE-1:0] tempresult;
291
+ reg [`DATA_SIZE*`OUTPUT_NODE-1:0] result_1;
292
+
293
+ //assign result = result_1;
294
+
295
+ complete u_complete(
296
+ .clk(clk),
297
+ .rst(rst),
298
+ .complete_en(complete_en),
299
+ .result_bram_douta(complete_result_bram_douta),
300
+ .result_bram_ena(complete_result_bram_ena),
301
+ .result_bram_addra(complete_result_bram_addra),
302
+ .result(tempresult),
303
+ .complete_finish(complete_finish)
304
+ );
305
+
306
+ // States
307
+ reg [7:0] state;
308
+ parameter S_IDLE = 8'b10000000,
309
+ S_CONV_1 = 8'b01000000,
310
+ S_POOL_1 = 8'b00100000,
311
+ S_CONV_2 = 8'b00010000,
312
+ S_POOL_2 = 8'b00001000,
313
+ S_FC_1 = 8'b00000100,
314
+ S_FC_2 = 8'b00000010,
315
+ COMPLETE = 8'b00000001;
316
+
317
+ always@(posedge clk)
318
+ begin
319
+ if(rst == 1'b1)
320
+ begin
321
+ state <= S_IDLE;
322
+ //finish <= 1'b1;
323
+ conv_1_en <= 1'b0;
324
+ pool_1_en <= 1'b0;
325
+ conv_2_en <= 1'b0;
326
+ pool_2_en <= 1'b0;
327
+ fc_1_en <= 1'b0;
328
+ relu_1_en <= 1'b0;
329
+ fc_2_en <= 1'b0;
330
+ result_1 <= 0;
331
+ complete_en <= 1'b0;
332
+ end
333
+ else
334
+ begin
335
+ case (state)
336
+ S_IDLE:
337
+ begin
338
+ if(start == 1'b1)
339
+ begin
340
+ finish <= 1'b0;
341
+ //result_1 <= 0;
342
+ conv_1_en <= 1'b1;
343
+ relu_1_en <= 1'b0;
344
+ state <= S_CONV_1;
345
+ end
346
+ else
347
+ begin
348
+ state <= S_IDLE;
349
+ end
350
+ end
351
+ S_CONV_1:
352
+ begin
353
+ if(conv_1_finish == 1'b1)
354
+ begin
355
+ pool_1_en <= 1'b1;
356
+ conv_1_en <= 1'b0;
357
+ relu_1_en <= 1'b0;
358
+ state <= S_POOL_1;
359
+ end
360
+ end
361
+ S_POOL_1:
362
+ begin
363
+ if(pool_1_finish == 1'b1)
364
+ begin
365
+ conv_2_en <= 1'b1;
366
+ pool_1_en <= 1'b0;
367
+ relu_1_en <= 1'b0;
368
+ state <= S_CONV_2;
369
+ end
370
+ end
371
+ S_CONV_2:
372
+ begin
373
+ if(conv_2_finish == 1'b1)
374
+ begin
375
+ pool_2_en <= 1'b1;
376
+ conv_2_en <= 1'b0;
377
+ relu_1_en <= 1'b0;
378
+ state <= S_POOL_2;
379
+ end
380
+ end
381
+ S_POOL_2:
382
+ begin
383
+ if(pool_2_finish == 1'b1)
384
+ begin
385
+ fc_1_en <= 1'b1;
386
+ pool_2_en <= 1'b0;
387
+ relu_1_en <= 1'b1;
388
+ state <= S_FC_1;
389
+ end
390
+ end
391
+ S_FC_1:
392
+ begin
393
+ if(fc_1_finish == 1'b1)
394
+ begin
395
+ fc_2_en <= 1'b1;
396
+ fc_1_en <= 1'b0;
397
+ relu_1_en <= 1'b0;
398
+ state <= S_FC_2;
399
+ end
400
+ end
401
+ S_FC_2:
402
+ begin
403
+ if(fc_2_finish == 1'b1)
404
+ begin
405
+ complete_en <= 1'b1;
406
+ fc_2_en <= 1'b0;
407
+ relu_1_en <= 1'b0;
408
+ state <= COMPLETE;
409
+ end
410
+ end
411
+ COMPLETE:
412
+ begin
413
+ if(complete_finish == 1'b1)
414
+ begin
415
+ result_1 <= tempresult;
416
+ state <= S_IDLE;
417
+ complete_en <= 1'b0;
418
+ finish <= 1'b1;
419
+ end
420
+ end
421
+ default:
422
+ begin
423
+ state <= S_IDLE;
424
+ conv_1_en <= 1'b0;
425
+ pool_1_en <= 1'b0;
426
+ conv_2_en <= 1'b0;
427
+ pool_2_en <= 1'b0;
428
+ fc_1_en <= 1'b0;
429
+ relu_1_en <= 1'b0;
430
+ fc_2_en <= 1'b0;
431
+ complete_en <= 1'b0;
432
+ end
433
+ endcase
434
+ end
435
+ end
436
+
437
+ // bias_weights_bram
438
+ always@(*)
439
+ begin
440
+ if(rst == 1'b1)
441
+ begin
442
+ bias_weights_bram_ena = 1'b0;
443
+ bias_weights_bram_wea = 1'b0;
444
+ end
445
+ else
446
+ begin
447
+ case (state)
448
+ S_CONV_1:
449
+ begin
450
+ bias_weights_bram_ena = conv_1_bias_weights_bram_ena;
451
+ bias_weights_bram_wea = 1'b0;
452
+ bias_weights_bram_addra = conv_1_bias_weights_bram_addra;
453
+ conv_1_bias_weights_bram_douta = bias_weights_bram_douta;
454
+ end
455
+ S_CONV_2:
456
+ begin
457
+ bias_weights_bram_ena = conv_2_bias_weights_bram_ena;
458
+ bias_weights_bram_wea = 1'b0;
459
+ bias_weights_bram_addra = conv_2_bias_weights_bram_addra;
460
+ conv_2_bias_weights_bram_douta = bias_weights_bram_douta;
461
+ end
462
+ S_FC_1:
463
+ begin
464
+ bias_weights_bram_ena = fc_1_bias_weights_bram_ena;
465
+ bias_weights_bram_wea = 1'b0;
466
+ bias_weights_bram_addra = fc_1_bias_weights_bram_addra;
467
+ fc_1_bias_weights_bram_douta = bias_weights_bram_douta;
468
+ end
469
+ S_FC_2:
470
+ begin
471
+ bias_weights_bram_ena = fc_2_bias_weights_bram_ena;
472
+ bias_weights_bram_wea = 1'b0;
473
+ bias_weights_bram_addra = fc_2_bias_weights_bram_addra;
474
+ fc_2_bias_weights_bram_douta = bias_weights_bram_douta;
475
+ end
476
+ default:
477
+ begin
478
+ bias_weights_bram_ena = 1'b0;
479
+ bias_weights_bram_wea = 1'b0;
480
+ end
481
+ endcase
482
+ end
483
+ end
484
+
485
+ // result_bram
486
+ always@(*)
487
+ begin
488
+ if(rst == 1'b1)
489
+ begin
490
+ result_bram_ena = 1'b0;
491
+ result_bram_wea = 1'b0;
492
+ end
493
+ else
494
+ begin
495
+ case(state)
496
+ S_CONV_1:
497
+ begin
498
+ result_bram_ena = conv_1_result_bram_ena;
499
+ result_bram_wea = conv_1_result_bram_wea;
500
+ result_bram_addra = conv_1_result_bram_addra;
501
+ result_bram_dina = conv_1_result_bram_dina;
502
+ end
503
+ S_POOL_1:
504
+ begin
505
+ result_bram_ena = pool_1_result_bram_ena;
506
+ result_bram_wea = pool_1_result_bram_wea;
507
+ result_bram_addra = pool_1_result_bram_addra;
508
+ result_bram_dina = pool_1_result_bram_dina;
509
+ pool_1_result_bram_douta = result_bram_douta;
510
+ end
511
+ S_CONV_2:
512
+ begin
513
+ result_bram_ena = conv_2_result_bram_ena;
514
+ result_bram_wea = conv_2_result_bram_wea;
515
+ result_bram_addra = conv_2_result_bram_addra;
516
+ result_bram_dina = conv_2_result_bram_dina;
517
+ conv_2_result_bram_douta = result_bram_douta;
518
+ end
519
+ S_POOL_2:
520
+ begin
521
+ result_bram_ena = pool_2_result_bram_ena;
522
+ result_bram_wea = pool_2_result_bram_wea;
523
+ result_bram_addra = pool_2_result_bram_addra;
524
+ result_bram_dina = pool_2_result_bram_dina;
525
+ pool_2_result_bram_douta = result_bram_douta;
526
+ end
527
+ S_FC_1:
528
+ begin
529
+ result_bram_ena = fc_1_result_bram_ena;
530
+ result_bram_wea = fc_1_result_bram_wea;
531
+ result_bram_addra = fc_1_result_bram_addra;
532
+ result_bram_dina = fc_1_result_bram_dina;
533
+ fc_1_result_bram_douta = result_bram_douta;
534
+ end
535
+ S_FC_2:
536
+ begin
537
+ result_bram_ena = fc_2_result_bram_ena;
538
+ result_bram_wea = fc_2_result_bram_wea;
539
+ result_bram_addra = fc_2_result_bram_addra;
540
+ result_bram_dina = fc_2_result_bram_dina;
541
+ fc_2_result_bram_douta = result_bram_douta;
542
+ end
543
+ COMPLETE:
544
+ begin
545
+ result_bram_ena = complete_result_bram_ena;
546
+ result_bram_addra = complete_result_bram_addra;
547
+ complete_result_bram_douta = result_bram_douta;
548
+ end
549
+ default:
550
+ begin
551
+ result_bram_ena = 1'b0;
552
+ result_bram_wea = 1'b0;
553
+ end
554
+ endcase
555
+ end
556
+ end
557
+
558
+ // input_bram
559
+ always@(*)
560
+ begin
561
+ if(rst == 1'b1)
562
+ begin
563
+ input_bram_ena = 1'b0;
564
+ input_bram_wea = 1'b0;
565
+ end
566
+ else
567
+ begin
568
+ if(state == S_CONV_1)
569
+ begin
570
+ input_bram_ena = conv_1_input_bram_ena;
571
+ input_bram_wea = 1'b0;
572
+ input_bram_addra = conv_1_input_bram_addra;
573
+ conv_1_input_bram_douta = input_bram_douta;
574
+ end
575
+ else
576
+ begin
577
+ input_bram_ena = 1'b0;
578
+ input_bram_wea = 1'b0;
579
+ end
580
+ end
581
+ end
582
+
583
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/max_pool.v ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/31/2017 07:38:39 AM
7
+ // Design Name:
8
+ // Module Name: max_pool
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define DATA_SIZE 8
23
+
24
+ module max_pool(
25
+ din, dout
26
+ );
27
+ input [4*`DATA_SIZE-1:0] din;
28
+ output [`DATA_SIZE-1:0] dout;
29
+
30
+ wire [`DATA_SIZE-1:0] max_1;
31
+ wire [`DATA_SIZE-1:0] max_2;
32
+
33
+ assign max_1 = ((din[4*`DATA_SIZE-1] == 1'b1) && (din[3*`DATA_SIZE-1] == 1'b0) || ((din[4*`DATA_SIZE-1] == din[3*`DATA_SIZE-1]) && (din[4*`DATA_SIZE-1:3*`DATA_SIZE] < din[3*`DATA_SIZE-1:2*`DATA_SIZE]))) ? din[3*`DATA_SIZE-1:2*`DATA_SIZE] : din[4*`DATA_SIZE-1:3*`DATA_SIZE];
34
+
35
+ assign max_2 = ((max_1[`DATA_SIZE-1] == 1'b1) && (din[2*`DATA_SIZE-1] == 1'b0) || ((max_1[`DATA_SIZE-1] == din[2*`DATA_SIZE-1]) && (max_1 < din[2*`DATA_SIZE-1:`DATA_SIZE]))) ? din[2*`DATA_SIZE-1:`DATA_SIZE] : max_1;
36
+
37
+ assign dout = ((max_2[`DATA_SIZE-1] == 1'b1) && (din[`DATA_SIZE-1] == 1'b0) || ((max_2[`DATA_SIZE-1] == din[`DATA_SIZE-1]) && (max_2 < din[`DATA_SIZE-1:0]))) ? din[`DATA_SIZE-1:0] : max_2;
38
+
39
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/multi_add.v ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/31/2017 08:47:25 AM
7
+ // Design Name:
8
+ // Module Name: multi_add
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define LENGTH 5
23
+ `define WIDTH 5
24
+ `define DATA_SIZE 8
25
+
26
+ module multi_add(
27
+ matrix1, matrix2, bias, relu_1_en, dout
28
+ );
29
+ input [`DATA_SIZE*`LENGTH*`WIDTH-1:0] matrix1;
30
+ input [`DATA_SIZE*`LENGTH*`WIDTH-1:0] matrix2;
31
+ input [`DATA_SIZE-1:0] bias;
32
+ input relu_1_en;
33
+ output [`DATA_SIZE-1:0] dout;
34
+
35
+ reg [2*`DATA_SIZE-1:0] temp;
36
+ reg sign;
37
+ reg [`DATA_SIZE-1:0] item1;
38
+ reg [`DATA_SIZE-1:0] item2;
39
+ reg [2*(`DATA_SIZE-1)-1:0] midvalue;
40
+ reg [`DATA_SIZE-1:0] result;
41
+ wire [`DATA_SIZE-1:0] relu_result;
42
+
43
+ relu_1 ma_relu(
44
+ .din(result),
45
+ .dout(relu_result)
46
+ );
47
+
48
+ integer i = 0;
49
+ integer data_begin = 0;
50
+
51
+ assign dout = (relu_1_en == 1'b1) ? relu_result : result;
52
+
53
+ always@(*)
54
+ begin
55
+ temp = 8'b0;
56
+ sign = 1'b0;
57
+ item1 = 8'b0;
58
+ item2 = 8'b0;
59
+ midvalue = 14'b0;
60
+ for(i = 0; i < `LENGTH*`WIDTH; i = i + 1)
61
+ begin
62
+ data_begin = `DATA_SIZE * (`LENGTH * `WIDTH - i) - 1;
63
+ sign = matrix1[data_begin] ^ matrix2[data_begin];
64
+ item1 = (matrix1[data_begin] == 1'b0) ? matrix1[data_begin-:8] : {matrix1[data_begin], ~matrix1[(data_begin-1)-:7] + 1'b1};
65
+ item2 = (matrix2[data_begin] == 1'b0) ? matrix2[data_begin-:8] : {matrix2[data_begin], ~matrix2[(data_begin-1)-:7] + 1'b1};
66
+ midvalue = item1[`DATA_SIZE-2:0] * item2[`DATA_SIZE-2:0];
67
+ temp = temp + {sign, midvalue, 1'b0};
68
+ end
69
+ temp = temp + {bias, 8'b0};
70
+ result = temp >> 8;
71
+ end
72
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/pool_1.v ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/30/2017 01:09:48 AM
7
+ // Design Name:
8
+ // Module Name: pool_1
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define CONV1_DEEP 20
23
+ `define POOL1_INPUT 24
24
+ `define POOL1_OUTPUT 12
25
+ `define POOL1_SIZE 2
26
+ `define DATA_SIZE 8
27
+
28
+ module pool_1(
29
+ clk, rst, pool_1_en, result_bram_douta,
30
+ result_bram_ena, result_bram_wea, result_bram_addra, result_bram_dina,
31
+ pool_1_finish
32
+ );
33
+ input clk;
34
+ input rst;
35
+ input pool_1_en;
36
+ input [`DATA_SIZE-1:0] result_bram_douta;
37
+ output reg result_bram_ena;
38
+ output reg result_bram_wea;
39
+ output reg [14:0] result_bram_addra;
40
+ output reg [`DATA_SIZE-1:0] result_bram_dina;
41
+ output reg pool_1_finish;
42
+
43
+ reg [4*`DATA_SIZE-1:0] din;
44
+ wire [`DATA_SIZE-1:0] dout;
45
+
46
+ reg [`DATA_SIZE-1:0] result;
47
+
48
+ max_pool pool_1_mp(
49
+ .din(din),
50
+ .dout(dout)
51
+ );
52
+
53
+ reg [4:0] state;
54
+
55
+ parameter S_IDLE = 5'b10000,
56
+ S_CHECK = 5'b01000,
57
+ S_LOAD_DATA = 5'b00100,
58
+ S_POOLING = 5'b00010,
59
+ S_STORE_RESULT = 5'b00001;
60
+
61
+ integer count = 0,
62
+ circle = 0,
63
+ channel = 0,
64
+ row = 0,
65
+ column = 0,
66
+ data_begin = 0;
67
+
68
+ parameter conv1_result_base = 0,
69
+ pool1_result_base = 11520;
70
+
71
+ always@(posedge clk)
72
+ begin
73
+ if(rst == 1'b1)
74
+ begin
75
+ state <= S_IDLE;
76
+ result_bram_ena <= 1'b0;
77
+ result_bram_wea <= 1'b0;
78
+ end
79
+ else
80
+ begin
81
+ if(pool_1_en == 1'b1)
82
+ begin
83
+ case(state)
84
+ S_IDLE:
85
+ begin
86
+ channel <= 0;
87
+ circle <= 0;
88
+ row <= 0;
89
+ column <= 0;
90
+ din <= 0;
91
+ result <= 0;
92
+ pool_1_finish <= 1'b0;
93
+ state <= S_CHECK;
94
+ end
95
+ S_CHECK:
96
+ begin
97
+ if(channel == `CONV1_DEEP)
98
+ begin
99
+ result_bram_ena <= 1'b0;
100
+ result_bram_wea <= 1'b0;
101
+ pool_1_finish <= 1'b1;
102
+ state <= S_IDLE;
103
+ end
104
+ else
105
+ begin
106
+ circle <= 0;
107
+ count <= 0;
108
+ state <= S_LOAD_DATA;
109
+ end
110
+ end
111
+ S_LOAD_DATA:
112
+ begin
113
+ if(count < `POOL1_SIZE * `POOL1_SIZE)
114
+ begin
115
+ if(circle == 0)
116
+ begin
117
+ result_bram_ena <= 1'b1;
118
+ result_bram_addra <= conv1_result_base + channel * `POOL1_INPUT * `POOL1_INPUT + (row + (count / `POOL1_SIZE)) * `POOL1_INPUT + column + count % `POOL1_SIZE;
119
+ circle <= circle + 1;
120
+ end
121
+ else if(circle == 3)
122
+ begin
123
+ data_begin = `DATA_SIZE * (`POOL1_SIZE * `POOL1_SIZE - count) - 1;
124
+ din[data_begin-:`DATA_SIZE] <= result_bram_douta;
125
+ count <= count + 1;
126
+ circle <= 0;
127
+ end
128
+ else
129
+ begin
130
+ circle <= circle + 1;
131
+ end
132
+ end
133
+ else
134
+ begin
135
+ circle <= 0;
136
+ count <= 0;
137
+ result_bram_ena <= 1'b0;
138
+ state <= S_POOLING;
139
+ end
140
+ end
141
+ S_POOLING:
142
+ begin
143
+ result <= dout;
144
+ circle <= 0;
145
+ state <= S_STORE_RESULT;
146
+ end
147
+ S_STORE_RESULT:
148
+ begin
149
+ if(circle == 0)
150
+ begin
151
+ result_bram_ena <= 1'b1;
152
+ result_bram_wea <= 1'b1;
153
+ result_bram_addra <= pool1_result_base + channel * `POOL1_OUTPUT * `POOL1_OUTPUT + row * `POOL1_OUTPUT + column;
154
+ result_bram_dina <= result;
155
+ circle <= circle + 1;
156
+ end
157
+ else if(circle == 3)
158
+ begin
159
+ result_bram_ena <= 1'b0;
160
+ result_bram_wea <= 1'b0;
161
+ circle <= 0;
162
+ if(column == `POOL1_OUTPUT - 1)
163
+ begin
164
+ if(row == `POOL1_OUTPUT - 1)
165
+ begin
166
+ channel <= channel + 1;
167
+ end
168
+ row <= (row + 1) % `POOL1_OUTPUT;
169
+ end
170
+ column <= (column + 1) % `POOL1_OUTPUT;
171
+ state <= S_CHECK;
172
+ end
173
+ else
174
+ begin
175
+ circle <= circle + 1;
176
+ end
177
+ end
178
+ default:
179
+ begin
180
+ state <= S_IDLE;
181
+ result_bram_ena <= 1'b0;
182
+ result_bram_wea <= 1'b0;
183
+ end
184
+ endcase
185
+ end
186
+ end
187
+ end
188
+
189
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/pool_2.v ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/01/2018 09:13:10 PM
7
+ // Design Name:
8
+ // Module Name: pool_2
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define CONV2_DEEP 50
23
+ `define POOL2_INPUT 8
24
+ `define POOL2_OUTPUT 4
25
+ `define POOL2_SIZE 2
26
+ `define DATA_SIZE 8
27
+
28
+ module pool_2(
29
+ clk, rst, pool_2_en, result_bram_douta,
30
+ result_bram_ena, result_bram_wea, result_bram_addra, result_bram_dina,
31
+ pool_2_finish
32
+ );
33
+ input clk;
34
+ input rst;
35
+ input pool_2_en;
36
+ input [`DATA_SIZE-1:0] result_bram_douta;
37
+ output reg result_bram_ena;
38
+ output reg result_bram_wea;
39
+ output reg [14:0] result_bram_addra;
40
+ output reg [`DATA_SIZE-1:0] result_bram_dina;
41
+ output reg pool_2_finish;
42
+
43
+ reg [4*`DATA_SIZE-1:0] din;
44
+ wire [`DATA_SIZE-1:0] dout;
45
+
46
+ reg [`DATA_SIZE-1:0] result;
47
+
48
+ max_pool pool_2_mp(
49
+ .din(din),
50
+ .dout(dout)
51
+ );
52
+
53
+ reg [4:0] state;
54
+
55
+ parameter S_IDLE = 5'b10000,
56
+ S_CHECK = 5'b01000,
57
+ S_LOAD_DATA = 5'b00100,
58
+ S_POOLING = 5'b00010,
59
+ S_STORE_RESULT = 5'b00001;
60
+
61
+ integer count = 0,
62
+ circle = 0,
63
+ channel = 0,
64
+ row = 0,
65
+ column = 0,
66
+ data_begin = 0;
67
+
68
+ parameter conv2_result_base = 14400,
69
+ pool2_result_base = 17600;
70
+
71
+ always@(posedge clk)
72
+ begin
73
+ if(rst == 1'b1)
74
+ begin
75
+ state <= S_IDLE;
76
+ result_bram_ena <= 1'b0;
77
+ result_bram_wea <= 1'b0;
78
+ end
79
+ else
80
+ begin
81
+ if(pool_2_en == 1'b1)
82
+ begin
83
+ case(state)
84
+ S_IDLE:
85
+ begin
86
+ channel <= 0;
87
+ circle <= 0;
88
+ row <= 0;
89
+ column <= 0;
90
+ din <= 0;
91
+ result <= 0;
92
+ pool_2_finish <= 1'b0;
93
+ state <= S_CHECK;
94
+ end
95
+ S_CHECK:
96
+ begin
97
+ if(channel == `CONV2_DEEP)
98
+ begin
99
+ result_bram_ena <= 1'b0;
100
+ result_bram_wea <= 1'b0;
101
+ pool_2_finish <= 1'b1;
102
+ state <= S_IDLE;
103
+ end
104
+ else
105
+ begin
106
+ circle <= 0;
107
+ count <= 0;
108
+ state <= S_LOAD_DATA;
109
+ end
110
+ end
111
+ S_LOAD_DATA:
112
+ begin
113
+ if(count < `POOL2_SIZE * `POOL2_SIZE)
114
+ begin
115
+ if(circle == 0)
116
+ begin
117
+ result_bram_ena <= 1'b1;
118
+ result_bram_addra <= conv2_result_base + channel * `POOL2_INPUT * `POOL2_INPUT + (row + (count / `POOL2_SIZE)) * `POOL2_INPUT + column + count % `POOL2_SIZE;
119
+ circle <= circle + 1;
120
+ end
121
+ else if(circle == 3)
122
+ begin
123
+ data_begin = `DATA_SIZE * (`POOL2_SIZE * `POOL2_SIZE - count) - 1;
124
+ din[data_begin-:`DATA_SIZE] <= result_bram_douta;
125
+ count <= count + 1;
126
+ circle <= 0;
127
+ end
128
+ else
129
+ begin
130
+ circle <= circle + 1;
131
+ end
132
+ end
133
+ else
134
+ begin
135
+ circle <= 0;
136
+ count <= 0;
137
+ result_bram_ena <= 1'b0;
138
+ state <= S_POOLING;
139
+ end
140
+ end
141
+ S_POOLING:
142
+ begin
143
+ result <= dout;
144
+ circle <= 0;
145
+ state <= S_STORE_RESULT;
146
+ end
147
+ S_STORE_RESULT:
148
+ begin
149
+ if(circle == 0)
150
+ begin
151
+ result_bram_ena <= 1'b1;
152
+ result_bram_wea <= 1'b1;
153
+ result_bram_addra <= pool2_result_base + channel * `POOL2_OUTPUT * `POOL2_OUTPUT + row * `POOL2_OUTPUT + column;
154
+ result_bram_dina <= result;
155
+ circle <= circle + 1;
156
+ end
157
+ else if(circle == 3)
158
+ begin
159
+ result_bram_ena <= 1'b0;
160
+ result_bram_wea <= 1'b0;
161
+ circle <= 0;
162
+ if(column == `POOL2_OUTPUT - 1)
163
+ begin
164
+ if(row == `POOL2_OUTPUT - 1)
165
+ begin
166
+ channel <= channel + 1;
167
+ end
168
+ row <= (row + 1) % `POOL2_OUTPUT;
169
+ end
170
+ column <= (column + 1) % `POOL2_OUTPUT;
171
+ state <= S_CHECK;
172
+ end
173
+ else
174
+ begin
175
+ circle <= circle + 1;
176
+ end
177
+ end
178
+ default:
179
+ begin
180
+ state <= S_IDLE;
181
+ result_bram_ena <= 1'b0;
182
+ result_bram_wea <= 1'b0;
183
+ end
184
+ endcase
185
+ end
186
+ end
187
+ end
188
+
189
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/LeNet/relu_1.v ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12/31/2017 07:55:17 AM
7
+ // Design Name:
8
+ // Module Name: relu_1
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define DATA_SIZE 8
23
+
24
+ module relu_1(
25
+ din, dout
26
+ );
27
+ input [`DATA_SIZE-1:0] din;
28
+ output [`DATA_SIZE-1:0] dout;
29
+
30
+ assign dout = (din[`DATA_SIZE-1] == 1'b1) ? 8'b0 : din;
31
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/README.md ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ # FPGA-Accelerator-for-AES-LeNet-VGG16
2
+ AES: the verilog code are from http://www.aoki.ecei.tohoku.ac.jp/crypto/web/cores.html
3
+ LeNet:
4
+ VGG16: Bugs exist
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/README.md ADDED
@@ -0,0 +1 @@
 
 
1
+
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/bram_top.v ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/24/2018 11:39:15 PM
7
+ // Design Name:
8
+ // Module Name: bram_top
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define DATA_SIZE 8
23
+
24
+ module bram_top(
25
+ clk, rst,
26
+ loaddma_start, base_addr, num,
27
+ doutb_in,
28
+ data_ena, data_wea, data_addra, data_dina,
29
+ storedma_start,
30
+ clkb_out, rstb_out, enb_out, web_out, addrb_out, dinb_out,
31
+ loaddma_finish,
32
+ vgg16_bram_douta,
33
+ storedma_finish
34
+ );
35
+ input clk;
36
+ input rst;
37
+ input loaddma_start;
38
+ input [19:0] base_addr;
39
+ input [31:0] num;
40
+ input [31:0] doutb_in;
41
+ input data_ena;
42
+ input [0:0] data_wea;
43
+ input [19:0] data_addra;
44
+ input [7:0] data_dina;
45
+ input storedma_start;
46
+ output clkb_out;
47
+ output rstb_out;
48
+ output reg enb_out;
49
+ output reg [3:0] web_out;
50
+ output reg [31:0] addrb_out;
51
+ output reg [31:0] dinb_out;
52
+ output reg loaddma_finish;
53
+ output [7:0] vgg16_bram_douta;
54
+ output reg storedma_finish;
55
+
56
+ reg vgg16_bram_ena;
57
+ reg [0:0] vgg16_bram_wea;
58
+ reg [19:0] vgg16_bram_addra;
59
+ reg [7:0] vgg16_bram_dina;
60
+ // wire [7:0] vgg16_bram_douta;
61
+
62
+ assign clkb_out = clk,
63
+ rstb_out = rst;
64
+
65
+ blk_mem_gen_0 vgg16_bram (
66
+ .clka(clk), // input wire clka
67
+ .ena(vgg16_bram_ena), // input wire ena
68
+ .wea(vgg16_bram_wea), // input wire [0 : 0] wea
69
+ .addra(vgg16_bram_addra), // input wire [19 : 0] addra
70
+ .dina(vgg16_bram_dina), // input wire [7 : 0] dina
71
+ .douta(vgg16_bram_douta) // output wire [7 : 0] douta
72
+ );
73
+
74
+ reg [2:0] state;
75
+ parameter S_IDLE = 3'b100,
76
+ S_LOAD_DMA = 3'b010,
77
+ S_STORE_DMA = 3'b001;
78
+
79
+ reg [31:0] count;
80
+
81
+ integer circle = 0,
82
+ databegin = 0,
83
+ total = 0;
84
+
85
+ always@(posedge clk)
86
+ begin
87
+ if(rst == 1'b1)
88
+ begin
89
+ vgg16_bram_ena <= 1'b0;
90
+ vgg16_bram_wea <= 1'b0;
91
+ vgg16_bram_addra <= 20'b0;
92
+ vgg16_bram_dina <= 8'b0;
93
+ state <= S_IDLE;
94
+ enb_out <= 1'b0;
95
+ web_out <= 4'b0;
96
+ addrb_out <= 32'b0;
97
+ dinb_out <= 32'b0;
98
+ loaddma_finish <= 1'b0;
99
+ end
100
+ else
101
+ begin
102
+ case (state)
103
+ S_IDLE:
104
+ begin
105
+ if(loaddma_start == 1'b1)
106
+ begin
107
+ enb_out <= 1'b1;
108
+ addrb_out <= 32'b0;
109
+ vgg16_bram_ena <= 1'b1;
110
+ vgg16_bram_wea <= 1'b1;
111
+ vgg16_bram_addra <= base_addr;
112
+ count <= 32'b0;
113
+ circle <= 0;
114
+ state <= S_LOAD_DMA;
115
+ loaddma_finish <= 1'b0;
116
+ end
117
+ else if(storedma_start == 1'b1)
118
+ begin
119
+ enb_out <= 1'b1;
120
+ web_out <= 4'b1;
121
+ addrb_out <= 32'b0;
122
+ dinb_out <= 32'b0;
123
+ vgg16_bram_ena <= 1'b1;
124
+ vgg16_bram_addra <= base_addr;
125
+ count <= 32'b0;
126
+ circle <= 0;
127
+ total <= 0;
128
+ storedma_finish <= 1'b0;
129
+ state <= S_STORE_DMA;
130
+ end
131
+ else
132
+ begin
133
+ vgg16_bram_ena <= data_ena;
134
+ vgg16_bram_wea <= data_wea;
135
+ vgg16_bram_addra <= data_addra;
136
+ vgg16_bram_dina <= data_dina;
137
+ state <= S_IDLE;
138
+ end
139
+ end
140
+ S_LOAD_DMA:
141
+ begin
142
+ if(count == num)
143
+ begin
144
+ count <= 32'b0;
145
+ state <= S_IDLE;
146
+ enb_out <= 1'b0;
147
+ addrb_out <= 32'b0;
148
+ circle <= 0;
149
+ vgg16_bram_ena <= 1'b0;
150
+ vgg16_bram_wea <= 1'b0;
151
+ vgg16_bram_addra <= 20'b0;
152
+ loaddma_finish <= 1'b1;
153
+ end
154
+ else
155
+ begin
156
+ databegin = `DATA_SIZE * (4 - circle) - 1;
157
+ vgg16_bram_dina <= doutb_in[databegin-:8];
158
+ vgg16_bram_addra <= vgg16_bram_addra + count[19:0];
159
+ count <= count + 32'b1;
160
+ circle = circle + 1;
161
+ if(circle == 4)
162
+ begin
163
+ circle <= 0;
164
+ addrb_out <= addrb_out + 32'b1;
165
+ end
166
+ end
167
+ end
168
+ S_STORE_DMA:
169
+ begin
170
+ if(circle == 2)
171
+ begin
172
+ if(count == num)
173
+ begin
174
+ state <= S_IDLE;
175
+ enb_out <= 1'b0;
176
+ web_out <= 4'b0;
177
+ addrb_out <= 32'b0;
178
+ dinb_out <= 32'b0;
179
+ vgg16_bram_ena <= 1'b0;
180
+ vgg16_bram_addra <= 20'b0;
181
+ count <= 32'b0;
182
+ total <= 0;
183
+ storedma_finish <= 1'b1;
184
+ end
185
+ else
186
+ begin
187
+ databegin = `DATA_SIZE * (4 - total) - 1;
188
+ dinb_out[databegin-:8] <= vgg16_bram_douta;
189
+ vgg16_bram_addra <= vgg16_bram_addra + 20'b1;
190
+ count <= count + 32'b1;
191
+ total = total + 1;
192
+ if(total == 4)
193
+ begin
194
+ total <= 0;
195
+ addrb_out <= addrb_out + 32'b1;
196
+ end
197
+ end
198
+ circle <= 0;
199
+ end
200
+ else
201
+ begin
202
+ circle <= circle + 1;
203
+ end
204
+ end
205
+ default:
206
+ begin
207
+ state <= S_IDLE;
208
+ end
209
+ endcase
210
+ end
211
+ end
212
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/conv.v ADDED
@@ -0,0 +1,555 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/21/2018 10:25:44 PM
7
+ // Design Name:
8
+ // Module Name: conv
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define DATA_SIZE 8
23
+ `define CONV_SIZE 3
24
+
25
+ module conv(
26
+ clk, rst, conv_en, addbefore, vgg16_bram_douta,
27
+ weight_base_addr, bias_base_addr, data_base_addr, result_base_addr,
28
+ rownum, columnnum, channelnum, filternum,
29
+ vgg16_bram_ena, vgg16_bram_wea, vgg16_bram_addra, vgg16_bram_dina,
30
+ conv_finish
31
+ );
32
+ input clk;
33
+ input rst;
34
+ input conv_en;
35
+ input addbefore;
36
+ input [7:0] vgg16_bram_douta;
37
+ input [19:0] weight_base_addr;
38
+ input [19:0] bias_base_addr;
39
+ input [19:0] data_base_addr;
40
+ input [19:0] result_base_addr;
41
+ input [9:0] rownum;
42
+ input [9:0] columnnum;
43
+ input [9:0] channelnum;
44
+ input [9:0] filternum;
45
+ output reg vgg16_bram_ena;
46
+ output reg [0:0] vgg16_bram_wea;
47
+ output reg [19:0] vgg16_bram_addra;
48
+ output reg [7:0] vgg16_bram_dina;
49
+ output reg conv_finish;
50
+
51
+ reg [`DATA_SIZE*`CONV_SIZE*`CONV_SIZE-1:0] matrix1;
52
+ reg [`DATA_SIZE*`CONV_SIZE*`CONV_SIZE-1:0] matrix2;
53
+ reg [`DATA_SIZE-1:0] bias;
54
+ reg relu_en;
55
+ wire [`DATA_SIZE-1:0] dout;
56
+
57
+ mult_add conv_ma(
58
+ .matrix1(matrix1),
59
+ .matrix2(matrix2),
60
+ .bias(bias),
61
+ .relu_en(relu_en),
62
+ .dout(dout)
63
+ );
64
+
65
+ // reg [9:0] row;
66
+ // reg [9:0] column;
67
+ // reg [9:0] channel;
68
+ // reg [9:0] filter;
69
+ integer row = 0,
70
+ column = 0,
71
+ channel = 0,
72
+ filter = 0,
73
+ count = 0,
74
+ circle = 0,
75
+ databegin = 0;
76
+
77
+ reg [`DATA_SIZE-1:0] result;
78
+
79
+ reg [6:0] state;
80
+ parameter S_IDLE = 7'b1000000,
81
+ S_CHECK = 7'b0100000,
82
+ S_LOAD_WEIGHTS = 7'b0010000,
83
+ S_LOAD_BIAS = 7'b0001000,
84
+ S_LOAD_DATA = 7'b0000100,
85
+ S_CONVOLUTE = 7'b0000010,
86
+ S_STORE_RESULT = 7'b0000001;
87
+
88
+ always@(posedge clk)
89
+ begin
90
+ if(rst == 1'b1)
91
+ begin
92
+ state <= S_IDLE;
93
+ vgg16_bram_ena <= 1'b0;
94
+ vgg16_bram_wea <= 1'b0;
95
+ row <= 0;
96
+ column <= 0;
97
+ channel <= 0;
98
+ filter <= 0;
99
+ count <= 0;
100
+ circle <= 0;
101
+ end
102
+ else
103
+ begin
104
+ if(conv_en == 1'b1)
105
+ begin
106
+ case (state)
107
+ S_IDLE:
108
+ begin
109
+ row <= 0;
110
+ column <= 0;
111
+ channel <= 0;
112
+ filter <= 0;
113
+ count <= 0;
114
+ circle <= 0;
115
+ matrix1 <= 0;
116
+ matrix2 <= 0;
117
+ bias <= 0;
118
+ relu_en <= 0;
119
+ result <= 0;
120
+ conv_finish <= 1'b0;
121
+ state <= S_CHECK;
122
+ end
123
+ S_CHECK:
124
+ begin
125
+ if(filter == filternum)
126
+ begin
127
+ vgg16_bram_ena <= 1'b0;
128
+ vgg16_bram_wea <= 1'b0;
129
+ conv_finish <= 1'b1;
130
+ state <= S_IDLE;
131
+ end
132
+ else
133
+ begin
134
+ circle <= 0;
135
+ count <= 0;
136
+ state <= S_LOAD_WEIGHTS;
137
+ end
138
+ end
139
+ S_LOAD_WEIGHTS:
140
+ begin
141
+ if(count < `CONV_SIZE * `CONV_SIZE)
142
+ begin
143
+ if(circle == 0)
144
+ begin
145
+ vgg16_bram_ena <= 1'b1;
146
+ vgg16_bram_addra <= weight_base_addr + filter * `CONV_SIZE * `CONV_SIZE * {10'b0, channelnum} + channel * `CONV_SIZE * `CONV_SIZE + count;
147
+ circle <= circle + 1;
148
+ end
149
+ else if(circle == 2)
150
+ begin
151
+ databegin = `DATA_SIZE * (`CONV_SIZE * `CONV_SIZE - count) - 1;
152
+ matrix2[databegin-:8] <= vgg16_bram_douta;
153
+ count <= count + 1;
154
+ circle <= 0;
155
+ end
156
+ else
157
+ begin
158
+ circle <= circle + 1;
159
+ end
160
+ end
161
+ else
162
+ begin
163
+ circle <= 0;
164
+ count <= 0;
165
+ vgg16_bram_ena <= 1'b0;
166
+ state <= S_LOAD_BIAS;
167
+ end
168
+ end
169
+ S_LOAD_BIAS:
170
+ begin
171
+ if(channel == 0)
172
+ begin
173
+ if(circle == 0)
174
+ begin
175
+ vgg16_bram_ena <= 1'b1;
176
+ vgg16_bram_addra <= bias_base_addr + filter;
177
+ circle <= circle + 1;
178
+ end
179
+ else if(circle == 2)
180
+ begin
181
+ bias <= vgg16_bram_douta;
182
+ circle <= 0;
183
+ count <= 0;
184
+ vgg16_bram_ena <= 1'b0;
185
+ state <= S_LOAD_DATA;
186
+ end
187
+ else
188
+ begin
189
+ circle <= circle + 1;
190
+ end
191
+ end
192
+ else
193
+ begin
194
+ bias <= result;
195
+ state <= S_LOAD_DATA;
196
+ end
197
+ end
198
+ S_LOAD_DATA:
199
+ begin
200
+ if(row > 0 && row < rownum - 10'b1 && column > 0 && column < columnnum - 10'b1)
201
+ begin
202
+ if(count < `CONV_SIZE * `CONV_SIZE)
203
+ begin
204
+ if(circle == 0)
205
+ begin
206
+ vgg16_bram_ena <= 1'b1;
207
+ vgg16_bram_addra <= data_base_addr + channel * rownum * columnnum + ((row-1) + count / `CONV_SIZE) * rownum + (column-1) + count % `CONV_SIZE;
208
+ circle <= circle + 1;
209
+ end
210
+ else if(circle == 2)
211
+ begin
212
+ databegin = `DATA_SIZE * (`CONV_SIZE * `CONV_SIZE - count) - 1;
213
+ matrix1[databegin-:8] <= vgg16_bram_douta;
214
+ count <= count + 1;
215
+ circle <= 0;
216
+ end
217
+ else
218
+ begin
219
+ circle <= circle + 1;
220
+ end
221
+ end
222
+ else
223
+ begin
224
+ circle <= 0;
225
+ count <= 0;
226
+ vgg16_bram_ena <= 1'b0;
227
+ relu_en <= 1'b1;
228
+ state <= S_CONVOLUTE;
229
+ end
230
+ end
231
+ else if(row == 0)
232
+ begin
233
+ if(column == 0)
234
+ begin
235
+ if(count < (`CONV_SIZE - 1) * (`CONV_SIZE - 1))
236
+ begin
237
+ if(circle == 0)
238
+ begin
239
+ vgg16_bram_ena <= 1'b1;
240
+ vgg16_bram_addra <= data_base_addr + channel * rownum * columnnum + (count / (`CONV_SIZE-1)) * rownum + column + count % (`CONV_SIZE-1);
241
+ circle <= circle + 1;
242
+ end
243
+ else if(circle == 2)
244
+ begin
245
+ databegin = `DATA_SIZE * (`CONV_SIZE * (`CONV_SIZE - 1) - (count/(`CONV_SIZE-1)+1) - count) - 1;
246
+ matrix1[databegin-:8] <= vgg16_bram_douta;
247
+ count <= count + 1;
248
+ circle <= 0;
249
+ end
250
+ else
251
+ begin
252
+ circle <= circle + 1;
253
+ end
254
+ end
255
+ else
256
+ begin
257
+ matrix1[71:40] <= 32'b0;
258
+ matrix1[23:16] <= 8'b0;
259
+ circle <= 0;
260
+ count <= 0;
261
+ vgg16_bram_ena <= 1'b0;
262
+ relu_en <= 1'b1;
263
+ state <= S_CONVOLUTE;
264
+ end
265
+ end
266
+ else if(column == columnnum - 10'b1)
267
+ begin
268
+ if(count < (`CONV_SIZE - 1) * (`CONV_SIZE - 1))
269
+ begin
270
+ if(circle == 0)
271
+ begin
272
+ vgg16_bram_ena <= 1'b1;
273
+ vgg16_bram_addra <= data_base_addr + channel * rownum * columnnum + (count / (`CONV_SIZE-1)) * rownum + (column-1) + count % (`CONV_SIZE-1);
274
+ circle <= circle + 1;
275
+ end
276
+ else if(circle == 2)
277
+ begin
278
+ databegin = `DATA_SIZE * (`CONV_SIZE * (`CONV_SIZE - 1) - count/(`CONV_SIZE-1) - count) - 1;
279
+ matrix1[databegin-:8] <= vgg16_bram_douta;
280
+ count <= count + 1;
281
+ circle <= 0;
282
+ end
283
+ else
284
+ begin
285
+ circle <= circle + 1;
286
+ end
287
+ end
288
+ else
289
+ begin
290
+ matrix1[71:48] <= 24'b0;
291
+ matrix1[31:24] <= 8'b0;
292
+ matrix1[7:0] <= 8'b0;
293
+ circle <= 0;
294
+ count <= 0;
295
+ vgg16_bram_ena <= 1'b0;
296
+ relu_en <= 1'b1;
297
+ state <= S_CONVOLUTE;
298
+ end
299
+ end
300
+ else
301
+ begin
302
+ if(count < `CONV_SIZE * (`CONV_SIZE - 1))
303
+ begin
304
+ if(circle == 0)
305
+ begin
306
+ vgg16_bram_ena <= 1'b1;
307
+ vgg16_bram_addra <= data_base_addr + channel * rownum * columnnum + (count / `CONV_SIZE) * rownum + (column-1) + count % `CONV_SIZE;
308
+ circle <= circle + 1;
309
+ end
310
+ else if(circle == 2)
311
+ begin
312
+ databegin = `DATA_SIZE * (`CONV_SIZE * (`CONV_SIZE - 1) - count) - 1;
313
+ matrix1[databegin-:8] <= vgg16_bram_douta;
314
+ count <= count + 1;
315
+ circle <= 0;
316
+ end
317
+ else
318
+ begin
319
+ circle <= circle + 1;
320
+ end
321
+ end
322
+ else
323
+ begin
324
+ matrix1[71:48] <= 24'b0;
325
+ circle <= 0;
326
+ count <= 0;
327
+ vgg16_bram_ena <= 1'b0;
328
+ relu_en <= 1'b1;
329
+ state <= S_CONVOLUTE;
330
+ end
331
+ end
332
+ end
333
+ else if(row == rownum - 10'b1)
334
+ begin
335
+ if(column == 0)
336
+ begin
337
+ if(count < (`CONV_SIZE - 1) * (`CONV_SIZE - 1))
338
+ begin
339
+ if(circle == 0)
340
+ begin
341
+ vgg16_bram_ena <= 1'b1;
342
+ vgg16_bram_addra <= data_base_addr + channel * rownum * columnnum + ((row-1) + count / (`CONV_SIZE-1)) * rownum + column + count % (`CONV_SIZE-1);
343
+ circle <= circle + 1;
344
+ end
345
+ else if(circle == 2)
346
+ begin
347
+ databegin = `DATA_SIZE * (`CONV_SIZE * `CONV_SIZE - (count/(`CONV_SIZE-1)+1) - count) - 1;
348
+ matrix1[databegin-:8] <= vgg16_bram_douta;
349
+ count <= count + 1;
350
+ circle <= 0;
351
+ end
352
+ else
353
+ begin
354
+ circle <= circle + 1;
355
+ end
356
+ end
357
+ else
358
+ begin
359
+ matrix1[71:64] <= 8'b0;
360
+ matrix1[47:40] <= 8'b0;
361
+ matrix1[23:0] <= 24'b0;
362
+ circle <= 0;
363
+ count <= 0;
364
+ vgg16_bram_ena <= 1'b0;
365
+ relu_en <= 1'b1;
366
+ state <= S_CONVOLUTE;
367
+ end
368
+ end
369
+ else if(column == columnnum - 10'b1)
370
+ begin
371
+ if(count < (`CONV_SIZE - 1) * (`CONV_SIZE - 1))
372
+ begin
373
+ if(circle == 0)
374
+ begin
375
+ vgg16_bram_ena <= 1'b1;
376
+ vgg16_bram_addra <= data_base_addr + channel * rownum * columnnum + ((row-1) + count / (`CONV_SIZE-1)) * rownum + (column-1) + count % (`CONV_SIZE-1);
377
+ circle <= circle + 1;
378
+ end
379
+ else if(circle == 2)
380
+ begin
381
+ databegin = `DATA_SIZE * (`CONV_SIZE * `CONV_SIZE - count/(`CONV_SIZE-1) - count) - 1;
382
+ matrix1[databegin-:8] <= vgg16_bram_douta;
383
+ count <= count + 1;
384
+ circle <= 0;
385
+ end
386
+ else
387
+ begin
388
+ circle <= circle + 1;
389
+ end
390
+ end
391
+ else
392
+ begin
393
+ matrix1[55:48] <= 8'b0;
394
+ matrix1[31:0] <= 32'b0;
395
+ circle <= 0;
396
+ count <= 0;
397
+ vgg16_bram_ena <= 1'b0;
398
+ relu_en <= 1'b1;
399
+ state <= S_CONVOLUTE;
400
+ end
401
+ end
402
+ else
403
+ begin
404
+ if(count < `CONV_SIZE * (`CONV_SIZE - 1))
405
+ begin
406
+ if(circle == 0)
407
+ begin
408
+ vgg16_bram_ena <= 1'b1;
409
+ vgg16_bram_addra <= data_base_addr + channel * rownum * columnnum + ((row-1) + count / `CONV_SIZE) * rownum + (column-1) + count % `CONV_SIZE;
410
+ circle <= circle + 1;
411
+ end
412
+ else if(circle == 2)
413
+ begin
414
+ databegin = `DATA_SIZE * (`CONV_SIZE * `CONV_SIZE - count) - 1;
415
+ matrix1[databegin-:8] <= vgg16_bram_douta;
416
+ count <= count + 1;
417
+ circle <= 0;
418
+ end
419
+ else
420
+ begin
421
+ circle <= circle + 1;
422
+ end
423
+ end
424
+ else
425
+ begin
426
+ matrix1[23:0] <= 24'b0;
427
+ circle <= 0;
428
+ count <= 0;
429
+ vgg16_bram_ena <= 1'b0;
430
+ relu_en <= 1'b1;
431
+ state <= S_CONVOLUTE;
432
+ end
433
+ end
434
+ end
435
+ end
436
+ S_CONVOLUTE:
437
+ begin
438
+ result <= dout;
439
+ circle <= 0;
440
+ state <= S_STORE_RESULT;
441
+ end
442
+ S_STORE_RESULT:
443
+ begin
444
+ if(addbefore == 1'b1)
445
+ begin
446
+ if(circle == 0)
447
+ begin
448
+ vgg16_bram_ena <= 1'b1;
449
+ vgg16_bram_addra <= result_base_addr + filter * rownum * columnnum + row * rownum + column;
450
+ circle <= circle + 1;
451
+ end
452
+ else if(circle == 2)
453
+ begin
454
+ vgg16_bram_dina <= result + vgg16_bram_douta;
455
+ vgg16_bram_wea <= 1'b1;
456
+ vgg16_bram_addra <= result_base_addr + filter * rownum * columnnum + row * rownum + column;
457
+ circle <= circle + 1;
458
+ end
459
+ else if(circle == 3)
460
+ begin
461
+ vgg16_bram_ena <= 1'b0;
462
+ vgg16_bram_wea <= 1'b0;
463
+ circle <= 0;
464
+ if(column == columnnum - 10'b1)
465
+ begin
466
+ if(row == rownum - 10'b1)
467
+ begin
468
+ if(channel == channelnum - 10'b1)
469
+ begin
470
+ filter <= filter + 1;
471
+ channel <= 0;
472
+ end
473
+ else
474
+ begin
475
+ channel <= channel + 1;
476
+ end
477
+ row <= 0;
478
+ end
479
+ else
480
+ begin
481
+ row <= row + 1;
482
+ end
483
+ column <= 0;
484
+ end
485
+ else
486
+ begin
487
+ column <= column + 1;
488
+ end
489
+ state <= S_CHECK;
490
+ end
491
+ else
492
+ begin
493
+ circle <= circle + 1;
494
+ end
495
+ end
496
+ else
497
+ begin
498
+ if(circle == 0)
499
+ begin
500
+ vgg16_bram_ena <= 1'b1;
501
+ vgg16_bram_wea <= 1'b1;
502
+ vgg16_bram_addra <= result_base_addr + filter * rownum * columnnum + row * rownum + column;
503
+ vgg16_bram_dina <= result;
504
+ circle <= circle + 1;
505
+ end
506
+ else if(circle == 2)
507
+ begin
508
+ vgg16_bram_ena <= 1'b0;
509
+ vgg16_bram_wea <= 1'b0;
510
+ circle <= 0;
511
+ if(column == columnnum - 10'b1)
512
+ begin
513
+ if(row == rownum - 10'b1)
514
+ begin
515
+ if(channel == channelnum - 10'b1)
516
+ begin
517
+ filter <= filter + 1;
518
+ channel <= 0;
519
+ end
520
+ else
521
+ begin
522
+ channel <= channel + 1;
523
+ end
524
+ row <= 0;
525
+ end
526
+ else
527
+ begin
528
+ row <= row + 1;
529
+ end
530
+ column <= 0;
531
+ end
532
+ else
533
+ begin
534
+ column <= column + 1;
535
+ end
536
+ state <= S_CHECK;
537
+ end
538
+ else
539
+ begin
540
+ circle <= circle + 1;
541
+ end
542
+ end
543
+ end
544
+ default:
545
+ begin
546
+ state <= S_IDLE;
547
+ vgg16_bram_ena <= 1'b0;
548
+ vgg16_bram_wea <= 1'b0;
549
+ end
550
+ endcase
551
+ end
552
+ end
553
+ end
554
+
555
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/fc.v ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/26/2018 08:39:53 AM
7
+ // Design Name:
8
+ // Module Name: fc
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define DATA_SIZE 8
23
+ `define CONV_SIZE 3
24
+
25
+ module fc(
26
+ clk, rst, fc_en,
27
+ weight_base_addr, bias_base_addr, data_base_addr, result_base_addr, rownum, columnnum,
28
+ vgg16_bram_douta,
29
+ vgg16_bram_ena, vgg16_bram_wea, vgg16_bram_addra, vgg16_bram_dina,
30
+ fc_finish
31
+ );
32
+ input clk;
33
+ input rst;
34
+ input fc_en;
35
+ input [19:0] weight_base_addr;
36
+ input [19:0] bias_base_addr;
37
+ input [19:0] data_base_addr;
38
+ input [19:0] result_base_addr;
39
+ input [9:0] rownum;
40
+ input [14:0] columnnum;
41
+ input [`DATA_SIZE-1:0] vgg16_bram_douta;
42
+ output reg vgg16_bram_ena;
43
+ output reg vgg16_bram_wea;
44
+ output reg [19:0] vgg16_bram_addra;
45
+ output reg [`DATA_SIZE-1:0] vgg16_bram_dina;
46
+ output reg fc_finish;
47
+
48
+ reg [`DATA_SIZE*`CONV_SIZE*`CONV_SIZE-1:0] matrix1;
49
+ reg [`DATA_SIZE*`CONV_SIZE*`CONV_SIZE-1:0] matrix2;
50
+ reg [`DATA_SIZE-1:0] bias;
51
+ reg relu_en;
52
+ wire [`DATA_SIZE-1:0] dout;
53
+ reg [`DATA_SIZE-1:0] result;
54
+
55
+ mult_add fc_ma(
56
+ .matrix1(matrix1),
57
+ .matrix2(matrix2),
58
+ .bias(bias),
59
+ .relu_en(relu_en),
60
+ .dout(dout)
61
+ );
62
+
63
+ reg [6:0] state;
64
+
65
+ parameter S_IDLE = 7'b1000000,
66
+ S_CHECK = 7'b0100000,
67
+ S_LOAD_WEIGHTS = 7'b0010000,
68
+ S_LOAD_BIAS = 7'b0001000,
69
+ S_LOAD_DATA = 7'b0000100,
70
+ S_MULTI_ADD = 7'b0000010,
71
+ S_STORE_RESULT = 7'b0000001;
72
+
73
+ integer count = 0,
74
+ row = 0,
75
+ column = 0,
76
+ circle = 0,
77
+ databegin = 0;
78
+
79
+ always@(posedge clk)
80
+ begin
81
+ if(rst == 1'b1)
82
+ begin
83
+ state <= S_IDLE;
84
+ vgg16_bram_ena <= 1'b0;
85
+ vgg16_bram_wea <= 1'b0;
86
+ end
87
+ else
88
+ begin
89
+ if(fc_en == 1'b1)
90
+ begin
91
+ case (state)
92
+ S_IDLE:
93
+ begin
94
+ row <= 0;
95
+ column <= 0;
96
+ matrix1 <= 0;
97
+ matrix2 <= 0;
98
+ bias <= 0;
99
+ relu_en <= 0;
100
+ result <= 0;
101
+ fc_finish <= 0;
102
+ state <= S_CHECK;
103
+ end
104
+ S_CHECK:
105
+ begin
106
+ if(row == rownum)
107
+ begin
108
+ vgg16_bram_ena <= 1'b0;
109
+ vgg16_bram_wea <= 1'b0;
110
+ relu_en <= 1'b0;
111
+ fc_finish <= 1'b1;
112
+ state <= S_IDLE;
113
+ end
114
+ else
115
+ begin
116
+ circle <= 0;
117
+ count <= 0;
118
+ result <= 0;
119
+ state <= S_LOAD_WEIGHTS;
120
+ end
121
+ end
122
+ S_LOAD_WEIGHTS:
123
+ begin
124
+ if(count < `CONV_SIZE * `CONV_SIZE)
125
+ begin
126
+ if(circle == 0)
127
+ begin
128
+ vgg16_bram_ena <= 1'b1;
129
+ vgg16_bram_addra <= weight_base_addr + row * rownum + column;
130
+ circle <= circle + 1;
131
+ end
132
+ else if(circle == 2)
133
+ begin
134
+ databegin = `DATA_SIZE * (`CONV_SIZE * `CONV_SIZE - count) - 1;
135
+ matrix2[databegin-:8] <= vgg16_bram_douta;
136
+ count <= count + 1;
137
+ circle <= 0;
138
+ end
139
+ else
140
+ begin
141
+ circle <= circle + 1;
142
+ end
143
+ end
144
+ else
145
+ begin
146
+ circle <= 0;
147
+ count <= 0;
148
+ column <= column - `CONV_SIZE * `CONV_SIZE;
149
+ vgg16_bram_ena <= 1'b0;
150
+ state <= S_LOAD_BIAS;
151
+ end
152
+ end
153
+ S_LOAD_BIAS:
154
+ begin
155
+ if(column < `CONV_SIZE * `CONV_SIZE)
156
+ begin
157
+ if(circle == 0)
158
+ begin
159
+ vgg16_bram_ena <= 1'b1;
160
+ vgg16_bram_addra <= bias_base_addr + row;
161
+ circle <= circle + 1;
162
+ end
163
+ else if(circle == 2)
164
+ begin
165
+ bias <= vgg16_bram_douta;
166
+ circle <= 0;
167
+ count <= 0;
168
+ vgg16_bram_ena <= 1'b0;
169
+ state <= S_LOAD_DATA;
170
+ end
171
+ end
172
+ else
173
+ begin
174
+ bias <= result;
175
+ state <= S_LOAD_DATA;
176
+ end
177
+ end
178
+ S_LOAD_DATA:
179
+ begin
180
+ if(count < `CONV_SIZE * `CONV_SIZE)
181
+ begin
182
+ if(circle == 0)
183
+ begin
184
+ vgg16_bram_ena <= 1'b1;
185
+ vgg16_bram_addra <= data_base_addr + column;
186
+ circle <= circle + 1;
187
+ end
188
+ else if(circle == 3)
189
+ begin
190
+ databegin = `DATA_SIZE * (`CONV_SIZE * `CONV_SIZE - count) - 1;
191
+ matrix1[databegin-:8] <= vgg16_bram_douta;
192
+ count <= count + 1;
193
+ circle <= 0;
194
+ column <= column + 1;
195
+ end
196
+ else
197
+ begin
198
+ circle <= circle + 1;
199
+ end
200
+ end
201
+ end
202
+ S_MULTI_ADD:
203
+ begin
204
+ result <= result + dout;
205
+ if(column == columnnum)
206
+ begin
207
+ circle <= 0;
208
+ state <= S_STORE_RESULT;
209
+ end
210
+ else
211
+ begin
212
+ circle <= 0;
213
+ count <= 0;
214
+ state <= S_LOAD_WEIGHTS;
215
+ end
216
+ end
217
+ S_STORE_RESULT:
218
+ begin
219
+ if(circle == 0)
220
+ begin
221
+ vgg16_bram_ena <= 1'b1;
222
+ vgg16_bram_wea <= 1'b1;
223
+ vgg16_bram_addra <= result_base_addr + row;
224
+ vgg16_bram_dina <= result;
225
+ circle <= circle + 1;
226
+ end
227
+ else if(circle == 2)
228
+ begin
229
+ vgg16_bram_ena <= 1'b0;
230
+ vgg16_bram_wea <= 1'b0;
231
+ circle <= 0;
232
+ column <= 0;
233
+ count <= 0;
234
+ row <= row + 1;
235
+ state <= S_CHECK;
236
+ end
237
+ else
238
+ begin
239
+ circle <= circle + 1;
240
+ end
241
+ end
242
+ default:
243
+ begin
244
+ state <= S_IDLE;
245
+ vgg16_bram_ena <= 1'b0;
246
+ vgg16_bram_wea <= 1'b0;
247
+ end
248
+ endcase
249
+ end
250
+ end
251
+ end
252
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/max_pool.v ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/21/2018 09:49:17 PM
7
+ // Design Name:
8
+ // Module Name: max_pool
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define DATA_SIZE 8
23
+
24
+ module max_pool(
25
+ din, dout
26
+ );
27
+ input [4*`DATA_SIZE-1:0] din;
28
+ output [`DATA_SIZE-1:0] dout;
29
+
30
+ wire [`DATA_SIZE-1:0] max_1;
31
+ wire [`DATA_SIZE-1:0] max_2;
32
+
33
+ assign max_1 = ((din[4*`DATA_SIZE-1] == 1'b1) && (din[3*`DATA_SIZE-1] == 1'b0) || ((din[4*`DATA_SIZE-1] == din[3*`DATA_SIZE-1]) && (din[4*`DATA_SIZE-1:3*`DATA_SIZE] < din[3*`DATA_SIZE-1:2*`DATA_SIZE]))) ? din[3*`DATA_SIZE-1:2*`DATA_SIZE] : din[4*`DATA_SIZE-1:3*`DATA_SIZE];
34
+
35
+ assign max_2 = ((max_1[`DATA_SIZE-1] == 1'b1) && (din[2*`DATA_SIZE-1] == 1'b0) || ((max_1[`DATA_SIZE-1] == din[2*`DATA_SIZE-1]) && (max_1 < din[2*`DATA_SIZE-1:`DATA_SIZE]))) ? din[2*`DATA_SIZE-1:`DATA_SIZE] : max_1;
36
+
37
+ assign dout = ((max_2[`DATA_SIZE-1] == 1'b1) && (din[`DATA_SIZE-1] == 1'b0) || ((max_2[`DATA_SIZE-1] == din[`DATA_SIZE-1]) && (max_2 < din[`DATA_SIZE-1:0]))) ? din[`DATA_SIZE-1:0] : max_2;
38
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/mult_add.v ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/20/2018 03:04:16 AM
7
+ // Design Name:
8
+ // Module Name: mult_add
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define LENGTH 3
23
+ `define WIDTH 3
24
+ `define DATA_SIZE 8
25
+
26
+ module mult_add(
27
+ matrix1, matrix2, bias, relu_en, dout
28
+ );
29
+ input [`DATA_SIZE*`LENGTH*`WIDTH-1:0] matrix1;
30
+ input [`DATA_SIZE*`LENGTH*`WIDTH-1:0] matrix2;
31
+ input [`DATA_SIZE-1:0] bias;
32
+ input relu_en;
33
+ output [`DATA_SIZE-1:0] dout;
34
+
35
+ reg [2*`DATA_SIZE-1:0] temp;
36
+ reg sign;
37
+ reg [`DATA_SIZE-1:0] item1;
38
+ reg [`DATA_SIZE-1:0] item2;
39
+ reg [2*(`DATA_SIZE-1)-1:0] midvalue;
40
+ reg [`DATA_SIZE-1:0] result;
41
+ wire [`DATA_SIZE-1:0] relu_result;
42
+
43
+ relu ma_relu(
44
+ .din(result),
45
+ .dout(relu_result)
46
+ );
47
+
48
+ integer i = 0;
49
+ integer data_begin = 0;
50
+
51
+ assign dout = (relu_en == 1'b1) ? relu_result : result;
52
+
53
+ always@(*)
54
+ begin
55
+ temp = 8'b0;
56
+ sign = 1'b0;
57
+ item1 = 8'b0;
58
+ item2 = 8'b0;
59
+ midvalue = 14'b0;
60
+ for(i = 0; i < `LENGTH*`WIDTH; i = i + 1)
61
+ begin
62
+ data_begin = `DATA_SIZE * (`LENGTH * `WIDTH - i) - 1;
63
+ sign = matrix1[data_begin] ^ matrix2[data_begin];
64
+ item1 = (matrix1[data_begin] == 1'b0) ? matrix1[data_begin-:8] : {matrix1[data_begin], ~matrix1[(data_begin-1)-:7] + 1'b1};
65
+ item2 = (matrix2[data_begin] == 1'b0) ? matrix2[data_begin-:8] : {matrix2[data_begin], ~matrix2[(data_begin-1)-:7] + 1'b1};
66
+ midvalue = item1[`DATA_SIZE-2:0] * item2[`DATA_SIZE-2:0];
67
+ temp = temp + {sign, midvalue, 1'b0};
68
+ end
69
+ temp = temp + {bias, 8'b0};
70
+ result = temp >> 8;
71
+ end
72
+
73
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/pool.v ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/26/2018 08:05:24 AM
7
+ // Design Name:
8
+ // Module Name: pool
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define DATA_SIZE 8
23
+ `define POOL_SIZE 2
24
+
25
+ module pool(
26
+ clk, rst, pool_en,
27
+ rownum, columnnum, channelnum,
28
+ data_base_addr, result_base_addr,
29
+ vgg16_bram_douta,
30
+ vgg16_bram_ena, vgg16_bram_wea, vgg16_bram_addra, vgg16_bram_dina,
31
+ pool_finish
32
+ );
33
+ input clk;
34
+ input rst;
35
+ input pool_en;
36
+ input [9:0] rownum;
37
+ input [9:0] columnnum;
38
+ input [9:0] channelnum;
39
+ input [19:0] data_base_addr;
40
+ input [19:0] result_base_addr;
41
+ input [`DATA_SIZE-1:0] vgg16_bram_douta;
42
+ output reg vgg16_bram_ena;
43
+ output reg vgg16_bram_wea;
44
+ output reg [19:0] vgg16_bram_addra;
45
+ output reg [`DATA_SIZE-1:0] vgg16_bram_dina;
46
+ output reg pool_finish;
47
+
48
+ reg [4*`DATA_SIZE-1:0] din;
49
+ wire [`DATA_SIZE-1:0] dout;
50
+
51
+ reg [`DATA_SIZE-1:0] result;
52
+
53
+ max_pool pool_mp(
54
+ .din(din),
55
+ .dout(dout)
56
+ );
57
+
58
+ reg [4:0] state;
59
+
60
+ parameter S_IDLE = 5'b10000,
61
+ S_CHECK = 5'b01000,
62
+ S_LOAD_DATA = 5'b00100,
63
+ S_POOLING = 5'b00010,
64
+ S_STORE_RESULT = 5'b00001;
65
+
66
+ integer count = 0,
67
+ circle = 0,
68
+ row = 0,
69
+ column = 0,
70
+ channel = 0,
71
+ databegin = 0;
72
+
73
+ always@(posedge clk)
74
+ begin
75
+ if(rst == 1'b1)
76
+ begin
77
+ state <= S_IDLE;
78
+ vgg16_bram_ena <= 1'b0;
79
+ vgg16_bram_wea <= 1'b0;
80
+ end
81
+ else
82
+ begin
83
+ if(pool_en == 1'b1)
84
+ begin
85
+ case (state)
86
+ S_IDLE:
87
+ begin
88
+ channel <= 0;
89
+ circle <= 0;
90
+ row <= 0;
91
+ column <= 0;
92
+ din <= 0;
93
+ result <= 0;
94
+ pool_finish <= 1'b0;
95
+ state <= S_CHECK;
96
+ end
97
+ S_CHECK:
98
+ begin
99
+ if(channel == channelnum)
100
+ begin
101
+ vgg16_bram_ena <= 1'b0;
102
+ vgg16_bram_wea <= 1'b0;
103
+ pool_finish <= 1'b1;
104
+ state <= S_IDLE;
105
+ end
106
+ else
107
+ begin
108
+ circle <= 0;
109
+ count <= 0;
110
+ state <= S_LOAD_DATA;
111
+ end
112
+ end
113
+ S_LOAD_DATA:
114
+ begin
115
+ if(count < `POOL_SIZE * `POOL_SIZE)
116
+ begin
117
+ if(circle == 0)
118
+ begin
119
+ vgg16_bram_ena <= 1'b1;
120
+ vgg16_bram_addra <= data_base_addr + channel * rownum * columnnum + (row*2 + (count / `POOL_SIZE)) * rownum + column*2 + count % `POOL_SIZE;
121
+ circle <= circle + 1;
122
+ end
123
+ else if(circle == 2)
124
+ begin
125
+ databegin = `DATA_SIZE * (`POOL_SIZE * `POOL_SIZE - count) - 1;
126
+ din[databegin-:8] <= vgg16_bram_douta;
127
+ count <= count + 1;
128
+ circle <= 0;
129
+ end
130
+ else
131
+ begin
132
+ circle <= circle + 1;
133
+ end
134
+ end
135
+ else
136
+ begin
137
+ circle <= 0;
138
+ count <= 0;
139
+ vgg16_bram_ena <= 1'b0;
140
+ state <= S_POOLING;
141
+ end
142
+ end
143
+ S_POOLING:
144
+ begin
145
+ result <= dout;
146
+ circle <= 0;
147
+ state <= S_STORE_RESULT;
148
+ end
149
+ S_STORE_RESULT:
150
+ begin
151
+ if(circle == 0)
152
+ begin
153
+ vgg16_bram_ena <= 1'b1;
154
+ vgg16_bram_wea <= 1'b1;
155
+ vgg16_bram_addra <= result_base_addr + channel * rownum * columnnum + row * rownum + column;
156
+ vgg16_bram_dina <= result;
157
+ circle <= circle + 1;
158
+ end
159
+ else if(circle == 2)
160
+ begin
161
+ vgg16_bram_ena <= 1'b0;
162
+ vgg16_bram_wea <= 1'b0;
163
+ circle <= 0;
164
+ if(column == columnnum - 10'b1)
165
+ begin
166
+ if(row == rownum - 10'b1)
167
+ begin
168
+ row <= 0;
169
+ channel <= channel + 1;
170
+ end
171
+ else
172
+ begin
173
+ row <= row + 1;
174
+ end
175
+ column <= 0;
176
+ end
177
+ else
178
+ begin
179
+ column <= column + 1;
180
+ end
181
+ end
182
+ else
183
+ begin
184
+ circle <= circle + 1;
185
+ end
186
+ end
187
+ default:
188
+ begin
189
+ state <= S_IDLE;
190
+ vgg16_bram_ena <= 1'b0;
191
+ vgg16_bram_wea <= 1'b0;
192
+ end
193
+ endcase
194
+ end
195
+ end
196
+ end
197
+
198
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/relu.v ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/21/2018 09:44:48 PM
7
+ // Design Name:
8
+ // Module Name: relu
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+ `define DATA_SIZE 8
23
+
24
+ module relu(
25
+ din, dout
26
+ );
27
+ input [`DATA_SIZE-1:0] din;
28
+ output [`DATA_SIZE-1:0] dout;
29
+
30
+ assign dout = (din[`DATA_SIZE-1] == 1'b1) ? 8'b0 : din;
31
+ endmodule
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/vgg16.c ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+
2
+
3
+ /***************************** Include Files *******************************/
4
+ #include "vgg16.h"
5
+
6
+ /************************** Function Definitions ***************************/
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/vgg16.h ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #ifndef VGG16_H
3
+ #define VGG16_H
4
+
5
+
6
+ /****************** Include Files ********************/
7
+ #include "xil_types.h"
8
+ #include "xstatus.h"
9
+
10
+ #define VGG16_S00_AXI_SLV_REG0_OFFSET 0
11
+ #define VGG16_S00_AXI_SLV_REG1_OFFSET 4
12
+ #define VGG16_S00_AXI_SLV_REG2_OFFSET 8
13
+ #define VGG16_S00_AXI_SLV_REG3_OFFSET 12
14
+ #define VGG16_S00_AXI_SLV_REG4_OFFSET 16
15
+ #define VGG16_S00_AXI_SLV_REG5_OFFSET 20
16
+ #define VGG16_S00_AXI_SLV_REG6_OFFSET 24
17
+ #define VGG16_S00_AXI_SLV_REG7_OFFSET 28
18
+ #define VGG16_S00_AXI_SLV_REG8_OFFSET 32
19
+ #define VGG16_S00_AXI_SLV_REG9_OFFSET 36
20
+
21
+
22
+ /**************************** Type Definitions *****************************/
23
+ /**
24
+ *
25
+ * Write a value to a VGG16 register. A 32 bit write is performed.
26
+ * If the component is implemented in a smaller width, only the least
27
+ * significant data is written.
28
+ *
29
+ * @param BaseAddress is the base address of the VGG16device.
30
+ * @param RegOffset is the register offset from the base to write to.
31
+ * @param Data is the data written to the register.
32
+ *
33
+ * @return None.
34
+ *
35
+ * @note
36
+ * C-style signature:
37
+ * void VGG16_mWriteReg(u32 BaseAddress, unsigned RegOffset, u32 Data)
38
+ *
39
+ */
40
+ #define VGG16_mWriteReg(BaseAddress, RegOffset, Data) \
41
+ Xil_Out32((BaseAddress) + (RegOffset), (u32)(Data))
42
+
43
+ /**
44
+ *
45
+ * Read a value from a VGG16 register. A 32 bit read is performed.
46
+ * If the component is implemented in a smaller width, only the least
47
+ * significant data is read from the register. The most significant data
48
+ * will be read as 0.
49
+ *
50
+ * @param BaseAddress is the base address of the VGG16 device.
51
+ * @param RegOffset is the register offset from the base to write to.
52
+ *
53
+ * @return Data is the data from the register.
54
+ *
55
+ * @note
56
+ * C-style signature:
57
+ * u32 VGG16_mReadReg(u32 BaseAddress, unsigned RegOffset)
58
+ *
59
+ */
60
+ #define VGG16_mReadReg(BaseAddress, RegOffset) \
61
+ Xil_In32((BaseAddress) + (RegOffset))
62
+
63
+ /************************** Function Prototypes ****************************/
64
+ /**
65
+ *
66
+ * Run a self-test on the driver/device. Note this may be a destructive test if
67
+ * resets of the device are performed.
68
+ *
69
+ * If the hardware system is not built correctly, this function may never
70
+ * return to the caller.
71
+ *
72
+ * @param baseaddr_p is the base address of the VGG16 instance to be worked on.
73
+ *
74
+ * @return
75
+ *
76
+ * - XST_SUCCESS if all self-test code passed
77
+ * - XST_FAILURE if any self-test code failed
78
+ *
79
+ * @note Caching must be turned off for this function to work.
80
+ * @note Self test may fail if data memory and device are not on the same bus.
81
+ *
82
+ */
83
+ XStatus VGG16_Reg_SelfTest(void * baseaddr_p);
84
+
85
+ #endif // VGG16_H
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/vgg16_selftest.c ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ /***************************** Include Files *******************************/
3
+ #include "vgg16.h"
4
+ #include "xparameters.h"
5
+ #include "stdio.h"
6
+ #include "xil_io.h"
7
+
8
+ /************************** Constant Definitions ***************************/
9
+ #define READ_WRITE_MUL_FACTOR 0x10
10
+
11
+ /************************** Function Definitions ***************************/
12
+ /**
13
+ *
14
+ * Run a self-test on the driver/device. Note this may be a destructive test if
15
+ * resets of the device are performed.
16
+ *
17
+ * If the hardware system is not built correctly, this function may never
18
+ * return to the caller.
19
+ *
20
+ * @param baseaddr_p is the base address of the VGG16instance to be worked on.
21
+ *
22
+ * @return
23
+ *
24
+ * - XST_SUCCESS if all self-test code passed
25
+ * - XST_FAILURE if any self-test code failed
26
+ *
27
+ * @note Caching must be turned off for this function to work.
28
+ * @note Self test may fail if data memory and device are not on the same bus.
29
+ *
30
+ */
31
+ XStatus VGG16_Reg_SelfTest(void * baseaddr_p)
32
+ {
33
+ u32 baseaddr;
34
+ int write_loop_index;
35
+ int read_loop_index;
36
+ int Index;
37
+
38
+ baseaddr = (u32) baseaddr_p;
39
+
40
+ xil_printf("******************************\n\r");
41
+ xil_printf("* User Peripheral Self Test\n\r");
42
+ xil_printf("******************************\n\n\r");
43
+
44
+ /*
45
+ * Write to user logic slave module register(s) and read back
46
+ */
47
+ xil_printf("User logic slave module test...\n\r");
48
+
49
+ for (write_loop_index = 0 ; write_loop_index < 4; write_loop_index++)
50
+ VGG16_mWriteReg (baseaddr, write_loop_index*4, (write_loop_index+1)*READ_WRITE_MUL_FACTOR);
51
+ for (read_loop_index = 0 ; read_loop_index < 4; read_loop_index++)
52
+ if ( VGG16_mReadReg (baseaddr, read_loop_index*4) != (read_loop_index+1)*READ_WRITE_MUL_FACTOR){
53
+ xil_printf ("Error reading register value at address %x\n", (int)baseaddr + read_loop_index*4);
54
+ return XST_FAILURE;
55
+ }
56
+
57
+ xil_printf(" - slave register write/read passed\n\n\r");
58
+
59
+ return XST_SUCCESS;
60
+ }
zhan6841_FPGA-Accelerator-for-AES-LeNet-VGG16/VGG16/vgg16_top.v ADDED
@@ -0,0 +1,790 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 01/24/2018 09:57:07 PM
7
+ // Design Name:
8
+ // Module Name: vgg16_top
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool Versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+
22
+
23
+ module vgg16_top(
24
+ clk, rst,
25
+ layer, vgg16_start, addbefore,
26
+ loaddma_start, base_addr, num,
27
+ doutb_in,
28
+ storedma_start,
29
+ clkb_out, rstb_out, enb_out, web_out, addrb_out, dinb_out,
30
+ loaddma_finish,
31
+ storedma_finish,
32
+ vgg16_finish
33
+ );
34
+ input clk;
35
+ input rst;
36
+ input [4:0] layer;
37
+ input vgg16_start;
38
+ input addbefore;
39
+ input loaddma_start;
40
+ input [19:0] base_addr;
41
+ input [31:0] num;
42
+ input [31:0] doutb_in;
43
+ input storedma_start;
44
+ output clkb_out;
45
+ output rstb_out;
46
+ output enb_out;
47
+ output [3:0] web_out;
48
+ output [31:0] addrb_out;
49
+ output [31:0] dinb_out;
50
+ output loaddma_finish;
51
+ output storedma_finish;
52
+ output reg vgg16_finish;
53
+
54
+ reg vgg16_bram_ena;
55
+ reg vgg16_bram_wea;
56
+ reg [19:0] vgg16_bram_addra;
57
+ reg [`DATA_SIZE-1:0] vgg16_bram_dina;
58
+
59
+ wire [`DATA_SIZE:0] vgg16_bram_douta;
60
+
61
+ bram_top vgg16_bram(
62
+ .clk(clk),
63
+ .rst(rst),
64
+ .loaddma_start(loaddma_start),
65
+ .base_addr(base_addr),
66
+ .num(num),
67
+ .doutb_in(doutb_in),
68
+ .data_ena(vgg16_bram_ena),
69
+ .data_wea(vgg16_bram_wea),
70
+ .data_addra(vgg16_bram_addra),
71
+ .data_dina(vgg16_bram_dina),
72
+ .storedma_start(storedma_start),
73
+ .clkb_out(clkb_out),
74
+ .rstb_out(rstb_out),
75
+ .enb_out(enb_out),
76
+ .web_out(web_out),
77
+ .addrb_out(addrb_out),
78
+ .dinb_out(dinb_out),
79
+ .loaddma_finish(loaddma_finish),
80
+ .vgg16_bram_douta(vgg16_bram_douta),
81
+ .storedma_finish(storedma_finish)
82
+ );
83
+
84
+ reg [9:0] rownum;
85
+ reg [9:0] columnnum;
86
+ reg [9:0] channelnum;
87
+ reg [9:0] filternum;
88
+ reg [19:0] weight_base_addr;
89
+ reg [19:0] bias_base_addr;
90
+ reg [19:0] data_base_addr;
91
+ reg [19:0] result_base_addr;
92
+
93
+ reg conv_en;
94
+ reg [`DATA_SIZE-1:0] conv_vgg16_bram_douta;
95
+ wire conv_vgg16_bram_ena;
96
+ wire [0:0] conv_vgg16_bram_wea;
97
+ wire [19:0] conv_vgg16_bram_addra;
98
+ wire [`DATA_SIZE-1:0] conv_vgg16_bram_dina;
99
+ wire conv_finish;
100
+
101
+ conv vgg16_conv(
102
+ .clk(clk),
103
+ .rst(rst),
104
+ .conv_en(conv_en),
105
+ .vgg16_bram_douta(conv_vgg16_bram_douta),
106
+ .weight_base_addr(weight_base_addr),
107
+ .bias_base_addr(bias_base_addr),
108
+ .data_base_addr(data_base_addr),
109
+ .result_base_addr(result_base_addr),
110
+ .rownum(rownum),
111
+ .columnnum(columnnum),
112
+ .channelnum(channelnum),
113
+ .filternum(filternum),
114
+ .vgg16_bram_ena(conv_vgg16_bram_ena),
115
+ .vgg16_bram_wea(conv_vgg16_bram_wea),
116
+ .vgg16_bram_addra(conv_vgg16_bram_addra),
117
+ .vgg16_bram_dina(conv_vgg16_bram_dina),
118
+ .conv_finish(conv_finish)
119
+ );
120
+
121
+ reg pool_en;
122
+ reg [`DATA_SIZE-1:0] pool_vgg16_bram_douta;
123
+ wire pool_vgg16_bram_ena;
124
+ wire [0:0] pool_vgg16_bram_wea;
125
+ wire [19:0] pool_vgg16_bram_addra;
126
+ wire [`DATA_SIZE-1:0] pool_vgg16_bram_dina;
127
+ wire pool_finish;
128
+
129
+ pool vgg16_pool(
130
+ .clk(clk),
131
+ .rst(rst),
132
+ .pool_en(pool_en),
133
+ .rownum(rownum),
134
+ .columnnum(columnnum),
135
+ .channelnum(channelnum),
136
+ .data_base_addr(data_base_addr),
137
+ .result_base_addr(result_base_addr),
138
+ .vgg16_bram_douta(pool_vgg16_bram_douta),
139
+ .vgg16_bram_ena(pool_vgg16_bram_ena),
140
+ .vgg16_bram_wea(pool_vgg16_bram_wea),
141
+ .vgg16_bram_addra(pool_vgg16_bram_addra),
142
+ .vgg16_bram_dina(pool_vgg16_bram_dina),
143
+ .pool_finish(pool_finish)
144
+ );
145
+
146
+ reg fc_en;
147
+ reg [`DATA_SIZE-1:0] fc_vgg16_bram_douta;
148
+ wire fc_vgg16_bram_ena;
149
+ wire [0:0] fc_vgg16_bram_wea;
150
+ wire [19:0] fc_vgg16_bram_addra;
151
+ wire [`DATA_SIZE-1:0] fc_vgg16_bram_dina;
152
+ wire fc_finish;
153
+
154
+ reg [14:0] fc_columnnum;
155
+
156
+ fc vgg16_fc(
157
+ .clk(clk),
158
+ .rst(rst),
159
+ .fc_en(fc_en),
160
+ .weight_base_addr(weight_base_addr),
161
+ .bias_base_addr(bias_base_addr),
162
+ .data_base_addr(data_base_addr),
163
+ .result_base_addr(result_base_addr),
164
+ .rownum(rownum),
165
+ .columnnum(fc_columnnum),
166
+ .vgg16_bram_douta(fc_vgg16_bram_douta),
167
+ .vgg16_bram_ena(fc_vgg16_bram_ena),
168
+ .vgg16_bram_wea(fc_vgg16_bram_wea),
169
+ .vgg16_bram_addra(fc_vgg16_bram_addra),
170
+ .vgg16_bram_dina(fc_vgg16_bram_dina),
171
+ .fc_finish(fc_finish)
172
+ );
173
+
174
+ parameter conv1_1_data_base_addr = 0,
175
+ conv1_1_weight_base_addr = 150528,
176
+ conv1_1_bias_base_addr = 150636,
177
+ conv1_1_result_base_addr = 150640;
178
+
179
+ parameter conv1_2_data_base_addr = 0,
180
+ conv1_2_weight_base_addr = 200704,
181
+ conv1_2_bias_base_addr = 200848,
182
+ conv1_2_result_base_addr = 200852;
183
+
184
+ parameter conv2_data_base_addr = 0,
185
+ conv2_weight_base_addr = 200704,
186
+ conv2_bias_base_addr = 203008,
187
+ conv2_result_base_addr = 203024;
188
+
189
+ parameter conv3_data_base_addr = 0,
190
+ conv3_weight_base_addr = 200704,
191
+ conv3_bias_base_addr = 237568,
192
+ conv3_result_base_addr = 237632;
193
+
194
+ parameter conv4_data_base_addr = 0,
195
+ conv4_weight_base_addr = 200704,
196
+ conv4_bias_base_addr = 348160,
197
+ conv4_result_base_addr = 348224;
198
+
199
+ parameter conv5_data_base_addr = 0,
200
+ conv5_weight_base_addr = 100352,
201
+ conv5_bias_base_addr = 395264,
202
+ conv5_result_base_addr = 395328;
203
+
204
+ parameter pool1_data_base_addr = 0,
205
+ pool1_result_base_addr = 200704;
206
+
207
+ parameter pool2_data_base_addr = 0,
208
+ pool2_result_base_addr = 200704;
209
+
210
+ parameter pool3_data_base_addr = 0,
211
+ pool3_result_base_addr = 200704;
212
+
213
+ parameter pool4_data_base_addr = 0,
214
+ pool4_result_base_addr = 200704;
215
+
216
+ parameter pool5_data_base_addr = 0,
217
+ pool5_result_base_addr = 100352;
218
+
219
+ parameter fc1_data_base_addr = 0,
220
+ fc1_weight_base_addr = 25088,
221
+ fc1_bias_base_addr = 426496,
222
+ fc1_result_base_addr = 426512;
223
+
224
+ parameter fc2_data_base_addr = 0,
225
+ fc2_weight_base_addr = 4096,
226
+ fc2_bias_base_addr = 528384,
227
+ fc2_result_base_addr = 528512;
228
+
229
+ parameter fc3_data_base_addr = 0,
230
+ fc3_weight_base_addr = 4096,
231
+ fc3_bias_base_addr = 516096,
232
+ fc3_result_base_addr = 516221;
233
+
234
+ reg [21:0] state;
235
+
236
+ parameter S_IDLE = 22'b1000_0000_0000_0000_0000_00,
237
+ S_CONV1_1 = 22'b0100_0000_0000_0000_0000_00,
238
+ S_CONV1_2 = 22'b0010_0000_0000_0000_0000_00,
239
+ S_POOL1 = 22'b0001_0000_0000_0000_0000_00,
240
+ S_CONV2_1 = 22'b0000_1000_0000_0000_0000_00,
241
+ S_CONV2_2 = 22'b0000_0100_0000_0000_0000_00,
242
+ S_POOL2 = 22'b0000_0010_0000_0000_0000_00,
243
+ S_CONV3_1 = 22'b0000_0001_0000_0000_0000_00,
244
+ S_CONV3_2 = 22'b0000_0000_1000_0000_0000_00,
245
+ S_CONV3_3 = 22'b0000_0000_0100_0000_0000_00,
246
+ S_POOL3 = 22'b0000_0000_0010_0000_0000_00,
247
+ S_CONV4_1 = 22'b0000_0000_0001_0000_0000_00,
248
+ S_CONV4_2 = 22'b0000_0000_0000_1000_0000_00,
249
+ S_CONV4_3 = 22'b0000_0000_0000_0100_0000_00,
250
+ S_POOL4 = 22'b0000_0000_0000_0010_0000_00,
251
+ S_CONV5_1 = 22'b0000_0000_0000_0001_0000_00,
252
+ S_CONV5_2 = 22'b0000_0000_0000_0000_1000_00,
253
+ S_CONV5_3 = 22'b0000_0000_0000_0000_0100_00,
254
+ S_POOL5 = 22'b0000_0000_0000_0000_0010_00,
255
+ S_FC1 = 22'b0000_0000_0000_0000_0001_00,
256
+ S_FC2 = 22'b0000_0000_0000_0000_0000_10,
257
+ S_FC3 = 22'b0000_0000_0000_0000_0000_01;
258
+
259
+ always@(posedge clk)
260
+ begin
261
+ if(rst == 1'b1)
262
+ begin
263
+ state <= S_IDLE;
264
+ conv_en <= 1'b0;
265
+ pool_en <= 1'b0;
266
+ fc_en <= 1'b0;
267
+ end
268
+ else
269
+ begin
270
+ case (state)
271
+ S_IDLE:
272
+ begin
273
+ if(vgg16_start == 1'b1)
274
+ begin
275
+ vgg16_finish <= 1'b0;
276
+ case (layer)
277
+ 5'b00000: // 0 conv1_1
278
+ begin
279
+ conv_en <= 1'b1;
280
+ weight_base_addr <= conv1_1_weight_base_addr;
281
+ bias_base_addr <= conv1_1_bias_base_addr;
282
+ data_base_addr <= conv1_1_data_base_addr;
283
+ result_base_addr <= conv1_1_result_base_addr;
284
+ rownum <= 10'd224;
285
+ columnnum <= 10'd224;
286
+ channelnum <= 10'd3;
287
+ filternum <= 10'd4;
288
+ state <= S_CONV1_1;
289
+ end
290
+ 5'b00001: // 1 conv1_2
291
+ begin
292
+ conv_en <= 1'b1;
293
+ weight_base_addr <= conv1_2_weight_base_addr;
294
+ bias_base_addr <= conv1_2_bias_base_addr;
295
+ data_base_addr <= conv1_2_data_base_addr;
296
+ result_base_addr <= conv1_2_result_base_addr;
297
+ rownum <= 10'd224;
298
+ columnnum <= 10'd224;
299
+ channelnum <= 10'd4;
300
+ filternum <= 10'd4;
301
+ state <= S_CONV1_2;
302
+ end
303
+ 5'b00010: // 2 pool1
304
+ begin
305
+ pool_en <= 1'b1;
306
+ rownum <= 10'd112;
307
+ columnnum <= 10'd112;
308
+ channelnum <= 10'd4;
309
+ data_base_addr <= pool1_data_base_addr;
310
+ result_base_addr <= pool1_result_base_addr;
311
+ state <= S_POOL1;
312
+ end
313
+ 5'b00011: // 3 conv2_1
314
+ begin
315
+ conv_en <= 1'b1;
316
+ weight_base_addr <= conv2_weight_base_addr;
317
+ bias_base_addr <= conv2_bias_base_addr;
318
+ data_base_addr <= conv2_data_base_addr;
319
+ result_base_addr <= conv2_result_base_addr;
320
+ rownum <= 10'd112;
321
+ columnnum <= 10'd112;
322
+ channelnum <= 10'd16;
323
+ filternum <= 10'd16;
324
+ state <= S_CONV2_1;
325
+ end
326
+ 5'b00100: // 4 conv2_2
327
+ begin
328
+ conv_en <= 1'b1;
329
+ weight_base_addr <= conv2_weight_base_addr;
330
+ bias_base_addr <= conv2_bias_base_addr;
331
+ data_base_addr <= conv2_data_base_addr;
332
+ result_base_addr <= conv2_result_base_addr;
333
+ rownum <= 10'd112;
334
+ columnnum <= 10'd112;
335
+ channelnum <= 10'd16;
336
+ filternum <= 10'd16;
337
+ state <= S_CONV2_2;
338
+ end
339
+ 5'b00101: // 5 pool2
340
+ begin
341
+ pool_en <= 1'b1;
342
+ rownum <= 10'd56;
343
+ columnnum <= 10'd56;
344
+ channelnum <= 10'd16;
345
+ data_base_addr <= pool2_data_base_addr;
346
+ result_base_addr <= pool2_result_base_addr;
347
+ state <= S_POOL2;
348
+ end
349
+ 5'b00110: // 6 conv3_1
350
+ begin
351
+ conv_en <= 1'b1;
352
+ weight_base_addr <= conv3_weight_base_addr;
353
+ bias_base_addr <= conv3_bias_base_addr;
354
+ data_base_addr <= conv3_data_base_addr;
355
+ result_base_addr <= conv3_result_base_addr;
356
+ rownum <= 10'd56;
357
+ columnnum <= 10'd56;
358
+ channelnum <= 10'd64;
359
+ filternum <= 10'd64;
360
+ state <= S_CONV3_1;
361
+ end
362
+ 5'b00111: // 7 conv3_2
363
+ begin
364
+ conv_en <= 1'b1;
365
+ weight_base_addr <= conv3_weight_base_addr;
366
+ bias_base_addr <= conv3_bias_base_addr;
367
+ data_base_addr <= conv3_data_base_addr;
368
+ result_base_addr <= conv3_result_base_addr;
369
+ rownum <= 10'd56;
370
+ columnnum <= 10'd56;
371
+ channelnum <= 10'd64;
372
+ filternum <= 10'd64;
373
+ state <= S_CONV3_2;
374
+ end
375
+ 5'b01000: // 8 conv3_3
376
+ begin
377
+ conv_en <= 1'b1;
378
+ weight_base_addr <= conv3_weight_base_addr;
379
+ bias_base_addr <= conv3_bias_base_addr;
380
+ data_base_addr <= conv3_data_base_addr;
381
+ result_base_addr <= conv3_result_base_addr;
382
+ rownum <= 10'd56;
383
+ columnnum <= 10'd56;
384
+ channelnum <= 10'd64;
385
+ filternum <= 10'd64;
386
+ state <= S_CONV3_3;
387
+ end
388
+ 5'b01001: // 9 pool3
389
+ begin
390
+ pool_en <= 1'b1;
391
+ rownum <= 10'd28;
392
+ columnnum <= 10'd28;
393
+ channelnum <= 10'd64;
394
+ data_base_addr <= pool3_data_base_addr;
395
+ result_base_addr <= pool3_result_base_addr;
396
+ state <= S_POOL3;
397
+ end
398
+ 5'b01010: // 10 conv4_1
399
+ begin
400
+ conv_en <= 1'b1;
401
+ weight_base_addr <= conv4_weight_base_addr;
402
+ bias_base_addr <= conv4_bias_base_addr;
403
+ data_base_addr <= conv4_data_base_addr;
404
+ result_base_addr <= conv4_result_base_addr;
405
+ rownum <= 10'd28;
406
+ columnnum <= 10'd28;
407
+ channelnum <= 10'd256;
408
+ filternum <= 10'd64;
409
+ state <= S_CONV4_1;
410
+ end
411
+ 5'b01011: // 11 conv4_2
412
+ begin
413
+ conv_en <= 1'b1;
414
+ weight_base_addr <= conv4_weight_base_addr;
415
+ bias_base_addr <= conv4_bias_base_addr;
416
+ data_base_addr <= conv4_data_base_addr;
417
+ result_base_addr <= conv4_result_base_addr;
418
+ rownum <= 10'd28;
419
+ columnnum <= 10'd28;
420
+ channelnum <= 10'd256;
421
+ filternum <= 10'd64;
422
+ state <= S_CONV4_2;
423
+ end
424
+ 5'b01100: // 12 conv4_3
425
+ begin
426
+ conv_en <= 1'b1;
427
+ weight_base_addr <= conv4_weight_base_addr;
428
+ bias_base_addr <= conv4_bias_base_addr;
429
+ data_base_addr <= conv4_data_base_addr;
430
+ result_base_addr <= conv4_result_base_addr;
431
+ rownum <= 10'd28;
432
+ columnnum <= 10'd28;
433
+ channelnum <= 10'd256;
434
+ filternum <= 10'd64;
435
+ state <= S_CONV4_3;
436
+ end
437
+ 5'b01101: // 13 pool4
438
+ begin
439
+ pool_en <= 1'b1;
440
+ rownum <= 10'd14;
441
+ columnnum <= 10'd14;
442
+ channelnum <= 10'd256;
443
+ data_base_addr <= pool4_data_base_addr;
444
+ result_base_addr <= pool4_result_base_addr;
445
+ state <= S_POOL4;
446
+ end
447
+ 5'b01110: // 14 conv5_1
448
+ begin
449
+ conv_en <= 1'b1;
450
+ weight_base_addr <= conv5_weight_base_addr;
451
+ bias_base_addr <= conv5_bias_base_addr;
452
+ data_base_addr <= conv5_data_base_addr;
453
+ result_base_addr <= conv5_result_base_addr;
454
+ rownum <= 10'd14;
455
+ columnnum <= 10'd14;
456
+ channelnum <= 10'd512;
457
+ filternum <= 10'd64;
458
+ state <= S_CONV5_1;
459
+ end
460
+ 5'b01111: // 15 conv5_2
461
+ begin
462
+ conv_en <= 1'b1;
463
+ weight_base_addr <= conv5_weight_base_addr;
464
+ bias_base_addr <= conv5_bias_base_addr;
465
+ data_base_addr <= conv5_data_base_addr;
466
+ result_base_addr <= conv5_result_base_addr;
467
+ rownum <= 10'd14;
468
+ columnnum <= 10'd14;
469
+ channelnum <= 10'd512;
470
+ filternum <= 10'd64;
471
+ state <= S_CONV5_2;
472
+ end
473
+ 5'b10000: // 16 conv5_3
474
+ begin
475
+ conv_en <= 1'b1;
476
+ weight_base_addr <= conv5_weight_base_addr;
477
+ bias_base_addr <= conv5_bias_base_addr;
478
+ data_base_addr <= conv5_data_base_addr;
479
+ result_base_addr <= conv5_result_base_addr;
480
+ rownum <= 10'd14;
481
+ columnnum <= 10'd14;
482
+ channelnum <= 10'd512;
483
+ filternum <= 10'd64;
484
+ state <= S_CONV5_3;
485
+ end
486
+ 5'b10001: // 17 pool5
487
+ begin
488
+ pool_en <= 1'b1;
489
+ rownum <= 10'd7;
490
+ columnnum <= 10'd7;
491
+ channelnum <= 10'd512;
492
+ data_base_addr <= pool5_data_base_addr;
493
+ result_base_addr <= pool5_result_base_addr;
494
+ state <= S_POOL5;
495
+ end
496
+ 5'b10010: // 18 fc1
497
+ begin
498
+ fc_en <= 1'b1;
499
+ weight_base_addr <= fc1_weight_base_addr;
500
+ bias_base_addr <= fc1_bias_base_addr;
501
+ data_base_addr <= fc1_data_base_addr;
502
+ result_base_addr <= fc1_result_base_addr;
503
+ rownum <= 10'd16;
504
+ fc_columnnum <= 15'd25088;
505
+ state <= S_FC1;
506
+ end
507
+ 5'b10011: // 19 fc2
508
+ begin
509
+ fc_en <= 1'b1;
510
+ weight_base_addr <= fc2_weight_base_addr;
511
+ bias_base_addr <= fc2_bias_base_addr;
512
+ data_base_addr <= fc2_data_base_addr;
513
+ result_base_addr <= fc2_result_base_addr;
514
+ rownum <= 10'd128;
515
+ fc_columnnum <= 15'd4096;
516
+ state <= S_FC2;
517
+ end
518
+ 5'b10100: // 20 fc3
519
+ begin
520
+ fc_en <= 1'b1;
521
+ weight_base_addr <= fc3_weight_base_addr;
522
+ bias_base_addr <= fc3_bias_base_addr;
523
+ data_base_addr <= fc3_data_base_addr;
524
+ result_base_addr <= fc3_result_base_addr;
525
+ rownum <= 10'd125;
526
+ fc_columnnum <= 15'd4096;
527
+ state <= S_FC3;
528
+ end
529
+ default:
530
+ begin
531
+ conv_en <= 1'b0;
532
+ pool_en <= 1'b0;
533
+ fc_en <= 1'b0;
534
+ state <= S_IDLE;
535
+ end
536
+ endcase
537
+ end
538
+ else
539
+ begin
540
+ vgg16_finish <= 1'b0;
541
+ state <= S_IDLE;
542
+ end
543
+ end
544
+ S_CONV1_1:
545
+ begin
546
+ if(conv_finish == 1'b1)
547
+ begin
548
+ conv_en <= 1'b0;
549
+ vgg16_finish <= 1'b1;
550
+ state <= S_IDLE;
551
+ end
552
+ end
553
+ S_CONV1_2:
554
+ begin
555
+ if(conv_finish == 1'b1)
556
+ begin
557
+ conv_en <= 1'b0;
558
+ vgg16_finish <= 1'b1;
559
+ state <= S_IDLE;
560
+ end
561
+ end
562
+ S_POOL1:
563
+ begin
564
+ if(pool_finish == 1'b1)
565
+ begin
566
+ pool_en <= 1'b0;
567
+ vgg16_finish <= 1'b1;
568
+ state <= S_IDLE;
569
+ end
570
+ end
571
+ S_CONV2_1:
572
+ begin
573
+ if(conv_finish == 1'b1)
574
+ begin
575
+ conv_en <= 1'b0;
576
+ vgg16_finish <= 1'b1;
577
+ state <= S_IDLE;
578
+ end
579
+ end
580
+ S_CONV2_2:
581
+ begin
582
+ if(conv_finish == 1'b1)
583
+ begin
584
+ conv_en <= 1'b0;
585
+ vgg16_finish <= 1'b1;
586
+ state <= S_IDLE;
587
+ end
588
+ end
589
+ S_POOL2:
590
+ begin
591
+ if(pool_finish == 1'b1)
592
+ begin
593
+ pool_en <= 1'b0;
594
+ vgg16_finish <= 1'b1;
595
+ state <= S_IDLE;
596
+ end
597
+ end
598
+ S_CONV3_1:
599
+ begin
600
+ if(conv_finish == 1'b1)
601
+ begin
602
+ conv_en <= 1'b0;
603
+ vgg16_finish <= 1'b1;
604
+ state <= S_IDLE;
605
+ end
606
+ end
607
+ S_CONV3_2:
608
+ begin
609
+ if(conv_finish == 1'b1)
610
+ begin
611
+ conv_en <= 1'b0;
612
+ vgg16_finish <= 1'b1;
613
+ state <= S_IDLE;
614
+ end
615
+ end
616
+ S_CONV3_3:
617
+ begin
618
+ if(conv_finish == 1'b1)
619
+ begin
620
+ conv_en <= 1'b0;
621
+ vgg16_finish <= 1'b1;
622
+ state <= S_IDLE;
623
+ end
624
+ end
625
+ S_POOL3:
626
+ begin
627
+ if(pool_finish == 1'b1)
628
+ begin
629
+ pool_en <= 1'b0;
630
+ vgg16_finish <= 1'b1;
631
+ state <= S_IDLE;
632
+ end
633
+ end
634
+ S_CONV4_1:
635
+ begin
636
+ if(conv_finish == 1'b1)
637
+ begin
638
+ conv_en <= 1'b0;
639
+ vgg16_finish <= 1'b1;
640
+ state <= S_IDLE;
641
+ end
642
+ end
643
+ S_CONV4_2:
644
+ begin
645
+ if(conv_finish == 1'b1)
646
+ begin
647
+ conv_en <= 1'b0;
648
+ vgg16_finish <= 1'b1;
649
+ state <= S_IDLE;
650
+ end
651
+ end
652
+ S_CONV4_3:
653
+ begin
654
+ if(conv_finish == 1'b1)
655
+ begin
656
+ conv_en <= 1'b0;
657
+ vgg16_finish <= 1'b1;
658
+ state <= S_IDLE;
659
+ end
660
+ end
661
+ S_POOL4:
662
+ begin
663
+ if(pool_finish == 1'b1)
664
+ begin
665
+ pool_en <= 1'b0;
666
+ vgg16_finish <= 1'b1;
667
+ state <= S_IDLE;
668
+ end
669
+ end
670
+ S_CONV5_1:
671
+ begin
672
+ if(conv_finish == 1'b1)
673
+ begin
674
+ conv_en <= 1'b0;
675
+ vgg16_finish <= 1'b1;
676
+ state <= S_IDLE;
677
+ end
678
+ end
679
+ S_CONV5_2:
680
+ begin
681
+ if(conv_finish == 1'b1)
682
+ begin
683
+ conv_en <= 1'b0;
684
+ vgg16_finish <= 1'b1;
685
+ state <= S_IDLE;
686
+ end
687
+ end
688
+ S_CONV5_3:
689
+ begin
690
+ if(conv_finish == 1'b1)
691
+ begin
692
+ conv_en <= 1'b0;
693
+ vgg16_finish <= 1'b1;
694
+ state <= S_IDLE;
695
+ end
696
+ end
697
+ S_POOL5:
698
+ begin
699
+ if(pool_finish == 1'b1)
700
+ begin
701
+ pool_en <= 1'b0;
702
+ vgg16_finish <= 1'b1;
703
+ state <= S_IDLE;
704
+ end
705
+ end
706
+ S_FC1:
707
+ begin
708
+ if(fc_finish == 1'b1)
709
+ begin
710
+ fc_en <= 1'b0;
711
+ vgg16_finish <= 1'b1;
712
+ state <= S_IDLE;
713
+ end
714
+ end
715
+ S_FC2:
716
+ begin
717
+ if(fc_finish == 1'b1)
718
+ begin
719
+ fc_en <= 1'b0;
720
+ vgg16_finish <= 1'b1;
721
+ state <= S_IDLE;
722
+ end
723
+ end
724
+ S_FC3:
725
+ begin
726
+ if(fc_finish == 1'b1)
727
+ begin
728
+ fc_en <= 1'b0;
729
+ vgg16_finish <= 1'b1;
730
+ state <= S_IDLE;
731
+ end
732
+ end
733
+ default:
734
+ begin
735
+ state <= S_IDLE;
736
+ conv_en <= 1'b0;
737
+ pool_en <= 1'b0;
738
+ fc_en <= 1'b0;
739
+ end
740
+ endcase
741
+ end
742
+ end
743
+
744
+ // vgg16_bram
745
+ always@(posedge clk)
746
+ begin
747
+ if(rst == 1'b1)
748
+ begin
749
+ vgg16_bram_ena <= 1'b0;
750
+ vgg16_bram_wea <= 1'b0;
751
+ end
752
+ else
753
+ begin
754
+ if(state == S_CONV1_1 || state == S_CONV1_2 ||
755
+ state == S_CONV2_1 || state == S_CONV2_2 ||
756
+ state == S_CONV3_1 || state == S_CONV3_2 || state == S_CONV3_3 ||
757
+ state == S_CONV4_1 || state == S_CONV4_2 || state == S_CONV4_3 ||
758
+ state == S_CONV5_1 || state == S_CONV5_2 || state == S_CONV5_3)
759
+ begin
760
+ vgg16_bram_ena = conv_vgg16_bram_ena;
761
+ vgg16_bram_wea = conv_vgg16_bram_wea;
762
+ vgg16_bram_addra = conv_vgg16_bram_addra;
763
+ vgg16_bram_dina = conv_vgg16_bram_dina;
764
+ conv_vgg16_bram_douta = vgg16_bram_douta;
765
+ end
766
+ else if(state == S_POOL1 || state == S_POOL2 || state == S_POOL3 || state == S_POOL4 || state == S_POOL5)
767
+ begin
768
+ vgg16_bram_ena = pool_vgg16_bram_ena;
769
+ vgg16_bram_wea = pool_vgg16_bram_wea;
770
+ vgg16_bram_addra = pool_vgg16_bram_addra;
771
+ vgg16_bram_dina = pool_vgg16_bram_dina;
772
+ pool_vgg16_bram_douta = vgg16_bram_douta;
773
+ end
774
+ else if(state == S_FC1 || state == S_FC2 || state == S_FC3)
775
+ begin
776
+ vgg16_bram_ena = fc_vgg16_bram_ena;
777
+ vgg16_bram_wea = fc_vgg16_bram_wea;
778
+ vgg16_bram_addra = fc_vgg16_bram_addra;
779
+ vgg16_bram_dina = fc_vgg16_bram_dina;
780
+ fc_vgg16_bram_douta = vgg16_bram_douta;
781
+ end
782
+ else
783
+ begin
784
+ vgg16_bram_ena <= 1'b0;
785
+ vgg16_bram_wea <= 1'b0;
786
+ end
787
+ end
788
+ end
789
+
790
+ endmodule