Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
18 years ago

Running into infinite loop problems

I'm relatively new to Quartus and I'm trying to define a latch with three outputs and the following properties:

L M | Q(t+1) | R(t+1) | S(t+1)

------------------------------

0 0 | S(t) | Q(t) | R(t)

0 1 | 0 | 1 | 1

1 0 | 1 | 0 | 1

1 1 | Q(t) | R(t) | S(t)

So I wrote the following code to do it. Running a functional simulation gets me in an infinite loop. Running a timing simulation results in unanticipated resulting waveform. Perhaps my VHDL code is wrong?

LIBRARY ieee;

USE ieee.std_logic_1164.all;

ENTITY newlatch IS

PORT (L,M,Clk : IN STD_LOGIC;

Q,R,S : OUT STD_LOGIC);

END newlatch;

ARCHITECTURE Behavior OF newlatch IS

SIGNAL LM: STD_LOGIC_VECTOR(1 DOWNTO 0);

SIGNAL Q1: STD_LOGIC;

SIGNAL R1: STD_LOGIC;

SIGNAL S1: STD_LOGIC;

BEGIN

PROCESS(L,M)

BEGIN

LM<=L & M;

IF Clk = '1' THEN

IF LM = "00" THEN

Q1<=S1;

R1<=Q1;

S1<=R1;

ELSIF LM = "01" THEN

Q1<='0';

R1<='1';

S1<='1';

ELSIF LM= "10" THEN

Q1<='1';

R1<='0';

S1<='1';

END IF;

Q<=Q1;

R<=R1;

S<=S1;

END IF;

END PROCESS;

END Behavior;

7 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    In FPGA's defining a latch can give you infinite loop in simulation. Latches have combination feedback, in fpga's the timing is hard to define so it is recomended the you do I synchornous design and not I asynchornous design. Due to the nature of FPGA's I would start with a D-flip-flop. You could take a look Quartus II Handbook :recommended hdl coding styles (http://www.altera.com/literature/hb/qts/qts_qii51007.pdf) for more advice.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I haven't synthesized your design, but some quick recommendations:

    1) I don't see a clk'EVENT, so you're not synchronous(in the sense that nothing changes at the edge of the clock.)

    2) If you do make this edge sensitive, make two processes out of it, one based solely on (clk), and then one that is combinatorial.

    3) For some reason you may not want to use the registers. If so, please explain why and what you're trying to do. The registers are the basic building block of the FPGA, and are essentially free. If you try not to use them and want everything as latches, you're asking for trouble. (Maybe not trouble, but minimally an in-efficient use of the silicon provided.) There are cases this is warranted, but please explain.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I want it to be level sensitive so I don't want it to change at the clock edge. I only want things to be able to change when clock is high. I'm trying to design a latch that behaviorally does what I specify it does. However things aren't working out the way I want them to and I don't quite undersatand why

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I missed this when I first looked at the code, but I'm not sure what is supposed to happen when L=0 and M=0. I thought everything just held their values(like a latch does), but they hold someone else's value, in a roundabout way. Conceptually, a latch is an output that feeds back on itself and into a mux(the select line of the mux is the latch's gate). What you're describing is a cross-connect and I'm not surprised they start toggling.

    Ignoring the other states, in this condition you have three nodes where Q <= S <= R <= Q <= S <= R <= etc., i.e. something that will toggle unless S = Q = R.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    If you really do need an unclocked latch, refer to the coding styles in the Quartus handbook that rmorley pointed you to. The chapter includes information on how to code latches to get a hazard-free implementation and where to look in the Analysis & Synthesis compilation report to confirm that you did indeed get that kind of implementation. If a latch is not listed as free of timing hazards, then it can do strange things in simulation and in real hardware. It could have glitches, wind up at the wrong level, or even oscillate continuously.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I went ahead and synthesized your code. The "User-Specified and Inferred Latches" table in the compilation report says that the latches are free of timing hazards, so at least the latch synthesis was OK. It's still preferred to use clocked registers instead of latches whenever possible as previous posters said.