0

I'm writing verilog code of 2 Bit Counter using JK Flip Flop that counts 0-3 and back to 0. I'm using Xilinx EDA. However I'm keep getting one error and I don't know what it means? The line numbers doesn't show up here, but error is located at "always @(posedge clk)".

ERROR:HDLCompiler:1401 - "C:\Users\Eduardo\Documents\SFSU\Fall 2014\Engr 378\Lab 3\TwoBitCounter\twobitcounter.v" Line 30: Signal q in unit jkff is connected to following multiple drivers:

`timescale 1ns / 1ps
module twobitcounter( q_out, qbar_out, j,k, clk, reset);
    input [1:0] j; input [1:0] k; input clk; input reset;
    output [1:0] q_out;
    output [1:0] qbar_out;
    wire [1:0] q_out;
    wire [1:0] qbar_out;
    wire clk;

    assign qbar_out[0] = ~q_out[0];
    assign j[0] = 1;
    assign k[0] = 1;
    assign j[1] = q_out[0];
    assign k[1] = q_out[0]; 

    jkff M1(q_out[0], qbar_out[0], j[0], k[0], clk, reset);
    jkff M2(q_out[1], qbar_out[1], j[1], k[1], qbar_out[0]);

endmodule

module jkff(output q_out, output qbar_out,
  input j, input k, input clk, input reset);

    reg q;
    assign q_out = q;
    assign qbar_out = ~q;

    initial begin 
        q = 1'b0;
        end
    always @(posedge clk)
        begin
        case({j,k})
        {1'b0, 1'b0}: begin
            q = q;
            end
        {1'b0, 1'b1}: begin
            q = 1'b0;
            end
        {1'b1, 1'b0}: begin
            q = 1'b1;
            end
        {1'b1, 1'b1}: begin
            q = ~q;
            end
        endcase
        end

    always @(posedge reset)
        begin
        q = 1'b0;
        end
endmodule
Greg
  • 18,111
  • 5
  • 46
  • 68
Eduardo Romero
  • 13
  • 1
  • 1
  • 4

2 Answers2

2

The issue is q is being set in two always blocks, which is not allowed in synthesis. Merge the two always blocks. Also, q is a flop, so it should be assigned using non-blocking assignment (<=), not blocking assignment (=).

always @(posedge clk or posedge reset)
begin
  if (reset == 1'b1) begin
    q <= 1'b0;
  end
  else begin
    case({j,k})
    {1'b0, 1'b0}: begin
        q <= q;
        end
    {1'b0, 1'b1}: begin
        q <= 1'b0;
        end
    {1'b1, 1'b0}: begin
        q <= 1'b1;
        end
    {1'b1, 1'b1}: begin
        q <= ~q;
        end
    endcase
  end
end

You should almost never use initial blocks in synthesizable code. Most FPGAs allow it for initialization. ASICs designs however do not support it. For both cases, if there is an asynchronous reset/set then it initial block shouldn't be used.

Greg
  • 18,111
  • 5
  • 46
  • 68
  • I tried your advise, but it gave me same error for module twobitcounter. I get an error at "assign qbar_out[0] = ~q_out[0];" It states that qbar_out[0] is connected to following multiple drivers – Eduardo Romero Oct 05 '14 at 01:54
  • `q_out[0]` and `qbar_out[0]` are both outputs of M1. That makes it dual driver which is illegal. Comment out `assign qbar_out[0] = ~q_out[0];`. Your also missing a port connection on M2 – Greg Oct 06 '14 at 16:12
1

The error is telling you that you are assigning q in different blocks. This creates an error. You are assigning q in both your initial block and your always block.

You should never use initial blocks in synthesizable code.

Russell
  • 3,384
  • 4
  • 31
  • 45
  • **Almost never use initial blocks (most synthesis tools for FPGAs allo initial blocks in certain cases, like initializing memory elements). Also, q is assigned in the @(posedge reset) block as well. – Unn Oct 02 '14 at 19:27