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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [async_ser_tx.vhd] - Blame information for rev 294

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

Line No. Rev Author Line
1 218 jshamlet
-- Copyright (c)2006, 2016, 2020 Jeremy Seth Henry
2 207 jshamlet
-- 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 220 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 :  async_ser_tx
25
-- Description:  Asynchronous transmitter wired for 8[N/E/O]1 data. Parity mode
26
--                and bit rate are set with generics.
27 209 jshamlet
--
28
-- Note: The baud rate generator will produce an approximate frequency. The
29
--        final bit rate should be within +/- 1% of the true bit rate to
30
--        ensure the receiver can successfully receive. With a sufficiently
31
--        high core clock, this is generally achievable for common PC serial
32
--        data rates.
33 218 jshamlet
--
34
-- Revision History
35
-- Author          Date     Change
36
------------------ -------- ---------------------------------------------------
37
-- Seth Henry      04/14/20 Code cleanup and revision section added
38 294 jshamlet
-- Seth Henry      09/13/21 Fixed inverted parity bit
39 207 jshamlet
 
40
library ieee;
41
use ieee.std_logic_1164.all;
42
use ieee.std_logic_unsigned.all;
43
use ieee.std_logic_arith.all;
44
use ieee.std_logic_misc.all;
45
 
46
entity async_ser_tx is
47
generic(
48 215 jshamlet
  Reset_Level                : std_logic;
49
  Enable_Parity              : boolean;
50
  Parity_Odd_Even_n          : std_logic;
51
  Clock_Divider              : integer
52 207 jshamlet
);
53
port(
54 215 jshamlet
  Clock                      : in  std_logic;
55
  Reset                      : in  std_logic;
56
  --
57
  Tx_Data                    : in  std_logic_vector(7 downto 0);
58
  Tx_Valid                   : in  std_logic;
59
  --
60
  Tx_Out                     : out std_logic;
61
  Tx_Done                    : out std_logic
62 216 jshamlet
);
63 207 jshamlet
end entity;
64
 
65
architecture behave of async_ser_tx is
66
 
67 208 jshamlet
  -- The ceil_log2 function returns the minimum register width required to
68
  --  hold the supplied integer.
69
  function ceil_log2 (x : in natural) return natural is
70
    variable retval          : natural;
71
  begin
72
    retval                   := 1;
73
    while ((2**retval) - 1) < x loop
74
      retval                 := retval + 1;
75
    end loop;
76
    return retval;
77
  end ceil_log2;
78
 
79 207 jshamlet
  constant Tick_Base         : integer := Clock_Divider - 1;
80
  constant Tick_Bits         : integer := ceil_log2(Tick_Base);
81
  constant TICK_DIV          : std_logic_vector(Tick_Bits - 1 downto 0) :=
82
                                 conv_std_logic_vector(Tick_Base, Tick_Bits);
83
 
84 208 jshamlet
  signal Tick_Cntr           : std_logic_vector(Tick_Bits - 1 downto 0) :=
85
                                 (others => '0');
86 207 jshamlet
 
87 208 jshamlet
  signal Tick_Trig           : std_logic := '0';
88
  signal Tx_Enable           : std_logic := '0';
89
  signal Tx_Buffer           : std_logic_vector(7 downto 0) := x"00";
90
  signal Tx_Parity           : std_logic := '0';
91
  signal Tx_State            : std_logic_vector(3 downto 0) := x"0";
92 207 jshamlet
  alias  Tx_Bit_Sel          is Tx_State(2 downto 0);
93
 
94
  -- State machine definitions
95
  constant IO_RSV0           : std_logic_vector(3 downto 0) := "1011"; -- B
96
  constant IO_RSV1           : std_logic_vector(3 downto 0) := "1100"; -- C
97
  constant IO_RSV2           : std_logic_vector(3 downto 0) := "1101"; -- D
98
  constant IO_IDLE           : std_logic_vector(3 downto 0) := "1110"; -- E
99
  constant IO_STRT           : std_logic_vector(3 downto 0) := "1111"; -- F
100
  constant IO_BIT0           : std_logic_vector(3 downto 0) := "0000"; -- 0
101
  constant IO_BIT1           : std_logic_vector(3 downto 0) := "0001"; -- 1
102
  constant IO_BIT2           : std_logic_vector(3 downto 0) := "0010"; -- 2
103
  constant IO_BIT3           : std_logic_vector(3 downto 0) := "0011"; -- 3
104
  constant IO_BIT4           : std_logic_vector(3 downto 0) := "0100"; -- 4
105
  constant IO_BIT5           : std_logic_vector(3 downto 0) := "0101"; -- 5
106
  constant IO_BIT6           : std_logic_vector(3 downto 0) := "0110"; -- 6
107
  constant IO_BIT7           : std_logic_vector(3 downto 0) := "0111"; -- 7
108
  constant IO_PARI           : std_logic_vector(3 downto 0) := "1000"; -- 8
109
  constant IO_STOP           : std_logic_vector(3 downto 0) := "1001"; -- 9
110
  constant IO_DONE           : std_logic_vector(3 downto 0) := "1010"; -- A
111
 
112
begin
113
 
114
  UART_Regs: process( Clock, Reset )
115
  begin
116
    if( Reset = Reset_Level )then
117
      Tick_Cntr              <= (others => '0');
118
      Tick_Trig              <= '0';
119
      Tx_State               <= IO_IDLE;
120
      Tx_Enable              <= '0';
121
      Tx_Buffer              <= (others => '0');
122
      if( Enable_Parity )then
123
        Tx_Parity            <= '0';
124
      end if;
125
      Tx_Out                 <= '1';
126
      Tx_Done                <= '0';
127
    elsif( rising_edge(Clock) )then
128
      Tick_Cntr              <= (others => '0');
129
      Tick_Trig              <= '0';
130
 
131
      if( Tx_Enable = '1' )then
132
        Tick_Cntr            <= Tick_Cntr - 1;
133
        Tick_Trig            <= '0';
134
        if( or_reduce(Tick_Cntr) = '0' )then
135
          Tick_Cntr          <= TICK_DIV;
136
          Tick_Trig          <= '1';
137
        end if;
138
      end if;
139
 
140
      if( Tx_Valid = '1' )then
141
        Tx_Buffer            <= Tx_Data;
142
        Tx_Enable            <= '1';
143
      end if;
144
 
145
      Tx_State               <= Tx_State + Tick_Trig;
146
      Tx_Done                <= '0';
147
      Tx_Out                 <= '1';
148
 
149
      case( Tx_State )is
150
        when IO_IDLE =>
151
          if( Enable_Parity )then
152 294 jshamlet
            Tx_Parity        <= not Parity_Odd_Even_n;
153 207 jshamlet
          end if;
154
 
155
        when IO_STRT =>
156
          Tx_Out             <= '0';
157
 
158
        when IO_BIT0 | IO_BIT1 | IO_BIT2 | IO_BIT3 |
159
             IO_BIT4 | IO_BIT5 | IO_BIT6 | IO_BIT7 =>
160
          Tx_Out             <= Tx_Buffer(conv_integer(Tx_Bit_Sel));
161
          if( Tick_Trig = '1' and Enable_Parity )then
162
            Tx_Parity        <= Tx_Parity xor Tx_Buffer(conv_integer(Tx_Bit_Sel));
163
          end if;
164
 
165
        when IO_PARI =>
166
          if( Enable_Parity )then
167
            Tx_Out           <= Tx_Parity;
168
          end if;
169
 
170
        when IO_STOP =>
171
 
172
        when IO_DONE =>
173
          Tx_Done            <= '1';
174
          Tx_Enable          <= '0';
175
          Tx_State           <= IO_IDLE;
176
 
177
        when others =>
178
 
179
      end case;
180
 
181
      if( Tx_Enable = '0' )then
182
        Tx_State             <= IO_IDLE;
183
      end if;
184
 
185
    end if;
186
  end process;
187
 
188
end architecture;

powered by: WebSVN 2.1.0

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