Top Banner
SystemVerilog Assertions Are For Design Engineers, Too! Don Mills LCDM Engineering Salt Lake City, Utah [email protected] Stuart Sutherland Sutherland HDL, Inc. Portland, Oregon [email protected]
23

SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

May 25, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

SystemVerilog AssertionsAre For Design Engineers, Too!

Don MillsLCDM EngineeringSalt Lake City, [email protected]

Stuart SutherlandSutherland HDL, Inc.

Portland, [email protected]

Page 2: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

2 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&

Presentation Overview

Why engineers don’t do assertionsExcuses, excuses, excuses

SystemVerilog Assertions overviewImmediate assertionsConcurrent assertions

Where assertions should be specifiedWhere to put the assertion codeWho should write the assertionsDeveloping an assertions test plan

Assertions for Design EngineersVerifying design assumptionsExamples, examples, examples

Lessons Learned

Page 3: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

3 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&Case Study:

Assertions for a Small DSP

A small Digital Signal Processor (DSP) design is used in this presentation to illustrate how to use SystemVerilog Assertions

The DSP is used as a training lab in Sutherland HDL coursesSynthesis students get to model the DSP as a final projectAssertion students get to add assertions to the DSP and testbench

The DSP contains…A clock generator andreset synchronizerA state machineSeveral registersA program counterCombinatorial decoder and ALUProgram and data memoriesA tri-state data bus

alu

pcregister

iobuf

status_reg

clock_gen

decodercontroller

iobuf

register

ram

ram

Page 4: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

4 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&Assertion-like Checks Can Be

Written in Plain Verilog

An assertion is a design condition that must be trueAssertions can be written in Verilog, but…It’s a lot of extra code!0 1 2 3 4 5

reqack

always @(posedge req) begin@(posedge clk) ; // synch to clockfork: watch_for_ack

parameter N = 3;begin: cycle_counter

repeat (N) @(posedge clk);$display("Assertion Failure", $time);disable check_ack;

end // cycle_counterbegin: check_ack

@(posedge ack)$display("Assertion Success", $time);disable cycle_counter;

end // check_ackjoin: watch_for_ack

end

Each request must be followed by an acknowledge within 1 to 3 clock cyclesEach request must be followed by an

acknowledge within 1 to 3 clock cycles

To test for a sequence of eventsrequires several lines of Verilog code• Difficult to write, read and maintain• Cannot easily be turned off during

reset or other don’t care times

To test for a sequence of eventsrequires several lines of Verilog code• Difficult to write, read and maintain• Cannot easily be turned off during

reset or other don’t care times

Page 5: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

5 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&Verilog-based Checker’s

Must be Hidden from Synthesis

A checking function written in Verilog looks like RTL codeSynthesis compilers cannot distinguish the hardware model from the embedded checker codeTo hide Verilog checker code from synthesis compilers, extra synthesis pragma’s must be added to the code

if (if_condition)// do true statements

else//synthesis translate_off if (!if_condition)//synthesis translate_on

// do the not true statements//synthesis translate_off else

$display("if condition tested either an X or Z"); //synthesis translate_on

RTL codeRTL code

checker codechecker code

RTL codeRTL code

checker codechecker code

How many engineer’s will go to this much extra effort to add

embedded checking to an if…else RTL statement?

How many engineer’s will go to this much extra effort to add

embedded checking to an if…else RTL statement?

Page 6: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

6 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&Obstacles to Writing

Self-Checking RTL Code

There are a number of reasons design engineers are reluctant to embed self-checking code within synthesizable RTL models…

These Are Valid Concerns!SystemVerilog Assertions overcome all of these obstacles!

Not enough time in the design scheduleMakes the RTL code hard to readNot synthesizableMight inadvertently change design behaviorWill slow down simulationIt’s the verification team’s job

Page 7: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

7 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&Advantages of

SystemVerilog Assertions

SystemVerilog Assertions have several advantages over coding assertion checks in Verilog…

Concise syntax!Dozens of lines of Verilog code can be represented in one line of SVA code

Can have severity levels!SystemVerilog assertion failures can be non-fatal or fatal errorsSimulators can enable/disable failure messages based on severity

Ignored by Synthesis!Don’t have to hide Verilog checker code within convoluted translate_off / translate_on synthesis pragmas

Can be disabled!SystemVerilog assertions can be turned off during reset, or until simulation reaches a specific simulation time or logic state

Page 8: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

8 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&SystemVerilog Has

Two Types of Assertions

Immediate assertions test for a condition at the current timealways @(state)assert ($onehot(state)) else $fatal;

generate a fatal error state variable is not a one-hot value

generate a fatal error state variable is not a one-hot value

An immediate assertion is the same as an if…else statement, but with assertion controlsAn immediate assertion is the same as an if…else statement, but with assertion controls

a_reqack: assert property (@(posedge clk) req ##[1:3] ack;) else $error;

0 1 2 3 4 5

reqack

a complex sequence can be defined in very concise codea complex sequence can be defined in very concise code

Concurrent assertions test for a sequence of events spread over multiple clock cycles

One line of SVA code replaces all the Verilog code in the example three slides back!One line of SVA code replaces all the Verilog code in the example three slides back!

Page 9: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

9 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&

Assertion Severity Levels

The severity of an assertion failure behavior can be specified$fatal [ ( finish_number, “message”, message_arguments ) ] ;

Terminates execution of the toolfinish_number is 0, 1 or 2, and controls the information printed by the tool upon exit(the same levels as with $finish)

$error [ ( “message”, message_arguments ) ] ;A run-time error severity; software continues execution

$warning [ ( “message”, message_arguments ) ] ;A run-time warning; software continues execution

$info [ ( “message”, message_arguments ) ] ;No severity; just print the message

Software tools may provide options to suppress errors or warnings or both

Software tools may provide options to suppress errors or warnings or both

always @(negedge reset)assert (state == LOAD) else $warning;

always @(negedge reset)assert (state == LOAD)else $fatal(0,“FSM %m behaved badly at %d”, $time);

Page 10: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

10 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&Expression Sequences

and the ## Cycle Delay

A sequence is a series of true/false expressions spread over oneor more clock cycles## represents a “cycle delay”

Specifies the number of clock cycles to wait until the next expression in the sequence is evaluated

property p_request_grant;

@(posedge clock) request ##1 grant ##1 !request ##1 !grant;

endproperty

ap_request_grant : assert property (p_request_grant); else $fatal;

request must be followed one clock cycle later by grantgrant must followed one clock cycle later by !request!request must be followed one clock cycle later by !grant

request must be followed one clock cycle later by grantgrant must followed one clock cycle later by !request!request must be followed one clock cycle later by !grant

“@(posedge clock)” is not a delay, it specifies what ## represents“@(posedge clock)” is not a delay, it specifies what ## represents

Page 11: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

11 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&SystemVerilog Design Constructs

With Built-in Assertions

Some SystemVerilog constructs have built-in assertion-like checking!

By using these constructs, designer’s get the advantages of self-checking, without having to write assertion statements!

always_comb / always_ffAllows tools to check that procedural code matches intentCheck that procedural block variables are not written to elsewhere

unique case / unique if…elseCheck that decision statements are fully specifiedCheck that decision branches are mutually exclusive

Enumerated variablesCheck that assignments are within the legal set of valuesCheck that two or more labels (e.g. state names) do not have the same value

Page 12: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

12 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&

What’s Next

Why engineers don’t do assertionsExcuses, excuses, excuses

SystemVerilog Assertions overviewImmediate assertionsConcurrent assertions

Where assertions should be specifiedWhere to put the assertion codeWho should write the assertionsDeveloping an assertions test plan

Assertions for Design EngineersVerifying design assumptionsExamples, examples, examples

Page 13: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

13 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&Assertions for the

DSP Example

wrN

rdN

addr data

datamemory

opcode[2:0]

b_data[15:0]

alu

pciw [15:0]

rstN

load_pc

inc_pc

cntd

load

inc

rstN

pc_cnt[11:0]

iw_data[15:0]

load_f

register16

rstN

load

d q

vcc

sys_clkclk>

fetchregistergnd

vcc wrN

rdN

addr data

iw [1

5:12

]

iw [1

1:0]

sys_clkclk>

data [15:0]

iobuf

data_rdyN

a

opcode

b

result

data

[15:

0]

alu_out [15:0]

status_reg

exceptionzero_flag

sys_clk clk>

rstN rstN

load_s load r_in

xbit

xbit

x_in

r_reg x_reg z_reg

load_b

sys_clk

rstN

load

d q

rstN

operandregister

> clk

ext_rstN(from test bench)

rstN

sys_clkctl_clkclock_gen

ext_clk(from test bench)

decoderinstruction

opcodeALU

operationdecode

dmem_rdN

dmem_wrN

controllerload_s

load_b

load_f

load_pc

rslt_oeN

dout_oeN

inc_pc

load_s

load_b

load_f

load_pc

rslt_oeN

dout_oeN

dmem_rdN

inc_pc

halt

set_brset_br

ctl_clk clk>

exception exception

zero_flag zero_flag

rstN rstN

instruction

branching branching

halt

dmem_wrN

iobuf

rslt_oeN

result [15:0]

zbit

zbit

z_in

data _out[15:0]

register16

ram(instance“dmem”)

programmemory

ram(instance“pmem”)

programcounter

set_br

b_in d_in

dout_oeN

branching

b_reg d_reg

Page 14: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

14 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&Where Assertions

Can be Specified

SystemVerilog Assertions can be… Assertion Based Verification should take advantage of all of

these capabilities

Assertion Based Verification should take advantage of all of

these capabilitiesEmbedded in the RTL codeExecutes as a programming statement, in-line with the RTL procedural codeWill be ignored by synthesis

In the design model, as a separate, concurrent block of codeExecutes in parallel with the design code throughout simulationWill be ignored by synthesis

External to the design model, in a separate fileCan be bound to specific instances of design modelsExecutes in parallel with the design code throughout simulationAllows verification engineers to add assertions to the design without actually modifying the design codeSynthesis never sees the assertion code

Page 15: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

15 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&

Guideline!

Design engineers should write assertions to verify assumptions that affect the functionality of a design block

The assertion documents the designer’s assumptionsExample: The ALU block assumes that the A, B and opcode inputs will never have a logic X or Z value

Verification engineers should write assertions that verify design functionality meets the overall design specification

The assertion verifies that the designer correctly implemented the specificationExample: The zero flag output of the ALU block should always be set if the ALU result output is zero

Page 16: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

16 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&Developing An Assertions

Test Plan

Before writing assertions, you need an “Assertions Test Plan”

The Assertions Test Plan should be developed before any design code is written!The Assertions Test Plan should be developed before any design code is written!

Which team is responsible for writing each assertionThe verification team?The design team?

Specifies what functionality needs to be verified with assertionsWhat type of assertion is needed for each test

Immediate or concurrent?Where the assertion should be placed

Embedded in the design?At the system interconnect level?Bound into the design?

Page 17: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

17 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&An Assertions Test Plan

Example

The assertions test plan defines…What to verifyWhich team is responsible for writing the assertion

Example: Program Counter assertions test plan

Each design block has a similar assertions test planEach design block has a similar assertions test plan

verification teamIf load, then pc == d input (must allow for clock-to-q delay)

verification teamIf increment, then pc increments by 1 (must allow for clock-to-q delay)

verification teamIf !load and !increment, then on posedge of clock, pc does not change (must allow for clock-to-q delay)

design teamIf increment, then d input never has any X or Z bits

design teamload and increment are mutually exclusive

Assigned ToFunctionality to Verify

Page 18: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

18 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&Assertion Plan Example 1:

Assertions on ALU Inputs

ALU design engineer assertions example

always_comb begin

// Check that inputs meet design assumptions (no X or Z bits)ai_a_never_x: assert (^a !== 1'bx);ai_b_never_x: assert (^b !== 1'bx);ai_opc_never_x: assert (^opcode !== 1'bx);

unique case (opcode) // “unique” verifies all opcodes are decoded ... // decode and execute operations

endcase end

Design engineer assertions are simple to add, and can greatly

reduce hard-to-find errors!

Design engineer assertions are simple to add, and can greatly

reduce hard-to-find errors!

design teamAfter reset, the opcode input never have any X or Z bits

design teamAfter reset, the B input never have any X or Z bits

design teamAll instructions are decoded

design teamAfter reset, the A, input never have any X or Z bits

Assigned ToFunctionality to Verify

Page 19: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

19 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&Assertion Plan Example 2:

State Machine Assertions

FSM design engineer assertions example

property p_fsm_onehot; // FSM state should always be one-hot @(posedge clk) disable iff (!rstN) $onehot(state);

endpropertyap_fsm_onehot: assert property (p_fsm_onehot);

property p_fsm_reset; // verify asynchronous reset to RESET state@(posedge clk) !rstN |-> state == RESET;

endpropertyap_fsm_reset: assert property (p_fsm_reset);

property p_fsm_decode_entry; // verify how DECODE state was entered@(posedge clk) disable iff (!rstN) state == DECODE |-> $past(state) == RESET || $past(state) == STORE;

endpropertyap_fsm_decode_entry: assert property (p_fsm_decode_entry);

Concurrent assertions can be used to verify

coverage too!

Concurrent assertions can be used to verify

coverage too!

design teamState is always one-hot

design teamIf !resetN (active low), state RESET

design teamIf in DECODE state, prior state was RESET or STORE

Assigned ToFunctionality to Verify

Page 20: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

20 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&

Lessons Learned…

Teaching engineers how to add assertions to the DSP training labhas shown that…

It takes time to write the assertions test planIt is not a trivial task, but it is critical to successfully using SVA!

The assertion test plan helps identify similar assertionsCan write an assertion once, and use it in several places

Assertions should not just duplicate the RTL codeEngineers need to learn to think differently

Disabling assertions during reset is importantHundreds of false assertion failures occur in the DSP during reset

The test plan needs to be flexibleSome times the responsibility for which team should write the assertion needs to change

Page 21: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

21 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&More

Lessons Learned…

Assertions may require different design partitioningThe DSP ALU block is difficult to check with concurrent assertions because it is pure combinational logic (no clock)Better design partitioning would put the ALU and its input and output registers into one design block

Enumerated type definitions should be defined globallyThe DSP state machine uses a local enumerated variable for the state namesAssertions written external to the state machine cannot access those enumerated names

Enumerated types should have explicit values defined for each labelAfter synthesis, labels disappear and only logic values existAssertions become invalid if the label does not have an explicit value

Page 22: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

22 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&

Summary

SystemVerilog Assertions enable true assertions based verificationIntegrated into the Verilog/SystemVerilog language

Don’t have to hide assertions in commentsAssertions have full visibility to all design codeExecution order is defined within simulation event scheduling

Easy to write (compared to other assertion solutions)Immediate and concurrent assertionsA concise, powerful sequential description languageSequence building blocks for creating complex sequences

Binding allows verification engineers to add assertions to a design without touching the design files

SystemVerilog assertions are a team effortSome assertions written by the design teamSome assertions written by the verification team

Page 23: SystemVerilog Assertions Are For Design Engineers, Too! · Design engineers should write assertions to verify assumptions that affect the functionality of a design block The assertion

23 of 23

LCDM EngineeringLCDM LCDM EngineeringEngineering &&&

What will assertions reveal about my design?

Questions & Answers…