ISE 10 Tutorial
ISE 10 Tutorial
Installing WebPACK................................................................. 7
Installing XSTOOLs.................................................................. 8
Hierarchical Design............................................................................51
ii
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
"I am new to using programmable logic like FPGAs and CPLDs. How do I start? Is there a tutorial and
some free tools I can use to learn more?"
XILINX has released a free version of their ISE software on the web (they call it WebPACK) so
that anyone can download a set of tools for CPLD and FPGA-based logic designs. And XESS
Corp. has written this tutorial that attempts to give you a gentle introduction to using the ISE
tools.
This tutorial shows the use of the ISE tools on three simple design examples: 1) an LED
decoder, 2) a counter which displays its current value on a seven-segment LED and 3) a
reprogrammable combination lock. Along the way, you will see:
That said, it is important to say what this tutorial will not teach you:
It will not teach you how to choose the best type of FPGA or CPLD for your design.
It will not teach you how to arrange your logic for the most efficient use of the resources in
an FPGA.
It will not teach you what to do if your design doesn't fit in a particular FPGA.
1
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
It will not show you every feature of the ISE software and discuss how to set every option
and property.
It will not show you how to use the variety of peripheral devices available on the XSA
Boards.
In short, this is just a tutorial to get you started using the XILINX ISE FPGA tools. After you go
through this tutorial you should be able to move on to more advanced topics.
2
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
FPGA Programming
Implementing a logic design with an FPGA usually consists of the following steps (depicted in
the figure which follows):
1. You enter a description of your logic circuit using a hardware description language (HDL)
such as VHDL or Verilog. You can also draw your design using a schematic editor.
2. You use a logic synthesizer program to transform the HDL or schematic into a netlist. The
netlist is just a description of the various logic gates in your design and how they are
interconnected.
3. You use the implementation tools to map the logic gates and interconnections into the
FPGA. The FPGA consists of many configurable logic blocks, which can be further
decomposed into look-up tables that perform logic operations. The CLBs and LUTs are
interwoven with various routing resources. The mapping tool collects your netlist gates into
groups that fit into the LUTs and then the place & route tool assigns the groups to specific
CLBs while opening or closing the switches in the routing matrices to connect them
together.
4. Once the implementation phase is complete, a program extracts the state of the switches in
the routing matrices and generates a bitstream where the ones and zeroes correspond to
open or closed switches. (This is a bit of a simplification, but it will serve for the purposes of
this tutorial.)
5. The bitstream is downloaded into a physical FPGA chip (usually embedded in some larger
system). The electronic switches in the FPGA open or close in response to the binary bits
in the bitstream. Upon completion of the downloading, the FPGA will perform the
operations specified by your HDL code or schematic.
That's really all there is to it. XILINX ISE provides the HDL and schematic editors, logic
synthesizer, fitter, and bitstream generator software. The XSTOOLs from XESS provide
utilities for downloading the bitstream into the FPGA on the XSA Board.
3
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
routing
resources FPGA Logic
Simulation
d<0>
Map, Place & Route d<1>
d<2>
s<0>
Bitstream
look-up table configurable
101010010101100101
function block 010110101010110101
010110100101101011
Timing Generate Bitstream 010101001010101010
101010101001101010
Simulation 110110110101001010
110100101011001011
001011001010101001
d<0> 010101101001101001
d<1> 011001100010101010
d<2> 101010100110010101
s<0>
4
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Installing ISE
Getting ISE
You can download the free ISE WebPACK software from this location:
http://www.xilinx.com/ise/logic_design_prod/webpack.htm . Click on the link to download the
software as shown below.
5
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
You will have to create an account and choose a user ID and password before you are allowed
to enter the download section of the site. Once you do that, you will get to a page where you
can select the software that you want to download. In this case, check the ISE WebPACK 10.1
box and click the Next button.
6
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Now you are finally at the page where you can download the ISE WebPACK software. Expand
the Download Files Individually option, and then click on the Download link. This will download the
entire install file for ISE WebPACK. With a size of 2.25 GB, it will take at least an hour to
download, even with a 5 Mb/s link.
Installing ISE
After the ISE WebPACK software download is complete, unpack the WebPACK_SFD.tar file and
double-click the setup.exe file to start the installer. During the installation, you will have to enter
the registration ID that you will receive in an email from Xilinx. Throughout the rest of the
installation, accept the default settings for everything and you shouldn’t have any problems.
The total installation will take at least an hour.
7
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Getting XSTOOLs
If you are going to download your FPGA bitstreams into an XESS FPGA Board, then you will
need to get the XSTOOLS software from http://www.xess.com/ho07000.html. Just download the
latest XSTOOLs setup file.
Installing XSTOOLs
Double-click the XSTOOLs setup file. The installation script will run and install the software.
Accept the default settings for everything.
8
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
An LED Decoder
The first FPGA design you will try is an LED decoder. An LED decoder takes a four-bit input
and outputs seven signals, which drive the segments of an LED digit. The LED segments will
be driven to display the digit corresponding to the hexadecimal value of the four input bits as
follows:
9
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
LED 7-Segment
Decoder LED
s0
s1 s6
d0 s2 s5 s4
d1 s3 s3
d2 s4 s2 s1
d3 s5
s6 s0
10
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
You start ISE by double-clicking the icon on the desktop. This will bring up an
empty project window as shown below. The window has four panes:
1. A source pane that shows the organization of the source files that make up your design.
There are four tabs so you can view the functional modules, source files, different
snapshots (or versions) of your project, or the HDL libraries for your project.
2. A process pane that lists the various operations you can perform on a given object in the
source pane.
3. A transcript pane that displays the various messages from the currently running process.
4. An editor pane where you can enter HDL code, schematics, state diagrams, etc.
source
pane
editor
pane
process
pane
transcript pane
To start your design, create a new project by selecting the FileÎNew Project item from the menu
bar. This brings up the New Project Wizard window where you can enter the name of your
project, the location of your project files, and the style in which you will describe your design at
the top level. I have given my project the descriptive name of design1 and will place the files
11
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Now you need to tell ISE what FPGA you are going to use for your design. The device family,
family member, package and speed grade for the FPGA on each model of XSA Board are
shown below.
Speed
XSA Board Device Family Device Package
Grade
XSA-50 Spartan2 XC2S50 TQ144 -5
XSA-100 Spartan2 XC2S100 TQ144 -5
XSA-200 Spartan2 XC2S200 FG256 -5
XSA-3S1000 Spartan3 XC3S1000 FT256 -4
12
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
For this tutorial, I will target my design to the XSA-3S1000 Board so I have set the Value field of
the Family, Device, Package and Speed properties as shown below. (Set these fields to whatever
values are appropriate for your particular board using the table shown above.) The other fields
can be left at their default values, so you can just click on the Next button to continue creating
the project.
13
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Click the Next button in the following two windows for creating or adding source files. (You will
create the VHDL source code for the LED decoder at a later step.)
14
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The final screen shows the pertinent information for the new project. Click on the Finish button
to complete the creation of the project.
15
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
A window appears where you must select the type of source file you want to add. Since you
are describing the LED decoder with VHDL, highlight the VHDL Module item. Then type the
name of the module, leddcd, into the File name field and click on Next.
16
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The Define Module window now appears where you can declare the inputs and outputs to the
LED decoder circuit. In the first row, click in the Port Name field and type in d (the name of the
inputs to the LED decoder). The d input bus has a width of four, so check the Bus box and type
3 in the MSB field while leaving 0 in the LSB field. Perform the same operations to create the
seven-bit wide s output bus that drives the LEDs.
17
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Click on Next in the Define Module window to get a summary of the information you just typed
in:
18
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
After clicking on Finish, the editor pane displays a design summary and a VHDL skeleton for the
LED decoder. (You can also see the leddcd.vhd file has been added to the Sources pane.) Click
on the leddcd tab at the bottom of the editor pane and then scroll to the bottom of the VHDL
skeleton. Lines 20-23 create links to the IEEE library and packages that contain various useful
definitions for describing a design. The LED decoder inputs and outputs are declared in the
VHDL entity on lines 30-33. You will describe the logic operations of the decoder in the
architecture section between lines 37 and 40.
19
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The completed VHDL file for the LED decoder is shown below. The architecture section
contains a single statement which assigns a particular seven-bit pattern to the s output bus for
any given four-bit input on the d bus (lines 39-54).
Once the VHDL source is entered, click on the button to save it in the leddcd.vhd file.
20
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The syntax checking tool grinds away and then displays the result in the process pane. In this
case, an error was found as indicated by the next to the Check Syntax process. But what is the
error and where is it?
21
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
22
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
On line 44, you can see that the ‘e’ was left off the end of the else keyword. After correcting
this error and saving the file, double-click the on Check Syntax in the Process pane to re-check the
VHDL code. The syntax checker now finds another error on line 56 of the VHDL code.
23
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Examining line 56, you can see it is just the end statement for the architecture section. The
actual error occurred on line 54. The VHDL syntax checker was expecting to find a ';' but it is
missing. After adding the semicolon and saving the file, the Check Syntax process runs without
errors and displays a .
24
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The synthesizer will read the VHDL code and transform it into a netlist of gates. This will take
less than a minute. If no problems are detected, a will appear next to the Synthesize process.
You can double-click on the View Synthesis Report to see the various synthesizer options that were
enabled and some device utilization and timing statistics for the synthesized design. (The
device utilization is also viewable on the Design Summary tab in the editor pane that appears
when you click on the ProjectÎView Design Summary menu item.) You can also double-click on
View RTL Schematic to see the schematic that was derived from the VHDL source code, but it’s
not very interesting in this case.
25
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
process by highlighting the leddcd object in the Sources pane and then double-click on the
Implement Design process.
26
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
You can watch the progress of the implementation process in the console tab of the transcript
pane. For a simple design this, the implementation is completed in less than 30 seconds (on a
2.0 GHz Athlon 64 X2 PC with 3 Gbytes of RAM). A successful implementation is indicated by
the next to the Implement Design process. You can expand the Implement Design process to see
the subprocesses within it. The Translate process converts the netlist output by the synthesizer
into a Xilinx-specific format and annotates it with any design constraints you may specify (more
on that later). The Map process decomposes the netlist and rearranges it so it fits nicely into
the circuitry elements contained in the specified FPGA device. Then the Place & Route process
assigns the mapped elements to specific locations in the FPGA and sets the switches to route
the logic signals between them. If the Implement Design provcess had failed, a would appear
next to the subprocess where the error occured. You may also see a that indicates a
successful completion but some warnings were issued or not all the subprocesses were
enabled.
27
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The device utilization of the LED decoder circuit can be found near the top of the place & route
report (or in the Design Summary tab). The circuit only uses four of the 7680 available slices in
the XC3S1000 FPGA. Each slice contains two CLBs and each CLB can compute the logic
function for one LED segment output.
Device utilization summary:
28
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The pad report shows what pins the LED decoder inputs and outputs use to enter and exit the
FPGA. (The pad report was edited to remove unused pins and fields so it would fit into this
document.)
----------|------------|----------|----------|------------|
Pin Number|Signal Name |Pin Usage |Direction |IO Standard |
----------|------------|----------|----------|------------|
A7 |s<4> |IOB |OUTPUT |LVCMOS25 |
A8 |s<0> |IOB |OUTPUT |LVCMOS25 |
B6 |s<6> |IOB |OUTPUT |LVCMOS25 |
B7 |d<0> |IOB |INPUT |LVCMOS25 |
B8 |s<1> |IOB |OUTPUT |LVCMOS25 |
C6 |d<3> |IOB |INPUT |LVCMOS25 |
C7 |d<2> |IOB |INPUT |LVCMOS25 |
C8 |s<2> |IOB |OUTPUT |LVCMOS25 |
D7 |s<5> |IOB |OUTPUT |LVCMOS25 |
D8 |d<1> |IOB |INPUT |LVCMOS25 |
E7 |s<3> |IOB |OUTPUT |LVCMOS25 |
The GXSPORT utility lets you set the levels on the eight data outputs of the PC parallel port.
The parallel port data pins attach to a group of eight specific pins on the FPGA of each model
of XSA Board. You should assign the LED decoder inputs to four of these so that you can
control the inputs using GXSPORT. The four pins I selected from the group of eight on each
XSA Board are shown below:
LED Decoder
XSA-50 XSA-100 XSA-200 XSA-3S1000
Input
d0 P50 P50 E13 N14
d1 P48 P48 C16 P15
d2 P42 P42 E14 R16
d3 P47 P47 D16 P14
29
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Likewise, each XSA Board has a seven-segment LED attached to the following pins of the
FPGA:
LED Decoder
XSA-50 XSA-100 XSA-200 XSA-3S1000
Output
s0 P67 P67 N14 M6
s1 P39 P39 D14 M11
s2 P62 P62 N16 N6
s3 P60 P60 M16 R7
s4 P46 P46 F15 P10
s5 P57 P57 J16 T7
s6 P49 P49 G16 R10
How do you direct the implementation process so it assigns the inputs and outputs to the pins
you want to use? This is done by using constraints. In this case, you are constraining the
implementation process so it assigns the inputs and outputs only to the pins shown in the
previous tables. Start creating these constraints by right-clicking the leddcd object in the Sources
pane and selecting New Source… from the pop-up menu.
30
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Select Implementation Constraints File as the type of source file you want to add and type leddcd in
the File Name field. Then click on the Next button.
You will receive a feedback window that shows the name and type of the file you created and
the file to which the constraints apply (leddcd.vhd). Click on the Finish button to complete the
addition of the leddcd.ucf constraint file to this project.
31
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Now highlight the leddcd object in the Sources pane and double-click the Floorplan IO – Pre-Synthesis
item in the Process pane to begin adding pin assignment constraints to the design.
32
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The Xilinx PACE window now appears. Click on the I/O Pins item in the Design Browser pane. A
list of the current inputs and outputs for the LED decoder will appear in the Design Object List – I/O
Pins pane. You can change your pin assigments here. You start by clicking in the Loc field for
the d<0> input. Then just type in the pin assignment for this input: N14. Do this for all of the
inputs and outputs using the pin assignments from the previous table.
After the pin assignments are entered, save the file. A dialog window will appear requesting
that you select the delimiter for the I/O buses. Select <> since the XST synthesizer is being
used for this project, and then click on OK. Then close the Xilinx PACE window.
Now you can re-implement your design by highlighting the leddcd object in the Sources pane and
double-clicking on the Implement Design process. After the implementation process completes,
33
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
double-click on Pad Report to view the pin assignments. Now the pad report shows the following
pin assignments:
----------|------------|----------|----------|------------|
Pin Number|Signal Name |Pin Usage |Direction |IO Standard |
----------|------------|----------|----------|------------|
M6 |s<0> |IOB |OUTPUT |LVCMOS25 |
M11 |s<1> |IOB |OUTPUT |LVCMOS25 |
N6 |s<2> |IOB |OUTPUT |LVCMOS25 |
N14 |d<0> |IOB |INPUT |LVCMOS25 |
P10 |s<4> |IOB |OUTPUT |LVCMOS25 |
P14 |d<3> |IOB |INPUT |LVCMOS25 |
P15 |d<1> |IOB |INPUT |LVCMOS25 |
R7 |s<3> |IOB |OUTPUT |LVCMOS25 |
R10 |s<6> |IOB |OUTPUT |LVCMOS25 |
R16 |d<2> |IOB |INPUT |LVCMOS25 |
T7 |s<5> |IOB |OUTPUT |LVCMOS25 |
The reported pin assignments match the assignments made in the Xilinx Pace window, so it
appears the I/O have been constrained to the appropriate pins.
34
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
35
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
1. The Design Hierarchy pane lists the LED decoder inputs, outputs and LUTs
assigned to the various CLBs in the FPGA.
2. The Design Nets pane lists the various signal nets in the LED decoder.
3. The Placement pane shows the array of CLBs in the FPGA. The I/O pins are also
shown around the periphery. (The pins used for Vcc, GND, and programming
are not shown.)
4. The Editable Floorplan pane allows you to change the pin assignments of the I/O
signals by dragging them to different pin locations.
36
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The CLBs used by the LED decoder circuit are highlighted in light-green and are clustered near
the lower right-hand edge of the CLB array. To enlarge this region of the array, click on the
button and then draw a rectangle around the highlighted CLBs in the Placement pane. The
enlarged view of the CLBs used by the LED decoder appear as shown below.
37
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
You can enable the display of the connections between I/O pins and CLBs by selecting the
EditÎPreferences menu item and then checking the boxes in the Ratsnest tab of the Edit
Preferences window as shown below.
38
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Now clicking on a CLB will highlight the nets connecting the inputs and output to the CLB.
39
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
In an analogous manner, you can click on an input pin to highlight which CLBs are dependent
on that input. (For this design, each input affects every CLB.)
40
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
To view all the available FPGA resources (not just those that are used by the design), select
EditÎPreferences and check all the boxes in the Resources tab as shown below.
Now the Placement pane shows all the LUTs, RAMs, buffers, etc. that are available in the FPGA.
41
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Viewing the placement of circuit elements after the place & route process can give you insights
into the resource usage of certain VHDL language constructs. In addition to viewing the
placement of the design, the Floorplanner can be used to re-arrange and optimize the
placement. This is akin to the software technique of hand-optimizing assembly code output by
a compiler. You won’t do this here, but it is an option for designs which push at the limits of the
capabilities of FPGAs.
When using the XSA-3S1000 Board, you need to set some options before generating the
bitstream. (This is not needed if you are using the XSA-50, XSA-100 or XSA-200 Board, but it
wouldn’t hurt, either.) Right-click on the Generate Programming File process and select Properties…
from the pop-up menu.
Now select the Configuration Options tab in the Process Properties window and set all the Pull Up
and Pull Down values to Float to disable the internal resistors of the FPGA. (At the minimum, the
Unused IOB Pins value must be set to Float, but it is best to set them all to Float.) If they are not
42
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
disabled, the strong internal pull-up and pull-down resistors in the Spartan3 FPGA will
overpower the external resistors on the XSA Board.
43
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
44
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Once you have set the bitstream generation options, highlight the leddcd object in the Sources
pane and double-click on the Generate Programming File process.
45
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Within a few seconds, a will appear next to the Generate Programming File process and a file
detailing the bitstream generation process will be created. A bitstream file named leddcd.bit can
now be found in the design1 folder.
46
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
(You can also communicate with the XSA Board through a USB port by attaching an XSUSB
interface as shown below. Downloading the bitstream is done in the same way regardless of
the choice of interface.)
47
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The XSA Boards are programmed using the gxsload utility. Double click the icon to
bring up the gxsload window:
Then click in the Board Type field and select XSA-3S1000 from the drop-down menu since this is
the board you are going to load with the bitstream.
48
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Then open a window that shows the contents of the folder where you stored the LED decoder
design (C:\TEMP\fpga_designs\design1 in my case). You just drag-and-drop the leddcd.bit file from
the design1 window into the FPGA/CPLD pane of the gxsload window.
Then you click on the Load button to initiate the programming of the FPGA. Downloading the
leddcd.bit file to the XSA Board takes only a few seconds.
49
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Double-clicking the icon initiates the gxsport utility. The d0, d1, d2, and d3 inputs of the
LED decoder are assigned to the pins controlled by the D0, D1, D2, and D3 buttons of the
gxsport window. To apply a given input bit pattern to the LED decoder, click on the D buttons
to toggle their values. Then click on the Strobe button to send the new bit pattern to the pins of
the parallel port and on to the FPGA. For example, setting (D3,D2,D1,D0) = (1,1,1,0) will cause
E to appear on the seven-segment LED of the XSA Board.
If you check the Count box in the gxsport window, then each click on the Strobe button
increments the eight-bit value represented by D7-D0. This makes it easy to check all sixteen
input combinations.
NOTE: Bit D7 of the parallel port controls the /PROGRAM pin of the FPGA. Do not set D7 to 0
or you will erase the configuration of the FPGA. Then you will have to download the bitstream
again to continue testing your design.
50
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Hierarchical Design
A Displayable Counter
You went through a lot of work for your first FPGA design, so you will reuse it in this design: a
four-bit counter whose value is displayed on a seven-segment display. The counter will
increment on the falling edge of the clock. The four-bit output from the counter enters the LED
decoder whereupon the counter value is displayed on the seven-segment LED. A high-level
diagram of the displayable counter looks like this:
This design is hierarchical in nature. The LED decoder and counter are modules, which are
interconnected within a top-level module.
51
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
52
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
You will target the same FPGA on the XSA Board, so the other properties in the New Project
window retain the same values you set in the previous project.
53
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
You won’t add any new source files at the moment, so just click on the Next button on the
Create New Source window. This brings up the Add Source window shown below that lets
you add existing source files to your new project. It will save time if you re-use the LED
decoder from the previous design, so click on the Add Source… button.
54
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The Add Existing Sources window appears. Move to the C:\TEMP\fpga_designs\design1 folder
and highlight the leddcd.vhd file that contains the VHDL source code for the LED decoder.
55
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The New Project window now shows that a copy of the leddcd.vhd file will be added to the
project. The Copy to Project box is checked, so the leddcd.vhd file will be copied from the design1
folder to the design2 folder. Unchecking this box will cause the design2 project to link directly to
the leddcd.vhd file in the design1 folder, so any change in this file will affect both projects. For this
example, I have chosen to make a separate copy so the box remains checked. This is the only
existing file you need to add, so click on the Next button to move on to the next window.
56
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The final screen shows the pertinent information for the new project. Click on the Finish button
to complete the creation of the project.
An Adding Source Files… window will appear that lets you specify the intended use for the
files you are adding. Files can be used during synthesis and implementation of an FPGA
bitstream as we did in the previous design. They can also be used only during simulation, such
as testbench files that specify stimulus patterns that are driven onto the inputs of a design. Or
they can be for both synthesis/implementation and simulation (as most are) because they
describe the function and/or structure of the design in both these domains. There are also files,
such as text files containing design documentation that are added to a project but are not used
in either domain. I selected All for the leddcd.vhd file even though it will only be used for
synthesis and implementation in this example.
57
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
After you click on Finish in the Adding Source Files… window, the Sources pane contains only
the single leddcd.vhd source file.
58
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Adding a Counter
Now you have to add the counter to the design. There is no counter module yet, so you have
to build one. Right-click on the xc3s1000-4ft256 object and select New Source… from the pop-up
menu.
59
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
As in the previous example, you are prompted for the type of file to add to the project. This
time, choose the IP (Coregen & Architecture Wizard) menu item. This will allow you to select a
counter from a library of pre-built, configurable components. Then type counter into the File
Name field and click on the Next button.
In the next window, expand the library tree until you find the binary counter component. Then
click on Next.
60
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
A very uninteresting summary of your choice appears. Click Finish to move on to the screens
that will let you configure the counter component for this design.
61
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The initial configuration window specifies a sixteen-bit counter that increments its value upward
by one on each clock cycle until it reaches 65,535 after which it rolls-over back to 0. You can
change many of the features of this counter on the following screens. (Click on the View Data
Sheet button to see all the details on how this counter can be configured.)
The only change that needs to be made for this design is to increase the counter width to 28
bits. Why build a 28-bit counter when only the upper four bits are used? The counter will be
driven by a clock signal on the XSA Board that has a frequency of 50 MHz. The LED display
would be changing much too quickly to see at this frequency. By connecting the LED decoder
to the upper four bits of the 28-bit counter, the display will only change once in every 224 clock
cycles. So the LED display will change every 224 / (50 x 106) = 0.336 seconds which is slow
enough to read.
After changing the counter width, click on Next to continue configuring the counter.
62
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
None of the advanced configuration options for the counter are needed in this design, so click
Next and Finish in the next two windows that appear.
63
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
After clicking Finish in the final configuration window, the counter module appears in the Sources
pane. The counter is stored as a Xilinx Coregen object (.xco file suffix). If you need to change
the operation of the counter, just double-click the counter module and make your changes in
the Coregen wizard windows that appear.
64
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
65
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Now it is time to create the top-level schematic that will hold the counter and LED decoder
symbols. Right-click on the xc3s1000-4ft256 object and select New Source… from the pop-up menu.
Then highlight the Schematic entry in the New Source window and name the schematic
disp_cnt. Then click on Next.
There is very little to do when initializing a schematic, so just click on the Finish button in the
Summary window that appears.
66
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The disp_cnt schematic object has now been added to the Sources pane. You can double-click it
to begin creating the schematic, but a schematic editor window should open automatically once
the file is created.
67
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Click on the Symbols tab at the bottom of the Sources pane. The Symbols tab contains a list of
categories for various logic circuit elements that can be used in a schematic. Below that is the
list of circuit element symbols in the highlighted category. A symbol can be selected from this
list and dropped into the drawing area to the right.
category list
symbol list
drawing area
68
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
To start creating the top-level schematic, highlight the second entry in the category list. The
c:/TEMP/fpga_designs/design2 category contains the schematic symbols for the design2 project’s
counter and LED decoder modules. You can see the names of these modules in the symbol
list.
69
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Click on the counter entry in the Symbols list. Then move the mouse cursor into the drawing area
and left-click to place an instance of the counter module into the schematic. Repeat this
process with the leddcd module to arrive at the arrangement shown below. (For clarity, I used
the EditÎPreferences command to turn off the display of the grid in the schematic drawing area.)
70
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
71
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Left-click the mouse on the q(27:0) bus on the right-hand edge of the counter module. Then
left-click on the d(3:0) bus on the left-hand edge of the leddcd module. This creates a four-bit
bus between the output of the counter and the input of the LED decoder.
However, the four-bit bus causes a problem. Click on the button to run a design-rule
check. An error is reported in the Console tab:
You can highlight the place in the schematic where the error occurs by clicking on the
disp_cnt.sch text in the error message. The error is caused by the mismatch between the
28-bit counter output and the four-bit bus. This problem is fixed as follows:
2. Right-click on the existing bus and select Delete from the pop-up menu to remove this
bus.
72
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
3. Click on the wiring tool, , click on the counter output, and draw a small bus stub
outward. Terminate the bus with a double-click. This will create a 28-bit bus connected
to the counter outputs.
4. Repeat the previous step to attach a four-bit bus to the LED decoder input.
5. Click on the selection tool, , and then hover the mouse pointer over the bus
connected to the counter. The name of the bus will be shown. In my project, the bus is
named XLXN_2(27:0).
6. Right-click on the bus connected to the LED decoder and select Rename Selected Bus… in
the pop-up menu.
7. Rename the LED decoder input bus from XLXN_3(3:0) to XLXN_2(27:24) as shown
below and click on the Apply button followed by the OK button. This will connect the LED
decoder inputs to the four most-significant bits of the counter output bus.
73
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
At this point, the schematic should appear as shown below. The design-rule checker should no
longer detect any problems.
74
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Now highlight the IO category and select a single-bit output buffer (obuf) from the list of symbols.
Attach the output buffer to the output of the LED decoder as shown below. Then run the
design-rule checker.
75
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The design-rule checker will detect a width-mismatch error in the above schematic since a
single output buffer has been attached to the seven-bit LED decoder output bus. This error can
be fixed by right-clicking on the OBUF symbol and selecting Object Properties from the pop-up
menu. Then append (6:0) to the instance name of the output buffer as shown below and
click on Apply followed by OK. This will expand the single buffer into an array of seven buffers
with each one connected to a bit of the LED decoder output bus.
76
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Next, attach a short bus segment to the output of the OBUF. Then click on the button for
adding I/O markers. Click on the Add an output marker button in the Options tab of the Processes
pane and then click on the free end of the wire segment that you just added.
77
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Clicking on the end of the wire creates a seven bit-wide set of output pins.
78
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The output pins automatically assume the same name as the bus to which they are attached,
but this name was automatically generated and doesn’t carry a lot of meaning. To change the
name of the outputs (and the associated bus), right-click on the I/O marker and select Object
Properties… from the pop-up menu.
79
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The Object Properties window allows you to set the name and direction of the pins.
Double-click the existing bus name and replace it with S(6:0). The direction of the bus pins is
already set to Output so you can finish by clicking on the OK button.
80
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
81
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Once the outputs from the circuit are in place, you can connect a single input I/O marker to the
clock input of the counter. (No input buffer is needed because the clock signal will enter the
FPGA through a dedicated global clock input.) Right-click (or double-click) on the I/O marker
and rename it to clk. After this, perform another schematic check to detect any errors and
save the schematic using the FileÎSave command.
82
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Once you save the schematic for the top-level module, the hierarchy in the Sources pane gets
updated. Now the counter and leddcd modules are shown as lower-level modules that are
included within the top-level disp_cnt module.
83
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
84
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Select Implementation Constraints File as the type of source file to add and type disp_cnt in the File
Name field. Then click on the Next button.
You will receive a feedback window that shows the name and type of the file you created and
the file to which it is associated. The pin assignments will be made for the top-level module in
the design hierarchy, so the constraints file is associated with the disp_cnt module. Click on
the Finish button to complete the addition of the disp_cnt.ucf file to this project.
85
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Now highlight the disp_cnt.sch object in the Sources pane and double-click the Floorplan IO – Pre-
Synthesis process to begin adding pin assignment constraints to the design.
The appropriate pin assignments for each model of XSA Board are shown below. The clk input
is assigned to a dedicated clock input on each FPGA to which a 50 MHz clock signal is applied.
The seven-segment LED pin assignments are the same as in the previous design.
86
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
In the Design Object List – I/O Pins pane of the Xilinx PACE window that appears, set the
pin assignments for the clock input and LED segment drivers as shown below. Then save the
pin assignments and close the PACE window.
87
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
88
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The displayable counter consumes 18 of the 7680 slices in the FPGA. Each slice contains two
CLBs, so the displayable counter uses a maximum of 36 CLBs. The 28-bit counter requires at
least 28 CLBs and the LED decoder requires 7 CLBs so this totals to 35 CLBs. So the device
utilization statistics make sense.
89
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
As a precaution, you should also click on the Pinout Report in the Design Summary pane and check
that the pin assignments for the clock input and LED decoder outputs match the assignments
made with PACE. (You can make the comparison easier by clicking on the Signal Name column
header to bring all the signal-pin assignments to the top.)
90
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
After the static timing delays are calculated, click on the Static Timing Report item in the Design
Summary pane to view the results of the analysis. From the information shown at the bottom of
the timing report, the minimum clock period for this design is 4.820 ns which means the
maximum clock frequency is 207.5 MHz. The clock frequency on the XSA Board is 50 MHz
which is well below the maximum allowable frequency for this design.
91
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
In this example, rather than use the gxsload utility you will employ the downloading utilities built
into ISE. The iMPACT programming tool downloads the bitstream through the JTAG interface
of the FPGA, so you need to adjust the way the bitstream is generated to account for this.
Right click on the Generate Programming File process and select the Properties… entry from the pop-
up menu.
92
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Select the Startup Options tab of the Process Properties window. Change the FPGA Start-Up Clock
property to JTAG Clock so the FPGA will react to the clock pulses put out by the iMPACT tool
during the final phase of the downloading process. If this option is not selected, the FPGA will
not finish its configuration process and it will fail to operate after the downloading completes.
Note that the startup clock is only used to complete the configuration process; it has no affect
on the clock that is used to drive the actual circuit after the FPGA is configured.
93
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Next, click on the Configuration Options tab and disable all the internal pull-up and pull-down
resistors in the FPGA as we did in the previous design. Then click OK.
94
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Now that the bitstream generation options are set, highlight the disp_cnt object in the Sources
pane and double-click on the Generate Programming File process to create the bitstream file.
95
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Within a few seconds, a will appear next to the Generate Programming File process and a file
detailing the bitstream generation process will be created. A bitstream file named disp_cnt.bit is
placed in the design2 folder.
96
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
XSA-3S1000 board so it will work with the iMPACT programming tool. Double click the
icon and then drag & drop the p3jtag.svf file from the C:\Program Files\XSTOOLS\XSA\3S1000 folder
into the FPGA/CPLD pane of the gxsload window. Then click on the Load button and the CPLD
will be reprogrammed in less than a minute.
After the p3jtag.svf file is loaded into the XSA Board, move the shunt on jumper J9 from the xs to
the xi position. The XSA Board is now setup so the FPGA can be configured through its JTAG
boundary-scan pins with the iMPACT programming tool. Note that this process only needs to
be done once because the CPLD on the XSA Board will retain its configuration even when
power is removed from the board. (If you want to go back to using the gxsload programming
utility, you must move the shunt on J9 back to the xs position and download the dwnldpar.svf file
into the CPLD.)
97
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
98
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The iMPACT – Welcome to iMPACT window now appears. Make sure the Configure Devices
using Boundary-Scan (JTAG) radio button is selected. Boundary-scan mode allows the
configuration of multiple FPGAs connected together in a chain. To accomplish this, the
iMPACT software needs to know the types of the FPGAs in the chain. There is just a single
FPGA on the XSA Board and you could easily describe this to iMPACT. But iMPACT can also
probe the boundary-scan chain and automatically identify the types of the FPGAs. This is even
easier, so select the Automatically connect to a cable and identify Boundary-Scan chain option and click on
the Next button.
99
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The Assign New Configuration File window now appears. You need to tell iMPACT what
bitstream file to download into the FPGA. Go to the folder where your design2 project is stored
and highlight the disp_cnt.bit file. Then click on the Open button.
The Device Programming Properties appears next. You can check the Verify box if you want
to make iMPACT readback the bitstream from the FPGA after the download to make sure it
was sent correctly. Then click on the OK button.
100
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The iMPACT software will probe the boundary-scan chain and the main iMPACT window will
appear showing a boundary-scan chain consisting of a single XC3S1000 FPGA.
101
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Now right-click on the xc3s1000 icon and select the Program… item on the pop-up menu. This will
initiate the download of the bitstream to the FPGA.
The progress of the bitstream download will be displayed. The download operation should take
less than a minute.
102
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
After the download operation completes, you can check the status messages in the bottom
pane of the iMPACT window to see if the FPGA was configured successfully.
103
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
In this chapter we will build an FSM that acts like a combination lock. The requirements for this
digital combination lock are:
3. The combination lock will open if the user enters an n-key sequence that matches the
combination. Otherwise, the lock stays locked.
4. The user must enter an entire n-key sequence before the lock either accepts or rejects
the sequence.
5. Once the combination lock is unlocked, the user can relock it or enter a new
combination as a sequence of key presses.
6. The lock will require the user to verify any new combination that is entered before it
replaces the previous combination.
A hierarchical view of the combination lock and its lower-level modules is shown below. The
combination lock consists of:
Keyboard interface: This module accepts a serial data stream and clock signal from a
standard PS/2 PC keyboard and converts it into a parallel scancode with an associated ready
signal that indicates the presence of the scancode.
Lock&key mechanism: This module accepts scancodes from the keyboard interface and
determines whether or not the correct combination has been entered and manages the entry of
new combinations.
The combination lock accepts the keyboard serial data and clock as inputs along with a main
clock that synchronizes the operations of both modules. There is also a reset input to initialize
104
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
the entire FSM upon startup. The combination lock visually indicates its current status on a
seven-segment LED.
7-Segment
LED
Combination s0 s6
Lock s1
s2 s5 s3 s4
RESET s3
s4 s2 s1
Kybd Clock s5
Keyboard Lock & Key s6 s0
Interface Mechanism
Kybd Data
105
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
psData: This signal carries the serial data stream as each key is pressed and released. Each
key is assigned an eight-bit scancode that is transmitted least-significant bit to most-significant
bit with a preceding start bit and a terminating parity bit and stop bit.
psClk: The falling edge of this signal indicates when the psData signal is valid.
106
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The keyboard interface will accept the serial data stream and will output the eight-bit scancode
in parallel along with an rdy pulse that indicates a valid scancode is available. The rdy pulse
will be generated when the psClk signal goes high and stays there. The timing of the psData,
psClk, and rdy signals is shown in Figure 2.
psClk
psData D0 D1 D2 D3 D4 D5 D6 D7 P
rdy
A single scancode is transmitted when a key is pressed. But two scancodes are transmitted
when the key is released: an initial scancode of 11110000 to indicate the key release, and then
the scancode for the key is sent again. The keyboard interface will be designed such that the
rdy signal pulses only after the key has been released.
The VHDL code for the keyboard interface is shown in Listing 1. The functions of the code for
the scancodeReg module are as follows:
Lines 36–41: The module receives the psData and psClk inputs from the keyboard and
outputs the eight-bit scancode and the rdy signals that were described above. A master clock
107
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
is also provided which synchronizes the operations of this module with the lock&key module. A
reset signal initializes the module when it first powers up.
Line 46: This line declares a 10-bit shift-register that holds the scancode value as it arrives
from the keyboard. The start bit, eight scancode bits, parity bit, and stop bit will enter the most-
significant bit of the sc_r register and shift towards the least-significant bit. By the end of a
scancode transmission the start bit will have shifted completely out of the register and be lost
while the scancode will end up in the lower eight bits of sc_r. The stop and parity bits will be in
the uppermost two bits.
Lines 47–48: These lines define a counter that is used to determine when the psClk signal is
no longer pulsing. The timeout value (line 47) is determined by dividing the main clock
frequency (defined in the GENERIC section on line 32) by the frequency of the psClk (line 33).
If the main clock is 50 MHz and the keyboard clock is 10 KHz, then the timeout value is 5000
which means it will take 5000 pulses of the main clock to determine if the psClk signal is static.
The timeout counter register is defined on line 48 as a natural that can hold a value as large as
the timeout value. By defining the counter register in this way, we can change the frequency of
the main clock or the keyboard clock and the timeout counter will be automatically resized by
the synthesizer with exactly the number of bits needed to store the timeout value.
Lines 56–66: This process parallelizes the serial keyboard data. If the reset input is active, the
scancode shift register is cleared to all zeroes. Otherwise, on falling edges of the keyboard
clock the value on the keyboard data signal is placed into the most-significant bit of the shift
register and the upper nine bits of the register are shifted one bit position downward. By the
end of a scancode transmission, the start bit will have shifted completely out of the register and
be lost while the scancode will end up in the lower eight bits of sc_r. The stop and parity bits
will be in the uppermost two bits.
Line 69: The eight lower bits of the sc_r register are output as the scancode output of the
module.
Lines 73–94: This process detects when the psClk signal has stopped pulsing and indicates
that a scancode is available. The timeout counter and scancode ready flag are cleared when
the module is reset. Then the counter is incremented as long as the psClk is at logic 1 and the
counter has not reached its timeout value yet. The counter is reset to zero if psClk is ever low
because that indicates the keyboard clock is still pulsing so the scancode cannot be complete.
But if the counter ever reaches the value TIMEOUT-1, then the scancode ready flag is pulsed
high for a single clock cycle.
Lines 98–114: This process checks the scRdy_r flag and looks for the scancode that matches
the KEYRELEASE scancode defined on line 52. After seeing the key release scancode, this
process looks for the next following scancode. Then it sets the flag that indicates the scancode
received after the KEYRELEASE code is ready for output.
Line 116: The ready flag from the previous process is output from the module.
108
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
1 ----------------------------------------------------------------------
2 -- Company: XESS Corp.
3 -- Engineer: Dave Vanden Bout
4 --
5 -- Create Date: 10:39:20 05/21/2006
6 -- Design Name: design3
7 -- Module Name: scancodereg - Behavioral
8 -- Project Name: design3
9 -- Target Devices:
10 -- Tool versions:
11 -- Description:
12 --
13 -- Dependencies:
14 --
15 -- Revision:
16 -- Revision 0.01 - File Created
17 -- Additional Comments:
18 --
19 --------------------------------------------------------------------
20 library IEEE;
21 use IEEE.STD_LOGIC_1164.all;
22 use IEEE.STD_LOGIC_ARITH.all;
23 use IEEE.STD_LOGIC_UNSIGNED.all;
24
25 ---- Uncomment the following library declaration if instantiating
26 ---- any Xilinx primitives in this code.
27 --library UNISIM;
28 --use UNISIM.VComponents.all;
29
30 entity scancodereg is
31 generic(
32 CLKFREQ : natural := 50_000; -- main clock freq (KHz)
33 PSCLKFREQ : natural := 10 -- keyboard clock freq (KHz)
34 );
35 port(
36 clk : in std_logic; -- main clock
37 rst : in std_logic; -- reset
38 psClk : in std_logic; -- keyboard clock
39 psData : in std_logic; -- keyboard data
40 scancode : out std_logic_vector(7 downto 0); -- key scancode
41 rdy : out std_logic -- true when scancode is ready
42 );
43 end scancodereg;
44
45 architecture Behavioral of scancodereg is
46 signal sc_r : std_logic_vector(9 downto 0); -- scancode shift reg
47 constant TIMEOUT : natural := CLKFREQ/PSCLKFREQ; -- psClk quiet timeout
48 signal cnt_r : natural range 0 to TIMEOUT; -- timeout counter
49 signal scRdy_r : std_logic; -- scan code is ready flag
50 signal rdy_r : std_logic; -- output scan code is ready flag
51 signal keyrel_r : std_logic; -- key has been released flag
52 constant KEYRELEASE : std_logic_vector(7 downto 0) := "11110000";
53 begin
54
55 -- this process gathers the keybrd scancode into the shift register
56 process(psClk, rst)
57 begin
58 -- async. reset of scancode ready flag
59 if rst = '1' then
60 sc_r <= (others => '0');
61 -- accept keyboard data on falling edge of keyboard clock
62 elsif falling_edge(psClk) then
63 -- key data arrives LSB first so right-shift it into MSB of register
109
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
110
Xilinx ISE 10 Tutorial
Once the VHDL code is entered and saved in the scancodereg.vhd file, you should check it for
syntax errors.
Once any syntax errors are corrected, create a schematic symbol for the keyboard interface.
You will use that later in the top-level schematic for this project.
The lock module follows the basic structure of almost all FSM’s I create. There is a
combinatorial process (lines 106-247) that computes the FSM’s next state given the current
state and inputs, and there is an update process (lines 251-277) that loads the computed
values into the registers on the next clock edge.
I also follow a register naming convention that appends _r to a signal or variable name if it
stores the current state, and _x is appended to the name if it stores the computed value that
will be loaded into the register on the next clock edge. This naming convention also allows me
to visually check my VHDL for errors because I know that only state registers ending with _x
112
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
should be on the left-hand side of the assignment operator (<=) in the combinatorial process
and registers with the _r register should only appear on the right-hand side because this
process computes the next state values using the current state values as operands. The
situation is reversed in the update process with the _r signals appearing on the left-hand side
of the assignment and _x signals on the right.
The lock&key FSM has 5 states (declared on lines 47-51) with the operations in each state
handled in a separate section of a case statement in the combinatorial process:
enterComb (lines 127-150): Upon entering this state, an L is displayed on the LED to indicate
the lock is locked. The value on the LED is incremented as each keypress is received from the
user (indicated by the rdy signal going high), and the scancode received from the
scancodereg module is compared against the next scancode of the active lock combination
stored in the activeComb array. If any key fails to match the corresponding entry in the
combination, then the match flag is cleared. Once the last key in the combination is checked
(as indicated by the index register that steps through the activeComb array), the FSM
transitions to the checkComb state.
checkComb (lines 154-164): The match flag is checked in this state. If the flag is set, then
the combination was entered correctly and the FSM moves to the unlocked state. Otherwise,
it returns to the enterComb state and waits for the user to enter another key sequence.
unlocked (lines 167-180): The LED displays a U in this state to indicate the lock is unlocked.
If the user presses a backspace key, the FSM transitions to the enterNewComb state so a
new combination can be entered. (Thus, the user must know the current combination before
he can enter a new combination.) Any other keypress relocks the lock and returns the FSM to
the enterComb state.
enterNewComb (lines 185-204): The LED displays an r upon entering this state to indicate
the combination is being replaced. Each keypress entered in this state is stored in the
newComb array and the value on the LED is incremented. After all the keys for a complete
combination are entered, the FSM goes to the verifyNewComb state.
verifyNewComb (lines 209-237): In this state, the user must re-type the new combination so it
can be compared against the contents of the newComb array. If any mismatch occurs, the
FSM immediately goes to the enterComb state and relocks the lock without changing the
combination. If the new combination is repeated correctly, the contents of the newComb array
are transferred to the activeComb array and the lock is relocked with the new combination.
The reset of the FSM and the actual state transitions is handled by the update process on
each rising edge of the master clock:
Lines 255–265: The FSM is synchronously reset if the reset input (rst) is high or if the power-
on reset flag (rst_i) is set. The power-on reset flag is initially set on line 67 so the FSM is
guaranteed to be reset whenever power is first applied to the FPGA. The FSM always resets to
the enterComb state and the lock is locked. If a power-on reset has occurred, then the active
combination is not defined so it must be loaded with a default combination (defined as the key
sequence “123456789” on lines 61-63). A reset caused by the reset input, however, will not
reload the reload the active combination so it will remain at whatever key sequence was set by
the user.
113
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Lines 267-276: When the FSM is not being reset, then all the state registers are loaded with
their next values here.
There are a few other points to note about the lock module:
• The FSM requires a single clock cycle duration for the scancode ready signal. If the rdy
output from the keyboard interface module stayed high for more than a single clock
cycle on each key press, this would cause a transition between multiple states of the
FSM.
• The COMB_LENGTH generic parameter on line 32 can be set to any value between 1
and 9 to change the number of keys in the combination. The use of an array to store
the combination (lines 56-58) allows the module to resize itself automatically if you
decide to change the length of the combination.
• The VHDL synthesizer automatically generates the bit encodings for the FSM states on
lines 47-51. If you want to explicitly code the states, you can place lines such as the
following below the state register declaration on line 53:
Next, highlight the scancodereg module in the Sources pane (since this was the first
module added to the project, it is the top-level module by default), and then right-click
on the Synthesize process and select Properties from the pop-up menu:
114
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Now your explicit state encodings will be used. You can check this by looking in the
synthesis report to see:
===============================================================
* Advanced HDL Synthesis *
===============================================================
115
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
116
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
117
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
158 -- If the match flag is still set, then all the user-entered scancodes
159 -- matched the active combination, so transition to the unlocked state.
160 if match_r = '1' then
161 lockState_x <= unlocked; -- keys match combination, so unlock the lock
162 else
163 lockState_x <= enterComb; -- otherwise, wait for another attempt
164 end if;
165
166 -- The lock is unlocked when the FSM is in this state.
167 when unlocked =>
168 led <= LED_UNLOCKED; -- show "U" on LED to indicate lock is unlocked
169 -- Wait until a new key scancode arrives.
170 if rdy = '1' then
171 -- After the active combination is entered, the user can hit the
172 -- backspace key to move to the state that allow entry of a new comb.
173 if sc = NEW_COMB_KEY then
174 lockState_x <= enterNewComb;
175 -- Otherwise, any other key will re-lock the lock and return to the state
176 -- that checks for the active combination to be entered.
177 else
178 lockState_x <= enterComb;
179 end if;
180 end if;
181
182 -- FSM is in this state when the user is entering a new combination
183 -- to replace the current active combination. The index_r register points to
184 -- the location where the next scancode will be stored in the new combination.
185 when enterNewComb =>
186 -- Show an "r" on the 7-segment LED before the user has pressed any keys.
187 -- After that, indicate the number of keypresses received from the user.
188 if index_r = 0 then
189 led <= LED_ENTERNEWCOMB; -- show "r" on LED to indicate comb. replacement
190 else
191 led <= ledDecoder(index_r); -- index into comb. array = # of user keypresses
192 end if;
193 -- Wait until a new key scancode arrives.
194 if rdy = '1' then
195 newComb_x(index_r) <= sc; -- store current scancode into new combination array
196 -- Increment the index if the new combination isn't complete yet.
197 if index_r /= COMB_LENGTH-1 then
198 index_x <= index_r + 1;
199 -- Otherwise, reset the index and get ready to verify the combination.
200 else
201 index_x <= 0;
202 lockState_x <= verifyNewComb;
203 end if;
204 end if;
205
206 -- FSM is in this state when the user is re-entering the new combination
207 -- to verify it against what was stored. The index_r register points to
208 -- the location of the next scancode that will be compared in the new comb.
209 when verifyNewComb =>
210 -- Show a "C" on the 7-segment LED before the user has pressed any keys.
211 -- After that, indicate the number of keypresses received from the user.
212 if index_r = 0 then
213 led <= LED_VERIFYNEWCOMB; -- show "C" on LED to indicate check of new comb.
214 else
215 led <= ledDecoder(index_r); -- index into comb. array = # of user keypresses
216 end if;
217 -- Wait until a new key scancode arrives.
218 if rdy = '1' then
219 -- Abort the operation as soon as a scancode does not match the new comb.
220 if sc /= newComb_r(index_r) then
221 index_x <= 0; -- reset the index
222 lockState_x <= enterComb; -- go wait for the user to enter the active comb.
223 else
224 -- Increment the index if the new comb. verification isn't complete yet.
225 if index_r /= COMB_LENGTH-1 then
226 index_x <= index_r + 1;
227 -- Otherwise, the user has correctly re-entered the new comb. so overwrite
228 -- the active comb. with the new comb. and return to the locked state.
118
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
229 else
230 for i in 0 to COMB_LENGTH-1 loop
231 activeComb_x(i) <= newComb_r(i);
232 end loop;
233 index_x <= 0;
234 lockState_x <= enterComb;
235 end if;
236 end if;
237 end if;
238
239 -- Something wrong has happened if the FSM ever gets into this state, so
240 -- reset into the locked state.
241 when others =>
242 index_x <= 0;
243 match_x <= '1';
244 lockState_x <= enterComb;
245
246 end case;
247 end process;
248
249 -- This process just updates the various state registers with their next values
250 -- as computed by the previous process. It also handles the reset of the FSM.
251 update: process(rst,clk)
252 begin
253 if rising_edge(clk) then
254 -- Handle the reset input or the power-on reset.
255 if rst='1' or rst_i='1' then
256 rst_i <= '0'; -- clear power-on reset
257 index_r <= 0;
258 match_r <= '1';
259 lockState_r <= enterComb;
260 -- On power-on, initialize the active combination from the default value.
261 if rst_i = '1' then
262 for i in 0 to COMB_LENGTH-1 loop
263 activeComb_r(i) <= defaultComb(i);
264 end loop;
265 end if;
266 -- Otherwise, update all registers with their next values.
267 else
268 index_r <= index_x;
269 match_r <= match_x;
270 lockState_r <= lockState_x;
271 for i in 0 to COMB_LENGTH-1 loop
272 activeComb_r(i) <= activeComb_x(i);
273 newComb_r(i) <= newComb_x(i);
274 end loop;
275 end if;
276 end if;
277 end process;
278
279 end Behavioral;
Once the VHDL for the lock module is entered, run a syntax check and generate a schematic
symbol as you did for the scancodereg module.
The VHDL description of the FSM can be useful for two reasons:
1. The editing area of the State Editor window gets very cluttered for complicated FSMs.
You can use the State Editor to draw an initial, simplified version of your FSM and then
add the rest of your description directly to the VHDL file. You cannot automatically
back-annotate the additions to the VHDL file back into the State Editor, so the VHDL file
must be used as the master design file for the FSM after you do this.
2. If you are unsure how to write FSM descriptions using VHDL, you can create simple
FSMs in the State Editor and export them as VHDL to view the basic language
constructs that are used.
119
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Enter the following components and connections into the schematic editor window:
120
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
1. The main clock (CLK) enters the FPGA on a dedicated clock pin (because that is the
way it is connected on the XSA Board) so the input pad can connect directly to a
general clock buffer (BUFG). Using the BUFG ensures that the clock signal reaches all
the flip-flops in the design with minimal skew so they all change state at the same time.
2. The clock from the PS/2 keyboard (PSCLK) enters on a generic I/O pin so it must go
through an input buffer (IBUF) before going through a BUFG.
3. The keyboard serial data signal (PSDATA) and the reset signal are standard, non-clock
inputs so they just connect to IBUFs.
4. The reset signal (RST_N) is sourced by an active-low DIP-switch button on the XSA
Board, so it passes through an inverter before going to the active-high reset inputs of
the scancodereg and lock modules.
5. An array of seven output buffers (OBUF) as was created as in the design example of
the previous chapter. The seven-bit led bus of the lock module connects to the inputs of
the output buffers, and the buffer outputs connect to the output pads.
Once the components are connected to each other and the I/O, run a design-rule check to
make sure there are no errors and then save the schematic. Then, make the schematic the
top-level module of the design by right-clicking it in the Sources pane and selecting Set as Top
Module from the pop-up menu.
121
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
The Xilinx software tends to balk at using general-purpose inputs as clock sources for registers,
and that’s exactly what is being done with the clock from the PS/2 keyboard. The PS/2 clock
has a very low frequency and it runs asynchronously with respect to the main logic clock, so it
doesn’t really matter that the PS/2 clock routing is sub-optimal. You can get around this error
by placing the constraint listed above (strip off the ‘<’ and ‘>’ delimiters) into the comblock.ucf
122
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
file. Then you can re-run the synthesis and implementation processes to get an FPGA
bitstream in the comblock.bit file.
Press LED
New State… This means…
key… displays…
The combination lock is locked and is
None enterComb waiting for the default combination to be
entered from the keyboard.
123
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
124
Xilinx ISE 10 Tutorial
XESS Corporation - www.xess.com ©2008 by XESS Corp.
Going Further…
OK! You made it to the end! You have scratched the surface of programmable logic design,
but how do you learn even more? Here are a few easy things to do:
Select HelpÎSoftware Manuals. You will be presented with an Adobe Acrobat document that
lists all the manuals for the ISE software. This includes a 300-page set of guidelines on
synthesis and simulation techniques for FPGA designs.
Select HelpÎXilinx on the WebÎXilinx Application Notes. This will take you to a large set of
interesting designs that have been done using Xilinx FPGAs.
Read the comp.arch.fpga newsgroup for helpful questions and answers about
programmable logic design.
125
Xilinx ISE 10 Tutorial