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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_async_serial.vhd] - Blame information for rev 223

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

Line No. Rev Author Line
1 207 jshamlet
-- Copyright (c)2020 Jeremy Seth Henry
2
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 213 jshamlet
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 207 jshamlet
--
24
-- VHDL Units :  o8_async_serial
25
-- Description:  Provides a single 8-bit, asynchronous transceiver. While the
26
--               width is fixed at 8-bits, the bit rate and parity controls
27
--               are settable via generics.
28 209 jshamlet
--
29 213 jshamlet
-- Register Map:
30
-- Offset  Bitfield Description                        Read/Write
31
--   0x00  AAAAAAAA TX Data (WR) RX Data (RD)             (RW)
32
--   0x01  DCBA---- FIFO Status                           (RO)
33
--                  A: RX FIFO Empty
34
--                  B: RX FIFO almost full (922/1024)
35
--                  C: TX FIFO Empty
36
--                  D: TX FIFO almost full (922/1024)
37
--
38 209 jshamlet
-- Note: The baud rate generator will produce an approximate frequency. The
39
--        final bit rate should be within +/- 1% of the true bit rate to
40
--        ensure the receiver can successfully receive. With a sufficiently
41
--        high core clock, this is generally achievable for common PC serial
42
--        data rates.
43 213 jshamlet
--
44
-- Revision History
45
-- Author          Date     Change
46
------------------ -------- ---------------------------------------------------
47
-- Seth Henry      12/20/19 Design Start
48
-- Seth Henry      04/10/20 Code cleanup and register documentation
49 207 jshamlet
 
50
library ieee;
51
use ieee.std_logic_1164.all;
52
use ieee.std_logic_unsigned.all;
53
use ieee.std_logic_arith.all;
54
use ieee.std_logic_misc.all;
55
 
56
library work;
57
  use work.open8_pkg.all;
58
 
59
entity o8_async_serial is
60
generic(
61 217 jshamlet
  Disable_Transmit           : boolean := FALSE;
62
  Disable_Receive            : boolean := FALSE;
63
  Bit_Rate                   : real;
64
  Enable_Parity              : boolean;
65
  Parity_Odd_Even_n          : std_logic;
66
  Sys_Freq                   : real;
67
  Reset_Level                : std_logic;
68
  Address                    : ADDRESS_TYPE
69 207 jshamlet
);
70
port(
71 217 jshamlet
  Clock                      : in  std_logic;
72
  Reset                      : in  std_logic;
73 207 jshamlet
  --
74 223 jshamlet
  Open8_Bus                  : in  OPEN8_BUS_TYPE;
75 217 jshamlet
  Rd_Data                    : out DATA_TYPE;
76 207 jshamlet
  --
77 217 jshamlet
  TX_Out                     : out std_logic;
78
  CTS_In                     : in  std_logic;
79
  RX_In                      : in  std_logic;
80
  RTS_Out                    : out std_logic
81 207 jshamlet
);
82
end entity;
83
 
84
architecture behave of o8_async_serial is
85
 
86
  signal FIFO_Reset          : std_logic := '0';
87
 
88
  constant User_Addr         : std_logic_vector(15 downto 1) :=
89
                                Address(15 downto 1);
90 223 jshamlet
  alias  Comp_Addr           is Open8_Bus.Address(15 downto 1);
91 207 jshamlet
  signal Addr_Match          : std_logic := '0';
92
 
93 223 jshamlet
  alias  Reg_Addr            is Open8_Bus.Address(0);
94 207 jshamlet
  signal Reg_Sel             : std_logic := '0';
95
  signal Rd_En               : std_logic := '0';
96
 
97
  signal TX_FIFO_Wr_En       : std_logic := '0';
98 223 jshamlet
  alias  TX_FIFO_Wr_Data     is Open8_Bus.Wr_Data;
99 207 jshamlet
  signal TX_FIFO_Rd_En       : std_logic := '0';
100
  signal TX_FIFO_Empty       : std_logic := '0';
101
  signal TX_FIFO_AFull       : std_logic := '0';
102
  signal TX_FIFO_Rd_Data     : DATA_TYPE := x"00";
103
 
104
  alias  Tx_Data             is TX_FIFO_Rd_Data;
105
 
106
  type TX_CTRL_STATES is (IDLE, TX_BYTE, TX_START, TX_WAIT );
107
  signal TX_Ctrl             : TX_CTRL_STATES := IDLE;
108
 
109
  signal TX_Xmit             : std_logic := '0';
110
  signal TX_Done             : std_logic := '0';
111
 
112
  constant BAUD_RATE_DIV     : integer := integer(Sys_Freq / Bit_Rate);
113
 
114
  signal CTS_sr              : std_logic_vector(3 downto 0) := "0000";
115
  alias  CTS_Okay            is CTS_sr(3);
116
 
117
  signal RX_FIFO_Wr_En       : std_logic := '0';
118 209 jshamlet
  signal RX_FIFO_Wr_Data     : DATA_TYPE := x"00";
119
  signal RX_FIFO_Rd_En       : std_logic := '0';
120
  signal RX_FIFO_Empty       : std_logic := '0';
121
  signal RX_FIFO_AFull       : std_logic := '0';
122
  signal RX_FIFO_Rd_Data     : DATA_TYPE := x"00";
123 207 jshamlet
 
124
begin
125
 
126
  Addr_Match                 <= '1' when Comp_Addr = User_Addr else '0';
127
 
128
  io_reg: process( Clock, Reset )
129
  begin
130
    if( Reset = Reset_Level )then
131 217 jshamlet
      Rd_En                  <= '0';
132
      Rd_Data                <= OPEN8_NULLBUS;
133
      RTS_Out                <= '0';
134 207 jshamlet
    elsif( rising_edge( Clock ) )then
135 217 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
136 223 jshamlet
      Rd_En                  <= Addr_Match and Open8_Bus.Rd_En;
137 217 jshamlet
      Reg_Sel                <= Reg_Addr;
138 207 jshamlet
      if( Rd_En = '1' and Reg_Sel = '1' )then
139 217 jshamlet
        Rd_Data(4)           <= RX_FIFO_Empty;
140
        Rd_Data(5)           <= RX_FIFO_AFull;
141
        Rd_Data(6)           <= TX_FIFO_Empty;
142
        Rd_Data(7)           <= TX_FIFO_AFull;
143 207 jshamlet
      end if;
144
      if( Rd_En = '1' and Reg_Sel = '0' )then
145 217 jshamlet
        Rd_Data              <= RX_FIFO_Rd_Data;
146 207 jshamlet
      end if;
147 217 jshamlet
      RTS_Out                <= not RX_FIFO_AFull;
148 207 jshamlet
    end if;
149
  end process;
150
 
151 213 jshamlet
TX_Disabled : if( Disable_Transmit )generate
152
 
153
  TX_FIFO_Empty              <= '1';
154
  TX_FIFO_AFull              <= '0';
155
  TX_Out                     <= '1';
156
 
157
end generate;
158
 
159
TX_Enabled : if( not Disable_Transmit )generate
160
 
161 223 jshamlet
  TX_FIFO_Wr_En              <= Open8_Bus.Wr_En and
162
                                Addr_Match and
163
                                (not Reg_Addr);
164 207 jshamlet
 
165
  FIFO_Reset                 <= '1' when Reset = Reset_Level else '0';
166
 
167
  U_TX_FIFO : entity work.fifo_1k_core
168
  port map(
169
    aclr                     => FIFO_Reset,
170
    clock                    => Clock,
171
    data                     => TX_FIFO_Wr_Data,
172
    rdreq                    => TX_FIFO_Rd_En,
173
    wrreq                    => TX_FIFO_Wr_En,
174
    empty                    => TX_FIFO_Empty,
175
    almost_full              => TX_FIFO_AFull,
176
    q                        => TX_FIFO_Rd_Data
177
  );
178
 
179
  tx_FSM: process( Clock, Reset )
180
  begin
181
    if( Reset = Reset_Level )then
182
      TX_Ctrl                <= IDLE;
183
      TX_Xmit                <= '0';
184
      TX_FIFO_Rd_En          <= '0';
185
      CTS_sr                 <= (others => '0');
186
    elsif( rising_edge(Clock) )then
187
      TX_Xmit                <= '0';
188
      TX_FIFO_Rd_En          <= '0';
189
      CTS_sr                 <= CTS_sr(2 downto 0) & CTS_In;
190
 
191
      case( TX_Ctrl )is
192
        when IDLE =>
193
          if( TX_FIFO_Empty = '0' and CTS_Okay = '1' )then
194
            TX_FIFO_Rd_En    <= '1';
195
            TX_Ctrl          <= TX_BYTE;
196
          end if;
197
 
198
        when TX_BYTE =>
199
          TX_Xmit            <= '1';
200
          TX_Ctrl            <= TX_START;
201
 
202
        when TX_START =>
203
          if( Tx_Done = '0' )then
204
            TX_Ctrl          <= TX_WAIT;
205
          end if;
206
 
207
        when TX_WAIT =>
208
          if( Tx_Done = '1' )then
209
            TX_Ctrl          <= IDLE;
210
          end if;
211
 
212
        when others => null;
213
      end case;
214
 
215
    end if;
216
  end process;
217
 
218
  U_TX : entity work.async_ser_tx
219
  generic map(
220
    Reset_Level              => Reset_Level,
221
    Enable_Parity            => Enable_Parity,
222
    Parity_Odd_Even_n        => Parity_Odd_Even_n,
223
    Clock_Divider            => BAUD_RATE_DIV
224
  )
225
  port map(
226
    Clock                    => Clock,
227
    Reset                    => Reset,
228
    --
229
    Tx_Data                  => Tx_Data,
230
    Tx_Valid                 => TX_Xmit,
231
    --
232
    Tx_Out                   => TX_Out,
233
    Tx_Done                  => Tx_Done
234
  );
235
 
236 213 jshamlet
end generate;
237
 
238
RX_Disabled : if( Disable_Transmit )generate
239
 
240
  RX_FIFO_Empty              <= '1';
241
  RX_FIFO_AFull              <= '0';
242
  RX_FIFO_Rd_Data            <= x"00";
243
 
244
end generate;
245
 
246
RX_Enabled : if( not Disable_Receive )generate
247
 
248 207 jshamlet
  U_RX : entity work.async_ser_rx
249
  generic map(
250
    Reset_Level              => Reset_Level,
251
    Enable_Parity            => Enable_Parity,
252
    Parity_Odd_Even_n        => Parity_Odd_Even_n,
253
    Clock_Divider            => BAUD_RATE_DIV
254
  )
255
  port map(
256
    Clock                    => Clock,
257
    Reset                    => Reset,
258
    --
259
    Rx_In                    => RX_In,
260
    --
261
    Rx_Data                  => RX_FIFO_Wr_Data,
262
    Rx_Valid                 => RX_FIFO_Wr_En,
263
    Rx_PErr                  => open
264
  );
265
 
266 223 jshamlet
  RX_FIFO_Rd_En              <= Open8_Bus.Rd_En and
267
                                Addr_Match and
268
                                (not Reg_Addr);
269 207 jshamlet
 
270
  U_RX_FIFO : entity work.fifo_1k_core
271
  port map(
272
    aclr                     => FIFO_Reset,
273
    clock                    => Clock,
274
    data                     => RX_FIFO_Wr_Data,
275
    rdreq                    => RX_FIFO_Rd_En,
276
    wrreq                    => RX_FIFO_Wr_En,
277
    empty                    => RX_FIFO_Empty,
278
    almost_full              => RX_FIFO_AFull,
279
    q                        => RX_FIFO_Rd_Data
280
  );
281
 
282 213 jshamlet
end generate;
283
 
284 207 jshamlet
end architecture;

powered by: WebSVN 2.1.0

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