首页 > 其他分享 >SystemVerilog -- 3.10 SystemVerilog Functions

SystemVerilog -- 3.10 SystemVerilog Functions

时间:2024-05-06 21:56:59浏览次数:15  
标签:function return -- res sum value 3.10 int SystemVerilog

SystemVerilog Functions

SystemVerilog函数具有与Verilog中的function相同的特征。

Functions

a的主要用途是返回一个可在表达式中使用且不能消耗模拟时间的值。function

  • function不能具有时间控制语句,如@ # fork join wait
  • function无法启动task,因为允许task消耗模拟时间。

ANSI-C style declaration

module tb;
  // There are two ways to call the function:
  initial begin
    // 1. Call function and assign value to a variable, and then use variable
    int s = sum(3, 4);
    $display ("sum(3, 4) = %0d", s);

    // 2. Call function and directly use value returned
    $display ("sum(5, 9) = %0d", sum(5, 9));
    $display ("mul(3, 1) = %0d", mul(3, 1));
  end

  // This function returns value of type "byte", and accepts two arguments "x" and "y". A return variable of the same name as function is implicitly declared and hence "sum" can be directly assigned without having to declare a separate return variable
  function byte sum (int x, int y);
    sum = x + y;
  endfunction

  // Instead of assigning to "mul", the computed value can be returned using "return" keyword
  function byte mul (int x, y);
    return x * y;
  endfunction
endmodule

模拟日志

ncsim> run
sum(3,4) = 7
sum(5,9) = 14
mul(3,1) = 3
ncsim: *W,RNQUIE: Simulation is complete.

Using declarations and directions

尽管后来在Verilog中引入了ANSI-C样式的声明,但端口方向的旧式声明仍然有效。SystemVerilog function可以将参数声明为输入和输出端口,如以下示例所示。

module tb;
  initial begin
     int res, s;
     s = sum(5, 9);
     $display ("s = %0d", sum(5, 9));
     $display ("sum(5, 9) = %0d", sum(5, 9));
     $display ("mul(3, 1) = %0d", mul(3, 1, res));
     $display ("res = %0d", res);
  end

  // Function has an 8-bit return value and accepts two inputs and provides the result through its output port and return val
  function bit [7:0] sum;
    input int x, y;
    output sum;
    sum = x + y;
  endfunction

  // Same as above but ports are given inline
  function byte mul (input int x, y, output int res);
    res = x * y + 1;
    return x * y
  endfunction
endmodule

模拟日志

ncsim> run
s = 14
sum(5,9) = 14
mul(3,1) = 3
res = 4
ncsim: *W,RNQUIE: Simulation is complete.

How to pass arguments by value ?

传递值是将参数传递给子例程的默认机制。每个参数都复制到子例程区域中,并且在子例程区域中对此局部变量所做的任何更改在子例程之外都不可见。

module tb;
  initial begin
    int a, res;
    
    // 1. Let's pick a random value from 1 to 10 and assign to "a"
    a = $urandom_range(1, 10);
    // Function is called with "" which is the default mode
    res = fn(a);

    // Even if value of a is changed inside the function, it is not reflected here
    $display ("After calling fn: a=%0d res=%0d", a, res);
  end

  // This function accepts arguments in "pass by value" mode and hence copies whatever arguments it gets into this local variable called "a".
  function int fn (int a);
    // Any change to this local variable is not reflected in the main variable declared above within the initial block
    a = a + 5;

    // Return some computed value
    return a * 10;
  endfunction
endmodule

请注意,从下面显示的日志中,即使为function中定义的局部变量分配了不同的值,块内的a值也不会更改。initial

模拟日志

ncsim> run
Before calling fn: a=2 res=0
After calling fn: a=2 res=70
ncsim: *W,RNQUIE: Simulation is complete.

How to pass arguments by reference ?

通过引用传递的参数不会复制到子例程区域,而是将对原始参数的引用传递给子例程。参数声明前面是关键字。对子例程内变量所做的任何更改都将反映在子例程外的原始变量中。ref

// Use "ref" to make this function accept arguments by reference
// Also make the function automatic
function automatic int fn (ref int a);
  // Any change to this local variable will be reflected in the main variable declared within the initial block
  a = a + 5;
  // Return some computed value
  return a * 10;
endfunction
Its illegal to use argument passing by reference for subroutines with a lifetime of `static`

模拟日志

ncsim> run
Before calling fn: a=2 res=0
After calling fn: a=7 res=70
ncsim: *W,RNQUIE: Simulation is complete.

标签:function,return,--,res,sum,value,3.10,int,SystemVerilog
From: https://www.cnblogs.com/sys-123456/p/18173958

相关文章

  • Codeforces Round 941 (Div. 2) Div 1 cf941 A-D
     A感觉A比较复杂啊,相比较B简单明了。way1只要有相同的一个数,它的数目大于等于k,那么它就可以进行一次操作,接下来可以再摸k-1个数,那么对于无法凑成k个数的数字来说,无论现在它有多少个数(>=1),加上这k-1个数,都能凑成数目>=k。同时,这个操作可以无限循环下去。所以这道题的出题设......
  • C语言常见错误
    C语言常见错误1、链式比较intmain(){inta=3;if(0<a<3)printf("%d\n",a);return0;}​ 在C语言中,条件表达式if(0<a<3)的写法并不会按预期的方式工作。这是因为C语言不支持链式比较(即,不支持a<b<c这样的表达式)。这样的表达式......
  • 【设计模式】策略模式
    一、介绍策略模式是一种行为设计模式,它能让你定义一系列算法,并将每种算法分别放入独立的类中,以使算法的对象能够相互替换。这里列举两个例子来说明下策略模式的使用场景:(1)根据会员等级来计算折扣力度。不同等级拥有不同的折扣力度,这样就可以根据策略模式去灵活的计算,就算之后又......
  • 雷达效果
    相位阵雷达效果一个简单思路:在一个扇形范围内,用多个射线检测来每tick判断点击查看代码#pragmaonce#include"CoreMinimal.h"#include"GameFramework/Actor.h"#include"Components/SphereComponent.h"#include"MyActor.generated.h"UCLASS()classTEST_A......
  • 【初中英语提分神器】中考高频词汇大全002-B/C开头单词高频,轻松掌握,考试无忧!速来围观!
    PDF格式公众号回复关键字:ZKGCH002B开头单词高频连词1but然而Ilikeapples,butIdon'tlikebananas.我喜欢苹果,但我不喜欢香蕉。2because因为Ididn'tgotoschooltodaybecauseIwassick.我今天没去上学,因为我生病了3before在……之前;先于;以前;在……面......
  • 安装zk
    安装zk AddZooKeeperServer   添加好2个zk 这时候先别启动新增的两个zk   这里没有要重启zk   (停1个非leader的zk,保留2个旧zk)重启全部zk  while:;do echo-n "                 ";date;for......
  • 一些不错的地理题
    意义类要去找主体、找问题对你的意义对我的意义对它的意义另外要善于运用材料信息第一题第二题通常考图加上文字第三题通常考根据表格去提取信息......
  • 历史研究(洛谷AT_joisc2014_c 歴史の研究)
    历史研究(洛谷AT_joisc2014_c 歴史の研究)题目描述IOI国历史研究的第一人——JOI教授,最近获得了一份被认为是古代IOI国的住民写下的日记。JOI教授为了通过这份日记来研究古代IOI国的生活,开始着手调查日记中记载的事件。日记中记录了连续N天发生的事件,大约每天发生一件。......
  • Logback日志框架
    简介Logback是一个成熟和稳定的日志框架,是Log4j框架的继承者,具有更好的性能和更加丰富的配置选项。它可以与SpringBoot框架集成,实现日志输出到控制台或者文件。基本概念Logback由三个模块组成logback-core:提供了更低级别的功能,最基本的日志系统。logback-classic:提供了更高......
  • 学习笔记:生成函数I
    形式幂级数多项式与形式幂级数多项式:\(A(x)=\sum_{i=0}^na_ix^i\)。形式幂级数:\(A(x)=\sum_{i\ge0}a_ix^i\)。其中\(a_i\inK\),\(K\)是一个域,通常考虑\(K=\mathbb{R}\)或\(K=\mathbb{Z}_{p}\)。注意这里的\(x\)可以理解为独立于域\(K\)的一个符号。......