FDS (Only Verilog)
FDS (Only Verilog)
– Design Entry: The journey begins with the design entry, where the initial con-
cept is articulated. This stage leverages intuition and experience, often utilizing
Schematic Capture for graphical representations or Hardware Description Lan-
guages (HDLs) like Verilog and VHDL for textual descriptions.
– Logic Synthesis: The design is then transformed into a logic gate structure,
where HDL codes are converted into networks of logic gates. This process not
only mirrors the intended circuit functionality through logic expressions but also
optimizes the design for speed, size, and power efficiency.
– Functional Simulation: This phase verifies the logical correctness of the design
by simulating logic functions. Assuming perfect gates, it generates timing wave-
forms for detailed analysis, ensuring the design operates as expected under ideal
conditions.
– Physical Design: Subsequently, the focus shifts to the physical layout, where
the logic expressions are mapped onto a chip using available components. This
involves the strategic placement of components and routing connections between
them.
– Timing Simulation: This step is crucial to ensure the design meets all timing
constraints, accounting for the physical realities of circuit implementation.
– Circuit Implementation: The final step is the circuit’s physical realization,
where the design is brought to life in hardware form.
75
CHAPTER 10. DIGITAL LOGIC AND VERILOG (PART V) (W6.1)
and nor
nand buf
xor not
or xnor
Examples
- AND Gate:
1 and and1 (out, in1, in2);
- OR Gate:
1 or or1 (out, in1, in2);
- NOT Gate:
1 not not1 (out, in);
- NAND Gate:
1 nand nand1 (out, in1, in2);
76
Notes by Ali EL AZDI
Naming Rules:
Case Sensitivity:
Style Guidelines:
Comments:
(words in purple are reserved keywords, words after two slashes are comments)
Port Declaration:
- Syntax:
1 port\_direction data\_type [port\_size] port\_name;
77
CHAPTER 10. DIGITAL LOGIC AND VERILOG (PART V) (W6.1)
- Vectors: Specify bit-width for multi-bit signals (e.g., [3:0] for 4 bits).
Examples:
Modules should be defined in the same source file, or the Verilog compiler must know
the locations of the modules.
Notes: ∗ module name and instance name are any valid identifiers.
∗ .port name specifies the subcircuit’s port to be connected.
78
Notes by Ali EL AZDI
Written in Verilog :
1 module adder4 (Cin, A, B, S, Cout);
2 // Port definitions
3 input Cin;
4 input [3:0] A, B; // 4-bit vectors
5 output [3:0] S; // 4-bit vector
6 output Cout;
7
8 // Intermediate signals
9 wire [3:1] C; // 3-bit vector for carry bits
10
11 // Design implementation using full-adder stages
12 fulladd stage0 (.c_in(Cin), .a(A[0]), .b(B[0]), .s(S[0]), .c_out(C[1]));
13 fulladd stage1 (.c_in(C[1]), .a(A[1]), .b(B[1]), .s(S[1]), .c_out(C[2]));
14 fulladd stage2 (.c_in(C[2]), .a(A[2]), .b(B[2]), .s(S[2]), .c_out(C[3]));
15 fulladd stage3 (.c_in(C[3]), .a(A[3]), .b(B[3]), .s(S[3]), .c_out(Cout));
16 endmodule
79
Chapter 11
Multiplexers, or MUXes, direct signals from several input lines to a single output line. They
select which input to transmit based on control signals.
80
Notes by Ali EL AZDI
e w f
0 0 Z
0 1 Z
1 0 0
1 1 1
e w f e w f
e w f
0 0 0 0 0 1
0 0 Z
0 1 1 0 1 0
0 1 Z
1 0 Z 1 0 Z
1 0 1
1 1 Z 1 1 Z
1 1 0
81
CHAPTER 11. DIGITAL LOGIC AND VERILOG(PART VI)(W6.2)
82
Notes by Ali EL AZDI
Value Meaning
0 Logic 0
1 Logic 1
x Unknown logic value or don’t care
z tri-state, high-impedance
size is the number of bits, radix is the base of the number, and constant is the value of the
number. Supported radices include:
Radix Meaning
d decimal default if no radix is specified
b binary
h hexadecimal
o octal
11.5.3 Parameters
Parameters are constants that can be used in the module. They are defined as follows:
1 parameter name = value;
eg.
1 parameter c = 8;
83
CHAPTER 11. DIGITAL LOGIC AND VERILOG(PART VI)(W6.2)
11.5.4 Nets
Nets are used to connect modules. There are two net types:
wires tri-states
1 wire [3:0] a; 1 tri z1
Operator Description
& Bit-wise AND
— Bit-wise OR
ˆ Bit-wise XOR
˜ Bit-wise NOT
Structural
1 module my_circuit_structural (
2 input x1, x2, x3, x4,
3 output f
4 );
5 wire w1, w2, w3, w4, w5, w6, w7,
w8;
6 and (w1, x1, x3);
7 not (w2, x2);
8 not (w3, x3);
9 and (w4, x2, x4);
10 or (w5, x1, w3);
11 or (w6, w2, x4);
12 or (w7, w1, w4);
13 and (w8, w5, w6);
14 or (f, w7, w8);
15 endmodule
84
Notes by Ali EL AZDI
Behavioral
1 module my_circuit_behavioral (
2 input x1, x2, x3, x4,
3 output f
4 );
5 wire w7, w8;
6 assign w7 = (x1 & x3) | (x2 & x4);
7 assign w8 = (x1 | ˜x3) & (˜x2 | x4);
8 assign f = w7 | w8;
9 endmodule
Definition
Continuous assignments are used in Verilog for assigning values to wires. The assign state-
ment makes the left-hand side (LHS) of the assignment dynamically reflect any changes in the
right-hand side (RHS) immediately.
Properties
- Executes continuously: The assignment reacts to any change in the RHS variables and
updates the LHS accordingly.
- Operates in parallel: The execution order in the code does not a↵ect the simulation
behavior.
Example
Consider the case where we want to perform a logical OR operation between two signals, w7
and w8, and assign the result to signal f:
1 assign f = w7 | w8;
85
CHAPTER 11. DIGITAL LOGIC AND VERILOG(PART VI)(W6.2)
Structure
1 always @*
2 [begin]
3 [procedural assignment statements]
4 [if-else statements]
5 [case statements]
6 [while, repeat, and for loops]
7 [task and function calls]
8 [end]
Structure
1 if (expression1)
2 begin
3 statement;
4 end
5 else if (expression2)
6 begin
7 statement;
8 end
9 else
10 begin
11 statement;
12 end
86
Notes by Ali EL AZDI
1 case (expression)
2 alternative1:
3 begin
4 statements;
5 end
6 alternative2:
7 begin
8 statements;
9 end
10 default:
11 begin
12 statements;
13 end
14 endcase
87
Chapter 12
- The sensor detects an undesirable event and sends a SET signal (logic high or 1) to the
alarm control circuit.
- The alarm control circuit activates the alarm when it receives the SET signal. The
ALARM ON signal becomes 1 (logic high), indicating that the alarm is active.
- The alarm remains active until a RESET signal (logic high or 1) is sent to the alarm
control circuit, turning o↵ the alarm (ALARM ON becomes 0).
- A memory element in the circuit ensures that the alarm remains active until it is explic-
itly reset, even if the SET signal goes back to low (0).
88
Notes by Ali EL AZDI
Storage elements in a circuit hold logic signal values, defining the circuit’s state. When inputs
change, the circuit may maintain its current state or transition to a new state. Consequently, the
circuit evolves through various states over time based on input changes.
Q Q
0 1
1 0
While this already allows the most basic form of memory, it’s not very practical, the given
value cannot be changed, and it’s not very reliable.
89
CHAPTER 12. DIGITAL LOGIC AND VERILOG(PART VII) (W7.2)
S R R̄ · S Qa Qnext Qb,next
0 0 0 Qa Qa Q̄a
0 1 0 0 0 1
1 0 1 Qa 1 0
1 1 0 0 0 0
This is how the next state of the latch is calculated, the current state of the latch is Qa and
Qb and the next state is Qnext and Qb,next respectively.
Qa,next = R + Qb = R + S + Qa
= R · (S + Qa )
= R · S + R · Qa = Qnext
Qb = S + Qa
S R R̄ · Qa Qa,next Qb,next
0 0 Qa Qa Qa
0 1 0 0 1
1 0 0 1 0
1 1 0 1 1
Qnext = S + R̄ · Qa
90
Notes by Ali EL AZDI
12.2.2 Flip-Flops
Personal Remark. Put simply, the first type requires actual C input to determine when to
update, while the other type updates Q in response to an event-driven signal, such as a clock
signal (refer to the following section about Clock signals for a clearer explanation).
D Flip-Flop
Flip-flops update outputs at specific times, unlike latches. A D flip-flop captures the input
from the D signal only during certain clock signal transitions, ensuring stable and predictable
outputs. Once the clock triggers (on a rising or falling edge), the output Q immediately
matches the input D and maintains this value, una↵ected by any further input changes, until
the next clock edge.
D Flip-Flop sensitive to the rising edge D Flip-Flop sensitive to the falling edge
12.3 Clock
Clocks are essential in digital systems for synchronization purposes. The timing of state
changes within the system is orchestrated by these clocks.
91
CHAPTER 12. DIGITAL LOGIC AND VERILOG(PART VII) (W7.2)
12.3.2 Waveform
The waveform of a clock signal can be described as follows:
• Period: The period of a clock signal is the duration of one complete cycle of the waveform
and is typically measured in seconds or fractions thereof (like nanoseconds, ns).
It is denoted by TCLK .
• Frequency: The frequency of a clock signal is defined as the number of complete cycles it
performs per second. Typical units of measurement are Hertz (Hz).
1
Calculated as f = TCLK , where TCLK is the period.
• Duty Cycle: The duty cycle is the percentage of the period during which the clock signal
Thigh
remains in the high state (often 50%). Calculated as D = TCLK ⇥ 100%.
The transition points of the clock signal, especially the rising edge, often trigger state changes
within the digital system, coordinating operations and ensuring the system functions correctly
and predictably.
Blocking Assignment
For example: Here, B is assigned the value of A, then C is assigned the value of B, and finally, D
is assigned the value of C. At the end, A = B = C = D. This is used to model combinational
logic (without memory).
1 always @ (...sensitivity list...)
2 begin
3 B = A;
4 C = B;
5 D = C;
6 end
Nonblocking Assignment
For example: Changes occur at the same time, so B is assigned the value of A, C is assigned the
old value of B, and D is assigned the old value of C. This is used to model sequential logic (with
memory).
92
Notes by Ali EL AZDI
always(*) Blocks
(=) Block Statements should be used.
The always @* block is used to model combinational logic, where the output depends only on
the current input values. It is sensitive to any change in the input signals.
1 // Example: AND gate
2 always @ (*)
3 begin
4 C = A & B;
5 end
93
CHAPTER 12. DIGITAL LOGIC AND VERILOG(PART VII) (W7.2)
A D Latch with Enable and Asynchronous Clear is a type of digital storage device used in
electronic circuits to store a single bit of data. It has a data input (D), a clock input (C), an
enable input (CE), and a clear input (CLR). The output (Q) reflects the input data (D) when
both the clock (C) and enable (CE) inputs are active (high). The asynchronous clear (CLR) has
the highest priority; when activated (high), it immediately resets the output (Q) to 0, regardless
of other input conditions. Thus the Verilog code for this is:
1 module Dlatch (input D, input C, input CE, input CLR,output reg Q);
2 always @ (*)
3 begin
4 if (CLR == 1)
5 Q <= 0;
6 else if ((C == 1) && (CE == 1))
7 Q <= D;
8 end
9 endmodule
Positive-Edge-Triggered D FF
D Flip-Flop with positive-edge-triggered clock input. The always block is executed on the positive
(rising) CLK edge Q gets set to D
Works the same as before but the use of posedge here means that the always block is also
executed if the CLR signal is high immediately without waiting for the next clock edge.
1 module Dff (input D, input CLK, input CLR, output reg Q);
2 always @ (posedge CLK or posedge CLR)
3 begin
4 if (CLR == 1)
5 Q <= 0;
6 else
7 Q <= D;
8 end
9 endmodule
Note: It would be a mistake to omit posedge and execute the always block on any change in CLR.
On a 1-to-0 transition on CLR, the else clause would execute and set Q to D even though no
CLK edge had occurred
94
Notes by Ali EL AZDI
Keywords Summary
Now what we’ve seen is just a composition of the following keywords, here’s a summary of them:
Asynchronous Clear: Allows the immediate resetting of the latch or flip-flop output to a low state (0),
regardless of the clock or other control signals’ states.
Enable: An additional control input that allows the latch to either accept new data when high (enabled)
or maintain its current state when low (disabled).
Positive-Edge Triggered: Indicates that the flip-flop updates its output only at the rising edge of
the clock signal. This specificity in timing helps to synchronize the data flow in digital systems,
reducing ambiguity and ensuring stability.
Synchronous Preset: Similar to asynchronous clear, but the preset operation occurs in sync with the
clock. This feature sets the output to a high state (1) during the clock’s active edge.
Complementary Output: Provides an additional output that is the logical inverse of the main output.
This feature simplifies the design of circuits that require the inverse logic state, reducing the need
for external inverting components.
95
CHAPTER 12. DIGITAL LOGIC AND VERILOG(PART VII) (W7.2)
Instead, use only D flip-flops and combinational logic. Implement separate always blocks,
one set for simple D flip-flops and another for potentially complex combinational logic.
Latches (hold of the last value due to bad timing, and if the last value is the initial one, it
must be defined) can inadvertently arise in combinational circuits. To prevent this, ensure all
variables in always@(*) blocks are assigned initial default values, ensuring consistent logic
states before proceeding to specific assignments.
In sequential logic, always employ nonblocking assignments using the <= operator in always
blocks to ensure simulation consistency across multiple blocks, as this method evaluates all
right-hand sides before assigning values to the left-hand sides.
96
Chapter 13
- Mealy Machine: The output depends on the current state and the current input.
All state transitions are made in sync with clock edges (rising or falling). Personal Remark:
Mnemothecnics: the double o in Moore: Only Output State. (uhm couldn’t find anything better)
97
CHAPTER 13. DIGITAL LOGIC AND VERILOG(PART VIII) (W8.1)
- The state memory is a set of n flip-flops that store the current state all connected to the
same clock (thus updating simultaneously once per clock period).
Note: The sensitivity list of the state machine may be ajusted as we’ve seen
98
Notes by Ali EL AZDI
- The state memory is a set of n flip-flops that store the current state all connected to the
same clock (thus updating simultaneously once per clock period).
Note: The sensitivity list of the state machine may be ajusted as we’ve seen
99
CHAPTER 13. DIGITAL LOGIC AND VERILOG(PART VIII) (W8.1)
Algorithm:
Three steps :
Step 1: We determine the next-state and output functions eg. f() and g()
Step 2: Use f() and g() to contruct a state/output table that shows the
entirety of the next state and the output of the circuit for all possible inputs
and states.
Step 3 (Optional): Draw the state diagram of the state machine using the
state/output table.
Step 3 (Optional): The state diagram is the simplest conceptual method to describe the
behavior of a sequential circuit. It is a graph representing circuit states as nodes and the
transitions between states as directed edges.
Example 1
Let’s analyze the state machine below:
100
Notes by Ali EL AZDI
Excitation
State variables Outputs
D2D1
x
Q2 Q1 z
0 1
0 0 10 10 1
0 1 10 10 1
1 0 01 11 0
1 1 10 10 1
101
CHAPTER 13. DIGITAL LOGIC AND VERILOG(PART VIII) (W8.1)
Example 2
Let’s analyze the state machine below:
Excitation
State variables Outputs
D2D1
x
Q2 Q1 Q0 z
0 1
0 0 0 000 001 1
0 0 1 001 010 0
0 1 0 010 011 1
0 1 1 011 100 0
1 0 0 100 101 1
1 0 1 101 110 0
1 1 0 110 111 1
1 1 1 111 000 0
102
Notes by Ali EL AZDI
103
CHAPTER 13. DIGITAL LOGIC AND VERILOG(PART VIII) (W8.1)
Example
Consider that we need to design a trafic light controller Let TA , TB : Signals from the trafic
sensors; active when there is traffic LA , LB : Signals for controlling traffic lights
104
Notes by Ali EL AZDI
Q⇤1 = D1 = Q1 Q0 + Q1 Q0 + Q1 Q0 = Q0 Q1
Q⇤0 = D0 = Q1 Q0 TA + Q1 Q0 TB
105
CHAPTER 13. DIGITAL LOGIC AND VERILOG(PART VIII) (W8.1)
...
Construct output table; write logic
expressions
Output Encoding Current State, S Outputs
GREEN 00 Q1 Q0 LA1 LA0 LB1 LB2
YELLOW 01 0 0 0 0 1 0
RED 10 0 1 0 1 1 0
1 0 1 0 0 0
1 1 1 0 0 1
1 module traffic_light_ctrl (input TA, TB, CLR, CLK, output reg [1:0] LA, LB)
;
2 reg [1:0] D, Q;
3 // Next-state logic
4 always @ (*) begin
5 D[1] = (˜Q[1] & Q[0]) | (Q[1] & ˜Q[0]);
6 D[0] = (˜Q[1] & ˜Q[0] & ˜TA)
7 | (Q[1] & ˜Q[0] & ˜TB);
8 end
9 // State memory
10 always @ (posedge CLK or posedge CLR)
11 begin
12 if (CLR == 1)
13 Q <= 0;
14 else
15 Q <= D;
16 end
17 // Output logic
18 always @ (*) begin
19 LA[1] = Q[1]; LA[0] = ˜Q[1] & Q[0];
20 LB[1] = ˜Q[1]; LB[0] = Q[1] & Q[0];
21 end
22 endmodule
106
Chapter 14
Note:
107
CHAPTER 14. DIGITAL LOGIC AND VERILOG, PART IX (W8.2)
Shift registers shift their n-bit value one bit to the left or right.
108
Notes by Ali EL AZDI
This visually shows the shi↵ting at each clock cycle. (The number to be shifted is the one on
the first column of the table), the one to be outputted is the one on the last column.
TCLK IN Q3 Q2 Q1 Q0 (OUT)
t0 1 0 0 0 0
t1 0 1 0 0 0
t2 1 0 1 0 0
t3 1 1 0 1 0
t4 1 1 1 0 1
t5 0 1 1 1 0
t6 0 0 1 1 1
t7 0 0 0 1 1
The timing diagram for this shift register, numbers in base 16 represent the parallel input and
output (each number is actually a 4-bit number input/output to D1,D2,D3,D4)
109
CHAPTER 14. DIGITAL LOGIC AND VERILOG, PART IX (W8.2)
110
Notes by Ali EL AZDI
14.4 Counters
Simple circuits that increment or decrement their value by 1, often used in various applications
such as counting occurrences of events or as timers, can be realized as add/sub circuits, though
that approach is generally considered overkill.
Di = Qi (Q0 Q1 · · · Qi 2 Qi 1 EN )
Modulo 2n counter:
111
CHAPTER 14. DIGITAL LOGIC AND VERILOG, PART IX (W8.2)
112
Notes by Ali EL AZDI
113
CHAPTER 14. DIGITAL LOGIC AND VERILOG, PART IX (W8.2)
Operator Description
! logical not
== logical equality
!= logical inequality
&& logical and
|| logical or
114
Notes by Ali EL AZDI
These equality operators have the same precedence level, which is lower than that of relational
operators.
115
Chapter 15
116
Notes by Ali EL AZDI
Timing Intervals:
Sequential logic elements’ outputs connect to inputs of other sequential logic elements.
For correct operation, all FFoutput-to-FFinput paths must meet timing constraints.
Setup-Time Constraints
117
CHAPTER 15. DIGITAL LOGIC AND VERILOG (PART X) (W9.1-W10.1)
Hold-Time Constraints
Hold-time constraints ensure stability immedi-
ately after the clock edge:
- Clock-to-Q delay,
tCQ : 0.8 ns tCQ 1.0 ns
1. Starts when data is loaded into the FF on the rising clock edge.
1 1
fmax = = ⇡ 370.37 MHz
Tmin 2.7 ns
118
Notes by Ali EL AZDI
Hold-Time Violations
To check for hold-time violations, we examine the shortest path between the rising clock edge
and the change of input D:
tmin thold
1.9 ns 0.4 ns
Clock-to-Q delay,
tCQ : 0.8 ns tCQ 1.0 ns
The longest path is t(Q0 , D3 ) = tCQ,max +3·tAN D +tXOR = 1.0 ns+3·1.2 ns+1.3 ns = 5.9 ns
The shortest clock period that satisfies the setup-time requirements is:
119
CHAPTER 15. DIGITAL LOGIC AND VERILOG (PART X) (W9.1-W10.1)
Hold-Time Violations
To check for hold-time violations, we examine the shortest paths between the rising clock edge
and the change of one of the FF inputs:
tmin thold
2.1 ns 0.4 ns
15.2 Metastability
120
Notes by Ali EL AZDI
For this part, I took some liberties in the way I organized the ideas presented
by the professor as I found that presenting the same idea twice, with then without
clock skew, was quite lengthy and redundant. Of course, feel free to take a look at
the Digital Logic and Verilog X (extended), if you feel like the professor’s approach
was better.
121
CHAPTER 15. DIGITAL LOGIC AND VERILOG (PART X) (W9.1-W10.1)
122
Notes by Ali EL AZDI
123
CHAPTER 15. DIGITAL LOGIC AND VERILOG (PART X) (W9.1-W10.1)
Clock skew is defined as the di↵erence in arrival times of the clock signal at di↵erent flip-flops:
tskew = D S
124
Notes by Ali EL AZDI
The signal ‘D‘ undergoes a lifecycle within a digital system that repeats every clock cycle. This
lifecycle can be described as follows: Personal Remark: Basically what you should retain from
this section
1. Born as Q of the Source Flip-Flop
(FF): The signal D starts as the output
(Q) of a source flip-flop.
This cycle, adjusted for clock skew, ensures the continuous flow of data through the digital
system but may a↵ect timing and performance.
125
CHAPTER 15. DIGITAL LOGIC AND VERILOG (PART X) (W9.1-W10.1)
126
Notes by Ali EL AZDI
1
tCQ,max + tcomb,max + tsetup tskew TCLK =
fCLK
1 1
tCQ,max + tcomb,max + tsetup tskew = tskew =
fmax,skew=0 fmax
- Positive skew improves the max frequency:
1 1
> ) fmax > fmax,skew=0
fmax,skew=0 fmax
What ifs...
- Q1: What if the circuit cannot work correctly at the desired frequency due to
unsatisfied setup-time constraints?
1
tCQ,max + tcomb,max + tsetup tskew TCLK =
fCLK
A1: Redesign the combinational logic to reduce the combinational path delay; try to
increase the clock skew. In practice, we let computer-aided design tools do this for us.
1
tCQ,max + tcomb,max + tsetup tskew TCLK =
fCLK
1 1
tCQ,max + tcomb,max + tsetup tskew = tskew =
fmax,skew=0 fmax
Positive skew improves the max frequency:
1 1
> ) fmax > fmax,skew=0
fmax,skew=0 fmax
127
CHAPTER 15. DIGITAL LOGIC AND VERILOG (PART X) (W9.1-W10.1)
- Q2: What if the circuit cannot work correctly due to unsatisfied hold-time
constraints?
A2: Insert bu↵ers on the short combinational paths to increase their delay; try to reduce
the clock skew. In practice, we let computer-aided design tools do this for us.
Recall:
tCQ,min + tcomb,min ( D S) thold
Substituting tskew = D S:
Basically...
3 - Find the shortest clock period TCLK that satisfies the setup-time constraints of all those
paths
1
4 - Compute fmax = TCLK
128
Notes by Ali EL AZDI
FF timing parameters
0.8 ns tcQ 1 ns
0 =0 1 =0 2 =0 3 = 2 ns
Note: For simplicity, we will assume that EN is available at the rising clock edge.
- Q0 to D3
- Q1 to D3
- Q2 to D3
- Q3 to D3
129
CHAPTER 15. DIGITAL LOGIC AND VERILOG (PART X) (W9.1-W10.1)
Step 2a - For all paths, find the longest combinational path delay
- Q0 to D3:
tcomb = 3 ⇥ tAND + tXOR = 3 ⇥ 1.2 +
1.3 = 4.9 ns
- Q1 to D3:
tcomb = 2 ⇥ tAND + tXOR = 2 ⇥ 1.2 +
1.3 = 3.7 ns
- Q2 to D3:
tcomb = tAND + tXOR = 1.2 + 1.3 =
2.5 ns
- Q3 to D3:
tcomb = tXOR = 1.3 ns
Step 2b - Find the shortest clock period that satisfies the path’s setup-time
constraint
Remember to include clock skew in the expressions
Clock period that satisfies other paths is determined by the path with the longest combi-
national delay:
130
Notes by Ali EL AZDI
Step 2b - Given the clock delay to the source FF, verify if hold-time constraint
is satisfied
tcQ,min + tcomb,min ( D S) thold
- Q0 to D3: 0.8 + 4.7 (2 0) =
3.5 ns > 0.4 ns
131
Chapter 16
132
Notes by Ali EL AZDI
w1 w0 y0 y1 y2 y3
0 0 1 0 0 0
0 1 0 1 0 0
1 0 0 0 1 0
1 1 0 0 0 1
y0 = w 1 w 0
y1 = w 1 w 0
y2 = w 1 w 0
y3 = w 1 w 0
En w1 w0 y0 y1 y2 y3
1 0 0 1 0 0 0
1 0 1 0 1 0 0
1 1 0 0 0 1 0
1 1 1 0 0 0 1
0 X X 0 0 0 0
y0 = En · w1 · w0
y1 = En · w1 · w0
y2 = En · w1 · w0
y3 = En · w1 · w0
133
CHAPTER 16. DIGITAL LOGIC AND VERILOG, PART XI (W10.2)
16.2 Memory
Personal Remark: Didn’t think the history of memory was worth mentioning (Slides 16-17,
Chapter XI) Memory types may vary in terms of:
134
Notes by Ali EL AZDI
- Writable or read-only
- Volatile or not: loses contents once the power supply is removed or not
(Out of Scope and Personal) For example, you might be aware of the di↵erence between RAM
and ROM. Random Access Memory (RAM) is volatile, meaning it loses its data when the power
is turned o↵, and is often used for fast runtime access. On the other hand, Read-Only Memory
(ROM) is non-volatile, meaning it retains its data even when the power is turned o↵, and is
typically used to store firmware or permanent software that does not need to be modified.
Memory capacity is the total number of data bytes it can store (important)
135
CHAPTER 16. DIGITAL LOGIC AND VERILOG, PART XI (W10.2)
- Ndwidth = 8
- data in = (B7)16
- we = 1
136
Notes by Ali EL AZDI
Read
- Ndwidth = 8
- we = 0
Memory read takes place and
mem[6] appears at data out port:
data out = mem[6] = (14)16
Memory write is not taking place
- Addressing: 2-bit address (addr[1:0]) selects one of 4 rows via 2-to-4 decoder.
- Data Output: 4-to-1 MUX selects data from addressed row to data out[3:0].
- The outputs of the address decoder, which enable one entire row of the memory array, are
called word lines
- The wires that carry data (input, output, or sometimes a shared in/out bus) are called bit
lines
137
CHAPTER 16. DIGITAL LOGIC AND VERILOG, PART XI (W10.2)
138
Notes by Ali EL AZDI
- Conditional operator ?: select one of the two alternate expressions (B, C) depending on
the value of a logical expression (A)
- If the logical expression (A) is true, it returns the first alternative (B)
139
Chapter 17
This is a small collection of examples of Finite State Machines (FSMs) and their implementation
in Verilog.
Personal Remark: Knowing what a Moore and Mealy machine is, is important to understand
this part of the course.
140
Notes by Ali EL AZDI
141
CHAPTER 17. DIGITAL LOGIC AND VERILOG - EXAMPLES - FSM (W10.2)
142
Chapter 18
This part of the course might seem a litttttle bit more advanced, as it applies the last few chapters
at a higher level.
You do need to recall two things, what a Tri-State Driver is (only allows current to flow when
the enable signal is active) and what a Bus with Tri-State drivers is, basically allows multiple
modules(things) to be connected to a single ”bus”, again, current of the bus only flows when the
enable signal is active.
Also, don’t expect understanding the thinking behind going from an Idea to an Actual Verilog
program without having done the exercices
IDLE: No swapping
R2TOR3: First copy
R1TOR2: Second copy
R3TOR1: Third copy
143
CHAPTER 18. DIGITAL LOGIC AND VERILOG (PART XII) (W11.1)
144
Notes by Ali EL AZDI
Now using the FSM controller, the bus and the reg, we can defined the swapping module as
such:
1 module regswap (clk, reset, swap);
2 parameter width = 8;
3 input clk, reset, swap;
4 wire wR1in, wR1out, wR2in, wR2out, wR3in, wR3out, wdone;
5 wire [width-1:0] wR1, wR2, wR3, wBus;
6
7 // Instantiate controller module
8 control controller_module ( .clk (clk), .reset (reset), .swap (swap),
9 .R1in (wR1in), .R1out (wR1out), .R2in (wR2in), .R2out (wR2out),
10 .R3in (wR3in), .R3out (wR3out), .done (wdone));
11
12 // Instantiate registers
13 regn #(.n (width)) R1 (.D (wBus), .clk (clk), .reset (reset), .en (
wR1in), .Q (wR1));
14 regn #(.n (width)) R2 (.D (wBus), .clk (clk), .reset (reset), .en (
wR2in), .Q (wR2));
15 regn #(.n (width)) R3 (.D (wBus), .clk (clk), .reset (reset), .en (
wR3in), .Q (wR3));
16
17 // Bus with tri-state drivers
18 bustri #(.n (width)) bustri1 (.w (wR1), .en (wR1out), .f (wBus));
19 bustri #(.n (width)) bustri2 (.w (wR2), .en (wR2out), .f (wBus));
20 bustri #(.n (width)) bustri3 (.w (wR3), .en (wR3out), .f (wBus));
21 endmodule
IDLE: No swapping
R2TOR3: First copy
R1TOR2: Second copy
R3TOR1: Third copy
145
CHAPTER 18. DIGITAL LOGIC AND VERILOG (PART XII) (W11.1)
146
Notes by Ali EL AZDI
Giving us the following implementation for reg swpping with a bus of MUXes :
147
CHAPTER 18. DIGITAL LOGIC AND VERILOG (PART XII) (W11.1)
- If a for loop is included in the generate block, the loop index variable must be of type
genvar.
- The generate construct enables combining for loops with module instantiations e↵ec-
tively.
148
Notes by Ali EL AZDI
18.3.1 Circuit
149
CHAPTER 18. DIGITAL LOGIC AND VERILOG (PART XII) (W11.1)
- Initialize the shift registers and the timer The Adder FSM has the following states:
(INIT input) - S0 = 0
- Load (in parallel) vectors A and B in their - S1 = 1
respective shift registers With S0 and S1 representing the carry
- Initialize the timer generated by the sum of the previous bits
- Clear the contents of the sum register (all We get the following State/output tables:
zeros)
- In each clock cycle
150
Notes by Ali EL AZDI
We have been writing Verilog models from their circuit drawing or their description and function-
ality, we will now be doing the inverse, given a Verilog model we will turn the model back to its
circuit and explain its functionality.
Enough with the talking, here’s the Verilog program we will be looking at:
151
CHAPTER 18. DIGITAL LOGIC AND VERILOG (PART XII) (W11.1)
1 // State register
2 always @(posedge clk or negedge resetn) begin
3 if (!resetn) S <= S1;
4 else S <= S_next;
5 end
6
7 // Output B register
8 always @(posedge clk or negedge resetn) begin
9 if (!resetn) B <= 0;
10 else if (lb) B <= 0;
11 else if (eb) B <= B + 1;
12 end
13
14 // Register A logic
15 always @(posedge clk or negedge resetn) begin
16 if (!resetn) A <= 0;
17 else if (la) A <= Data;
18 else if (ea) begin
19 for (k = n - 1; k > 0; k = k - 1) begin
20 A[k-1] <= A[k];
21 end
22 A[n-1] <= 1’b0;
23 end
24 end
25
Now, we can analyse this step by step, starting with the guesswhat declaration:
1 module guesswhat (clk, resetn, la,
s, Data, B, done); Step 1: Start by analyzing the module
2 parameter n = 8;
interfaces, parameters, and internal signals
3 input clk, resetn, la, s;
4 input [n-1:0] Data;
(wires) and variables (reg, integer).
5 output reg [3:0] B;
- The output, B, has a width di↵erent
6 output reg done;
7 parameter S1 = 2’b00, S2 = 2’b01, from the Data input.
S3 = 2’b10;
- S1,S2,S3, are likely states of an FSM
8 integer k;
9 reg [n-1:0] A; - integer k is likely a loop iterator
10 wire z;
11 reg [1:0] S_next, S; - and A is a register of the same width
12 reg ea, eb, lb; as Data input
13 //continues...
152
Notes by Ali EL AZDI
1 //...
2 always @(*) begin
3 ea = 0; The FSM Output Logic will likelt look
4 lb = 0; like this:
5 eb = 0;
6 done = 0;
7 case (S)
8 S1: lb = 1;
9 S2: begin
10 ea = 1;
11 if (A[0]) eb = 1;
12 else eb = 0;
13 end
14 S3: done = 1; - S1: lb = 1, others zero.
15 endcase - S2 : ea = 1, eb = A[0], others zero.
16 end - S3 : done = 1, others zero.
17 // continues...
153
CHAPTER 18. DIGITAL LOGIC AND VERILOG (PART XII) (W11.1)
Sequential Circuits
FSM State Register
- negedge in the sensitivity list:
resetn is an asynchronous reset active low,
1 // ... external signal.
2 always @(posedge clk or negedge - State after power-on reset: S1;
resetn) begin Hence, S1 is the default state.
3 if (!resetn) S <= S1;
4 else S <= S_next;
5 end
6 // continues...
- B + 1: up counter
Shift register A
Combinational Logic
Now that we have a broad idea of the circuit, we need to understand when it stops
154
Notes by Ali EL AZDI
1 //...
The last assignment in the module,
2 assign z = ˜| A; z =⇠ |A, uses the Verilog reduction NOR
3 endmodule operator, which sets z to 1 when all bits
4 // END of vector A (the output of the shift regis-
ter) are zero.
All of this leads us to the following circuit:
Operator Description
& reduction and
⇠& reduction nand
| reduction or
⇠| reduction nor
ˆ reduction xor
⇠ˆ reduction xnor
155
CHAPTER 18. DIGITAL LOGIC AND VERILOG (PART XII) (W11.1)
Examples
Consider the vector A = 80 b10101111:
- Example 1: z = &A
z = (((((((1 & 1) & 1) & 1) & 0) & 1) & 0) & 1) = 0
- Example 2: z = ⇠ˆA
z = ⇠(((((((1 ˆ 1) ˆ 1) ˆ 1) ˆ 0) ˆ 1) ˆ 0) ˆ 1) = 1
156