| 1 |
134 |
jguarin200 |
--! @file sm.vhd
|
| 2 |
|
|
--! @brief Maquina de Estados. Controla la operación interna y genera los mecanismos de sincronización con el exterior (interrupciones).
|
| 3 |
|
|
--! @author Julián Andrés Guarín Reyes
|
| 4 |
|
|
--------------------------------------------------------------
|
| 5 |
|
|
-- RAYTRAC
|
| 6 |
|
|
-- Author Julian Andres Guarin
|
| 7 |
|
|
-- sm.vhd
|
| 8 |
|
|
-- This file is part of raytrac.
|
| 9 |
|
|
--
|
| 10 |
|
|
-- raytrac is free software: you can redistribute it and/or modify
|
| 11 |
|
|
-- it under the terms of the GNU General Public License as published by
|
| 12 |
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
| 13 |
|
|
-- (at your option) any later version.
|
| 14 |
|
|
--
|
| 15 |
|
|
-- raytrac is distributed in the hope that it will be useful,
|
| 16 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 17 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 18 |
|
|
-- GNU General Public License for more details.
|
| 19 |
|
|
--
|
| 20 |
|
|
-- You should have received a copy of the GNU General Public License
|
| 21 |
|
|
-- along with raytrac. If not, see <http://www.gnu.org/licenses/>.
|
| 22 |
|
|
|
| 23 |
|
|
|
| 24 |
|
|
library ieee;
|
| 25 |
|
|
use ieee.std_logic_1164.all;
|
| 26 |
139 |
jguarin200 |
use ieee.std_logic_unsigned.all;
|
| 27 |
134 |
jguarin200 |
|
| 28 |
|
|
|
| 29 |
|
|
|
| 30 |
|
|
entity sm is
|
| 31 |
142 |
jguarin200 |
generic (
|
| 32 |
|
|
width : integer := 32;
|
| 33 |
|
|
widthadmemblock : integer := 9
|
| 34 |
|
|
--!external_readable_widthad :
|
| 35 |
145 |
jguarin200 |
);
|
| 36 |
134 |
jguarin200 |
port (
|
| 37 |
|
|
|
| 38 |
145 |
jguarin200 |
--! Señales normales de secuencia.
|
| 39 |
|
|
clk,rst: in std_logic;
|
| 40 |
142 |
jguarin200 |
|
| 41 |
145 |
jguarin200 |
--! ENTRADAS DE CONTROL DE SINCRONIZACION.
|
| 42 |
|
|
--! Señales de sincronización de recursos.
|
| 43 |
|
|
--! Las siguientes entradas tienen relevancia y son sensibles en los estados EXECUTE_INSTRUCTION y VERMEER_EXECUTE.
|
| 44 |
142 |
jguarin200 |
|
| 45 |
145 |
jguarin200 |
|
| 46 |
|
|
|
| 47 |
|
|
--! ENTRADAS
|
| 48 |
|
|
|
| 49 |
|
|
|
| 50 |
|
|
|
| 51 |
142 |
jguarin200 |
--! Instruction Q, instruction.
|
| 52 |
|
|
instrQq:in std_logic_vector(width-1 downto 0);
|
| 53 |
145 |
jguarin200 |
instrQ_empty:in std_logic;
|
| 54 |
142 |
jguarin200 |
|
| 55 |
145 |
jguarin200 |
|
| 56 |
|
|
adda,addb:out std_logic_vector (widthadmemblock-1 downto 0);
|
| 57 |
|
|
sync_chain_0,instrRdAckd:out std_logic;
|
| 58 |
142 |
jguarin200 |
|
| 59 |
145 |
jguarin200 |
full_r: in std_logic; --! Indica que la cola de resultados no puede aceptar mas de 32 elementos.
|
| 60 |
|
|
|
| 61 |
142 |
jguarin200 |
|
| 62 |
|
|
|
| 63 |
145 |
jguarin200 |
|
| 64 |
142 |
jguarin200 |
--! DataPath Control uca code.
|
| 65 |
145 |
jguarin200 |
dpc_uca : out std_logic_vector (2 downto 0)
|
| 66 |
142 |
jguarin200 |
|
| 67 |
|
|
|
| 68 |
134 |
jguarin200 |
);
|
| 69 |
|
|
end entity;
|
| 70 |
139 |
jguarin200 |
|
| 71 |
|
|
architecture sm_arch of sm is
|
| 72 |
|
|
|
| 73 |
145 |
jguarin200 |
type macState is (LOAD_INSTRUCTION,FLUSH_ARITH_PIPELINE,EXECUTE_INSTRUCTION);
|
| 74 |
|
|
--! LOAD_INSTRUCTION: Estado en el que se espera que en la cola de instrucciones haya una instrucción para ejecutar.
|
| 75 |
|
|
--! EXECUTE_INSTRUCTION: Estado en el que se ejecuta la instrucción de la cola de instrucciones.
|
| 76 |
|
|
--! FLUSH_ARITH_PIPELINE: Estado en el que se espera un número específico de ciclos de reloj, para que se desocupe el pipeline aritmético.
|
| 77 |
|
|
|
| 78 |
139 |
jguarin200 |
signal state : macState;
|
| 79 |
|
|
constant rstMasterValue : std_logic:='0';
|
| 80 |
|
|
|
| 81 |
142 |
jguarin200 |
component customCounter
|
| 82 |
|
|
generic (
|
| 83 |
145 |
jguarin200 |
EOBFLAG : string ;
|
| 84 |
|
|
ZEROFLAG : string ;
|
| 85 |
|
|
BACKWARDS : string ;
|
| 86 |
|
|
EQUALFLAG : string ;
|
| 87 |
|
|
subwidth : integer;
|
| 88 |
|
|
width : integer
|
| 89 |
142 |
jguarin200 |
|
| 90 |
|
|
);
|
| 91 |
|
|
port (
|
| 92 |
|
|
clk,rst,go,set : in std_logic;
|
| 93 |
145 |
jguarin200 |
setValue,cmpBlockValue : in std_Logic_vector(width-1 downto subwidth);
|
| 94 |
|
|
zero_flag,eob_flag,eq_flag : out std_logic;
|
| 95 |
142 |
jguarin200 |
count : out std_logic_vector(width-1 downto 0)
|
| 96 |
145 |
jguarin200 |
);
|
| 97 |
|
|
end component;
|
| 98 |
139 |
jguarin200 |
|
| 99 |
145 |
jguarin200 |
signal s_instr_uca: std_logic_vector(2 downto 0);
|
| 100 |
|
|
signal s_dpc_uca: std_logic_vector(2 downto 0);
|
| 101 |
|
|
signal s_block_start_a: std_logic_vector(4 downto 0);
|
| 102 |
|
|
signal s_block_start_b: std_logic_vector(4 downto 0);
|
| 103 |
|
|
signal s_block_end_a: std_logic_vector(4 downto 0);
|
| 104 |
|
|
signal s_block_end_b: std_logic_vector(4 downto 0);
|
| 105 |
|
|
signal s_combinatory: std_logic;
|
| 106 |
|
|
signal s_delay_field: std_logic_vector(7 downto 0);
|
| 107 |
|
|
signal s_set_b: std_logic; --! Señal para colocar un valor arbitrario en el contador B.
|
| 108 |
|
|
signal s_set_a: std_logic;
|
| 109 |
|
|
signal s_set_dly: std_logic;
|
| 110 |
|
|
signal s_go_b: std_logic; --! Salida para controlar la pausa(0) o marcha(1) del contador de direcciones del operando B/D.
|
| 111 |
|
|
signal s_go_a: std_logic; --! Salida para controlar la pausa(0) o marcha(1) del contador de direcciones del operando A/C.
|
| 112 |
|
|
signal s_go_delay: std_logic; --! Salida para controlar la pausa(0) o marcha(1) del contador de delay, para el flush del pipeline aritmético.
|
| 113 |
|
|
signal s_zeroFlag_delay:std_logic; --! Bandera de cero del contador delay.
|
| 114 |
|
|
signal s_eq_b,s_eq_a: std_logic; --! Indica cuando se está leyendo el último bloque de memoria con operandos de entrada de a y de b respectivamente.
|
| 115 |
|
|
signal s_eb_b,s_eb_a: std_logic; --! Indica que se está leyendo en memoria el último operando del bloque actual, b o a, respectivamente.
|
| 116 |
|
|
|
| 117 |
134 |
jguarin200 |
begin
|
| 118 |
145 |
jguarin200 |
--! Código UCA, pero en la etapa DPC: La diferencia es que UCA en la etapa DPC, decodifica el datapath dentro del pipeline aritmético.
|
| 119 |
|
|
dpc_uca <= s_dpc_uca;
|
| 120 |
142 |
jguarin200 |
|
| 121 |
145 |
jguarin200 |
|
| 122 |
|
|
--! Bloques asignados en la instrucci´øn
|
| 123 |
142 |
jguarin200 |
s_block_start_a <= instrQq(width-4 downto width-8);
|
| 124 |
|
|
s_block_end_a <= instrQq(width-9 downto width-13);
|
| 125 |
139 |
jguarin200 |
|
| 126 |
145 |
jguarin200 |
s_block_start_b <= instrQq(width-14 downto width-18);
|
| 127 |
|
|
s_block_end_b <= instrQq(width-19 downto width-23);
|
| 128 |
|
|
|
| 129 |
|
|
--! Campo que define si la instrucción es combinatoria
|
| 130 |
|
|
s_combinatory <= instrQq(width-24);
|
| 131 |
|
|
|
| 132 |
|
|
--! Campo que define cuantos clocks debe esperar el sistema, despues de que se ejecuta una instrucción, para que el pipeline aritmético quede vacio.
|
| 133 |
|
|
s_delay_field <= instrQq(width-25 downto width-32);
|
| 134 |
|
|
|
| 135 |
|
|
--! UCA code, código con la instrucción a ejecutar.
|
| 136 |
|
|
s_instr_uca <= instrQq(31 downto 29);
|
| 137 |
|
|
|
| 138 |
142 |
jguarin200 |
--! Address Counters
|
| 139 |
|
|
counterA:customCounter
|
| 140 |
145 |
jguarin200 |
generic map ("YES","NO","NO","YES",4,9)
|
| 141 |
|
|
port map (clk,rst,s_go_a,s_set_a,s_block_start_a,s_block_end_a,open,s_eb_a,s_eq_a,adda);
|
| 142 |
142 |
jguarin200 |
counterB:customCounter
|
| 143 |
145 |
jguarin200 |
generic map ("YES","NO","NO","YES",4,9)
|
| 144 |
|
|
port map (clk,rst,s_go_b,s_set_b,s_block_start_b,s_block_end_b,open,s_eb_b,s_eq_b,addb);
|
| 145 |
|
|
counterDly:customCounter
|
| 146 |
|
|
generic map("NO","YES","YES","NO",0,5)
|
| 147 |
|
|
port map (clk,rst,s_go_delay,s_set_dly,s_delay_field(4 downto 0),"00000",s_zeroFlag_delay,open,open,open);
|
| 148 |
139 |
jguarin200 |
|
| 149 |
|
|
|
| 150 |
145 |
jguarin200 |
sm_comb:
|
| 151 |
|
|
process (state, full_r,s_eb_b,s_combinatory,s_zeroFlag_delay,s_eq_b,s_eb_a,s_eq_a,instrQ_empty)
|
| 152 |
|
|
begin
|
| 153 |
|
|
--!Señal de play/pause del contador de direcciones para el parámetro B/D.
|
| 154 |
|
|
s_go_b <= not(full_r and s_eb_b);
|
| 155 |
139 |
jguarin200 |
|
| 156 |
145 |
jguarin200 |
--!Señal de play/pause del contador de direcciones para el parámetro A/C.
|
| 157 |
|
|
if s_combinatory='0' then
|
| 158 |
|
|
s_go_a <= not(full_r and s_eb_b);
|
| 159 |
|
|
|
| 160 |
|
|
else
|
| 161 |
|
|
s_go_a <= not(full_r) and s_eb_b and s_eq_b;
|
| 162 |
|
|
end if;
|
| 163 |
|
|
|
| 164 |
|
|
--!Señal de play/pause del contador del arithmetic pipeline flush counter.
|
| 165 |
|
|
s_go_delay <= not(s_zeroFlag_delay);
|
| 166 |
|
|
|
| 167 |
|
|
--! Si estamos en el final de la instrucción, "descargamos" esta de la máquina de estados con acknowledge read.
|
| 168 |
|
|
if s_eb_b='1' and s_eq_b='1' and s_eb_a='1' and s_eq_a='1' and state=EXECUTE_INSTRUCTION then
|
| 169 |
|
|
instrRdAckd <= '1';
|
| 170 |
|
|
else
|
| 171 |
|
|
instrRdAckd <= '0';
|
| 172 |
|
|
end if;
|
| 173 |
|
|
|
| 174 |
|
|
if (s_eb_a='1' and s_eq_a='1') or state=LOAD_INSTRUCTION or state=FLUSH_ARITH_PIPELINE then
|
| 175 |
|
|
s_set_a <= '1';
|
| 176 |
|
|
else
|
| 177 |
|
|
s_set_a <= '0';
|
| 178 |
|
|
end if;
|
| 179 |
|
|
|
| 180 |
|
|
|
| 181 |
|
|
|
| 182 |
|
|
if (s_eb_b='1' and s_eq_b='1') or state=LOAD_INSTRUCTION or state=FLUSH_ARITH_PIPELINE then
|
| 183 |
|
|
s_set_b <= '1';
|
| 184 |
|
|
else
|
| 185 |
|
|
s_set_b <= '0';
|
| 186 |
|
|
end if;
|
| 187 |
|
|
|
| 188 |
|
|
end process;
|
| 189 |
|
|
|
| 190 |
139 |
jguarin200 |
sm_proc:
|
| 191 |
145 |
jguarin200 |
process (clk,rst,state, full_r,s_eb_b,s_combinatory,s_zeroFlag_delay,s_eq_b,s_eb_a,s_eq_a,instrQ_empty)
|
| 192 |
139 |
jguarin200 |
begin
|
| 193 |
145 |
jguarin200 |
|
| 194 |
139 |
jguarin200 |
if rst=rstMasterValue then
|
| 195 |
145 |
jguarin200 |
|
| 196 |
|
|
state <= LOAD_INSTRUCTION;
|
| 197 |
|
|
s_set_dly <= '1';
|
| 198 |
|
|
sync_chain_0 <= '0';
|
| 199 |
|
|
s_dpc_uca <= (others => '0');
|
| 200 |
|
|
|
| 201 |
|
|
|
| 202 |
142 |
jguarin200 |
elsif clk='1' and clk'event then
|
| 203 |
139 |
jguarin200 |
|
| 204 |
|
|
case state is
|
| 205 |
142 |
jguarin200 |
|
| 206 |
145 |
jguarin200 |
--! Cargar la siguiente instrucción.
|
| 207 |
|
|
when LOAD_INSTRUCTION =>
|
| 208 |
|
|
|
| 209 |
|
|
if instrQ_empty='0' and full_r='0' then
|
| 210 |
139 |
jguarin200 |
|
| 211 |
145 |
jguarin200 |
--! Siguiente estado: Ejecutar la instrucción.
|
| 212 |
|
|
state <= EXECUTE_INSTRUCTION;
|
| 213 |
|
|
|
| 214 |
|
|
--! Asignar el código UCA para que comience la decodificación.
|
| 215 |
|
|
s_dpc_uca <= s_instr_uca;
|
| 216 |
|
|
|
| 217 |
|
|
--! Validar el siguiente dato dentro del pipeline aritmético.
|
| 218 |
|
|
sync_chain_0 <= '1';
|
| 219 |
|
|
|
| 220 |
|
|
--! En el estado EXECUTE, el valor del contador de delay se debe mantener fijo, y puesto en el valor de delay que contiene la instruccion.
|
| 221 |
|
|
s_set_dly <= '1';
|
| 222 |
|
|
|
| 223 |
|
|
|
| 224 |
|
|
|
| 225 |
|
|
end if;
|
| 226 |
|
|
|
| 227 |
|
|
--! Ejecución de la instruccion
|
| 228 |
|
|
when EXECUTE_INSTRUCTION =>
|
| 229 |
|
|
|
| 230 |
|
|
|
| 231 |
|
|
if s_eb_b='1'and s_eq_b='1' and s_eb_a='1' and s_eq_a='1' then --! Revisar si es el fin de la instruccion
|
| 232 |
|
|
|
| 233 |
|
|
--!Ya no ingresaran mas datos al pipeline aritmético, invalidar.
|
| 234 |
|
|
sync_chain_0 <= '0';
|
| 235 |
|
|
|
| 236 |
|
|
if s_zeroFlag_delay='1' then
|
| 237 |
|
|
|
| 238 |
|
|
state <= LOAD_INSTRUCTION;
|
| 239 |
|
|
s_set_dly <= '1';
|
| 240 |
142 |
jguarin200 |
|
| 241 |
145 |
jguarin200 |
|
| 242 |
|
|
else
|
| 243 |
|
|
|
| 244 |
|
|
state <= FLUSH_ARITH_PIPELINE;
|
| 245 |
|
|
s_set_dly <= '0';
|
| 246 |
142 |
jguarin200 |
|
| 247 |
145 |
jguarin200 |
end if;
|
| 248 |
|
|
|
| 249 |
|
|
--! Invalidar/validar datos dentro del pipeline aritmético.
|
| 250 |
|
|
elsif s_eb_b='1' and full_r='1' then
|
| 251 |
|
|
--! Invalidar el siguiente dato dentro del pipeline aritmético.
|
| 252 |
|
|
sync_chain_0 <= '0';
|
| 253 |
142 |
jguarin200 |
else
|
| 254 |
145 |
jguarin200 |
sync_chain_0 <= '1';
|
| 255 |
|
|
end if;
|
| 256 |
|
|
|
| 257 |
|
|
--! Ejecución de la instrucción
|
| 258 |
|
|
when FLUSH_ARITH_PIPELINE =>
|
| 259 |
|
|
--! Este estado permanece así hasta que, haya una instrucción
|
| 260 |
|
|
if s_zeroFlag_delay='1' then
|
| 261 |
142 |
jguarin200 |
|
| 262 |
145 |
jguarin200 |
state <= LOAD_INSTRUCTION;
|
| 263 |
|
|
s_set_dly <= '1';
|
| 264 |
|
|
|
| 265 |
|
|
|
| 266 |
|
|
end if;
|
| 267 |
|
|
|
| 268 |
|
|
when others => null;
|
| 269 |
|
|
|
| 270 |
139 |
jguarin200 |
end case;
|
| 271 |
|
|
end if;
|
| 272 |
|
|
end process;
|
| 273 |
142 |
jguarin200 |
|
| 274 |
134 |
jguarin200 |
end architecture;
|