Professional Documents
Culture Documents
Lab Introduction:
In this first lab project, we’re going to be doing a lot of things, probably for the first time for
most. Our goal is to introduce the material, some of the tools, and the concepts that we’ll be
working with this term and that we’ll use to design and develop modern (embedded) digital
systems that we find in nearly every commercial product today.
We strongly encourage you to read the entire lab specification before starting any work. This
is important. However, do not feel over whelmed; we are here to work with you and we
certainly do not expect you to be an expert in everything by the end of this first project. We
do want you to start to become familiar with the ideas, though.
Lab Objectives:
So, that said, the
objectives of this lab are the following:
To begin to learn to work with data sheets for digital components.
To learn some of the behaviours of real world logic components and how these may
vary within the range of the specifications given in the data sheets. These really aren’t
like the textbook parts which are a good ideal model…
To begin to learn the Verilog Hardware Description Language
To begin to learn to use a modeling language like Verilog to aid in the design of
complex digital systems.
To learn to develop a test bench and to formulate then run tests on a gate-level
structural model of a system that we are designing. Note, we will use only structural
Verilog in all labs this term.
To begin to learn to move our design from the modeled version to a real world
implementation in a programmable part.
To introduce the Altera’s Terasic DE1 development board.
To learn that the job sometimes takes longer than we think it will.
Prerequisites:
You must have some typing and programming experience. A basic understanding of
electronics that you will have gotten in your physics classes. You should understand voltage,
- 1 of 20 -
current, resistance, and Ohm's law. You must also have some introduction to Boolean
algebra.
Cautions and Warnings:
Never try to run your circuit with the power turned off. Under such circumstances, the
results are generally less than satisfying.
Since current is dq/dt, if you are running low on current, raise your circuit board to about the
same level as the power supply and use short leads. This has the affect of reducing the dt in
the denominator and giving you more current.
If your circuit is turning on too slowly, lower your breadboard so that it is substantially
below the power supply. This enables the charge to get a running start before coming into
your board. It will now turn on much faster.
Throwing your completed but malfunctioning implementation on the floor, stomping on it,
and screaming ‘work you stupid fool, work’ is typically not the most effective debugging
technique although it is perhaps one of the more satisfying.
When you are debugging you circuit, wiring it, taking it apart, and rewiring again several
dozen times does little to fix what may be a design error. Such an approach is not highly
recommended, but, can keep you entertained for hours….particularly if you can convince
your partner to do it.
Sometimes - but only in the most dire of situations – sacrificing small animals to the smoke
demon living in your circuit does work. However, these are not included in your lab kit and
must be purchased separately from an outside vendor. Also, be aware that code gremlins are
not affected by such sacrifices.
Alternately, blaming your lab partner can work for a short time…until everyone finds out
that you are really to blame.
Always keep only a single copy of your Verilog code. This ensures that you will always
have a maximum amount of disk space available for games, email, and some interesting
pictures. If a code eating gremlin happens to destroy your only copy, not to worry, you can
always retype and debug it again.
Instruments and Tools for Digital System Design and Analysis
There are some helpful traditional instruments and tools that we will be
using in this lab and then when we start working as practicing engineers.
Digital Multimeter (DMM) / Digital Volt Meter (DVM)
A typical handheld DMM is shown in figure 1. The DMM is a very
useful instrument when you are analyzing electronic circuits. It can be
used to check whether or not a certain DC voltage is applied or has the Digital Voltmeter
expected value. Figure 1
- 2 of 20 -
Oscilloscope
A multimeter is a useful tool for looking at the value of a
(typically DC) signal. To be able to see how a signal is changing
in time or to analyze its waveform, we must use an oscilloscope.
A Tektronix instrument is shown in figure 2.
Altera Terasic DE1 Development Board
The Altera Development board is typical of the environments
with which one might work during the design, development, and
test of many contemporary digital systems. The environment
supports a variety of different kinds of components that, during Oscilloscope
the later phases of a project development, will be migrated to a Figure 2
system board that will be integrated into the completed design.
The picture in figure 3 shows the DE1 board with most of the major components highlighted.
We will be using the various input/output devices located directly on the board such as the
switches and LEDs. Further details will be provided in each lab. Take note of the large FPGA
(Field Programmable Gate Array) that is highlighted on the board. Later on in the term we
will be programming this and directly interfacing with many the devices on the board. Think
of it like a universal logic unit that all the devices can talk to. For now there is a program
loaded into the FPGA to allow you to use the Input/Output connectors on the board to make
the earlier labs easier.
Figure 3
The FPGA
FPGA is an acronym for Field Programmable Gate Array. Essentially, it is a large array of
logical elements that have been connected together. However, in an FPGA, the connections
between these logical elements can be programmed and reprogrammed. This means that it
can be used to build many different kinds of hardware all on the same chip.
- 3 of 20 -
OK, time to get to work…
Build
To implement the system, we are going to use the Altera Cyclone II FPGA on the DE 1
Board.
Although the Quartus tools support graphical entry, for our first design using the DE 1 Board
and Quartus environment, we are going to work with the Verilog code that we have
developed and tested up to this point.
All of that said, read through this section completely before starting your implementation…
Important First Steps
First scan through the DE1_user_manual:
This is a very good overview of the capabilities of the board and a handy
reference.
Start with and follow Tutorial 1in Appendix B of our class text to see how to get a
new project started (also see Recommended Design Approach below).
Specifically, first read sections B1, B2, and B4; then, from Appendix C, read
sections C1.1 and C1.2.
- 4 of 20 -
The same information is presented in a slightly different form, in the Altera
tutorial in the DE1 tutorials in the document: tut_quartus_intro_verilog.pdf.
Bear in mind, that the devices in the Altera tutorial (s) are different from ours,
they may reference the DE2 board, and there may be some information that we
are not using at the moment.
Go through this document to get a step-by-step guided tour of developing a
Verilog entry project in the Quartus environment.
After you have gone over the tutorials and other background material, you’re
ready to go.
- 5 of 20 -
Debug and Test
Verify that your design meets all the requirements in the original specification.
A C A C
B A B
AB C B AB C A B
00 0 00 0 0 1
AND Gate OR Gate Inverter
01 0 01 1 1 0
10 0 10 1
11 1 11 1
Figure 4
3. When you run the simulation. Does the output of the Verilog program agree or disagree
with your truth table?
4. Please explain the apparently incorrect order in which the results in the output log are
printed; specifically when the output line “Producing Glitch” prints.
5. Change the value of the delay in the program from 10 to 0. Rerun the simulation. Does
the output of the Verilog program agree or disagree with your truth table? Please explain
any differences.
6. Change the delay in the program back to 10 time units. Change the stimDelay in the test
module to 5 time units. What affect does this change have on the outputs of your
simulation?
7. What advantages are there to using a program like Verilog? Name three or more.
- 6 of 20 -
Laboratory Part 1 Report
Your report for part 1 must contain the following,
1. Each team member must include a copy his or her simulation results. Such results
must include source code, waveforms, and text file output.
Note, this does not mean one set of results duplicated three times.
2. One set of answers to all the questions from the team. The answers to the questions
must be typed. The diagrams must be done using a drawing tool such as Visio.
3. One copy of the design and simulation results for the Comparator.
SEL1
SEL1 SEL2 RESULT
0 0 R e s u lt = A and B
SEL2 0 1 R e s u lt = A or B
1 0 R e s u lt = A xor B
1 1 R e s u lt = 0
A
B
RESULT
Figure 5
Logic Diagram for the MultiFunction Logic Block
As we saw in the first model, the Verilog module names for these gates are the same as the
gate type, except they should be in lower case. The inverter in Verilog is called a not gate.
Follow the circuit diagram and the selection assignments shown above.
Create the source code for the gate-level (structural) model. To make life easier, look
to the sample Verilog code as an example.
Use common sense in creating signal names, a person with no prior knowledge of the
lab should be able to tell them apart.
- 7 of 20 -
Create a tester and a test bench then use these to confirm that the design implements
the truth table in table 1.
If the Verilog code from the Comparator design is used as a guide, do not use any time
delays, i.e.: omit the #delay from any gate instantiation. Use module, gate, and signal names
that reflect the current circuit, not those from the Comparator design.
Implementation and Test
After confirming the operation of the design, we now implement the circuit.
In the Quartus IDE, create a new project and enter your MultiFunction source code file. Be
certain not to include your test bench or test code. Assign pins to connect each of the system
inputs to one of the switches and, similarly, each of the outputs to one of the LEDs on the
DE1 board.
Confirm that the functionality of your design agrees with its truth table.
Laboratory Part 2 Report
Your report for part 2 must contain the following,
1. A copy the Verilog source code for the MultiFunction Logic Block, the testbench,
and the tester.
2. A copy of the simulation results.
- 8 of 20 -
since the single arm is making contact in either position. We see examples of such
switches in figure 6.
Pole Pole
Throws
Throw
Normally Closed
Normally Open
Wiper Wiper
Single Pole - Single Throw Single Pole - Double Throw Double Pole - Single Throw Double Pole - Double Throw
Figure 6
Switch Examples
In this lab, we will be using a DIP switch to assert our logic input states. It's also
important to remember that semiconductors - integrated circuits - are delicate and static
sensitive devices.
To use a DIP switch as a logic input, connect the switch as shown in figure 7. Observe
that we have a pull-up resistor shown. Such a resistor is used to ensure that we never
have an open input (nothing connected) to the circuit input when the switch is in the open
position.
When we start to study sequential circuit design in future
labs, we will encounter some problems that are Vcc
use a single pole - double throw switch. But for now, the
circuit input
setup as shown will be good enough.
Switch Closed - Logic 0
The DIP switch we will be using in the lab has the same Switch Open - Logic 1
330 ohms
Using an LED as an Output Device
LED is an acronym for a Light-Emitting Diode. An LED +
is a semiconductor device that emits light (much like a -
light bulb) when the voltage applied at the anode (+) is
Logic 0
larger than (beyond a threshold) what is applied at the
Figure 8
cathode (-). Under such conditions, a current will flow Driving an LED
from the anode to the cathode.
- 9 of 20 -
We must regulate the amount of current that flows into (yes, current actually flows in!) a
logic gate by connecting a current-limiting resistor. The value used is usually 330 ohm.
Shown in figure 8 is one method for connecting the LED to a TTL gate output.
LED's can be obtained in units or banks. We will be using an LED bank, which is called
a "LED Bargraph Display", as a set of indicators for this lab. The anode on the bargraph
displays is the side with the lettering.
To use LED's as indicators, active low circuitry should be used, meaning that the LED
will light up when the output from the circuit is at the "low" state.
Single In-Line Package (SIP) Resistors
For tasks such as pull-ups or termination, one often buys sets of resistors in a single in-
line package, or SIP. They come in various values and have various interconnections.
A package containing n resistors has n+1 pins where the
extra pin is the supply voltage (or sometimes, ground)
connection. Internally, an 8-pin, 7-resistor SIP has the
schematic in figure 9 (note the gray dot's position).
Vcc
Chip Pin-Numbering Convention Figure 9
Inside a SIP Resistor Package
To standardize circuit-wiring applications, there is a
pin-numbering convention for IC chips. One way of
marking chips is shown in figure 10. Notice that there is a small circle on one side but
not the other or round cut-away at the top. To the left of that is pin number 1. Along that
left side, the pin numbers increment and wrap around the end without the cutaway (or the
circle) until you reach the pin opposite pin 1. DIP's
usually come in packages of 14, 16, and 20 pins. 14 13 12 11 10 9 8 14 13 12 11 10 9 8
- 11 of 20 -
Compare these values from the data sheet with what you measured for V1 and V2?
Which configuration, that in figure 11 or that in figure 12, gave the brighter LED
display? Why?
- 12 of 20 -
Appendix A - Verilog Overview - The Verilog Hardware Description Language
What is Verilog?
Verilog is a hardware description language that provides a means of specifying a digital
system at a wide range of levels of abstraction. The language supports the early
conceptual stages of design with its behavioral level of abstraction, and the later
implementation stages with its structural level of abstraction. The language provides
hierarchical constructs, allowing the designer to control the complexity of a description.
Note: this description is an excerpt from the book Verilog Hardware Description
Language, by Thomas and Moorby.
endmodule
The inputs_list and the outputs_list are optional, but you will use them most of the
time. The standard convention in Verilog is that the outputs_lis comes before the
inputs_list when the module is declared. For each item in the inputs_list and the
- 13 of 20 -
outputs_lis, the input and output declarations must be made following the module
declaration.
The syntax is as follow:
input inputs_list;
output outputs_list;
The variable types reg and wire can also be formed into a bus such as:
reg [msb:lsb] reg_list;
wire [msb:lsb] wire_list;
where msb is the bit index of the most significant bit, and lsb is the bit index of the
least significant bit. Verilog can actually handle a reverse-ordered notation, but it is
better to follow a convention and stick to the traditional method. The value of the lsb
bit index needs to be zero always since bit position 0 conventionally denotes the least-
significant bit. This bus specification can be extended to input and output lists.
Putting Together a Circuit - The Behavioral Model and The Gate-Level Model
Once you have declared your module and variables, you then need to define the circuit.
There are two ways to define a circuit. You can specify the gates and/or other modules
and all the interconnections in between. That is the gate-level model. Instead, if you just
want to see how a circuit would behave through a set of equations and instructions, you
can use the behavioral model. For the pre-lab, you will be typing in only the gate-level
structural model.
If you require a physical definition of your circuit right before producing a mask for a
production chip, and you need to make sure that the logic is correct, the gate-level model
should be used. However, if you simply want to prototype an idea for a circuit just to see
how it works and do not want to spend time verifying interconnections, the behavioral
model is preferred.
Simulation Time Keeping and Time Units
Since the analysis of circuit timing is such an important issue in the design of digital
circuits, Verilog has its own time-keeping units. It is not in any particular time unit, but
unit time in Verilog can be equated to, say, 10 nanoseconds (10 ns) in real time. The idea
is to run conceptual simulations on a circuit, so you are not constrained by the ordinary
time unit convention. If you have a clock running in your source code, you can set one
half-period of that clock to one time unit in Verilog, as long as it is the smallest resolution
of simulation time needed.
- 14 of 20 -
Using the time-keeping units provided in Verilog, we can assign propagation delays to
the gates and modules that are declared in a Verilog source code. The # symbol functions
as a delay assignment operator. Note, where ever the # symbol appears in the sample
source file, you will type in the proper values for your delay. Good coding style
recommends using symbolic constants instead of what are called magic numbers.
always and initial
These are two of the many keywords in Verilog which allow you to set stimuli (user-
set input conditions) to the module that you created. always pertains to a continuous
process, and initial pertains to a process that is initiated at start-up .
Multiple always and initial statements can be put together to have a group of events
happening in a circuit.
These are behavioural statements. We can use these to model more complex flow of
control schemes.
$display and $monitor statements
Both of these statements allow you to see the states of certain signals in your module
in text form. The output is directed to the screen (or window). The difference between
the two statements is that $display only happens when it is called, whereas
$monitor shows the states only when there is a transition in one or more of the
signals that are specified. Here is the syntax:
$display (["string_format"], variable_list);
$monitor (["string_format"], variable_list);
The string_format is optional for both statements. You can format how the variables
will show up by using format fields (%b for binary, %d for decimal, and %h for
hexadecimal). As a convention, a logic "high" is denoted as a 1, and logic "low" is
denoted as a 0, and an unknown state is denoted as an x.
These state output statements must be placed within an always or initial routine.
Graphical Output Display
The $display and $monitor statements provide ways to verify the behavior of your
digital design. However, looking at a stream of numbers and trying to decide if your
circuit is running correctly can be very tiresome. To make the circuit verification (and
debugging) process more intuitive, a handy waveform viewer is available as part of the
Verilogger simulation package. It allows you to do the following:
look at the specified signals, in single bits or busses, as waveforms
zoom in and out of a time section of your waveform display
use markers on signal transitions to analyze timing characteristics
header is the title of the waveform printout,
begin_time and end_time mark the boundaries of the simulation
portion that you want to print out.
- 15 of 20 -
Appendix B - Verilog Source File Example
A Verilog source comprises 3 major elements: a test bench, a collection of stimulus and
measurement modules, and a circuit or system that we are studying. That circuit of system is
made up of a number of logical components. A logical component may be an atomic device
like a logic gate or it may consist of a number of components that may, in turn, be made up
of logical devices. The stimulus module provides signals into the UUT and the measurement
module acquires the corresponding outputs of the UUT.
As we see in Figure 11 below, a Verilog program is very much like a real electronic bench
with stimulus and measurement equipment connected to the circuit or system we are studying
- Unit Under Test (UUT). Like the real bench, the equipment is connected to the UUT using
wires.
te s t b e n c h w ir e s
s tim u lu s
U U T
m e a s u re
Figure 11
Layout of a Verilog Program
On occasion, in a Verilog program, the stimulus and measurement may be in the same
software module.
Let’s look at a sample program:
- 16 of 20 -
/**************************************************************
* Title: Lab 1: Sample Verilog source code A Simple Comparator
* Model type: Gate level
* Description: This Verilog source code simulates all input conditions of a lesseq than comparison
* Programmer: Your name
* Date: Date typed
**************************************************************/
module testBench;
// wires connect things together
wire lesseq, a, b, c, d;
endmodule
// within the modules, wires are implied…we can put them in if we want to
parameter delay = 2;
output lesseq; // Ouputs: lesseq
input a, b, c, d; // Inputs: to compare, ab and cd
- 17 of 20 -
/**************************************************************
* Title: Lab 1: Sample Verilog source code A Simple Comparator
* Model type: Gate level
* Description: This Verilog source code simulates all input conditions of a lesseq than comparison
* Programmer: Your name
* Date: Date typed
**************************************************************/
// text after a '//' on a line is considered a comment
// multiple line comments are enclosed by '/*' and '*/'
/**************************************************************
* Define the test bench module.
**************************************************************/
module testBench;
// wires connect things together
wire lesseq, a, b, c, d;
endmodule
/**************************************************************
* Define the Comparator module.
**************************************************************/
// within the modules, wires are implied…we can put them in if we want to
parameter delay = 10
output lesseq; // Ouputs: lesseq
input a, b c, d; // Inputs: to compare, ab and cd
- 18 of 20 -
/**************************************************************
* Define the tester module.
**************************************************************/
module TestModule (a,b,c,d, lesseq); // declare test module
// Declare variables:
// the following code illustrates how we can test for aberrant conditions
// we use the specific SEQUENCE of events to cause a glitch - were there others
begin
$display("Producing Glitch");
#stimDelay {a,b,c,d} = 0; // a=0, b=0, c=0, d=0
#stimDelay {a,b,c,d} = 10; // a=1, b=0, c=1, d=0
end
- 20 of 20 -