-1
\$\begingroup\$

I want to write code and simulate waveforms for flip flops strictly using dataflow modelling. In this case I'm simulating a jk flip flop with only j,k and clock (no set , reset). It compiles fine, but when I try to simulate run the waveforms (j,k,clk,q,qbar), my modelsim stops responding. I guess it has something to do with using output as input, as I'm not well versed with sequential circuits in vhdl but combinational circuits seem doable.

library ieee;
use ieee.std_logic_1164.all;
entity jkff_data is
   port (clk,j,k : in std_logic;
            q,qbar : out std_logic);
 end jkff_data;
architecture dataflow of jkff_data is
signal s1, s2, s3, s4 : std_logic;
begin
   s1 <= not(j and clk and qbar); 
   s2 <= not(k and clk and q); 
   s3 <= s1 nand qbar; 
   s4 <= s2 nand q; 
   q <= s3 ;
   qbar <= s4 ;
end dataflow;

my circuit with signals enter image description here

\$\endgroup\$
8
  • \$\begingroup\$ Can you try q and qbar outputs as buffer type port instead of out type. \$\endgroup\$ Commented Feb 25, 2021 at 8:42
  • 3
    \$\begingroup\$ You have a combinational feedback loop. Synthisers will flag it as DRC error and you won't able to use this "unpredictable" logic on an actual board. Not sure what you are trying to achieve with this code. And of course if it's for simulation purpose only, then you have infinitely triggered concurrent statements. \$\endgroup\$
    – Mitu Raj
    Commented Feb 25, 2021 at 12:17
  • 2
    \$\begingroup\$ Feedback with negation and zero delays means you get delta cycle oscillations where each delta cycle causes another until you hit the iteration limit. Some FPGA synthesis tools won't balk when synthesizing feedback loops. Preventing oscillation requires tuning delays or using a pulse derived from a clock edge. Alternatively you can use a master-slave flip flop. \$\endgroup\$
    – user8352
    Commented Feb 25, 2021 at 23:07
  • 1
    \$\begingroup\$ @AyushSinha Hey, addressing the question, which is about the Modelsim freezing. I tried to reproduce it as you intended, without a tesbench by changing inputs of your jkff_data in the waveform, but it looks to successfully simulate for me. It gives undesired unknown values at the outputs, but it is another question, I guess. So could you give some more details, what leads to Modelsim freezing (software version, compilation options, etc.)? Moreover, is it still a problem? \$\endgroup\$
    – megasplash
    Commented Mar 3, 2021 at 9:09
  • 3
    \$\begingroup\$ This is not the circuit of JK F/F. This is JK Latch's circuit. In popular naming convention, F/F are classified as edge triggered and Latches as level triggered. This is a level triggered latch. \$\endgroup\$
    – Mitu Raj
    Commented Mar 3, 2021 at 15:13

1 Answer 1

1
\$\begingroup\$

Note: The circuit you are using is not the correct one. See JK latch, possible Ben Eater error?

The output remains unknown when simulated without a reset. Try this one which uses a reset:

Try this code:

use IEEE.std_logic_1164.all;

entity JK_FF is
port (J, K, CLK, reset: in std_logic; -- inputs
      Q, Q_bar: out std_logic);
end entity JK_FF;

architecture dataflow of jk_ff is
signal s1, s2, s3, s4 : std_logic;
begin
   s1 <= not(j and clk and q_bar); 
   s2 <= not(k and clk and q); 
   s3 <= s1 nand q_bar; 
   s4 <= s2 nand q; 
   q <= s3 and (not(reset)) ;
   q_bar <= s4 and reset;
end dataflow;

You can simulate it on EDA Playground (requires logging in) in your browser here: https://edaplayground.com/x/wDX5

The problem is you can't set an initial value for Q and Q_bar without a reset. This code uses an asynchronous reset, but you should use a synchronous reset if you are targeting an FPGA.

Of course, the synthesizability of this is another question. The synthesizability of this VHDL code depends entirely on the synthesizer you are using. An HDL code might work in simulation but may not necessarily be synthesizable.

For ASICs a reset signal is recommended for all flip-flops except pipeline registers.

See also:

  1. Is it possible to create a working JK-flip flop using gate level description in Verilog
  2. JK-flip flop using gate level description in Verilog give me a timming error

If you want a JK Flip-Flop in VHDL that works, use this:

library IEEE;
use IEEE.std_logic_1164.all;

entity JK_FF is
port (J, K, CLK: in std_logic; -- inputs
      Q, Q_bar: out std_logic);
end entity JK_FF;

architecture behaviour of JK_FF is 
signal QINT:std_logic;
begin
process (CLK) 
  variable JK : std_logic_vector( 1 downto 0);
  begin
  JK := J & K; 
  if rising_edge(clk) then    
   case (JK) is
      when "00" => QINT <= QINT;
      when "01" => QINT <= '0';
      when "10" => QINT <= '1';
      when "11" => QINT <= not QINT;
      when others => QINT <= 'X';
    end case;  
  end if;
end process;
Q <= QINT;
Q_bar <= not QINT;
end behaviour;
\$\endgroup\$
7
  • \$\begingroup\$ If I try plotting signals by adding to wave, it doesn't give the right outputs. For eg. I start with reset = 1, clk is a clock, it gives q = 0 (fine). I force reset to zero, and J = 1, K = 1. Q should be toggling but it doesnt and stays stuck at Q=0. \$\endgroup\$ Commented Mar 3, 2021 at 7:45
  • \$\begingroup\$ @AyushSinha When you have not built a JK FF, how can you expect a JK FF simulation results? \$\endgroup\$ Commented Mar 3, 2021 at 9:50
  • \$\begingroup\$ I have done jk ff using behavioural modelling already, I need to do it using dataflow strictly. And my first comment was regarding the case when I simulated the code that you first provided (the non behavioural one) \$\endgroup\$ Commented Mar 3, 2021 at 13:54
  • \$\begingroup\$ @AyushSinha that is not a jk ff. The first one is a jk latch with a reset \$\endgroup\$ Commented Mar 3, 2021 at 13:56
  • 1
    \$\begingroup\$ So In general dataflow and structural styles aren't good for building sequential circuits. As for the initial state of Q, You used reset. \$\endgroup\$ Commented Mar 5, 2021 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.