79
Design Patterns by Example for SystemVerilog Verification Environments Enabled by SystemVerilog 1800-2012 Eldon Nelson ( [email protected] ) Intel Corporation ®

Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

  • Upload
    others

  • View
    63

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Design Patterns by Example for SystemVerilog Verification

Environments Enabledby SystemVerilog 1800-2012Eldon Nelson ( [email protected] )

Intel Corporation®

Page 2: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Outline

• Design Patterns– Approach

• Composition versus Inheritance• Strategy Pattern

– Class Interface and Implements– Application to Evolving Packet Class

• Decorator Pattern– Typed Constructor Calls– Application to Layered Constraints

Eldon Nelson - Intel Corporation 2

Page 3: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Full Code Examples

https://github.com/tenthousandfailures/systemverilog-design-patterns

Eldon Nelson - Intel Corporation 3

Page 4: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()QuackBehavior()FlyBehavior()

Duck {abstract}

4

Page 5: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()QuackBehavior()FlyBehavior()

Duck {abstract}

display()quack()fly()

MallardDuck

5

Page 6: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()QuackBehavior()FlyBehavior()

Duck {abstract}

display()quack()fly()

MallardDuckdisplay()squeak()flynoway()

RubberDuck

6

Page 7: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()QuackBehavior()FlyBehavior()

Duck {abstract}

display()quack()fly()

MallardDuck

display()RedheadDuck

display()squeak()flynoway()

RubberDuck

7

Page 8: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()QuackBehavior()FlyBehavior()

Duck {abstract}

display()quack()fly()

MallardDuck

display()RedheadDuck

display()squeak()flynoway()

RubberDuck

display()mutequack()

DecoyDuck

8

Page 9: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()QuackBehavior()FlyBehavior()

Duck {abstract}

display()quack()fly()

MallardDuck

display()RedheadDuck

display()squeak()flynoway()

RubberDuck

display()mutequack()

DecoyDuck

no flyno quack

flyquack

9

Page 10: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments
Page 11: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()QuackBehavior()FlyBehavior()

Duck {abstract}

display()fly()

DroneDuck

display()quack()fly()

MallardDuck

display()RedheadDuck

display()squeak()flynoway()

RubberDuck

display()mutequack()

DecoyDuck

no flyno quack

flyquack

11

Page 12: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()QuackBehavior()FlyBehavior()

Duck {abstract}

display()fly()

DroneDuck

display()quack()fly()

MallardDuck

display()RedheadDuck

display()squeak()flynoway()

RubberDuck

display()mutequack()

DecoyDuck

display()mutequack()

DroneDuck

no flyno quack

flyquack

??

12

Page 13: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Eldon Nelson - Intel Corporation 13

Page 14: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Design Patterns 1994

• Is the definitive first text• Examples in the programming language

Smalltalk or in C++ with features from 1994• Code fragment examples center around a

Word Processor GUI Application

Eldon Nelson - Intel Corporation 14

Page 15: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Framework

Library

Design Pattern

15

Page 16: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Eldon Nelson - Intel Corporation 16

Page 17: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Eldon Nelson - Intel Corporation 17

Page 18: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Head First Design Patterns

• Examples written solely in Java• Complete examples given for every exercise• Examples are on a variety of whimsical fictional

applications

Eldon Nelson - Intel Corporation 18

Page 19: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Outline

• Design Patterns– Approach

• Composition versus Inheritance• Strategy Pattern

– Class Interface and Implements– Application to Evolving Packet Class

• Decorator Pattern– Typed Constructor Calls– Application to Layered Constraints

Eldon Nelson - Intel Corporation 19

Page 20: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Composition

“Object composition is an alternative to class inheritance. Here, new functionality is obtained by assembling or composing objects to get more complex functionality. Object composition requires that the objects being composed have well-defined interfaces. This style of reuse is called black-box reuse, because no internal details of objects are visible. Objects appear only as ‘black boxes’”

- “Design Patterns” p.19

Eldon Nelson - Intel Corporation 20

Page 21: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

One Direction

Santa ClaraHigh School

Love Song

Money

21

Page 22: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

One Direction

DVCon

Love Song

Money

22

Page 23: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

One Direction

LondonHigh School

Love Song

Money

23

Page 24: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

One Direction

LondonHigh School

Arguments

Return

Function

Love Song

Money

24

Page 25: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

One Direction

LondonHigh School

Arguments

Return

Function

DVCon LectureX

Money

25

Page 26: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

One Direction

LondonHigh School

Arguments

Return

Function

DVCon LectureX

Riot

X{

26

Page 27: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

27

Page 28: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Interface Class (Contract)

• “interface class” cannot define implementation– In contrast with an “virtual class” which can– Defines the arguments and return values

• “interface class” must haveonly “pure virtual function”

• No: constraints or covergroups

Eldon Nelson - Intel Corporation 28

Page 29: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

quack()

<<interface>>QuackBehavior

quack()Quack

quack()Squeak

quack()MuteQuack

29

Page 30: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Interface Class (Contract)

JAVApublic interface QuackBehavior {

public void quack();}

SYSTEMVERILOGinterface class QuackBehavior;

pure virtual function void quack();endclass

NewSV1800-2012

Eldon Nelson - Intel Corporation 30

Page 31: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

ImplementsJAVA

public class Squeak implements QuackBehavior {public void quack() {

System.out.println("Squeak");}

}

SYSTEMVERILOGclass Squeak implements QuackBehavior;virtual function void quack();

$display("Squeak");endfunction

endclass

NewSV1800-2012

Eldon Nelson - Intel Corporation 31

Page 32: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Phrase “Duck Typing”

• “In other words, don't check whether it IS-a duck: check whether it QUACKS-like-a duck, WALKS-like-a duck, etc, etc, depending on exactly what subset of duck-like behaviour you need to play your language-games with.”

– Alexis Martelli, 7/26/2000 in comp.lang.python

Eldon Nelson - Intel Corporation 32

Page 33: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Outline

• Design Patterns– Approach

• Composition versus Inheritance• Strategy Pattern

– Class Interface and Implements– Application to Evolving Packet Class

• Decorator Pattern– Typed Constructor Calls– Application to Layered Constraints

Eldon Nelson - Intel Corporation 33

Page 34: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Strategy Pattern

“Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.”

- Definition of the Strategy Pattern from“Design Patterns” [1, p. 135]

Eldon Nelson - Intel Corporation 34

Page 35: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()performQuack()performFly()setFlyBehavior()setQuackBehavior()

flyBehavior: FlyBehaviorquackBehavior: QuackBehavior

Duck {abstract}

new()display()

MallardDucknew()display()

RedheadDucknew()display()

RubberDucknew()display()

DecoyDuck

35

Page 36: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()performQuack()performFly()setFlyBehavior()setQuackBehavior()

flyBehavior: FlyBehaviorquackBehavior: QuackBehavior

Duck {abstract}

new()display()

MallardDucknew()display()

RedheadDucknew()display()

RubberDucknew()display()

DecoyDuck

36

Page 37: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

class RubberDuck extends Duck;

function new();FlyNoWay f = new();Squeak q = new();setFlyBehavior(f);setQuackBehavior(q);

endfunction

virtual function void display();$display("I'm a rubber duckie");

endfunction

endclass

display()performQuack()performFly()setFlyBehavior()setQuackBehavior()

flyBehavior: FlyBehaviorquackBehavior: QuackBehavior

Duck {abstract}

new()display()

MallardDucknew()display()

RedheadDucknew()display()

RubberDucknew()display()

DecoyDuck

Eldon Nelson - Intel Corporation 37

Page 38: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

virtual class Duck;FlyBehavior flyBehavior;QuackBehavior quackBehavior;

function void setQuackBehavior(QuackBehavior qb);quackBehavior = qb;

endfunction

function void performQuack();quackBehavior.quack();

endfunction

display()performQuack()performFly()setFlyBehavior()setQuackBehavior()

flyBehavior: FlyBehaviorquackBehavior: QuackBehavior

Duck {abstract}

new()display()

MallardDucknew()display()

RedheadDucknew()display()

RubberDucknew()display()

DecoyDuck

Eldon Nelson - Intel Corporation 38

Page 39: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()performQuack()performFly()setFlyBehavior()setQuackBehavior()

flyBehavior: FlyBehaviorquackBehavior: QuackBehavior

Duck {abstract}

new()display()

MallardDucknew()display()

RedheadDucknew()display()

RubberDucknew()display()

DecoyDuck

fly()

<<interface>>FlyBehavior

fly()FlyWithWings

fly()FlyNoWay

quack()

<<interface>>QuackBehavior

quack()Quack

quack()Squeak

quack()MuteQuack

39

Page 40: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Favor Composition

“Favor object composition over class inheritance.”

- Second Principle of Object-Oriented Programming from “Design Patterns” [1, p. 20]

Eldon Nelson - Intel Corporation 40

Page 41: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Strategy PatternApplied to Packet Problem

• Encapsulation of a changing packet format• Fields change width and location• Changeable checking algorithm – CRC or Parity

Eldon Nelson - Intel Corporation 41

Page 42: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Policy ClassesAKA Strategy Pattern

Eldon Nelson - Intel Corporation 42

Page 43: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Outline

• Design Patterns– Approach

• Composition versus Inheritance• Strategy Pattern

– Class Interface and Implements– Application to Evolving Packet Class

• Decorator Pattern– Typed Constructor Calls– Application to Layered Constraints

Eldon Nelson - Intel Corporation 43

Page 44: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Decorator Pattern

“Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extended functionality.”

- Definition of the Decorator Pattern from“Design Patterns” [1, p. 175]

Eldon Nelson - Intel Corporation 44

Page 45: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Decorator PatternExample Starbuzz Coffee

• Coffee type like: HouseBlend or DarkRoast• Has Condiments like: Mocha or Whip• Each Coffee type has a price and each Condiment

has a price• An order could be

– “Dark Roast with Double Mocha and Whip”– Give the string and price for this order

Eldon Nelson - Intel Corporation 45

Page 46: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

starbuzz

new(Beverage)getDescription(): stringcost(): real

WhipBeverage beverage

new(Beverage)getDescription(): stringcost(): real

SoyBeverage beverage

new(Beverage)getDescription(): stringcost(): real

MochaBeverage beverage

new(Beverage)getDescription(): stringcost(): real

MilkBeverage beverage

new()cost(): real

HouseBlendnew()cost(): real

Espresso

getDescription(): stringcost(): real

Beverage {abstract}description: string

CondimentDecorator {abstract}

new()cost(): real

Decafnew()cost(): real

DarkRoast

46

Page 47: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

starbuzz

new(Beverage)getDescription(): stringcost(): real

WhipBeverage beverage

new(Beverage)getDescription(): stringcost(): real

SoyBeverage beverage

new(Beverage)getDescription(): stringcost(): real

MochaBeverage beverage

new(Beverage)getDescription(): stringcost(): real

MilkBeverage beverage

new()cost(): real

HouseBlendnew()cost(): real

Espresso

getDescription(): stringcost(): real

Beverage {abstract}description: string

CondimentDecorator {abstract}

new()cost(): real

Decafnew()cost(): real

DarkRoast

47

Page 48: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

starbuzz

new(Beverage)getDescription(): stringcost(): real

WhipBeverage beverage

new(Beverage)getDescription(): stringcost(): real

SoyBeverage beverage

new(Beverage)getDescription(): stringcost(): real

MochaBeverage beverage

new(Beverage)getDescription(): stringcost(): real

MilkBeverage beverage

new()cost(): real

HouseBlendnew()cost(): real

Espresso

getDescription(): stringcost(): real

Beverage {abstract}description: string

CondimentDecorator {abstract}

new()cost(): real

Decafnew()cost(): real

DarkRoast

48

Page 49: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

starbuzz

new(Beverage)getDescription(): stringcost(): real

WhipBeverage beverage

new(Beverage)getDescription(): stringcost(): real

SoyBeverage beverage

new(Beverage)getDescription(): stringcost(): real

MochaBeverage beverage

new(Beverage)getDescription(): stringcost(): real

MilkBeverage beverage

new()cost(): real

HouseBlendnew()cost(): real

Espresso

getDescription(): stringcost(): real

Beverage {abstract}description: string

CondimentDecorator {abstract}

new()cost(): real

Decafnew()cost(): real

DarkRoast

49

Page 50: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

class Mocha extends CondimentDecorator;

Beverage beverage;

function new(Beverage beverage);this.beverage = beverage;

endfunction

virtual function string getDescription();return {beverage.getDescription(), ", Mocha"};

endfunction

virtual function real cost();return (0.20 + beverage.cost());

endfunction

endclass

Eldon Nelson - Intel Corporation 50

Page 51: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

class Mocha extends CondimentDecorator;

Beverage beverage;

function new(Beverage beverage);this.beverage = beverage;

endfunction

virtual function string getDescription();return {beverage.getDescription(), ", Mocha"};

endfunction

virtual function real cost();return (0.20 + beverage.cost());

endfunction

endclass

Eldon Nelson - Intel Corporation 51

Page 52: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

module top;import starbuzz::*;

Beverage beverage;string str;

initial beginbeverage = Darkroast::new;beverage = Mocha::new(beverage);beverage = Mocha::new(beverage);beverage = Whip::new(beverage);str.realtoa(beverage.cost());$display({beverage.getDescription(), " $", str});

NewSV1800-2012

“Dark Roast Mocha Mocha Whip $1.65”

Eldon Nelson - Intel Corporation 52

Page 53: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Decorator Pattern Application

• “SystemVerilog Constraint Layering via Reusable Randomization Policy Classes”John Dickol, DVCon 2015

• Dynamically applying multiple combinations of SystemVerilog constraints at runtime

Eldon Nelson - Intel Corporation 53

Page 54: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

layer

txn: rand addr_txnnew(addr_txn)

addr_permit

rprint()print()

addr: rand bit [31:0]size: rand int

addr_txn

addr_txnDecorator {abstract}

txn: rand addr_txnnew(addr_txn)

addr_prohibit

{constraint} {constraint}

{constraint}

54

Page 55: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

layer

txn: rand addr_txnnew(addr_txn)

addr_permit

rprint()print()

addr: rand bit [31:0]size: rand int

addr_txn

addr_txnDecorator {abstract}

txn: rand addr_txnnew(addr_txn)

addr_prohibit

{constraint} {constraint}

{constraint}

55

Page 56: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

class addr_permit extends addr_txnDecorator;rand addr_txn txn;

function new(addr_txn txn);this.txn = txn;

endfunction

constraint c_addr_permit {addr inside {['h00000000 : 'h0000FFFF – txn.size]} ||addr inside {['h10000000 : 'h1FFFFFFF – txn.size]};

txn.addr == addr;txn.size == size;

}

endclass

Eldon Nelson - Intel Corporation 56

Page 57: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

class addr_permit extends addr_txnDecorator;rand addr_txn txn;

function new(addr_txn txn);this.txn = txn;

endfunction

constraint c_addr_permit {addr inside {['h00000000 : 'h0000FFFF – txn.size]} ||addr inside {['h10000000 : 'h1FFFFFFF – txn.size]};

txn.addr == addr;txn.size == size;

}

endclass

Eldon Nelson - Intel Corporation 57

Page 58: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

module top;

layer::addr_txn txn;

initial begintxn = new;txn = layer::addr_prohibit::new(txn);txn = layer::addr_permit::new(txn);txn.rprint();

Eldon Nelson - Intel Corporation 58

DecoratorPatternLayeredConstraint

Page 59: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Awesome Let's Use It!

• Software Design Pattern ideas are widely accepted and refined over decades

• The examples ported to SystemVerilog have ahigh fidelity with their Java sources– Meaning SystemVerilog has feature parity to meet

these examples without compromises with SV 2012• Simulator Vendor Compatibility

Eldon Nelson - Intel Corporation 59

Page 60: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

SV 2012Simulator Compatibility

Simulator localconstructor

keyword“implements”

keyword“interface class”

typedconstructor calls

A Y Y Y Y

B Y Y Y N

C Y N N N

NewSV1800-2012

NewSV1800-2012

NewSV1800-2012

Eldon Nelson - Intel Corporation 60

Page 61: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Design PatternCompatibility per Simulator

Simulator Decorator Singleton Observer Strategy State

A P P P P P

B S P P P P

C S P N N N

P (Preferred Implementation) à S (Satisfactory Implementation) à N (Not Directly Supported)

Eldon Nelson - Intel Corporation 61

Page 62: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Outline

• Design Patterns– Approach

• Composition versus Inheritance• Strategy Pattern

– Class Interface and Implements– Application to Evolving Packet Class

• Decorator Pattern– Typed Constructor Calls– Application to Layered Constraints

Eldon Nelson - Intel Corporation 62

Page 63: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Opportunitieswith Design Patterns

• Excited to see how these new language features give an alternative to inheritance

• Excited to see more use of the Decorator Pattern as it is one of the most common– constraints are just the beginning

• Vendor support will come– but please tell them you want it!

Eldon Nelson - Intel Corporation 63

Page 64: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Eldon Nelson - Intel Corporation 64

Page 65: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Eldon Nelson - Intel Corporation 65

Questions

Please Vote at http://vote.dvcon.org

Page 66: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Shy Audience Questions

1. Why use an “interface class” when you could use an “virtual class” and put in your own “pure virtual functions”?

2. Show me the horror that is the Decorator Pattern without “typed constructor calls”

3. What can I do to improve the adoption of these SystemVerilog features in the simulators?

4. Is the paper better than your presentation –cause… you know?

Eldon Nelson - Intel Corporation 66

Page 67: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Shy Audience Questions 2

1. Why do you sign your @ieee.org email address on all your papers?

2. What pattern if you had more time would you like to share next? Well, other than the Packet Protocol protocol one you said you skipped.

Eldon Nelson - Intel Corporation 67

Page 68: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Backup Slides

Eldon Nelson - Intel Corporation 68

Page 69: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

UVM Policy Not Exactly Strategy Pattern

Eldon Nelson - Intel Corporation 69

Page 70: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

module top;import starbuzz::*;

Beverage beverage;string str;

initial beginbeverage = Darkroast::new;beverage = Mocha::new(beverage);beverage = Mocha::new(beverage);beverage = Whip::new(beverage);str.realtoa(beverage.cost());$display({beverage.getDescription(), " $", str});

NewSV1800-2012

“Dark Roast Mocha Mocha Whip $1.65”

DecoratorPatternWithTypedConstructorCalls

Eldon Nelson - Intel Corporation 70

Page 71: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

module top;import starbuzz::*;

Beverage beverage;DarkRoast darkroast;Mocha mocha;Whip whip;string str;

initial begindarkroast = new;beverage = new darkroast;mocha = new(beverage);beverage = new mocha;mocha = new(beverage);beverage = new mocha;whip = new(beverage);beverage = new whip;str.realtoa(beverage.cost());$display({beverage.getDescription(), " $", str});

DecoratorPatternWithoutTypedConstructorCalls

Eldon Nelson - Intel Corporation 71

Page 72: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

Strategy Pattern

• Removed from Presentation for Time Issues

Eldon Nelson - Intel Corporation 72

Page 73: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

11 10 89 7 6 45 3 2 01

reserved data addr cmd

v2 protocol1213

11 10 89 7 6 45 3 2 01

data addr cmd

v3 protocol1213

11 10 89 7 6 45 3 2 01

reserved addr data cmd

v1 protocol1213

check

check

check

73

Page 74: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

packet

performUnpack(logic [13:0]): bitperformPack(): logic [13:0]print()setPackBehavior(TPackBehavior)setCheckBehavior(CheckBehavior)

packBehavior: TpackBehaviorcheckBehavior: CheckBehaviorfields: rand TFields

base_packet#(type TPackBehavior = PackBehavior, type TFields = Fields) {abstract}

new()v1_packet

new()v2_packet

new()v3_packet

pack():logic [13:0]unpack(logic [13:0]): TFields

<<interface>>PackBehavior#(type TFields = Fields)

pack(): logic [13:0]unpack(logic [13:0]): TFields

v1_pack

pack(): logic [13:0]unpack(logic [13:0]): TFields

v2_pack

pack(): logic [13:0]unpack(logic [13:0])

<<interface>>CheckBehavior

pack() : logic [13:0]unpack(logic [13:0]): bit

Paritypack(): logic [13:0]unpack(logic [13:0]): bit

Crc

pack(): logic [13:0]unpack(logic [13:0]): TFields

v3_pack

addr: rand logic [3:0]data: rand logic [3:0]cmd: rand logic [2:0]reserved: rand logic

Fieldsaddr: rand logic [4:0]

Fields_v3

74

Page 75: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

display()performQuack()performFly()setFlyBehavior()setQuackBehavior()

flyBehavior: FlyBehaviorquackBehavior: QuackBehavior

Duck {abstract}

new()display()

MallardDucknew()display()

RedheadDucknew()display()

RubberDucknew()display()

DecoyDuck

fly()

<<interface>>FlyBehavior

fly()FlyWithWings

fly()FlyNoWay

quack()

<<interface>>QuackBehavior

quack()Quack

quack()Squeak

quack()MuteQuack

75

Page 76: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

packet

performUnpack(logic [13:0]): bitperformPack(): logic [13:0]print()setPackBehavior(TPackBehavior)setCheckBehavior(CheckBehavior)

packBehavior: TpackBehaviorcheckBehavior: CheckBehaviorfields: rand TFields

base_packet#(type TPackBehavior = PackBehavior, type TFields = Fields) {abstract}

new()v1_packet

new()v2_packet

new()v3_packet

pack():logic [13:0]unpack(logic [13:0]): TFields

<<interface>>PackBehavior#(type TFields = Fields)

pack(): logic [13:0]unpack(logic [13:0]): TFields

v1_pack

pack(): logic [13:0]unpack(logic [13:0]): TFields

v2_pack

pack(): logic [13:0]unpack(logic [13:0])

<<interface>>CheckBehavior

pack() : logic [13:0]unpack(logic [13:0]): bit

Paritypack(): logic [13:0]unpack(logic [13:0]): bit

Crc

pack(): logic [13:0]unpack(logic [13:0]): TFields

v3_pack

addr: rand logic [3:0]data: rand logic [3:0]cmd: rand logic [2:0]reserved: rand logic

Fieldsaddr: rand logic [4:0]

Fields_v3

76

Page 77: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

packet

performUnpack(logic [13:0]): bitperformPack(): logic [13:0]print()setPackBehavior(TPackBehavior)setCheckBehavior(CheckBehavior)

packBehavior: TpackBehaviorcheckBehavior: CheckBehaviorfields: rand TFields

base_packet#(type TPackBehavior = PackBehavior, type TFields = Fields) {abstract}

new()v1_packet

new()v2_packet

new()v3_packet

pack():logic [13:0]unpack(logic [13:0]): TFields

<<interface>>PackBehavior#(type TFields = Fields)

pack(): logic [13:0]unpack(logic [13:0]): TFields

v1_pack

pack(): logic [13:0]unpack(logic [13:0]): TFields

v2_pack

pack(): logic [13:0]unpack(logic [13:0])

<<interface>>CheckBehavior

pack() : logic [13:0]unpack(logic [13:0]): bit

Paritypack(): logic [13:0]unpack(logic [13:0]): bit

Crc

pack(): logic [13:0]unpack(logic [13:0]): TFields

v3_pack

addr: rand logic [3:0]data: rand logic [3:0]cmd: rand logic [2:0]reserved: rand logic

Fieldsaddr: rand logic [4:0]

Fields_v3

Eldon Nelson - Intel Corporation 77

Page 78: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

class v1_pack implements PackBehavior;virtual function Fields unpack(logic [13:0] raw);

Fields fields = new;fields.reserved = raw[11];fields.addr = raw[10:7];fields.data = raw[6:3];fields.cmd = raw[2:0];return fields;

endfunction

Eldon Nelson - Intel Corporation 78

pack():logic [13:0]unpack(logic [13:0]): TFields

<<interface>>PackBehavior#(type TFields = Fields)

pack(): logic [13:0]unpack(logic [13:0]): TFields

v1_pack

pack(): logic [13:0]unpack(logic [13:0]): TFields

v2_pack

pack(): logic [13:0]unpack(logic [13:0]): TFields

v3_pack

Page 79: Design Patterns by Example for SystemVerilog Verification ...events.dvcon.org/2016/proceedings/slides/08_2.pdf · Design Patterns by Example for SystemVerilog Verification Environments

class v1_packet extends base_packet;function new();

v1_pack p = new();Crc c = new();

setPackBehavior(p);setCheckBehavior(c);

endfunctionendclass

Eldon Nelson - Intel Corporation 79

performUnpack(logic [13:0]): bitperformPack(): logic [13:0]print()setPackBehavior(TPackBehavior)setCheckBehavior(CheckBehavior)

packBehavior: TpackBehaviorcheckBehavior: CheckBehaviorfields: rand TFields

base_packet#(type TPackBehavior = PackBehavior, type TFields = Fields) {abstract}

new()v1_packet

new()v2_packet

new()v3_packet