1 |
3 |
guanucolui |
----------------------------------------------------------------------------------
|
2 |
|
|
-- Company:
|
3 |
|
|
-- Engineer:
|
4 |
|
|
--
|
5 |
|
|
-- Create Date: 00:19:21 06/04/2010
|
6 |
|
|
-- Design Name:
|
7 |
|
|
-- Module Name: det_top - Behavioral
|
8 |
|
|
-- Project Name:
|
9 |
|
|
-- Target Devices:
|
10 |
|
|
-- Tool versions:
|
11 |
|
|
-- Description:
|
12 |
|
|
--
|
13 |
|
|
-- Dependencies:
|
14 |
|
|
--
|
15 |
|
|
-- Revision:
|
16 |
|
|
-- Revision 0.01 - File Created
|
17 |
|
|
-- Additional Comments:
|
18 |
|
|
--
|
19 |
|
|
-- TRAMA:
|
20 |
|
|
-- The allowable characters transmitted for all other fields are hexadecimal 09, AF (ASCII coded). The devices monitor the bus
|
21 |
|
|
-- continuously for the colon character(:). When this character is received, each device decodes the next character until it detects the
|
22 |
|
|
-- End-Of-Frame(CR Y LF) --> (en ese orden).
|
23 |
|
|
|
24 |
|
|
----------------------------------------------------------------------------------
|
25 |
|
|
library IEEE;
|
26 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
27 |
|
|
use IEEE.STD_LOGIC_ARITH.ALL;
|
28 |
|
|
use IEEE.STD_LOGIC_UNSIGNED.ALL;
|
29 |
|
|
|
30 |
|
|
---- Uncomment the following library declaration if instantiating
|
31 |
|
|
---- any Xilinx primitives in this code.
|
32 |
|
|
--library UNISIM;
|
33 |
|
|
--use UNISIM.VComponents.all;
|
34 |
|
|
|
35 |
|
|
entity det_top is
|
36 |
|
|
generic (
|
37 |
|
|
DIRE_LOCAL_ALTO : std_logic_vector(7 downto 0) := "00110001"; -- 1 ASCII
|
38 |
|
|
DIRE_LOCAL_BAJO : std_logic_vector(7 downto 0) := "00110001"; -- 1 ASCII
|
39 |
|
|
bits : integer := 8; -- ancho de datos de la memoria
|
40 |
|
|
addr_bits : integer := 8 -- 2^addr_bits = numero bits de direccionamiento
|
41 |
|
|
);
|
42 |
|
|
Port (
|
43 |
|
|
clk :in std_logic;
|
44 |
|
|
reset :in std_logic;
|
45 |
|
|
data :in std_logic_vector(7 downto 0);
|
46 |
|
|
new_data :in std_logic;
|
47 |
|
|
error :out std_logic;
|
48 |
|
|
end_det :out std_logic;
|
49 |
|
|
--para escritura de ram:
|
50 |
|
|
E :out std_logic; -- habilitador de la ram
|
51 |
|
|
WE :out std_logic; -- habilitador de escritura
|
52 |
|
|
ADDR :out std_logic_vector(addr_bits-1 downto 0);
|
53 |
|
|
data_ram :out std_logic_vector(bits-1 downto 0)); --dato a guardar en ram
|
54 |
|
|
end det_top;
|
55 |
|
|
|
56 |
|
|
architecture Behavioral of det_top is
|
57 |
|
|
|
58 |
|
|
--*******************************************************************
|
59 |
|
|
-- DECLARACION COMPONENTE ASCII a BIN
|
60 |
|
|
--*******************************************************************
|
61 |
|
|
component ascii_bin
|
62 |
|
|
port(
|
63 |
|
|
clk :in std_logic;
|
64 |
|
|
reset :in std_logic;
|
65 |
|
|
new_data :in std_logic;
|
66 |
|
|
Nnew_data :out std_logic;
|
67 |
|
|
ascii :in std_logic_vector(7 downto 0);
|
68 |
|
|
bin :out std_logic_vector(7 downto 0));
|
69 |
|
|
end component;
|
70 |
|
|
|
71 |
|
|
--*******************************************************************
|
72 |
|
|
-- DECLARACION COMPONENTE PONDERA
|
73 |
|
|
--*******************************************************************
|
74 |
|
|
component pondera_top
|
75 |
|
|
port(
|
76 |
|
|
clk: in std_logic;
|
77 |
|
|
reset: in std_logic;
|
78 |
|
|
bin_HL: in std_logic_vector(7 downto 0);
|
79 |
|
|
new_data: in std_logic;
|
80 |
|
|
trama_ok: in std_logic;
|
81 |
|
|
bin: out std_logic_vector(7 downto 0);
|
82 |
|
|
bin_ok: out std_logic
|
83 |
|
|
);
|
84 |
|
|
end component;
|
85 |
|
|
|
86 |
|
|
--*******************************************************************
|
87 |
|
|
-- DECLARACION COMPONENTE CALCULO LRC
|
88 |
|
|
--*******************************************************************
|
89 |
|
|
component lrc
|
90 |
|
|
port(
|
91 |
|
|
clk :in std_logic;
|
92 |
|
|
reset :in std_logic;
|
93 |
|
|
trama :in std_logic;
|
94 |
|
|
dato_ok :in std_logic;
|
95 |
|
|
dato :in std_logic_vector(7 downto 0);
|
96 |
|
|
lrc_ok :out std_logic);
|
97 |
|
|
end component;
|
98 |
|
|
|
99 |
|
|
--*******************************************************************
|
100 |
|
|
-- SEALES MAQUINA DE ESTADO
|
101 |
|
|
--*******************************************************************
|
102 |
|
|
|
103 |
|
|
type state_type is (st1_det, st2_dire_alto, st3_dire_bajo, st4_comp, st5_func_alto, st6_func_bajo, st7_CR, st8_dato_y_LRC_rec, st9_LF);
|
104 |
|
|
signal state, next_state : state_type;
|
105 |
|
|
|
106 |
|
|
signal Scomp : std_logic:='0';
|
107 |
|
|
signal Serror : std_logic:='0';
|
108 |
|
|
signal SCR : std_logic_vector(7 downto 0):=(others => '0');
|
109 |
|
|
signal SLF : std_logic_vector(7 downto 0):=(others => '0');
|
110 |
|
|
signal Sdire_bajo : std_logic_vector(7 downto 0):=(others => '0');
|
111 |
|
|
signal Sdire_alto : std_logic_vector(7 downto 0):=(others => '0');
|
112 |
|
|
signal Sfunc_bajo : std_logic_vector(7 downto 0):=(others => '0');
|
113 |
|
|
signal Sfunc_alto : std_logic_vector(7 downto 0):=(others => '0');
|
114 |
|
|
|
115 |
|
|
--*******************************************************************
|
116 |
|
|
-- SEALES BLOQUE RAM
|
117 |
|
|
--*******************************************************************
|
118 |
|
|
signal SEram : std_logic; -- habilitador de la ram
|
119 |
|
|
signal SEram_write : std_logic; -- habilitador de escritura
|
120 |
|
|
signal Sram_addr :std_logic_vector(addr_bits-1 downto 0):=(others=>'0') ;
|
121 |
|
|
signal Sdata_in_ram :std_logic_vector(bits-1 downto 0):=(others=>'0') ;
|
122 |
|
|
--*************************************************************************
|
123 |
|
|
-- seales para el detector de lrc bajo y alto
|
124 |
|
|
--*************************************************************************
|
125 |
|
|
signal SQ1 : std_logic_vector(7 downto 0):=(others => '0');
|
126 |
|
|
signal SQ2 : std_logic_vector(7 downto 0):=(others => '0');
|
127 |
|
|
|
128 |
|
|
signal SsQ1 : std_logic:='0';
|
129 |
|
|
signal SsQ2 : std_logic:='0';
|
130 |
|
|
signal SsQ3 : std_logic:='0';
|
131 |
|
|
signal Sstate_bin : std_logic:='0';
|
132 |
|
|
--*************************************************************************
|
133 |
|
|
-- seales componente ascii a binario
|
134 |
|
|
--*************************************************************************
|
135 |
|
|
signal Sascii : std_logic_vector(7 downto 0):=(others => '0');
|
136 |
|
|
signal Sbin : std_logic_vector(7 downto 0):=(others => '0');
|
137 |
|
|
signal SNnew_data :std_logic:='0';
|
138 |
|
|
|
139 |
|
|
--*************************************************************************
|
140 |
|
|
-- seales componente pondera
|
141 |
|
|
--*************************************************************************
|
142 |
|
|
signal Sbin_pond : std_logic_vector(7 downto 0):=(others => '0');
|
143 |
|
|
signal Sbin_ok_pond : std_logic:='0';
|
144 |
|
|
signal Strama_ok : std_logic:='0';
|
145 |
|
|
|
146 |
|
|
--*************************************************************************
|
147 |
|
|
-- seales componente calculador lrc
|
148 |
|
|
--*************************************************************************
|
149 |
|
|
|
150 |
|
|
signal Sdata_ram :std_logic_vector(bits-1 downto 0):=(others=>'0');
|
151 |
|
|
signal Slrc_ok :std_logic :='0';
|
152 |
|
|
|
153 |
|
|
begin
|
154 |
|
|
|
155 |
|
|
--*******************************************************************
|
156 |
|
|
-- INSTANCIACION COMPONENTE ASCII BINARIO
|
157 |
|
|
--*******************************************************************
|
158 |
|
|
ascii2bin: ascii_bin
|
159 |
|
|
port map(
|
160 |
|
|
clk => clk,
|
161 |
|
|
reset => reset,
|
162 |
|
|
new_data => new_data,
|
163 |
|
|
Nnew_data => SNnew_data,
|
164 |
|
|
ascii => Sascii,
|
165 |
|
|
bin => Sbin
|
166 |
|
|
);
|
167 |
|
|
|
168 |
|
|
--*******************************************************************
|
169 |
|
|
-- INSTANCIACION COMPONENTE PONDERA
|
170 |
|
|
--*******************************************************************
|
171 |
|
|
ponderacion: pondera_top
|
172 |
|
|
port map(
|
173 |
|
|
clk => clk,
|
174 |
|
|
reset => reset,
|
175 |
|
|
bin_HL => Sbin,
|
176 |
|
|
new_data => SNnew_data,
|
177 |
|
|
bin => Sdata_ram,
|
178 |
|
|
trama_ok => Strama_ok,
|
179 |
|
|
bin_ok => Sbin_ok_pond
|
180 |
|
|
);
|
181 |
|
|
--*******************************************************************
|
182 |
|
|
|
183 |
|
|
|
184 |
|
|
--*******************************************************************
|
185 |
|
|
-- INSTANCIACION COMPONENTE CALCULAR LRC
|
186 |
|
|
--*******************************************************************
|
187 |
|
|
cal_lrc: lrc
|
188 |
|
|
port map(
|
189 |
|
|
clk => clk,
|
190 |
|
|
reset => reset,
|
191 |
|
|
trama => Strama_ok,
|
192 |
|
|
dato_ok => Sbin_ok_pond,
|
193 |
|
|
dato => Sdata_ram,
|
194 |
|
|
lrc_ok => Slrc_ok
|
195 |
|
|
);
|
196 |
|
|
--*******************************************************************
|
197 |
|
|
|
198 |
|
|
|
199 |
|
|
--*******************************************************************
|
200 |
|
|
-- SINCRONIZMO DE LAS SALIDAS
|
201 |
|
|
--*******************************************************************
|
202 |
|
|
SYNC_PROC: process (clk)
|
203 |
|
|
begin
|
204 |
|
|
if (clk'event and clk = '1') then
|
205 |
|
|
if (reset = '1') then
|
206 |
|
|
state <= st1_det;
|
207 |
|
|
error <= '0';
|
208 |
|
|
else
|
209 |
|
|
state <= next_state;
|
210 |
|
|
error <= Serror;
|
211 |
|
|
-- assign other outputs to internal signals
|
212 |
|
|
end if;
|
213 |
|
|
end if;
|
214 |
|
|
end process;
|
215 |
|
|
|
216 |
|
|
--*******************************************************************
|
217 |
|
|
-- CODIFICACION ACCION EN LOS ESTADOS
|
218 |
|
|
--*******************************************************************
|
219 |
|
|
|
220 |
|
|
--MEALY State-Machine - Outputs based on state and inputs
|
221 |
|
|
OUTPUT_DECODE: process (state, new_data)
|
222 |
|
|
begin
|
223 |
|
|
--insert statements to decode internal output signals
|
224 |
|
|
--below is simple example
|
225 |
|
|
if (state = st2_dire_alto and new_data = '1') then
|
226 |
|
|
Sdire_alto <= data;
|
227 |
|
|
Sascii <= data; -- a convertir y LRC
|
228 |
|
|
end if;
|
229 |
|
|
|
230 |
|
|
if (state = st3_dire_bajo and new_data = '1') then
|
231 |
|
|
Sdire_bajo <= data;
|
232 |
|
|
Sascii <= data; -- a convertir y LRC
|
233 |
|
|
end if;
|
234 |
|
|
|
235 |
|
|
if (state = st4_comp and Sdire_alto = DIRE_LOCAL_ALTO and Sdire_bajo = DIRE_LOCAL_BAJO) then -- direccin del esclavo
|
236 |
|
|
Scomp <= '1';
|
237 |
|
|
else
|
238 |
|
|
Scomp <= '0';
|
239 |
|
|
end if;
|
240 |
|
|
|
241 |
|
|
if (state = st5_func_alto and new_data = '1') then
|
242 |
|
|
Sfunc_alto <= data;
|
243 |
|
|
Sascii <= data; -- a convertir y LRC luego: Sdata_in_ram <= data;
|
244 |
|
|
|
245 |
|
|
end if;
|
246 |
|
|
|
247 |
|
|
if (state = st6_func_bajo and new_data = '1') then
|
248 |
|
|
Sfunc_bajo <= data;
|
249 |
|
|
Sascii <= data; -- a convertir y LRC luego: Sdata_in_ram <= data;
|
250 |
|
|
end if;
|
251 |
|
|
|
252 |
|
|
if (state = st7_CR and data = "01000110") then --"." ascii --CR en ASCII
|
253 |
|
|
SCR <= data;
|
254 |
|
|
end if;
|
255 |
|
|
|
256 |
|
|
if (state = st8_dato_y_LRC_rec and new_data = '1') then
|
257 |
|
|
Sascii <= data; -- a convertir y LRC luego: Sdata_in_ram <= data;
|
258 |
|
|
end if;
|
259 |
|
|
|
260 |
|
|
if (state = st9_LF and data = "01000001") then --A ascii--LF en ASCII
|
261 |
|
|
SLF <= data;
|
262 |
|
|
Serror <= Slrc_ok;
|
263 |
|
|
end_det <= '1';
|
264 |
|
|
else
|
265 |
|
|
end_det <= '0';
|
266 |
|
|
end if;
|
267 |
|
|
|
268 |
|
|
end process;
|
269 |
|
|
|
270 |
|
|
--*******************************************************************
|
271 |
|
|
-- CONDICION DE LOS ESTADOS A SEGUIR
|
272 |
|
|
--*******************************************************************
|
273 |
|
|
|
274 |
|
|
NEXT_STATE_DECODE: process (state, new_data)
|
275 |
|
|
begin
|
276 |
|
|
--declare default state for next_state to avoid latches
|
277 |
|
|
next_state <= state; --default is to stay in current state
|
278 |
|
|
--insert statements to decode next_state
|
279 |
|
|
--below is a simple example
|
280 |
|
|
case (state) is
|
281 |
|
|
when st1_det =>
|
282 |
|
|
if new_data = '1' and data = "00111010" then -- : ASCII
|
283 |
|
|
next_state <= st2_dire_alto;
|
284 |
|
|
end if;
|
285 |
|
|
when st2_dire_alto =>
|
286 |
|
|
if new_data = '1' then
|
287 |
|
|
next_state <= st3_dire_bajo;
|
288 |
|
|
end if;
|
289 |
|
|
when st3_dire_bajo =>
|
290 |
|
|
if new_data = '1' then
|
291 |
|
|
next_state <= st4_comp;
|
292 |
|
|
end if;
|
293 |
|
|
when st4_comp =>
|
294 |
|
|
if Sdire_alto = DIRE_LOCAL_ALTO and Sdire_bajo = DIRE_LOCAL_BAJO then
|
295 |
|
|
next_state <= st5_func_alto;
|
296 |
|
|
else
|
297 |
|
|
next_state <= st1_det;
|
298 |
|
|
end if;
|
299 |
|
|
when st5_func_alto =>
|
300 |
|
|
if new_data = '1' then
|
301 |
|
|
next_state <= st6_func_bajo;
|
302 |
|
|
end if;
|
303 |
|
|
when st6_func_bajo =>
|
304 |
|
|
if new_data = '1' then
|
305 |
|
|
next_state <= st7_CR;
|
306 |
|
|
end if;
|
307 |
|
|
when st7_CR =>
|
308 |
|
|
if data = "00101110" then -- "." ascii --"00001010" then -- LF ASCII
|
309 |
|
|
next_state <= st9_LF;
|
310 |
|
|
else
|
311 |
|
|
next_state <= st8_dato_y_LRC_rec;
|
312 |
|
|
end if;
|
313 |
|
|
when st8_dato_y_LRC_rec =>
|
314 |
|
|
if new_data = '1' then
|
315 |
|
|
next_state <= st7_CR;
|
316 |
|
|
end if;
|
317 |
|
|
when st9_LF =>
|
318 |
|
|
if data = "01000001" then --A asci --"00001101" then -- CR ASCII
|
319 |
|
|
next_state <= st1_det;
|
320 |
|
|
end if;
|
321 |
|
|
when others =>
|
322 |
|
|
next_state <= st1_det;
|
323 |
|
|
end case;
|
324 |
|
|
end process;
|
325 |
|
|
|
326 |
|
|
--**************Escritura en bloque ram*****************************
|
327 |
|
|
SEram <= '1' when state = st8_dato_y_LRC_rec or state = st5_func_alto or state = st6_func_bajo else
|
328 |
|
|
'0';
|
329 |
|
|
ADDR <= Sram_addr;
|
330 |
|
|
data_ram <= Sdata_ram;
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
|
334 |
|
|
--**************Escritura en bloque ram*****************************
|
335 |
|
|
guardar_en_ram: process(clk,reset)
|
336 |
|
|
begin
|
337 |
|
|
if reset = '1' or state = st1_det then
|
338 |
|
|
Sram_addr <= (others=>'0');
|
339 |
|
|
elsif clk'event and clk = '1' then
|
340 |
|
|
if Sbin_ok_pond = '1' then
|
341 |
|
|
Sram_addr <= Sram_addr +1;
|
342 |
|
|
end if;
|
343 |
|
|
end if;
|
344 |
|
|
end process guardar_en_ram;
|
345 |
|
|
WE <= Sbin_ok_pond;
|
346 |
|
|
--*************************************************************************
|
347 |
|
|
|
348 |
|
|
|
349 |
|
|
Strama_ok <= '1' when state /= st1_det and state /= st9_LF and data /= "00111010" else
|
350 |
|
|
'0';
|
351 |
|
|
E <= Strama_ok;
|
352 |
|
|
|
353 |
|
|
process(clk, reset)
|
354 |
|
|
begin
|
355 |
|
|
if (reset = '1') then
|
356 |
|
|
SsQ1 <= '0';
|
357 |
|
|
SsQ2 <= '0';
|
358 |
|
|
SsQ3 <= '0';
|
359 |
|
|
elsif (clk'event and clk = '1') then
|
360 |
|
|
SsQ1 <= Sstate_bin;
|
361 |
|
|
SsQ2 <= SsQ1;
|
362 |
|
|
SsQ3 <= SsQ2;
|
363 |
|
|
end if;
|
364 |
|
|
end process;
|
365 |
|
|
Sstate_bin <= '1' when state = st9_LF else
|
366 |
|
|
'0';
|
367 |
|
|
end Behavioral;
|
368 |
|
|
|