Getting a simple syntax error when using a gate primitive? - verilog

So I'm trying to implement a basic ALU for my first Verilog course, DSD II. Xilinx keeps reporting an error on the lines where I call the gate primitives "and"/"or", but I've used them in previous assignments this way with no errors. Can anybody see what I'm missing?
Code:
module ALU(a,b, opcode, carry, Y, zeroflag);
input [15:0]a;
input [15:0]b;
input [3:0]opcode;
wire In = {opcode,a,b};
output reg carry;
output reg [15:0]Y;
output reg zeroflag;
always #(In)
begin
case(opcode)
//Zero Op
4'b0000 :
begin
Y = 16'h00;
carry = 0;
zeroflag = 1;
end
//Add
4'b0001:
begin
Ripple_Carry_Adder RCA1(Y,carry,a,b,carry);
end
//Subtract
4'b0010:
begin
end
//Multiply
4'b0011:
begin
Y = a*b;
if (Y > 65535)
carry = 1;
else
carry = 0;
if (Y == 0)
zeroflag = 1;
else
zeroflag = 0;
end
//Divide
4'b0100:
begin
Y = a/b;
if (Y > 65535)
carry = 1;
else
carry = 0;
if (Y == 0)
zeroflag = 1;
else
zeroflag = 0;
end
//And
4'b0110:
begin
and(Y, a, b);
if (Y == 0)
zeroflag = 1;
else
zeroflag = 0;
end
//Or
4'b0111:
begin
or(Y,a,b);
if (Y == 0)
zeroflag = 1;
else
zeroflag = 0;
end
//Zero Test
4'b1001:
if((a || b) == 0)
begin
Y = 0;
zeroflag = 1;
carry = 0;
end
//Greater Than
4'b1010:
begin
if(a > b)
Y = a;
else if (b > a)
Y = b;
else
Y = 16'h00;
if (Y == 0)
zeroflag = 1;
else
zeroflag = 0;
end
//Equal
4'b1011:
begin
if (a == b)
Y = 16'h11;
else
Y = 16'h00;
if (Y == 0)
zeroflag = 1;
else
zeroflag = 0;
end
//Less Than
4'b1100:
begin
if(a < b)
Y = a;
else if (b < a)
Y = b;
else
Y = 16'h00;
if (Y == 0)
zeroflag = 1;
else
zeroflag = 0;
end
default :
begin
Y = 16'hxx;
carry = 1'bx;
zeroflag = 1'bx;
end
endcase
end
endmodule

You are instantiating the primitives (as well as the module Ripple_Carry_Adder) inside of an always block, which is not allowed.
Any time you create a module or primitive instance, think of it as placing down a physical piece of hardware. You cannot create it conditionally - it is always there.
So for something like an ALU design, you may want all the operations (add, sub, multiply, divide, etc.) to always happen, and then select the desired output depending on the opcode.

Related

Gate instantiation in if else statement in verilog

I am designing Signed comparator that uses the unisgned comparator module. i.e. if A and B are 4 bit vectors and
if A[3] ==1 and B[3]==0 then
Gout = 0, Eout = 0 and Lout = 1.
if A[3]==0 and B[3]==1 then
Gout = 1, Eout = 0 and Lout = 0;
else if both A[3] and B[3] are same then
the unisigned comparator module has to be instantiated.
How can I write this gate instantiation inside a if else statement?
module SCOMP(A,B,Great_in,Equal_in,Less_in,Great_out,Equal_out,Less_out);
input[3:0] A;
input[3:0] B;
input Great_in,Equal_in,Less_in;
output Great_out,Equal_out,Less_out;
reg[3:0] X;
reg[3:0] Y;
reg p,q,r;
wire x,y,z;
initial
begin
X = 0000& A[2:0];
Y = 0000& B[2:0];
end
COMP4 g1(X,Y,Gin,Ein,Lin,x,y,z);
always #(*)
begin
if ((A[3]==0)&& (B[3]==1))
begin
assign p = 1;
assign q = 0;
assign r =0;
end
else if ((A[3]== 1)&&(B[3]==0))
begin
assign p = 0;
assign q = 0;
assign r = 1;
end
else
begin
assign p = x;
assign q = y;
assign r = z;
end
end
assign Great_out = p;
assign Equal_out = q;
assign Less_out = r;
endmodule
Verilog is a Hardware Description Language. Hardware either exists or it doesn't. Instantiation of anything is like soldering a chip to a PCB. Instantiating something inside an if statement would be like designing a PCB where the chips can magically appear or disappear depending on some input to the PCB.
Your "unsigned comparator module" has to exist for all time - it has to be instantiated unconditionally. You then need to use your if statements to decide whether to use the outputs from this "unsigned comparator module" or to ignore them, eg:
// the instance of the "unsigned comparator module"
unsigned_comparator_module UCM ( ... .gout(ucm_gout), .eout(ucm_eout), .lout(ucm_lout) ... );
always #* begin
if (A[3] == 1 && B[3] == 0) begin
Gout = 0; Eout = 0; Lout = 1;
end else if (A[3] == 0 && B[3] == 1) begin
Gout = 1; Eout = 0; Lout = 0;
end else if (A[3] == B[3]) begin
Gout = ucm_gout; Eout = ucm_eout; Lout = ucm_lout;
end
end
the best way to do that inside the if condition write the comparator code where u want. for eaxample
else if both A[3] and B[3] are same then
the unisigned comparator module has to be written here.

error in verilog code of object on left side should be assigned a variable

I have been trying to make an asynchronous fifo in verilog but I'm facing a problem of object "empty" and "full" on left side of assignment should have variable data type.
Top module:
module async_fifo (reset, wclock, rclock, datain, dataout, e, f);
input [15:0] datain;
output reg [15:0] dataout;
//reg [15:0] mem1, mem2, mem3, mem4, mem5, mem6, mem7, mem8;
reg [15:0] mem [7:0];
input reset, rclock, wclock;
/*reg [0:2] wptr, rptr;
initial wptr = 3'b000;
initial rptr = 3'b000;*/
integer wflag = 0, rflag = 0;
wire empty , full;
input e,f;
reg [0:2] wptr = 3'b000, rptr = 3'b000;
counter c(wclock, rclock, empty, full);
e = empty;
f = full;
always#(posedge wclock)
begin
if(f == 1'b0)
begin
e = 1'b0;
if (wptr < 3'b111)
begin
mem[wptr] = datain;
wptr = wptr + 3'b001;
end
else if(wptr == 3'b111 && wflag == 0)
wflag = 1;
else if (wflag == 1)
begin
wptr = 3'b000;
wflag = 0;
end
end
end
always#(posedge rclock)
begin
if(e == 1'b0)
begin
f = 1'b0;
if (rptr < 3'b111)
begin
dataout = mem[rptr];
rptr = rptr + 3'b001;
end
else if(rptr == 3'b111 && rflag == 0)
rflag = 1;
else if (rflag == 1)
begin
rptr = 3'b000;
rflag = 0;
end
end
end
endmodule
Counter module:
module counter(w_clock, r_clock, empty, full);
input w_clock, r_clock;
output reg empty = 0, full = 0;
integer rear = 0, front = 0;
always # (posedge w_clock)
begin
if ((front == 1 && rear == 8) || front == rear + 1)
full = 1;
else if(rear == 8)
begin
rear = 1;
empty = 0;
end
else
begin
rear = rear+1;
empty = 0;
end
end
always # (posedge r_clock)
begin
if (front == 0 && rear == 0)
empty = 1;
else if(front == 8)
begin
front = 1;
full = 0;
end
else
begin
front = front+1;
full = 0;
end
end
endmodule
You are using full and empty in left hand side of behavioral block (always). So they have to be registers.
But at the same time they are output of counter and have to be wires.
You can't use variables in that way.They can either be output of an instant and only used in right hand side of other parts of code or be regsietrs for using in left hand side of behavioral blocks that can also be input of another instant.
You better change your coding style.
Here is an examole of async FIFO:
http://www.asic-world.com/code/hdl_models/aFifo.v
And also you need to study about blocking & nonblocking assignment and race conditions. Take a look at this:
http://ee.hawaii.edu/~sasaki/EE361/Fall01/vstyle.txt

"Multiple Constant Drivers" Error Verilog with Quartus Prime

I am working on designing a finite state machine in Verilog to represent a stack. The module is as follows:
module state_machine (s, Enable, Clock, Resetn, c, OF_Err, UF_Err);
input [2:0] s;
input Enable, Clock, Resetn;
output reg [1:0] c;
output reg OF_Err = 0, UF_Err = 0;
reg [2:0] y, Y;
parameter [2:0] A = 3'b000, B = 3'b001, C = 3'b010, D = 3'b011, E = 3'b100;
always #(s, y, Enable)
if (Enable)
begin
case (y)
A: if (s == 3'b000) Y = B;
else
begin
Y = A;
UF_Err = 1;
end
B: if (s == 3'b000) Y = C;
else if (s == 3'b001) Y = A;
else
begin
Y = B;
UF_Err = 1;
end
C: if (s == 3'b000) Y = D;
else if (s == 3'b100) Y = C;
else Y = B;
D: if (s == 3'b000) Y = E;
else if (s == 3'b100) Y = D;
else Y = C;
E: if (s == 3'b000)
begin
Y = E;
OF_Err = 1;
end
else if (s == 3'b100) Y = E;
else Y = D;
default: Y = 3'bxxx;
endcase
c[1] = y[1];
c[0] = y[0];
end
always #(negedge Resetn, posedge Clock)
begin
if (Resetn == 0)
begin
y <= A;
OF_Err = 0; //Problem
UF_Err = 0; //Problem
end
else y <= Y;
end
OF_Err and UF_Err are indicators of overflow and underflow errors, respectively.
However, I get the following errors when compiling my project:
Error (10028): Can't resolve multiple constant drivers for net "OF_Err" at state_machine.v(59)
Error (10029): Constant driver at state_machine.v(10)
Error (10028): Can't resolve multiple constant drivers for net "UF_Err" at state_machine.v(59)
These only appeared after I added the commented lines. I want to reset the over- and underflow indicators when the FSM is reset, but I can't do it the way I have it. How do I go about doing this?
(If it's of any value, this is to be executed on an Altera DE2-115).
In two always blocks you have assigned the values to OF_Err and UF_Err. This is the reason it is showing multiple constant driver error.
module state_machine (s, Enable, Clock, Resetn, c, OF_Err, UF_Err);
input [2:0] s;
input Enable, Clock, Resetn;
output reg [1:0] c;
output OF_Err, UF_Err; //modified
reg [2:0] y, Y;
reg of_Err, uf_Err; //added
parameter [2:0] A = 3'b000, B = 3'b001, C = 3'b010, D = 3'b011, E =3'b100;
always #*
begin
if (Enable)
begin
case (y)
A: if (s == 3'b000)
Y = B;
else
begin
Y = A;
uf_Err = 1; //modified
end
B: if (s == 3'b000)
Y = C;
else if (s == 3'b001)
Y = A;
else
begin
Y = B;
uf_Err = 1; //modified
end
C: if (s == 3'b000)
Y = D;
else if (s == 3'b100)
Y = C;
else
Y = B;
D: if (s == 3'b000)
Y = E;
else if (s == 3'b100)
Y = D;
else Y = C;
E: if (s == 3'b000)
begin
Y = E;
of_Err = 1; //modified
end
else if (s == 3'b100) Y = E;
else Y = D;
default: Y = 3'bxxx;
endcase
c[1] = y[1];
c[0] = y[0];
end
else
begin
//write the condition if the Enable signal is not high.I guess you're trying to synthesize
end
end
always #(negedge Resetn, posedge Clock)
begin
if (Resetn == 0)
begin
y <= A;
// OF_Err = 0; //Problem
// UF_Err = 0; //Problem
end
else y <= Y;
end
assign OF_Err = !Resetn? of_Err : 1'b0; //added
assign UF_Err = !Resetn? uf_Err : 1'b0; //added
endmodule
As others have already pointed out, OF_Err and UF_Err were driver by two always blocks which is illegal for synthesis. I recommend creating two additional variables of_Err and uf_Err like Arvind suggested. However I recommend keeping OF_Err and UF_Err as flops.
The if (Enable) in the combinational block infers Y,c and the *_Err as level-sensitive latches. I highly doubt this is what you intendeds. I recommend moving the if (Enable) into synchronous always block and keeping the combinational logic as pure combinational.
c is a simple assignment, so it might make more sense having it as a wire instead of a reg with a simple assign statement. It can be in the combinational block, but I prefer to separate combinational input from output.
You did use #(s, y, Enable) correctly, however #* or the synonymous #(*) is recommenced for combinational block. #* is an auto sensitivity list which saves you typing, maintenance, and removes the risk of forgetting a signal.
always #*
begin
of_Err = OF_Err; // <-- default values
uf_Err = UF_Err;
case (y)
// ... your original case code with OF_Err/UF_Err renamed to of_Err/uf_Err
endcase
end
always #(posedge Clock, negedge Resetn) // style difference, I prefer the clock to be first
begin
if (Resetn == 1'b0)
begin
y <= A;
OF_Err <= 1'b0;
UF_Err <= 1'b0;
end
else if (Enable)
begin
y <= Y;
OF_Err <= of_Err;
UF_Err <= uf_Err;
end
end
assign c[1:0] = y[1:0];
Because OF_Err and UF_ERR are driven by multiple always blocks.
A reg should be driven by only one always block. And if it is having
multiple drivers by design, then it should be a wire.
Here is your modified code.
module state_machine (s, Enable, Clock, Resetn, c, OF_Err, UF_Err);
input [2:0] s;
input Enable, Clock, Resetn;
output reg [1:0] c;
output reg OF_Err = 0, UF_Err = 0;
reg [2:0] y, Y;
parameter [2:0] A = 3'b000, B = 3'b001, C = 3'b010, D = 3'b011, E = 3'b100;
always #(s, y, Enable, negedge reset)
begin
if (!reset)
begin
OF_Err = 0; //Problem
UF_Err = 0; //Problem
end
else
begin
if (Enable)
begin
case (y)
A: if (s == 3'b000) Y = B;
else
begin
Y = A;
UF_Err = 1;
end
B: if (s == 3'b000) Y = C;
else if (s == 3'b001) Y = A;
else
begin
Y = B;
UF_Err = 1;
end
C: if (s == 3'b000) Y = D;
else if (s == 3'b100) Y = C;
else Y = B;
D: if (s == 3'b000) Y = E;
else if (s == 3'b100) Y = D;
else Y = C;
E: if (s == 3'b000)
begin
Y = E;
OF_Err = 1;
end
else if (s == 3'b100) Y = E;
else Y = D;
default: Y = 3'bxxx;
endcase
c[1] = y[1];
c[0] = y[0];
end
end
end
always #(negedge Resetn, posedge Clock)
begin
if(Resetn == 0)
y <= A;
else
y <= Y;
end

How to test primality in Verilog?

I have the Verilog code shown below, and if I try to compile it I get an error message. The point is that I'm trying to manipulate an input, which as long as I know cannot be done in Verilog. The point is that I need check the following condition in Verilog:
static int prime(unsigned long long n)
{
unsigned long long val = 1;
unsigned long long divisor = 5;
if (n == 2 || n == 3)
return 1;
if (n < 2 || n%2 == 0 || n%3 == 0)
return 0;
for ( ; divisor<=n/divisor; val++, divisor=6*val-1)
{
if (n%divisor == 0 || n%(divisor+2) == 0)
return 0;
}
return 1;
}
At the moment I have the following code:
module prime(clk, rst, start, A, ready, P);
input clk, rst, start;
input [7:0] A;
output ready, P;
reg ready, P;
wire [7:0] divisor;
assign divisor = 5;
wire [7:0] val;
assign val = 1;
always # (posedge clk or posedge rst) begin
if (!rst) begin
P <= 0;
end
else if (start) begin
case (A)
0 : P <= 1;
1 : P <= 1;
2 : P <= 1;
3 : P <= 1;
endcase
if (A%2 == 0 && A != 2) begin
P <= 0;
end
else begin
for( ; divisor <= A/divisor; val=val+1, divisor=6*val-1) begin
if (A%divisor == 0 || A%(divisor+2) == 0) begin
P <= 0;
end
end
// need to set P to 1
end
end
end
endmodule
Please also note I need to test primes in the form of 6n+1 or 6n-1, and I also need to assume in my code that 0 and 1 are also primes.
If I try the above code I get an error message saying:
Enhanced FOR loop is not enabled for verilog
If anyone can help me solve the error and finish my logic in Verilog, I would be glad.
The Verilog BNF does not allow empty or compound statements in for(;;). Change the file to *.sv to compile it under SystemVerilog rules. Otherwise change your for loop statement to have simple statements
for( divisor =5; divisor <= A/divisor; divisor=6*val-1) begin
if (A%divisor == 0 || A%(divisor+2) == 0) begin
P <= 0;
end
val++;
end
Also, you can't make procedural assignments to wires. make them variables.

How to check unknown logic in Verilog?

I'm checking primality of a number in a form of 6n+1 or 6n-1. I have the below code, but it doesn't seem to be generated correct result.
module prime(clk, rst, start, A, ready, P);
input clk, rst, start;
input [7:0] A;
output ready, P;
reg ready, P;
reg [7:0] divisor;
reg val;
always # (posedge clk or negedge rst) begin
if (!rst) begin
P <= 1'bx;
end
else if (start)
begin
case (A)
-1 : P <= 1;
0 : P <= 1;
1 : P <= 1;
2 : P <= 1;
3 : P <= 1;
5 : P <= 1;
endcase
if (A%2 == 0 && A != 2) begin
P <= 0;
ready <= 1;
end
else if(A > 6)
begin
for(divisor = 5; divisor <= A/divisor; divisor=divisor+6) begin
if (A%divisor == 0 || A%(divisor+2) == 0) begin
P <= 0;
end
end
if (P != 0 && P == 1'bx) begin // need to fix
P <= 1;
end
end
end
end
endmodule
The point is that this part doesn't seem to be working:
if (P != 0 && P == 1'bx)
How can I check whether a variable includes unknown logic, meaning x. Checking as the above
P == 1'bx
doesn't seem to be working.
You should rather use case equality operator (===), which tests 4-state logical equality, then logical equality operator (==).
For the == operator, the result is x, if either operand contains an x or a z.
For the === operator bits with x and z are included in the comparison.

Resources