Given the following code:
module add16 (input [15:0] a,b, output[15:0] sum, output court);
Are both input a
and input b
16 bits long, or is only input a
16 bits long?
2 Answers 2
The format is:
<direction> <type> [<packed size] list,of,var,names
The same principle applies to all variables.
So in your example as b
has no direction/type/size definitions, it will take the same definitions as the port that came before it, a
, hence both should be the same size and direction.
Personally I avoid this syntax an prefer to specify each ports direction, type and size individually. I find it clearer and avoids any ambiguity.
Until you specify a direction, a parameter "inherits" its size and direction from the previous one.
Re-write the module definition:
module add16 (
input [15:0] a, b,
output [15:0] sum,
output carryout
);
Should be clearer now.
Here b
"inherits" its direction and size from a
.
If you wrote output [15:0] sum, carryout
the carryout
would be 16-bit long. But defining another direction with output
allowed you to specify a size, 1-bit in this case.
-
\$\begingroup\$ Thanks! So, in this example, the
output
is the direction and thetype
is omitted. \$\endgroup\$mendax1234– mendax12342023年11月21日 09:25:15 +00:00Commented Nov 21, 2023 at 9:25 -
\$\begingroup\$ @mendax1234 the
output
is direction, yes. Fortype
, you normally don't have to specify a type for module inputs i.e. it's already in "bits" and you specify the size only. So the syntax isdir [MSB:LSB] var_name
. \$\endgroup\$Rohat Kılıç– Rohat Kılıç2023年11月21日 09:29:16 +00:00Commented Nov 21, 2023 at 9:29 -
\$\begingroup\$ @mendax1234 when a direction is given, type is assumed to be
wire
. For outputs you can also haveoutput reg
. \$\endgroup\$Tom Carpenter– Tom Carpenter2023年11月21日 09:53:20 +00:00Commented Nov 21, 2023 at 9:53 -
\$\begingroup\$ @TomCarpenter Okok. So, what's the difference between
output reg
andoutput wire
? I know that if I want to assign the output value in an always block, it needs to be a reg and it can't be the wire. Are there any other differences? \$\endgroup\$mendax1234– mendax12342023年11月21日 10:22:15 +00:00Commented Nov 21, 2023 at 10:22 -
\$\begingroup\$ @mendax1234 That's the only difference, the reg type can be assigned in a procedural block. \$\endgroup\$Tom Carpenter– Tom Carpenter2023年11月21日 10:31:29 +00:00Commented Nov 21, 2023 at 10:31
input a
andinput b
are 16 bits. However, is this just a coding convention? \$\endgroup\$