Labaratory 4
Labaratory 4
Vidal
CpE_3D
CPE 314 INTRO TO HDL
LABORATORY 4
Behavioral Modeling in Verilog
Objective/s:
Procedure:
The Verilog code models a D flip-flop circuit that captures the input value of D on the falling edge of
the clock signal, with a reset feature that sets the output Q to 0. In my testbench, named TBblocking,
I initialize the clock, reset, and D signals. I start by activating the reset signal for 50 time units to
ensure Q is cleared to 0, then deactivate it to resume normal operation. Subsequently, I apply
different values to D: setting it to 1 for 100 time units, switching it to 0 for another 100 time units,
changing it back to 1 for 95 time units, and finally setting it to 0 again. The clock is toggled every 10
time units to maintain a regular clock cycle. Throughout the simulation, I use the $monitor function
to display the values of clk, reset, D, and Q, providing real-time insights into the flip-flop's behavior.
Running this simulation in a Verilog environment allows me to verify the flip-flop's response to
various input scenarios and validate its functionality.
C. Simulate the circuit and observe its behavior. Record the resulting output.
As you can see, Q updates on the falling edge of the clock and takes on the value of D.
A. Compare the outputs of the two circuits under the same input conditions.
The outputs of the two circuits are the same under the same input conditions because
even when they behave differently due to the nature of their assignments, there are no
dependencies between signals. The lack of dependencies between the signals in the
circuits resulted in both circuits having different executions but coming up with the
same output.
B. Discuss the differences in behavior due to the use of blocking and nonblocking
assignments.
After evaluating a+b, the result is immediately assigned to x, and only then can we
proceed with evaluating 2*a and assign the result to y. Meanwhile, when using non-
blocking assignments, the assignment does not occur immediately after evaluation
but at the end of the current time step, allowing evaluations to be done concurrently.
In this example, a + b and 2 * a would be evaluated simultaneously, and the
assignment of the respective results would occur at the end of the current time step
C. Explain how the execution order affects the timing of state updates.
Blocking Assignments: Blocking assignments execute in order of appearance in the code.
When there are multiple assignments in a single always block, the state of the signals in that
block would change before all assignments would be evaluated. This leads to unintended
feedback where a value of a signal immediately affects the following assignments within the
same block. For example, if a is assigned to b and then b is used for another assignment within
the same block, b will reflect the value of a at that exact moment, potentially leading to a race
condition.
Non-Blocking Assignments: Non-blocking assignments evaluate all the assignments first
before assigning them all to their respective signal at the end of the time step. This process
prevents earlier assignments from affecting the following assignments within the same block at
the same clock cycle making the timing of state updates more predictable. For example, if a and
b are both set based on inputs in a clocked process, b will reflect the previous value of a before
the clock edge, which is typically the desired behavior in sequential circuits.
i. What is the main difference in simulation output between blocking and nonblocking
assignments?
The difference between the simulation output between blocking and nonblocking
assignment, is that blocking assignments immediately update their outputs
sequentially which lead to unintended results especially in this case where
dependent variables are involved. Meanwhile, non-blocking assignments update
their outputs at the end of the time step. It allowed all updates to be processed in a
single cycle, making the output more predictable.
ii. In what situations would you prefer one over the other?
I would prefer blocking assignments for combinational logic since it needs immediate
results without concerning time. It is preferable in situations where the next
computations depend on the immediate result of the current computation.
Non-blocking assignment on the other hand would be preferred in sequential logic,
where calculations within a clock cycle must be completed before any outputs change.
Non-blocking assignments help prevent race conditions in these situations making the
design more intuitive.
Conclusions:
In this lab activity, we examined the differences between blocking and non-blocking assignments in
Verilog's behavioral modeling. We created a behavioral model of a D flip-flop using both assignment
types and observed how they affected the output. The results demonstrated that blocking
assignments update variables immediately after being evaluated, whereas non-blocking assignments
evaluate all the code within the block first and then update the variables simultaneously at the end of
the time step.