Outline
Basic OOP in SystemVerilog Advanced OOP in SV Random Nunber Generator Homwork today
System-Level Modeling and Verification for SystemCommunications System Verilog Lecture 4 Prof. Xiaofang Zhou ASIC & Systems, Dept. of Microeletronics FUDAN University Shanghai, CHINA
OOP in SV
What's Object Oriented Program
OO code are easier to write and maintain
Let you create complex data types Tie data with routines work with them More privacy, more safety and robustness
A first Class in SV
A class contains 3 data and 2 functions
data in a class are called "Property" subroutines in a class are called "Method"
The ": name" after endfunction / endclass are optional class BusTran;
bit [31 : 0] addr, crc, data[8]; function void display; $display("BusTran: %h", addr); endfunction : display function void calc_crc; crc = addr ^ data.xor; endfunction : calc_crc
3
Class can contain other classes more features: Inheritance, Virtual methods, etc
endclass : BusTran
Terms in OOP
Class: a basic building block containing
routines and variables Object: an instance of a class Handle: a pointer to an object. Handle in SV are strongly typed. Property / Method : data/task, function
There are class data and object data
type g proto definin tern" "ex code o f th e Body ::" Name "Class
function void BusTran2::display; $display("BusTran: %h", addr); endfunction : display function void BusTran2::calc_crc; crc = addr ^ data.xor; endfunction : calc_crc
the same object Line 7 b1 points to a 2nd object, and b2 points to the 1st BusTran3 object.
1 2 3 4 5 6 7 8 9
program test; BusTran3 b1, b2; initial begin b1 = new; b2 = b1; b1 = new; end endprogram
8
Garbage collection
SV automatically does garbage collection after an object is deallocated (no longer used)
BusTran3 b;// create handle b = new; // allocate an object b = new; // allocate a 2nd one, free the 1st b = null; // Manually deallocate the 2nd
For circular lists, you need to manually broken the links, clear all handles before SV can deallocate these object
9
This methodology is questionable. But when writing testbenches, our goal is to run faster and to control over all variables to generate the widest range of stimulus values.
10
Class data
Class data is a property that commonly
i.e. make it private, and give a pair of set/get method to user package CompE; use strict;
#!/usr/bin/perl -w use strict; use CompE; use vars qw($a); $a = CompE->new(0); $a->x(100);
set
sub new { my $proto = shift; my $type = ref $proto || $proto; my($x) = @_; $x = 0 if not $x; my($this) = {x=>$x}; bless $this, $type; } de get co sub x { my $this = shift; $this->{x} = shift if (@_); $this->{x}; } 1;
11
a ss dat
Second id =
1, count =
set/
BusTran5 b1, b2; initial begin b1 = new; b2 = new; $display("Second id = %d, count = %d", b2.id, b2.count); end endprogram
12
'this'?
In many OOP methods, 'this' or 'self' is used to refer to the object or class itself. So does SV. SV will search a name in current scope, then look it up in higher scope, and soon, until it finds a match. In SV, you can omit the 'this.' in most case.
scope of class 'Scoping'
class Scoping; string oname; function new(string oname); t data this.oname = oname; Objec e' 'onam endfunction endclass
Class BusTran6 shall initialize the stats handle (calling new) Class Bustran6 can refer to things in the Statstics class using hierarchical syntax, e.g. stats.start() Separate big class into small ones if necessary
14
data.
task transmit(BusTran bt); bt.timestamp = $time; endtask BusTran b; initial begin b = new(); b.addr = 42; transmit(b); end
16
task gen_good(int n); BusTran b; repeat (n) begin b = new(); b.addr = $random(); $display(); transmit(b); end in bjects New o ration endtask e ch it
ea
task create_pckt(ref BusTran bt); bt = new(); bt.addr = 42; endtask BusTran b; initial begin create_packet(b); $display(b.addr); end
t objec
17
new objects and copy recursively if copy.crc = crc; necessary. copy.data = data;
class BusTran; bit [31:0] addr, crc, data[8]; static int count = 0; int id; Statistics stats; function new; stats = new; id = count++; endfunction function BusTran copy; copy = new; copy.addr = addr; copy.stats = stats.copy; id = count++; endfunction endclass BusTran src, dst; initial begin src = new; src.stats.startT = 42; dst = src.copy; dst.stats.startT = 84; end
20
BusTran src, dst; initial begin src = new; handle = new handle src.stats.startT = 42; is a shallow copy. It only dst = new src; dst.stats.startT = 84; copies the top level object. e end he sam ares t dst sh ject d src an stats ob
19
Outline
Basic OOP in SystemVerilog Advanced OOP in SV Class example Composition Inheritance (extended class) Virtual Function Random Nunber Generator Homwork today
21
22
Class example
Task: Write a class that generate wave forms to test our ADC_Adapter DUT There's unlimited kinds of waveforms (DC, square wave, saw tooth, ). May we simplify the job with OOP? One big class or several relative small classes? Does SV OOP support composition, inheritance, virtual functions?
Square Source DC Source DC Saw Tooth Source
Saw to
oth
function int runSawTooth; runSawTooth = calcSawTooth(); n++; if (n >= PERIOD) n = 0; endfunction function int calcSawTooth; result = n * AMP * 2 / PERIOD - AMP + MID; if (result < MIN) result = MIN; if (result > MAX) result = MAX; calcSawTooth = result; endfunction // more other waveform code // endclass Source_AllinOne S;
uctor constr
DUT
ADC adapter wishbone slave/master Checker
DC
23
24
de ted co repea
function int run; run = calc(); S.n++; if (S.n >= S.PERIOD) S.n = 0; endfunction function int calc; S.result = S.n * S.AMP * 2 / S.PERIOD - S.AMP + S.MID; if (S.result < S.MIN) S.result = S.MIN; if (S.result > S.MAX) S.result = S.MAX; calc = S.result; endfunction endclass SawTooth S; initial begin S = new; repeat(1000) begin $display(S.run); end end endmodule
ted repea
code
ov class SawTooth extends Source; e rem function int run; Can w run = calc (); n++; if (n >= PERIOD) n = 0; endfunction function int calc; result = n * AMP * 2 / PERIOD - AMP + MID; if (result < MIN) result = MIN; if (result > MAX) result = MAX; calc = result; endfunction endclass
SawTooth S; initial begin S = new; repeat(1000) begin $display(S.run); end end endmodule
e this?
25
26
pe prototy same
class SawTooth extends Source; virtual function int calc; result = n * AMP * 2 / PERIOD - AMP + MID; if (result < MIN) result = MIN; if (result > MAX) result = MAX; calc = result; endfunction endclass SawTooth S;
ciate ins tan vel Don't t le bstrac ectly the a ir lass d base c
function new(int period = 200, mid = 2048, max = 4097, min = 0, amp = 2000); super.new(mid, max, min); PERIOD = period; AMP = amp; endfunction virtual function void step; super.step(); if (n >= PERIOD) n = 0; endfunction endclass class SawTooth extends Period; virtual function int calc; result = n * AMP * 2 / PERIOD - AMP + MID; if (result < MIN) result = MIN; if (result > MAX) result = MAX; calc = result; endfunction endclass SawTooth S; initial begin S = new; repeat(1000) begin $display(S.run); end end
initial begin function new(int period = 200, mid = 2048, S = new; max = 4097, min = 0, amp = 2000); repeat(1000) begin PERIOD = period; $display(S.run); MID = mid; end MAX = max; MIN = min; end AMP = amp; endfunction endmodule function int run; run = calc (); calc" n++; rrect " if (n >= PERIOD) n = 0; the co Call endfunction virtual function int calc; result = MID; od calc = result; l meth Virtua endfunction endclass
function new(int mid = 2048, max = 4097, min = 0); MID = mid; MAX = max; MIN = min; endfunction virtual function void step; n++; endfunction virtual function int calc; // $display("Can't call me directly"); calc = MID; endfunction function int run; run = calc(); step(); endfunction endclass class Period extends Source; int PERIOD; int AMP;
endmodule
27
28
Outline
Basic OOP in SystemVerilog Advanced OOP in SV Random Nunber Generator Homwork today Theory:
Xi+1=(a*Xi+c) mod m, i = 0, 1, 2,
when c != 0, (Wont be discussed here) when c == 0, m is a prime number, the maximun period of sequence X is Pmax = m 1; X0 within [1..m-1], Dont set X0 to 0. when c == 0, m is 2b, Pmax = m/4.
Class Example
A class implements the previous PRNG
1. class OOPRNG4093; 2. logic [11 : 0] x; 3. function new(logic [11 : 0]n); 4. this.Srand(n); 5. endfunction 6. function void Srand(logic [11 : 0] n); 7. x = n % 4093; 8. endfunction 9. function logic [11 : 0] Rand(); 10. x = (x*7) % 4093; 11. Rand = x; 12. endfunction 13. endclass 14. 15. 16. 17. 18. 19. 20.
# # # # # # # # # # # # # # # # # # # # 700 807 1556 2706 2570 1618 3140 1515 2419 561 3927 2931 52 364 2548 1464 2062 2155 2806 3270
#!/usr/bin/perl -w use strict; my ($X, $a, %h, $n); my $M = 4093; for $a (1..20) { ($X, $n, %h) = (1, 1); until (exists $h{$X}) { $h{$X} = $n++; $X = ($X * $a) % $M; } print "$a, period ", $n - $h{$X}, "\n"; }
14. 1;
1, period 1 2, period 4092 3, period 341 4, period 2046 5, period 4092 6, period 4092 7, period 4092 8, period 1364 9, period 341 10, period 22 11, period 2046 12, period 2046 13, period 1364 14, period 682 15, period 4092 16, period 1023 17, period 31 18, period 4092 19, period 4092 20, period 372
31
module test_OO; OOPRNG4093 A; initial begin A = new(100); repeat(20) $display(A.Rand); end endmodule
32
Homework
Implement the PRNG in RTL code.
Synthesizable systemverilog or verilog code 12 bit Wishbone interface
Wishbone write calls Srand Ignore writen value outside of [1..4092] Wishbone read calls Rand
Conclusions
Inheritance and virtual function are useful
Use + operator instead of net list adder Try to avoid * % > < operators Document your design in doc (not docx) file before starting coding. Send your .doc, .sv, tb.sv with your student id in subject to svsc@xfzhou.almostmy.com
33
tools to design very nice classes. The base class shall be describing something in higher level, or something abstract Leave callbacks in your class. Be more friendly with design reuse
34
References
IEEE Standard for SystemVerilog Unified Hardware Design, Specification, and Verification Language, IEEE, IEEE Std 1800TM 2005, 22 November 2005. S. Sutherland, S. Davidmann and P. Flake, SystemVerilog for Design, Kluwer Academic Publishers, 2004. C. Spear, SystemVerilog for Verification, Springer, 2006. S. Vijayaraghavan and M. Ramanathan, A Practical Guide for SystemVerilog Assertions, Springer, 2005 SystemVerilog Tutorials, Doulos Ltd., http://www.doulos.com/knowhow/sysverilog/tutorial/ SystemVerilog Tutorial, electroSofts.com, http://electrosofts.com/systemverilog/ Jerry Bankds, J.S. Carson II, etc, Discrete-Event System Simulation (4th Ed), Chapter 7 Random-Number Generation (, 20059)
35
Thank You
36