VHDL记录

news2024/9/24 15:24:53

文章目录

  • 使用function名称作为“常量”
  • numeric_std包集中使用乘法的注意项
  • variable的使用
  • 对于entity设置属性的方法
  • 在entity声明中嵌入function的定义
  • VHDL仿真
    • 读写文件
      • File declaration/File handing
      • File reading
      • File writing
      • 小例子
    • 使用函数
  • 模块中打印出调试信息

使用function名称作为“常量”

测试代码如下,function仅有名称,没有输入,在function内部使用全局constant进行参数处理,后续代码中可以直接使用function名称作为常量
在这里插入图片描述

numeric_std包集中使用乘法的注意项

近日看到一个VHDL Coding Style中提示说,使用numeric_std包集时,不要直接将unsigned/signed数据与natural/integer类型的数据相乘。

今天看了一下numeric_std的源码发现,如果直接直接将无符号数/有符号数与整数相乘的话,乘积很有可能会溢出。主要原因是由于,包集在实现整数与符号数相乘的时候,是先将整数转成了无符号数/有符号数,之后再进行的乘法运算,而整数转换后的位宽是与输入的符号数的位宽相一致的,这就可能导致在整数进行类型转换的过程中,出现数据溢出的情况。

这其实算不上bug,因为源码中对此进行了明确说明,主要是在使用的时候需要注意规避这一点,不要让符号数与整数直接相乘,可以手动进行位宽转换后再做运算。相应的源码如下图所示,
在这里插入图片描述

在这里插入图片描述

variable的使用

在网上流传的介绍VHDL Coding Style的文章中,一般是不建议在可综合的代码中使用变量的,这大概是由于variable具有局部作用域、赋值立即生效、仿真工具支持的不够等几个原因。但最近看到几段VHDL代码,大量使用variable,且使用方式非常规范。在一个prcoess中进行组合逻辑的设计,在另一个prcoess中使用寄存器进行时序逻辑的设计。在使用变量的process中,一般是如下的处理流程,

  • Latch the current value
  • 各类组合逻辑
  • Combinatorial outputs before the reset
  • Reset
  • Register the variable for next clock cycle
  • Registered Outputs
comb : process (localMac, r, rst, rxMaster, txSlave) is
  variable v : RegType;
  variable i : natural;
begin
  -- Latch the current value
  v := r;

  -- Reset the flags
  v.rxSlave := AXI_STREAM_SLAVE_INIT_C;
  if txSlave.tReady = '1' then
     v.txMaster.tValid := '0';
     v.txMaster.tLast  := '0';
     v.txMaster.tUser  := (others => '0');
     v.txMaster.tKeep  := (others => '1');
  end if;

  -- State Machine
  case r.state is
    -- 
  end case;
  
  -- Combinatorial outputs before the reset
  rxSlave <= v.rxSlave;

  -- Reset
  if (rst = '1') then
     v := REG_INIT_C;
  end if;

  -- Register the variable for next clock cycle
  rin <= v;

  -- Registered Outputs 
  txMaster <= r.txMaster;

end process comb;

seq : process (clk) is
begin
  if rising_edge(clk) then
     r <= rin after TPD_G;
  end if;
end process seq;

free_range_vhdl.pdf
11.4 Signals vs. Variables

在这里插入图片描述

对于entity设置属性的方法

在这里插入图片描述
类似的,对于verilog的module设置attribute的方法如下,
在这里插入图片描述

在entity声明中嵌入function的定义

在这里插入图片描述

VHDL仿真

有时写vhdl时,使用了record、array等数据类型,此时使用verilog进行仿真时略有不便,用vhdl仿真倒是方便不少。

读写文件

File declaration/File handing

在这里插入图片描述

File reading

在这里插入图片描述

File writing

在这里插入图片描述

小例子

在这里插入图片描述

在这里插入图片描述

使用函数

library ieee;
    use ieee.std_logic_1164.all;
    use ieee.numeric_std.all;

entity test is
end test;

architecture Behavioral of test is

    -- convert a hex string to a std_logic_vector
    function hex_string_to_std_logic_vector(inp: string; width : integer)
        return std_logic_vector is

        constant strlen       : integer := inp'LENGTH;
        variable result       : std_logic_vector(width-1 downto 0);
        variable bitval       : std_logic_vector((strlen*4)-1 downto 0);
        variable posn         : integer;
        variable ch           : character;
        variable vec          : string(1 to strlen);
    begin
        vec := inp;

        -- default value is zero
        result := (others => '0');
        posn := (strlen*4)-1;

        for i in 1 to strlen loop
            ch := vec(i);
            case ch is
                when '0' => bitval(posn downto posn-3) := "0000";
                when '1' => bitval(posn downto posn-3) := "0001";
                when '2' => bitval(posn downto posn-3) := "0010";
                when '3' => bitval(posn downto posn-3) := "0011";
                when '4' => bitval(posn downto posn-3) := "0100";
                when '5' => bitval(posn downto posn-3) := "0101";
                when '6' => bitval(posn downto posn-3) := "0110";
                when '7' => bitval(posn downto posn-3) := "0111";
                when '8' => bitval(posn downto posn-3) := "1000";
                when '9' => bitval(posn downto posn-3) := "1001";
                when 'A' | 'a' => bitval(posn downto posn-3) := "1010";
                when 'B' | 'b' => bitval(posn downto posn-3) := "1011";
                when 'C' | 'c' => bitval(posn downto posn-3) := "1100";
                when 'D' | 'd' => bitval(posn downto posn-3) := "1101";
                when 'E' | 'e' => bitval(posn downto posn-3) := "1110";
                when 'F' | 'f' => bitval(posn downto posn-3) := "1111";
                when others => bitval(posn downto posn-3) := "XXXX";
                               -- synthesis translate_off
                               ASSERT false
                                   REPORT "Invalid hex value" SEVERITY ERROR;
                               -- synthesis translate_on
            end case;
            posn := posn - 4;
        end loop;

        if (width <= strlen*4) then
            -- bitval larger than desired width
            result :=  bitval(width-1 downto 0);
        else
            -- bitval smaller than desired width
            -- MSB is padded with zeros since default value for result is all 0s
            result((strlen*4)-1 downto 0) := bitval;
        end if;
        return result;
    end;
    
    -- convert a binary string into a std_logic_vector (e.g., 0b10.1 = 101)
    function bin_string_to_std_logic_vector (inp : string)
        return std_logic_vector
    is
        variable pos : integer;
        variable vec : string(1 to inp'length);
        variable result : std_logic_vector(inp'length-1 downto 0);
    begin
        vec := inp;
        pos := inp'length-1;
        -- Set default value
        result := (others => '0');

        for i in 1 to vec'length loop
            -- synthesis translate_off
            if (pos < 0) and (vec(i) = '0' or vec(i) = '1' or vec(i) = 'X' or vec(i) = 'U')  then
                assert false
                    report "Input string is larger than output std_logic_vector. Truncating output.";
                return result;
            end if;
            -- synthesis translate_on

            if vec(i) = '0' then
                result(pos) := '0';
                pos := pos - 1;
            end if;
            if vec(i) = '1' then
                result(pos) := '1';
                pos := pos - 1;
            end if;
            -- synthesis translate_off
            if (vec(i) = 'X' or vec(i) = 'U') then
                result(pos) := 'U';
                pos := pos - 1;
            end if;
            -- synthesis translate_on
        end loop;
        return result;
    end;
    
    signal data_1  : std_logic_vector(5  downto 0);
    signal data_2  : std_logic_vector(7  downto 0);
     
begin
    
    data_1 <= bin_string_to_std_logic_vector("101001");
    data_2 <= hex_string_to_std_logic_vector("45",8);

end Behavioral;

模块中打印出调试信息

VHDL的仿真不如verilog方便,因此一般我都是用verilog对整个模块做仿真。但有的时候,想在某个可综合的VHDL模块内部嵌入仿真调试信息,这样在仿真时可以更直观的观测到运行结果。这在verilog中可以直接使用display和monitor函数,在VHDL中则需要借用report函数或者自己编写函数实现。

library ieee;
    use ieee.std_logic_1164.all;
    use ieee.numeric_std.all;
library work;
--    use work.stdio_h.all;

entity counter is
generic(
    SIM_VERBOSE : natural := 1
); 
port ( 
    clk : in  std_logic;
    cnt : out std_logic_vector(15 downto 0)
);
end counter;

architecture Behavioral of counter is
    
    signal cnt_i : unsigned(15 downto 0) := (others=>'0');
    
begin
    
    cnt <= std_logic_vector(cnt_i);
    process(clk)
    begin
        if rising_edge(clk) then
            cnt_i <= cnt_i + 1;
            
--            if(SIM_VERBOSE=1) then
--                if(cnt_i = 20) then
--                    printf("The time is %d ns\n",now);
--                    printf("The cnt_i value is %u\n",std_logic_vector(cnt_i)); 
--                    printf("-------------------------\n");
--                end if;
--            end if;
        end if;
    end process;
    
    -- synthesis translate_off
    gen_sim_info : if SIM_VERBOSE=1 generate
    begin
        process(cnt_i)
            use work.stdio_h.all;
        begin
            if(cnt_i = 20) then
                printf("The time is %d ns\n",now);
                printf("The cnt_i value is %u\n",std_logic_vector(cnt_i)); 
                printf("-------------------------------------------------------\n");
                printf("The cnt_i value is %s,the cnt_i part value is %u \n",std_logic_vector(cnt_i),std_logic_vector(cnt_i(3 downto 0)));
                printf("-------------------------------------------------------\n");
            end if;
        end process;
    end generate;
    -- synthesis translate_on

end Behavioral;

Modelsim运行结果如下所示,
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/890390.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

(搜索) 剑指 Offer 12. 矩阵中的路径 ——【Leetcode每日一题】

❓剑指 Offer 12. 矩阵中的路径 难度&#xff1a;中等 给定一个 m * n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 单词必须按照字母顺序&#xff0c;通过相邻的单元格内的字母构…

.netcore grpc客户端工厂及依赖注入使用

一、客户端工厂概述 gRPC 与 HttpClientFactory 的集成提供了一种创建 gRPC 客户端的集中方式。可以通过依赖包Grpc.Net.ClientFactory中的AddGrpcClient进行gRPC客户端依赖注入AddGrpcClient函数提供了许多配置项用于处理一些其他事项&#xff1b;例如AOP、重试策略等 二、案…

CS5523规格书|MIPI转EDP方案设计|替代LT8911芯片电路原理|ASL集睿致远CS替代龙讯

ASL芯片&#xff08;集睿致远&#xff09; CS5523是一款MIPI DSI输入&#xff0c;DP/e DP输出转换芯片&#xff0c;可pin to pin替代LT8911龙讯芯片。 MIPI DSI 最多支持 4 个通道&#xff0c;每个通道的最大运行速度为 1.5Gps。对于DP 1.2输出&#xff0c;它支持1.62Gbps和2.…

一文揭露AI聊天机器人到底是怎么实现自助应答的

现在很多的企业都会使用客服系统&#xff0c;主要是想通过它们来解决企业的一些问题和需求。所有就衍生了——AI聊天机器人这个新工具&#xff0c;它是把AI人工智能运用到客户服务当中&#xff0c;让AI来帮助我们完成一些解答客户问题的操作。下面我们就来说一下AI聊天机器人是…

创建和使用分区

创建和使用分区 创建一个名为 /home/curtis/ansible/partition.yml 的 playbook&#xff1a; 该palybook包含一个paly&#xff0c;该paly在balancers主机组的主机上运行&#xff1a; 在设备vdb上创建单个主分区&#xff0c;编号为1&#xff0c;大小为1500MiB 使用ext4文件系统…

数据建模和设计for CDGP第五章——如何绘制鸭掌图

CDGP中第五章必考一个数据模型设计题&#xff0c;分值10分。 主要考点 1、围绕一个场景如&#xff08;外卖送餐、图书馆管理系统等&#xff09;进行关系型逻辑数据模型设计2、要求满足范式化&#xff08;通常为3NF&#xff09;3、突出重点的实体并描述实体间的关系 加gzh“大数…

Linux下gdb调试

1.基本命令操作 2.调试方式启动运行无参程序 以下是linux下GDB调试的一个实例&#xff0c;先给出一个示例用的小程序&#xff0c;C语言代码&#xff1a; main.c #include <stdio.h>void Print(int i){printf("hello,程序猿编码 %d\n", i); }int main(int argc…

1281. 整数的各位积和之差

诸神缄默不语-个人CSDN博文目录 力扣刷题笔记 文章目录 1. 简单粗暴的遍历2. 其实也是遍历&#xff0c;但是用Python内置函数只用写一行 1. 简单粗暴的遍历 Python版&#xff1a; class Solution:def subtractProductAndSum(self, n: int) -> int:he0ji1while n>1:last…

杭电比赛总结

我们的队伍&#xff1a;team013 另外两队&#xff1a;team014、team015 ​ 今天是我第一次打杭电&#xff0c;发现杭电多数都是猜结论题 先给一下我们的提交数据 Submit TimeProblem IDTimeMemoryJudge Status4:59:59101115 MS1692 KWrong Answer4:59:55101115 MS1684 KWrong…

互联网发展历程:畅通无阻,流控的智慧应对

随着互联网的蓬勃发展&#xff0c;网络使用量不断增加&#xff0c;可能导致网络拥挤问题。在这个数字化时代&#xff0c;人们需要寻找一种方法来优化网络使用&#xff0c;确保数据的流畅传输。而在这一背景下&#xff0c;流量控制&#xff08;流控&#xff09;应运而生。 网络拥…

如何使用SpringBoot 自定义转换器

&#x1f600;前言 本篇博文是关于SpringBoot 自定义转换器的使用&#xff0c;希望你能够喜欢&#x1f60a; &#x1f3e0;个人主页&#xff1a;晨犀主页 &#x1f9d1;个人简介&#xff1a;大家好&#xff0c;我是晨犀&#xff0c;希望我的文章可以帮助到大家&#xff0c;您的…

Android Studio实现解析HTML获取图片URL,将URL存到list,进行瀑布流展示

目录 效果展示build.gradle(app)添加的依赖(用不上的可以不加)AndroidManifest.xml错误代码activity_main.xmlitem_image.xmlMainActivityImage适配器ImageModel 接收图片URL效果展示 build.gradle(app)添加的依赖(用不上的可以不加) dependencies {implementation co…

TRT8系列—— 版本差异注意事项

TRT8 一个大版本&#xff0c;8.4-、 8.5、 8.6&#xff08;包含预览功能&#xff09;却有很多变动&#xff0c;一不注意就发现很混乱&#xff0c;特备注此贴。建议具体case可以参考这个合集&#xff0c;真心安利&#xff1a;https://github.com/NVIDIA/trt-samples-for-hackath…

VirtualBox移动虚拟机存储位置,给C盘瘦身

不知不觉&#xff0c;五月份买的电脑的C盘突然要爆了。 我也搞不懂我的软件明明都放在D盘&#xff0c;C盘还是那么满。直到我看到了之前VirtualBox的一些东西。好家伙&#xff0c;直接干了我快30G了。 所以&#xff0c;我在想能不能将这些东西移到D盘&#xff0c;同时也不影响我…

android framework-Pixel3真机系统内置第三方apk实战

一、在/packages/apps创建独一无二的文件夹TestCamera 二、拷贝第三方应用到TestCamera文件夹下 三、创建Android.mk LOCAL_PATH: $(call my-dir)include $(CLEAR_VARS) # Module name should match apk name to be installed LOCAL_MODULE : TestCamera LOCAL_MODULE_TAGS : o…

scrollIntoView 导致整个页面上移问题

更改参数 block: ‘start’, behavior: smooth’更改为 behavior: ‘smooth’, block: ‘nearest’, inline: ‘start’ 在这里插入图片描述

JVM---jvm里的内存溢出

目录 堆溢出 虚拟机栈和本地方法栈溢出&#xff08;栈溢出很少出现&#xff09; 方法区和运行时常量池溢出 本机内存直接溢出&#xff08;实际中很少出现、了解即可&#xff09; 堆溢出 堆溢出&#xff1a;最常见的是大list&#xff0c;list里面有很多元素 堆溢出该怎么解决…

棋牌的逆向工程

前言 之前有些朋友都找我做棋牌的逆向工程 一来就说什么透视啥的 我只能让你早点洗洗睡 不过话说回来 棋牌逆向的思路也是有很多的 例如撞库 溢出 透视吧也能实现不过难度可想而知 透视有些棋牌是可以 不过只能看手里自己的牌例如炸金花 没开牌的时候内存中可能会存放当前的牌…

第四章nginx组件精讲

nginx配件location匹配的规则和优先级&#xff08;重点面试题&#xff09; RUI&#xff1a;统一资源标识符&#xff0c;是一种字符串标识&#xff0c;用于标识抽象的或者物理资源&#xff08;文件&#xff0c;图片&#xff0c;视频&#xff09; nginx当中&#xff1a;uri ww…