0
\$\begingroup\$

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?

JYelton
35.7k34 gold badges149 silver badges281 bronze badges
asked Nov 21, 2023 at 8:59
\$\endgroup\$
1
  • \$\begingroup\$ After digging deep into Verilog, I mean seeing more Verilog examples. I think this question is obvious, both input a and input b are 16 bits. However, is this just a coding convention? \$\endgroup\$ Commented Nov 21, 2023 at 9:09

2 Answers 2

1
\$\begingroup\$

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.

answered Nov 21, 2023 at 9:18
\$\endgroup\$
0
1
\$\begingroup\$

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.

answered Nov 21, 2023 at 9:22
\$\endgroup\$
6
  • \$\begingroup\$ Thanks! So, in this example, the output is the direction and the type is omitted. \$\endgroup\$ Commented Nov 21, 2023 at 9:25
  • \$\begingroup\$ @mendax1234 the output is direction, yes. For type, 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 is dir [MSB:LSB] var_name. \$\endgroup\$ Commented Nov 21, 2023 at 9:29
  • \$\begingroup\$ @mendax1234 when a direction is given, type is assumed to be wire. For outputs you can also have output reg. \$\endgroup\$ Commented Nov 21, 2023 at 9:53
  • \$\begingroup\$ @TomCarpenter Okok. So, what's the difference between output reg and output 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\$ Commented Nov 21, 2023 at 10:22
  • \$\begingroup\$ @mendax1234 That's the only difference, the reg type can be assigned in a procedural block. \$\endgroup\$ Commented Nov 21, 2023 at 10:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.