Calling a module with different Array element in verilog - verilog

I have input like that
a1 = 4'b 1001;
a2 = 4'b 1000;
a3 = 4'b 1111;
say I have a module fulladder that takes three inputs .
I need to call this module with all LSB of input that is like that
fulladder fa (a1[3] , a2[3] , a2[4] )
module full adder (a,b,c, carry , sum);
body -----
endmodule
it works fine with me !!
but but if the fulladder takes input value as array
like bellow then how I can call the module;
module fulladder2 (a , sum, carry)
input [2:0] a;
output sum;
output carry;
body-----
endmoduole
example
fulladder2 f2 (3'b.a(a1[3]a2[3]a2[4]) ?? it shows error can not declare ASCII in binary
or fulladder2 f2 (.a(a1[3],a2[3],a2[4]) ?? it also show error .
I can not convert the module fulladder2 like fulladder .

Use the concatenation operator {...}.
fulladder2 f2( {a1[3],a2[3],a3[3]}, ...
better:
fulladder2 f2 (
.a ( {a1[3],a2[3],a3[3]} ),
.sum (sum),
.carry (carry)
);
Also I noticed two different definitions where you have swapped the carry and sum.
module full adder (a,b,c, carry , sum);
and:
module fulladder2 (a , sum, carry)
That is not a problem if you use the .name(...) port assignment but it is a potential danger if you don't.
As to your nomenclature: what you use is called a 'vector' not an 'array'. In Verilog a one dimensional entity is called a vector. An array is normally two dimensional. A single bit is a wire or reg.

Related

Verilog [dot] meaning?

What does this " .depth_log2(7) " and .i_wclk mean in Verilog code?
asynch_fifo #(.depth_log2(7),
.data_width(22),
.rd_flop1_megedge(1'b1),
) USB2_ASYNCH_FIFO (
.i_wclk(i_usb2_sieclockin_ip),
);
I'm not able to understand what that .depth_log2 and .rd_flop1_megedge means
When you instantiate a module, such module might have some parameters. You can leave them at default, or you can initialize them at the values you prefer. In your example you are setting the depth at 7, the data width at 22 etc..
In general, if you have a verilog module like this:
module my_module
#( parameter P1 = 2,
parameter P2 = 0)
( input clk,
output reg [P1-1:0] out);
// Module logic
endmodule
You can instantiate it with the dot notation
wire wire_clk;
wire [2-1:0] wire_out;
my_module #(.P1(2),
.P2(3) ) u0
( .clk(wire_clk),
.out(wire_out);
This is called instantiation. Using this "." notation you are basically saying that you want to connect a constant 7 to depth_log2 parameter of your component asynch_fifo.

Does Verilog Module Instantiation Order Matter?

module parity (
a , // First input
b , // Second input
c , // Third Input
d , // Fourth Input
y // Parity output
);
// Input Declaration
input a ;
input b ;
input c ;
input d ;
// Ouput Declaration
output y ;
// port data types
wire a ;
wire b ;
wire c ;
wire d ;
wire y ;
// Internal variables
wire out_0 ;
wire out_1 ;
// Code starts Here
xor u0 (out_0,a,b);
xor u1 (out_1,c,d);
xor u2 (y,out_0,out_1);
endmodule // End Of Module parity
Suppose I have the module above. Does the order of the xor module declarations matter? If I reordered the declarations likes so:
xor u1 (out_1,c,d);
xor u2 (y,out_0,out_1);
xor u0 (out_0,a,b);
Would the synthesized circuit be the same?
The Verilog language is used to describe behavior of connected hardware-like elements and algorithms. The connections define how the elements are evaluated during simulation and how they are synthesized. The simulation scheduling (and hardware behavior) is based on the events which happen in the connection network.
Therefore, the order in which you instantiate those elements is irrelevant, if you connect them right. For example
module a(input i, output o);
endmodule
module b(input i, output o);
endmodule
module top(input i, output o);
a a1(i, t);
b b1(t, o);
endmodule
as soon as output of the module a a1 is connected to the input of module b b1, it will behave the same as in here:
module top(input i, output o);
b b1(t, o);
a a1(i, t);
endmodule
for readability reasons, you might prefer the first version though.

how to implement verilog divisible by 6?

I wanna create divisible by 6 module in verilog using divisible by 2 module and divisible by 3 module, I grasped the idea, but maybe my verilog grammar is wrong.
the module structure i want to implement is below,
module Divisible_6(input [3:0] a, output out);
using
module Divisible_2(
input [3:0] a,
output out
);
module Divisible_3(
input [3:0] a,
output out
);
and if input is 6(0110), then both divisible_2 and divisible_3 output 1,
and both 1 goes in to the AND gate, then came out with 1.
1 = true, 0 = false.
how can I implement the idea in verilog language?
and help is appreciated, thanks.
input: 1~15
Since I understand question a little bit different than #Morgan, I'd rather try this code:
module Divisible_6(
input [3:0] a,
output out
);
wire out_1, out_2;
assign out = out_1 & out_2;
Divisible_2(
.a (a),
.out(out_1)
);
Divisible_3(
.a (a),
.out(out_2)
);
endmodule
I assume that both Divisible_2 and Divisible_3 take 4-bit input and return boolean value (divisible (1) or not (0)).
You need divisible by 6 design, which can only be obtained, if your input is both divisible by 2 and divisible by 3.
Hence you can use both modules, like the following code.
// Defining the nets
logic out1, out2;
// Continuous Assignment
assign out = out1 & out2;
// Instantiation of 2 modules with instance name d1 & d2
divisible_2 d1 (a, out1);
divisible_3 d2 (a, out2);

Verilog Module Instantiation Syntax Clarification

I've been looking at some verilog code and came across something I've never seen before and have not been able to find information about online.
module FA_n_bit(c_out, Sum, A, B, c_in);
parameter word_size = 4; // the default size of this n bit adder
input [word_size-1:0] A, B;
input c_in;
output [word_size-1:0] Sum;
output c_out;
wire [word_size-1:0] c_inner;
// the c_out of the ith 1-bit full aderr is the c_in of the (i+1)th full adder
FA_one_bit fullAdder [word_size-1:0](
{c_out, c_inner[word_size-1:1]},
Sum,
A,
B,
{c_inner[word_size-1:1], c_in}
);
endmodule
I understand the parameter syntax, but I am having a hard time understanding what the FA_one_bit fullAdder [word_size-1:0] (...) syntax does.
any help would be greatly appreciated.
So far I think that its declaring 4 fullAdders but I get lost at the concatenation of the c_out and c_inner[word_size-1:1].
The FA_one_bit is another module instantiated inside FA_n_bit module. The instance name is fullAdder. Also, [word_size-1:0] indicates that word_size number of instances are created.
In Verilog, when you are instantiating a module, that means you are adding extra hardware to the board. Here, 4 fullAdders are added.
Concatenations are expressed using the brace characters { and }, with commas separating the expressions within. Referring to SystemVerilog LRM IEEE 1800-2012, section 11.4.12:
A concatenation is the result of the joining together of bits resulting from one or more expressions. The
concatenation shall be expressed using the brace characters { and }, with commas separating the expressions
within.
// if a, b and c are 8-bit numbers, the results has 24 bits
For Example: {a, b[3:0], c, 4'b1001}
Here, {c_out, c_inner[word_size-1:1]} means 1-bit of c_out and word_size-1 bits of c_inner from MSB are concatenated. This shall result in a signal of width word_size.
Yet another example from LRM:
{a, b[3:0], w, 3'b101}
// equivalent to the following
{a, b[3], b[2], b[1], b[0], w, 1'b1, 1'b0, 1'b1}
The MSB of concatenated signal is c_out and MSB-1 position is for c_inner[word_size-1] and LSB of signal is c_inner[1].
For more information on array of instances, refer to Array of modules link. Refer IEEE 1800-2012 section 11.4.12 for concatenation operator.

Is this code structure going in the right direction?

I am trying to utilize a 7 segment display. I have written a module which I want to take 4 inputs and change the hex output. There seems to be an issue with unpacked/packed arrays and I really don't know what on earth I'm doing. Any help much appreciated.
module hexDisplay(hex, c0, c1, c2, c3);
input c0;
input c1;
input c2;
input c3;
output hex[6:0];
reg out[6:0];
always#(*)
begin
case({c3, c2, c1, c0})
4'b0000:out [5:0] = 1;
// 0001-1111 go here
//...
default:out [6:0] = 0;
endcase
assign hex = out;
end
endmodule
Errors:
Error (10773): Verilog HDL error at lab2pre.v(55): declaring module ports or function arguments with unpacked array types requires SystemVerilog extensions
Error (10133): Verilog HDL Expression error at lab2pre.v(61): illegal part select of unpacked array "out"
Error (10133): Verilog HDL Expression error at lab2pre.v(62): illegal part select of unpacked array "out"
Error (10048): Verilog HDL error at lab2pre.v(64): values cannot be assigned directly to all or part of array "hex" - assignments must be made to individual elements only
Error (10137): Verilog HDL Procedural Assignment error at lab2pre.v(64): object "hex" on left-hand side of assignment must have a variable data type
Error (10044): Verilog HDL error at lab2pre.v(64): expression cannot reference entire array "out"
Error: Quartus II 64-Bit Analysis & Synthesis was unsuccessful. 6 errors, 1 warning
Error: Peak virtual memory: 959 megabytes
Error: Processing ended: Tue Feb 2 17:33:35 2016
Error: Elapsed time: 00:00:15
Error: Total CPU time (on all processors): 00:00:46
Error (293001): Quartus II Full Compilation was unsuccessful. 8 errors, 1 warning
2 Errors :
You need to have "packed" array rather than an "unpacked" array for
"out" & "hex" nets.
SystemVerilog supports both packed arrays and unpacked arrays of data.
The term packed array is used to refer to the dimensions declared
before the data identifier name. The term unpacked array is used to
refer to the dimensions declared after the data identifier name.
bit [7:0] c1; // packed array of scalar bit types
real u [7:0]; // unpacked array of real types
A packed array is a mechanism for subdividing a vector into subfields,
which can be conveniently accessed as array elements. Consequently, a
packed array is guaranteed to be represented as a contiguous set of
bits.
An unpacked array may or may not be so represented. A packed array
differs from an unpacked array in that, when a packed array appears as
a primary, it is treated as a single vector.
So in the code, you require, out & hex to be used as a continuous
bit vector, then it should be packed array, instead of unpacked
array.
Refer to topic 7.4 of the Systemverilog LRM.
assign statement to hex, cannot be with in always block. Because an
assign statement is used for modeling only combinational logic and it
is executed continuously. So the assign statement is called
'continuous assignment statement' as there is no sensitive list.
So it can't be within always block, which is executed as per
sensitivity list.
So your final working code is as below:
module hexDisplay(hex, c0, c1, c2, c3);
input c0;
input c1;
input c2;
input c3;
output [6:0] hex;
reg [6:0] out;
always#(*)
begin
case({c3, c2, c1, c0})
4'b0000:out [5:0] = 1;
// 0001-1111 go here
//...
default:out [6:0] = 0;
endcase
end
assign hex = out;
endmodule
Try something like this. Move the range specifiers ([6:0]) to the left of the signal names, and move the assign outside of the always block.
module hexDisplay(hex, c0, c1, c2, c3);
input c0;
input c1;
input c2;
input c3;
output [6:0] hex;
reg [6:0] out;
always#(*)
begin
case({c3, c2, c1, c0})
4'b0000:out [5:0] = 1;
// 0001-1111 go here
//...
default:out [6:0] = 0;
endcase
end
assign hex = out;
endmodule
whatever variable in always block must be reg , here you assign hex in always which is by default wire so if you assign hex at out side of always u will get compile free code.

Resources