Advertisement
CHU2

JKFFTF (currently not working)

Jun 11th, 2024
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VeriLog 1.51 KB | Source Code | 0 0
  1. module JKflipflopTF (
  2.   input J, K,
  3.   input clock,
  4.   output reg [3:0] Q, Qb
  5. );
  6.  
  7.     initial Q <= 4'b0;
  8.  
  9.   // Function to implement JK flip-flop logic
  10.   function [1:0] F (input J, K);
  11.     begin
  12.       case ({J, K})
  13.         2'b00: F[0] = Q[0];  // Hold state (no change)
  14.         2'b01: F[0] = 1'b0;  // Reset
  15.         2'b10: F[0] = 1'b1;  // Set
  16.         2'b11: F[0] = !Q[0]; // Toggle
  17.       endcase
  18.       F[1] = !F[0];
  19.     end
  20.   endfunction
  21.  
  22.   // Counter logic using always block
  23.   always @(posedge clock) begin
  24.   Q[3] <= F(Q[3], !Q[0]); // Update MSB
  25.   Q[2] <= F(Q[2], Q[3]);  // Update next based on current MSB
  26.   Q[1] <= F(Q[1], Q[2]);
  27.   Q[0] <= F(Q[0], J);     // Update LSB based on J
  28.   Qb <= ~Q;                // Assign complement of Q to Qb
  29. end
  30.  
  31.  
  32. endmodule
  33.  
  34. module JKflipflopTFTB;
  35.   reg clock, reset;
  36.   reg J, K;
  37.   wire [3:0] Q, Qb;
  38.  
  39.   JKflipflopTF counter (
  40.     .J(J),
  41.     .K(K),
  42.     .clock(clock),
  43.     .Q(Q),
  44.     .Qb(Qb)
  45.   );
  46.  
  47.   // Generate clock signal
  48.   always #10 clock = !clock;
  49.  
  50.   // Optional reset logic (not used in this example)
  51.   initial begin
  52.     reset = 1'b1;
  53.     #10 reset = 1'b0;
  54.   end
  55.  
  56.   initial begin
  57.   $monitor("Count = %b", Q);
  58.   clock <= 1'b0;
  59.   J <= 1'b1;  // Set J to 1 for increment
  60.   K <= 1'b0;  // Set K to 0 to maintain state (previously 1'b1)
  61.   #100;
  62. end
  63.  
  64.   always @(posedge clock) begin
  65.     // Removed unnecessary code (previously caused issues)
  66.     $display("Clock cycle: %d, Q: %b, Qb: %b", $time, Q, Qb);  // Display values
  67.   end
  68.  
  69. endmodule
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement