首页 > 其他分享 >SystemVerilog -- 2.20 Data Types ~ SystemVerilog Structure

SystemVerilog -- 2.20 Data Types ~ SystemVerilog Structure

时间:2024-05-03 18:22:51浏览次数:16  
标签:SystemVerilog -- ctrl st fruit Structure expiry reg structure

SystemVerilog Structure

Structure可以包含不同数据类型的元素,这些元素可以作为一个整体引用,也可以通过其名称单独引用。这些元素具有相同数据类型的数组完全不同。

// Normal arrays -> a collection of variables of same data type
int array [10];          // all elements are of int type
bit [7:0] mem [256];     // all elements are of bit type

// Structures -> a collection of variables of different data types
struct {
  byte   val1;
  int    val2;
  string val3;
}; struct_name;

Syntax

struct {
  [list of variables]
} struct_name;

Unpacked Structures

默认情况下,结构是unpacked的,可以使用关键字进行定义,并且可以在大括号内提供成员声明列表,后跟structure的名称。struct

Structure Example

module tb;
  // Create a structure called "st_fruit"
  // which to store the fruit's name, count and expiry date in days.
  // Note : this structure declaration can also be placed outside the module
  struct {
    string fruit;
    int    count;
    byte   expiry;
  } st_fruit;

  initial begin
    // st_fruit is a structure variable, so let's initialize it
    st_fruit = '{"apple", 4, 15};
    
    // Display the structure variable
    $display ("st_fruit = %p", st_fruit);

    // Change fruit to pineapple, and expiry to 7
    st_fruit.fruit  = "pineapple";
    st_fruit.expiry = 7;
    $display ("st_fruit = %p", st_fruit);
  end
endmodule

模拟日志

ncsim> run
st_fruit = '{fruit:"apple", count:4, expiry:'hf}
st_fruit = '{fruit:"pineapple", count:4, expiry:'hf}
ncsim: *W,RNQUIE: Simulation is complete

What is the need to typedef a structure ?

在上面的示例中只创建了一个变量,但如果需要创建具有相同成分的多个structure变量,则最好通过创建structure的用户定义数据类型。然后st_fruit将成为一个数据类型,然后可用于创建该类型的变量。typedef

module tb;
  // Create a structure called "st_fruit"
  // which to store the fruit's name, count and expiry date in days
  // Note : this structure declaration can also be placed outside the module
  typedef struct {
    string fruit;
    int    count;
    byte   expiry;
  } st_fruit;

  initial begin
    // st_fruit is a data tyoe, so we need to declare a varible of this data type
    st_fruit fruit1 = '{"apple", 4, 15};
    st_fruit fruit2;

    // Display the structure variable
    $display ("fruit1 = %p fruit2 = %p", fruit1, fruit2);

    // Assign one structure variable to another and print 
    // Note that contents of this variable is copied into the other
    fruit2 = fruit1;
    $display ("fruit1 = %p fruit2 = %p", fruit1, fruit2);
    
    // Change fruit1 to see if fruit2 is affected
    fruit1.fruit = "orange";
    $display ("fruit1 = %p fruit2 = %p", fruit1, fruit2);
  end
endmodule

模拟日志

ncsim> run
fruit1 = '{fruit:"apple", count:4, expiry:'hf}   fruit2 = '{fruit:"", count:0, expiry:'h0}   
fruit1 = '{fruit:"apple", count:4, expiry:'hf}   fruit2 = '{fruit:"apple", count:4, expiry:'hf} 
fruit1 = '{fruit:"orange", count:4, expiry:'hf}   fruit2 = '{fruit:"apple", count:4, expiry:'hf} 
ncsim: *W,RNQUIE: Simulation is complete

Packed Structures

Packed Structures是一种将向量细分为字段的机制,这些字段可以作为成员访问,并在内存中打包在一起,没有间隙。Structure中的第一个成员是最重要的,随后的成员按重要性递减的顺序排列。

使用关键字声明Packed Structure,默认情况下该关键字是无符号的。packed

Example

// Create a "packed" structure data type which is similar to creating
// bit [7:0] ctrl_reg
// ctrl_reg [0]   represents en
// ctrl_reg [3:1] represents cfg
// ctrl_reg [7:4] represents mode
typedef struct packed {
  bit [3:0] mode;
  bit [2:0] cfg;
  bit       en;
} st_ctr1;

module tb;
  st_ctr1   ctrl_reg;

  initial begin
    // Initialize paacked structure variable
    ctrl_reg = '{4'ha, 3'h5, 1};

    // Change packed structure member to something else
    ctrl_reg.mode = 4'h3
    $display ("ctrl_reg = %p", ctrl_reg);

    // Assign a packed value to the structure variable
    ctrl_reg = 8'hfa;
    $display ("ctrl_reg = %p", ctrl_reg);
  end
endmodule

模拟日志

ncsim> run
ctrl_reg = '{mode:'ha, cfg:'h5, en:'h1}
ctrl_reg = '{mode:'h3, cfg:'h5, en:'h1}
ctrl_reg = '{mode:'hf, cfg:'h5, en:'h0}
ncsim: *W,RNQUIE: Simulation is complete

标签:SystemVerilog,--,ctrl,st,fruit,Structure,expiry,reg,structure
From: https://www.cnblogs.com/sys-123456/p/18171466

相关文章

  • ai网页详情页-测试-只差样式修改
    HTML代码<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>ImageUploadandD......
  • P4391 [BOI2009] Radio Transmission 无线传输
    原题链接题解KMP算法的应用。我们知道KMP算法中NEXT数组是当前位置除外的最大前后缀长度。直接抛出结论:ans=cnt-Next[n]证明过程code #include<bits/stdc++.h>usingnamespacestd;constintN=1e6+5;intNext[N];strings;voidNEXT(){intcnt=s.size();......
  • 计算机操作系统
    计算机操作系统是管理计算机硬件与软件资源的软件,它为用户和应用程序提供了一个接口,使计算机的各种资源得以协调、高效运行。合理分配和管理CPU、内存、磁盘等资源,控制进程的运行、调度和终止,组织和管理文件及文件系统,管理各类输入输出设备,为应用程序提供各种服务和支持。早期计......
  • 微机结构
    微机结构发展史,起初,计算机使用电子管,体积庞大且效率低下。后来,晶体管的出现带来了巨大变革,计算机变得更小、更快。集成电路的发明则使计算机实现了更高的集成度和性能更优。个人计算机的出现让计算机走入寻常百姓家,而多核技术的发展则进一步提升了计算机的处理能力和效率。在这个......
  • Spring 中 bean 的生命周期
    Spring中的bean指的是被IoC管理的对象,通常都是以DI的方式来使用,并不需要手动管理它们的生命周期。但是,有时候我们需要对特定的bean进行额外的初始化、销毁操作,此时就可以通过Spring中的拓展接口来实现。基本生命周期Spring中IoC的顶层接口是BeanFactory,默认实现......
  • exhentai服务器不稳定怎么解决
    exhentai服务器不稳定问题可以归因于以下几个原因及解决方案:网站流量过大:避开高峰时段访问,使用加速器或代理服务器。服务器硬件故障:联系技术支持,尝试不同的连接或浏览器。网络连接问题:检查连接稳定性,确保带宽充足,优先使用有线连接。浏览器缓存或cookie问题:清除缓存和cookie,尝试不......
  • P2024 [NOI2001] 食物链
    原题链接题解带权并查集的应用,普通的并查集只能表示结点间的一种关系(如同一集合中的都是朋友)。而带权并查集的结点权值表示该结点与根结点的关系。相对应,带权并查集的路径压缩也复杂了一点。code #include<bits/stdc++.h>usingnamespacestd;constintN=5e4+5;intn,k......
  • 5.3
    有人懂吗众部,随我行军!......
  • 解决创建SpringBoot工程加载较慢的问题
    设置ServerURL将https://start.spring.io改为https://start.aliyun.com如图所示:启动演示如图所示,启动成功......
  • CF941
    Alink其实,只要有第一次,那么下次随意找一个队列里有的数加\(k-1\)个进去,加上队列里那一个删掉\(k\)个,到最后一次肯定是剩\(k-1\)个。没有第一次,就是\(n\)。点击查看代码#include<bits/stdc++.h>usingnamespacestd;intt;intn,k;inta[105];intmp[105];voidqwq......