forked from marsohod4you/FPGA_FM_transmitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.v
127 lines (109 loc) · 1.87 KB
/
serial.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
module serial(
input wire reset,
input wire clk100, //100MHz
input wire rx,
input wire [7:0]sbyte,
input wire send,
output reg [7:0]rx_byte,
output reg rbyte_ready,
output reg tx,
output reg busy,
output wire [7:0]rb
);
parameter RCONST = 434; //230400bps
assign rb = {1'b0,rx_byte[7:1]};
reg [1:0]shr;
always @(posedge clk100)
shr <= {shr[0],rx};
wire rxf; assign rxf = shr[1];
reg [15:0]cnt;
always @(posedge clk100 or posedge reset)
begin
if(reset)
cnt <= 0;
else
begin
if(cnt == RCONST || num_bits==10)
cnt <= 0;
else
cnt <= cnt + 1'b1;
end
end
reg [3:0]num_bits;
reg [7:0]shift_reg;
always @(posedge clk100 or posedge reset)
begin
if(reset)
begin
num_bits <= 0;
shift_reg <= 0;
end
else
begin
if(num_bits==10 && rxf==1'b0)
num_bits <= 0;
else
if(cnt == RCONST)
num_bits <= num_bits + 1'b1;
if( cnt == RCONST/2 )
shift_reg <= {rxf,shift_reg[7:1]};
end
end
always @(posedge clk100 or posedge reset)
if(reset)
begin
rx_byte <= 0;
end
else
begin
if(num_bits==9 && cnt == RCONST/2)
rx_byte <= shift_reg[7:0];
end
reg [1:0]flag;
always @(posedge clk100 or posedge reset)
if(reset)
flag <= 2'b00;
else
flag <= {flag[0],(num_bits==9)};
always @*
rbyte_ready = (flag==2'b01);
reg [8:0]send_reg;
reg [3:0]send_num;
reg [15:0]send_cnt;
wire send_time; assign send_time = send_cnt == RCONST;
always @(posedge clk100 or posedge reset)
begin
if(reset)
begin
send_reg <= 0;
send_num <= 0;
send_cnt <= 0;
end
else
begin
if(send)
send_cnt <= 0;
else
if(send_time)
send_cnt <= 0;
else
send_cnt <= send_cnt + 1'b1;
if(send)
begin
send_reg <= {sbyte,1'b0};
send_num <= 0;
end
else
if(send_time && send_num!=10)
begin
send_reg <= {1'b1,send_reg[8:1]};
send_num <= send_num + 1'b1;
end
end
end
always @*
begin
busy = send_num!=10;
tx = send_reg[0];
end
endmodule