registers and shift registers discussion d8.2. d flip-flop 0 0 1 1 1 0 x 0 q 0 ~q 0 d clk q ~q d...

22
Registers and Shift Registers Discussion D8.2

Post on 22-Dec-2015

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

Registersand Shift Registers

Discussion D8.2

Page 2: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

D Flip-Flop

0 0 11 1 0X 0 Q0 ~Q0

D CLK Q ~Q

D gets latched to Q on the rising edge of the clock.

Positive edge triggered

CLK

D Q

~QCLK

D Q

~Q

Page 3: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

A 1-Bit Register

CLK

D Q

~Q

CLK

Q0

~Q0

LOAD

INP0

Q0next = Q0 & ~LOAD | INP0 & LOAD

reg1Q0

~Q0

LOAD

INP0

CLK

Page 4: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

A 1-Bit Register

If LOAD = 1, then INP0 gets latched to Q0 on the rising edge of the clock, CLK

reg1Q0

~Q0

LOAD

INP0

CLK

Page 5: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

A 4-Bit Register

reg1Q0

~Q0

LOAD

INP0

reg1Q1

~Q1INP1

reg1Q2

~Q2INP2

reg1Q3

~Q3INP3

CLK

reg1Q0

~Q0

LOAD

INP0

CLK

Page 6: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

Implementing Registers in Verilog

// A 4-bit register with asynchronous clear and loadmodule reg4(Clk,Clear,Load,D,Q);

input [3:0] D;input Clk,Clear,Load;output [3:0] Q;reg [3:0] Q;

always @(posedge Clk or posedge Clear)if(Clear == 1)

Q <= 0;else if(Load)

Q <= D;endmodule Reg

Clear

Clk

D(3:0)

Load

Q(3:0)

Page 7: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

4-Bit Shift Register

CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q

data_in

CLK

Q0Q1Q2Q3

Page 8: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

shift4.vmodule ShiftReg(clk,clr,data_in,Q); input clk; input clr; input data_in; output [3:0] Q;

reg [3:0] Q;

// 4-bit Shift Register always @(posedge clk or posedge clr)begin if(clr == 1)

Q <= 0; else

begin Q[3] <= data_in; Q[2:0] <= Q[3:1];end

endendmodule

CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q

data_in

CLK

Q0Q1Q2Q3

Note non-blocking assignment

Page 9: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

shift4 simulation

Page 10: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

Ring Counter

CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q

CLK

Q0Q1Q2Q3

Page 11: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

ring4.vmodule ring4(clk,clr,Q); input clk; input clr; output [3:0] Q;

reg [3:0] Q;

// 4-bit Ring Counter always @(posedge clk or posedge clr)begin if(clr == 1)

Q <= 1; else

begin Q[3] <= Q[0]; Q[2:0] <= Q[3:1];end

endendmodule

Page 12: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

ring4 simulation

Page 13: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

Johnson Counter

CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q

CLK

Q3 Q2 Q1 Q0

Page 14: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

module johnson4(clk,clr,Q); input clk; input clr; output [3:0] Q;

reg [3:0] Q;

// 4-bit Johnson Counter always @(posedge clk or posedge clr)begin if(clr == 1)

Q <= 0; else

begin Q[3] <= ~Q[0]; Q[2:0] <= Q[3:1];end

endendmodule

johnson4.v

Page 15: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

Johnson Counter

Page 16: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q

CLK

Q3 Q2 Q1 Q0

A Random Number Generator

Page 17: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q CLK

D Q

!Q

CLK

Q3 Q2 Q1 Q0

Q3 Q2 Q1 Q0

0 0 0 1 11 0 0 0 81 1 0 0 C1 1 1 0 E1 1 1 1 F0 1 1 1 71 0 1 1 B0 1 0 1 5

Q3 Q2 Q1 Q0

1 0 1 0 A1 1 0 1 D0 1 1 0 60 0 1 1 31 0 0 1 90 1 0 0 40 0 1 0 20 0 0 1 1

Page 18: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

module rand4(clk,clr,Q); input clk; input clr; output [3:0] Q;

reg [3:0] Q;

// 4-bit Random number generator always @(posedge clk or posedge clr)begin if(clr == 1)

Q <= 1; else

begin Q[3] <= Q[3] ^ Q[0]; Q[2:0] <= Q[3:1];end

endendmodule

rand4.v

Page 19: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

A Random Number Generator

Page 20: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

clk

inp

Q2

Q0

Q1

outp

Clock Pulse

Page 21: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

module clk_pulse(clk,clr,inp,outp); input clk; input clr; input inp; output outp;

wire outp; reg [2:0] Q;

// clock pulse generator always @(posedge clk or posedge clr)begin if(clr == 1)

Q <= 0; else

begin Q[2] <= inp; Q[1:0] <= Q[2:1];end

endassign outp = Q[2] & Q[1] & ~Q[0];

endmodule

clk_pulse.v

Page 22: Registers and Shift Registers Discussion D8.2. D Flip-Flop 0 0 1 1 1 0 X 0 Q 0 ~Q 0 D CLK Q ~Q D gets latched to Q on the rising edge of the clock. Positive

clk

inpQ2

Q0Q1

outp