VHDL - Latches, Flip-Flops and Resets
In questa pagina 4
This note describes in VHDL the basic sequential elements of Sequential Systems - State, Latches and Flip-FlopsA sequential circuit has outputs that depend on the inputs and on the state held in memory elements; it is a combinational network with flip-flops in a feedback loop, where present state is the flip-flop outputs and next state is their inputs. Synchronous circuits update only on the clock edge. The SR latch (NOR) sets, resets, holds, and forbids S=R=1; the D latch is transparent while C=1; the edge-triggered D flip-flop is two D latches in cascade (master-slave) and samples D only on the active clock edge, with Q(t+1)=D(t).Sequential Systems - State, Latches and Flip-Flops →. Recall (VHDL - Concurrent Statements, Processes and TestbenchesConcurrent VHDL statements are the plain signal assignment, when-else (priority logic, conditions evaluated in order) and with-select (parallel logic, every case covered once, others required). A process is a concurrent statement with a sequential body (if, case, loop) that wakes up when a signal in its sensitivity list changes; for combinational logic the list holds all inputs; signals are updated when the process suspends and the last assignment wins. An if without else or an uncovered case creates unwanted memory. A testbench has an empty entity, the instantiated DUT and a stimulus process with wait statements.VHDL - Concurrent Statements, Processes and Testbenches →) that inside a process statements are sequential and that a signal not assigned in all branches keeps its value: this is exactly memory.
SR latch with enable
Dataflow, with NAND gates (active-high inputs and control ):
library IEEE;
use IEEE.std_logic_1164.all;
entity SRlatch is
port ( S, R, C : in std_logic;
Q, Q_n : out std_logic );
end SRlatch;
architecture dataflow of SRlatch is
signal S_n, R_n : std_logic;
begin
S_n <= S nand C;
R_n <= R nand C;
Q <= S_n nand Q_n;
Q_n <= R_n nand Q; -- feedback: Q and Q_n drive each other
end dataflow;The feedback is in the code: both outputs also appear on the right-hand side. Reading an out port is allowed from VHDL-2008; in earlier versions internal signals are assigned to the outputs.
Behavioral, with a case on the pair (condition as enable, no else):
architecture behavioral of SRlatch is
signal SR : std_logic_vector(1 downto 0);
begin
SR <= S & R;
process (SR, C)
begin
if C = '1' then
case SR is
when "00" => null; -- hold: no assignment
when "01" => Q <= '0'; Q_n <= '1'; -- reset
when "10" => Q <= '1'; Q_n <= '0'; -- set
when "11" => Q <= '1'; Q_n <= '1'; -- forbidden
when others => Q <= 'X'; Q_n <= 'X';
end case;
end if; -- C = '0': no assignment, memory
end process;
end behavioral;null and the if without else leave the outputs unassigned: they keep their previous value, i.e. the latch holds its state.
D latch
entity Dlatch is
port ( D, C : in std_logic;
Q, Q_n : out std_logic );
end Dlatch;
architecture arch of Dlatch is
begin
process (D, C)
begin
if C = '1' then -- no else: for C = '0' the outputs keep their value
Q <= D;
Q_n <= not D;
end if;
end process;
end arch;Property. The sensitivity list contains both and : with every change of reactivates the process and reaches the output (transparency).
Positive-edge-triggered D flip-flop
library IEEE;
use IEEE.std_logic_1164.all;
entity dff is
port ( CLK, D : in std_logic;
Q : out std_logic );
end dff;
architecture simple of dff is
begin
process (CLK)
begin
if CLK'event and CLK = '1' then -- rising edge
Q <= D;
end if;
end process;
end simple;Definition (rising edge in VHDL).
CLK'eventis true whenCLKhas just changed value;CLK'event and CLK = '1'identifies the rising edge (with'0'the flip-flop would be negative-edge-triggered). In modern VHDLrising_edge(CLK)is often written. The process is sensitive only toCLK: is not in the list, because a change of alone must do nothing.
Asynchronous and synchronous reset
Reset brings the flip-flop to 0 and gives a known initial state.
Definition (asynchronous reset). Acts immediately, at any time, even without a clock edge.
RSTis in the sensitivity list and tested first:architecture async_reset of dff is begin process (RST, CLK) begin if RST = '1' then -- has priority over the clock Q <= '0'; elsif CLK'event and CLK = '1' then Q <= D; end if; end process; end async_reset;(with
RSTadded to the entity:port (CLK, RST, D : in std_logic; Q : out std_logic)).
Definition (synchronous reset). Synchronised with the clock: it acts only on the active edge and needs a clock edge to be effective. The list contains only
CLKandRSTis tested inside the edge branch:architecture sync_reset of dff is begin process (CLK) begin if CLK'event and CLK = '1' then if RST = '1' then Q <= '0'; else Q <= D; end if; end if; end process; end sync_reset;
Example. With asynchronous reset, RST='1' makes Q='0' immediately. With synchronous reset, Q does not change between clock edges; at the next edge it becomes '0'.
How to recognise them from code (frequent quiz):
| code | circuit |
|---|---|
process(CLK) + if (CLK'event and CLK='1') + inside if (RST='1') |
positive-edge FF, synchronous reset |
process(RST, CLK) + if RST='1' ... elsif (CLK'event and CLK='1') |
positive-edge FF, asynchronous reset |
Registers and a clocked testbench
A register with load enable and an asynchronous clear is in Shift Registers and CountersA shift register is a chain of D flip-flops, each output feeding the next input, so bits move one position per clock edge; it has serial input and output, optional parallel load (Shift has priority over Load) and a bidirectional version with a 4-to-1 MUX per stage (S1S0: 00 hold, 01 shift left, 10 shift right, 11 load). Uses: serial/parallel conversion, serial interfaces, delay lines, pseudo-random generators. Counters step through a sequence of states: the ripple (asynchronous) counter clocks each flip-flop with the previous output and accumulates delay, the synchronous counter uses one clock for all flip-flops and is faster, with EN and CO for cascading.Shift Registers and Counters →. The testbench of a synchronous circuit has two processes (VHDL - Finite State Machines and Sequential TestbenchesA finite state machine is described in behavioral VHDL with an enumerated type for the states and three processes: the state register (reset and state <= next_state on the clock edge), the next state as a combinational function of state and input (case state, with default next_state <= state), and the output as a combinational function of the state alone (Moore) or of state and input (Mealy). The testbench has a clock process and an input process (initial reset, then a test sequence that visits all states).VHDL - Finite State Machines and Sequential Testbenches →): one generating the clock and one applying inputs away from the active edge (for PET devices, on the falling edge), as discussed in Timing of Sequential Systems - Setup, Hold and Maximum Clock FrequencyA flip-flop stores the right value only if its input is stable for the setup time before and the hold time after the clock edge, and its output changes after the propagation delay. In a synchronous circuit the clock period must satisfy T >= t_pd,FF + t_COMB + t_s,FF + t_slack, which fixes the maximum frequency; slowing the clock cures setup violations but not hold violations, which require t_pd,FF,min + t_COMB,min >= t_h. Clock skew modifies both constraints.Timing of Sequential Systems - Setup, Hold and Maximum Clock Frequency →.
Versione ripasso
- Memory in VHDL: a process where some branch leaves a signal unassigned.
- SR latch: dataflow with NANDs and feedback (
S_n <= S nand C,Q <= S_n nand Q_n,Q_n <= R_n nand Q); behavioral withif C='1',case SR,"00" => null,"01"reset,"10"set,"11"forbidden. - D latch:
process (D, C),if C='1' then Q <= D; Q_n <= not D;noelse; with transparent. - PET D FF:
process (CLK),if CLK'event and CLK='1' then Q <= D;( not in the list);'0'gives NET;rising_edge(CLK)in modern VHDL. - Asynchronous reset:
process (RST, CLK),if RST='1' then Q <= '0'; elsif CLK'event and CLK='1' then Q <= D;acts at once, priority over clock. - Synchronous reset:
process (CLK),if CLK'event and CLK='1' then if RST='1' then Q <= '0'; else Q <= D;needs an edge. - Recognising:
RSTin the list and tested first means asynchronous; tested inside the edge branch means synchronous. - Testbench: clock process plus input process, inputs applied on the falling edge.