registers and counters - eecs.yorku.ca · • use a mux (it can get messy with gates) counters •...

65
Registers and Counters Two of the most common uses of Flip-Flops Both are collections of Flip-Flops The registers perform operations like load, clear, shift Counters count up or down, load, clear

Upload: hoangdang

Post on 25-May-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Registers and Counters

• Two of the most common uses of Flip-Flops

• Both are collections of Flip-Flops

• The registers perform operations like load, clear, shift

• Counters count up or down, load, clear

Page 2: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Synchronous Circuits

• We deal mostly with synchronous circuits

– several exceptions in this chapter

• One (wise) convention is that there is no combinational logic between the clock and the F-F.

• The clock pulse should reach all F-Fs at the same time. No skew

Page 3: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Parallel Load

• The first and most obvious thing to do with a register

• The D F-F loads data at every cycle. We want to control that

• Very easy: put an AND gate before the clock

Page 4: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

!!WRONG!!

• No logic before the clock! It is not purebred synchronous otherwise

• Still useful: asynchronous circuits are used widely.

• We can also add an asynchronous clear. We use the letter R for this to distinguish it from the C of the clock

Page 5: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 6: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Will not Look Pretty

• The synchronous version will be complex

• The idea is to load the new data when we want them loaded

• And load the old data back in when we want to keep them

• Needs more gates, but that’s life

Page 7: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 8: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Shift Registers

• Shift is a very common operation

– mult/divide by 2, 4, 8 etc

– serial load/store

• Very easy if we only want to shift

Page 9: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 10: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Data Transfer

• We can transfer data serially

• With an AND gate and two simple shift registers, we can do it (asynchronously)

• We omit many “small” details though...

– like we need a counter

Page 11: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 12: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Serial Adder

• Serial adders have some advantages

– little hardware

– no worries about carry propagation delays

– really high clock speeds

– can have any number of bits

• Used to be more common...

Page 13: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

We Need...

• Two serial registers

• one full adder

• one F-F to remember the carry

• clock circuitry

• a bit of intuition

Page 14: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 15: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Use the Force

• We know how to design sequential circuits

• We can apply the design techniques to our serial adder and might save a gate or two

Page 16: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Serial Adder State Table

PresentState

NextStateInput

Out-put

Flip-FlopInputs

Q

00001111

xy

0001101100011011

Q

00010111

S

01101001

Page 17: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Serial Adder State Table

PresentState

NextStateInput

Out-put

Flip-FlopInputs

Q

00001111

xy

0001101100011011

Q

00010111

S

01101001

JK

0X0X0X1XX1X0X0X0

Page 18: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 19: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Universal Shift Register

• Can shift left, right, load, or do nothing

• We need two control inputs

– 00 do nothing

– 01 shift right

– 10 shift left

– 11 parallel load

• Use a MUX (it can get messy with gates)

Page 20: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 21: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Counters

• We are interested mainly in binary counters

• We start with asynchronous counters

– they are called ripple counters

• They have many uses in digital and non-digital systems

Page 22: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Ripple Counters

• In asynchronous circuits we are allowed to play with the clock

• We use trailing edge F-F (we could use leading edge too)

• T F-F are ideal for counters

• But D F-F can also be used

Page 23: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Basic Operation

• When counting up a F-F flips when the preceding F-F goes from high to low

• A T F-F should have its input connected to 1

• A D F-F should have its input connected to the complement of its output.

Page 24: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 25: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Ripple Down Counters

• Have nothing to do with settling the score after a pillow fight!

• Really easy to do with ripple counters: Either

– use positive edge F-F

– connect the clock of a stage to the complement of the previous stage

Page 26: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

BCD Ripple Counter

• Should count from zero to nine

• At nine should cycle back to zero

• We can dream up a solution and verify it

– it is after all an ad-hoc solution

Page 27: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 28: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 29: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Cascading BCD Ripple Counters

• We can connect several such counters to create multibit counters

• The MSB of every stage is connected to the clock input of the next

Page 30: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 31: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Synchronous Counters

• That’s what we know how to do well!

• We can use the standard design procedures

• Or observe that a bit toggles if all its predecessors are 1

– the opposite for down-counters

Page 32: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 33: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Down Counters

• We can easily convert the up-counter to down-counter

• We only need to use the complement of the output of a F-F instead of its straight output

Page 34: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

How about Both

• The Up-Down inputs connect either the output or the complement of the output to the next stage.

• We should make sure that if both up and down are true the circuit does something meaningful

Page 35: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 36: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Let’s Get Ambitious

• Design a counter with

– count up

– parallel load

– no-op

– asynchronous clear

Page 37: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 38: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

This Thing at the Top

• This looks redundant

• But it really is a buffer to protect the circuit driving the LOAD input

– If we had 16 bits, then the circuit driving LOAD might not be able drive LOAD quickly. The buffer isolates and protects it.

Page 39: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Synchronous BCD Counter

• Less of a black art than designing a BCD ripple counter

• We just have to load zero in the F-F when the count reaches 9

• Or we set the asynchronous clear when the count reaches 10

Page 40: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 41: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Counter with Unused States

• Unused states are the source of don’t cares

• One concern is recovery from an error (e.g. due to power fluctuation)

• We design the circuit the usual way

• Then analyze it to see what happens after we go to an unused state

Page 42: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

State Table

PresentState

NextState Flip-Flop Inputs

A B C

0 0 00 0 10 1 00 1 11 0 01 0 11 1 01 1 1

A B C

0 0 10 1 01 0 0x x x1 0 11 1 00 0 0x x x

Page 43: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

State Table

PresentState

NextState Flip-Flop Inputs

A B C

0 0 00 0 10 1 00 1 11 0 01 0 11 1 01 1 1

A B C

0 0 10 1 01 0 0x x x1 0 11 1 00 0 0x x x

J K J K J K

0 X 0 X 1 X0 X 1 X X 11 X X 1 0 XX X X X X XX 0 0 X 1 XX 0 1 X X 1X 1 X 1 0 XX X X X X X

Page 44: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 45: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Ring Counters

• Timing signals can be generated by a regular counter plus a decoder

• Alternatively can be generated by a ring counter

• A single bit is shifted along a circular shift register

Page 46: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Cost of Ring Counters

• Ring counters need more F-F

• They do not need a decoder

• They have a large number of unused states

– and thus create lots of don’t cares in the accompanying combinational logic. But we do not design them in a systematic way.

Page 47: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 48: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Advantages-Disadvantages

• More F-F but less combinational logic

• Much less complex wiring

• Reinitialization after an error is hard (we need to be able to specify which F-F are set and which are reset during start-up)

Page 49: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Johnson Counter

• The shift register is a switch tail ring

• Needs half the F-F

• Needs a few two-input gates

Page 50: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 51: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Advantages-Disadvantages

• Needs half the number of F-F

• Needs only 2-input gates

• But it still needs gates

• And still needs more than log N F-F

• How about error recovery?

Page 52: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Error Recovery

• There are two ways:

– Detect the illegal state and reset every F-F

– Nudge the counter towards a legal state by assigning

• D_c = (A+C)B

• It will eventually go to a legal state.

Page 53: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

HDL for Registers

• Can be done using either structural or behavioral techniques

• Registers can be used (instantiated) then as modules

• We do a few examples

Page 54: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Universal Shift Register

• Design a 4-bit universal shift register

• Make it synchronous

• Do behavioral first

Page 55: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Universal Shift Registermodule shftreg (s1,s0,Pin,lfin,rtin,A,CLK,Clr);   input s1,s0;                     //Select inputs   input lfin, rtin;                //Serial inputs         input CLK,Clr;                   //Clock and Clear   input [3:0] Pin;                 //Parallel input   output [3:0] A;                  //Register output   reg [3:0] A;   always @ (posedge CLK or negedge Clr)     if (~Clr) A = 4'b0000;      else        case ({s1,s0})         2'b00: A = A;              //No change         2'b01: A = {rtin,A[3:1]};  //Shift right         2'b10: A = {A[2:0],lfin};  //Shift left         2'b11: A = Pin;            //Parallel load input        endcaseendmodule

Page 56: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Structural Version

• We have to define the building block

• Then instantiate it several times

Page 57: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Structural Versionmodule SHFTREG (I,select,lfin,rtin,A,CLK,Clr);   input [3:0] I;            //Parallel input   input [1:0] select;       //Mode select   input lfin,rtin,CLK,Clr;  //Serial inputs,clock,clear   output [3:0] A;           //Parallel output //Instantiate the four stages   stage ST0 (A[0],A[1],lfin,I[0],A[0],select,CLK,Clr);   stage ST1 (A[1],A[2],A[0],I[1],A[1],select,CLK,Clr);   stage ST2 (A[2],A[3],A[1],I[2],A[2],select,CLK,Clr);   stage ST3 (A[3],rtin,A[2],I[3],A[3],select,CLK,Clr);endmodule 

Page 58: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

The Building Block   input [1:0] select;   output Q;   reg Q;   reg D;//4x1 multiplexer   always @ (i0 or i1 or i2 or i3 or select)          case (select)            2'b00: D = i0;            2'b01: D = i1;            2'b10: D = i2;            2'b11: D = i3;         endcase//D flip­flop    always @ (posedge CLK or negedge Clr)         if (~Clr) Q = 1'b0;         else Q = D;endmodule

Page 59: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

A Synchronous Counter

• Not much harder given that Verilog has the addition operation built in

• The only tricky thing is the carry-out

Page 60: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Synchronous Countermodule counter (Count,Load,IN,CLK,Clr,A,CO);  input Count,Load,CLK,Clr;  input [3:0] IN;                //Data input  output CO;                     //Output carry  output [3:0] A;                //Data output  reg [3:0] A;  assign CO = Count & ~Load & (A == 4'b1111);  always @ (posedge CLK or negedge Clr)    if (~Clr) A = 4'b0000;    else if (Load)  A = IN;            else if (Count) A = A + 1'b1;      else A = A;                  // no change, default condition endmodule

Page 61: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Ripple Counter

• Yes, even asynchronous circuits can be described in Verilog

• Use structural specification for the counter and behavioral for the F-F that constitutes the building block.

Page 62: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Ripple Countermodule ripplecounter (A0,A1,A2,A3,Count,Reset);   output A0,A1,A2,A3;   input Count,Reset;//Instantiate complementing flip­flop   CF F0 (A0,Count,Reset);   CF F1 (A1,A0,Reset);   CF F2 (A2,A1,Reset);   CF F3 (A3,A2,Reset);endmodule 

Page 63: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input

Complementing F-Fmodule CF (Q,CLK,Reset);   output Q;   input CLK,Reset;   reg Q;   always @ (negedge CLK or posedge Reset)      if (Reset) Q = 1'b0;      else  Q = #2 (~Q);     // Delay of 2 time units endmodule

Page 64: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input
Page 65: Registers and Counters - eecs.yorku.ca · • Use a MUX (it can get messy with gates) Counters • We are interested mainly in binary counters ... • A T F-F should have its input