1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
[\<- Shannon's Expansion and FPGAs](7.md)
---
# Verilog basics
## Introductory Verilog concepts
### Verilog
- A language to specify hardware
- Hardware Description Language (HDL)
- Has many features of other programming languages but it does \*not\* execute sequentially like most languages do
- Can specify structure or behavior
- Structure is like an actual circuit; netlist
- Behavior would be like logic equations or truth table
- Can be synthesized into a specific structure
### Verilog module
- HW is always described inside a module
- Like a schematic
- Always at least one output, and then however many inputs the circuit consumes
- Referred to as ports
- Taken together this is called the interface
- Variable names are used to make connections, either from ports to internal circuitry or between circuit elements inside the module
---
## Describing structure of 2:1 mux
- Example: Inputs x1,x2, and s, and output f
- This is the interface
- Need to describe what's happening inside
- Need to describe how gates are connected
- Use Verilog primitives to describe structure
![diagram](8.1.png)
### Interfaces for primitves
- AND (2-input example):
```
module and(y,x1,x2);
input x1,x2;
output y;
```
- OR (4-input example):
```
module or (y,x1,x2,x3,x4);
input x1,x2,x3,x4;
output y;
```
- NOT (always just one input):
```
module not (y,x);
input x;
output y;
```
### Structural example
- A gate-level implementation of a 2:1 mux
- Ports connected by name association
- E.g., "k" connects the output of the NOT gate to one of the inputs to the first AND gate
```
module example1(x1, x2, s, f);
input x1, x2, s;
output f;
wire k, g, h;
not(k, s);
and(g, k, x1);
and(h, s, x1);
or(f, g, h);
endmodule
```
![diagram](8.2.png)
- Note that the declaration of the wires in the above code is optional, but makes for good habit
- If not explicitly declared, `k`, `g`, and `h` would be implicitly considered wires by Verilog
---
## Behavioral description of 2:1 mux
### Behavioral code
- Uses syntax like our boolean algebra equations
- `&` instead of `*`
- `|` instead of `+`
- `~` instead of `!`
- Keyword of `assign` means the equation is always evaluated, just like logic gates work
```
module example3(x1, x2, s, f);
input x1, x2, s;
output f;
assign f = (~s & x1) | (s & x2);
endmodule
```
![diagram](8.3.png)
---
## Behavioral description of a more complex module
- Describe this circuit, with outputs f, g, h
![diagram](8.4.png)
- Assign statements don't have to be in the order listed
- Specifying the behavior of a wire
- Also don't have to specify intermediate nodes
```
module example4(x1, x2, x3, x4, f, g, h);
input x1, x2, x3, x4;
output f, g, h;
assign g = (x1 & x3) | (x2 & x4);
assign h = (x1 | ~x3) & (~x2 | x4);
assign f = g | h;
endmodule
```
---
## Hierarchy in Verilog
### Hierarchical design
- Block diagram of two modules working together
- Outputs of adder module connected to inputs of display module
![diagram](8.5.png)
### Code for the adder
- What is the truth table for a circuit that treats two inputs as numbers and adds them together?
![diagram](8.6.png)
```
//An adder module
module adder(a, b, s1, s0);
input a, b;
output s1, s0;
assign s1 = a & b;
assign s0 = a ^ b;
endmodule
```
- Note that `^` is XOR in Verilog
### Code for the display driver
- Useful to work out the truth table in order to figure out the equations
```
//A module for driving a 7-segment display
module display(s1, s0, a, b, c, d, e, f, g);
input s1, s0;
output a, b, c, d, e, f, g;
assign a = ~s0;
assign b = 1;
assign c = ~s1;
assign d = ~s0;
assign e = ~s0;
assign f = ~s1 & ~s0
assign g = s1 & ~s0
endmodule
```
### Instantiating and connecting
- Note the "wire" declarations for the internal (to this module) connections between adder and display
- Variables not part of interface definition
```
module adder_display (x, y, a, b, c, d, e, f, g);
input x, y;
output a, b, c, d, e, f, g;
wire w1, w0;
adder U1(x, y, w1, w0);
display U2(w1, w0, a, b, c, d, e, f, g);
endmodule
```
---
[Number systems and adders ->](9.md)
|