Salta al contenuto
Note per Studenti Shift Registers and Counters Report

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, SISI carrying 1,0,1,1,0,0,0,1,1,01,0,1,1,0,0,0,1,1,0 (one bit per cycle, starting from 00000000); the bit enters Q0Q_0 and advances towards Q3Q_3:

edge 1 2 3 4 5 6 7 8 9 10
SISI 1 0 1 1 0 0 0 1 1 0
Q3Q2Q1Q0Q_3Q_2Q_1Q_0 0001 0010 0101 1011 0110 1100 1000 0001 0011 0110
SO=Q3SO=Q_3 0 0 0 1 0 1 1 0 0 0

After 4 edges the first bit (11) reaches SOSO: 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 ShiftShift (as for LoadLoad), 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 ShiftShift and LoadLoad:

ShiftShift LoadLoad operation
0 0 hold
0 1 parallel load of DiD_i
1 X shift (from Q0Q_0 towards Q3Q_3)

Property (shift and parallel load). In RTL: Shift: Q←sl QShift:\ Q\leftarrow sl\,Q (SISI enters Q0Q_0, each Qi→Qi+1Q_i\to Q_{i+1}, towards the MSB) and Shift‾⋅Load: Q←D\overline{Shift}\cdot Load:\ Q\leftarrow D; hold is implicit when no condition holds. Shift has priority over load.

Example. With Q=0110Q=0110, SI=1SI=1 and Shift=1Shift=1 (even if Load=1Load=1): Q←sl Q=1101Q\leftarrow sl\,Q=1101.

For shifting in the opposite direction the circuit is the same with the indices of DiD_i and QiQ_i reversed: Shift: Q←sr QShift:\ Q\leftarrow sr\,Q.

Bidirectional (universal) shift register

Each stage gets a 4-to-1 multiplexer with select S1S0S_1S_0:

S1S_1 S0S_0 operation
0 0 hold
0 1 shift left
1 0 shift right
1 1 parallel load

Serial inputs LSI (left serial input, enters Q0Q_0 in shift left) and RSI (right serial input, enters Q3Q_3 in shift right); serial outputs Q3Q_3 (shift left) and Q0Q_0 (shift right). In RTL: S1S0‾: Q←sr QS_1\overline{S_0}:\ Q\leftarrow sr\,Q; S1‾S0: Q←sl Q\overline{S_1}S_0:\ Q\leftarrow sl\,Q; S1S0: Q←DS_1S_0:\ Q\leftarrow D.

Uses

  • Serial interfaces: moving nn bits to a distant receiver on nn 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 nn-bit binary counter counts from 00 to 2n−12^n-1. Two categories: ripple (asynchronous) and synchronous.

Example. A 4-bit binary counter counts 00 to 24−1=152^4-1=15 and restarts from 00000000.

Ripple counter

Definition (ripple counter). A chain of D FFs whose outputs Q0,Q1,Q2,Q3Q_0,Q_1,Q_2,Q_3 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 DD input of each FF is tied to its own Q‾\overline Q, so at the rising edge of its clock each FF toggles. An asynchronous reset clears the register.

Q0Q_0 toggles at every system clock edge; Q1Q_1 whenever Q0Q_0 goes 1→01\to0; Q2Q_2 whenever Q1Q_1 falls; and so on. From 00000000: after the first edge 00010001; after the second 00100010; the sequence is 0000,0001,0010,…,1111,0000,…0000,0001,0010,\dots,1111,0000,\dots.

The settling time depends on the number of FFs: from 01110111 to 10001000 the flip-flops change one at a time in cascade, and for a time 4 tp4\,t_p the outputs pass through intermediate values 0111→0110→0100→0000→10000111\to0110\to0100\to0000\to1000. 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 ENEN (enables counting) and output COCO (carry output, for multi-stage counters). With EN=1EN=1, at each edge the stored value is incremented by 1. Each XOR adds 0 or 1 to the stored bit (bit ii toggles when all lower bits are 1), each AND propagates ENEN to the next stage. The delay is that of 4 ANDs for COCO and 3 ANDs plus an XOR for Q3Q_3.

Example. With EN=1EN=1 and Q=0111Q=0111 the next edge gives Q=1000Q=1000.

Parallel version. Replacing the chain of 2-input ANDs with multi-input ANDs in parallel gives a faster counter. Connecting COCO of one 4-bit counter to ENEN 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

nn-bit register with synchronous load and asynchronous active-low clear (generic parameter):

vhdl
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 Q0Q_0, serial output SO =Q3=Q_3):

vhdl
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:

vhdl
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 EN=1EN=1 the count advances at every rising edge and COCO is 1 when the count is 11111111; with EN=0EN=0 the count is frozen.

Versione ripasso

  • Shift register: chain of D FFs, same clock; SI enters Q0Q_0, SO =Q3=Q_3; 4 edges of delay (delay line). Enable via FF-input MUX rather than clock gating.
  • Example: SI=1,0,1,1,0,0,0,1,1,0SI=1,0,1,1,0,0,0,1,1,0 from 00000000 gives Q3Q2Q1Q0=0001,0010,0101,1011,0110,1100,1000,0001,0011,0110Q_3Q_2Q_1Q_0=0001,0010,0101,1011,0110,1100,1000,0001,0011,0110.
  • Parallel load: Shift:Q←sl QShift:Q\leftarrow sl\,Q; Shift‾ Load:Q←D\overline{Shift}\,Load:Q\leftarrow D; hold otherwise; Shift beats Load. Q=0110Q=0110, SI=1SI=1, Shift=1Shift=1: 11011101. Reverse direction: Q←sr QQ\leftarrow sr\,Q.
  • Universal register (4-to-1 MUX per stage, S1S0S_1S_0): 0000 hold, 0101 shift left, 1010 shift right, 1111 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 nn-bit counts 00 to 2n−12^n-1.
  • Ripple counter: each FF clocked by the complemented output of the previous one, D=Q‾D=\overline Q (toggle); 0111→10000111\to1000 passes 0110,0100,00000110,0100,0000 (about 4tp4t_p); direct output as clock gives a down counter; simple but asynchronous, delay grows with nn.
  • Synchronous counter: one clock, XORs toggle bit ii when all lower bits are 1, ANDs propagate ENEN; COCO 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; shift shift <= shift(2 downto 0) & SI; counter count + 1 when EN=1EN=1, CO=ENCO=EN and count=1111count=1111.
  • Mistakes: ripple delay grows with nn, synchronous much less; slsl goes towards the MSB.

Teoria collegata