{"id":94644,"date":"2026-08-14T22:48:57","date_gmt":"2026-08-14T14:48:57","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/94644.html"},"modified":"2026-08-14T22:48:57","modified_gmt":"2026-08-14T14:48:57","slug":"sv%e5%ad%a6%e4%b9%a0%e8%ae%b0%e5%bd%95%ef%bc%88%e5%85%ab%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/94644.html","title":{"rendered":"SV\u5b66\u4e60\u8bb0\u5f55\uff08\u516b\uff09"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"8.1%20%E7%BB%A7%E6%89%BF-toc\" style=\"margin-left:0px\">8.1 \u7ee7\u627f<\/p>\n<p id=\"%E4%BA%8B%E5%8A%A1%E5%9F%BA-toc\" style=\"margin-left:40px\">\u4e8b\u52a1\u57fa<\/p>\n<p id=\"%E6%89%A9%E5%B1%95-toc\" style=\"margin-left:40px\">\u6269\u5c55<\/p>\n<p id=\"%E6%9B%B4%E5%A4%9AOOP%E6%9C%AF%E8%AF%AD-toc\" style=\"margin-left:40px\">\u66f4\u591aOOP\u672f\u8bed<\/p>\n<p id=\"%E6%89%A9%E5%B1%95%E7%B1%BB%E4%B8%AD%E7%9A%84%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">\u6269\u5c55\u7c7b\u4e2d\u7684\u6784\u9020\u51fd\u6570<\/p>\n<p id=\"driver%20class-toc\" style=\"margin-left:40px\">driver class<\/p>\n<p id=\"generator%20class-toc\" style=\"margin-left:40px\">generator class<\/p>\n<p id=\"8.2%20%E8%93%9D%E5%9B%BE%EF%BC%88blueprint%EF%BC%89%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:0px\">8.2 \u84dd\u56fe&#xff08;blueprint&#xff09;\u6a21\u5f0f<\/p>\n<p id=\"environment%20class-toc\" style=\"margin-left:40px\">environment class<\/p>\n<p id=\"%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84testbench-toc\" style=\"margin-left:40px\">\u4e00\u4e2a\u7b80\u5355\u7684testbench<\/p>\n<p id=\"%E7%94%A8%E6%89%A9%E5%B1%95transaction%20class-toc\" style=\"margin-left:40px\">\u7528\u6269\u5c55transaction class<\/p>\n<p id=\"%E7%94%A8%E6%89%A9%E5%B1%95%E7%B1%BB%E6%94%B9%E5%8F%98%E9%9A%8F%E6%9C%BA%E7%BA%A6%E6%9D%9F-toc\" style=\"margin-left:40px\">\u7528\u6269\u5c55\u7c7b\u6539\u53d8\u968f\u673a\u7ea6\u675f<\/p>\n<p id=\"8.3%20downcasting%20and%20virtual%20method-toc\" style=\"margin-left:0px\">8.3 downcasting and virtual method<\/p>\n<p id=\"%E7%94%A8%24cast%20downcasting-toc\" style=\"margin-left:40px\">\u7528$cast downcasting<\/p>\n<p id=\"virtual%20method-toc\" style=\"margin-left:40px\">virtual method<\/p>\n<p id=\"%E7%AD%BE%E5%90%8D%EF%BC%88signature%EF%BC%89-toc\" style=\"margin-left:40px\">\u7b7e\u540d&#xff08;signature&#xff09;<\/p>\n<p id=\"8.4%20%E5%90%88%E6%88%90%E3%80%81%E7%BB%A7%E6%89%BF%E5%92%8C%E5%85%B6%E4%BB%96%E6%9B%BF%E4%BB%A3%E6%96%B9%E6%B3%95-toc\" style=\"margin-left:0px\">8.4 \u5408\u6210\u3001\u7ee7\u627f\u548c\u5176\u4ed6\u66ff\u4ee3\u65b9\u6cd5<\/p>\n<p id=\"%E5%90%88%E6%88%90%E4%B8%8E%E7%BB%A7%E6%89%BF%EF%BC%88composition%20and%20inheritance%EF%BC%89-toc\" style=\"margin-left:40px\">\u5408\u6210\u4e0e\u7ee7\u627f&#xff08;composition and inheritance&#xff09;<\/p>\n<p id=\"composition%E7%9A%84%E9%97%AE%E9%A2%98-toc\" style=\"margin-left:40px\">composition\u7684\u95ee\u9898<\/p>\n<p id=\"inheritacne%E7%9A%84%E9%97%AE%E9%A2%98-toc\" style=\"margin-left:40px\">inheritacne\u7684\u95ee\u9898<\/p>\n<p id=\"%E7%8E%B0%E5%AE%9E%E7%9A%84%E9%80%89%E6%8B%A9-toc\" style=\"margin-left:40px\">\u73b0\u5b9e\u7684\u9009\u62e9<\/p>\n<p id=\"8.5%20%E5%A4%8D%E5%88%B6object-toc\" style=\"margin-left:0px\">8.5 \u590d\u5236object<\/p>\n<p id=\"copy_data-toc\" style=\"margin-left:40px\">copy_data<\/p>\n<p id=\"%E4%B8%BAcopy%E6%8C%87%E5%AE%9A%E5%9C%B0%E5%9D%80-toc\" style=\"margin-left:40px\">\u4e3acopy\u6307\u5b9a\u5730\u5740<\/p>\n<p id=\"8.6%20%E6%8A%BD%E8%B1%A1%E7%B1%BB%E5%92%8C%E7%BA%AF%E8%99%9A%E6%96%B9%E6%B3%95-toc\" style=\"margin-left:0px\">8.6 \u62bd\u8c61\u7c7b\u548c\u7eaf\u865a\u65b9\u6cd5<\/p>\n<p id=\"8.7%20callback-toc\" style=\"margin-left:0px\">8.7 callback<\/p>\n<p id=\"%E5%88%9B%E5%BB%BAcallback-toc\" style=\"margin-left:40px\">\u521b\u5efacallback<\/p>\n<p id=\"%E7%94%A8callback%E6%B3%A8%E5%85%A5%E5%B9%B2%E6%89%B0-toc\" style=\"margin-left:40px\">\u7528callback\u6ce8\u5165\u5e72\u6270<\/p>\n<p id=\"scoreboard-toc\" style=\"margin-left:40px\">scoreboard<\/p>\n<p id=\"%E7%94%A8callback%E8%BF%9E%E6%8E%A5scoreboard-toc\" style=\"margin-left:40px\">\u7528callback\u8fde\u63a5scoreboard<\/p>\n<p id=\"%E7%94%A8callback%20debug%E4%BA%8B%E5%8A%A1%E5%A4%84%E7%90%86%E5%99%A8-toc\" style=\"margin-left:40px\">\u7528callback debug\u4e8b\u52a1\u5904\u7406\u5668<\/p>\n<p id=\"8.8%20%E5%8F%82%E6%95%B0%E5%8C%96%E7%9A%84%E7%B1%BB-toc\" style=\"margin-left:0px\">8.8 \u53c2\u6570\u5316\u7684\u7c7b<\/p>\n<p id=\"stack-toc\" style=\"margin-left:40px\">stack<\/p>\n<p id=\"%E5%85%B3%E4%BA%8E%E5%8F%82%E6%95%B0%E5%8C%96%E7%B1%BB%E7%9A%84%E5%BB%BA%E8%AE%AE-toc\" style=\"margin-left:40px\">\u5173\u4e8e\u53c2\u6570\u5316\u7c7b\u7684\u5efa\u8bae<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"8.1%20%E7%BB%A7%E6%89%BF\">8.1 \u7ee7\u627f<\/h2>\n<h3 id=\"%E4%BA%8B%E5%8A%A1%E5%9F%BA\">\u4e8b\u52a1\u57fa<\/h3>\n<p>\u4e8b\u52a1\u57fa\u7c7b\u5305\u542b\u53d8\u91cf\u548c\u5b50\u7a0b\u5e8f&#xff0c;\u53d8\u91cf\u5305\u542b\u6e90\u5730\u5740&#xff0c;\u76ee\u7684\u5730\u5740&#xff0c;8\u4e2a\u6570\u636e\u5b57&#xff0c;\u7528\u4e8e\u68c0\u67e5\u9519\u8bef\u7684CRC&#xff0c;\u5b50\u7a0b\u5e8f\u5305\u62ec\u8ba1\u7b97CRC&#xff0c;\u8f93\u51fa\u5185\u5bb9\u3002<\/p>\n<p>virtual\u7684\u4f5c\u7528\u540e\u9762\u518d\u8bb2<\/p>\n<p>Sample 8.1 Base Transaction class<br \/>\nclass Transaction;<br \/>\n  rand bit [31:0] src, dst, data[8];  \/\/ Random variables<br \/>\n  bit [31:0] crc; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Calculated variable<br \/>\n \u00a0<br \/>\n  virtual function void calc_crc;<br \/>\n    crc &#061; src ^ dst ^ data.xor;<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\n  virtual function void display(input string prefix&#061;&#034;&#034;);<br \/>\n \u00a0  $display(&#034;%sTr: src&#061;%h, dst&#061;%h, crc&#061;%h&#034;,<br \/>\n \u00a0            prefix, src, dst, crc);<br \/>\n  endfunction<br \/>\nendclass <\/p>\n<h3 id=\"%E6%89%A9%E5%B1%95\">\u6269\u5c55<\/h3>\n<p>\u4e0a\u9762\u6211\u4eec\u7ed9\u51fa\u4e86\u80fd\u591f\u751f\u6210\u6b63\u5e38\u4e8b\u52a1\u7684\u4e8b\u52a1\u57fa&#xff0c;\u73b0\u5728\u6211\u4eec\u60f3\u8981\u5728\u5b83\u7684\u57fa\u7840\u4e0a\u6269\u5c55\u4e3a\u80fd\u591f\u6ce8\u5165\u9519\u8bef\u7684bad\u7c7b\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u6269\u5c55\u540e\u7684\u7ed3\u679c\u3002virtual\u7684\u4f5c\u7528\u662f\u8ba9\u4e00\u4e2a\u65b0\u7c7b\u80fd\u591f\u5728\u5b83\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u6269\u5c55\u3002<\/p>\n<p>super\u7528\u4e8e\u8c03\u7528\u4e0b\u4e00\u5c42\u7684\u51fd\u6570\u3002\u4f46\u662fsuper.super\u8fd9\u7c7b\u591a\u5c42\u8c03\u7528\u662f\u975e\u6cd5\u7684\u3002<\/p>\n<p>\u6ce8\u610f&#xff0c;BadTr\u4f5c\u4e3a\u6269\u5c55\u53ef\u4ee5\u770b\u5230Tr\u7684\u6240\u6709\u53d8\u91cf&#xff0c;\u6240\u4ee5\u5728\u4f7f\u7528crc\u7684\u65f6\u5019\u6ca1\u6709\u52a0\u524d\u7f00\u3002<\/p>\n<p>Sample 8.2 Extended Transaction class<br \/>\nclass BadTr extends Transaction;<br \/>\n  rand bit bad_crc;<br \/>\n \u00a0<br \/>\n  virtual function void calc_crc;<br \/>\n \u00a0 \u00a0 super.calc_crc(); \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Compute good CRC<br \/>\n \u00a0 \u00a0 if (bad_crc) crc &#061; ~crc;  \/\/ Corrupt the CRC bits<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\n  virtual function void display(input string prefix&#061;&#034;&#034;);<br \/>\n \u00a0 \u00a0 $write(&#034;%sBadTr: bad_crc&#061;%b, &#034;, prefix, bad_crc);<br \/>\n \u00a0 \u00a0 super.display();<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\nendclass : BadTr <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"597\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260814144855-6a7f2ad785911.png\" width=\"1151\" \/><\/p>\n<p>\u53ef\u4ee5\u7ecf\u5e38\u628a\u51fd\u6570\u547d\u540d\u4e3avirtual\u7279\u6027\u65b9\u4fbf\u6269\u5c55&#xff0c;\u9664\u4e86new\u51fd\u6570\u90fd\u53ef\u4ee5\u8fd9\u4e48\u505a\u3002<\/p>\n<h3 id=\"%E6%9B%B4%E5%A4%9AOOP%E6%9C%AF%E8%AF%AD\">\u66f4\u591aOOP\u672f\u8bed<\/h3>\n<p>variable\u53ebproperty&#xff1b;task, function\u53ebmethod&#xff1b;\u505a\u6269\u5c55\u7684\u65f6\u5019&#xff0c;\u88ab\u6269\u5c55\u7684\u57fa\u7c7b\u53ebparent\/ super class&#xff0c;\u6269\u5c55\u51fa\u6765\u7684\u53ebderived class \/ subclass&#xff1b;prototype\u662f\u6307\u660e\u53c2\u6570\u5217\u8868\u548c\u8fd4\u56de\u7c7b\u578b\u7684\u7b2c\u4e00\u884c\u3002<\/p>\n<h3 id=\"%E6%89%A9%E5%B1%95%E7%B1%BB%E4%B8%AD%E7%9A%84%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0\">\u6269\u5c55\u7c7b\u4e2d\u7684\u6784\u9020\u51fd\u6570<\/h3>\n<p>\u5982\u679cparent class\u4e2d\u6709\u6784\u9020\u51fd\u6570\u6784\u9020\u4e86\u5176\u4e2d\u7684\u67d0\u4e9b\u53d8\u91cf&#xff0c;\u5728subclass\u4e2d\u5fc5\u987b\u5148\u8c03\u7528parent\u7684new<\/p>\n<p>Sample 8.3 Constructor with argument in an extended class<br \/>\nclass Base1;<br \/>\n  int var;<br \/>\n  function new(input int var); \/\/ Has argument<br \/>\n \u00a0 \u00a0 this.var &#061; var;<br \/>\n  endfunction<br \/>\nendclass<br \/>\n\u200b<br \/>\nclass Extended extends Base1;<br \/>\n  function new(input int var); \/\/ Needs argument<br \/>\n \u00a0 \u00a0 super.new(var);  \/\/ Must be first line of new<br \/>\n \u00a0 \u00a0 \/\/ Other constructor actions<br \/>\n  endfunction<br \/>\nendclass <\/p>\n<h3 id=\"driver%20class\">driver class<\/h3>\n<p>\u63a5\u6536generator\u4ea7\u751f\u7684\u4e8b\u52a1\u5e76\u53d1\u7ed9DUT<\/p>\n<p>Sample 8.4 Driver class<br \/>\nclass Driver;<br \/>\n  mailbox gen2drv;<br \/>\n \u00a0<br \/>\n  function new(input mailbox gen2drv);<br \/>\n    this.gen2drv &#061; gen2drv;<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\n  task main;<br \/>\n \u00a0 \u00a0 Transaction tr; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Handle to a Transaction object or<br \/>\n \u00a0 \u00a0                             \/\/ an class derived from Transaction<br \/>\n \u00a0 \u00a0 forever begin<br \/>\n \u00a0 \u00a0 \u00a0 gen2drv.get(tr); \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Get transaction from generator<br \/>\n \u00a0 \u00a0 \u00a0 tr.calc_crc(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Process the transation<br \/>\n \u00a0 \u00a0 \u00a0 &#064;ifc.cb.src &#061; tr.src; \u00a0 \u00a0 \/\/ Send transaction<br \/>\n \u00a0 \u00a0 \u00a0 &#8230;<br \/>\n \u00a0 \u00a0 end<br \/>\n  endtask<br \/>\nendclass <\/p>\n<p>\u5982\u679c\u4e00\u4e2ahandle\u80fd\u591f\u6307\u5411parent class&#xff0c;\u90a3\u4e48\u5b83\u4e5f\u80fd\u6307\u5411subclass\u3002<\/p>\n<p>\u5982\u679cparent class\u548csubclass\u4e2d\u90fd\u6709\u540d\u5b57\u76f8\u540c\u7684\u51fd\u6570&#xff0c;\u7f16\u8bd1\u5668\u600e\u4e48\u77e5\u9053\u8c03\u7528\u54ea\u4e2a&#xff1f;\u5982\u679chandle\u6307\u5411subclass&#xff0c;\u5c31\u8c03\u7528subclass\u91cc\u7684&#xff1b;\u5982\u679c\u6307\u5411parent class&#xff0c;\u5c31\u8c03\u7528parent class\u91cc\u7684\u3002<\/p>\n<h3 id=\"generator%20class\">generator class<\/h3>\n<p>Sample 8.5 Generator class<br \/>\n\/\/ Generator class that uses Transaction objects<br \/>\n\/\/ First attempt&#8230; too limited<br \/>\nclass Generator;<br \/>\n  mailbox gen2drv;<br \/>\n  Transaction tr;<br \/>\n \u00a0<br \/>\n  function new(input mailbox gen2drv);<br \/>\n    this.gen2drv &#061; gen2drv; \u00a0  \/\/ this-&gt; class-level var<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\n  task run();<br \/>\n \u00a0 \u00a0 forever begin<br \/>\n \u00a0 \u00a0 \u00a0 tr &#061; new(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Construct transaction<br \/>\n \u00a0 \u00a0 \u00a0 assert(tr.randomize());  \/\/ Randomize it<br \/>\n \u00a0 \u00a0 \u00a0 gen2drv.put(tr); \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Send to driver \u00a0<br \/>\n \u00a0 \u00a0 end<br \/>\n  endtask<br \/>\nendclass <\/p>\n<p>\u5bf9\u4e8e\u8fd9\u79cdgenerator&#xff0c;\u6709\u4e00\u4e2a\u95ee\u9898\u662fhandle\u88ab\u4f8b\u5316\u5b8c\u9a6c\u4e0a\u5c31\u968f\u673a\u5316&#xff0c;\u6ca1\u673a\u4f1a\u6307\u5411subclass\u3002\u8fd9\u4e2a\u95ee\u9898\u4f1a\u5728\u540e\u9762\u89e3\u51b3\u3002<\/p>\n<p>\u6570\u636e\u5411\u7684class\u6bd4\u5982\u7f51\u7edc\u3001\u603b\u7ebf\u4e8b\u52a1\u90fd\u4f1a\u6709\u7c7b\u4f3c\u7684property\u548cmethod\u3002<\/p>\n<p>\u63a7\u5236\u5411\u7684class\u6bd4\u5982generator\u548cdriver\u6709\u76f8\u540c\u7684\u7ed3\u6784&#xff0c;\u53ef\u4ee5\u628a\u5b83\u4eec\u90fd\u547d\u540d\u4e3atransaction\u7684subclass&#xff0c;\u52a0\u4e00\u4e9bvirtual run\u548cwrap_up method\u8fdb\u53bb\u3002<\/p>\n<h2 id=\"8.2%20%E8%93%9D%E5%9B%BE%EF%BC%88blueprint%EF%BC%89%E6%A8%A1%E5%BC%8F\">8.2 \u84dd\u56fe&#xff08;blueprint&#xff09;\u6a21\u5f0f<\/h2>\n<p>\u5982\u679c\u6211\u4eec\u628agenerator\u60f3\u8c61\u6210\u4e00\u4e2a\u5370\u82b1\u673a&#xff0c;\u6839\u636e\u6a21\u5177\u7684\u5f62\u72b6\u5f15\u51fa\u5bf9\u5e94\u5f62\u72b6\u7684\u7269\u54c1\u7136\u540e\u53d1\u9001\u51fa\u53bb&#xff0c;\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8\u6a21\u5177\u7684\u5f62\u72b6\u6765\u5b9e\u73b0\u8f93\u51fa\u4e0d\u540c\u7684\u7269\u54c1\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"454\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260814144855-6a7f2ad7d3a2b.png\" width=\"1048\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"453\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260814144856-6a7f2ad81750e.png\" width=\"1053\" \/><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u7279\u70b9\u5c31\u662f\u9700\u8981\u628adeclaration\u548cconstruction\u5206\u5f00\u3002<\/p>\n<p>Sample 8.6 Generator class using blueprint pattern<br \/>\nclass Generator;<br \/>\n  mailbox gen2drv;<br \/>\n  Transaction blueprint;<br \/>\n \u00a0<br \/>\n  function new(input mailbox gen2drv);<br \/>\n \u00a0 \u00a0 this.gen2drv &#061; gen2drv;<br \/>\n \u00a0 \u00a0 blueprint &#061; new();<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\n  task run();<br \/>\n \u00a0 \u00a0 Transaction tr;<br \/>\n \u00a0 \u00a0 forever begin<br \/>\n \u00a0 \u00a0 \u00a0 assert(blueprint.randomize);<br \/>\n \u00a0 \u00a0 \u00a0 tr &#061; blueprint.copy();<br \/>\n \u00a0 \u00a0 \u00a0 gen2drv.put(tr); \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 end<br \/>\n  endtask<br \/>\nendclass <\/p>\n<p>tr\u5c31\u662f\u4e0a\u56fe\u4e2d\u7684\u526f\u672ccopy&#xff0c;blueprint\u5c31\u662f\u8f93\u5165\u7684\u6a21\u5177&#xff0c;\u901a\u8fc7\u6539\u53d8blueprint\u5c31\u80fd\u5b9e\u73b0\u8f93\u51fasubclass\u3002<\/p>\n<p>.copy\u51fd\u6570\u8fd4\u56de\u7684\u662fhandle&#xff0c;\u56e0\u6b64\u4e0d\u4f1a\u51fa\u73b0\u591a\u4e2ahandle\u6307\u5411\u4e00\u4e2aobject\u3002<\/p>\n<\/p>\n<h3 id=\"environment%20class\">environment class<\/h3>\n<p>Sample 8.7 Environment class<br \/>\n\/\/ Testbench environment class<br \/>\nclass Environment;<br \/>\n  Generator gen;<br \/>\n  Driver drv;<br \/>\n  mailbox gen2drv;<br \/>\n \u00a0<br \/>\n  function void build(); \u00a0 \/\/ Build the environment by<br \/>\n \u00a0 \u00a0 gen2drv &#061; new(); \u00a0 \u00a0 \u00a0 \/\/ constructing the mailbox,<br \/>\n \u00a0 \u00a0 gen &#061; new(gen2drv); \u00a0  \/\/ generator,<br \/>\n \u00a0 \u00a0 drv &#061; new(gen2drv); \u00a0  \/\/ and driver<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\n  task run();<br \/>\n \u00a0 \u00a0 fork<br \/>\n \u00a0 \u00a0 \u00a0 gen.run();<br \/>\n \u00a0 \u00a0 \u00a0 drv.run();<br \/>\n \u00a0 \u00a0 join_none<br \/>\n  endtask<br \/>\n \u00a0<br \/>\n  task wrap_up();<br \/>\n     \/\/ Empty for now &#8211; call scoreboard for report<br \/>\n  endtask<br \/>\nendclass <\/p>\n<h3 id=\"%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84testbench\">\u4e00\u4e2a\u7b80\u5355\u7684testbench<\/h3>\n<p>Sample 8.8 Simple test program using environment defaults<br \/>\nprogram automatic test;<br \/>\n  Environment env;<br \/>\n  initial begin<br \/>\n \u00a0 \u00a0 env &#061; new(); \u00a0  \/\/ Construct the environment<br \/>\n \u00a0 \u00a0 env.build(); \u00a0  \/\/ Build testbench objects<br \/>\n \u00a0 \u00a0 env.run(); \u00a0 \u00a0  \/\/ Run the test<br \/>\n \u00a0 \u00a0 env.wrap_up; \u00a0  \/\/ Clean up afterwards<br \/>\n  end<br \/>\nendprogram <\/p>\n<h3 id=\"%E7%94%A8%E6%89%A9%E5%B1%95transaction%20class\">\u7528\u6269\u5c55transaction class<\/h3>\n<p>\u4e0b\u9762\u7684begin&#8230;end\u662f\u4e3a\u4e86\u9650\u5236badtr\u7684\u4f5c\u7528\u8303\u56f4<\/p>\n<p>Sample 8.9 Injecting an extended transaction into testbench<br \/>\nprogram automatic test;<br \/>\n\u200b<br \/>\n  Environment env;<br \/>\n  initial begin<br \/>\n \u00a0 \u00a0 env &#061; new();<br \/>\n \u00a0 \u00a0 env.build(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Construct generator, etc.<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 begin<br \/>\n \u00a0 \u00a0 \u00a0 BadTr bad &#061; new(); \u00a0 \u00a0 \u00a0  \/\/ Replace blueprint with<br \/>\n \u00a0 \u00a0 \u00a0 env.gen.blueprint &#061; bad;  \/\/ the &#034;bad&#034; one<br \/>\n \u00a0 \u00a0 end<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 env.run(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 env.wrap_up(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n  end<br \/>\nendprogram <\/p>\n<h3 id=\"%E7%94%A8%E6%89%A9%E5%B1%95%E7%B1%BB%E6%94%B9%E5%8F%98%E9%9A%8F%E6%9C%BA%E7%BA%A6%E6%9D%9F\">\u7528\u6269\u5c55\u7c7b\u6539\u53d8\u968f\u673a\u7ea6\u675f<\/h3>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u5728\u6269\u5c55\u7c7b\u4e2d\u52a0\u4e86\u7ea6\u675f\u3002\u6ce8\u610f&#xff0c;\u5982\u679c\u6269\u5c55\u7c7b\u4e2d\u7ea6\u675f\u7684\u540d\u5b57\u548cparent class\u76f8\u540c&#xff0c;\u90a3\u4e48\u4f1a\u53d1\u751f\u66ff\u4ee3\u884c\u4e3a\u3002<\/p>\n<p>Sample 8.10 Using inheritance to add a constraint<br \/>\nclass Nearby extends Transaction;<br \/>\n  constraint c_nearby {<br \/>\n    dst inside {[src-100:src&#043;100]};<br \/>\n  }<br \/>\nendclass<br \/>\n\u200b<br \/>\nprogram automatic test;<br \/>\n  Environment env;<br \/>\n  initial begin<br \/>\n \u00a0 \u00a0 env &#061; new();<br \/>\n \u00a0 \u00a0 env.build(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Construct generator, etc.<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 begin<br \/>\n \u00a0 \u00a0 \u00a0 Nearby nb &#061; new(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Create a new blueprint<br \/>\n \u00a0 \u00a0 \u00a0 env.gen.blueprint &#061; nb; \u00a0 \u00a0  \/\/ Replace the blueprint<br \/>\n \u00a0 \u00a0 end<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 env.run(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 env.wrap_up(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n  end<br \/>\nendprogram <\/p>\n<h2 id=\"8.3%20downcasting%20and%20virtual%20method\">8.3 downcasting and virtual method<\/h2>\n<h3 id=\"%E7%94%A8%24cast%20downcasting\">\u7528$cast downcasting<\/h3>\n<p>\u8fd9\u91cc\u4e3e\u4e86\u4e24\u7c7b\u505a\u4f8b\u5b50&#xff0c;\u4e00\u4e2a\u662fbase class transaction&#xff0c;\u4e00\u4e2a\u662fextended class badtr\u3002\u524d\u8005\u58f0\u660e\u4e86\u4e00\u4e2ahandle tr&#xff0c;\u540e\u8005\u58f0\u660e\u4e86\u4e24\u4e2ahandle bad bad2\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u884c\u4e3a\u662f\u5408\u6cd5\u7684&#xff0c;base handle\u53ef\u4ee5\u6307\u5411extend class<\/p>\n<p>Sample 8.12 Copying extended handle to base handle<br \/>\nTransaction tr;<br \/>\nBadTr bad;<br \/>\nbad &#061; new(); \u00a0 \u00a0 \u00a0 \/\/ Construct BadTr extended object<br \/>\ntr &#061; bad; \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Base handle points to extended obj<br \/>\n$display(tr.src);  \/\/ Display base variable<br \/>\ntr.display; \u00a0 \u00a0 \u00a0  \/\/ Calls BadTr::display <\/p>\n<p>\u4e0b\u9762\u7684\u884c\u4e3a\u662f\u975e\u6cd5\u7684&#xff0c;extend handle\u4e0d\u80fd\u6307\u5411base class&#xff0c;\u56e0\u4e3a\u5b83\u4f1a\u627e\u4e0d\u5230extend\u4e2d\u989d\u5916\u5b9a\u4e49\u7684\u53d8\u91cf<\/p>\n<p>Sample 8.13 Copying a base handle to an extended handle<br \/>\ntr &#061; new(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Construct base object<br \/>\nbad &#061; tr; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ ERROR: WILL NOT COMPILE<br \/>\n$display(bad.bad_crc);  \/\/ bad_crc is not in base object <\/p>\n<p>\u4f46\u4e0b\u9762\u7684\u884c\u4e3a\u662f\u5408\u6cd5\u7684&#xff0c;\u4e0a\u4e00\u79cd\u975e\u6cd5\u7684\u539f\u56e0\u4e0d\u5728handle\u672c\u8eab&#xff0c;\u800c\u5728\u4e8eobject&#xff0c;\u5982\u679cbase handle\u672c\u8eab\u6307\u5411extend class&#xff0c;\u90a3\u4e48\u8fd9\u79cd\u8d4b\u503c\u5c31\u5408\u6cd5\u4e86\u3002<\/p>\n<p>Sample 8.14 Using $cast to copy handles<br \/>\nbad &#061; new(); \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Construct BadTr extended object<br \/>\ntr &#061; bad; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Base handle points to extended obj<br \/>\n\u200b<br \/>\n\/\/ Check the object type &amp; copy. Simulation error if mismatch<br \/>\n\/\/ If successful, bad2 points to the object referenced by tr<br \/>\n$cast(bad2, tr);<br \/>\n\u200b<br \/>\n\/\/ Check for type mismatch, no simulation error<br \/>\nif(!$cast(bad2, tr))<br \/>\n  $display(&#034;cannot assign tr to bad2&#034;);<br \/>\n \u00a0<br \/>\n$display(bad2.bad_crc); \/\/ bad_crc exists in original obj <\/p>\n<p>$cast\u4f1a\u628atr\u7684object\u8d4b\u503c\u7ed9bad2&#xff0c;\u540c\u65f6\u68c0\u67e5\u662f\u5426\u5408\u7406&#xff0c;\u5982\u679c\u4e0d\u5408\u7406\u4f1a\u8fd4\u56de0\u3002<\/p>\n<h3 id=\"virtual%20method\">virtual method<\/h3>\n<p>\u4ecd\u7136\u7528\u4e4b\u524d\u8bf4\u7684\u4e24\u4e2aclass&#xff0c;\u4e0b\u9762\u662f\u5bf9virtual method\u7684\u8c03\u7528<\/p>\n<p>Sample 8.16 Calling class methods<br \/>\nTransaction tr;<br \/>\nBadTr bad;<br \/>\n\u200b<br \/>\ninitial begin<br \/>\n  tr &#061; new();<br \/>\n  tr.calc_crc(); \u00a0 \/\/ Calls Transaction::calc_crc<br \/>\n \u00a0<br \/>\n  bad &#061; new();<br \/>\n  bad.calc_crc();  \/\/ Calls BadTr::calc_crc<br \/>\n \u00a0<br \/>\n  tr &#061; bad; \u00a0 \u00a0 \u00a0  \/\/ Base handle points to ext obj<br \/>\n  tr.calc_crc(); \u00a0 \/\/ Calls BadTr::calc_crc<br \/>\nend <\/p>\n<p>\u5982\u679c\u4e00\u4e2amethod\u88ab\u6807\u8bb0\u4e3avirtual&#xff0c;\u5f53\u8c03\u7528\u7684\u65f6\u5019&#xff0c;sv\u4f1a\u6839\u636eobject\u7684type\u53bb\u8c03\u7528&#xff1b;\u4f46\u5982\u679c\u6ca1\u6709\u88ab\u6807\u8bb0\u4e3avirtual&#xff0c;\u90a3\u5c31\u4f1a\u6839\u636ehandle\u7684type\u53bb\u8c03\u7528&#xff0c;\u6bd4\u5982\u6700\u540e\u4ee5\u4e00\u4e2atr\u8c03\u7528\u7684\u4ecd\u7136\u4f1a\u662fTransaction::calc_crc<\/p>\n<p>OOP\u4e2d\u591a\u4e2a\u5b50\u7a0b\u5e8f\u53eb\u76f8\u540c\u540d\u5b57\u7684\u5c5e\u4e8e\u53eb\u505a\u201c\u591a\u6001&#xff08;polymorphism&#xff09;\u201d<\/p>\n<h3 id=\"%E7%AD%BE%E5%90%8D%EF%BC%88signature%EF%BC%89\">\u7b7e\u540d&#xff08;signature&#xff09;<\/h3>\n<p>\u4f7f\u7528virtual method\u7684\u95ee\u9898\u662f&#xff0c;\u6240\u6709extend class\u90fd\u5fc5\u987b\u6709\u76f8\u540c\u7684signature&#xff0c;\u5177\u4f53\u8868\u73b0\u4e3a\u5b83\u4eec\u7684\u53c2\u6570\u5fc5\u987b\u76f8\u540c&#xff0c;\u4e0d\u80fd\u52a0\u4e5f\u4e0d\u80fd\u51cf\u3002<\/p>\n<h2 id=\"8.4%20%E5%90%88%E6%88%90%E3%80%81%E7%BB%A7%E6%89%BF%E5%92%8C%E5%85%B6%E4%BB%96%E6%9B%BF%E4%BB%A3%E6%96%B9%E6%B3%95\">8.4 \u5408\u6210\u3001\u7ee7\u627f\u548c\u5176\u4ed6\u66ff\u4ee3\u65b9\u6cd5<\/h2>\n<h3 id=\"%E5%90%88%E6%88%90%E4%B8%8E%E7%BB%A7%E6%89%BF%EF%BC%88composition%20and%20inheritance%EF%BC%89\">\u5408\u6210\u4e0e\u7ee7\u627f&#xff08;composition and inheritance&#xff09;<\/h3>\n<p>composition\u7684\u7cbe\u9ad3\u5728\u4e8e\u201c\u6709\u201d&#xff0c;\u6bd4\u5982\u4e00\u4e2apacket\u6709header\u548cbody\u3002<\/p>\n<p>inheritance\u7684\u7cbe\u9ad3\u5728\u4e8e\u201c\u662f\u201d&#xff0c;\u6bd4\u5982badtr\u662ftransaction&#xff0c;\u53ea\u4e0d\u8fc7\u591a\u4e86\u4e9b\u4fe1\u606f\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u5b83\u4eec\u4e4b\u95f4\u7684\u5bf9\u6bd4<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"603\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260814144856-6a7f2ad850180.png\" width=\"1092\" \/><\/p>\n<p>1.\u662f\u5426\u6709\u591a\u4e2a\u5c0fclass\u8981\u8fde\u63a5\u6210\u5927class&#xff1f;\u5982\u679c\u662f\u7684\u8bdd&#xff0c;\u53ea\u80fd\u7528composition&#xff0c;\u56e0\u4e3ainheritance\u4e0d\u652f\u6301\u4ece\u591a\u4e2aclass\u7ee7\u627f<\/p>\n<p>2.badtr\u548ctransaction\u662f\u4e00\u4e2a\u5c42\u6b21\u7684<\/p>\n<p>3.\u5bf9\u4e8einheritance&#xff0c;\u4f4e\u5c42\u6b21\u7684\u4fe1\u606f&#xff0c;\u6bd4\u5982src&#xff0c;dst\u90fd\u9700\u8981\u5b58\u5728<\/p>\n<p>4.\u5bf9\u4e8egenerator\u6765\u8bf4&#xff0c;\u5b83\u7684\u529f\u80fd\u662f\u53d1\u9001transaction&#xff0c;\u5982\u679cbadtr\u662fcomposite transaction&#xff0c;\u90a3\u4e48generator\u672c\u8eab\u662f\u65e0\u6cd5\u8bc6\u522b\u8fd9\u79cd\u65b0\u4e1c\u897f\u7684&#xff0c;\u5c31\u9700\u8981\u6539\u4ee3\u7801&#xff1b;\u4f46\u5982\u679c\u5b83\u4eec\u662finheritance\u7684\u5173\u7cfb&#xff0c;generator\u5c31\u53ef\u4ee5\u628abadtr\u5f53\u6210transaction\u4e00\u6837\u53d1\u9001\u3002<\/p>\n<h3 id=\"composition%E7%9A%84%E9%97%AE%E9%A2%98\">composition\u7684\u95ee\u9898<\/h3>\n<p>\u5982\u679c\u4f60\u8fd9\u4e48\u5199\u4e00\u4e2aclass&#xff0c;\u4e3a\u4e86\u65b9\u4fbf\u7406\u89e3\u6545\u610f\u628atransaction\u62c6\u5206\u4e3a\u5c0f\u7684composition<\/p>\n<p>Sample 8.17 Building an Ethernet frame with composition<br \/>\n\/\/ Not recommended<br \/>\nclass EthMacFrame;<br \/>\n  typedef enum {II, IEEE} kind_e;<br \/>\n  rand kind_e kind;<br \/>\n  rand bit [47:0] da, sa;<br \/>\n  rand bit [15:0] len;<br \/>\n  &#8230;<br \/>\n  rand Vlan vlan_h;<br \/>\nendclass<br \/>\n\u200b<br \/>\nclass Vlan;<br \/>\n  rand bit [15:0] vlan;<br \/>\nendclass <\/p>\n<p>1.\u5f53\u4f60\u60f3\u8bbf\u95eevlan&#xff0c;\u9700\u8981\u5199eth_h.vlan_h.vlan&#xff0c;\u5982\u679c\u5c42\u6b21\u66f4\u591a&#xff0c;\u540d\u5b57\u4f1a\u53d8\u5f97\u66f4\u957f\u66f4\u590d\u6742<\/p>\n<p>2.kind\u53ef\u80fd\u4f1a\u88ab\u968f\u673a\u5316\u4e3aII\u6216\u8005IEEE&#xff0c;\u5982\u679c\u662fII&#xff0c;\u90a3\u5c31\u4e0d\u9700\u8981vlan&#xff0c;\u5982\u679c\u662fIEEE&#xff0c;\u90a3\u5c31\u9700\u8981vlan\u3002\u800c\u968f\u673a\u5316\u7684\u65f6\u5019\u4e0d\u77e5\u9053kind\u4f1a\u662f\u4ec0\u4e48&#xff0c;\u6240\u4ee5\u5728new()\u51fd\u6570\u4e2d&#xff0c;\u65e0\u8bba\u5982\u4f55\u90fd\u9700\u8981\u968f\u673a\u5316vlan&#xff0c;\u9632\u6b62\u62a5\u9519\u3002\u65e2\u7136\u9700\u8981\u65e0\u6761\u4ef6\u968f\u673a\u5316vlan&#xff0c;\u90a3\u628a\u5b83\u989d\u5916\u5206\u4e00\u7c7b\u5c31\u6ca1\u610f\u4e49\u4e86\u3002<\/p>\n<p>\u6240\u4ee5\u5de5\u7a0b\u4e0a\u4e0d\u592a\u63a8\u8350\u4f7f\u7528\u8fd9\u79cd\u5c42\u6b21\u5316&#xff0c;\u8c03\u7528\u8d77\u6765\u9ebb\u70e6&#xff0c;\u66f4\u559c\u6b22\u6241\u5e73\u5316&#xff0c;\u5c31\u628a\u5404\u79cd\u53d8\u91cf\u76f4\u63a5\u5199\u8fdb\u53bb\u5c31\u884c&#xff0c;\u5b9e\u5728\u4e0d\u884c\u518d\u7528inheritance&#xff0c;\u5b83\u5728\u8c03\u7528\u4e0a\u4e5f\u662f\u4e00\u79cd\u6241\u5e73\u5316\u3002<\/p>\n<h3 id=\"inheritacne%E7%9A%84%E9%97%AE%E9%A2%98\">inheritacne\u7684\u95ee\u9898<\/h3>\n<p>Sample 8.18 Building an Ethernet frame with inheritance<br \/>\n\/\/ Not recommended<br \/>\nclass EthMacFrame;<br \/>\n  typedef enum {II, IEEE} kind_e;<br \/>\n  rand kind_e kind;<br \/>\n  rand bit [47:0] da, sa;<br \/>\n  rand bit [15:0] len;<br \/>\n  &#8230;<br \/>\nendclass<br \/>\n\u200b<br \/>\nclass Vlan extends EthMacFrame;<br \/>\n  rand bit [15:0] vlan;<br \/>\nendclass <\/p>\n<p>1.\u6bcf\u5f53\u4f60\u5c1d\u8bd5\u7ed9extend handle\u8d4bbase handle\u7684\u65f6\u5019&#xff0c;\u90fd\u8981\u7528$cast\u68c0\u67e5<\/p>\n<p>2.\u521b\u5efavirtual method\u662f\u590d\u6742\u7684&#xff0c;\u56e0\u4e3a\u53c2\u6570\u7684\u4e2a\u6570\u4e0d\u80fd\u6539&#xff0c;\u5982\u679c\u60f3\u6539\u9700\u8981\u4ecebase class\u4fee\u6539<\/p>\n<p>3.randomization\u6709\u56f0\u96be&#xff0c;\u5728base class\u4e2d\u4e0d\u80fd\u7ea6\u675fvlan<\/p>\n<h3 id=\"%E7%8E%B0%E5%AE%9E%E7%9A%84%E9%80%89%E6%8B%A9\">\u73b0\u5b9e\u7684\u9009\u62e9<\/h3>\n<p>\u5b9e\u9645\u4e0a\u6211\u4eec\u9700\u8981\u6709\u6240\u53d6\u820d&#xff0c;composition\u5e26\u6765\u5c42\u6b21\u5316\u95ee\u9898&#xff0c;inheritance\u5e26\u6765\u8bbe\u8ba1\u56f0\u96be\u7684\u95ee\u9898&#xff0c;\u4e00\u4e2a\u7b80\u5355\u7684\u529e\u6cd5\u662f\u628a\u6240\u6709\u5185\u5bb9\u90fd\u5199\u5728\u4e00\u4e2aclass\u91cc<\/p>\n<p>Sample 8.19 Building a flat Ethernet frame<br \/>\nclass eth_mac_frame;<br \/>\n  typedef enum {II, IEEE} kind_e;<br \/>\n  rand kind_e kind;<br \/>\n  rand bit [47:0] da, sa;<br \/>\n  rand bit [15:0] len, vlan;<br \/>\n  &#8230;<br \/>\n  constraint eth_mac_frame_II {<br \/>\n \u00a0 \u00a0 if (kind &#061;&#061; II) {<br \/>\n \u00a0 \u00a0 \u00a0 data.size() inside {[46:1500]};<br \/>\n \u00a0 \u00a0 \u00a0 len &#061;&#061; data.size();<br \/>\n  }}<br \/>\n  constraint eth_mac_frame_ieee {<br \/>\n \u00a0 \u00a0 if (kind &#061;&#061; IEEE) {<br \/>\n \u00a0 \u00a0 \u00a0 data.size() inside {[46:1500]};<br \/>\n \u00a0 \u00a0 \u00a0 len &lt; 1522;<br \/>\n  }}<br \/>\nendclass<br \/>\n\u200b <\/p>\n<h2 id=\"8.5%20%E5%A4%8D%E5%88%B6object\">8.5 \u590d\u5236object<\/h2>\n<p>\u524d\u9762\u5728blueprint\u90e8\u5206\u4e2d\u6211\u4eec\u770b\u5230\u4e86copy\u51fd\u6570&#xff0c;\u672c\u8282\u8be6\u7ec6\u8bb2\u4e00\u4e0b\u3002copy\u662f\u8981\u628aclass\u4e2d\u4ea7\u751f\u7684\u65b0\u6570\u636ecopy\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<p>Sample 8.20 Base transaction class with a virtual copy function<br \/>\nclass Transaction;<br \/>\n  rand bit [31:0] src, dst, data[8];  \/\/ Variables<br \/>\n  bit [31:0] crc;<br \/>\n \u00a0<br \/>\n  virtual function Transaction copy();<br \/>\n \u00a0 \u00a0 copy &#061; new();<br \/>\n \u00a0 \u00a0 copy.src &#061; src;  \/\/ Copy data fields<br \/>\n \u00a0 \u00a0 copy.dst &#061; dst;<br \/>\n \u00a0 \u00a0 copy.data &#061; data;<br \/>\n \u00a0 \u00a0 copy.crc &#061; crc;<br \/>\n  endfunction<br \/>\nendclass <\/p>\n<p>subclass\u7684copy\u51fd\u6570\u8fd4\u56de\u7684object\u7c7b\u578b\u4ecd\u7136\u662ftransaction<\/p>\n<p>Sample 8.21 Extended transaction class with virtual copy method<br \/>\nclass BadTr extends Transaction;<br \/>\n  rand bit bad_crc;<br \/>\n  virtual function Transaction copy();<br \/>\n \u00a0 \u00a0 BadTr bad;<br \/>\n \u00a0 \u00a0 bad &#061; new();<br \/>\n \u00a0 \u00a0 bad.src &#061; src;  \/\/ Copy data fields<br \/>\n \u00a0 \u00a0 bad.dst &#061; dst;<br \/>\n \u00a0 \u00a0 bad.data &#061; data;<br \/>\n \u00a0 \u00a0 bad.crc &#061; crc;<br \/>\n \u00a0 \u00a0 bad.bad_crc &#061; bad_crc;<br \/>\n \u00a0 \u00a0 return bad;<br \/>\n  endfunction<br \/>\nendclass : BadTr <\/p>\n<h3 id=\"copy_data\">copy_data<\/h3>\n<p>\u89e3\u51b3\u65b9\u6cd5\u4e4b\u4e00\u5c31\u662f\u628acopy\u62c6\u5206\u4e3a\u4e24\u4e2a\u51fd\u6570&#xff0c;\u4e00\u4e2a\u662f\u521b\u5efa\u5e76copy&#xff0c;\u53e6\u4e00\u4e2a\u662fcopy_data\u3002<\/p>\n<p>\u4e0b\u9762\u662fbase class\u7684copy_data<\/p>\n<p>Sample 8.22 Base transaction class with copy_data function<br \/>\nclass Transaction;<br \/>\n  rand bit [31:0] src, dst, data[8];  \/\/ Variables<br \/>\n  bit [31:0] crc;<br \/>\n \u00a0<br \/>\n  virtual function void copy_data(input Transaction tr);<br \/>\n \u00a0 \u00a0 tr.src  &#061; src; \u00a0 \u00a0 \u00a0  \/\/ Copy the data fields<br \/>\n \u00a0 \u00a0 tr.dst  &#061; dst;<br \/>\n \u00a0 \u00a0 tr.data &#061; data;<br \/>\n \u00a0 \u00a0 tr.crc  &#061; crc;<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\n  virtual function Transaction copy();<br \/>\n \u00a0 \u00a0 copy &#061; new();<br \/>\n \u00a0 \u00a0 copy_data(copy);<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\nendclass <\/p>\n<p>\u4e0b\u9762\u662fsubclass\u7684copy_data&#xff0c;\u8981\u5148\u628a\u5b83base class\u7684argument\u90fdcopy\u4e86&#xff0c;\u518dcopy\u5b83\u4f5c\u4e3aextend class\u591a\u51fa\u6765\u7684\u90e8\u5206\u3002<\/p>\n<p>Sample 8.23 Extended transaction class with copy_data function<br \/>\nclass BadTr extends Transaction;<br \/>\n  rand bit bad_crc;<br \/>\n \u00a0<br \/>\n  virtual function void copy_data(input Transaction tr);<br \/>\n \u00a0 \u00a0 BadTr bad;<br \/>\n \u00a0 \u00a0 super.copy_data(tr); \u00a0 \u00a0 \/\/ Copy base data<br \/>\n \u00a0 \u00a0 $cast(bad, tr); \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Cast base handle to ext\u00d5d<br \/>\n \u00a0 \u00a0 bad.bad_crc &#061; bad_crc; \u00a0 \/\/ Copy extended data<br \/>\n  endfunction<br \/>\n\u200b<br \/>\n  virtual function Transaction copy();<br \/>\n \u00a0 \u00a0 BadTr bad;<br \/>\n \u00a0 \u00a0 bad &#061; new(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Construct BadTr<br \/>\n \u00a0 \u00a0 copy_data(bad); \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Copy data fields<br \/>\n \u00a0 \u00a0 return bad;<br \/>\n  endfunction<br \/>\nendclass : BadTr <\/p>\n<h3 id=\"%E4%B8%BAcopy%E6%8C%87%E5%AE%9A%E5%9C%B0%E5%9D%80\">\u4e3acopy\u6307\u5b9a\u5730\u5740<\/h3>\n<p>\u524d\u9762\u5728copy\u7684\u65f6\u5019&#xff0c;\u90fd\u9700\u8981\u989d\u5916\u5f00\u8f9f\u4e00\u5757\u5730\u5740&#xff0c;\u80fd\u4e0d\u80fd\u8282\u7ea6\u4e00\u70b9\u5185\u5b58&#xff1f;\u53ef\u4ee5\u76f4\u63a5\u4e3acopy\u6307\u5b9a\u5b83\u7684\u5730\u5740<\/p>\n<p>Sample 8.24 Base transaction class with copy function<br \/>\nclass Transaction;<br \/>\n\u200b<br \/>\n  virtual function Transaction copy(Transaction to&#061;null);<br \/>\n \u00a0 \u00a0 if (to &#061;&#061; null)<br \/>\n \u00a0 \u00a0 \u00a0 copy &#061; new();\/\/ Construct new object<br \/>\n \u00a0 \u00a0 else<br \/>\n \u00a0 \u00a0 \u00a0 copy &#061; to; \u00a0 \/\/ or use exisiting<br \/>\n \u00a0 \u00a0 copy_data(copy);<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\n  \/\/ Uses copy_data() method from Sample 8.22<br \/>\nendclass<br \/>\n\u200b<br \/>\nSample 8.25 Extended transaction class with new copy function<br \/>\nclass BadTr;<br \/>\n\u200b<br \/>\n  virtual function Transaction copy(Transaction to&#061;null);<br \/>\n \u00a0 \u00a0 BadTr bad;<br \/>\n \u00a0 \u00a0 if (to &#061;&#061; null)<br \/>\n \u00a0 \u00a0 \u00a0 bad &#061; new(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Create a new object<br \/>\n \u00a0 \u00a0 else<br \/>\n \u00a0 \u00a0 \u00a0 assert($cast(bad, to));  \/\/ Reuse existing one<br \/>\n \u00a0 \u00a0 copy_data(bad); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Copy data fields<br \/>\n \u00a0 \u00a0 return bad;<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\nendclass : BadTr <\/p>\n<h2 id=\"8.6%20%E6%8A%BD%E8%B1%A1%E7%B1%BB%E5%92%8C%E7%BA%AF%E8%99%9A%E6%96%B9%E6%B3%95\">8.6 \u62bd\u8c61\u7c7b\u548c\u7eaf\u865a\u65b9\u6cd5<\/h2>\n<p>\u62bd\u8c61\u7c7b\u548c\u7eaf\u865a\u65b9\u6cd5\u66f4\u50cf\u662f\u63d0\u4f9b\u4e86\u4e00\u79cd\u201c\u63a5\u53e3\u201d&#xff0c;\u4f5c\u4e3abase class&#xff0c;\u5b83\u6307\u51fa\u6240\u6709extend class\u9700\u8981\u6709\u4ec0\u4e48\u7c7b\u578b&#xff0c;\u4ec0\u4e48\u51fd\u6570\u3002<\/p>\n<p>Sample 8.26 Abstract class with pure virtual methods<br \/>\nvirtual class BaseTr;<br \/>\n  static int count; \u00a0  \/\/ Number of instance created<br \/>\n  int id; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Unique transaction id<br \/>\n \u00a0<br \/>\n  function new();<br \/>\n    id &#061; count&#043;&#043;; \u00a0 \u00a0  \/\/ Give each object a unique ID<br \/>\n  endfunction<br \/>\n\u200b<br \/>\n  pure virtual function bit compare(input BaseTr to);<br \/>\n  pure virtual function BaseTr copy(input BaseTr to&#061;null);<br \/>\n  pure virtual function void display(input string prefix&#061;&#034;&#034;);<br \/>\n \u00a0<br \/>\nendclass : BaseTr <\/p>\n<p>\u6240\u6709\u60f3\u57fa\u4e8ebasetr\u7684subclass\u90fd\u5fc5\u987b\u5b9e\u73b0\u5176\u4e2d\u6307\u51fa\u7684\u51fd\u6570&#xff0c;\u5426\u5219\u5c31\u4f1a\u65e0\u6cd5\u5b9e\u4f8b\u5316<\/p>\n<h2 id=\"8.7%20callback\">8.7 callback<\/h2>\n<p>\u6211\u4eec\u60f3\u5199\u4e00\u4e2a\u80fd\u5c3d\u53ef\u80fd\u901a\u7528\u7684\u6d4b\u8bd5\u5e73\u53f0&#xff0c;\u90a3\u4e48\u5c31\u7528callback\u3002\u5b83\u7684\u4e3b\u8981\u601d\u60f3\u662f&#xff1a;driver\u7684run\u6d41\u7a0b\u662f\u56fa\u5b9a\u7684&#xff0c;\u5373\u53d6\u4e8b\u52a1-&gt;pre_tx-&gt;\u53d1\u9001-&gt;post_tx&#xff0c;\u8fd9\u4e2a\u5e8f\u5217\u5728\u7f16\u8bd1\u6216\u4eff\u771f\u5f00\u59cb\u524d\u5c31\u5df2\u786e\u5b9a&#xff0c;\u4e0d\u4f1a\u56e0\u6d4b\u8bd5\u7528\u4f8b\u4e0d\u540c\u800c\u6539\u53d8\u3002\u4e24\u4e2atx\u4e2d\u5177\u4f53\u6267\u884c\u4ec0\u4e48\u4ee3\u7801\u4e0d\u7531 Driver \u51b3\u5b9a&#xff0c;\u800c\u7531\u6d4b\u8bd5\u7528\u4f8b\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u6ce8\u5165\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"477\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260814144856-6a7f2ad8989c3.png\" width=\"1099\" \/><\/p>\n<p>\u53ef\u4ee5\u5c06run\u5199\u4f5cvirtual method&#xff0c;\u4f46\u95ee\u9898\u662f\u5f53\u4f60\u60f3\u589e\u52a0\u65b0\u884c\u4e3a\u65f6&#xff0c;\u5fc5\u987b\u5728new method\u4e2d\u91cd\u590d\u6e90\u7801\u3002\u800c\u4e14\u4e00\u65e6\u5bf9base class\u505a\u4e86\u4fee\u6539&#xff0c;\u4f1a\u5bf9\u6240\u6709\u7684subclass\u90fd\u6709\u5f71\u54cd\u3002<\/p>\n<h3 id=\"%E5%88%9B%E5%BB%BAcallback\">\u521b\u5efacallback<\/h3>\n<p>\u4e00\u4e2acallback task\u662f\u5728\u9876\u5c42\u7684test\u4e2d\u521b\u5efa\u7684&#xff0c;\u5728driver\u4e2d\u8c03\u7528\u3002\u4f46driver\u5e76\u4e0d\u4f9d\u8d56\u4e8e\u6d4b\u8bd5\u4ee3\u7801&#xff0c;\u800c\u662f\u4f9d\u8d56\u4e8e\u4e00\u4e2aabstract class\u3002\u6d4b\u8bd5\u4ee3\u7801\u901a\u8fc7extend abstract class\u6765\u5199\u5165\u5b83\u60f3\u5199\u7684\u4e1c\u897f\u3002<\/p>\n<p>\u6ce8\u610f&#xff0c;pre_tx\u548cpost_tx\u5e76\u6ca1\u6709\u5199\u4f5cpure virtual&#xff0c;\u8fd9\u662f\u56e0\u4e3a\u6709\u7684\u6d4b\u8bd5\u53ef\u80fd\u53ea\u7528\u5176\u4e2d\u4e00\u4e2a&#xff0c;\u5982\u679c\u5199\u4f5cpure\u8fd8\u5fc5\u987b\u8981\u5b9e\u73b0\u53e6\u4e00\u4e2a\u3002<\/p>\n<p>Sample 8.29 Base callback class<br \/>\nvirtual class Driver_cbs;  \/\/ Driver callbacks<br \/>\n  virtual task pre_tx(ref Transaction tr, ref bit drop);<br \/>\n     \/\/ By default, callback does nothing<br \/>\n  endtask<br \/>\n \u00a0<br \/>\n  virtual task post_tx(ref Transaction tr);<br \/>\n     \/\/ By default, callback does nothing<br \/>\n  endtask<br \/>\nendclass<br \/>\n\u200b<br \/>\nSample 8.30 Driver class with callbacks<br \/>\nclass Driver;<br \/>\n  Driver_cbs cbs[$];<br \/>\n \u00a0<br \/>\n  task run();<br \/>\n \u00a0 \u00a0 bit drop;<br \/>\n \u00a0 \u00a0 Transaction tr;<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 forever begin<br \/>\n \u00a0 \u00a0 \u00a0 drop &#061; 0;<br \/>\n \u00a0 \u00a0 \u00a0 agt2drv.get(tr);<br \/>\n \u00a0 \u00a0 \u00a0 foreach (cbs[i]) cbs[i].pre_tx(tr, drop);<br \/>\n \u00a0 \u00a0 \u00a0 if (!drop) continue;<br \/>\n \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 transmit(tr);<br \/>\n \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 foreach (cbs[i]) cbs[i].post_tx(tr);<br \/>\n \u00a0 \u00a0 end<br \/>\n  endtask<br \/>\nendclass <\/p>\n<h3 id=\"%E7%94%A8callback%E6%B3%A8%E5%85%A5%E5%B9%B2%E6%89%B0\">\u7528callback\u6ce8\u5165\u5e72\u6270<\/h3>\n<p>\u6211\u4eec\u901a\u5e38\u7528callback\u6ce8\u5165\u9519\u8bef\u3001\u5ef6\u65f6\u7b49\u3002<\/p>\n<p>$urandom_range(0,99)\u662f\u57280-99\u4e4b\u95f4\u968f\u673a\u4ea7\u751f\u4e00\u4e2a\u6570\u3002\u7136\u540e\u53bb\u548c0\u6bd4\u8f83&#xff0c;\u5982\u679c\u76f8\u7b49&#xff0c;drop&#061;1&#xff0c;\u8868\u793a\u5f53\u524d\u7684\u8fd9\u4e2a\u4e8b\u52a1\u8981\u4e22\u5f03\u3002<\/p>\n<p>\u5728program\u4e2d&#xff0c;\u901a\u8fc7initialize dcd&#xff0c;\u7ed9dcd\u521b\u5efa\u597dobject\u3002push_back\u653e\u5230\u524d\u9762\u4ee3\u7801\u4e2d\u5199\u7684cbs\u5e8f\u5217\u4e2d&#xff0c;push_back\u8868\u793a\u653e\u5230\u961f\u5c3e&#xff0c;\u4e5f\u53ef\u4ee5\u7528push_front&#xff0c;\u653e\u5230\u6700\u524d\u9762\u3002<\/p>\n<p>\u5728\u542f\u52a8run\u7684\u65f6\u5019&#xff0c;\u4f1a\u968f\u673a\u51fadrop\u503c&#xff0c;\u8fd9\u6837\u5c31\u77e5\u9053\u5f53\u524d\u8fd9\u4e2atransaction\u662f\u5426\u4e22\u5f03\u3002<\/p>\n<p>Sample 8.31 Test using a callback for error injection<br \/>\nclass Driver_cbs_drop extends Driver_cbs;<br \/>\n virtual task pre_tx(ref Transaction tr, ref bit drop);<br \/>\n \u00a0 \u00a0 \/\/ Randomly drop 1 out of every 100 transactions<br \/>\n \u00a0 \u00a0 drop &#061; ($urandom_range(0,99) &#061;&#061; 0);<br \/>\n  endtask<br \/>\nendclass<br \/>\n\u200b<br \/>\nprogram automatic test;<br \/>\n  Environment env;<br \/>\n \u00a0<br \/>\n  initial begin<br \/>\n \u00a0 \u00a0 env &#061; new();<br \/>\n \u00a0 \u00a0 env.gen_cfg();<br \/>\n \u00a0 \u00a0 env.build();<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 begin \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Create error injection callback<br \/>\n \u00a0 \u00a0 \u00a0 Driver_cbs_drop dcd &#061; new();<br \/>\n \u00a0 \u00a0 \u00a0 env.drv.cbs.push_back(dcd); \/\/ Put into driver&#039;s Queue<br \/>\n \u00a0 \u00a0 end<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 env.run();<br \/>\n \u00a0 \u00a0 env.wrap_up();<br \/>\n  end<br \/>\nendprogram <\/p>\n<h3 id=\"scoreboard\">scoreboard<\/h3>\n<p>scoreboard\u7684\u8bbe\u8ba1\u53d6\u51b3\u4e8eDUT&#xff0c;\u6839\u636eDUT\u5904\u7406\u4e8b\u52a1\u7684\u4e0d\u540c&#xff0c;scoreboard\u9700\u8981\u53d6\u51fa\u4e0d\u540c\u7684\u503c\u8fdb\u884c\u5bf9\u6bd4\u3002<\/p>\n<p>\u4e0b\u9762\u5c31\u662f\u4e00\u4e2ascoreboard\u3002save_expect\u4fdd\u5b58\u671f\u671b\u4e8b\u52a1&#xff0c;\u8fd9\u662f\u6211\u4eec\u9884\u671fDUT\u53ef\u80fd\u4f1a\u8f93\u51fa\u7684\u5185\u5bb9&#xff1b;compare_actual\u5c06DUT\u8f93\u51fa\u548c\u671f\u671b\u4e8b\u52a1\u5bf9\u6bd4&#xff0c;\u7ed3\u679c\u53ef\u80fd\u662f0\/1\/n match&#xff0c;1 match\u662f\u7406\u60f3\u60c5\u51b5\u3002<\/p>\n<p>Sample 8.32 Simple scoreboard for atomic transactions<br \/>\nclass Scoreboard;<br \/>\n  Transaction scb[$];  \/\/ Store expected trans in queue<br \/>\n \u00a0<br \/>\n  function void save_expect(input Transaction tr);<br \/>\n    scb.push_back(tr);<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\n  function void compare_actual(input Transaction tr);<br \/>\n \u00a0 \u00a0 int q[$];<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 q &#061; scb.find_index(x) with (x.src &#061;&#061; tr.src);<br \/>\n \u00a0 \u00a0 case (q.size())<br \/>\n \u00a0 \u00a0 \u00a0 0: $display(&#034;No match found&#034;);<br \/>\n \u00a0 \u00a0 \u00a0 1: scb.delete(q[0]);<br \/>\n \u00a0 \u00a0 \u00a0 default:<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 $display(&#034;Error, multiple matches found!&#034;);<br \/>\n \u00a0 \u00a0 endcase<br \/>\n  endfunction : compare_actual<br \/>\nendclass : Scoreboard <\/p>\n<h3 id=\"%E7%94%A8callback%E8%BF%9E%E6%8E%A5scoreboard\">\u7528callback\u8fde\u63a5scoreboard<\/h3>\n<p>\u6269\u5c55\u7c7bDriver_cbs_scoreboard\u9996\u5148\u6784\u5efa\u4e86scoreboard&#xff0c;\u7136\u540e\u5199\u4e86\u7528\u4e8escoreboard\u7684callback&#xff0c;\u628a\u4f20\u5230DUT\u7684\u4e8b\u52a1\u653e\u5230scoreboard\u7684\u9884\u671f\u4e8b\u52a1\u5e8f\u5217\u4e2d\u3002<\/p>\n<p>\u8fd9\u91cc\u6ca1\u6709\u5c55\u73b0\u51famonitor\u7684\u4e00\u65b9<\/p>\n<p>Sample 8.33 Test using callback for scoreboard<br \/>\nclass Driver_cbs_scoreboard extends Driver_cbs;<br \/>\n  Scoreboard scb;<br \/>\n \u00a0<br \/>\n virtual task pre_tx(ref Transaction tr, ref bit drop);<br \/>\n \u00a0 \u00a0 \/\/ Put transaction in the scoreboard<br \/>\n \u00a0 \u00a0 scb.save_expected(tr);<br \/>\n  endtask<br \/>\n \u00a0<br \/>\n  function new(input Scoreboard scb);<br \/>\n    this.scb &#061; scb;<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\nendclass<br \/>\n\u200b<br \/>\nprogram automatic test;<br \/>\n  Environment env;<br \/>\n  initial begin<br \/>\n \u00a0 \u00a0 env &#061; new();<br \/>\n \u00a0 \u00a0 env.gen_cfg();<br \/>\n \u00a0 \u00a0 env.build();<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 begin \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Create scoreboard callback<br \/>\n \u00a0 \u00a0 \u00a0 Driver_cbs_scoreboard dcs &#061; new(env.scb);<br \/>\n \u00a0 \u00a0 \u00a0 env.drv.cbs.push_back(dcs); \/\/ Put into driver\u00d5s Q<br \/>\n \u00a0 \u00a0 end<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 env.run();<br \/>\n \u00a0 \u00a0 env.wrap_up();<br \/>\n  end<br \/>\nendprogram <\/p>\n<h3 id=\"%E7%94%A8callback%20debug%E4%BA%8B%E5%8A%A1%E5%A4%84%E7%90%86%E5%99%A8\">\u7528callback debug\u4e8b\u52a1\u5904\u7406\u5668<\/h3>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728callback\u4e2d\u52a0\u4ee3\u7801\u6765\u663e\u793a\u4e8b\u52a1&#xff0c;\u5982\u679c\u6709\u591a\u4e2a\u4e8b\u52a1&#xff0c;\u53ef\u4ee5\u6253\u5370\u51fa\u5c42\u6b21&#xff0c;\u8fd8\u53ef\u4ee5\u901a\u8fc7\u628a\u4ee3\u7801\u653e\u5728\u5176\u4ed6callback\u4e4b\u524d\/\u4e4b\u540e\u6765\u5b9a\u4f4d\u95ee\u9898\u5728\u54ea\u3002<\/p>\n<p>\u6ce8\u610f\u4e0d\u8981\u4fee\u6539\u5176\u4ed6\u73af\u5883\u3002<\/p>\n<h2 id=\"8.8%20%E5%8F%82%E6%95%B0%E5%8C%96%E7%9A%84%E7%B1%BB\">8.8 \u53c2\u6570\u5316\u7684\u7c7b<\/h2>\n<h3 id=\"stack\">stack<\/h3>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u6709push\u548cpop\u7684\u6808\u7ed3\u6784\u3002<\/p>\n<p>local\u6807\u8bb0\u6807\u5fd7\u8fd9\u4e2a\u6210\u5458\u53ea\u80fd\u7528\u672cclass\u5185\u90e8\u7684method\u8bbf\u95ee&#xff0c;\u5bf9\u5b83\u7684subclass\u548c\u5916\u90e8\u90fd\u4e0d\u53ef\u89c1\u3002<\/p>\n<p>Sample 8.34 Stack using the int type<br \/>\nclass IntStack;<br \/>\n  local int stack[100]; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Holds data values<br \/>\n  local int top;<br \/>\n \u00a0<br \/>\n  function void push(input int i); \/\/ Push value on top<br \/>\n     stack[&#043;&#043;top] &#061; i;<br \/>\n  endfunction : push<br \/>\n \u00a0<br \/>\n  function int pop(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Remove value from top<br \/>\n     return stack[top&#8211;];<br \/>\n  endfunction<br \/>\nendclass : IntStack <\/p>\n<p>\u5982\u679c\u4f60\u60f3\u5b9a\u4e49\u4e00\u4e2a\u80fd\u5b58\u50a8\u6307\u5b9a\u7c7b\u578b\u7684\u6808&#xff0c;\u8fd9\u5c31\u662f\u53c2\u6570\u5316\u7684\u7c7b<\/p>\n<p>Sample 8.35 Parameterized class for a stack<br \/>\nclass Stack #(type T&#061;int);<br \/>\n  local T stack[100]; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Holds data values<br \/>\n  local int top;<br \/>\n\u200b<br \/>\n  function void push(input T i); \/\/ Push new value on top<br \/>\n     stack[&#043;&#043;top] &#061; i;<br \/>\n  endfunction : push<br \/>\n \u00a0<br \/>\n  function T pop(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Remove value from top \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n     return stack[top&#8211;];<br \/>\n  endfunction<br \/>\nendclass : Stack <\/p>\n<p>\u4e0b\u9762\u7684generator\u4e5f\u662f\u4e00\u4e2a\u53c2\u6570\u5316\u7684\u7c7b<\/p>\n<p>Sample 8.37 Parameterized generator class using blueprint pattern<br \/>\nclass Generator #(type T&#061;BaseTr);<br \/>\n  mailbox gen2drv;<br \/>\n  T blueprint; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ Blueprint object<br \/>\n\u200b<br \/>\n  function new(input mailbox gen2drv);<br \/>\n \u00a0 \u00a0 this.gen2drv &#061; gen2drv;<br \/>\n \u00a0 \u00a0 blueprint &#061; new(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Create default<br \/>\n  endfunction<br \/>\n \u00a0<br \/>\n  task run();<br \/>\n \u00a0 \u00a0 T tr;<br \/>\n \u00a0 \u00a0 forever begin<br \/>\n \u00a0 \u00a0 \u00a0 assert(blueprint.randomize); \/\/ Randomize object<br \/>\n \u00a0 \u00a0 \u00a0 tr &#061; blueprint.copy(); \u00a0 \u00a0 \u00a0 \/\/ Make a copy<br \/>\n \u00a0 \u00a0 \u00a0 gen2drv.put(tr); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Send to driver<br \/>\n \u00a0 \u00a0 end<br \/>\n  endtask<br \/>\nendclass <\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u8c03\u7528\u4e0a\u9762\u7684generator\u53d1\u9001transaction class\u7684\u4e8b\u52a1<\/p>\n<p>Sample 8.38 Simple testbench using parameterized generator class<br \/>\nprogram automatic test;<br \/>\n  initial begin<br \/>\n \u00a0 \u00a0 Generator #(Transaction) gen;<br \/>\n \u00a0 \u00a0 mailbox gen2drv;<br \/>\n \u00a0 \u00a0 gen2drv &#061; new(1);<br \/>\n \u00a0 \u00a0 gen &#061; new(gen2drv);<br \/>\n \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 fork<br \/>\n \u00a0 \u00a0 \u00a0 gen.run();<br \/>\n \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 \u00a0 repeat (5) begin<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 Transaction tr;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 gen2drv.peek(tr);  \/\/ Get next transaction<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 tr.display();<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 gen2drv.get(tr); \u00a0 \/\/ Remove transaction<br \/>\n \u00a0 \u00a0 \u00a0 end<br \/>\n \u00a0 \u00a0 \u00a0<br \/>\n \u00a0 \u00a0 join_any<br \/>\n end<br \/>\nendprogram \/\/ tes <\/p>\n<h3 id=\"%E5%85%B3%E4%BA%8E%E5%8F%82%E6%95%B0%E5%8C%96%E7%B1%BB%E7%9A%84%E5%BB%BA%E8%AE%AE\">\u5173\u4e8e\u53c2\u6570\u5316\u7c7b\u7684\u5efa\u8bae<\/h3>\n<p>1.\u4ece\u975e\u53c2\u6570\u5316\u7684class\u5f00\u59cb\u8bbe\u8ba1&#xff0c;\u8fd9\u6837\u66f4\u7b80\u5355<\/p>\n<p>2.\u5b8f\u53ef\u4ee5\u66ff\u4ee3\u53c2\u6570\u5316&#xff0c;\u4f46\u662fdebug\u66f4\u9ebb\u70e6<\/p>\n<p>3.\u5982\u679c\u6709\u51e0\u4e2a\u4f7f\u7528\u76f8\u540c\u7c7b\u578b\u4e8b\u52a1\u7684class&#xff0c;\u53ef\u4ee5\u7528\u53c2\u6570\u5316&#xff0c;\u4e5f\u53ef\u4ee5\u7528\u5b8f<\/p>\n<p>4.virtual methods\u4f1a\u5e2e\u52a9\u4f60\u521b\u5efa\u53c2\u6570\u5316\u7684\u7c7b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55<br \/>\n8.1 \u7ee7\u627f<br \/>\n\u4e8b\u52a1\u57fa<br \/>\n\u6269\u5c55<br \/>\n\u66f4\u591aOOP\u672f\u8bed<br \/>\n\u6269\u5c55\u7c7b\u4e2d\u7684\u6784\u9020\u51fd\u6570<br \/>\ndriver class<br \/>\ngenerator class<br \/>\n8.2 \u84dd\u56fe&#xff08;blueprint&#xff09;\u6a21\u5f0f<br \/>\nenvironment class<br \/>\n\u4e00\u4e2a\u7b80\u5355\u7684testbench<br \/>\n\u7528\u6269\u5c55transaction class<br \/>\n\u7528\u6269\u5c55\u7c7b\u6539\u53d8\u968f\u673a\u7ea6\u675f<br \/>\n8.3 downcasting and virtual method<br \/>\n\u7528$cast downcasting<br \/>\nvirtual <\/p>\n","protected":false},"author":2,"featured_media":94639,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[87,371,100],"topic":[],"class_list":["post-94644","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-87","tag-371","tag-100"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SV\u5b66\u4e60\u8bb0\u5f55\uff08\u516b\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/94644.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SV\u5b66\u4e60\u8bb0\u5f55\uff08\u516b\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 8.1 \u7ee7\u627f \u4e8b\u52a1\u57fa \u6269\u5c55 \u66f4\u591aOOP\u672f\u8bed \u6269\u5c55\u7c7b\u4e2d\u7684\u6784\u9020\u51fd\u6570 driver class generator class 8.2 \u84dd\u56fe&#xff08;blueprint&#xff09;\u6a21\u5f0f environment class \u4e00\u4e2a\u7b80\u5355\u7684testbench \u7528\u6269\u5c55transaction class \u7528\u6269\u5c55\u7c7b\u6539\u53d8\u968f\u673a\u7ea6\u675f 8.3 downcasting and virtual method \u7528$cast downcasting virtual\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/94644.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-14T14:48:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260814144855-6a7f2ad785911.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94644.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/94644.html\",\"name\":\"SV\u5b66\u4e60\u8bb0\u5f55\uff08\u516b\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-14T14:48:57+00:00\",\"dateModified\":\"2026-08-14T14:48:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/94644.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/94644.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94644.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SV\u5b66\u4e60\u8bb0\u5f55\uff08\u516b\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SV\u5b66\u4e60\u8bb0\u5f55\uff08\u516b\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/94644.html","og_locale":"zh_CN","og_type":"article","og_title":"SV\u5b66\u4e60\u8bb0\u5f55\uff08\u516b\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 8.1 \u7ee7\u627f \u4e8b\u52a1\u57fa \u6269\u5c55 \u66f4\u591aOOP\u672f\u8bed \u6269\u5c55\u7c7b\u4e2d\u7684\u6784\u9020\u51fd\u6570 driver class generator class 8.2 \u84dd\u56fe&#xff08;blueprint&#xff09;\u6a21\u5f0f environment class \u4e00\u4e2a\u7b80\u5355\u7684testbench \u7528\u6269\u5c55transaction class \u7528\u6269\u5c55\u7c7b\u6539\u53d8\u968f\u673a\u7ea6\u675f 8.3 downcasting and virtual method \u7528$cast downcasting virtual","og_url":"https:\/\/www.wsisp.com\/helps\/94644.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-14T14:48:57+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260814144855-6a7f2ad785911.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"12 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/94644.html","url":"https:\/\/www.wsisp.com\/helps\/94644.html","name":"SV\u5b66\u4e60\u8bb0\u5f55\uff08\u516b\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-14T14:48:57+00:00","dateModified":"2026-08-14T14:48:57+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/94644.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/94644.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/94644.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"SV\u5b66\u4e60\u8bb0\u5f55\uff08\u516b\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/94644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=94644"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/94644\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/94639"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=94644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=94644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=94644"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=94644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}