-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuart.vhdl
484 lines (416 loc) · 13 KB
/
uart.vhdl
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
-- /*
-- * Copyright (C) 2009 Micah Dowty
-- * (C) 2018 Trammell Hudson
-- *
-- * Permission is hereby granted, free of charge, to any person obtaining a copy
-- * of this software and associated documentation files (the "Software"), to deal
-- * in the Software without restriction, including without limitation the rights
-- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- * copies of the Software, and to permit persons to whom the Software is
-- * furnished to do so, subject to the following conditions:
-- *
-- * The above copyright notice and this permission notice shall be included in
-- * all copies or substantial portions of the Software.
-- *
-- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- * THE SOFTWARE.
-- */
-- Translated to VHDL from Verilog by nobodywasishere
-- Original: https://github.com/osresearch/up5k
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use ieee.math_real.all;
--
-- Byte transmitter, RS-232 8-N-1
--
-- Transmits on 'serial'. When 'ready' goes high, we can accept another byte.
-- It should be supplied on 'data' with a pulse on 'data_strobe'.
--
entity uart_tx is
port (
mclk : in std_logic;
reset : in std_logic;
baud_x1 : in std_logic;
data : in unsigned(7 downto 0);
data_strobe : in std_logic;
serial : out std_logic;
ready : out std_logic
);
end uart_tx;
architecture synth of uart_tx is
signal shiftreg : unsigned(9 downto 0);
signal serial_r : std_logic;
begin
process (mclk) begin
if (rising_edge(mclk)) then
ready <= '0';
if (reset = '1') then
shiftreg <= "0000000000";
serial_r <= '0';
elsif (data_strobe = '1') then
shiftreg <= '1' & data & '0';
ready <= '0';
elsif (baud_x1 = '1') then
if (shiftreg = "000000000") then
serial_r <= '0';
ready <= '1';
else
serial_r <= NOT shiftreg(0);
shiftreg <= '0' & shiftreg(9 downto 1);
end if;
elsif (shiftreg = "000000000") then
ready <= '1';
end if;
serial <= NOT serial_r;
end if;
end process;
end;
--/*
-- * Byte receiver, RS-232 8-N-1
-- *
-- * Receives on 'serial'. When a properly framed byte is
-- * received, 'data_strobe' pulses while the byte is on 'data'.
-- *
-- * Error bytes are ignored.
-- */
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use ieee.math_real.all;
entity uart_rx is
port (
mclk : in std_logic;
reset : in std_logic;
baud_x4 : in std_logic;
serial : in std_logic;
data : out unsigned(7 downto 0);
data_strobe : out std_logic
);
end uart_rx;
architecture synth of uart_rx is
component d_flipflop is
port(
clk : in std_logic;
reset : in std_logic;
sig_in : in std_logic;
sig_out : out std_logic
);
end component;
signal serial_sync : std_logic;
signal shiftreg : unsigned(8 downto 0);
signal state : unsigned(5 downto 0);
signal bit_count : unsigned(3 downto 0);
signal bit_phase : unsigned(1 downto 0);
signal sampling_phase : std_logic;
signal start_bit : std_logic;
signal stop_bit : std_logic;
signal waiting_for_start : std_logic;
signal error_sig : std_logic;
begin
dut1: d_flipflop port map (clk => mclk, reset => reset, sig_in => serial, sig_out => serial_sync);
bit_count <= state(5 downto 2);
bit_phase <= state(1 downto 0);
data <= shiftreg(7 downto 0);
process (mclk) begin
if (bit_phase = 1) then
sampling_phase <= '1';
else
sampling_phase <= '0';
end if;
if (bit_count = 0 AND sampling_phase = '1') then
start_bit <= '1';
else
start_bit <= '0';
end if;
if (bit_count = 9 AND sampling_phase = '1') then
stop_bit <= '1';
else
stop_bit <= '0';
end if;
if (state = 0 AND serial_sync = '1') then
waiting_for_start <= '1';
else
waiting_for_start <= '0';
end if;
if ((start_bit = '1' AND serial_sync = '1') OR (stop_bit = '1' AND serial_sync = '0')) then
error_sig <= '1';
else
error_sig <= '0';
end if;
end process;
process (mclk) begin
if (rising_edge(mclk)) then
if (reset = '1') then
state <= "000000";
data_strobe <= '0';
shiftreg <= shiftreg;
elsif (baud_x4 = '1') then
if (waiting_for_start = '1' OR error_sig = '1' OR stop_bit = '1') then
state <= "000000";
else
state <= state + 1;
end if;
if (bit_phase = 1) then
shiftreg <= serial_sync & shiftreg(8 downto 1);
else
shiftreg <= shiftreg;
end if;
data_strobe <= (stop_bit AND NOT error_sig);
end if;
end if;
end process;
end;
-- /*
-- * Output UART with a block RAM FIFO queue.
-- *
-- * Add bytes to the queue and they will be printed when the line is idle.
-- */
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use ieee.math_real.all;
entity uart_tx_fifo is
port (
clk : in std_logic;
reset : in std_logic;
baud_x1 : in std_logic;
data : in unsigned(7 downto 0);
data_strobe : in std_logic;
serial : out std_logic
);
end uart_tx_fifo;
architecture synth of uart_tx_fifo is
signal NUM : integer := 32;
signal uart_txd_ready : std_logic;
signal uart_txd_strobe : std_logic;
signal uart_txd : unsigned(7 downto 0);
signal fifo_available : std_logic;
signal fifo_read_strobe : std_logic;
component uart_tx is
port (
mclk : in std_logic;
reset : in std_logic;
baud_x1 : in std_logic;
data : in unsigned(7 downto 0);
data_strobe : in std_logic;
serial : out std_logic;
ready : out std_logic
);
end component;
component fifo is
generic (
data_width : integer := 8;
data_num : integer := 32
);
port (
clk : in std_logic;
reset : in std_logic;
data_available : out std_logic;
write_data : in unsigned(data_width - 1 downto 0);
write_strobe : in std_logic;
read_data : out unsigned(data_width - 1 downto 0);
read_strobe : in std_logic
);
end component;
begin
dut1 : uart_tx port map (mclk => clk, reset => reset, baud_x1 => baud_x1,
serial => serial, ready => uart_txd_ready, data => uart_txd,
data_strobe => uart_txd_strobe);
dut2 : fifo port map (clk => clk, reset => reset, write_data => data, write_strobe => data_strobe,
data_available => fifo_available, read_data => uart_txd,
read_strobe => fifo_read_strobe);
process (clk) begin
if (rising_edge(clk)) then
if (fifo_available = '1' and uart_txd_ready = '1' and data_strobe = '0' and uart_txd_strobe = '0') then
fifo_read_strobe <= '1';
uart_txd_strobe <= '1';
else
uart_txd_strobe <= '0';
fifo_read_strobe <= '0';
end if;
end if;
end process;
end;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use ieee.math_real.all;
entity d_flipflop is
port(
clk : in std_logic;
reset : in std_logic;
sig_in : in std_logic;
sig_out : out std_logic
);
end d_flipflop;
architecture synth of d_flipflop is
begin
process (clk) begin
if(rising_edge(clk)) then
if(reset = '1') then
sig_out <= '0';
else
sig_out <= sig_in;
end if;
end if;
end process;
end;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use ieee.math_real.all;
entity divide_by_n is
generic (
N : integer
);
port (
clk : in std_logic;
reset : in std_logic;
sig_out : out std_logic
);
end divide_by_n;
architecture synth of divide_by_n is
function CLOG2 (
x : in integer
) return integer is
variable y : integer;
begin
if (x <= 2) then
y := 1;
elsif (x <= 4) then
y := 2;
elsif (x <= 8) then
y := 3;
elsif (x <= 16) then
y := 4;
elsif (x <= 32) then
y := 5;
elsif (x <= 64) then
y := 6;
elsif (x <= 128) then
y := 7;
elsif (x <= 256) then
y := 8;
elsif (x <= 512) then
y := 9;
elsif (x <= 1024) then
y := 10;
elsif (x <= 2048) then
y := 11;
elsif (x <= 4096) then
y := 12;
elsif (x <= 8192) then
y := 13;
elsif (x <= 16384) then
y := 14;
elsif (x <= 32768) then
y := 15;
elsif (x <= 65536) then
y := 16;
else
y := -1;
end if;
return y;
end function;
signal counter : unsigned(CLOG2(N) - 1 downto 0);
begin
process (clk) begin
if (rising_edge(clk)) then
sig_out <= '0';
if (reset = '1') then
counter <= to_unsigned(0, CLOG2(N));
elsif (counter = "0") then
sig_out <= '1';
counter <= to_unsigned((N - 1), CLOG2(N));
else
counter <= counter - 1;
end if;
end if;
end process;
end;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use ieee.math_real.all;
entity fifo is
generic (
data_width : integer := 8;
data_num : integer := 32
);
port (
clk : in std_logic;
reset : in std_logic;
data_available : out std_logic;
write_data : in unsigned(data_width - 1 downto 0);
write_strobe : in std_logic;
read_data : out unsigned(data_width - 1 downto 0);
read_strobe : in std_logic
);
end fifo;
architecture synth of fifo is
type mem_buffer is array (0 to data_num - 1) of unsigned(data_width - 1 downto 0);
signal buffer_one : mem_buffer;
signal size_log2 : integer := integer(ceil(log2(real(data_width))));
signal write_ptr : unsigned( size_log2 - 1 downto 0 ) := "00";
signal read_ptr : unsigned( size_log2 - 1 downto 0 ) := "00";
begin
read_data <= buffer_one(to_integer(read_ptr));
process (clk) begin
if (rising_edge(clk)) then
if (read_ptr /= write_ptr) then
data_available <= '1';
else
data_available <= '0';
end if;
if (reset = '1') then
write_ptr <= "00";
read_ptr <= "00";
else
if (write_strobe = '1') then
buffer_one(to_integer(write_ptr)) <= write_data;
write_ptr <= write_ptr + 1;
end if;
if (read_strobe = '1') then
read_ptr <= read_ptr + 1;
end if;
end if;
end if;
end process;
end;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use ieee.math_real.all;
entity pwm is
port (
clk : in std_logic;
bright : in unsigned(15 downto 0);
sig_out : out std_logic
);
end pwm;
architecture synth of pwm is
signal counter : unsigned(15 downto 0);
begin
process (clk) begin
if (rising_edge(clk)) then
counter <= counter + 1;
if (counter < bright) then
sig_out <= '1';
else
sig_out <= '0';
end if;
end if;
end process;
end;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --