设计模式在芯片验证中的应用——模板方法

一、模板方法

模板方法(Template Method)设计模式是一种行为设计模式, 它在父类中定义了一个功能的框架, 允许子类在不修改结构的情况下重写功能的特定步骤。也就是模板方法定义了一组有序执行的操作,将一些步骤的实现留给子类,同时保持整体功能结构。该技术通常也用于为主要操作提供预处理和后处理的钩子(hook)。

UVM库用了很多模板方法,比如uvm_sequence里的pre_body()和post_body()就是body()方法的钩子,在分别允许用户在body()执行之前和之后做一些其它处理。

模板方法设计模式主要包括以下几个组件:

  • 抽象类 (Abstract­Class):会声明完成一个功能所需各个步骤的方法, 以及依次调用它们实际步骤。 功能步骤可以被声明为抽象类型, 也可以提供一些默认实现。另外也可以提供一些放在主要功能步骤之前或之后的可选步骤方法(钩子),这些方法为子类提供额外的功能扩展点。
  • 具体类 (Concrete­Class):可以重写所有步骤实现, 但不能重写模板方法自身执行各个步骤方法的顺序。

我们以UVM中的monitor来举个模板方法应用的例子,利用模板方法可以扩展monitor主要功能,而且不容易误破坏monitor主功能。在base monitor组件中定义了非virtual的collect_transactions()模板方法,并提供了空的pre_collect()和post_ collect ()钩子方法。在继承的子monitor中,通过实现pre_ collect ()和post_ collect ()的具体内容,来提供了特定项目需求的操作。然后使用UVM factory方法将子monitor的对象去替换base monitor的对象。

下图为模板方法设计模式在monitor中应用的UML类图。

二、参考代码

monitor的模板方法设计模式参考代码如下:

class base_monitor extends uvm_monitor;

    `uvm_component_utils (base_monitor)

    function new(string name = " base_monitor ", uvm_component parent=null);
        super.new(name, parent);
    endfunction : new

    task collect_transactions();
        pre_collect();
        collect();
        post_collect();
    endtask : collect_transactions

    virtual task pre_collect();
        `uvm_info("PRE_COLLECT", "EMPTY method", UVM_LOW)
    endtask : pre_collect

    task collect();
        `uvm_info("COLLECT", "collect begin", UVM_LOW)
        `uvm_info("COLLECT", "collect end", UVM_LOW)
    endtask : collect

    virtual task post_collect();
        `uvm_info("POST_COLLECT", "EMPTY method", UVM_LOW)
    endtask : post_collect

endclass : base_monitor

模板方法设计模式-具有空钩子的base monitor

class son_monitor extends base_monitor;

    `uvm_component_utils (son_monitor)

    function new(string name = " son_monitor ", uvm_component parent=null);
        super.new(name, parent);
    endfunction : new

    virtual task pre_collect();
        `uvm_info("PRE_COLLECT", "PRE: collect item", UVM_LOW)
    endtask : pre_collect

    virtual task post_collect();
        `uvm_info("POST_COLLECT", "POST: collect item", UVM_LOW)
    endtask : post_collect

endclass : son_monitor

模板方法设计模式-带有实现钩子的son monitor

模拟测试代码如下:

// Use UVM factory overrde in the uvm_env
set_type_override_by_type(base_monitor::get_type(), son_monitor::get_type(), 'b0);

输出仿真日志如下:

 | # UVM_INFO  @ 0.000ns: uvm_test_top.env.agent.mon_h [PRE_COLLECT] EMPTY method
 | # UVM_INFO  @ 0.000ns: uvm_test_top.env.agent.mon_h [COLLECT] collect begin
 | # UVM_INFO  @ 0.000ns: uvm_test_top.env.agent.mon_h [COLLECT] collect end
 | # UVM_INFO  @ 0.000ns: uvm_test_top.env.agent.mon_h [POST_COLLECT] EMPTY method

模板方法设计模式-带有空钩子的base monitor的输出结果

 | # UVM_INFO  @ 0.000ns: uvm_test_top.env.agent.mon_h [PRE_COLLECT] PRE: collect item
 | # UVM_INFO  @ 0.000ns: uvm_test_top.env.agent.mon_h [COLLECT] collect begin
 | # UVM_INFO  @ 0.000ns: uvm_test_top.env.agent.mon_h [COLLECT] collect end
 | # UVM_INFO  @ 0.000ns: uvm_test_top.env.agent.mon_h [POST_COLLECT] POST: collect item

模板方法设计模式-带有实现钩子的子monitor的输出结果

输出仿真文件显示了模板方法模式的效果。在前者中,消息由具有空pre_collect()和post_collect()钩子的base monitor生成。在后一种情况下,将使用子monitor的实例,并使用已实现的钩子,这些钩子可用于特定于项目的处理,且无需修改base monitor的代码。子monitor主要的collect()方法继承自base monitor,因此两种情况下保持一致。模板方法collect_transactions()确保钩子会在主函数collect()之前和之后合适的地方被调用了。

模板方法的通常使用方式就是定义一个基本的抽象类,并且指定哪些抽象方法需要再子类中实现。模板方法的主要缺点就是,如果父类和子类都实现了复杂的功能,调试起来将非常麻烦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷公子的藏经阁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值