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

Subversion Repositories light8080

[/] [light8080/] [trunk/] [vhdl/] [demos/] [4kbasic/] [rs232_rx.vhdl] - Blame information for rev 82

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 82 ja_rd
--##############################################################################
2
-- RS-232 receiver, parametrizable bit rate through generics.
3
-- Bit rate defaults to 19200 bps @50MHz.
4
-- WARNING: Hacked up for light8080 demo. Poor performance, no formal testing!
5
-- I don't advise using this in for any general purpose.
6
--##############################################################################
7
 
8
library ieee;
9
use ieee.std_logic_1164.all;
10
use ieee.std_logic_arith.all;
11
use ieee.std_logic_unsigned.all;
12
 
13
entity rs232_rx is
14
  generic (
15
    BAUD_RATE     : integer := 19200;
16
    CLOCK_FREQ    : integer := 50000000);
17
    port (
18
        rxd       : in std_logic;
19
 
20
        data_rx   : out std_logic_vector(7 downto 0);
21
        rx_rdy    : out std_logic;
22
        read_rx   : in std_logic;
23
 
24
        clk       : in std_logic;
25
        reset     : in std_logic);
26
end rs232_rx;
27
 
28
architecture hardwired of rs232_rx is
29
 
30
-- Bit sampling period is 1/16 of the baud rate
31
constant SAMPLING_PERIOD : integer := (CLOCK_FREQ / BAUD_RATE) / 16;
32
 
33
 
34
--##############################################################################
35
 
36
-- Serial port signals
37
signal rxd_q :            std_logic;
38
signal tick_ctr :         std_logic_vector(3 downto 0);
39
signal state :            std_logic_vector(3 downto 0);
40
signal next_state :       std_logic_vector(3 downto 0);
41
signal start_bit_detected : std_logic;
42
signal reset_tick_ctr :   std_logic;
43
signal stop_bit_sampled : std_logic;
44
signal load_rx_buffer :   std_logic;
45
signal stop_error :       std_logic;
46
signal samples :          std_logic_vector(2 downto 0);
47
signal sampled_bit :      std_logic;
48
signal do_shift :         std_logic;
49
signal rx_buffer :        std_logic_vector(7 downto 0);
50
signal rx_shift_reg :     std_logic_vector(9 downto 0);
51
signal tick_ctr_enable :  std_logic;
52
signal tick_baud_ctr :    std_logic_vector(10 downto 0);
53
 
54
signal rx_rdy_flag :      std_logic;
55
signal set_rx_rdy_flag :  std_logic;
56
 
57
 
58
begin
59
 
60
process(clk)
61
begin
62
  if clk'event and clk='1' then
63
    if reset='1' then
64
      tick_baud_ctr <= (others => '0');
65
    else
66
      if conv_integer(tick_baud_ctr)=SAMPLING_PERIOD then -- 325 for 9600 bps
67
        tick_baud_ctr <= (others => '0');
68
      else
69
        tick_baud_ctr <= tick_baud_ctr + 1;
70
      end if;
71
    end if;
72
  end if;
73
end process;
74
 
75
tick_ctr_enable<= '1' when conv_integer(tick_baud_ctr)=SAMPLING_PERIOD else '0';
76
 
77
process(clk)
78
begin
79
  if clk'event and clk='1' then
80
    if reset='1' then
81
      rxd_q <= '0';
82
    else
83
      if tick_ctr_enable='1' then
84
        rxd_q <= rxd;
85
      end if;
86
    end if;
87
  end if;
88
end process;
89
 
90
 
91
start_bit_detected <= '1' when state="0000" and rxd_q='1' and rxd='0' else '0';
92
reset_tick_ctr <= '1' when start_bit_detected='1' else '0';
93
 
94
stop_bit_sampled <= '1' when state="1010" and tick_ctr="1011" else '0';
95
load_rx_buffer <= '1' when stop_bit_sampled='1' and sampled_bit='1' else '0';
96
stop_error <= '1' when stop_bit_sampled='1' and sampled_bit='0' else '0';
97
 
98
process(clk)
99
begin
100
  if clk'event and clk='1' then
101
    if reset='1' then
102
      tick_ctr <= "0000";
103
    else
104
      if tick_ctr_enable='1' then
105
        if tick_ctr="1111" or reset_tick_ctr='1' then
106
          tick_ctr <= "0000";
107
        else
108
          tick_ctr <= tick_ctr + 1;
109
        end if;
110
      end if;
111
    end if;
112
  end if;
113
end process;
114
 
115
next_state <=
116
  "0001" when state="0000" and start_bit_detected='1' else
117
  "0000" when state="0001" and tick_ctr="1010" and sampled_bit='1' else
118
  "0000" when state="1010" and tick_ctr="1111" else
119
  state + 1 when tick_ctr="1111" and do_shift='1' else
120
  state;
121
 
122
 
123
process(clk)
124
begin
125
  if clk'event and clk='1' then
126
    if reset='1' then
127
      state <= "0000";
128
    else
129
      if tick_ctr_enable='1' then
130
        state <= next_state;
131
      end if;
132
    end if;
133
  end if;
134
end process;
135
 
136
process(clk)
137
begin
138
  if clk'event and clk='1' then
139
    if reset='1' then
140
      samples <= "000";
141
    else
142
      if tick_ctr_enable='1' then
143
        if tick_ctr="0111" then
144
          samples(0) <= rxd;
145
        end if;
146
        if tick_ctr="1000" then
147
          samples(1) <= rxd;
148
        end if;
149
        if tick_ctr="1001" then
150
          samples(2) <= rxd;
151
        end if;
152
      end if;
153
    end if;
154
  end if;
155
end process;
156
 
157
with samples select
158
  sampled_bit <=  '0' when "000",
159
                  '0' when "001",
160
                  '0' when "010",
161
                  '1' when "011",
162
                  '0' when "100",
163
                  '1' when "101",
164
                  '1' when "110",
165
                  '1' when others;
166
 
167
process(clk)
168
begin
169
  if clk'event and clk='1' then
170
    if reset='1' then
171
      rx_buffer <= "00000000";
172
      set_rx_rdy_flag <= '0';
173
    else
174
      if tick_ctr_enable='1' and load_rx_buffer='1' and rx_rdy_flag='0' then
175
        rx_buffer <= rx_shift_reg(8 downto 1);
176
        set_rx_rdy_flag <= '1';
177
      else
178
        set_rx_rdy_flag <= '0';
179
      end if;
180
    end if;
181
  end if;
182
end process;
183
 
184
process(clk)
185
begin
186
  if clk'event and clk='1' then
187
    if reset='1' then
188
      rx_rdy_flag <= '0';
189
    else
190
      if set_rx_rdy_flag='1' then
191
        rx_rdy_flag <= '1';
192
      else
193
        if read_rx = '1' then
194
          rx_rdy_flag <= '0';
195
        end if;
196
      end if;
197
    end if;
198
  end if;
199
end process;
200
 
201
do_shift <= state(0) or state(1) or state(2) or state(3);
202
 
203
-- reception shift register
204
process(clk)
205
begin
206
  if clk'event and clk='1' then
207
    if reset='1' then
208
      rx_shift_reg <= "1111111111";
209
    else
210
      if tick_ctr_enable='1' then
211
        if tick_ctr="1010" and do_shift='1' then
212
          rx_shift_reg(9) <= sampled_bit;
213
          rx_shift_reg(8 downto 0) <= rx_shift_reg(9 downto 1);
214
        end if;
215
      end if;
216
    end if;
217
  end if;
218
end process;
219
 
220
rx_rdy <= rx_rdy_flag;
221
 
222
data_rx <= rx_buffer;
223
 
224
end hardwired;

powered by: WebSVN 2.1.0

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