Shift Registers and Counters
In questa pagina 3
This note describes two widely used registers, shift registers and counters, building on Registers and Load ControlAn n-bit register is n flip-flops with a common clock, and a counter is a register that steps through a sequence of states. Loading can be controlled by clock gating (an OR with the clock, which saves power but adds delays and clock skew) or, preferably, by load enable (a 2-to-1 MUX before each D input selecting the new data or the flip-flop's own output). Complex systems are split into a datapath (registers and arithmetic-logic blocks) and a control unit (an FSM that sends control signals and receives status signals).Registers and Load Control → and Microoperations and RTLA microoperation is an elementary operation on register data, written in Register Transfer Language (RTL), for example K1: R2 <- R1 meaning that when K1 is 1 the content of R1 is copied into R2 at the clock edge. Microoperations are transfers (simultaneous ones separated by commas exchange contents), arithmetic (add, subtract, increment, decrement), logic bit by bit with masks (AND clears, OR sets to 1, XOR complements) and shifts (sl towards the MSB doubles, sr towards the LSB halves).Microoperations and RTL →.
Shift registers
Definition (shift register). A shift register is a chain of D flip-flops in cascade (each output is the next input), all with the same clock; at every active edge the bits shift sideways by one position (right, left, or both directions).
Simplest 4-bit register (only FFs): serial input SI = input of the first FF; serial output SO = output of the last; shift at every rising edge.
Example. 4-bit shift register, carrying (one bit per cycle, starting from ); the bit enters and advances towards :
| edge | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | |
| 0001 | 0010 | 0101 | 1011 | 0110 | 1100 | 1000 | 0001 | 0011 | 0110 | |
| 0 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 0 | 0 |
After 4 edges the first bit () reaches : the serial output reproduces the input with a delay of 4 clock cycles (a delay line).
Shift enable. Shifting can be enabled by gating the clock with (as for ), with the same skew problems: better a control at the FF input (multiplexer with hold).
Parallel load
If the input of each FF is accessible, the register can be loaded in parallel, and if all outputs are accessible the data can be read in parallel. This converts parallel to serial and serial to parallel.
Each stage: three ANDs, an OR and a D FF; two control signals and :
| operation | ||
|---|---|---|
| 0 | 0 | hold |
| 0 | 1 | parallel load of |
| 1 | X | shift (from towards ) |
Property (shift and parallel load). In RTL: ( enters , each , towards the MSB) and ; hold is implicit when no condition holds. Shift has priority over load.
Example. With , and (even if ): .
For shifting in the opposite direction the circuit is the same with the indices of and reversed: .
Bidirectional (universal) shift register
Each stage gets a 4-to-1 multiplexer with select :
| operation | ||
|---|---|---|
| 0 | 0 | hold |
| 0 | 1 | shift left |
| 1 | 0 | shift right |
| 1 | 1 | parallel load |
Serial inputs LSI (left serial input, enters in shift left) and RSI (right serial input, enters in shift right); serial outputs (shift left) and (shift right). In RTL: ; ; .
Uses
- Serial interfaces: moving bits to a distant receiver on lines is expensive (area, delay, power, noise); with shift registers a single line is enough: the transmitter loads data in parallel, bits travel serially, the receiver collects them in a shift register and reads them in parallel once all have arrived.
- Multiplication (sum of shifted partial products).
- Pseudo-random number generator: feed the input with the XOR of some register bits.
- Delay line: choose the delay by changing the clock frequency or the tap.
Counters
Definition (counter). A counter is a register that, in response to a series of inputs (the clock), goes through a predefined sequence of states (binary numbers, random sequence, ...), giving a count at its output. An -bit binary counter counts from to . Two categories: ripple (asynchronous) and synchronous.
Example. A 4-bit binary counter counts to and restarts from .
Ripple counter
Definition (ripple counter). A chain of D FFs whose outputs give the binary number. The LSB flip-flop receives the system clock; each next flip-flop receives the complemented output of the previous one as clock; the input of each FF is tied to its own , so at the rising edge of its clock each FF toggles. An asynchronous reset clears the register.
toggles at every system clock edge; whenever goes ; whenever falls; and so on. From : after the first edge ; after the second ; the sequence is .
The settling time depends on the number of FFs: from to the flip-flops change one at a time in cascade, and for a time the outputs pass through intermediate values . Reading in this interval gives wrong values.
Down counter: if each FF is clocked by the direct (not complemented) output of the previous one, the count goes down.
Pros and cons. Very simple structure; but it is asynchronous: output bits have different delays with respect to the input clock, growing with the number of bits. Used where speed is not critical: low-frequency clock or rare events as clock.
Synchronous binary counter
All flip-flops share the same clock: they switch together, not in cascade.
Definition (synchronous binary counter). Input (enables counting) and output (carry output, for multi-stage counters). With , at each edge the stored value is incremented by 1. Each XOR adds 0 or 1 to the stored bit (bit toggles when all lower bits are 1), each AND propagates to the next stage. The delay is that of 4 ANDs for and 3 ANDs plus an XOR for .
Example. With and the next edge gives .
Parallel version. Replacing the chain of 2-input ANDs with multi-input ANDs in parallel gives a faster counter. Connecting of one 4-bit counter to of a second one gives an 8-bit counter. Compared with the ripple counter, the synchronous one is more complex but faster and less prone to timing problems.
VHDL
-bit register with synchronous load and asynchronous active-low clear (generic parameter):
library IEEE;
use IEEE.std_logic_1164.all;
entity register_wLoad is
generic ( n : integer := 8 );
port ( clk, clear_n, load : in std_logic;
D : in std_logic_vector(n-1 downto 0);
Q : out std_logic_vector(n-1 downto 0) );
end register_wLoad;
architecture beh of register_wLoad is
begin
process (clk, clear_n)
begin
if clear_n = '0' then
Q <= (others => '0');
elsif clk'event and clk = '1' then
if load = '1' then Q <= D; end if; -- load = '0': hold
end if;
end process;
end beh;4-bit shift register with asynchronous reset (serial input SI into , serial output SO ):
entity ShiftRegister_4_r is
port ( CLK, RST, SI : in std_logic;
SO : out std_logic;
Q : out std_logic_vector(3 downto 0) );
end ShiftRegister_4_r;
architecture beh of ShiftRegister_4_r is
signal shift : std_logic_vector(3 downto 0);
begin
process (RST, CLK)
begin
if RST = '1' then
shift <= "0000";
elsif CLK'event and CLK = '1' then
shift <= shift(2 downto 0) & SI; -- shift towards the MSB
end if;
end process;
Q <= shift;
SO <= shift(3);
end beh;4-bit counter with enable, asynchronous reset and carry-out:
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
entity Counter_4_r is
port ( CLK, RST, EN : in std_logic;
Q : out std_logic_vector(3 downto 0);
CO : out std_logic );
end Counter_4_r;
architecture beh of Counter_4_r is
signal count : std_logic_vector(3 downto 0);
begin
process (RST, CLK)
begin
if RST = '1' then
count <= "0000";
elsif CLK'event and CLK = '1' then
if EN = '1' then
count <= std_logic_vector(unsigned(count) + 1);
end if;
end if;
end process;
Q <= count;
CO <= '1' when (EN = '1' and count = "1111") else '0';
end beh;With the count advances at every rising edge and is 1 when the count is ; with the count is frozen.
Versione ripasso
- Shift register: chain of D FFs, same clock; SI enters , SO ; 4 edges of delay (delay line). Enable via FF-input MUX rather than clock gating.
- Example: from gives .
- Parallel load: ; ; hold otherwise; Shift beats Load. , , : . Reverse direction: .
- Universal register (4-to-1 MUX per stage, ): hold, shift left, shift right, load. LSI, RSI inputs.
- Uses: serial/parallel conversion, single-line serial interface, multiplication, pseudo-random generator (XOR feedback), delay line.
- Counter: register stepping through states; binary -bit counts to .
- Ripple counter: each FF clocked by the complemented output of the previous one, (toggle); passes (about ); direct output as clock gives a down counter; simple but asynchronous, delay grows with .
- Synchronous counter: one clock, XORs toggle bit when all lower bits are 1, ANDs propagate ; for cascading (two 4-bit counters make an 8-bit one); parallel ANDs make it faster.
- VHDL: register with
if load = '1' then Q <= D;after async clear; shiftshift <= shift(2 downto 0) & SI; countercount + 1when , and . - Mistakes: ripple delay grows with , synchronous much less; goes towards the MSB.