Skip to content

Commit 450eb40

Browse files
authored
Add files via upload
1 parent 18ac69c commit 450eb40

File tree

16 files changed

+2189
-2
lines changed

16 files changed

+2189
-2
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
# hust-cse-crypto-verilog
2-
华中科技大学网络空间安全学院密码工程学基础2023
1+
# hust-cse-crypto-verilog
2+
3+
**华中科技大学网络空间安全学院密码工程学基础2023**
4+
5+
*powered by mxy*
6+
7+
------
8+
9+
*First,用过本课程库的请star一下~*
10+
11+
四个实验分别在四个文件夹里,请使用iverilog编译,先编译代码本体如`aes.v`,再编译`test bench``tb_aes.v`
12+
13+
编译工具链推荐使用`vscode`+`iverilog`,具体安装方法请自行搜索(知乎上有)。
14+
15+
需要烧录在`FPGA`板子上的,请使用`vivado`生成比特流(课程第一节课会讲),同时,仿真占比90%,纯仿真使用`vscode`即可。

实验1(UART)/Computation.v

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
`timescale 1ns / 1ps
2+
3+
module Computation(
4+
Clk ,
5+
Rst ,
6+
In_enable ,
7+
Out_done ,
8+
// FIFO
9+
In_rcv_dout ,
10+
Out_rcv_rd_en ,
11+
Out_snd_din ,
12+
Out_snd_wr_en
13+
);
14+
15+
//---------------------------------------------------------------------------------------
16+
// Parameter
17+
//---------------------------------------------------------------------------------------
18+
parameter TCQ = 1 ;
19+
//FSM-------------------------------------------------------------------
20+
parameter STA_IDLE = 4'h0 ;
21+
parameter STA_READING = 4'h1 ;
22+
parameter STA_ROUND = 4'h2 ;
23+
parameter STA_ROUND_DONE = 4'h3 ;
24+
parameter STA_WRITING = 4'h4 ;
25+
parameter STA_COMPUTING_DONE = 4'h5 ;
26+
27+
//---------------------------------------------------------------------------------------
28+
// Port Define
29+
//---------------------------------------------------------------------------------------
30+
input wire Clk ;
31+
input wire Rst ;
32+
input wire In_enable ;
33+
output wire Out_done ;
34+
// FIFO
35+
input wire [31:0] In_rcv_dout ;
36+
output wire Out_rcv_rd_en ;
37+
output wire [31:0] Out_snd_din ;
38+
output wire Out_snd_wr_en ;
39+
40+
//---------------------------------------------------------------------------------------
41+
// Signals
42+
//---------------------------------------------------------------------------------------
43+
reg out_done ;
44+
reg out_rcv_rd_en ;
45+
reg [31:0] out_snd_din ;
46+
reg out_snd_wr_en ;
47+
48+
reg [31:0] rcv_dout ;
49+
reg [31:0] tmp ;
50+
reg [7:0] cnt_round ;
51+
52+
//---------------------------------------------------------------------------------------
53+
// Instance
54+
//---------------------------------------------------------------------------------------
55+
//CompIla InstCompIla(
56+
// .clk (Clk ), // input wire clk
57+
// .probe0 (In_enable ), // input wire [0:0] probe0
58+
// .probe1 (Out_done ), // input wire [0:0] probe1
59+
// .probe2 (In_rcv_dout ), // input wire [31:0] probe2
60+
// .probe3 (Out_rcv_rd_en ), // input wire [0:0] probe3
61+
// .probe4 (Out_snd_din ), // input wire [31:0] probe4
62+
// .probe5 (Out_snd_wr_en ), // input wire [0:0] probe5
63+
// .probe6 (curr_state ), // input wire [7:0] probe6
64+
// .probe7 (cnt_round ) // input wire [7:0] probe7
65+
//);
66+
67+
//---------------------------------------------------------------------------------------
68+
// Function Codes
69+
//---------------------------------------------------------------------------------------
70+
assign Out_done = out_done;
71+
assign Out_rcv_rd_en = out_rcv_rd_en;
72+
assign Out_snd_din = out_snd_din;
73+
assign Out_snd_wr_en = out_snd_wr_en;
74+
75+
//---------------------------------------------------------------------------------------
76+
// FSM
77+
//---------------------------------------------------------------------------------------
78+
reg [7:0] curr_state ;
79+
reg [7:0] next_state ;
80+
81+
//State Tranfer
82+
always @(posedge Clk) begin
83+
if(Rst == 1'b1)
84+
curr_state <= #TCQ STA_IDLE;
85+
else
86+
curr_state <= #TCQ next_state;
87+
end
88+
89+
//Calculate the next state
90+
always @( * ) begin
91+
case(curr_state)
92+
STA_IDLE : begin
93+
if(In_enable)
94+
next_state = STA_READING;
95+
else
96+
next_state = STA_IDLE;
97+
end
98+
99+
STA_READING : begin
100+
next_state = STA_ROUND;
101+
end
102+
103+
STA_ROUND : begin
104+
next_state = STA_ROUND_DONE;
105+
end
106+
107+
STA_ROUND_DONE : begin
108+
next_state = STA_WRITING;
109+
end
110+
111+
STA_WRITING : begin
112+
if(cnt_round < 8'd4)
113+
next_state = STA_READING;
114+
else if(cnt_round == 8'd4)
115+
next_state = STA_COMPUTING_DONE;
116+
end
117+
118+
STA_COMPUTING_DONE : begin
119+
next_state = STA_IDLE;
120+
end
121+
122+
default : begin
123+
next_state = STA_IDLE;
124+
end
125+
endcase
126+
end
127+
128+
//state output
129+
always @(posedge Clk) begin
130+
if(Rst == 1'b1) begin
131+
out_rcv_rd_en <= #TCQ 1'b0;
132+
out_snd_wr_en <= #TCQ 1'b0;
133+
out_snd_din <= #TCQ 32'd0;
134+
rcv_dout <= #TCQ 32'd0;
135+
out_done <= #TCQ 1'b0;
136+
cnt_round <= #TCQ 8'd0;
137+
end
138+
139+
else case(curr_state)
140+
STA_IDLE : begin
141+
out_rcv_rd_en <= #TCQ 1'b0;
142+
out_snd_wr_en <= #TCQ 1'b0;
143+
out_snd_din <= #TCQ 32'd0;
144+
rcv_dout <= #TCQ 32'd0;
145+
out_done <= #TCQ 1'b0;
146+
cnt_round <= #TCQ 8'd0;
147+
end
148+
149+
STA_READING : begin
150+
out_snd_wr_en <= #TCQ 1'b0;
151+
out_snd_din <= #TCQ 32'd0;
152+
out_rcv_rd_en <= #TCQ 1'b1;
153+
rcv_dout <= #TCQ In_rcv_dout;
154+
end
155+
156+
STA_ROUND : begin
157+
out_rcv_rd_en <= #TCQ 1'b0;
158+
tmp <= #TCQ {rcv_dout[7:0], rcv_dout[15:8], rcv_dout[23:16], rcv_dout[31:24]};
159+
end
160+
161+
STA_ROUND_DONE : begin
162+
cnt_round <= #TCQ cnt_round + 8'd1;
163+
end
164+
165+
STA_WRITING : begin
166+
out_snd_wr_en <= #TCQ 1'b1;
167+
out_snd_din <= #TCQ tmp;
168+
169+
end
170+
171+
STA_COMPUTING_DONE : begin
172+
out_snd_wr_en <= #TCQ 1'b0;
173+
out_done <= #TCQ 1'b1;
174+
end
175+
endcase
176+
end
177+
178+
endmodule
179+

实验1(UART)/Top.v

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
`timescale 1ns / 1ps
2+
3+
module Top(
4+
Clk_p ,
5+
Clk_n ,
6+
//Rx and Tx
7+
In_rx ,
8+
Out_tx
9+
);
10+
//---------------------------------------------------------------------------------------
11+
// Parameter
12+
//---------------------------------------------------------------------------------------
13+
parameter TCQ = 1 ;
14+
15+
//---------------------------------------------------------------------------------------
16+
// Port Define
17+
//---------------------------------------------------------------------------------------
18+
input wire Clk_p ;
19+
input wire Clk_n ;
20+
//Rx and Tx
21+
input wire In_rx ;
22+
output wire Out_tx ;
23+
24+
//---------------------------------------------------------------------------------------
25+
// Signals
26+
//---------------------------------------------------------------------------------------
27+
//ClkWid
28+
wire clk200m ;
29+
wire clk100m ;
30+
wire clk50m ;
31+
wire locked ;
32+
//RcvFifo
33+
wire [7:0] rcv_din ;
34+
wire rcv_wr_en ;
35+
wire rcv_full ;
36+
wire [5:0] rcv_wr_data_count ;
37+
wire [31:0] rcv_dout ;
38+
wire rcv_rd_en ;
39+
wire rcv_empty ;
40+
wire [3:0] rcv_rd_data_count ;
41+
//SndFifo
42+
wire [31:0] snd_din ;
43+
wire snd_wr_en ;
44+
wire snd_full ;
45+
wire [3:0] snd_wr_data_count ;
46+
wire [7:0] snd_dout ;
47+
wire snd_rd_en ;
48+
wire snd_empty ;
49+
wire [5:0] snd_rd_data_count ;
50+
//Computation
51+
wire comp_enable ;
52+
wire comp_done ;
53+
54+
//---------------------------------------------------------------------------------------
55+
// Instance
56+
//---------------------------------------------------------------------------------------
57+
IBUFGDS InstClkTrans
58+
(
59+
.O(clk200m),
60+
.I(Clk_p),
61+
.IB(Clk_n)
62+
);
63+
64+
ClkWiz InstClkWiz(
65+
.clk_out100m (clk100m ), // output clk_out100m
66+
.clk_out50m (clk50m ), // output clk_out50m
67+
.clk_in200m (clk200m ), // input clk_in100m
68+
.locked (locked )
69+
);
70+
71+
Uart InstUart(
72+
.Clk (clk100m ),
73+
.Rst (!locked ),
74+
//Rx and Tx
75+
.In_rx (In_rx ),
76+
.Out_tx (Out_tx ),
77+
//FIFO
78+
.In_rcv_wr_data_count (rcv_wr_data_count ),
79+
.In_rcv_full (rcv_full ),
80+
.Out_rcv_wr_en (rcv_wr_en ),
81+
.Out_rcv_din (rcv_din ),
82+
.In_snd_rd_data_count (snd_rd_data_count ),
83+
.In_snd_empty (snd_empty ),
84+
.Out_snd_rd_en (snd_rd_en ),
85+
.In_snd_dout (snd_dout ),
86+
//Computation
87+
.In_comp_done (comp_done ),
88+
.Out_comp_enable (comp_enable )
89+
);
90+
91+
RcvFifo InstRcvFifo(
92+
.rst (!locked ), // input wire rst
93+
.wr_clk (clk100m ), // input wire wr_clk
94+
.rd_clk (clk50m ), // input wire rd_clk
95+
.din (rcv_din ), // input wire [7 : 0] din
96+
.wr_en (rcv_wr_en ), // input wire wr_en
97+
.rd_en (rcv_rd_en ), // input wire rd_en
98+
.dout (rcv_dout ), // output wire [31 : 0] dout
99+
.full (rcv_full ), // output wire full
100+
.empty (rcv_empty ), // output wire empty
101+
.rd_data_count (rcv_rd_data_count ), // output wire [3 : 0] rd_data_count
102+
.wr_data_count (rcv_wr_data_count ) // output wire [5 : 0] wr_data_count
103+
);
104+
105+
SndFifo InstSndFifo(
106+
.rst (!locked ), // input wire rst
107+
.wr_clk (clk50m ), // input wire wr_clk
108+
.rd_clk (clk100m ), // input wire rd_clk
109+
.din (snd_din ), // input wire [31 : 0] din
110+
.wr_en (snd_wr_en ), // input wire wr_en
111+
.rd_en (snd_rd_en ), // input wire rd_en
112+
.dout (snd_dout ), // output wire [7 : 0] dout
113+
.full (snd_full ), // output wire full
114+
.empty (snd_empty ), // output wire empty
115+
.rd_data_count (snd_rd_data_count ), // output wire [5 : 0] rd_data_count
116+
.wr_data_count (snd_wr_data_count ) // output wire [3 : 0] wr_data_count
117+
);
118+
119+
Computation InstComputation(
120+
.Clk (clk50m ),
121+
.Rst (!locked ),
122+
.In_enable (comp_enable ),
123+
.Out_done (comp_done ),
124+
//FIFO
125+
.In_rcv_dout (rcv_dout ),
126+
.Out_rcv_rd_en (rcv_rd_en ),
127+
.Out_snd_din (snd_din ),
128+
.Out_snd_wr_en (snd_wr_en )
129+
);
130+
//ILA
131+
TopIla InstTopIla(
132+
.clk (clk100m ), // input wire clk
133+
.probe0 (comp_enable ), // input wire [0:0] probe0
134+
.probe1 (comp_done ), // input wire [0:0] probe1
135+
.probe2 (rcv_dout ), // input wire [31:0] probe2
136+
.probe3 (rcv_rd_en ), // input wire [0:0] probe3
137+
.probe4 (snd_din ), // input wire [31:0] probe4
138+
.probe5 (snd_wr_en ), // input wire [0:0] probe5
139+
.probe6 (snd_empty ), // input wire [0:0] probe6
140+
.probe7 (snd_rd_data_count ) // input wire [5:0] probe7
141+
//.probe8 ( ), // input wire [0:0] probe8
142+
//.probe9 ( ), // input wire [0:0] probe9
143+
//.probe10 ( ), // input wire [0:0] probe10
144+
//.probe11 ( ), // input wire [0:0] probe11
145+
//.probe12 ( ), // input wire [0:0] probe12
146+
//.probe13 ( ), // input wire [0:0] probe13
147+
//.probe14 ( ), // input wire [0:0] probe14
148+
//.probe15 ( ) // input wire [0:0] probe15
149+
);
150+
//---------------------------------------------------------------------------------------
151+
// Function Codes
152+
//---------------------------------------------------------------------------------------
153+
154+
155+
//---------------------------------------------------------------------------------------
156+
// FSM
157+
//---------------------------------------------------------------------------------------
158+
159+
160+
endmodule
161+

0 commit comments

Comments
 (0)