Professional Documents
Culture Documents
7.1.4.4 Numbers
Numbers include real and integer numbers. In the IEEE library numeric_std package, numbers can be
decimal numbers, negative “-,” you can use exponential notation “E,” and you can even use underscores “_”
to make the number easier to read.
Integer number range: -2,147,483,647 to 2,147,483,647
Real number range: -1.0E38 to + 1.0E38
Following are some examples:
3.14
0.7E8
123_456
7.1.4.5 Base
Sometimes it is more convenient or easier to read when a base other than 10 is used when representing a
number. Here are a couple of examples. The first one is 90 and second one is 240 in decimal.
BASE 2: 2#1011010#
BASE 8: 8#132#
BASE 16: 16#5A#
BASE 2: 2#1111_0000#
BASE 8: 8#360#
BASE 16: 16#F0#
7.1.4.6 Physical
This is a special word in VHDL which represents time, current, and voltage. In FPGA design, we only use
time. Following is an example of 200 Nano seconds.
200 ns
You will see and use a lot of time words in a test bench. (In Chapter 4, in the section “The Test Bench,”
we defined the SYS_CLK to toggle every 10 ns) It is a very useful way to generate a clock in test bench. Keep
in mind that it is an abstraction and will only work in simulation. As such it can appear in your test bench file
but not in your design file. We will talk more about how to create clocks in Chapter 10.
133
Chapter 7 ■ Number Theory for FPGAs
7.2 Grammar in VHDL
VHDL is a strongly typed language. This means that you need to type more (compared with Verilog, which is
another HDL language). VHDL is always checking the types of objects to ensure consistency and prevent you
from mixing incompatible data types or performing actions that don’t make sense. It is needed in hardware
design because most of the time hardware needs to be the exact type and size to work. It is like needing to
use the correct type and size of screw driver to remove a screw. This section will only cover the most useful
grammar, and you will see more of VHDL grammar throughout the next couple of chapters.
7.2.1 Statements in VHDL
Every VHDL statement is terminated with a semicolon. It is quite similar to other computer languages.
VHDL statements are easy to create, but what the statement means (and what it ultimately does) is up to
you. The semicolons only help you do half of the job.
7.2.2 How to Comment
VHDL only has line comments and doesn't support block comment in VHDL-93. You need to put a double
hyphen (--) in front of the comments and rest of the line will be commented out (Listing 7-15).
In the example, we have <= and :=. We are not suggesting you interchange them. The <= sign is used to
assign signals and the := is used to assign variables.
134
Chapter 7 ■ Number Theory for FPGAs
7.3 Summary
After this chapter you should understand the basic elements in VHDL (signal, variable, and constant) and
how to name them (identifiers). You should not use any reserved words for the name of your identifiers. A lot
of the reserved words (keywords) will be used in the next chapter. Remember to have enough comment in
your code to provide a good description, but don’t comment every trivial thing.
In Chapter 8 we are going to employ these basic rules to build some useful designs.
■■Tip Alt + Shift in Notepad ++ for block edit is very useful in VHDL coding
“God made the integers, all the rest is the work of man.”
—Leopold Kronecker
135
CHAPTER 8
In this chapter, we will show you how to use the basic elements (signals) to create combinational logic in
VHDL (VHSIC (very high speed integrated circuit) Hardware Description Language). We’ll also cover two
IEEE (Institute of Electrical and Electronics Engineers) libraries, namely, std_logic_1164 and numeric_std
which provide features for more than just Boolean logic in your FPGA (field-programmable gateway array)
design. By the end of this chapter you will know how to create a 4-bit adder using two different approaches
and then you’ll use ModelSim to simulate the 4-bit adder.
8.1 Boolean Algebra
Boolean algebra is like designing switches for turning light bulbs on and off. Figure 8-1 shows a design that
uses two switches to control the doorbell. If we would like to ring the bell when either one of the switches is
connected (high), then we need an OR gate.
In Boolean algebra, we use the following equation to describe what is happening in the doorbell design:
Bell Ring = Switch 1 + Switch 2
The plus (+) sign in Boolean algebra is logical OR. There are two more basic operation signs in Boolean
algebra. They are which is AND and ~ which is NOT. Boolean algebra only has two possible outputs, logical
high/true (1) or logical low/false (0) inputs and outputs.
Let’s look at one more example. We would like the output (Result) set to high when all inputs (A, B, and
C) are low (Figure 8-2). The Boolean algebra equation will be like the following:
Result = ~A ~B ~C
It is very easy to create the logic gates from the Boolean algebra equation. It is like a direct map to the OR,
AND, and NOT gates. This gate-level implementation can be easily written in VHDL, as shown in Listing 8-1.
entity boolean_algebra_example2 is
port (
A: in std_logic;
B: in std_logic;
C: in std_logic;
Result: out std_logic
);
end boolean_algebra_example2;
end behavioral;
138