I tried to make a CMP instruction in Verilog. To keep the result of the substraction, I declared a wire. This is what the code looks like (It is executed in an always statement).
wire [data_width:0] tmp_wire = reg_accumulator - reg_x;
f_zero <= tmp_wire & 'hFF == 0;
f_carry <= tmp_wire & 'h100;
Now Icarus Verilog complains about a syntax error and that reg_accumulator - reg_x is not an l-value:
cpu.v:149: syntax error
cpu.v:149: Syntax in assignment statement l-value.
And why does it complain? What would be the right approach to declare a temporary variable in a function / task?
module comparator(
input clk,
input [7:0] op_a,
input [7:0] op_b
);
reg f_zero;
reg f_carry;
function compare;
input [data_width-1:0] a;
input [data_width-1:0] b;
begin
wire [7:0] tmp_wire = reg_accumulator - reg_x;
f_zero <= tmp_wire & 'hFF == 0;
f_carry <= tmp_wire & 'h100;
end
endfunction
always @(posedge clk) begin
compare(op_a, op_b);
end
endmodule // comparator
alwaysblock should be toregs (as you rightfully declaredf_zeroandf_carry). You put the wire declaration and assignment inside thealwaysso the error is for trying to update awirein analwaysblock which is illegal...reg_accumulatorandreg_xare not defined...function compare; input [data_width-1:0] a; input [data_width-1:0] b; wire [data_width:0] tmp = a - b; begin end endfunction