verilog design of sequential logic · l4: verilog design of sequential logic 2012/2013-1 ⋅...

33
L-4: Verilog Design of Sequential Logic 2012/2013-1 VERILOG DESIGN OF SEQUENTIAL LOGIC CAD for ASIC Design 1

Upload: others

Post on 11-May-2020

87 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

VERILOG DESIGN OF

SEQUENTIAL LOGIC

CAD for ASIC Design 1

Page 2: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

⋅ Synchronous sequential logic circuits rely on storage elements for their operations. 

⋅ Flips­flops   (FFs)   and   latches   are   two   commonly   used   one­bit storage elements. 

⋅ Others as registers, shift registers, and counters. ⋅ Only synchronous sequential logic is considered.

CAD for ASIC Design 2

Page 3: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

LATCHES & FLIP­FLOPS

⋅ A latch is a level­sensitive memory device (transparent). o As   long   as   the   pulse   remains   at   the   active   high   level,   any 

changes in the data input will change the state of the latch.  o A flip­flop (FF) is an edge­triggered memory device. 

⋅ An edge­triggered FF ignores the pulse while it is at a constant level (non­transparent).o Triggers only during a transition of the clock signal.o Could on the positive edge of the clock (posedge),  or negative 

edge (negedge).

CAD for ASIC Design 3

Page 4: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

D Latch

⋅ A latch is inferred because the IF statement is incomplete.⋅ The notion of implied memory is instantiated in this case.

CAD for ASIC Design 4

always @ (S or Data_In)if ( S )     Data_out = Data_In;

Page 5: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Signal edge detection in Verilog 

⋅ The  always  blocks  are   sensitive   to   the  levels  of   the  signals   that appear in the sensitivity list. 

⋅ FF changes only as a result of a clock transition of the clock signal, rather than its level. 

⋅ Verilog uses event qualifier posedge and negedge⋅ E.g.,  always @ (posedge clock). 

CAD for ASIC Design 5

Page 6: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Basic positive­edge triggered D flip­flop:

Note: Synthesis tools do not support mixed sensitivity, for example:always @ (posedge clock or reset).

CAD for ASIC Design 6

f/fD

Q

d

CLKq

module  FF (CLK, d, q) ;input  CLK, d  ; output q ;

           reg q  ;

always @ (posedge CLK)q = d ;

endmodule

Page 7: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

 Blocking and Non­Blocking Assignments

⋅ In   a   sequential   block,   blocking   assignment   statements   (=)   are evaluated immediately in the order they are specified. 

⋅ Called  blocked  assignments   because   a   statement   must   complete execution   (i.e.   update   memory)   before   the   next   statement   can execute.

CAD for ASIC Design 7

module  shiftregY (A, B, C, D, clk) ;input   D, clk  ; output A, B, C ;reg   A, B, C ;always @ (posedge clk) begin

C = D ;B = C ;A = B ;

endendmodule

qdD A

clk

Page 8: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

⋅ A  non­blocking   assignment  (  <=)   is   called  concurrent   procedural  assignments. 

⋅ Allows scheduling of  assignments  without  blocking execution of the statements that follow.

CAD for ASIC Design 8

module  shiftregX (A, B, C, D, clk) ;input  D, clk  ; output A, B, C;reg  A, B, C ;

always @ (posedge clk) begin

A <= B ;B <= C ;C <= D ;

endendmodule

module  shiftregY (A, B, C, D, clk) ;input   D, clk  ; output  A, B, C;reg   A, B, C;

always @ (posedge clk) begin

C <= D ;B <= C ;A <= B ;

endendmodule

Page 9: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Executed concurrently rather than sequentially.⋅ The order has no effect. ⋅ The statements  are  evaluated using  the values  when the  always 

block is entered. ⋅ The result of each non­blocking assignment is not seen until the end 

of the always block. 

CAD for ASIC Design 9

q q qd d dD C B A

clk

Fig. 4-1

Page 10: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Recommendation: 

When modelling logic that includes edge­triggerred register transfers:⋅ The synchronous  (i.e.  edge­sensitive)  operations  be  described by 

non­blocking assignments⋅ The   combinational   logic   be   described   with   blocking   assignment 

statements.

CAD for ASIC Design 10

Page 11: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Example 4­4:  D flip­flop with asynchronous reset.

Exercise:   Modify to include active­low asynchronous reset (RST) and preset (PST) inputs.

CAD for ASIC Design 11

f/fD

Q

Data_in

CLKData_out

clr

RST

module  FF2 (CLK, Data_in, RST, Data_out);input   CLK, Data_in, RST; output Data_out;reg       Data_out;

always @ (posedge RST or posedge CLK)if ( RST )  Data_out <= 0 ;else  Data_out <= Data_in ;

endmodule

Page 12: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Example 4­5:  Flip­flop with a tristate output

CAD for ASIC Design 12

f/f

D

Q

CLK

Data_outEN

Data_In

C

OE

module tri_DFF (CLK, EN, OE, Data_In, Data_Out ); input  CLK, EN, OE, Data_in;output Data_Out  ;

reg Temp ;

always @ (posedge CLK)     if (EN)  Temp = Data_In;

assign Data_Out = OE ? Temp : 1‘bz ; // tri­state function

endmodule

Page 13: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

 

Registers

⋅ Registers are just n­bit (n >1) structures consisting of FFs. ⋅ A common clock is used for each FF in the register.

CAD for ASIC Design 13

Page 14: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

CAD for ASIC Design 14

module reg8 (clock, load, rst, pst, data, Q);input clock, load ;

          input rst, pst ;         input [7:0] data ;

output [7:0] Q ;reg [7:0] Q ;

always @ (posedge rst or posedge pst or negedge clock)  

if  (rst)  Q <= 0 ;else if  (pst)  Q <= 1 ;else if  (load)  Q <= data ;

endmodule

reg8load

clock

data D

en

clkclr

rst

q Q8

8

pst

pre

Page 15: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

The timing diagram?

CAD for ASIC Design 15

clk

load

pst

rst

Q

data1 data2 data3data

Page 16: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Shift registers

CAD for ASIC Design 16

module shiftLreg8 (clk, rst, en, ldsh, w, d, q) ;

input [7:0]  d ;input  clk, rst, en, ldsh, w ;output [7:0]  q ;integer k;

    ....

always @ (negedge rst or posedge clk) beginif ( !rst )  

q <= 0 ;else if ( en ) 

     if ( ldsh )         q <= d;     else            begin          q[0] <= w;          for ( k = 1; k <8; k = k+1 )

q[k] <= q[k – 1];           end

endendmodule

Page 17: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

If replaced with this code, what is now the shift direction?

...for ( k = 0; k < 7; k = k+1 )

q[k] <= q[k + 1] ;q[7] <= w ;

...

CAD for ASIC Design 17

Page 18: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Example 4­8:  4­bit Universal Shift Register  

CAD for ASIC Design 18

module univShiftReg (MSBin, LSBin, s1, s0, clock, rst, DataIn, MSBout, LSBout, DataOut) 

input MSBin, LSBin, s1, s0  ;       input  clock, rst  ;       input  [3:0] Datain ;   

output MSBout, LSBout  ;output [3:0]  Dataout ;

reg [3:0]  Dataout ;

assign  MSBout  = DataOut[3] ;assign   LSBout  = DataOut[0] ;

...

...always @ (negedge clock)  begin

if ( rst )  // synchronous resetDataOut <= 0 ;

else case ( {s1, s0} )     2’b00  :  DataOut <= DataOut ; //  hold   // shift right   2’b01  :  DataOut <= { MSBin,DataOut[3:1] } ;    // shift left   2’b10  :  DataOut <= { DataOut[2:0], LSBin } ; 

   2’b11  :  DataOut <= DataIn ; // parallel  loadendcase

end endmodule

Page 19: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Exercise: ⋅ Sketch the iobd of this universal shift register⋅ Obtain   its   functional   block   diagram   in   terms   of   FFs   and   the 

associated glue logic.

CAD for ASIC Design 19

Page 20: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Synchronous Counters 

Example 4­9:  A 5­bit up­counter with a parallel load

CAD for ASIC Design 20

module upcount5 ( data, Q, clk, rst, ld, inc) ;input   [4:0] data  ;output  [4:0]  Q ;input   clk, rst ;input   ld, inc ;reg  [4:0]  Q ;

always @ ( posedge rst or posedge clk ) if ( rst )  Q <= 0 ;else if ( ld )  Q <= data ;else if ( inc )  Q <= Q + 1;

endmodule

Page 21: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Exercise:  Provide   the   I/O block diagram,  and derive   the  operations table for this counter.

CAD for ASIC Design 21

Page 22: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Example 4­10:  

⋅ Explain the counter.

CAD for ASIC Design 22

module U_D_counter ( data, count, clk, rst, dir ) ;input   [7:0] data  ;output  [7:0] count ;input   clk, rst ;input  [1:0] dir ;reg  [7:0]  count ;

always @ ( negedge rst or negedge clk ) if ( rst == 0 )  count <= 8’b00000000 ; 

else if (dir == 2’b00 || dir == 2’b11)  count <= count ; else if ( dir == 2’b01 )   count <= count + 1;

else if ( dir == 2’b10 )   count <= count ­1;endmodule

Page 23: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Simple Sequential Logic

Example 4­11: 

CAD for ASIC Design 23

module PULSER (CLK, PB, PB_Pulse) ;input CLK, PB ;output PB_Pulse ;reg  Q1, Q2 ;

always @ (posedge CLK) begin

Q1 <= PB;Q2 <= Q1;

end

assign PB_Pulse = ~ ( ~Q1 | Q2 ) ; endmodule

CLK

PB_Pulse

PBQ2

Page 24: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Example 4­12: Derive the circuit synthesized from this Verilog code fragment.

CAD for ASIC Design 24

module circuit4_12 (S1, CLK, A, B, C, S3, S2, OU) ;input S1, CLK ;input  [7:0]  A, B, C, S3 ;output [7:0]  S2, OU ;reg [7:0]  S2, OU ;wire [5:0]  TEMP1, TEMP2 ;   // internal signals

always @ (posedge CLK)if ( S1 )

if (TEMP1 < 8)  S2 <= A + B;else  S2 <= C;

always @ (TEMP2 or TEMP1 or S3 or A)if (TEMP2 > TEMP1)  OU <= S3 + A;

  ...endmodule

Page 25: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

It is important that the designer have good knowledge on hardware mapping. 

⋅ Depending   on   the   resource   constraints,   the   logic   synthesis   will generate one or two adders to execute the two addition operations. 

⋅ A circuit for comparing a variable and a constant is different from a circuit for comparing two variables. 

CAD for ASIC Design 25

Page 26: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Finite State Machine (FSM) 

⋅ A circuit type with memory.⋅ Usually as datapath controller unit. ⋅ Via algorithmic state machine (ASM) flowchart, an FSM is readily 

modelled in HDL. ⋅ We focus only on synthesizable models in this book.

CAD for ASIC Design 26

Page 27: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

⋅ There are two basic models of FSMs: Moore and Mealy. ⋅ In a Mealy machine, the next state (NS) and the outputs depend on 

both the present state (PS) and the inputs. ⋅ The NS of a Moore machine depends on the PS and the inputs, but 

the outputs depend on only the PS. ⋅ All FSMs have the general feedback structure.⋅ We   will   deal   only   with   synchronous   FSMs,   hence   the   state 

transitions of the machine are synchronized by the active edge of a common clock. In this case, the state register is consisted of edge­triggered flip­flops.

CAD for ASIC Design 27

Page 28: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Example 4­13: Verilog Modelling of an FSM (version A coding).

CAD for ASIC Design 28

ZA

A

A

A

S0

S1

S2

S3

1

0

0 1

0 1

0

1

Z

A

CLK

NSLogic

NS

Outputlogic

PS

Y1

Y0

y1

y0Z

Stateregister

Page 29: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

CAD for ASIC Design 29

module FSM1 ( A, CLK, reset, Z );input A, CLK, reset ;output Z ;reg [1:0] y,  Y ;parameter [1:0]   S0 = 2’b00,  S1 = 2’b01,  S2 = 2’b10, S3 = 2’b11;

    //  NS logic module:always @ (A or y)

 case  ( y ) S0  :  if ( A )   Y = S1;   else  Y = S0;  S1  :  if ( A ) Y = S3;  else  Y = S2;  S2  :  if ( A ) Y = S3;   else  Y = S2;  S3  :  if ( A ) Y = S0;   else  Y = S3;  

         endcase//  state register module:always @ ( negedge reset or posedge CLK )

if ( !reset )  y <= S0 ;else  y <= Y ;

// define the Output Logic:   assign  Z = ( y == S0 ) | (y == S3) ; endmodule

Page 30: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Example 4­14: Verilog Modelling of FSM (version B coding).

CAD for ASIC Design 30

A

A

A

S0

S3

S2

1

0

1

reset

A

S1

Z

Z

0

0

1

0

1

Page 31: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

module FSM2 ( A, CLK, reset, Z );input A, CLK, reset ;output Z ;reg [1:0] y,  Y ;parameter [1:0]   S0 = 0, S1 = 1, S2 =2, S3=3;

//  define STATE_REG: always @ (negedge reset or negedge CLK)   

if ( reset  == 0 )  y <= A ;else  y <= Y ;

...

...// define NS_OUTPUT and OUTPUT 

LOGIC: always @ ( y or A )  begin

case ( y  )S0 :  if (A )  begin  Y = S3 ;  Z = 1;  end 

  else  begin  Y = S0 ;  Z = 0;  endS1 :   if ( A ) begin  Y = S0 ;  Z = 1;  end 

  else  begin  Y = S1 ;  Z = 0;  endS2 :  if ( A ) begin  Y = S1 ;  Z = 0;  end 

  else  begin  Y = S2 ;  Z = 0;  endS3 :  if ( A ) begin  Y = S1 ;  Z = 0;  end

  else  begin  Y = S2 ;  Z = 0;  endendcaseend

endmodule

CAD for ASIC Design 31

Page 32: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

Another version? ⋅ Will this work?

  

CAD for ASIC Design 32

module FSM2 ( A, CLK, reset, Z );input A, CLK, reset ;output Z ;reg [1:0] y ;parameter [1:0]   S0 = 0, S1 = 1, S2 =2, S3=3;//  define STATE_REG: always @ (negedge reset or negedge CLK)   

  beginif ( reset  == 0 )  y <= A ;else  case ( y  )S0 :  if (A )  begin  y <= S3 ;  Z = 1;  end 

  else  begin  y <= S0 ;  Z = 0;  endS1 :   if ( A ) begin  y <= S0 ;  Z = 1;  end 

  else  begin  y <= S1 ;  Z = 0;  endS2 :  if ( A ) begin  y <= S1 ;  Z = 0;  end 

  else  begin  y <= S2 ;  Z = 0;  endS3 :  if ( A ) begin  y <= S1 ;  Z = 0;  end   else  begin  y <= S2 ;  Z = 0;  end

endcase;end;endmodule

Page 33: VERILOG DESIGN OF SEQUENTIAL LOGIC · L4: Verilog Design of Sequential Logic 2012/2013-1 ⋅ Synchronous sequential logic circuits rely on storage elements for their operations. ⋅

L­4: Verilog Design of Sequential Logic 2012/2013-1

DIY Problems⋅ P4­1⋅ P4­2⋅ P4­6⋅ P4­7⋅ P4­11

CAD for ASIC Design 33