]> rtime.felk.cvut.cz Git - fpga/uart.git/blob - uart.vhd
cddd6c86f23984958f5aa143d2861db09486ab44
[fpga/uart.git] / uart.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use ieee.std_logic_unsigned.all;
5
6 entity uart is
7   generic (
8     output_fifo_width : integer := 2
9   );
10   port (
11     mclk        : in  std_logic;
12     per_addr    : in  std_logic_vector (7  downto 0);
13     per_din     : in  std_logic_vector (15 downto 0);  -- unused
14     per_en      : in  std_logic;
15     per_wen     : in  std_logic_vector (1  downto 0);  -- unused
16     puc         : in  std_logic;                       -- unused
17     --per_irq_acc : in  std_logic;                       -- unused
18     --per_irq     : out std_logic;
19     per_dout    : out std_logic_vector (15 downto 0);
20
21     txd         : out std_logic
22   );
23 end uart;
24
25 -- Registers:
26 --  - Prescaler .16b
27 --  - Modulator .8b
28 --  - TX data .8b
29 --  - TX enable
30 --  - TX buffer full .1b
31 --  - TX buffer empty .1b
32 --  - TX buffer empty IE 1.b
33 --  - RX data .8b
34 --  - RX enable
35 --  - RX buffer empty .1b
36 --  - RX buffer half full .1b
37 --  - RX buffer full .1b
38 --  - RX buffer half full IE .1b
39 --  - RX buffer full IE .1b
40 --  - RX framing error .1b
41 --  - RX buffer overflow .1b
42
43 --------------------------------------------------------------------------------
44
45 architecture dataflow of uart is
46
47   component tx_control is
48     port (
49       clk        : in  std_logic;
50       reset      : in  std_logic;
51       tx_ready   : in  std_logic;
52       fifo_empty : in  std_logic;
53       tx_we      : out std_logic;
54       fifo_pop   : out std_logic
55     );
56   end component;
57
58   component transmitter is
59     port (
60       clk    : in  std_logic;
61       reset  : in  std_logic;
62       data   : in  std_logic_vector (7 downto 0);
63       we     : in  std_logic;
64       ready  : out std_logic;
65       tx     : out std_logic
66     );
67   end component;
68
69   component fifo is
70     generic (
71       width : integer := 2
72     );
73     port (
74       clk      : in  std_logic;
75       reset    : in  std_logic;
76       we       : in  std_logic;
77       re       : in  std_logic;
78       clear_ow : in  std_logic;
79       d_in     : in  std_logic_vector (7 downto 0);
80       d_out    : out std_logic_vector (7 downto 0);
81       full     : out std_logic;
82       hfull    : out std_logic;
83       empty    : out std_logic;
84       overflow : out std_logic
85     );
86   end component;
87
88   component baud_gen is
89     port (
90       clk      : in  std_logic;
91       ce       : in  std_logic;
92       reset    : in  std_logic;
93       scale    : in std_logic_vector (15 downto 0);
94       clk_baud : out  std_logic
95     );
96   end component;
97
98 --------------------------------------------------------------------------------
99
100   type boolean_vector is array (natural range <>) of boolean;
101
102 --------------------------------------------------------------------------------
103   
104   constant base_addr : integer := 16#0100#;
105   
106   constant UBAUD : integer := base_addr + 00;
107   constant UTX   : integer := base_addr + 02;
108
109   signal reg_we : std_logic_vector (511 downto 0);
110   signal reg_re : boolean_vector (511 downto 0);
111
112   signal tx_clk : std_logic;
113   signal tx_data : std_logic_vector (7 downto 0);
114   signal tx_we : std_logic;
115   signal tx_ready : std_logic;
116
117   signal tx_fifo_empty : std_logic;
118   signal tx_fifo_re : std_logic;
119   signal tx_fifo_we : std_logic;
120
121 --------------------------------------------------------------------------------
122
123 begin
124
125   process (per_addr, per_wen, per_en)
126   begin
127     for i in reg_re'range loop
128       reg_re (i) <= false;
129       reg_we (i) <= '0';
130       
131       if (per_en = '1' and per_addr = i/2) then
132         reg_re (i) <= true;
133         
134         if (per_wen (i mod 2) = '1') then
135           reg_we (i) <= '1';
136         end if;
137       end if;
138     end loop;
139   end process;
140   
141
142   tx_control_0 : tx_control port map (
143     clk        => mclk,
144     reset      => puc,
145     tx_ready   => tx_ready,
146     fifo_empty => tx_fifo_empty,
147     tx_we      => tx_we,
148     fifo_pop   => tx_fifo_re
149   );
150
151   transmitter_0 : transmitter port map (
152     clk    => tx_clk,
153     reset  => puc,
154     data   => tx_data,
155     we     => tx_we,
156     ready  => tx_ready,
157     tx     => txd
158   );
159
160   tx_fifo : fifo port map (
161     clk      => mclk,
162     reset    => puc,
163     we       => tx_fifo_we,
164     re       => tx_fifo_re,
165     clear_ow => '0',
166     d_in     => per_din (7 downto 0),
167     d_out    => tx_data,
168     full     => open,
169     hfull    => open,
170     empty    => tx_fifo_empty,
171     overflow => open
172   );
173
174   baud_gen_0 : baud_gen port map (
175     clk      => mclk,
176     ce       => '1',
177     reset    => puc,
178     scale    => "0000000000000000",
179     clk_baud => tx_clk
180   );
181
182
183 --------------------------------------------------------------------------------
184
185   tx_fifo_we <= reg_we (UTX);
186   
187 end dataflow;
188