首页 > 其他分享 >SystemVerilog -- 2.2 Data Types ~ Signed integers,byte

SystemVerilog -- 2.2 Data Types ~ Signed integers,byte

时间:2024-04-29 21:46:09浏览次数:16  
标签:integers -- variables Signed sign 0d var byte bits

SystemVerilog 'integer' and 'byte'

除了 verilog 支持的所有数据类型外,SystemVerilog 还具有许多其他2-state的数据类型。现代testbench中最常用的数据类型是bitintlogicbyte
integer

整数是没有小数部分的数字,换句话说,它们是整数。SystemVerilog有三种新的signed数据类型保存整数值,每种类型都有不同的大小。数据的范围是-32768到32767。可以使用关键字和显式定义符号。此外,他们也可以通过铸造相互转换。shortint longint signed unsigned

// ubyte is converted to signed type and assigned to si
si = signed' (ubyte);

Signed

默认情况下,整数变量本质上是有符号的,因此可以同时保存正值和负值。

module tb;
  // By default int data types are signed which means that MASB is the sign bit and the integer variables can also store negative numbers
  shortint  var_a;
  int       var_b;
  longint   var_c;

  initial begin
    // Print initial values of the integer variables
    $display ("Sizes var_a=%0d var_b=%0d var_c=%0d", $bits(var_a),  $bits(var_b),  $bits(var_c));

    // Assign the maximum value for each of the variables 
    // MSB of each variable represents the sign bit and is set to 0 
    // Reset of the bit positions are filled with 1 and hence you get the maximum value that these variables can hold
    #1 var_a = 'h7FFF;
       var_b = 'h7FFF_FFFF;
       var_c = 'h7FFF_FFFF_FFFF_FFFF;

    // When added a 1, the sign changes to negative because this is a signed varible
    #1 var_a += 1; // Value becomes 'h8000 => which is a rollover from + sign to - sign
       var_b += 1; // Value becomes 'h8000_0000 => which is a rollover from + sign to - sign
       var_c += 1;
  end

  // Start a monitor to print out values of each varibles as they change
  initial 
    $monitor (" var_a=%0d var_b=%0d var_c=%0d", var_a, var_b, var_c);
endmodule

系统任务返回变量中的位数。请注意,var_avar_bvar_c会滚动到消极的一面。$bits

Simulation Log

Sizes var_a=16 var_b=32 var_c=64
var_a=0 var_b=0 var_c=0
var_a=32767 var_b=2147483647 var_c=9223372036854775807
var_a=-32768 var_b=-2147483648 var_c=-9223372036854775808

Unsigned

我们将上面示例中声明的变量更改为unsigned类型,并查看结果。

module tb;
    // In this case, we are going to make it unsigneed which means that MSB no longer holds the sign information and hence these variables can only store positive values
    shortint  var_a;
    int       var_b;
    longint   var_c;

  initial begin
    // Print initial values of the integer variables
    $display ("Sizes var_a=%0d var_b=%0d var_c=%0d", $bits(var_a),  $bits(var_b),  $bits(var_c));

    // Assign the maximum value for each of the variables 
    // MSB of each variable represents the sign bit and is set to 0 
    // Reset of the bit positions are filled with 1 and hence you get the maximum value that these variables can hold
    #1 var_a = 'h7FFF;
       var_b = 'h7FFF_FFFF;
       var_c = 'h7FFF_FFFF_FFFF_FFFF;

    // When added a 1, value rolls over to 0
    #1 var_a += 1; // Value becomes 'h0
       var_b += 1; // Value becomes 'h0
       var_c += 1;
  end

  // Start a monitor to print out values of each varibles as they change
  initial 
    $monitor (" var_a=%0d var_b=%0d var_c=%0d", var_a, var_b, var_c);
endmodule

Simulation Log

Sizes var_a=16 var_b=32 var_c=64
var_a=0 var_b=0 var_c=0
var_a=65535 var_b=4294967295 var_c=18446744073709551615
var_a=0 var_b=0 var_c=0

byte

A 是整数的更短版本,大小为 8 位。默认情况下,byte是一个有符号变量,并且具有与上一节中描述的整数相同的属性。

module tb;
    byte             s_byte; // by default byte is signed
    byte  unsigned   u_byte; // Byte is set to unsigned

    initial begin
      $display ("Size s_byte=%0d, u_byte=%0d", $bits(s_byte), $bits(u_byte));

      // Assign the "assumed" maximum value to both variables
      #1 s_byte = 'h7F;
         u_byte = 'h7F;

      // Increment by 1, and see that a_byte rolled over to negative because byte is signed by default. u_byte keeps increasing because it is unsigned and can go upto 255
      #1 s_byte += 1;
         u_byte += 1;

      // Assign 255 (8'hFF) to u_byte -> this is the max value it can hold
      #1 u_byte = 'hFF;

      // Add 1 and see that it rolls over to 0
      #1 u_byte += 1;
    end



    initial begin
      $monitor (" [%0t ns] s_byte=%0d u_byte=%0d", $time, s_byte, u_byte);
    end
endmodule

Simulation Log

ncsim> run
Size s_byte=8, u_byte=8
[0 ns] s_byte=0 u_byte=0
[0 ns] s_byte=127 u_byte=127

[0 ns] s_byte=-128 u_byte=1128

[0 ns] s_byte=-128 u_byte=255
[0 ns] s_byte=-128 u_byte=0
ncsim: *W,RNQUIE: Simulation is complete.

标签:integers,--,variables,Signed,sign,0d,var,byte,bits
From: https://www.cnblogs.com/sys-123456/p/18133410

相关文章

  • 闲话 4.29:伯特兰定理及另一道题
    伯特兰公设任意\(\ge4\)的正整数\(n\)满足:存在一个质数\(p\in(n,2n)\)。以下\(p\)均取质数,\(p_i\)表示第\(i\)个质数。引理1:\[\prod_{p\len}p\le4^n,n>1\]首先有一个想法:\[\ln\prod_{p\len}p\le\pi(n)\lnn\simn\len\ln4\]这些放缩是相当松的,因为......
  • XYCTF pwn部分题解 (部分题目详解)
    hello_world(签到)思路:✅这道题就是利用printf函数泄露libc的基地址,然后再次进行栈溢出通过system,/bin/sh来获取shellwp:invisible_flag思路:✅题目提示orw,那我们先看看是否开了沙盒那么是开了沙盒的,试试orw读取flag虽然保护全开但是程序会执行我们写的shellcdoe那么就可......
  • C语言中四舍五入问题总结
    C语言中四舍五入问题的总结在C语言中大部分情况下都是不需要四舍五入的。除了一种情况:在使用输出函数printf()限制浮点型输出的小数位个数eg:printf("%0.2f",1.567);//输出的结果是1.57其他情况下都不需要四舍五入,比如强制转换在不同类型的混合运算中,编译器也会自动地转......
  • 二叉树笔试题解题思路
    数据结构二叉树笔试题:解题思路:1.判断是否为空树,若为空树,则返回0;2.定义两个指针备份根结点地址,定义两个整型变量a,b并初始化为0,记录左右子树的深度;先对根结点的左子树进行遍历,若根结点的左结点不为NULL,则a++,把根结点的左结点赋值为新的根结点,再进行上述操作,若根结点的左结点......
  • 树(tree) [一]
    树(tree)[一]基本概念:​ 日常生活中,很多数据的组织形式本质上是一棵树。比如一个公司中的职员层级关系、一个学校中的院系层级关系、淘汰赛中的各次比赛队伍、一个家族中的族谱成员关系等都是树状逻辑结构。由于树状结构表现出来都是具有层次的,因此也被称为层次结构。树是一种......
  • C++ 学习笔记
    ​1、基础概念C++是一种高性能的编程语言,由BjarneStroustrup在1980年代初设计,旨在为C语言添加面向对象的功能。自那时起,C++已发展成为一种支持过程性、面向对象和泛型编程的多范式语言,广泛应用于系统软件、游戏开发、驱动程序、嵌入式固件等领域。要开始使用C++,首先需要......
  • 获取给定区域内的符合颜色值的第一个和最后一个坐标
    fromPILimportImageGrabimportpyautoguiimporttimeimportpyperclipimportnumpydef获取给定区域内的符合颜色值的第一个和最后一个坐标(left_x:int,left_y:int,right_x:int,right_y:int,color_r:int,color_g:int,color_b:int)->list:'''注意,本函数直接截取......
  • 组件通信
    父子组件prop和eventstyle和class父组件可以向子组件传递style和class,它们会合并到子组件的根元素中attribute如果父组件传递了一些属性到子组件,但子组件并没有声明这些属性,则它们称之为attribute,这些属性会直接附着在子组件的根元素上不包括style和class,它们会被......
  • Unity游戏框架设计之协程管理器
    Unity游戏框架设计之协程管理器代码设计/**协程管理器*/publicclassCoroutineManager:SingletonMono<CoroutineManager>{/***创建CoroutineTask*/publicCoroutineTaskCreateCoroutine(IEnumeratorcoroutine,Action<bool>finishHandler......
  • Windows开源输入法 - RIME输入法
    前言#上一篇文章介绍了Windows下的包管理器,本文继续介绍输入法。事实上Windows的输入法生态比Linux/Mac丰富很多,不过很多国产输入法存在窃取隐私、植入广告、乱安装流氓软件等问题,现在有开源的RIME输入法可以选择,何必受这气呢......