英文版PDF格式
《Hardware Verification with C++.》
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . xxi
C h a p t e r 1 : I n t r o d u c t i o n . . . . . . . . . . . . . . . . . . 1
Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
What is Functional Verification?. . . . . . . . . . . . . . . . . 4
Why Focus on C++? . . . . . . . . . . . . . . . . . . . . . . . . . 4
A Tour of the Handbook . . . . . . . . . . . . . . . . . . . . . . 5
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . 6
Part I:
C++ and Verification
(The Why and How) . . . . . . . . . . . . . . . . . . . .7
C h a p t e r 2 : Wh y C + + ? . . . . . . . . . . . . . . . . . . . . . 9
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
C++: A Good Verification Language. . . . . . . . . . . . . . 11
A Look at Hardware-Verification Languages . . . . . . . . 13
Randomization . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Coverage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Object-oriented support . . . . . . . . . . . . . . . . . . . 14
Main Benefits of Using C++ . . . . . . . . . . . . . . . . . . . 15
C o n t e n t s
viii Hardware Verification with C++
Other Benefits of Using C++ . . . . . . . . . . . . . . . . . . 16
Drawbacks of Using C++ . . . . . . . . . . . . . . . . . . . . . 17
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . 17
Chapter 3: OOP, C++, and Verification . . . . 19
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
The Evolution of OOP and C++ . . . . . . . . . . . . . . . . . 21
Assembly programming: The early days. . . . . . . . . . 21
Procedural languages: The next big step . . . . . . . . . 21
OOP: Inheritance for functionality . . . . . . . . . . . . . 22
OOP: Inheritance for interface . . . . . . . . . . . . . . . 24
OOP: Templates . . . . . . . . . . . . . . . . . . . . . . . . . 24
C++: The standard template library . . . . . . . . . . . . 25
The Evolution of Functional Verification . . . . . . . . . . 25
Verification through inspection . . . . . . . . . . . . . . . 25
Verification through randomness . . . . . . . . . . . . . . 26
The emergence of
hardware verification languages . . . . . . . . . . . . . . 27
OOP: A current trend in
hardware verification languages . . . . . . . . . . . . . . 27
OOP: Problems with the current approach. . . . . . . . 28
OOP: A possible next step . . . . . . . . . . . . . . . . . . 28
OOP Using C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Data abstraction through classes . . . . . . . . . . . . . . 29
A DMA descriptor example . . . . . . . . . . . . . . . . . 29
Access control . . . . . . . . . . . . . . . . . . . . . . . . . 31
Constructors and destructors . . . . . . . . . . . . . . . 32
Member methods and variables . . . . . . . . . . . . . . 33
Operator overloading . . . . . . . . . . . . . . . . . . . . 34
Inheritance for functionality . . . . . . . . . . . . . . . . . 34
Inheritance for interface . . . . . . . . . . . . . . . . . . . 36
Templating . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
A word about user-defined templates. . . . . . . . . . 39
Namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
A Practitioner’s Handbook ix
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . 41
C h a p t e r 4 : A L a y e r e d Ap p r o a c h . . . . . . . . . . . 43
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
A Whiteboard Drawing . . . . . . . . . . . . . . . . . . . . . . 46
An “ends-in” approach. . . . . . . . . . . . . . . . . . . . . 47
Refining the whiteboard blocks . . . . . . . . . . . . . . . 47
The “Common-Currency” Components . . . . . . . . . . . 48
The Interface Layer in Detail. . . . . . . . . . . . . . . . . . 49
The wire layer . . . . . . . . . . . . . . . . . . . . . . . . . . 50
The agent layer . . . . . . . . . . . . . . . . . . . . . . . . . 52
The transaction layer . . . . . . . . . . . . . . . . . . . . . 53
The Top-Layer Components . . . . . . . . . . . . . . . . . . 54
What is a Test? . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
The Test Component . . . . . . . . . . . . . . . . . . . . . . . 58
The Test Irritator . . . . . . . . . . . . . . . . . . . . . . . . . 60
A Complete Test . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . 63
Part II:
An Open-Source Environment with C++ . . . . . .65
C h a p t e r 5 : T e a l B a s i c s . . . . . . . . . . . . . . . . . . . 67
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
What Teal provides . . . . . . . . . . . . . . . . . . . . . . . 68
Teal’s similarity to HDLs . . . . . . . . . . . . . . . . . . . 69
A tiny but complete example . . . . . . . . . . . . . . . . 69
Teal’s Main Components. . . . . . . . . . . . . . . . . . . . . 71
Using Teal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Your C++ test . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
C o n t e n t s
x Hardware Verification with C++
Creating registers . . . . . . . . . . . . . . . . . . . . . . . 74
Working with a reg or vreg . . . . . . . . . . . . . . . . . . 75
Logging Output. . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Using Test Parameters . . . . . . . . . . . . . . . . . . . . . . 79
Accessing Memory . . . . . . . . . . . . . . . . . . . . . . . . . 81
A memory note example . . . . . . . . . . . . . . . . . . . 82
Constrained Random Numbers . . . . . . . . . . . . . . . . . 84
Required initialization . . . . . . . . . . . . . . . . . . . . . 84
Using random numbers. . . . . . . . . . . . . . . . . . . . . 84
Working with Simulation Events and Concurrency . . . . 86
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . 88
Chapter 6 : Truss: A Standard V e r i f i c a t i o n
F r amewo r k . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
General Considerations . . . . . . . . . . . . . . . . . . . . . 93
Using a language other than C++ . . . . . . . . . . . . . . 93
Keeping it simple . . . . . . . . . . . . . . . . . . . . . . . . 94
Major Classes and Their Roles . . . . . . . . . . . . . . . . . 95
Key test algorithm: The “dance” . . . . . . . . . . . . . . 96
The verification_component Abstract Base Class. . . . . 99
Detailed Responsibilities
of the Major Components . . . . . . . . . . . . . . . . . . . .100
The testbench class. . . . . . . . . . . . . . . . . . . . . . .101
Watchdog timer . . . . . . . . . . . . . . . . . . . . . . . . .102
Test class . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102
Test Component and Irritator Classes . . . . . . . . . . . .106
The test component abstract base class . . . . . . . . .106
An AHB example . . . . . . . . . . . . . . . . . . . . . . . . .108
Test-component housekeeping functionality . . . . . .109
The irritator abstract base class . . . . . . . . . . . . . .110
Using the irritator. . . . . . . . . . . . . . . . . . . . . . .112
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113
A Practitioner’s Handbook xi
C h a p t e r 7 : T r u s s F l ow . . . . . . . . . . . . . . . . . . . . 115
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116
About verification_top.cpp . . . . . . . . . . . . . . . . . . .116
The Test Component Dance . . . . . . . . . . . . . . . . . .119
The Irritator Dance . . . . . . . . . . . . . . . . . . . . . . . .121
Compiling and Running Tests. . . . . . . . . . . . . . . . . .122
Truss run script . . . . . . . . . . . . . . . . . . . . . . . . .123
Switches . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124
The Truss makefile . . . . . . . . . . . . . . . . . . . . . . .125
The First Test: A Directed Test . . . . . . . . . . . . . . . .125
The Second Test:
Adding Channels and Random Parameters . . . . . . . . .127
The channel classes. . . . . . . . . . . . . . . . . . . . . . .128
Building the second test. . . . . . . . . . . . . . . . . . . .129
Building the second test’s test_component . . . . . . .132
Adjusting the second test’s parameters . . . . . . . . .134
The Remaining Tests:
Mix-and-Match Test Components . . . . . . . . . . . . . . .136
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137
Chapter 8: T r u s s E x amp l e . . . . . . . . . . . . . . . . 139
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140
Directory Structure . . . . . . . . . . . . . . . . . . . . . . . .140
Theory of Operation . . . . . . . . . . . . . . . . . . . . . . .142
Running the Simple ALU Example . . . . . . . . . . . . . . .144
Points of Interest . . . . . . . . . . . . . . . . . . . . . . . . .144
Power-on Reset . . . . . . . . . . . . . . . . . . . . . . . . . .145
Driver and Monitor Protocol . . . . . . . . . . . . . . . . . .146
The alu_test_component . . . . . . . . . . . . . . . . . . . .147
Checking the Chip . . . . . . . . . . . . . . . . . . . . . . . . .147
Completing the Test . . . . . . . . . . . . . . . . . . . . . . .149
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150
C o n t e n t s
xii Hardware Verification with C++
Part III:
Using OOP for Verification
(Best Practices). . . . . . . . . . . . . . . . . . . . . . .153
C h a p t e r 9 : T h in k in g OOP . . . . . . . . . . . . . . . . . 155
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156
Sources of Complexity . . . . . . . . . . . . . . . . . . . . . .157
Essential complexity vs.
implementation complexity . . . . . . . . . . . . . . . . .157
Flexibility vs. complexity . . . . . . . . . . . . . . . . . . .158
Apparent simplicity vs.
hiding inherent complexity . . . . . . . . . . . . . . . . . .161
Example: How hiding complexity
can create confusion . . . . . . . . . . . . . . . . . . . . .161
Example: How apparent simplicity
leads to later problems . . . . . . . . . . . . . . . . . . .162
Team dynamics . . . . . . . . . . . . . . . . . . . . . . . . .164
Team roles . . . . . . . . . . . . . . . . . . . . . . . . . . .164
Using a “code buddy” . . . . . . . . . . . . . . . . . . . .165
Creating Adaptable Code . . . . . . . . . . . . . . . . . . . .166
Achieving adaptability . . . . . . . . . . . . . . . . . . . . .166
Why is adaptability tricky? . . . . . . . . . . . . . . . . . .167
Architectural Considerations
to Maximize Adaptability . . . . . . . . . . . . . . . . . . . .168
Changes are easy—or just plain impossible. . . . . . . .168
Where is adaptation likely to happen? . . . . . . . . . .170
Separating Interface from Implementation . . . . . . . .170
Interface, Implementation, and Base Classes . . . . . . .171
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
For Further Reading . . . . . . . . . . . . . . . . . . . . . . .173
A Practitioner’s Handbook xiii
C h a p t e r 1 0 : D e s i g n in g wi t h OOP . . . . . . . . . . 175
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176
Keeping the Abstraction Level Consistent . . . . . . . . .176
Using “Correct by Construction” . . . . . . . . . . . . . . .178
The Value of Namespaces . . . . . . . . . . . . . . . . . . . .181
Data Duplication—A Necessary Evil . . . . . . . . . . . . . .183
Designing Well, Optimizing Only When Necessary . . . .184
Using the Interface, Only the Interface . . . . . . . . . . .185
Verification Close to the Programming Model . . . . . . .186
The Three Parts of Checking . . . . . . . . . . . . . . . . . .187
Separating the Test from the Testbench . . . . . . . . . .189
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190
For Further Reading . . . . . . . . . . . . . . . . . . . . . . .191
Chapter 1 1 : OOP Classes . . . . . . . . . . . . . . . . . 193
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194
Defining Classes . . . . . . . . . . . . . . . . . . . . . . . . . .195
How Much Electricity? . . . . . . . . . . . . . . . . . . . . . .195
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196
Namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . .196
Operator overloading. . . . . . . . . . . . . . . . . . . . . .196
Pointers and virtual functions . . . . . . . . . . . . . . . .197
Even more electricity . . . . . . . . . . . . . . . . . . . . .197
Smart pointers . . . . . . . . . . . . . . . . . . . . . . . . . .197
Global Services. . . . . . . . . . . . . . . . . . . . . . . . . . .198
Namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . .198
Static methods . . . . . . . . . . . . . . . . . . . . . . . . . .200
Singletons—A Special Case of Static Methods . . . . . .200
Namespaces or static methods? . . . . . . . . . . . . . . .201
Other considerations . . . . . . . . . . . . . . . . . . . . . .202
Class Instance Identifiers . . . . . . . . . . . . . . . . . . . .203
Object memory address as identifiers. . . . . . . . . . .203
Strings as identifiers . . . . . . . . . . . . . . . . . . . . . .203
C o n t e n t s
xiv Hardware Verification with C++
Static integers as identifiers . . . . . . . . . . . . . . . . .204
Combination identifiers . . . . . . . . . . . . . . . . . . . .204
Class Inheritance for Reuse . . . . . . . . . . . . . . . . . . .205
A BFM base class example . . . . . . . . . . . . . . . . . .205
A BFM agent class . . . . . . . . . . . . . . . . . . . . . . . .206
Reusing the BFM class . . . . . . . . . . . . . . . . . . . . .206
Class Inheritance for Interfaces . . . . . . . . . . . . . . . .207
Inheritance for a verification component . . . . . . . .208
Inheritance for a payload interface . . . . . . . . . . . .208
Operator Overloading for
Common-Currency Classes . . . . . . . . . . . . . . . . . . .210
Operator overloading in a communications channel. .211
Considerations . . . . . . . . . . . . . . . . . . . . . . . . . .212
Creating Classes—
What the Compiler Will Do for You. . . . . . . . . . . . . .212
Advanced Class Techniques. . . . . . . . . . . . . . . . . . .214
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215
For Further Reading . . . . . . . . . . . . . . . . . . . . . . .216
C h a p t e r 1 2 : OOP C o n n e c t i o n s . . . . . . . . . . . . . 217
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218
How Tight a Connection? . . . . . . . . . . . . . . . . . . . .219
Types of Connections. . . . . . . . . . . . . . . . . . . . . . .221
Peer-to-peer connections . . . . . . . . . . . . . . . . . . .221
Master-to-slave and push-vs.-pull connections . . . . .221
Two Tight Connection Techniques . . . . . . . . . . . . . .223
Using pointers . . . . . . . . . . . . . . . . . . . . . . . . . .223
Using inheritance . . . . . . . . . . . . . . . . . . . . . . . .224
Threads and Connections . . . . . . . . . . . . . . . . . . . .226
Events—explicit blocking interconnects. . . . . . . . . .226
Hiding the thread block in a method . . . . . . . . . . .227
Fancier Connections . . . . . . . . . . . . . . . . . . . . . . .229
Listener or callback connections . . . . . . . . . . . . . .230
Channel connections . . . . . . . . . . . . . . . . . . . . . .231
A Practitioner’s Handbook xv
Action object connections . . . . . . . . . . . . . . . . . .232
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233
For Further Reading . . . . . . . . . . . . . . . . . . . . . . .234
C h a p t e r 1 3 : C o d i n g OOP . . . . . . . . . . . . . . . . . . 235
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236
“If” Tests—A Necessary Evil . . . . . . . . . . . . . . . . . .236
“If” tests and abstraction levels . . . . . . . . . . . . . .237
“If” tests and code structure . . . . . . . . . . . . . . . .238
Repeated “if” expressions . . . . . . . . . . . . . . . . . .239
“If” tests and factory functions . . . . . . . . . . . . . . .240
A factory function example . . . . . . . . . . . . . . . .241
Coding Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . .244
Coding only what you need to know . . . . . . . . . . . .244
Reservable resources. . . . . . . . . . . . . . . . . . . . . .245
Using register fields instead of
hard-coded integers . . . . . . . . . . . . . . . . . . . . . .245
Using data members carefully . . . . . . . . . . . . . . . .246
Coding Idioms. . . . . . . . . . . . . . . . . . . . . . . . . . . .248
The singleton idiom. . . . . . . . . . . . . . . . . . . . . . .248
Public nonvirtual methods:
Virtual protected methods . . . . . . . . . . . . . . . . . .249
Pointers for change, references for efficiency . . . . .251
Avoiding accessor methods—
using constant references instead . . . . . . . . . . . . .252
Enumeration for Data, Integer for Interface . . . . . . . .253
Using the constructor and destructor
to simplify the code . . . . . . . . . . . . . . . . . . . . . .254
Using a boolean to indicate success . . . . . . . . . . . .255
What’s in a Name? . . . . . . . . . . . . . . . . . . . . . . . .257
Keeping class name the same as file name . . . . . . .257
Keeping class and instance names related . . . . . . . .258
Coding with Style . . . . . . . . . . . . . . . . . . . . . . . . .259
Proceeding with caution. . . . . . . . . . . . . . . . . . . .259
C o n t e n t s
xvi Hardware Verification with C++
General syntax conventions . . . . . . . . . . . . . . . . .260
Syntactic sugar. . . . . . . . . . . . . . . . . . . . . . . . . .260
Using override and non_virtual . . . . . . . . . . . . . .261
Using owner and cached . . . . . . . . . . . . . . . . . .262
Identifying private and protected members . . . . . . .263
Using Templates . . . . . . . . . . . . . . . . . . . . . . . . . .264
Standard templates for data management . . . . . . . .264
Writing templates for adaptable code. . . . . . . . . . .267
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268
For Further Reading . . . . . . . . . . . . . . . . . . . . . . .269
Part IV:
Examples
(Putting It All Together) . . . . . . . . . . . . . . . . .271
C h a p t e r 1 4 : B l o c k L e v e l T e s t i n g . . . . . . . . . . 273
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274
Theory of Operation . . . . . . . . . . . . . . . . . . . . . . .275
Verification environment . . . . . . . . . . . . . . . . . . .276
Verification IP . . . . . . . . . . . . . . . . . . . . . . . . . .277
UART VIPs . . . . . . . . . . . . . . . . . . . . . . . . . . . .277
Wishbone VIP . . . . . . . . . . . . . . . . . . . . . . . . . .278
The verification dance . . . . . . . . . . . . . . . . . . . . .279
Running the UART Example . . . . . . . . . . . . . . . . . . .279
Points of Interest . . . . . . . . . . . . . . . . . . . . . . . . .280
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . .280
VIP UART configuration class. . . . . . . . . . . . . . . . .281
Randomization of enums . . . . . . . . . . . . . . . . . .282
UART 16550 configuration class . . . . . . . . . . . . . . .284
Configuring the Chip . . . . . . . . . . . . . . . . . . . . . . .286
Register access. . . . . . . . . . . . . . . . . . . . . . . . . .286
The wishbone_memory_bank and
wishbone_driver . . . . . . . . . . . . . . . . . . . . . . . . .289
Traffic Generation . . . . . . . . . . . . . . . . . . . . . . . .290
A Practitioner’s Handbook xvii
The generator_agent and uart_bfm_agent classes. . .291
The Checker . . . . . . . . . . . . . . . . . . . . . . . . . . . .293
Checking the data. . . . . . . . . . . . . . . . . . . . . . . .294
Connecting It All Together . . . . . . . . . . . . . . . . . . .296
The testbench . . . . . . . . . . . . . . . . . . . . . . . . . .296
Building the channels . . . . . . . . . . . . . . . . . . . .296
Building the configuration and interface port. . . . .297
Building the interface-layer objects . . . . . . . . . . .298
The wishbone objects . . . . . . . . . . . . . . . . . . . .299
The test component . . . . . . . . . . . . . . . . . . . . . .300
The uart_basic_test_component::randomize()
method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .301
The basic data test . . . . . . . . . . . . . . . . . . . . . . .302
More Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304