for loops in
VerilogA for
loop in Verilog is similar to the for loop in C.
Most often a Verilog loop is used for combinational
logic, comprised of blocking assignments
(= as
opposed to <=). A
simple example is given in src/add_bits.v,
which computes the sum over all bits in a vector:
always @(a) begin
q = 0;
for (idx=0; idx<8; idx=idx+1) begin
q = q + a[idx];
end
endThe lines in this block are evaluated sequentially, one after the other. Imagine the successive evaluations laid out like dominos. Although they are evaluated in sequence, the whole sequence must complete – every domino must fall – before the next clock cycle arrives. After synthesis, the circuit looks like a lineup of combinational adders:
0 -->[+]-->[+]-->[+]-->[+]-->[+]-->[+]-->[+]-->[+]--> q
^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | |
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
Create a top module to
contain add_bits as a submodule
instance. In the top module, do
the following:
clk, load, (1 bit), and a (8-bits)q
(4 bits)reg named
_a (the underscore (_) distinguishes it as a separate wire
from the port a)wire named
_qadd_bits
module, and name the instance add_bits_instance_a to add_bits_instance.a_q to add_bits_instance.qalways block
with this behavior:
q <= _qif
load is 1, then assign _a <= aA testbench template is
provided in src/testbench.v.
Modify the testbench to test
your top module. Specifically,
you need to declare a wire for q, instantiate top, connect its ports, and make
additional $write and
$fwrite
statements to report q in the
log text. Simulate your design to verify that q is correct for several random values
of a.
When verified, create an XDC file (use Basys3_Master.xdc as a template) and
map a to the lower (right-most)
switches, load to btnU, and q to the lower 4 led signals. Edit build.tcl as needed, then
implement the design.
Open the timing report and note the WNS.
Program your design onto the Basys3 board and test the following cases:
a = 8'b0000_0001a = 8'b0101_0101a = 8'b1111_1111Save a photo of your board for each test case, with filenames case1, case2, and case3. Save the photo files with the
appropriate graphics file suffix (.png, .jpg, etc).
Turn in your work using git:
git add case* src/*.v *.rpt *.txt *.tcl *.bit *.xdc
git commit . -m "Complete"
git push origin mainIndicate on Canvas that your assignment is done.