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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [async_ser_rx.vhd] - Blame information for rev 207

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

Line No. Rev Author Line
1 207 jshamlet
-- Copyright (c)2006, 2016, 2019 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
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
--
24
-- VHDL Units :  async_ser_rx
25
-- Description:  Asynchronous receiver wired for 8[N/E/O]1 data. Parity mode
26
--                and bit rate are set with generics.
27
 
28
library ieee;
29
use ieee.std_logic_1164.all;
30
use ieee.std_logic_unsigned.all;
31
use ieee.std_logic_arith.all;
32
use ieee.std_logic_misc.all;
33
 
34
library work;
35
  use work.open8_pkg.all;
36
 
37
entity async_ser_rx is
38
generic(
39
    Reset_Level              : std_logic;
40
    Enable_Parity            : boolean;
41
    Parity_Odd_Even_n        : std_logic;
42
    Clock_Divider            : integer
43
);
44
port(
45
    Clock                    : in  std_logic;
46
    Reset                    : in  std_logic;
47
    --
48
    Rx_In                    : in  std_logic;
49
    --
50
    Rx_Data                  : out std_logic_vector(7 downto 0);
51
    Rx_Valid                 : out std_logic;
52
    Rx_PErr                  : out std_logic
53
);
54
end entity;
55
 
56
architecture behave of async_ser_rx is
57
 
58
  -- The ceil_log2 function returns the minimum register width required to
59
  --  hold the supplied integer.
60
  function ceil_log2 (x : in natural) return natural is
61
    variable retval          : natural;
62
  begin
63
    retval                   := 1;
64
    while ((2**retval) - 1) < x loop
65
      retval                 := retval + 1;
66
    end loop;
67
    return retval;
68
  end ceil_log2;
69
 
70
  -- Period of each bit in sub-clocks (subtract one to account for zero)
71
  constant Half_Per_i        : integer := (Clock_Divider / 2) - 1;
72
  constant Full_Per_i        : integer := Clock_Divider - 1;
73
  constant Baud_Bits         : integer := ceil_log2(Full_Per_i);
74
 
75
  constant HALF_PERIOD       : std_logic_vector(Baud_Bits - 1 downto 0) :=
76
                                 conv_std_logic_vector(Half_Per_i, Baud_Bits);
77
  constant FULL_PERIOD       : std_logic_vector(Baud_Bits - 1 downto 0) :=
78
                                 conv_std_logic_vector(Full_Per_i, Baud_Bits);
79
 
80
  signal Rx_Baud_Cntr        : std_logic_vector(Baud_Bits - 1 downto 0);
81
 
82
  signal Rx_In_SR            : std_logic_vector(3 downto 0);
83
  alias  Rx_In_Q             is Rx_In_SR(3);
84
 
85
  signal Rx_Buffer           : std_logic_vector(7 downto 0);
86
  signal Rx_Parity           : std_logic;
87
  signal Rx_PErr_int         : std_logic;
88
 
89
  signal Rx_State            : std_logic_vector(3 downto 0);
90
  alias  Rx_Bit_Sel          is Rx_State(2 downto 0);
91
 
92
  -- State machine definitions
93
  constant IO_RSV0           : std_logic_vector(3 downto 0) := "1011"; -- B
94
  constant IO_RSV1           : std_logic_vector(3 downto 0) := "1100"; -- C
95
  constant IO_STRT           : std_logic_vector(3 downto 0) := "1101"; -- D
96
  constant IO_IDLE           : std_logic_vector(3 downto 0) := "1110"; -- E
97
  constant IO_SYNC           : std_logic_vector(3 downto 0) := "1111"; -- F
98
  constant IO_BIT0           : std_logic_vector(3 downto 0) := "0000"; -- 0
99
  constant IO_BIT1           : std_logic_vector(3 downto 0) := "0001"; -- 1
100
  constant IO_BIT2           : std_logic_vector(3 downto 0) := "0010"; -- 2
101
  constant IO_BIT3           : std_logic_vector(3 downto 0) := "0011"; -- 3
102
  constant IO_BIT4           : std_logic_vector(3 downto 0) := "0100"; -- 4
103
  constant IO_BIT5           : std_logic_vector(3 downto 0) := "0101"; -- 5
104
  constant IO_BIT6           : std_logic_vector(3 downto 0) := "0110"; -- 6
105
  constant IO_BIT7           : std_logic_vector(3 downto 0) := "0111"; -- 7
106
  constant IO_PARI           : std_logic_vector(3 downto 0) := "1000"; -- 8
107
  constant IO_STOP           : std_logic_vector(3 downto 0) := "1001"; -- 9
108
  constant IO_DONE           : std_logic_vector(3 downto 0) := "1010"; -- A
109
 
110
begin
111
 
112
  Rx_Perr                    <= Rx_PErr_int;
113
 
114
  UART_Regs: process( Clock, Reset )
115
  begin
116
    if( Reset = Reset_Level )then
117
      Rx_In_SR               <= (others => '0');
118
      Rx_State               <= IO_IDLE;
119
      Rx_Baud_Cntr           <= (others => '0');
120
      Rx_Buffer              <= (others => '0');
121
      Rx_Parity              <= '0';
122
      Rx_Data                <= (others => '0');
123
      Rx_Valid               <= '0';
124
      Rx_PErr_int            <= '0';
125
    elsif( rising_edge(Clock) )then
126
      Rx_In_SR               <= Rx_In_SR(2 downto 0) & Rx_In;
127
 
128
      Rx_Valid               <= '0';
129
      case( Rx_State )is
130
        when IO_STRT =>
131
          if( Rx_In_Q = '1' )then
132
            Rx_State         <= Rx_State + 1;
133
          end if;
134
 
135
        when IO_IDLE =>
136
          Rx_Baud_Cntr       <= HALF_PERIOD;
137
          Rx_Parity          <= Parity_Odd_Even_n;
138
          if( Rx_In_Q = '0' )then
139
            Rx_State         <= Rx_State + 1;
140
          end if;
141
 
142
        when IO_SYNC =>
143
          Rx_Baud_Cntr       <= Rx_Baud_Cntr - 1;
144
          if( Rx_Baud_Cntr = 0)then
145
            Rx_Baud_Cntr     <= FULL_PERIOD;
146
            Rx_State         <= Rx_State + 1;
147
            if( Rx_In_Q = '1' )then -- RxD going low was spurious
148
              Rx_State       <= IO_IDLE;
149
            end if;
150
          end if;
151
 
152
        when IO_BIT0 | IO_BIT1 | IO_BIT2 | IO_BIT3 |
153
             IO_BIT4 | IO_BIT5 | IO_BIT6 | IO_BIT7 =>
154
          Rx_Baud_Cntr       <= Rx_Baud_Cntr - 1;
155
          if( Rx_Baud_Cntr = 0 )then
156
            Rx_Baud_Cntr     <= FULL_PERIOD;
157
            Rx_Buffer(conv_integer(Rx_Bit_Sel)) <= Rx_In_Q;
158
            if( Enable_Parity )then
159
              Rx_Parity      <= Rx_Parity xor Rx_In_Q;
160
              Rx_State       <= Rx_State + 1;
161
            else
162
              Rx_PErr_int    <= '0';
163
              Rx_State       <= Rx_State + 2;
164
            end if;
165
          end if;
166
 
167
        when IO_PARI =>
168
          Rx_Baud_Cntr       <= Rx_Baud_Cntr - 1;
169
          if( Rx_Baud_Cntr = 0 )then
170
            Rx_Baud_Cntr     <= FULL_PERIOD;
171
            Rx_PErr_int      <= Rx_Parity xor Rx_In_Q;
172
            Rx_State         <= Rx_State + 1;
173
          end if;
174
 
175
        when IO_STOP =>
176
          Rx_Baud_Cntr       <= Rx_Baud_Cntr - 1;
177
          if( Rx_Baud_Cntr = 0 )then
178
            Rx_State         <= Rx_State + 1;
179
          end if;
180
 
181
        when IO_DONE =>
182
          Rx_Data            <= Rx_Buffer;
183
          Rx_Valid           <= not Rx_PErr_int;
184
          Rx_State           <= Rx_State + 1;
185
 
186
        when others =>
187
          Rx_State           <= IO_IDLE;
188
 
189
      end case;
190
 
191
    end if;
192
  end process;
193
 
194
end architecture;

powered by: WebSVN 2.1.0

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