-
Notifications
You must be signed in to change notification settings - Fork 0
/
top.v
123 lines (105 loc) · 2.51 KB
/
top.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
/*
* IR receiver with UART interface
*
* Top level module
*
* Viktor Prutyanov, 2021
*/
module top (
/* 50 MHz clock */
input CLK,
/* IR input */
input IRDA,
/* 7-segment LED display */
output DS_EN1, DS_EN2, DS_EN3, DS_EN4,
output DS_A, DS_B, DS_C, DS_D, DS_E, DS_F, DS_G,
/* UART TX */
output TXD
);
assign {DS_EN1, DS_EN2, DS_EN3, DS_EN4} = ~anodes;
assign {DS_A, DS_B, DS_C, DS_D, DS_E, DS_F, DS_G} = segments;
reg [15:0]hd_data = 16'b0;
wire [3:0]anodes;
wire [6:0]segments;
hex_display hex_display (
.clk(CLK),
.data(hd_data),
.anodes(anodes),
.segments(segments)
);
reg uart_start = 1'b0;
reg [7:0]uart_data = 8'b0;
wire uart_idle;
uart_tx #(.BAUDRATE(5_000_000)) uart_tx (
.clk(CLK),
.start(uart_start),
.data(uart_data),
.idle(uart_idle),
.q(TXD)
);
reg ir_in = 1'b0;
always @(posedge CLK)
ir_in <= ~IRDA;
wire ir_edge;
wire ir_pulse;
wire [24:0]ir_cnt;
ir_rx ir_rx (
.clk(CLK),
.rx(ir_in),
.rx_edge(ir_edge),
.pulse(ir_pulse),
.cnt(ir_cnt)
);
reg [19:0]ir_cnt_buf = 20'b0;
wire [39:0]dur_chars;
cnt_to_chars ctc(
.cnt(ir_cnt_buf / 50),
.chars(dur_chars)
);
reg pulse_buf = 1'b0;
always @(posedge CLK) begin
if (ir_edge) begin
ir_cnt_buf <= ir_cnt;
pulse_buf <= ir_pulse;
end
end
reg [2:0]idle_cnt = 0;
always @(posedge CLK) begin
if (uart_idle)
idle_cnt <= idle_cnt + 1;
else
idle_cnt <= 0;
end
reg [2:0]char_cnt = 0;
always @(posedge CLK) begin
if (ir_edge)
char_cnt <= 3'h1;
if (idle_cnt == 3'b111) begin
if (char_cnt)
uart_start <= 1'b1;
case (char_cnt)
'h0: begin uart_data <= 8'h2b; end
'h1: begin char_cnt <= 3'h2; uart_data <= pulse_buf ? 8'h2b : 8'h2d; end
'h2: begin char_cnt <= 3'h3; uart_data <= dur_chars[31:24]; end
'h3: begin char_cnt <= 3'h4; uart_data <= dur_chars[23:16]; end
'h4: begin char_cnt <= 3'h5; uart_data <= dur_chars[15:8]; end
'h5: begin char_cnt <= 3'h6; uart_data <= dur_chars[7:0]; end
'h6: begin char_cnt <= 3'h7; uart_data <= 8'b00001101; end
'h7: begin char_cnt <= 3'h0; uart_data <= 8'b00001010; end
endcase
end
else
uart_start <= 1'b0;
end
endmodule
module cnt_to_chars (
input [19:0]cnt,
output [39:0]chars
);
genvar i;
generate
for (i = 0; i < 5; i = i + 1) begin : genchar
assign chars[i * 8 +: 8] = ((cnt[i * 4 +: 4] < 10) ? 8'h30 : 8'h57) + cnt[i * 4 +: 4];
end
endgenerate
endmodule