OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_uart.vhd] - Blame information for rev 60

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2 50 zero_gravi
-- # << NEORV32 - Universal Asynchronous Receiver and Transmitter (UART0/1) >>                     #
3 2 zero_gravi
-- # ********************************************************************************************* #
4 51 zero_gravi
-- # Frame configuration: 1 start bit, 8 bit data, parity bit (none/even/odd), 1 stop bit,         #
5
-- # programmable BAUD rate via clock pre-scaler and 12-bit BAUD value config register. RX engine  #
6
-- # a simple 2-entry data buffer (for double-buffering).                                          #
7
-- # Interrupts: UART_RX_available, UART_TX_done                                                   #
8 30 zero_gravi
-- #                                                                                               #
9 51 zero_gravi
-- # Support for RTS("RTR")/CTS hardware flow control:                                             #
10
-- # * uart_rts_o = 0: RX is ready to receive a new char, enabled via CTRL.ctrl_uart_rts_en_c      #
11
-- # * uart_cts_i = 0: TX is allowed to send a new char, enabled via CTRL.ctrl_uart_cts_en_c       #
12
-- #                                                                                               #
13 50 zero_gravi
-- # UART0 / UART1:                                                                                #
14
-- # This module is used for implementing UART0 and UART1. The UART_PRIMARY generic configures the #
15
-- # interface register addresses and simulation output setting for UART0 (UART_PRIMARY = true)    #
16
-- # or UART1 (UART_PRIMARY = false).                                                              #
17
-- #                                                                                               #
18 51 zero_gravi
-- # SIMULATION MODE:                                                                              #
19 30 zero_gravi
-- # When the simulation mode is enabled (setting the ctrl.ctrl_uart_sim_en_c bit) any write       #
20
-- # access to the TX register will not trigger any UART activity. Instead, the written data is    #
21
-- # output to the simulation environment. The lowest 8 bits of the written data are printed as    #
22 50 zero_gravi
-- # ASCII char to the simulator console.                                                          #
23
-- # This char is also stored to the file "neorv32.uartX.sim_mode.text.out" (where X = 0 for UART0 #
24
-- # and X = 1 for UART1). The full 32-bit write data is also stored as 8-digit hexadecimal value  #
25
-- # to the file "neorv32.uartX.sim_mode.data.out" (where X = 0 for UART0 and X = 1 for UART1).    #
26 51 zero_gravi
-- # No interrupts are triggered when in SIMULATION MODE.                                          #
27 2 zero_gravi
-- # ********************************************************************************************* #
28
-- # BSD 3-Clause License                                                                          #
29
-- #                                                                                               #
30 48 zero_gravi
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
31 2 zero_gravi
-- #                                                                                               #
32
-- # Redistribution and use in source and binary forms, with or without modification, are          #
33
-- # permitted provided that the following conditions are met:                                     #
34
-- #                                                                                               #
35
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
36
-- #    conditions and the following disclaimer.                                                   #
37
-- #                                                                                               #
38
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
39
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
40
-- #    provided with the distribution.                                                            #
41
-- #                                                                                               #
42
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
43
-- #    endorse or promote products derived from this software without specific prior written      #
44
-- #    permission.                                                                                #
45
-- #                                                                                               #
46
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
47
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
48
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
49
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
50
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
51
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
52
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
53
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
54
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
55
-- # ********************************************************************************************* #
56
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
57
-- #################################################################################################
58
 
59
library ieee;
60
use ieee.std_logic_1164.all;
61
use ieee.numeric_std.all;
62
 
63
library neorv32;
64
use neorv32.neorv32_package.all;
65 30 zero_gravi
use std.textio.all; -- obviously only for simulation
66 2 zero_gravi
 
67
entity neorv32_uart is
68 50 zero_gravi
  generic (
69
    UART_PRIMARY : boolean := true -- true = primary UART (UART0), false = secondary UART (UART1)
70
  );
71 2 zero_gravi
  port (
72
    -- host access --
73
    clk_i       : in  std_ulogic; -- global clock line
74
    addr_i      : in  std_ulogic_vector(31 downto 0); -- address
75
    rden_i      : in  std_ulogic; -- read enable
76
    wren_i      : in  std_ulogic; -- write enable
77
    data_i      : in  std_ulogic_vector(31 downto 0); -- data in
78
    data_o      : out std_ulogic_vector(31 downto 0); -- data out
79
    ack_o       : out std_ulogic; -- transfer acknowledge
80
    -- clock generator --
81
    clkgen_en_o : out std_ulogic; -- enable clock generator
82
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
83
    -- com lines --
84
    uart_txd_o  : out std_ulogic;
85
    uart_rxd_i  : in  std_ulogic;
86 51 zero_gravi
    -- hardware flow control --
87
    uart_rts_o  : out std_ulogic; -- UART.RX ready to receive ("RTR"), low-active, optional
88
    uart_cts_i  : in  std_ulogic; -- UART.TX allowed to transmit, low-active, optional
89 2 zero_gravi
    -- interrupts --
90 48 zero_gravi
    irq_rxd_o   : out std_ulogic; -- uart data received interrupt
91
    irq_txd_o   : out std_ulogic  -- uart transmission done interrupt
92 2 zero_gravi
  );
93
end neorv32_uart;
94
 
95
architecture neorv32_uart_rtl of neorv32_uart is
96
 
97 50 zero_gravi
  -- interface configuration for UART0 / UART1 --
98
  constant uart_id_base_c      : std_ulogic_vector(data_width_c-1 downto 0) := cond_sel_stdulogicvector_f(UART_PRIMARY, uart0_base_c,      uart1_base_c);
99
  constant uart_id_size_c      : natural                                    := cond_sel_natural_f(        UART_PRIMARY, uart0_size_c,      uart1_size_c);
100
  constant uart_id_ctrl_addr_c : std_ulogic_vector(data_width_c-1 downto 0) := cond_sel_stdulogicvector_f(UART_PRIMARY, uart0_ctrl_addr_c, uart1_ctrl_addr_c);
101
  constant uart_id_rtx_addr_c  : std_ulogic_vector(data_width_c-1 downto 0) := cond_sel_stdulogicvector_f(UART_PRIMARY, uart0_rtx_addr_c,  uart1_rtx_addr_c);
102
 
103
  -- IO space: module base address --
104
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
105
  constant lo_abb_c : natural := index_size_f(uart_id_size_c); -- low address boundary bit
106
 
107 30 zero_gravi
  -- simulation output configuration --
108
  constant sim_screen_output_en_c : boolean := true; -- output lowest byte as char to simulator console when enabled
109
  constant sim_text_output_en_c   : boolean := true; -- output lowest byte as char to text file when enabled
110
  constant sim_data_output_en_c   : boolean := true; -- dump 32-word to file when enabled
111
 
112 50 zero_gravi
  -- simulation output file configuration --
113
  constant sim_uart_text_file_c : string := cond_sel_string_f(UART_PRIMARY, "neorv32.uart0.sim_mode.text.out", "neorv32.uart1.sim_mode.text.out");
114
  constant sim_uart_data_file_c : string := cond_sel_string_f(UART_PRIMARY, "neorv32.uart0.sim_mode.data.out", "neorv32.uart1.sim_mode.data.out");
115 2 zero_gravi
 
116 51 zero_gravi
  -- control register --
117 2 zero_gravi
  signal ctrl : std_ulogic_vector(31 downto 0);
118
 
119 51 zero_gravi
  -- control register bits --
120 2 zero_gravi
  constant ctrl_uart_baud00_c  : natural :=  0; -- r/w: UART baud config bit 0
121
  constant ctrl_uart_baud01_c  : natural :=  1; -- r/w: UART baud config bit 1
122
  constant ctrl_uart_baud02_c  : natural :=  2; -- r/w: UART baud config bit 2
123
  constant ctrl_uart_baud03_c  : natural :=  3; -- r/w: UART baud config bit 3
124
  constant ctrl_uart_baud04_c  : natural :=  4; -- r/w: UART baud config bit 4
125
  constant ctrl_uart_baud05_c  : natural :=  5; -- r/w: UART baud config bit 5
126
  constant ctrl_uart_baud06_c  : natural :=  6; -- r/w: UART baud config bit 6
127
  constant ctrl_uart_baud07_c  : natural :=  7; -- r/w: UART baud config bit 7
128
  constant ctrl_uart_baud08_c  : natural :=  8; -- r/w: UART baud config bit 8
129
  constant ctrl_uart_baud09_c  : natural :=  9; -- r/w: UART baud config bit 9
130
  constant ctrl_uart_baud10_c  : natural := 10; -- r/w: UART baud config bit 10
131
  constant ctrl_uart_baud11_c  : natural := 11; -- r/w: UART baud config bit 11
132 51 zero_gravi
  constant ctrl_uart_sim_en_c  : natural := 12; -- r/w: UART <<SIMULATION MODE>> enable
133
  -- ...
134
  constant ctrl_uart_rts_en_c  : natural := 20; -- r/w: enable hardware flow control: assert rts_o if ready to receive
135
  constant ctrl_uart_cts_en_c  : natural := 21; -- r/w: enable hardware flow control: send only if cts_i is asserted
136 42 zero_gravi
  constant ctrl_uart_pmode0_c  : natural := 22; -- r/w: Parity config (0=even; 1=odd)
137
  constant ctrl_uart_pmode1_c  : natural := 23; -- r/w: Enable parity bit
138 2 zero_gravi
  constant ctrl_uart_prsc0_c   : natural := 24; -- r/w: UART baud prsc bit 0
139
  constant ctrl_uart_prsc1_c   : natural := 25; -- r/w: UART baud prsc bit 1
140
  constant ctrl_uart_prsc2_c   : natural := 26; -- r/w: UART baud prsc bit 2
141 51 zero_gravi
  constant ctrl_uart_cts_c     : natural := 27; -- r/-: current state of CTS input
142 2 zero_gravi
  constant ctrl_uart_en_c      : natural := 28; -- r/w: UART enable
143 51 zero_gravi
  -- ...
144 2 zero_gravi
  constant ctrl_uart_tx_busy_c : natural := 31; -- r/-: UART transmitter is busy
145
 
146
  -- data register flags --
147 51 zero_gravi
  constant data_rx_perr_c  : natural := 28; -- r/-: Rx parity error
148
  constant data_rx_ferr_c  : natural := 29; -- r/-: Rx frame error
149
  constant data_rx_overr_c : natural := 30; -- r/-: Rx data overrun
150 42 zero_gravi
  constant data_rx_avail_c : natural := 31; -- r/-: Rx data available
151 2 zero_gravi
 
152
  -- access control --
153
  signal acc_en : std_ulogic; -- module access enable
154
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
155
  signal wr_en  : std_ulogic; -- word write enable
156
  signal rd_en  : std_ulogic; -- read enable
157
 
158
  -- clock generator --
159
  signal uart_clk : std_ulogic;
160
 
161 42 zero_gravi
  -- numbers of bits in transmission frame --
162
  signal num_bits : std_ulogic_vector(03 downto 0);
163
 
164 51 zero_gravi
  -- hardware flow-control IO buffer --
165
  signal uart_cts_ff : std_ulogic_vector(1 downto 0);
166
  signal uart_rts    : std_ulogic;
167
 
168 2 zero_gravi
  -- uart tx unit --
169 42 zero_gravi
  type uart_tx_t is record
170 51 zero_gravi
    busy       : std_ulogic;
171
    done       : std_ulogic;
172
    bitcnt     : std_ulogic_vector(03 downto 0);
173
    sreg       : std_ulogic_vector(10 downto 0);
174
    baud_cnt   : std_ulogic_vector(11 downto 0);
175
    tx_granted : std_ulogic; -- allowed to start sending when 1
176
    cts        : std_ulogic; -- allow new transmission when 1
177 42 zero_gravi
  end record;
178
  signal uart_tx : uart_tx_t;
179 2 zero_gravi
 
180
  -- uart rx unit --
181 51 zero_gravi
  type ry_data_buf_t is array (0 to 1) of std_ulogic_vector(07 downto 0);
182 42 zero_gravi
  type uart_rx_t is record
183
    sync     : std_ulogic_vector(04 downto 0);
184
    busy     : std_ulogic;
185
    busy_ff  : std_ulogic;
186
    bitcnt   : std_ulogic_vector(03 downto 0);
187
    sreg     : std_ulogic_vector(09 downto 0);
188
    baud_cnt : std_ulogic_vector(11 downto 0);
189 51 zero_gravi
    rtr      : std_ulogic; -- ready to receive when 1
190
    --
191
    avail    : std_ulogic_vector(02 downto 0);
192
    data     : ry_data_buf_t;
193
    data_rd  : std_ulogic_vector(07 downto 0);
194
    ferr     : std_ulogic_vector(01 downto 0); -- frame error (stop bit not set)
195
    ferr_rd  : std_ulogic;
196
    perr     : std_ulogic_vector(01 downto 0); -- parity error
197
    perr_rd  : std_ulogic;
198 42 zero_gravi
  end record;
199
  signal uart_rx : uart_rx_t;
200 2 zero_gravi
 
201
begin
202
 
203
  -- Access Control -------------------------------------------------------------------------
204
  -- -------------------------------------------------------------------------------------------
205 50 zero_gravi
  acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = uart_id_base_c(hi_abb_c downto lo_abb_c)) else '0';
206
  addr   <= uart_id_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
207 2 zero_gravi
  wr_en  <= acc_en and wren_i;
208
  rd_en  <= acc_en and rden_i;
209
 
210
 
211
  -- Read/Write Access ----------------------------------------------------------------------
212
  -- -------------------------------------------------------------------------------------------
213
  rw_access: process(clk_i)
214
  begin
215
    if rising_edge(clk_i) then
216
      ack_o <= acc_en and (rden_i or wren_i);
217
      -- write access --
218
      if (wr_en = '1') then
219 50 zero_gravi
        if (addr = uart_id_ctrl_addr_c) then
220 42 zero_gravi
          ctrl <= (others => '0');
221
          ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c) <= data_i(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
222
          ctrl(ctrl_uart_sim_en_c)                           <= data_i(ctrl_uart_sim_en_c);
223
          ctrl(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c) <= data_i(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c);
224
          ctrl(ctrl_uart_prsc2_c  downto ctrl_uart_prsc0_c)  <= data_i(ctrl_uart_prsc2_c  downto ctrl_uart_prsc0_c);
225 51 zero_gravi
          ctrl(ctrl_uart_rts_en_c)                           <= data_i(ctrl_uart_rts_en_c);
226
          ctrl(ctrl_uart_cts_en_c)                           <= data_i(ctrl_uart_cts_en_c);
227 42 zero_gravi
          ctrl(ctrl_uart_en_c)                               <= data_i(ctrl_uart_en_c);
228 2 zero_gravi
        end if;
229
      end if;
230
      -- read access --
231
      data_o <= (others => '0');
232
      if (rd_en = '1') then
233 50 zero_gravi
        if (addr = uart_id_ctrl_addr_c) then
234 42 zero_gravi
          data_o(ctrl_uart_baud11_c downto ctrl_uart_baud00_c) <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
235
          data_o(ctrl_uart_sim_en_c)                           <= ctrl(ctrl_uart_sim_en_c);
236
          data_o(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c) <= ctrl(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c);
237
          data_o(ctrl_uart_prsc2_c  downto ctrl_uart_prsc0_c)  <= ctrl(ctrl_uart_prsc2_c  downto ctrl_uart_prsc0_c);
238 51 zero_gravi
          data_o(ctrl_uart_rts_en_c)                           <= ctrl(ctrl_uart_rts_en_c);
239
          data_o(ctrl_uart_cts_en_c)                           <= ctrl(ctrl_uart_cts_en_c);
240 42 zero_gravi
          data_o(ctrl_uart_en_c)                               <= ctrl(ctrl_uart_en_c);
241
          data_o(ctrl_uart_tx_busy_c)                          <= uart_tx.busy;
242 51 zero_gravi
          data_o(ctrl_uart_cts_c)                              <= uart_cts_ff(1);
243 50 zero_gravi
        else -- uart_id_rtx_addr_c
244 60 zero_gravi
          data_o(data_rx_avail_c) <= or_reduce_f(uart_rx.avail);
245
          data_o(data_rx_overr_c) <= and_reduce_f(uart_rx.avail);
246 51 zero_gravi
          data_o(data_rx_ferr_c)  <= uart_rx.ferr_rd;
247
          data_o(data_rx_perr_c)  <= uart_rx.perr_rd;
248
          data_o(7 downto 0)      <= uart_rx.data_rd;
249 2 zero_gravi
        end if;
250
      end if;
251
    end if;
252
  end process rw_access;
253
 
254 42 zero_gravi
  -- number of bits to be sampled --
255 51 zero_gravi
  -- if parity flag is ENABLED:  11 bit -> "1011" (1 start bit + 8 data bits + 1 parity bit + 1 stop bit)
256
  -- if parity flag is DISABLED: 10 bit -> "1010" (1 start bit + 8 data bits + 1 stop bit)
257 42 zero_gravi
  num_bits <= "1011" when (ctrl(ctrl_uart_pmode1_c) = '1') else "1010";
258 2 zero_gravi
 
259 42 zero_gravi
 
260 2 zero_gravi
  -- Clock Selection ------------------------------------------------------------------------
261
  -- -------------------------------------------------------------------------------------------
262
  -- clock enable --
263
  clkgen_en_o <= ctrl(ctrl_uart_en_c);
264
 
265
  -- uart clock select --
266
  uart_clk <= clkgen_i(to_integer(unsigned(ctrl(ctrl_uart_prsc2_c downto ctrl_uart_prsc0_c))));
267
 
268
 
269
  -- UART Transmitter -----------------------------------------------------------------------
270
  -- -------------------------------------------------------------------------------------------
271
  uart_tx_unit: process(clk_i)
272
  begin
273
    if rising_edge(clk_i) then
274
      -- serial engine --
275 42 zero_gravi
      uart_tx.done <= '0';
276
      if (uart_tx.busy = '0') or (ctrl(ctrl_uart_en_c) = '0') or (ctrl(ctrl_uart_sim_en_c) = '1') then -- idle or disabled or in SIM mode
277
        uart_tx.busy     <= '0';
278
        uart_tx.baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
279
        uart_tx.bitcnt   <= num_bits;
280
        uart_tx.sreg(0)  <= '1';
281 50 zero_gravi
        if (wr_en = '1') and (ctrl(ctrl_uart_en_c) = '1') and (addr = uart_id_rtx_addr_c) and (ctrl(ctrl_uart_sim_en_c) = '0') then -- write trigger and not in SIM mode
282 42 zero_gravi
          if (ctrl(ctrl_uart_pmode1_c) = '1') then -- add parity flag
283 60 zero_gravi
            uart_tx.sreg <= '1' & (xor_reduce_f(data_i(7 downto 0)) xor ctrl(ctrl_uart_pmode0_c)) & data_i(7 downto 0) & '0'; -- stopbit & parity bit & data & startbit
284 42 zero_gravi
          else
285
            uart_tx.sreg <= '1' & '1' & data_i(7 downto 0) & '0'; -- (dummy fill-bit &) stopbit & data & startbit
286
          end if;
287
          uart_tx.busy <= '1';
288 2 zero_gravi
        end if;
289 51 zero_gravi
      elsif (uart_clk = '1') and (uart_tx.tx_granted = '1') then
290 42 zero_gravi
        if (uart_tx.baud_cnt = x"000") then
291
          uart_tx.baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
292
          uart_tx.bitcnt   <= std_ulogic_vector(unsigned(uart_tx.bitcnt) - 1);
293
          uart_tx.sreg     <= '1' & uart_tx.sreg(uart_tx.sreg'left downto 1);
294 2 zero_gravi
        else
295 42 zero_gravi
          uart_tx.baud_cnt <= std_ulogic_vector(unsigned(uart_tx.baud_cnt) - 1);
296 2 zero_gravi
        end if;
297 42 zero_gravi
        if (uart_tx.bitcnt = "0000") then
298
          uart_tx.busy <= '0'; -- done
299
          uart_tx.done <= '1';
300
        end if;
301 2 zero_gravi
      end if;
302 51 zero_gravi
      -- transmission granted --
303
      if (ctrl(ctrl_uart_en_c) = '0') then -- disabled
304
        uart_tx.tx_granted <= '0';
305
      elsif (uart_tx.done = '1') then
306
        uart_tx.tx_granted <= '0';
307
      elsif (uart_tx.cts = '1') then
308
        uart_tx.tx_granted <= '1';
309
      end if;
310 2 zero_gravi
      -- transmitter output --
311 51 zero_gravi
      uart_txd_o <= uart_tx.sreg(0) or (not uart_tx.tx_granted); -- keep TX line idle (=high) if waiting for permission to start sending (->CTS)
312 2 zero_gravi
    end if;
313
  end process uart_tx_unit;
314
 
315
 
316
  -- UART Receiver --------------------------------------------------------------------------
317
  -- -------------------------------------------------------------------------------------------
318
  uart_rx_unit: process(clk_i)
319
  begin
320
    if rising_edge(clk_i) then
321
      -- input synchronizer --
322 42 zero_gravi
      uart_rx.sync <= uart_rxd_i & uart_rx.sync(4 downto 1);
323 2 zero_gravi
 
324
      -- serial engine --
325 51 zero_gravi
      if (uart_rx.busy = '0') or (ctrl(ctrl_uart_en_c) = '0') or (ctrl(ctrl_uart_sim_en_c) = '1') then -- idle or disabled or in SIM mode
326 42 zero_gravi
        uart_rx.busy     <= '0';
327
        uart_rx.baud_cnt <= '0' & ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud01_c); -- half baud delay at the beginning to sample in the middle of each bit
328
        uart_rx.bitcnt   <= num_bits;
329
        if (ctrl(ctrl_uart_en_c) = '0') then -- to ensure defined state when reading
330 51 zero_gravi
          uart_rx.perr <= (others => '0');
331
          uart_rx.ferr <= (others => '0');
332
          uart_rx.data <= (others => (others => '0'));
333 42 zero_gravi
        elsif (uart_rx.sync(2 downto 0) = "001") then -- start bit? (falling edge)
334
          uart_rx.busy <= '1';
335 2 zero_gravi
        end if;
336
      elsif (uart_clk = '1') then
337 42 zero_gravi
        if (uart_rx.baud_cnt = x"000") then
338
          uart_rx.baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
339
          uart_rx.bitcnt   <= std_ulogic_vector(unsigned(uart_rx.bitcnt) - 1);
340
          uart_rx.sreg     <= uart_rx.sync(0) & uart_rx.sreg(uart_rx.sreg'left downto 1);
341 2 zero_gravi
        else
342 42 zero_gravi
          uart_rx.baud_cnt <= std_ulogic_vector(unsigned(uart_rx.baud_cnt) - 1);
343 2 zero_gravi
        end if;
344 42 zero_gravi
        if (uart_rx.bitcnt = "0000") then
345
          uart_rx.busy <= '0'; -- done
346 51 zero_gravi
          -- data buffer (double buffering) --
347 60 zero_gravi
          uart_rx.perr(0) <= ctrl(ctrl_uart_pmode1_c) and (xor_reduce_f(uart_rx.sreg(8 downto 0)) xor ctrl(ctrl_uart_pmode0_c));
348 51 zero_gravi
          uart_rx.ferr(0) <= not uart_rx.sreg(9); -- check stop bit (error if not set)
349 42 zero_gravi
          if (ctrl(ctrl_uart_pmode1_c) = '1') then -- add parity flag
350 51 zero_gravi
            uart_rx.data(0) <= uart_rx.sreg(7 downto 0);
351 42 zero_gravi
          else
352 51 zero_gravi
            uart_rx.data(0) <= uart_rx.sreg(8 downto 1);
353 42 zero_gravi
          end if;
354 51 zero_gravi
          uart_rx.perr(1) <= uart_rx.perr(0);
355
          uart_rx.ferr(1) <= uart_rx.ferr(0);
356
          uart_rx.data(1) <= uart_rx.data(0);
357 42 zero_gravi
        end if;
358 2 zero_gravi
      end if;
359
 
360
      -- RX available flag --
361 42 zero_gravi
      uart_rx.busy_ff <= uart_rx.busy;
362 51 zero_gravi
      if (ctrl(ctrl_uart_en_c) = '0') then -- disabled
363
        uart_rx.avail <= "000";
364
      elsif ((uart_rx.avail(0) = '1') or (uart_rx.avail(1) = '1')) and (rd_en = '1') and (addr = uart_id_rtx_addr_c) then -- RX read access
365
        uart_rx.avail <= '0' & '0' & uart_rx.avail(1);
366 42 zero_gravi
      elsif (uart_rx.busy_ff = '1') and (uart_rx.busy = '0') then -- RX done
367 51 zero_gravi
        uart_rx.avail <= uart_rx.avail(1 downto 0) & '1';
368 2 zero_gravi
      end if;
369
    end if;
370
  end process uart_rx_unit;
371
 
372 51 zero_gravi
  -- Receiver double-buffering - buffer read --
373
  uart_rx.perr_rd <= uart_rx.perr(1) when (uart_rx.avail(1) = '1') else uart_rx.perr(0);
374
  uart_rx.ferr_rd <= uart_rx.ferr(1) when (uart_rx.avail(1) = '1') else uart_rx.ferr(0);
375
  uart_rx.data_rd <= uart_rx.data(1) when (uart_rx.avail(1) = '1') else uart_rx.data(0);
376 2 zero_gravi
 
377 51 zero_gravi
  -- RX engine ready for a new char? --
378
  uart_rx.rtr <= '1' when (uart_rx.avail(2 downto 0) = "000") and (uart_rx.busy = '0') and (uart_rx.busy_ff = '0') and (ctrl(ctrl_uart_en_c) = '1') else '0';
379
 
380
 
381
  -- Hardware Flow Control ------------------------------------------------------------------
382 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
383 51 zero_gravi
  uart_tx.cts <= (not uart_cts_ff(1)) when (ctrl(ctrl_uart_cts_en_c) = '1') else '1'; -- input is low-active, internal signal is high-active
384
  uart_rts    <= (not uart_rx.rtr)    when (ctrl(ctrl_uart_rts_en_c) = '1') else '0'; -- output is low-active
385
 
386
  -- flow-control input/output synchronizer --
387
  flow_control_buffer: process(clk_i)
388
  begin
389
    if rising_edge(clk_i) then -- should be mapped to IOBs
390
      uart_cts_ff <= uart_cts_ff(0) & uart_cts_i;
391
      uart_rts_o  <= uart_rts;
392
    end if;
393
  end process flow_control_buffer;
394
 
395
 
396
  -- Interrupts -----------------------------------------------------------------------------
397
  -- -------------------------------------------------------------------------------------------
398
  -- UART RX data available
399 48 zero_gravi
  irq_rxd_o <= uart_rx.busy_ff and (not uart_rx.busy);
400 51 zero_gravi
  -- UART TX complete
401 48 zero_gravi
  irq_txd_o <= uart_tx.done;
402 2 zero_gravi
 
403
 
404 30 zero_gravi
  -- SIMULATION Output ----------------------------------------------------------------------
405
  -- -------------------------------------------------------------------------------------------
406 56 zero_gravi
-- pragma translate_off
407
-- synthesis translate_off
408
-- RTL_SYNTHESIS OFF
409 30 zero_gravi
  sim_output: process(clk_i) -- for SIMULATION ONLY!
410 50 zero_gravi
    file file_uart_text_out : text open write_mode is sim_uart_text_file_c;
411
    file file_uart_data_out : text open write_mode is sim_uart_data_file_c;
412
    variable char_v         : integer;
413
    variable line_screen_v  : line; -- we need several line variables here since "writeline" seems to flush the source variable
414
    variable line_text_v    : line;
415
    variable line_data_v    : line;
416 30 zero_gravi
  begin
417
    if rising_edge(clk_i) then
418
      if (ctrl(ctrl_uart_en_c) = '1') and (ctrl(ctrl_uart_sim_en_c) = '1') then -- UART enabled and simulation output selected?
419 50 zero_gravi
        if (wr_en = '1') and (addr = uart_id_rtx_addr_c) then -- write access to tx register
420 30 zero_gravi
 
421
          -- print lowest byte to ASCII char --
422
          char_v := to_integer(unsigned(data_i(7 downto 0)));
423
          if (char_v >= 128) then -- out of range?
424
            char_v := 0;
425
          end if;
426
 
427
          if (char_v /= 10) and (char_v /= 13) then -- skip line breaks - they are issued via "writeline"
428
            if (sim_screen_output_en_c = true) then
429
              write(line_screen_v, character'val(char_v));
430
            end if;
431
            if (sim_text_output_en_c = true) then
432
              write(line_text_v, character'val(char_v));
433
            end if;
434
          end if;
435
 
436
          if (char_v = 10) then -- line break: write to screen and text file
437
            if (sim_screen_output_en_c = true) then
438
              writeline(output, line_screen_v);
439
            end if;
440
            if (sim_text_output_en_c = true) then
441 50 zero_gravi
              writeline(file_uart_text_out, line_text_v);
442 30 zero_gravi
            end if;
443
          end if;
444
 
445
          -- dump raw data as 8 hex char text to file --
446
          if (sim_data_output_en_c = true) then
447
            for x in 7 downto 0 loop
448
              write(line_data_v, to_hexchar_f(data_i(3+x*4 downto 0+x*4))); -- write in hex form
449
            end loop; -- x
450 50 zero_gravi
            writeline(file_uart_data_out, line_data_v);
451 30 zero_gravi
          end if;
452
 
453
        end if;
454
      end if;
455
    end if;
456
  end process sim_output;
457 56 zero_gravi
-- RTL_SYNTHESIS ON
458
-- synthesis translate_on
459
-- pragma translate_on
460 30 zero_gravi
 
461 2 zero_gravi
end neorv32_uart_rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.