Beginning FPGA Programming - Partie39
Beginning FPGA Programming - Partie39
10.1.3.2 Arithmetic Operation
The second is a new way to do adding. The reason for using the ieee.numeric_std package is that we can
directly using add (+), subtract (-), multiplication (*), and divide (/) arithmetic operators. In Listing 10-7, we
are using the adder (+) to increment the counter value, which is the same as the 4-bit full adder in Chapter
8 without the complicated full adder VHDL files. You only need to use one line of VHDL code. Another
good reason is that you can easily overload a signed or an unsigned value with an integer value. Figure 10-9
provides simple rules for signed and unsigned value overloading.
In the example in Listing 10-7, we use one overloading in the following code:
On every rising edge of the clock where enable is equal to 1 (High) this overloading code will get run.
It means increment by one.
184
Chapter 10 ■ Sequential Logic: IF This, THEN That
7.
After step 6 (a-p), the ModelSim Wave window should show Figure 10-10 as the
result. If both the COUNTER and counter_reg value did not show as Hexadecimal
and unsigned number, then right-click on the waveform signal name. It will pop
up a menu on the left-hand side ➤ Radix ➤ click the Radix you would like to
show (like COUNTER use hexadecimal and counter_reg uses unsigned).
185
Chapter 10 ■ Sequential Logic: IF This, THEN That
10.1.3.4.2 Typecasting Conversion
You can use typecasting to convert equal-size arrays of unsigned/signed and std_logic_vector. Figure 10-12
shows an example.
186
Chapter 10 ■ Sequential Logic: IF This, THEN That
Most of the synchronous sequential designs use combinational logic and one or more flip-flops. It looks
like Figure 10-13. The sequential design has a set of inputs (Listing 10-7 has ENABLE and Reset as input) and
generates a set of outputs (Listing 10-7 has COUNTER).
The output values of flip-flops are defined as state (Listing 10-7). Figure 10-13 shows that the flip-flop
outputs (state) depend on the combination of input and current state of every clock (Listing 10-7 is the rising
edge of the CLOCK) such that the state changes from one to another (Listing 10-7 the counter value).
The output of the sequential design is a function of the current state and input. Listing 10-7 shows
that the output COUNTER only depends on the current state (counter_reg). Remember that the output of a
sequential design MUST depend on current state and it does not necessarily need to depend on input. The
green arrow in Figure 10-13 does not necessarily exist, if the outputs are only depend on inputs after one
clock cycle. This concept is important for Chapter 11, which covers Finite State Machines (FSM).
187
Chapter 10 ■ Sequential Logic: IF This, THEN That
Let’s create a simple test bench VHDL file to test the 4-bit up counter design. Listing 10-8 shows the
test bench VHDL file. Please save this file as counter_4bit_vhd_tst.vht (.vht stand for VHDL test bench)
under the <counter 4-bit project folder>/simulation/modelsim and run “vcom -work work counter_4bit_
vhd_tst.vht” in ModelSim Transcript.
ENTITY counter_4bit_vhd_tst IS
END counter_4bit_vhd_tst;
ARCHITECTURE counter_4bit_tb OF counter_4bit_vhd_tst IS
-- signals
SIGNAL CLOCK : STD_LOGIC := '1';
SIGNAL COUNTER : STD_LOGIC_VECTOR(3 DOWNTO 0);
SIGNAL ENABLE : STD_LOGIC;
SIGNAL RESET : STD_LOGIC;
COMPONENT counter_4bit
PORT (
CLOCK : IN STD_LOGIC;
COUNTER : BUFFER STD_LOGIC_VECTOR(3 DOWNTO 0);
ENABLE : IN STD_LOGIC;
RESET : IN STD_LOGIC
);
END COMPONENT;
BEGIN
i1 : counter_4bit
PORT MAP (
-- list connections between master ports and signals
CLOCK => CLOCK,
COUNTER => COUNTER,
ENABLE => ENABLE,
RESET => RESET
);
-- CLOCK --
CLOCK <= not CLOCK AFTER 50 ps;
reset_p : PROCESS
BEGIN
-- code that executes only once
RESET <= '0';
WAIT FOR 25 ps;
RESET <= '1';
WAIT FOR 100 ps;
RESET <= '0';
WAIT; -- RESET stay Low forever
END PROCESS reset_p;
enable_p : PROCESS
188