Beginning FPGA Programming - Partie37
Beginning FPGA Programming - Partie37
begin
Q1 <= '1';
Q1_N <= '0';
elsif ( PRE1_N = '1' and CLR1_N = '0') then -- Clear the output Q as Low(0)
Q1 <= '0';
Q1_N <= '1';
elsif ( rising_edge(CLOCK1) ) then -- PRE_N and CLR_N are inactive (both are 1) and
-- rising edge of clock happen
Q1 <= D1;
Q1_N <= not D1;
end if;
end process;
-- The second D-Flip Flop sequential process
-- Only execute this process when CLOCK, PRE_N or CLR_N changes state
d_ff_2_p: process(CLOCK2, PRE2_N, CLR2_N)
begin
if ( PRE2_N = '0' and CLR2_N = '0') then
Q2 <= '1';
Q2_N <= '1';
elsif (PRE2_N = '0' and CLR2_N = '1') then -- Preset the output Q as High(1)
Q2 <= '1';
Q2_N <= '0';
elsif ( PRE2_N = '1' and CLR2_N = '0') then -- Clear the output Q as Low(0)
Q2 <= '0';
Q2_N <= '1';
elsif ( rising_edge(CLOCK2) ) then -- PRE_N and CLR_N are inactive (both are 1) and
-- rising edge of clock happen
Q2 <= D2;
Q2_N <= not D2;
end if;
end process;
end behavioral;
174
Chapter 10 ■ Sequential Logic: IF This, THEN That
■■Tips d_ff_1_p and d_ff_2_p are labels for the individual processes. This is not required by VHDL, but the
labels can improve the description of the process and make it easier for other designers to read and understand.
175
Chapter 10 ■ Sequential Logic: IF This, THEN That
4.
Type the following script in the ModelSim Transcript window. The first one runs
the simulation and the second one adds all the signals to the window.
a. vsim work.d_flipflop_7474_example
b. add wave -position insertpoint sim:/d_flipflop_7474_example/*
5.
Create two separate clocks for two different flip-flops
a. force -freeze sim:/d_flipflop_7474_example/CLOCK1 1 0, 0 {50 ps} -r 100
b. force -freeze sim:/d_flipflop_7474_example/CLOCK2 1 0, 0 {35 ps} -r 70
6.
Initialize all of the input signals
a. force -freeze sim:/d_flipflop_7474_example/PRE1_N 0 0
b. force -freeze sim:/d_flipflop_7474_example/CLR1_N 0 0
c. force -freeze sim:/d_flipflop_7474_example/D1 0 0
d. force -freeze sim:/d_flipflop_7474_example/PRE2_N 0 0
e. force -freeze sim:/d_flipflop_7474_example/CLR2_N 0 0
f. force -freeze sim:/d_flipflop_7474_example/D2 0 0
176
Chapter 10 ■ Sequential Logic: IF This, THEN That
7.
Run the simulation for 275 ps (all three letters (run) are NOT capital letters)
a. run 275 ps
8.
Set both preset values to high
a. force -freeze sim:/d_flipflop_7474_example/PRE1_N 1 0
b. force -freeze sim:/d_flipflop_7474_example/PRE2_N 1 0
9.
Run the simulation for 125 ps (all three letters (run) are NOT capital letter)
a. run 125 ps
10.
Change preset and clear value
a. force -freeze sim:/d_flipflop_7474_example/PRE1_N 0 0
b. force -freeze sim:/d_flipflop_7474_example/CLR1_N 1 0
c. force -freeze sim:/d_flipflop_7474_example/PRE2_N 0 0
d. force -freeze sim:/d_flipflop_7474_example/CLR2_N 1 0
11.
Run the simulation for 125 ps
a. run 125 ps
12.
Set the Preset to high
a. force -freeze sim:/d_flipflop_7474_example/PRE1_N 1 0
b. force -freeze sim:/d_flipflop_7474_example/PRE2_N 1 0
13.
Run the simulation for 225 ps
a. run 225 ps
14.
Set both D inputs to high
a. force -freeze sim:/d_flipflop_7474_example/D1 1 0
b. force -freeze sim:/d_flipflop_7474_example/D2 1 0
15.
Run the simulation for 125 ps
a. run 125 ps
In Figure 10-3, the two flip-flops in the d_flipflop_7474_example are driven by the same input except
CLOCK1 and CLOCK2 are different. The red arrow (A) in the figure shows that Q1 and Q1_N change on
the rising edge of CLOCK1. Q2 and Q2_N change on the rising edge of CLOCK2 (red arrow B). This dual
flip-flop design shows that sequential statements ONLY execute when the IF condition is true (which
is the clock input). The Q output also depends on the D input. Q1 and Q1_N follow the D1 input on the
rising edge of CLOCK1 (red arrows C and D). Q2 and Q2_N follow the D2 input on the rising edge of
CLOCK2 (red arrows E and F).
177
Chapter 10 ■ Sequential Logic: IF This, THEN That
10.1.2 Shift Registers
In the last example, one D flip-flop only stores one bit of data (the Q output). When a group of n flip-flops
is used to store n bits of data, we say these flip-flops are a register. Using different ways to group flip-flops
together will form a different type of register which provides different functions. Shift registers are one of the
commonly used types of registers.
In this section, we will show you the simplest shift register example: the serial in serial out shift register.
It works like a time delay module. When anything happens on the input, it will happen on the output after
x amount of time. The unit of time is the clock period on the clock input. It provides a discrete delay of the
digital signal. A clock synchronized signal is delayed by “n” discrete clock cycle times, where “n” is the
number of flip-flops in the shift register. Therefore, an eight flip-flop shift register delays input data by eight
clock cycles.
■■Note Shift registers are used in many places (e.g., pseudo random generator, serial communication,
delay input, and storage).
Four-bit shift registers can be built by four D flip-flops. They are connected serially as in Figure 10-4.
The data bits are loaded into the shift register in a serial fashion using the SERIAL_IN input. The values of
each D flip-flop are sending to the next D flip-flop whose Q output is connected to D output, at each positive
edge of the CLOCK.
178