RTUEE / EC / EEEYr 2019 · Sem 72019

Q6VHDL

Question

16 marks

3. Write a VHDL code for the following: (a) JK flip flop (b) D flip flop using JK flip flop (c) T flip flop (d) 3-input XOR gate. [16]

Answer

(a) JK Flip Flop

vhdl
process(clk)
begin
    if rising_edge(clk) then
        case (J & K) is
            when "00" => Q <= Q;
            when "01" => Q <= '0';
            when "10" => Q <= '1';
            when "11" => Q <= not Q;
            when others => Q <= 'X';
        end case;
    end if;
end process;

As detailed elsewhere in this examination, the JK flip-flop's four input combinations produce, respectively: no change (J=K=0), reset to 0 (J=0,K=1), set to 1 (J=1,K=0), and toggle (J=K=1), directly implemented here via a case statement inside a clocked process, giving an edge-triggered JK flip-flop.

(b) D Flip Flop Using JK Flip Flop

vhdl
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity d_from_jk is
    port (clk, D : in STD_LOGIC; Q : out STD_LOGIC);
end d_from_jk;

architecture structural of d_from_jk is
    signal J, K : STD_LOGIC;
    signal Q_int : STD_LOGIC := '0';
begin
    J <= D;
    K <= not D;
    process(clk)
    begin
        if rising_edge(clk) then
            case (J & K) is
                when "00" => Q_int <= Q_int;
                when "01" => Q_int <= '0';
                when "10" => Q_int <= '1';
                when "11" => Q_int <= not Q_int;
                when others => Q_int <= 'X';
            end case;
        end if;
    end process;
    Q <= Q_int;
end structural;

A D flip-flop can be derived from a JK flip-flop by tying J=D and K=D' (the complement of D): when D=0, this gives J=0,K=1 (forcing Q to 0, matching the desired D flip-flop behavior of Q following D=0); when D=1, this gives J=1,K=0 (forcing Q to 1, matching D=1) - since J and K are always complementary in this configuration, the JK flip-flop's 'hold' (J=K=0) and 'toggle' (J=K=1) states are never actually reached, leaving only the 'set' and 'reset' behaviors active, exactly reproducing the simple D flip-flop's Q(next)=D behavior using the more general JK flip-flop as the underlying building block.

(c) T Flip Flop

vhdl
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity t_ff is
    port (clk, T : in STD_LOGIC; Q : out STD_LOGIC);
end t_ff;

architecture behavioral of t_ff is
    signal Q_int : STD_LOGIC := '0';
begin
    process(clk)
    begin
        if rising_edge(clk) then
            if T = '1' then
                Q_int <= not Q_int;
            end if;
        end if;
    end process;
    Q <= Q_int;
end behavioral;

A T (Toggle) flip-flop changes (toggles) its output state on every clock edge when T=1, and holds its current state unchanged when T=0 - implemented directly here with a single conditional statement inside the clocked process, this simple toggle behavior makes T flip-flops the standard building block for binary ripple and synchronous counters, where each successive counter bit toggles at half the rate of the bit below it.

(d) 3-Input XOR Gate

vhdl
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity xor3 is
    port (A, B, C : in STD_LOGIC; Y : out STD_LOGIC);
end xor3;

architecture dataflow of xor3 is
begin
    Y <= A xor B xor C;
end dataflow;

A 3-input XOR gate produces a '1' output whenever an odd number of its three inputs (one or all three) are '1', and a '0' output when an even number (zero or two) of the inputs are '1' - VHDL's xor operator applied across all three inputs directly expresses this odd-parity function, and this exact 3-input XOR structure is precisely the Sum output equation of a full adder (Sum = A xor B xor Cin), directly connecting this basic logic gate to the full-adder circuit examined in the alternate paper's version of this question.

Considering all four circuits in this question together, a clear pattern emerges: the JK flip-flop is the most general of the basic clocked storage elements (able to hold, set, reset, or toggle depending on its two control inputs), and both the D flip-flop and the T flip-flop shown here are derived from it simply by tying its J and K inputs together in specific complementary or common patterns, demonstrating that a single, sufficiently general flip-flop design can serve as the universal building block from which every other common flip-flop type can be constructed purely through input wiring, without needing a separate from-scratch VHDL description for each flip-flop type. This 'one general building block, several derived special cases' pattern mirrors a broader theme across digital logic design, where a small number of sufficiently expressive primitive elements (universal gates such as NAND/NOR, or a general JK flip-flop) can be combined or configured to realize a much larger family of derived functions or components.

The 3-input XOR gate included in this question is a useful bridge back to the sequential elements examined alongside it, since XOR (and its parity-detection behavior) is the core combinational element inside virtually every adder circuit's Sum computation, and pairing a full-adder-style XOR/carry combinational structure with a clocked flip-flop (whether derived here as a D or T flip-flop from the more general JK) is exactly the combination used to build sequential arithmetic circuits such as the serial adder examined elsewhere in this examination, where a combinational XOR/carry computation is repeatedly applied across successive clock cycles with the carry value held in a flip-flop between cycles - this connects the seemingly separate topics of basic flip-flop derivation and basic combinational gate design into the larger picture of how sequential arithmetic circuits are actually built from these smaller pieces.

It is also worth examining why the D-flip-flop-from-JK and T-flip-flop derivations shown here use structurally different VHDL approaches despite both being 'special cases' of the general JK flip-flop: the D-flip-flop derivation is written structurally, explicitly wiring J<=D and K<=(not D) and then instantiating the same case-statement JK behavior, directly demonstrating the input-tying derivation technique in the VHDL code itself; the T-flip-flop shown here, by contrast, is written directly as its own simplified behavioral description (toggling only when T='1'), rather than being derived structurally from a JK entity instance, even though a T flip-flop could equally well have been built by tying a JK flip-flop's J and K inputs together as a single input T (giving J=K=T, which produces exactly toggle-when-T='1'-else-hold behavior) - both are valid VHDL coding choices, and the difference illustrates that a derived flip-flop type can be described either structurally (explicitly instantiating and wiring the more general component) or directly with its own simplified independent behavioral description, with the same final synthesized hardware resulting either way for a specific fixed-function flip-flop.

The 3-input XOR gate examined in part (d) also has a direct connection to the D and T flip-flop derivations shown here through the broader theme of characteristic equations versus truth-table-based descriptions: just as the JK flip-flop's behavior can be expressed either via an explicit case statement over its truth table or via its characteristic equation Q(next) = JQ' + K'Q (as shown in detail in the alternate 2021 paper of this examination), the 3-input XOR's odd-parity behavior can likewise be expressed either as an explicit truth table (case or selected-assignment) covering all eight input combinations, or far more compactly via the single algebraic XOR expression used in the actual code here - recognizing when a combinational or sequential function has a compact underlying algebraic form (as both the JK characteristic equation and the XOR expression do) versus when it instead requires an explicit enumerated truth-table description (as the 7-segment-to-BCD converter examined elsewhere in this examination does) is a recurring and important judgment call in writing efficient, readable VHDL.

Finally, it is worth noting that all four sub-parts of this question - the JK flip-flop, the D and T derivations, and the 3-input XOR gate - are each written using the STD_LOGIC type from the IEEE std_logic_1164 package (rather than the simpler BIT type), consistent with the standard modern VHDL convention discussed elsewhere in this examination of using the richer nine-valued type for any signal intended to realistically model hardware, which correctly allows the simulator to show an uninitialized 'U' value on each flip-flop's Q output before its first clock edge establishes a known state.

Back to Paper