- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- Labs The future of collective knowledge sharing
- About the company

Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Vector assignment in Verilog
It is my first attempt to use verilog. I have defined an 8-bit bidirectional port, a data register and a data direction register in the following way:
It works perfectly, however, I'm sure, there must be an easier way to accomplish the same result.
I have tried with a for cycle:
as well as assigning the whole vector at once:
without any result. Thanks for any help.
You want a generate-for loop.
This gets unrolled into what you originally wrote.
- Thank you very much. I didn't know about generate-for loop, but after your answer and a bit more reading around I think I've got the difference between loop evaluated at compile time vs. runtime. – Luca Sep 20, 2017 at 3:42
Your Answer
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct .
Not the answer you're looking for? Browse other questions tagged verilog or ask your own question .
- The Overflow Blog
- What it’s like being a professional workplace bestie (Ep. 603)
- Featured on Meta
- Moderation strike: Results of negotiations
- Our Design Vision for Stack Overflow and the Stack Exchange network
- Temporary policy: Generative AI (e.g., ChatGPT) is banned
- Call for volunteer reviewers for an updated search experience: OverflowAI Search
- Discussions experiment launching on NLP Collective
Hot Network Questions
- Turned away for medical service at private urgent care due to being enrolled in medicare even with private insurance
- How to know which areas of a new song to sing softly and which areas to sing loudly?
- is it possible to turn this dumb-waiter into an elevator?
- Two ideal ohmmeters in parallel
- Unexpected tangency
- Can we simultaneously use the particles 地 and 得 with a single verb, e.g. 他高兴地跳得高?
- Is wearing a mask in public a no-no (making one looks weird)?
- Should I send an email to African American colleagues given a recent nearby targeted crime?
- Coworker hiding and stealing lab material
- AI tricks space pirates into attacking its ship; kills all but one as part of effort to "civilize" space
- How can I raise behavioral concerns as a candidate?
- Is this Linear Logic Prover Wrong?
- How to make scrollable TogglerBar?
- What is the purpose of 192.168.1.0?
- Does Blinking creature loss the benefit on attacking if the opponent can see invisible creature?
- How to find files that don’t have a suffixed version?
- What do Americans say instead of “can’t be bothered”?
- Why do programming languages use delimiters (quotes) for strings?
- polynomials and dimensions
- Sustainable eating habits as a pilot
- What adhesive solution should use for walls with powdery surface?
- Does Bayesianism give an out for pseudoscience that it shouldn’t deserve?
- Voltage Tolerance for Power Supplies
- Best practices for community choir audition with unknown song
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It only takes a minute to sign up.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Assigning to a parameterized 2d Verilog array
I have a Verilog array defined as :
I want to assign every array element with a vector that is all ones: "11...1". I tried this but I get an error:
What's the correct syntax ?
- system-verilog
2 Answers 2
To assign unpacked 2d array in SystemVerilog with a single line:
Verilog cannot be done in a single line. It must use a for-loop:

- \$\begingroup\$ Suppose: num_elements = 4 element_width = 8 Making it more complicated I want each element to get "0xFA" So now - my ONLY option is to use the for-loop approach ? There's no way to do it in a single line like in VHDL ? some_array <= ( others => ( others => x"FA" ) ) ; \$\endgroup\$ – shaiko Sep 7, 2020 at 9:02
- 1 \$\begingroup\$ @shaiko the SystemVerilog '{default: } approach will still work. Refer to the SystemVerilog LRM \$\endgroup\$ – Greg Sep 7, 2020 at 17:30
- \$\begingroup\$ It'll work for packed as well as unpacked array ? \$\endgroup\$ – shaiko Sep 7, 2020 at 17:45
Since some_array is all packed, you can do
- \$\begingroup\$ What if it was unpacked ? \$\endgroup\$ – shaiko Sep 6, 2020 at 20:10
- \$\begingroup\$ I'm looking for a syntax to assign each array element individually...similar to what I wrote in my example. \$\endgroup\$ – shaiko Sep 6, 2020 at 20:34
- 1 \$\begingroup\$ In your example, each element is a single bit. You might want to check out the default: clause of an array assignment pattern \$\endgroup\$ – dave_59 Sep 6, 2020 at 22:55

Your Answer
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct .
Not the answer you're looking for? Browse other questions tagged verilog system-verilog or ask your own question .
- The Overflow Blog
- What it’s like being a professional workplace bestie (Ep. 603)
- Featured on Meta
- Moderation strike: Results of negotiations
- Our Design Vision for Stack Overflow and the Stack Exchange network
Hot Network Questions
- What is the purpose of 192.168.1.0?
- Is it an Element?
- Convert a Gaussian integer to its positive form
- Pros and cons to round PCB design
- How can I find a list of Chrome's hidden urls with chrome://?
- What is the word for when a life event is coming to an end like a vacation and you feel an immense feeling of sadness due to not wanting to leave
- Small dents on bicycle frame
- Reward flight cancelled and denied rerouting
- Does Bayesianism give an out for pseudoscience that it shouldn’t deserve?
- Best practices for community choir audition with unknown song
- What is important to keep in mind in grading proof-based courses?
- Correcting how a student writes symbols
- What does the clown mean by saying that brothels in the cities "shall stand for seed" in the second scene of "Measure for Measure"?
- How to force a resource pack on a world?
- I'm liking to take it easy on the weekend
- How much monthly costs can I expect if I get a kitten or two?
- Does Blinking creature loss the benefit on attacking if the opponent can see invisible creature?
- Escape velocity when Sun is present
- Did "Joe the Plumber" perform plumbing work for money between when he left the Air Force and October 2008?
- Can we simultaneously use the particles 地 and 得 with a single verb, e.g. 他高兴地跳得高?
- Entire Perimeter of FPGA Getting Hot - Why?
- How you get any work done is beyond me?
- Feasibility of “invisible” orbital bombs
- What is the traction conversion between T and H vs A and AA traction on automobile tires?
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
Verilog Assignments
Variable declaration assignment, net declaration assignment, assign deassign, force release.
- Procedural continuous
Legal LHS values
An assignment has two parts - right-hand side (RHS) and left-hand side (LHS) with an equal symbol (=) or a less than-equal symbol (<=) in between.
The RHS can contain any expression that evaluates to a final value while the LHS indicates a net or a variable to which the value in RHS is being assigned.
Procedural Assignment
Procedural assignments occur within procedures such as always , initial , task and functions and are used to place values onto variables. The variable will hold the value until the next assignment to the same variable.
The value will be placed onto the variable when the simulation executes this statement at some point during simulation time. This can be controlled and modified the way we want by the use of control flow statements such as if-else-if , case statement and looping mechanisms.
An initial value can be placed onto a variable at the time of its declaration as shown next. The assignment does not have a duration and holds the value until the next assignment to the same variable happens. Note that variable declaration assignments to an array are not allowed.
If the variable is initialized during declaration and at time 0 in an initial block as shown below, the order of evaluation is not guaranteed, and hence can have either 8'h05 or 8'hee.
Procedural blocks and assignments will be covered in more detail in a later section.
Continuous Assignment
This is used to assign values onto scalar and vector nets and happens whenever there is a change in the RHS. It provides a way to model combinational logic without specifying an interconnection of gates and makes it easier to drive the net with logical expressions.
Whenever b or c changes its value, then the whole expression in RHS will be evaluated and a will be updated with the new value.
This allows us to place a continuous assignment on the same statement that declares the net. Note that because a net can be declared only once, only one declaration assignment is possible for a net.
Procedural Continuous Assignment
- assign ... deassign
- force ... release
This will override all procedural assignments to a variable and is deactivated by using the same signal with deassign . The value of the variable will remain same until the variable gets a new value through a procedural or procedural continuous assignment. The LHS of an assign statement cannot be a bit-select, part-select or an array reference but can be a variable or a concatenation of variables.
These are similar to the assign - deassign statements but can also be applied to nets and variables. The LHS can be a bit-select of a net, part-select of a net, variable or a net but cannot be the reference to an array and bit/part select of a variable. The force statment will override all other assignments made to the variable until it is released using the release keyword.


IMAGES
VIDEO
COMMENTS
1 Answer Sorted by: 2 You want a generate-for loop. genvar i; for (i = 0; i < 8; i = i + 1) begin assign pa [i] = (data_dir_reg [i]) ? data_reg [i] : 1'bZ; end This gets unrolled into what you originally wrote. Share Follow answered Sep 19, 2017 at 6:03 dave_59 39.1k 3 24 63 Thank you very much.
1 Answer Sorted by: 1 SystemVerilog has the bit fill literals '0, '1, 'x, and 'z. This means fill a vector with a digit to whatever width is required by the context. (In a self-determined context, it is just a single bit) You should write: assign x = '0; Share Cite Follow edited Sep 14, 2020 at 0:39 answered Sep 14, 2020 at 0:23 dave_59
1 To assign unpacked 2d array in SystemVerilog with a single line: some_array <= ' { default: '1 }; Verilog cannot be done in a single line. It must use a for-loop: for ( i = 0; i < num_elements; i = i +1) some_array [i] <= {element_width {1'b1}}; Share Cite Follow answered Sep 7, 2020 at 0:04 Greg 4,270 1 21 32
This is used to assign values onto scalar and vector nets and happens whenever there is a change in the RHS. It provides a way to model combinational logic without specifying an interconnection of gates and makes it easier to drive the net with logical expressions. // Example model of an AND gate wire a, b, c; assign a = b & c; Whenever b or c ...