Tai Chau
Tai Chau

Reputation: 33

Bit by bit comparison between two variables in Verilog

Currently, I am beginning to write the firmware by Verilog for one idea. It is comparing bit by bit between two variables and then using one binary counter to count the number of different bits.

For example:

I have two variables in verilog

A : 8'b00100001;

B : 8'b01000000;

Then I give the condition to compare bit by bit between two variables. If there is difference between 1 bit of A and 1 bit of B at same bit position, binary counter will count.

This is my verilog code:

module BERT_test(
  input CLK,
  input RST,
  input [7:0] SIG_IN,
  input [7:0] SIG_OUT,
  output [7:0] NUM_ERR
);
  integer i;
  reg[7:0] sign_in;
  reg[7:0] sign_out;

  always @(posedge CLK) begin
    sign_in[7:0] <= SIG_IN[7:0];
    sign_out[7:0] <= SIG_OUT[7:0];
  end

  reg [15:0] bit_err; 

  // Combinational Logic
  always @* begin
    bit_err = 8'b0;
    for (i=0;i<8;i=i+1) begin
      if (sign_in[i] == sign_out[i]) begin
        bit_err = bit_err + 8'b0;
      end else begin
        bit_err = bit_err + 8'b1;
      end
    end
    assign NUM_ERR = bit_err;
  end
endmodule 

Then I had a mistake

Reference to vector wire 'NUM_ERR' is not a legal reg or variable lvalue

I do not know how to solve this problem. Are there any solutions for this problem or how I need to modify my firmware, please suggest me.

Upvotes: 0

Views: 7798

Answers (2)

Oldfart
Oldfart

Reputation: 6269

You have an assign WITHIN an always block. Move it outside.

Adding zero to bit error if the bits are the same is superfluous.

if (sign_in[i] != sign_out[i]) 
  bit_err = bit_err + 8'b1;

Also bit error is 16 bits so it is not wrong to add 8'b1 but misleading.

Upvotes: 0

Matthew
Matthew

Reputation: 13987

You are driving NUM_ERR (a net) from an always block. It is not permitted to drive nets from always blocks (or initial blocks). You need to move this line:

assign NUM_ERR = bit_err;

outside the always block.

You should not use an assign statement inside an always block. This is legal but is deprecated and means something weird. If you have included this line inside the always block by mistake, then indenting you code properly would have shown it up.

Upvotes: 2

Related Questions