1 | library IEEE;
|
2 | use IEEE.STD_LOGIC_1164.ALL;
|
3 | use IEEE.STD_LOGIC_ARITH.ALL;
|
4 | use IEEE.STD_LOGIC_UNSIGNED.ALL;
|
5 |
|
6 | entity sram_rx is
|
7 | port ( clk50 : in std_logic;
|
8 | sw : in std_logic_vector ( 2 downto 0);
|
9 | ledg : out std_logic_vector ( 7 downto 0);
|
10 | ledr : out std_logic_vector ( 7 downto 0);
|
11 |
|
12 | raddr : out std_logic_vector (17 downto 0);
|
13 | rdata : inout std_logic_vector (7 downto 0);
|
14 |
|
15 | ce : out std_logic;
|
16 | we : out std_logic;
|
17 | oe : out std_logic;
|
18 | lb : out std_logic;
|
19 | ub : out std_logic;
|
20 | rxd : in std_logic
|
21 | );
|
22 | end sram_rx;
|
23 |
|
24 | architecture behavioral of sram_rx is
|
25 |
|
26 | signal addr : std_logic_vector ( 17 downto 0);
|
27 | signal data_sw : std_logic_vector ( 7 downto 0);
|
28 | signal state : std_logic_vector ( 2 downto 0);
|
29 |
|
30 | signal rx_en : std_logic;
|
31 | signal rx_data : std_logic_vector (7 downto 0);
|
32 |
|
33 | signal cnt : integer range 0 to 25000000;
|
34 |
|
35 | component rs232_rx port(
|
36 | clk50 : in std_logic;
|
37 | rxd : in std_logic;
|
38 | rx_en : out std_logic;
|
39 | rx_data : out std_logic_vector (7 downto 0)
|
40 | );
|
41 | end component;
|
42 |
|
43 | begin
|
44 |
|
45 | rx: rs232_rx
|
46 | port map(
|
47 | clk50 => clk50,
|
48 | rxd => rxd,
|
49 | rx_en => rx_en,
|
50 | rx_data => rx_data
|
51 | );
|
52 |
|
53 | process (clk50)
|
54 | begin
|
55 | if rising_edge(clk50) then
|
56 | case state is
|
57 | --- idle state
|
58 | when "000" => rdata <= (others => 'Z');
|
59 |
|
60 | --- write state
|
61 | when "001" => raddr <= addr;
|
62 | rdata <= data_sw;
|
63 | ce <= '0';
|
64 | oe <= '1';
|
65 | we <= '1';
|
66 | lb <= '0';
|
67 | ub <= '0';
|
68 | state <= "010";
|
69 |
|
70 | when "010" => ce <= '1';
|
71 | we <= '0';
|
72 | state <= "011";
|
73 |
|
74 | when "011" => ce <= '0';
|
75 | rdata <= data_sw;
|
76 | state <= "000";
|
77 |
|
78 | --- read state
|
79 | when "100" => raddr <= addr;
|
80 | rdata <= (others => 'Z');
|
81 | ce <= '0';
|
82 | oe <= '0';
|
83 | we <= '1';
|
84 | lb <= '0';
|
85 | ub <= '0';
|
86 | state <= "101";
|
87 |
|
88 | when "101" => ce <= '1';
|
89 | we <= '1';
|
90 | state <= "110";
|
91 |
|
92 | when "110" => ce <= '0';
|
93 | oe <= '1';
|
94 | ledg <= rdata;
|
95 | state <= "000";
|
96 |
|
97 | when others => state <= "000";
|
98 | end case;
|
99 |
|
100 | if rx_en='1' then
|
101 | addr <= addr+1;
|
102 | data_sw <= rx_data;
|
103 | ledr <= rx_data;
|
104 | state <= "001";
|
105 | end if;
|
106 |
|
107 | if sw(0) = '1' then
|
108 | rdata <= (others => 'Z');
|
109 | raddr <= (others => '0');
|
110 | we <= '1';
|
111 | ce <= '1';
|
112 | oe <= '1';
|
113 | end if;
|
114 |
|
115 | if sw(1) = '1' then
|
116 | addr <= (others => '0');
|
117 | data_sw <= (others => '0');
|
118 | ledr <= (others => '0');
|
119 | ledg <= (others => '0');
|
120 | end if;
|
121 |
|
122 | if sw(2) = '1' and addr < 255 then
|
123 | if (cnt<2000000) then
|
124 | cnt <= cnt+1;
|
125 | else
|
126 | cnt <= 0;
|
127 | state <= "100";
|
128 | addr <=addr+1;
|
129 | end if;
|
130 | end if;
|
131 |
|
132 | end if;
|
133 | end process;
|
134 |
|
135 | end behavioral;
|
136 |
|
137 | ------------------------------------------------------
|
138 | library ieee;
|
139 | use ieee.std_logic_1164.all;
|
140 | use ieee.numeric_std.all;
|
141 |
|
142 | entity rs232_rx is
|
143 | generic(
|
144 | quarz_taktfrequenz : integer := 50000000;
|
145 | baudrate : integer := 19200
|
146 | );
|
147 |
|
148 | port(
|
149 | clk50 : in std_logic;
|
150 | rxd : in std_logic;
|
151 | rx_en : out std_logic;
|
152 | rx_data : out std_logic_vector (7 downto 0)
|
153 | );
|
154 | end rs232_rx;
|
155 |
|
156 | architecture behavioral of rs232_rx is
|
157 |
|
158 | type rx_state_t is (idle, busy, ready);
|
159 | signal rx_state : rx_state_t := idle;
|
160 |
|
161 | signal rxd_sr : std_logic_vector (3 downto 0) := "1111";
|
162 | signal rxsr : std_logic_vector (7 downto 0) := "00000000";
|
163 | signal rxbitcnt : integer range 0 to 9 := 9;
|
164 | signal rxcnt : integer range 0 to (quarz_taktfrequenz/baudrate)-1;
|
165 |
|
166 | begin
|
167 |
|
168 | process begin
|
169 | wait until rising_edge(clk50);
|
170 | rxd_sr <= rxd_sr(rxd_sr'left-1 downto 0) & rxd;
|
171 | rx_en <= '0';
|
172 |
|
173 | case rx_state is
|
174 | when idle => -- warten auf startbit
|
175 | if (rxd_sr(3 downto 2) = "10") then
|
176 | rxcnt <= ((quarz_taktfrequenz/baudrate)-1)/2;
|
177 | rxbitcnt <= 0;
|
178 | rx_state <= busy;
|
179 | end if;
|
180 |
|
181 | when busy =>
|
182 | if (rxbitcnt<9) then
|
183 | if(rxcnt<(quarz_taktfrequenz/baudrate)-1) then
|
184 | rxcnt <= rxcnt+1;
|
185 | else
|
186 | rxcnt <= 0;
|
187 | rxbitcnt <= rxbitcnt+1;
|
188 | rxsr <= rxd_sr(rxd_sr'left-1) & rxsr(rxsr'left downto 1);
|
189 | end if;
|
190 | else
|
191 | rx_state <= ready;
|
192 | end if;
|
193 |
|
194 | when ready =>
|
195 | rx_data <= rxsr;
|
196 | rx_state <= idle;
|
197 | rx_en <= '1';
|
198 | end case;
|
199 | end process;
|
200 |
|
201 | end behavioral;
|