首页 > 其他分享 >SystemVerilog -- 2.6 Data Types ~ SystemVerilog Dynamic Arrays

SystemVerilog -- 2.6 Data Types ~ SystemVerilog Dynamic Arrays

时间:2024-05-02 20:11:31浏览次数:12  
标签:-- dynamic Dynamic fruits new array id SystemVerilog size

SystemVerilog Dynamic Arrays

Dynamic Arrays是一个unpacked Arrays,其大小可以在运行时设置或更改。因此与静态数组完全不同,静态数组的大小是在数组声明期间预先确定的。Dynamic Arrays的默认大小为零,直到由构造函数设置。new()

Syntax

Dynamic Array的尺寸由空方括号指定。[]

[data_type] [identifier_name] [];

bit [7:0] stack [];    // A dynamic array of 8-bit vector
string    names [];    // A dynamic array that can contain strings

该函数用于数组分配大小,并在需要时初始化其元素。new()

Dynamic Array Example

module tb;
  // Create a dynamic array that can hold elements of type int
  int array [];

  initial begin
    // Create a size for the dynamic array -> size here is 5
    // so that it can hold 5 values
    array = new [5];

    // Initialize the array with five values
    array = '{31, 67, 10, 4, 99};

    // Loop through the array and print their values
    foreach (array[i])
      $display ("array[%0d] = %0d", array[i]);
  end
enmodule

模拟日志

ncsim> run
array[0] = 31
array[1] = 67
array[2] = 10
array[3] = 4
array[4] = 99
ncsim: *W,RNQUIE: Simulation is complete.

Dynamic Array Mathods

Function Description
function int size (); Returns the current size of the array, 0 if array has not been created
function void delete (); Empties the array resulting in a zero-sized array
module tb;
  // Create a dynamic array that can hold elements of type string
  string fruits [];

  initial begin
    // Create a size for the dynamic array -> size here is 5
    // so that it can hold 5 values
    fruits = new [3];

    // Initialize the array with five values
    fruits = '{"apple", "orange", "mango"};

    // Print size of the dynamic array
    $display ("fruits.size() = %0d", fruits.size());

    // Empty the dynamic array by deleting all items
    fruits.delete();
    $display ("fruits.size() = %0d", fruits.size());
  end
endmodule

模拟日志

ncsim> run
fruits.size() = 3
fruits.size() = 0
ncsim: *W,RNQUIE: Simulation is complete.

How to add new items to a dynamic array ?

很多时候,我们可能需要在不丢失其原始内容的情况下向现有的动态数组添加新元素。由于运算符用于为数组分配特定大小,因此我们还必须在创建后将旧数组内容复制到新数组中。new()

int array [];
array = new [10];

// This creates one more slot in the array, while keeping old contents
array = new [array.size()+1] (array);

Copying dynamic array example

module tb;
  // Create two dynamic arrays of type int
  int array [];
  int id    [];

  initial begin
    // Allocate 5 memory locations to "array" and initialize with values
    array = new [5];
    array = '{1, 2, 3, 4, 5};

    // Point "id" to "array"
    id = array;

    // Display contentts of "id"
    $display ("id = %p", id);

    // Grow size by 1 and copy sxisting elements to the new dyn.Array "id" 
    id = new [id.size()+1] (id);

    // Assign value 6 to the newly added location [index 5]
    id [id.size()-1] = 6;
    // Display contentts of new "id"
    $display ("New id = %p", id);
    // Display size of both arrays
    $display ("array.size() = %0d, id.size() = %0d", array.size(), id.size());
  end
endmodule

模拟日志

run -all
ncsim> run
id = ' {1, 2, 3, 4, 5}
New id = ' {1, 2, 3, 4, 5, 6}
array.size() = 5
, id.size() = 6
ncsim: *W,RNQUIE: Simulation is complete.

标签:--,dynamic,Dynamic,fruits,new,array,id,SystemVerilog,size
From: https://www.cnblogs.com/sys-123456/p/18170425

相关文章

  • 将社会脆弱性纳入高分辨率全球洪水风险绘图
    将社会脆弱性纳入高分辨率全球洪水风险绘图贡献将高分辨率流洪水模型的年平均超标概率估计值与网格化人口和贫困数据相结合,创建了90米分辨率的全球洪水脆弱性调整风险指数(VARIFlood)。该指数提供了国家内部或国家之间相对风险的估计值,并通过识别以高密度和高社会脆弱性为特征......
  • 基于有限体积法和交错网格的SIMPLE算法推导及实现
    基于有限体积法和交错网格的SIMPLE算法推导及实现SIMPLE算法,半隐式速度压力耦合算法,是专门求解不可压流体流动的算法。由于不可压流体控制方程中,密度常常被视为常数,没有表征流体密度、压力、温度联系的状态方程,压力以梯度项的形式存在于动量方程中,无法显性表达或者直接求解,造成了......
  • simpread-课程 27:系统日志之 ILogger 与 Log4Net
    一、ILogger介绍1.1简介ILogger是.NET框架提供的一个接口,用于统一不同日志库的调用方式。ILogger本身并不提供具体的日志记录功能,而是通过实现它的类来执行这些操作。所以我们可以借助第三方日志库或自定义实现ILoggerProvider,将日志消息写入到文件、控制台或数据库中,也......
  • 微机结构
    微机结构微机,即微型计算机,是我们日常生活和工作中不可或缺的工具。它由硬件和软件两大部分组成。硬件部分:中央处理器(CPU):微机的“大脑”,负责执行指令和处理数据。内存(RAM):临时存储正在运行的程序和数据。存储设备:如硬盘、固态硬盘,用于长期保存数据和程序。输入输出设备:如键盘......
  • 高一下三模
    前传(想题ing)某人:热搜有一个“20岁小伙……”我:(?,看看热搜)(看热搜ing)Huge:你公自来机房就是干这个的?(以下省略N字)Huge:你下次(whk)考试要是没有进步公自就不要来机房了(?)正文语文——105pts并不是很中规中矩的分数但是没啥数学——112pts相当有说道一直觉得立体几何......
  • acwing351
    https://www.acwing.com/activity/content/problem/content/9051/NOIP2007提高组T4。本题是加强版。题目描述设\(T=(V,E,W)\)是一个无圈且连通的无向图(也称为无根树),每条边带有正整数的权,我们称\(T\)为树网(treenetwork),其中\(V,E\)分别表示结点与边的集合,\(W\)表示各边......
  • mysql 事务日志
    事务日志简介事务有四种特性:原子性、一致性、隔离性、持久性,详情请看《mysql事务的基础知识》。其中隔离性由锁机制实现,原子性、一致性由undo日志(undolog称为回滚日志,回滚记录到某个特定版本)来保证,持久性则是由redo日志(redolog称为重做日志,提供写操作,恢复提交事务修......
  • day30-JavaScript(2)
    1、BOM对象BOM:Broswerobjectmodel,即浏览器提供我们开发者在javascript用于操作浏览器的对象。1.1、window对象窗口方法//BOMBrowserobjectmodel浏览器对象模型//js中最大的一个对象.整个浏览器窗口出现的所有东西都是window对象的内容.console.log(window);......
  • [学习笔记] 质数与唯一分解定理 - 数论
    素性测试素性测试就是判断某个数是否为质数。费马小定理内容:若\(p\)为质数,\(a\)为任意整数,有\(a^{p-1}\equiv1(mod\p)\)那么可以多次随机取一个基数\(a\in(1,p)\)若\(p\)满足上式,那么它为质数的可能性就越大。MillarRabin素性测试inlinellqpow(lla,lln,ll......
  • 微机结构
    微型计算机的基本结构由控制器,运算器,存储器,输入设备,输出设备构成。微型计算机,是指由微处理器作为CPU的计算机。由大规模集成电路组成的、体积较小的电子计算机。由微处理机(核心)、存储片、输入和输出片、系统总线等组成。特点是体积小、灵活性大、价格便宜、使用方便。这类计算机......