-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart_transceiver_tb.v
More file actions
136 lines (108 loc) · 2.37 KB
/
Copy pathuart_transceiver_tb.v
File metadata and controls
136 lines (108 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Testbench for UART modules. It consists of the connected receiver
// and transmitter. During the test, TEST_BYTE is transmitted to the
// receiver and simultaneously transmitted by the transmiter. Each of
// these steps is automatically checked.
`timescale 1us / 1ns
module uart_transceiver_tb;
// Testbench uses a 1 MHz clock
// Baud rate of the UART is 115200
// CLOCK_PER_BIT = round(CLK_PERIOD_US * BIT_DURATION_US) = 9
parameter CLK_PERIOD = 1.0; // In us
parameter BIT_DURATION = 8.7; // In us
parameter CLK_PER_BIT = 9;
parameter [7:0] TEST_BYTE = 8'b0110_1010;
reg clock;
reg reset;
reg rx;
reg [7:0] rx_test = 0;
wire [7:0] data;
wire ready;
wire busy;
wire tx;
integer i = 0;
// Test modules
uart_receiver #(
.CLKS_PER_BIT(CLK_PER_BIT)
) receiver_dut (
.rx(rx),
.clock(clock),
.reset(reset),
.rx_byte_ready(ready),
.rx_data(data)
);
uart_transmitter #(
.CLKS_PER_BIT(CLK_PER_BIT)
) transmitter_dut (
.clock(clock),
.reset(reset),
.data(data),
.start_transmit(ready),
.tx(tx),
.tx_busy(busy)
);
// Clock signal
always #(CLK_PERIOD/2) clock <= ~clock;
// Initialisaton task
task init();
begin
clock <= 0;
reset <= 0;
rx <= 1;
end
endtask
// Reset task
task reset_pulse();
begin
reset <= 1;
#(CLK_PERIOD);
reset <= 0;
end
endtask
// Send byte via UART
task send_byte (
input [7:0] byte
);
integer i;
begin
// Start bit
rx <= 0;
#(BIT_DURATION);
//Sending byte
for (i=0; i < 8; i=i+1) begin
rx <= byte[i];
#(BIT_DURATION);
end
// Checking if the receiving was successful.
if (data == TEST_BYTE) $display("Reception succeed.");
else $display("Reception failure!");
//Stop bit
rx <= 1;
#(BIT_DURATION);
end
endtask
// Receive and check
always @(negedge tx) begin
// Skip start bit
#(BIT_DURATION);
// Skip half of the first bit
#(BIT_DURATION/2);
// Receive byte
for (i=0; i < 8; i=i+1) begin
rx_test[i] <= tx;
#(BIT_DURATION);
end
// Skip half of the last bit
#(BIT_DURATION/2);
// Skip stop bit
#(BIT_DURATION);
// Checking if the transmission was successful.
if (rx_test == TEST_BYTE) $display("Transmission succeed.");
else $display("Transmission failure!");
end
// Main simulation cycle
initial begin
init();
reset_pulse();
send_byte(TEST_BYTE);
end
endmodule