Author Topic: HDL code for positive edge triggered J-K flip-flop with preset and clear  (Read 482 times)

0 Members and 1 Guest are viewing this topic.

Offline caius

  • Regular Contributor
  • *
  • Posts: 131
  • Country: it
    • Caius Arcade Repairs & Engineering
Hi all,
I' new to HDL and I'm not yet able to write my own code so I'm using block diagram/schematics under Quartus II.I'm looking for Verilog code (I prefer this over VHDL) to implement TTL gates .I need code for a positive edge triggered J-K flip-flop with preset, clear and complementary outputs (like the 74LS109 for example).Can some one help me?Thanks in advance!
 

Offline cruff

  • Contributor
  • Posts: 34
  • Country: us
Try searching for "verilog jk flip flop" and I expect you will have your pick of examples.
 

Offline caius

  • Regular Contributor
  • *
  • Posts: 131
  • Country: it
    • Caius Arcade Repairs & Engineering
I did many searches but found only example of JK flip flop with no preset.As aid, I need something like the 74LS019.
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 3822
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
I speak VHDL, not Verilog, so I'm afraid I can't help with your example - but...

All too often I see people trying to learn a hardware description language by coding individual logic functions, then trying to build those up into a model of the circuit they actually want - like you'd have to do if you were restricted to building your circuit using off-the-shelf components.

I've seen examples that show ridiculously simple logic - like, say, a single OR gate - and wrap it up in a process or even a whole component, then instantiate it at a higher level in order to actually use it. This is kind of OK as a way to demonstrate the syntax of the language, but as a way to learn how to really use HDL, it's terribly misleading.

The beauty of an FPGA is that you can describe the behaviour you want without having to worry about how that behaviour might, possibly, be realised using off-the-shelf logic gates. If you're following a tutorial or worked example which does spend more than a trivial amount of time creating complex circuits this way, I'd advise you to look elsewhere. Don't ever get into the mind set of building real circuits directly from these traditional building blocks; that's the job of the synthesis tool, not you.

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 8447
  • Country: us
First you need the Truth Table for a JK flop with Preset
https://electronicsarea.com/jk-flip-flop-preset-clear-inputs-truth-table/
Pay very close attention to the effect of the asynchronous Preset and Clear inputs.

At page 6, there is a VHDL description for a JK flop with Preset
https://web.nmsu.edu/~tturner5/courses/et398/files/reports/TurnerET398Lab6.pdf

On page 7 there is code for the flop.  Note how the asynchronous Preset and Clear inputs are handled in the process before the clocking is even considered.  These asynchronous inputs mask out any transition change that might occur from the J and K inputs and the clock.

I don't use Verilog so transmorgifying from VHDL to Verilog is on you.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 8447
  • Country: us
Here's a video of a Xilinx ISE Verilog project including simulation for a J-K flop with asynchronous Clear.  Adding asynchronous Preset is trivial.


 

Offline caius

  • Regular Contributor
  • *
  • Posts: 131
  • Country: it
    • Caius Arcade Repairs & Engineering
Thank for replies, guys.I wonder if there is some kind of library for 74 TTLs in Verilog (or VHDL, I don't dislike it).
 

Offline emece67

  • Frequent Contributor
  • **
  • Posts: 393
  • Country: es
There are some (very incomplete) TTL HDL libraries, but such is not the way FPGA are intended to be used. I can only advice you to give up such approach and:
  • continue using schematics, if you do not want to learn HDLs
  • or learn any HDL and make proper usage of it
in both cases always relying on synchronous design.

If my memory serves me well, the old Altera MAX+plusII software came with a library of TTL parts you can use in HDL designs, but this was many years ago, at the beginning of the FPGA era, and as an aid for people to move from TTL to FPGA.

As and exercise, I wrote this:
Code: [Select]
module ff_jk_sc (SET_n, CLR_n, CLK, J, K_n, Q, Q_n);
  input   SET_n;
  input   CLR_n;
  input   CLK;
  input   J;
  input   K_n;
  output  Q;
  output  Q_n;

  reg     Q;

  assign Q_n = ~Q;

  always @(negedge SET_n, negedge CLR_n, posedge CLK) begin
    if (!SET_n) begin
      Q <= 1;
    end else if (!CLR_n) begin
      Q <= 0;
    end else begin
      case ( {J, K_n} )
        2'b00:    Q <= 0;
        2'b11:    Q <= 1;
        2'b10:    Q <= ~Q;
        default:  ; // nothing
      endcase
    end
  end
endmodule
not exactly a 74109 (here SET has priority over CLR in case both are active simultaneously) but, after synthesis, one arrives to the mess you can see below. That's 3 FF/latches + 7 LUT.
Information must flow.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 7193
  • Country: fr
Question of course is: why would you implement basic logic gates and flip-flops in HDL? Other than as an exercise? (Which I suppose it is...)

Except for very particular cases, implementing such low-level logic in HDL is a pretty inefficient way of using a HDL. Just saying.
 

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 5576
  • Country: ca
The OP never specified that the preset and clear need to be asynchronous.

Keeping things synchronous in the FPGA keep the design timing very predictable unless you specifically know your fitter will be using hardware async preset and clear within the logic cells.
__________
BrianHG.
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 2598
  • Country: dk
Question of course is: why would you implement basic logic gates and flip-flops in HDL? Other than as an exercise? (Which I suppose it is...)

Except for very particular cases, implementing such low-level logic in HDL is a pretty inefficient way of using a HDL. Just saying.

not only inefficient, that kind of async logic is also a bad match for an FPGA
 

Online NorthGuy

  • Super Contributor
  • ***
  • Posts: 2579
  • Country: ca
You can build something from logic ICs and there are many of them that you can use.

FPGA is built the same way, it has blocks and interconnect. You connect the blocks pretty much the same way as you would connect logic ICs, except blocks are different - you won't fond JK flip-flops in FPGA, but you will find LUTs for example. There's some overlap - for example, D flip flops are present both in FPGA and logic IC series.

If you use logic ICs, it will never occur to you to build a LUT because you can use various ready-made gates instead.

Similarly, if you use FPGA, there's no point in building JK flip flops, because it's better to use resources which are already present in FPGA.

FPGA is actually much easier than logic ICs - the variety of blocks is not anywhere close to the variety of logic ICs and the blocks are more configurable. Not to mention the tools will build circuits for you.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf