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
Related
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.
This is a module for a 8 bit shift register using two 4 bit shift registers. 4 bit register modules works fine. (Just testing this for right shift for the moment)
module shift_reg_8 (Out,In,shift_left,shift_right,s0,s1,enable,clock);
output [7:0] Out;
wire [3:0]Least_Out,Most_Out;
input [7:0] In;
wire [3:0]Least_In,Most_In;
reg temp;
initial
temp = In[4];
assign {Most_In,Least_In} = In;
input shift_left,shift_right,enable,s0,s1,clock;
shift_reg least_sig_reg(Least_Out,Least_In,shift_left,shift_right,s0,s1,enable,clock);
shift_reg most_sig_reg(Most_Out,Most_In,shift_left,shift_right,s0,s1,enable,clock);
assign Out = {Most_Out,Least_Out};
assign Out[3] = temp;
endmodule
Here's the test bench.
module stimulus;
reg [7:0]INPUT;
reg ENABLE,CLOCK,S0,S1,SL,SR;
wire [7:0] OUTPUT;
shift_reg_8 my8shiftreg(OUTPUT,INPUT,SL,SR,S0,S1,ENABLE,CLOCK); // SL = Shift_Left, SR = Shift_Right, SO,S1 = Controls
initial
begin
CLOCK = 1'b0;
INPUT = 8'b01101110; ENABLE = 1;S0 = 0;S1 = 1;SL = 0;SR = 1;
#14 $display("Test 1 (Right Shift): INPUT = %b, S1 = %b, S0 = %b, SR = %b, OUTPUT = %b\n",INPUT,S1,S0,SR,OUTPUT);
end
always
#5 CLOCK = ~CLOCK;
initial
#100 $stop;
endmodule
This gives an output like this when simulated.
Test 1 (Right Shift): INPUT = 01101110, S1 = 1, S0 = 0, SR = 1, OUTPUT = 1011x111
what might be the problem?
Here's the shift_reg code...
module shift_reg(Out,In,shift_left,shift_right,s0,s1,enable,clock);
output [3:0] Out;
input [3:0] In;
input shift_left,shift_right,enable,s0,s1,clock;
reg [3:0] out_reg;
always #(posedge clock & enable)
begin
if ((s0 == 1'b0) && (s1 == 1'b0)) // Holding
begin
end
else if ((s0 == 1'b1) && (s1 == 1'b0)) // Left Shift
begin
out_reg[0] <= shift_left;
out_reg[1] <= In[0];
out_reg[2] <= In[1];
out_reg[3] <= In[2];
end
else if ((s0 == 1'b0) && (s1 == 1'b1)) // Right Shift
begin
out_reg[0] <= In[1];
out_reg[1] <= In[2];
out_reg[2] <= In[3];
out_reg[3] <= shift_right;
end
else if ((s0 == 1'b1) && (s1 == 1'b1)) // Loading
begin
out_reg <= In;
end
end
assign Out = out_reg;
endmodule
You are assigning out[3] twice.
assign Out = {Most_Out,Least_Out}; // Assigns all 8 bits
assign Out[3] = temp; // Assigns bit 3 again.
The multiple drivers is causing an unknown state.
I can't figure out , where this errors.Invalid use of input signal <ck> as target error is coming from?
module register
#(parameter Width = 8)
(output reg [Width-1:0] out,
input [Width-1:0] in,
input clear, load, clock);
always #(posedge clock)
if (~clear)
out<= 0;
else if (~load)
out<=in;
endmodule
module adder
#(parameter Width = 8)
(input [Width-1:0] a,b,
output [Width-1:0] sum);
assign sum = a + b;
endmodule
module compareLT // compares a < b
#(parameter Width = 8)
(input [Width-1:0] a, b,
output out);
assign out = a < b;
endmodule
module compareLEQ // compares a <= b
#(parameter Width = 8)
(input [Width-1:0] a, b,
output out);
assign out = a <= b;
endmodule
module roshanpoop
#(parameter Width = 8)
(input ck, reset,
input [Width-1:0] yln,
output [Width-1:0] y, x);
wire [Width-1:0] i, addiOut, addxOut;
wire yLoad, yClear, xLoad, xClear, iLoad,iClear;
register #(Width) I (i, addiOut, iClear, iLoad, ck);
register #(Width) Y (y, yIn, yClear, yLoad, ck);
register #(Width) X (x, addxOut, xClear, xLoad, ck);
adder #(Width) addI (addiOut, 'b1, i),
addX (x, y, addxOut);
compareLT #(Width) cmpX (x, 'b0, xLT0);
compareLEQ #(Width) cmpI (i, 'd10, iLEQ10);
fsm ctl (xLT0,iLEQ10 ,yLoad, yClear, xLoad, xClear, iLoad,iClear, ck, reset);
endmodule
module fsm
(input LT,LEQ, ck, reset,
output reg yLoad, yClear, xLoad, xClear, iLoad, iClear);
reg [2:0] cState, nState;
always #(posedge ck,negedge reset)
if (~reset)
cState <= 0;
else
cState <= nState;
always#(cState, LT,LEQ)
case (cState)
3'b00: begin //stateA
yLoad = 1; yClear = 1; xLoad = 1; xClear = 0;
iLoad = 1; iClear = 0; nState = 3'b001;
end
3'b001: begin // state B
yLoad = 1; yClear = 1; xLoad = 0; xClear = 1;
iLoad = 0; iClear = 1; nState = 3'b010;
end
3'b010: begin //state C
yLoad = 1; yClear = 1; xLoad = 1; xClear = 1;
iLoad = 1; iClear = 1;
if(LEQ) nState = 3'b001;
if(~LEQ & LT) nState = 3'b011;
if (~LEQ & ~LT) nState = 3'b100;
end
3'b011: begin //state D
yLoad = 1; yClear = 0; xLoad = 1; xClear = 1;
iLoad = 1; iClear = 1; nState = 3'b101;
end
3'b100: begin //state E
yLoad = 1; yClear = 1; xLoad = 1; xClear = 0;
iLoad = 1; iClear = 1; nState = 3'b101;
end
default: begin // required to satisfy combinational synthesis rules
yLoad = 1; yClear = 1; xLoad = 1; xClear = 1;
iLoad = 1; iClear = 1;nState = 3'b000;
$display("Oops, unknown state: %b", cState);
end
endcase
endmodule
error:
line no:70
Invalid use of input signal ck as target,
Invalid use of input signal target as target.
In module roshanpoop above mentioned error are coming . what might be the problem ?
The error is caused by this instantiation:
fsm ctl (xLT0,iLEQ10 ,yLoad, yClear, xLoad, xClear, iLoad,iClear, ck, reset);
of the module:
module fsm
(input LT,LEQ, ck, reset,
output reg yLoad, yClear, xLoad, xClear, iLoad, iClear);
You are using positional instantiation, which is not recomended, because it makes the task of maintaining your module more difficult (think, for example, if you want to add signals to your module: if you add it in the middle of the module's definition, all remaining signals will be wrongly connected).
Here, the use of positional instantiation has caused signal ck from the top module to be connected to iLoad, which is an output signal from fsm, so you are trying to put a value to a input only signal ck.
The way to have it right is to use explicit instantiation, where each signal from the module is explicitly named and assigned to a signal from the top module, like this:
fsm ctl (.LT(xLT0),
.LEQ(iLEQ10),
.yLoad(yLoad),
.yClear(yClear),
.xLoad(xLoad),
.xClear(xClear),
.iLoad(iLoad),
.iClear(iClear),
.ck(ck),
.reset(reset)
);
So, regardless of where in the argument list you put signal clk it will be always connected to the right signal inside the module.
Not an answer but some tips on potentially making the code easier to understand. I would post as a comment but code examples do not work well in comments.
1) Manual sensitivity lists can [should] be avoided when using a modern toolset.
always#(cState, LT,LEQ)
With an automatic sensitivity list would just be:
always #(*)
// Or
always #*
If you are able to use SystemVerilog (as question tags indicate) then the preferred method is :
always_comb
2) Instead of:
yLoad = 1; yClear = 1; xLoad = 1; xClear = 0;
iLoad = 1; iClear = 0;
For every case, we could have
reg [5:0] temp_control;
assign {yLoad, yClear xLoad, xClear, iLoad, iClear} = temp_control;
//...
always #*
case(cState)
3'b000: begin //stateA
temp_control = 6'b111010; nState = 3'b001;
end
3'b001: begin // state B
temp_control = 6'b110101; nState = 3'b010;
end
3'b010: begin //state C
temp_control = 6'b111111;
if(LEQ) nState = 3'b001;
if(~LEQ & LT) nState = 3'b011;
if (~LEQ & ~LT) nState = 3'b100;
end
//...
Better still create Mnemonics for the temp_controls.
localparam [5:0] CTRL_LOAD = 6'b111010;
localparam [5:0] CTRL_CLEAR = 6'b111010;
Mnemonics for the states are also really helpful:
localparam [2:0] STATE_INIT = 3'b000;
localparam [2:0] STATE_START = 3'b001;
localparam [2:0] STATE_STOP = 3'b010;
The the FSM structure might look some thing like:
always #*
case(cState)
STATE_INIT: begin //stateA
temp_control = CTRL_LOAD; nState = STATE_START;
end
STATE_START: begin // state B
temp_control = CTRL_CLEAR; nState = STATE_STOP;
end
STATE_STOP: begin //state C
temp_control = CTRL_HALT;
if(LEQ) nState = STATE_START;
if(~LEQ & LT) nState = STATE_RECYCLE;
if (~LEQ & ~LT) nState = STATE_CRUSH;
end
As the readability improves it is often easier to spot an incorrectly used signal.
Ok so I know my code works for a 3 number sequence but for with the finite state machine model I drew out this should be correct but it doesn't work for a 4 number sequence. It only detects the first 3. I need to detect with an overlap "0110" from the sequence 01100110110111. It should have 3 "0110" sequences and 2 overlapping but when I run my Verilog it detects 4 "0110" sequences which tells me it's only grabbing "011" Can someone look at my code and see what I'm doing wrong? I could simply add another state but I wouldn't think that's the correct way since I don't have another state in my diagram.
module moore_seq
(
input clock, reset, x,
output reg z
);
//assign binary encoded codes to the states A through D
parameter A = 2'b00,
B = 2'b01,
C = 2'b10,
D = 2'b11;
reg [1:0] current_state, next_state;
//Section 1: Next state generator (NSG)
always#(*)
begin
casex(current_state) //ignore unknown and Hi-Z inputs
A: if (x == 1)
next_state = A;
else
next_state = B;
B: if (x == 1)
next_state = C;
else
next_state = B;
C: if (x == 1)
next_state = D;
else
next_state = B;
D: if (x == 1)
next_state = A;
else
next_state = B;
endcase
end
//Section 2: Output Generator (OG)
always#(*)
begin
if(current_state == D)
z = 1;
else
z = 0;
end
//Section 3: The Flip Flops
always#(posedge clock, posedge reset)
begin
if(reset == 1)
current_state <= A;
else
current_state <= next_state;
end
endmodule
UPDATED:
parameter A = 3'b000,
B = 3'b001,
C = 3'b010,
D = 3'b011,
E = 3'b100;
reg [1:0] current_state, next_state;
//Section 1: Next state generator (NSG)
always#(*)
begin
casex(current_state) //ignore unknown and Hi-Z inputs
A: if (x == 1)
next_state = A;
else
next_state = B;
B: if (x == 1)
next_state = C;
else
next_state = B;
C: if (x == 1)
next_state = D;
else
next_state = B;
D: if (x == 1)
next_state = A;
else
next_state = E;
E: if (x == 1)
next_state = C;
else
next_state = B;
endcase
end
//Section 2: Output Generator (OG)
always#(*)
begin
if(current_state == E)
z = 1;
else
z = 0;
end
I suppose, four states is enough. Just change
//Section 2: Output Generator (OG)
always#(*)
begin
if(current_state == D)
to
//Section 2: Output Generator (OG)
always#(*)
begin
if(current_state == D && x == 0 )
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.