VHDL Programs
VHDL Programs
VHDL Programs
Solution 2:
------------------------------------------------
ENTITY parity_generator IS
GENERIC (n: INTEGER := 7);
PORT (input: IN BIT_VECTOR (n-1 DOWNTO 0);
output: OUT BIT_VECTOR (n DOWNTO 0));
END parity_generator;
------------------------------------------------
ARCHITECTURE parity OF parity_generator IS
BEGIN
PROCESS (input)
VARIABLE temp: BIT;
BEGIN
temp := '0';
FOR i IN input'RANGE LOOP
temp := temp XOR input(i);
END LOOP;
output <= (temp & input);
END PROCESS;
END parity;
------------------------------------------------
END IF;
----BCD to SSD conversion:--------
CASE temp1 IS
WHEN 0 => digit1 <= ”1111110”; --7E
WHEN 1 => digit1 <= ”0110000”; --30
WHEN 2 => digit1 <= ”1101101”; --6D
WHEN 3 => digit1 <= ”1111001”; --79
WHEN 4 => digit1 <= ”0110011”; --33
WHEN 5 => digit1 <= ”1011011”; --5B
WHEN 6 => digit1 <= ”1011111”; --5F
WHEN 7 => digit1 <= ”1110000”; --70
WHEN 8 => digit1 <= ”1111111”; --7F
WHEN 9 => digit1 <= ”1111011”; --7B
WHEN OTHERS => NULL;
END CASE;
CASE temp2 IS
WHEN 0 => digit2 <= ”1111110”; --7E
WHEN 1 => digit2 <= ”0110000”; --30
WHEN 2 => digit2 <= ”1101101”; --6D
WHEN 3 => digit2 <= ”1111001”; --79
WHEN 4 => digit2 <= ”0110011”; --33
WHEN 5 => digit2 <= ”1011011”; --5B
WHEN 6 => digit2 <= ”1011111”; --5F
WHEN 7 => digit2 <= ”1110000”; --70
WHEN 8 => digit2 <= ”1111111”; --7F
WHEN 9 => digit2 <= ”1111011”; --7B
WHEN OTHERS => NULL;
END CASE;
END PROCESS;
END counter;
-----------------------------------------------------------
IF (stby='1') THEN
pr_state <= YY;
count := 0;
ELSIF (clk'EVENT AND clk='1') THEN
count := count + 1;
IF (count=time) THEN
pr_state <= nx_state;
count := 0;
END IF;
END IF;
END PROCESS;
--------Upper section of state machine:----
PROCESS (pr_state, test)
BEGIN
CASE pr_state IS
WHEN RG =>
r1<='1'; r2<='0'; y1<='0'; y2<='0'; g1<='0'; g2<='1';
nx_state <= RY;
IF (test='0') THEN time <= timeRG;
ELSE time <= timeTEST;
END IF;
WHEN RY =>
r1<='1'; r2<='0'; y1<='0'; y2<='1'; g1<='0'; g2<='0';
nx_state <= GR;
IF (test='0') THEN time <= timeRY;
ELSE time <= timeTEST;
END IF;
WHEN GR =>
r1<='0'; r2<='1'; y1<='0'; y2<='0'; g1<='1'; g2<='0';
nx_state <= YR;
IF (test='0') THEN time <= timeGR;
ELSE time <= timeTEST;
END IF;
WHEN YR =>
r1<='0'; r2<='1'; y1<='1'; y2<='0'; g1<='0'; g2<='0';
nx_state <= RG;
IF (test='0') THEN time <= timeYR;
ELSE time <= timeTEST;
END IF;
WHEN YY =>
r1<='0'; r2<='0'; y1<='1'; y2<='1'; g1<='0'; g2<='0';
nx_state <= RY;
END CASE;
END PROCESS;
END behavior;
--------------------------------------------------------------------
------------------------------------------
a ab b bc c cd
time1 time2 time1 time2 time1
time2
time2
fa f ef e de d
time1 time2 time1 time2 time1
Circuit Design with VHDL, Volnei A. Pedroni, MIT Press, 2004
WHEN e =>
dout <= "0000100"; --Decimal 4
flip<='1';
next_state <= ef;
WHEN ef =>
dout <= "0000110"; --Decimal 6
flip<='0';
next_state <= f;
WHEN f =>
dout <= "0000010"; --Decimal 2
flip<='1';
next_state <= fa;
WHEN fa =>
dout <= "1000010"; --Decimal 66
flip<='0';
next_state <= a;
END CASE;
END PROCESS;
END fsm;
-----------------------------------------------------------
Solution 2 (more generic and with the proper function for stop):
-------------------------------------------------------------------------
ENTITY ssd_game IS
GENERIC (fclk: INTEGER := 50000; --clock frequency in kHz
delay1: INTEGER := 250; --desired long delay in ms
delay2: INTEGER := 40); --desired short delay in ms
PORT (clk, stop: IN BIT;
dout: OUT BIT_VECTOR(6 DOWNTO 0));
END ssd_game;
-------------------------------------------------------------------------
ARCHITECTURE fsm OF ssd_game IS
CONSTANT time1: INTEGER := fclk*delay1; --use 4 for initial simulations
CONSTANT time2: INTEGER := fclk*delay2; --use 2 for initial simulations
TYPE states IS (a, ab, b, bc, c, cd, d, de, e, ef, f, fa);
SIGNAL present_state, next_state: states;
--Optional:
--ATTRIBUTE enum_encoding: STRING;
--ATTRIBUTE enum_encoding OF states: TYPE IS "sequential";
SIGNAL delay: INTEGER RANGE 0 TO time1;
BEGIN
-------Lower section of FSM:---------------
PROCESS (clk, stop)
VARIABLE count: INTEGER RANGE 0 TO time1;
BEGIN
IF (clk'EVENT AND clk='1') THEN
IF (stop='0') THEN
count := count + 1;
IF (count=delay) THEN
count := 0;
present_state <= next_state;
END IF;
END IF;
END IF;
END PROCESS;
-------Upper section of FSM:---------------
PROCESS (present_state)
BEGIN
CASE present_state IS
WHEN a =>
dout <= "1000000"; --Decimal 64
delay <= time1;
next_state <= ab;
WHEN ab =>
dout <= "1100000"; --Decimal 96
Circuit Design with VHDL, Volnei A. Pedroni, MIT Press, 2004