Why does the following redeclaration error happen in verilog? - verilog

I'm trying to implement a simple verilog code as below:
module test1(
input ACLK,
input RST,
output test_output1,
output test_output2
);
//wire ACLK;
//wire RST;
reg test_output1;
reg test_output2;
assign test_output1 = ACLK;
always #(posedge ACLK or negedge RST)
begin
if(!RST)
begin
//test_output1 <=0;
test_output2 <=0;
end
else
begin
//test_output1 <=0;
test_output2 <=1;
end
end
endmodule
I get the following error message when I try to synthesize it in Xilinx ISE:
=========================================================================
* HDL Compilation *
=========================================================================
Compiling verilog file "test1.v" in library work
ERROR:HDLCompilers:27 - "test1.v" line 30 Illegal redeclaration of 'test_output1'
ERROR:HDLCompilers:27 - "test1.v" line 31 Illegal redeclaration of 'test_output2`
I am unable to resolve this error. Any help would be highly appreciated.

If you declare the directional of the port in the portlist, you must also declare the type. This is referred to as an ANSI style header.
There is also a non-ANSI style header that separates the portlist, directional, and type. If you are fallowing IEEE1364-1995 convention then you must use non-ANSI style and you cannot declare the type (e.g. output reg test_output2; is illegal, while output test_output2; reg test_output2; is legal). Since IEEE1364-2001 ANSI and non-ANSI style is supported (and the non-ANSI allows output reg test_output2;). All modern Verilog simulators are SystemVerilog (IEEE1800) simulators, therefore it is the designers choice. (ANSI style is more popular as it is less typing).
ANSI style header:
module test1(
input ACLK,
input RST,
output test_output1,
output reg test_output2 );
Non-ANSI style header:
module test1( ACLK, RST, test_output1, test_output2 );
input ACLK;
input RST;
output test_output1;
output test_output2;
reg test_output2;
Note: With IEEE1364, you can not drive a reg with an assign statement, it must be a net type. IEEE1800 has softened the rule the it is recommenced logic in stead of reg, but generally if you are going to use assign then you should be assigning a net (e.g. wire).

Add following modification:
You used test_output1 in assign statement so it should be of type wire.
module test1(
input wire ACLK,
input wire RST,
output wire test_output1,
output reg test_output2
);
You have already declared test_output1 and test_outpu2 as output and it is by default of type wire, so you just have to implicitly specify wire or reg according to usage,
// reg test_output1;
// reg test_output2;

Related

"Target of concurrent assignment or output port connection should be a net type"

I'm running into the following errors when trying to synthesize my code to run on my Anvyl board:
ERROR:HDLCompiler:329 - "C:/Users/Chase/Desktop/Code
Templates/final_bcd_counter.v" Line 25: Target <digit_1> of concurrent assignment or output port connection should be a net type.
ERROR:HDLCompiler:329 - "C:/Users/Chase/Desktop/Code
Templates/final_bcd_counter.v" Line 26: Target <digit_2> of concurrent assignment or output port connection should be a net type.
I was provided with a Lab_board.v file to drive the board which is as follows:
`timescale 1ns / 1ps
module lab_board(LED, SW, CLK);
output [7:0] LED;
input [7:0] SW;
input CLK;
bcd_count_7 counter(
.max_count(SW[6:0]),
.CLK(CLK),
.run(SW[7]),
.digit_l(LED[3:0]),
.digit_2(LED[7:4])
);
endmodule
The code that the errors are throw in is my final_bcd_counter.v file which is the main driver of the program that passes all the needed values to the board. It is as follows:
// This is the top module for the programmable BCD counter.
// It implements a programmable 7-bit counter and a binary-
// to-bcd converter that can output two digits.
module bcd_count_7(max_count, CLK, run, digit_1, digit_2);
input [6:0] max_count;
input CLK, run;
output reg [3:0] digit_1;
output reg [3:0] digit_2;
//Wires and registers for interconnect if needed
wire [6:0] countin_out;
// Programmable 7-bit counter module
prog_count_7 counter(.max_count(max_count),
.run(run),
.CLK(CLK),
.count_out(countin_out));
// Binary-to-BCD Converter for converting count_out to BCD
binary_bcd_2 bcd_converter(.bin_in(countin_out),
.digit_1(digit_1),
.digit_2(digit_2));
endmodule
I've tried changing the type of digit_1 and digit_2 with no avail. Could the solution be creating wires that connect to the lab board instead of passing output registers, if so, what would that look like?
Any help is appreciated. I can provide the code of the other modules in the program if needed.
Thanks!
You've declared digit_1/2 as a variable and it needs to be a net in Verilog I'm assuming those are output ports from your binary_bcd_2 module. SystemVerilog does not have this restriction.
Simply remove the reg keyword from the port declaration. I've added wire for clarity, but that is what is implicit
module bcd_count_7(
input wire [6:0] max_count,
input wire CLK, run,
output wire [3:0] digit_1,
output wire [3:0] digit_2
);

Displaying a bus in Verilog

I'm writing a code in Verilog which would count till 15 on every edge of a clock and would go back to 0. However, I'm not able to display waveforms. All I can see is Z (high impedance).
`timescale 1ns / 1ps
module Counter(
input ck,
output [3:0] a
);
reg a;
reg [3:0] i = 4'b0000;
always#(posedge ck)
begin
a = i;
if(i==15)
i = 0;
else
i = i+1;
end
endmodule
Here is the testbench to drive it:
module Counter_tb;
wire clock;
wire [3:0] ta;
Clocker mygate(.clk(clock));
Counter mygate2(.ck(clock), .a(ta));
initial
begin
$display(ta, clock);
end
endmodule
The waveform of clock is displayed properly, but not ta. What could be the possible mistake?
You declared a twice, once as an 4-bit output (inferred wire type), and once a an internal single bit reg. The way you did it is non-compliant with the IEEE standard because is does not follow the ANSI or non-ANSI style. Some simulators may allow what you did and work correctly, others throw compiling errors (strict IEEE compliance), and some get confused. I'm guessing the latter is what happened with your simulator; maybe there was a warning message in your compile log you over looked.
Get rid of the line reg a; and change output [3:0] a to output reg [3:0] a to make it compatible with an ANSI style header. With an ANSI portlist style, the ports direction, type, width, and name are declared on the same name.
module Counter(
input ck,
output reg [3:0] a
);
The Alternative is the Non-ANSI style (not recommended), which is required one line for declaring the port order, another for the direction & size, and a optional additional line to make it a reg. A proper Non-ANSI style header below.The Non-ANSI style header is required with IEEE1364-1995. ANSI was added and became the recommended styles since IEEE1364-2001, with non-ANSI supported so legacy code could continue being used.
module Counter(ck, a);
input ck;
output [3:0] a;
reg [3:0] a;
Other note, flops should be assigned with non-blocking (<=) assignments, instead of blocking (=) assignments. At minimum change a = i; to a <= i; to get in the practice of proper coding style that will avoid race conditions in the verilog scheduler.
You call "$display" once at time 0. What you are expecting to see?
Try to change $display to "$monitor(ta, clock);".

Quartus Error (10028) with memory

My goal is to form this code with Quartus, but the problem is I don't know how to fix the problem.
I've tried to know what Error 10028 means, but I cant figure out how to work with it.
Can someone teach me how to fix it or if there's a way to bypass it?
module mem (r_wb,addr,d,q);
input r_wb;//0write 1read
input [7:0] addr;
input [7:0 ] d;
output [7:0] q;
reg [7:0] q;
reg [7:0] mem_bank [0:255];
always #(r_wb)
if (r_wb) q=mem_bank[addr];
else mem_bank[addr]=d;
always #(addr)
if (r_wb) q=mem_bank[addr];
else mem_bank[addr]=d;
always #(d)
if (r_wb) q=mem_bank[addr];
else mem_bank[addr]=d;
endmodule
The code you put in your comment is mostly correct in the fact that you do need a clocking signal. However you should be using non-blocking assignment (<=).
I would recommend changing your model header to ANSI style which has been around since 2001. The non-ANSI style required q the be identified three times; port list, direction, type. ANSI style compacts it. Non-ANSI is good to understand because a lot of synthesizers and code generators still use it by default. But any modern simulator or synthesizer will accept ANSI style as input.
module mem (
input clk,
input r_wb, //0write 1read
input [7:0] addr,
input [7:0 ] d,
output reg [7:0] q) ;
reg [7:0] mem_bank [0:255];
always #(posedge clk)
if (r_wb) q<=mem_bank[addr];
else mem_bank[addr]<=d;
endmodule

localparam after wire declaration

For a very strange reason (scripts we use) I need to be able to declare a localparam AFTER I declare wires and regs in a module:
module blah (clk, rst, in, out);
input clk;
input rst;
input [2:0] in;
output [3:0] out;
wire res;
localparam NUMBER=5;
...
is this legal verilog code? I would also appreciate a link to the relevant seciton in the documentation. Thanks!
This is valid Verilog (2001). Verilog 2001 saw the introduction of localparam, for all versions it is still syntactically valid to use parameter in this context. localparam indicates that it can not be overridden.
Usage can be seen in section 23.10 Overriding module parameters of SystemVerilog IEEE Std 1800-2012.
From IEEE 1800-2012:
For example:
module generic_fifo
#(MSB=3, LSB=0) // parameter port list parameters
(input wire [MSB:LSB] in,
input wire clk, read, write, reset,
output logic [MSB:LSB] out,
output logic full, empty );
parameter DEPTH=4; // module item parameter
localparam FIFO_MSB = DEPTH*MSB;
localparam FIFO_LSB = LSB;
// These constants are local, and cannot be overridden.
// They can be affected by altering the value parameters above
logic [FIFO_MSB:FIFO_LSB] fifo;
logic [LOG2(DEPTH):0] depth;
always #(posedge clk or posedge reset) begin
casez ({read,write,reset})
// implementation of fifo
endcase
end
endmodule
Exactly. As per the Verilog IEEE Std 1364-2001, you can use localparam in your Verilog code. It can be declared after wire declaration, no problem for that.

How can i make my verilog shifter more general?

Here i have a shifter but as of rite now it only works for up to 3 bits. I've been looking and i can't find out how to make it work for up to 8 bits.
module shifter(a,b,out);
input [7:0] a, b;
output [7:0] out;
wire [7:0] out1, out2, out3;
mux_8b_2to1 first(a[7:0], {a[3:0],a[7:4]}, b[2], out1);
mux_8b_2to1 second(out1[7:0], {out1[5:0],out1[7:6]}, b[1], out2);
mux_8b_2to1 third(out2[7:0], {out2[6:0],out2[7]}, b[0], out);
endmodule
What you have is a Barrel Shifter. Two ways to make it more generic are make it a functional model (still synthesis-able) or structural model with a generate block. Both approaches follow IEEE Std 1364-2001 (aka Verilog-2001).
The functional generic approach for a barrel shifter only needs a down-shifter. The general function is out = {in,in} >> (WIDTH-shift) where leftover bits can be ignored. To protect for double-roll (i.e. shift > WIDTH ), use the mod operator on the shift (WIDTH-(shift%WIDTH)).
module barrel_shifter_functional #( parameter CTRL=3, parameter WIDTH=CTRL**2 )
( input wire [WIDTH-1:0] in,
input wire [ CTRL-1:0] shift,
output wire [WIDTH-1:0] out );
assign out = {2{in}} >> (WIDTH-(shift%WIDTH));
endmodule
The structural generic approach for a barrel shifter needs a generate block. The for loop in the generate block will unravel at compile time, not run time like a for loop like in an always block. To keep it generic also have have the 2-to-1 mux have a parametrized width. FYI, you can use the generate block with functional code too, for example comment out the mux_2to1 instantiation and uncomment the assign statement below it. Learn more about the generate block by reading IEEE Std 1800-2012 ยง 27. Generate constructs.
module barrel_shifter_structeral #( parameter CTRL=3, parameter WIDTH=CTRL**2 )
( input wire [WIDTH-1:0] in,
input wire [ CTRL-1:0] shift,
output wire [WIDTH-1:0] out );
wire [WIDTH-1:0] tmp [CTRL:0];
assign tmp[CTRL] = in;
assign out = tmp[0];
genvar i;
generate
for (i = 0; i < CTRL; i = i + 1) begin : mux
mux_2to1 #(.WIDTH(WIDTH)) g(
.in0(tmp[i+1]),
.in1({tmp[i+1][WIDTH-(2**i)-1:0],tmp[i+1][WIDTH-1:WIDTH-(2**i)]}),
.sel(shift[i]),
.out(tmp[i]) );
// assign tmp[i] = shift[i] ? {tmp[i+1][WIDTH-(2**i)-1:0],tmp[i+1][WIDTH-1:WIDTH-(2**i)]} : tmp[i+1];
end : mux
endgenerate
endmodule
module mux_2to1 #( parameter WIDTH=8 )
( input wire [WIDTH-1:0] in0, in1,
input wire sel,
output wire [WIDTH-1:0] out );
assign out = sel ? in1 : in0;
endmodule
Both examples are functionally equivalent and synthesize provided CTRL is less than or equal to the ceiling of log2(WIDTH). Synthesis will likely give different results. The generate method will exclusively use 2-to-1 muxes while the pure functional method will depend on the quality of the optimizer.
Working example # http://www.edaplayground.com/s/6/500
I've used the >> and << operators to generate a synthetizable design using ISEWebPack, as this:
module shifter(
input wire [7:0] a,
input wire [7:0] b,
input wire leftright, // 0=shift right, 1=shift left
output reg [7:0] out
);
always #* begin
if (leftright==0)
out = a>>b;
else
out = a<<b;
end
endmodule
This way, the symthesis tool will know that you want to implement a shifter and can use its own macros to best synthetize it:
Synthesizing Unit <shifter>.
Related source file is "shifter.v".
Found 8-bit shifter logical right for signal <out$shift0002> created at line 30.
Found 8-bit shifter logical left for signal <out$shift0003> created at line 32.

Resources