VHDL記錄

文章目錄

  • 使用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) isvariable v : RegType;variable i : natural;
begin-- Latch the current valuev := r;-- Reset the flagsv.rxSlave := AXI_STREAM_SLAVE_INIT_C;if txSlave.tReady = '1' thenv.txMaster.tValid := '0';v.txMaster.tLast  := '0';v.txMaster.tUser  := (others => '0');v.txMaster.tKeep  := (others => '1');end if;-- State Machinecase r.state is-- end case;-- Combinatorial outputs before the resetrxSlave <= v.rxSlave;-- Resetif (rst = '1') thenv := REG_INIT_C;end if;-- Register the variable for next clock cyclerin <= v;-- Registered Outputs txMaster <= r.txMaster;end process comb;seq : process (clk) is
beginif rising_edge(clk) thenr <= 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_vectorfunction hex_string_to_std_logic_vector(inp: string; width : integer)return std_logic_vector isconstant 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);beginvec := inp;-- default value is zeroresult := (others => '0');posn := (strlen*4)-1;for i in 1 to strlen loopch := vec(i);case ch iswhen '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_offASSERT falseREPORT "Invalid hex value" SEVERITY ERROR;-- synthesis translate_onend case;posn := posn - 4;end loop;if (width <= strlen*4) then-- bitval larger than desired widthresult :=  bitval(width-1 downto 0);else-- bitval smaller than desired width-- MSB is padded with zeros since default value for result is all 0sresult((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_vectorisvariable pos : integer;variable vec : string(1 to inp'length);variable result : std_logic_vector(inp'length-1 downto 0);beginvec := inp;pos := inp'length-1;-- Set default valueresult := (others => '0');for i in 1 to vec'length loop-- synthesis translate_offif (pos < 0) and (vec(i) = '0' or vec(i) = '1' or vec(i) = 'X' or vec(i) = 'U')  thenassert falsereport "Input string is larger than output std_logic_vector. Truncating output.";return result;end if;-- synthesis translate_onif vec(i) = '0' thenresult(pos) := '0';pos := pos - 1;end if;if vec(i) = '1' thenresult(pos) := '1';pos := pos - 1;end if;-- synthesis translate_offif (vec(i) = 'X' or vec(i) = 'U') thenresult(pos) := 'U';pos := pos - 1;end if;-- synthesis translate_onend loop;return result;end;signal data_1  : std_logic_vector(5  downto 0);signal data_2  : std_logic_vector(7  downto 0);begindata_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 issignal cnt_i : unsigned(15 downto 0) := (others=>'0');begincnt <= std_logic_vector(cnt_i);process(clk)beginif rising_edge(clk) thencnt_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_offgen_sim_info : if SIM_VERBOSE=1 generatebeginprocess(cnt_i)use work.stdio_h.all;beginif(cnt_i = 20) thenprintf("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_onend Behavioral;

Modelsim運行結果如下所示,
在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/40993.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/40993.shtml
英文地址,請注明出處:http://en.pswp.cn/news/40993.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

RTC實驗

一、RTC簡介 RTC(Real Time Clock)即實時時鐘&#xff0c;它是一個可以為系統提供精確的時間基準的元器件&#xff0c;RTC一般采用精度較高的晶振作為時鐘源&#xff0c;有些RTC為了在主電源掉電時還可以工作&#xff0c;需要外加電池供電BCD碼&#xff0c;四位二進制表示一位…

Java Persistence APl(JPA)——JPA是啥? SpringBoot整合JPA JPA的增刪改查 條件模糊查詢 多對一查詢

目錄 引出Jpa是啥&#xff1f;Jpa的使用創建實體類寫dao接口類寫服務類 crud增刪改查增加修改根據id刪除全查詢分頁查詢 條件查詢模糊查詢單條件查詢多條件查詢模糊查詢排序查詢 多對一查詢定義實體類auto主鍵策略下新增進行全查詢測試 全部代碼application.yml配置類pom配置文…

Java反射機制是什么?

Java反射機制是 Java 語言的一個重要特性。 在學習 Java 反射機制前&#xff0c;大家應該先了解兩個概念&#xff0c;編譯期和運行期。 編譯期是指把源碼交給編譯器編譯成計算機可以執行的文件的過程。在 Java 中也就是把 Java 代碼編成 class 文件的過程。編譯期只是做了一些…

Python學習筆記第五十二天(Pandas 安裝)

Python學習筆記第五十二天 Pandas 安裝查看安裝版本 安裝驗證后記 Pandas 安裝 安裝 pandas 需要基礎環境是 Python&#xff0c;開始前我們假定你已經安裝了 Python 和 Pip。 使用 pip 安裝 pandas: pip install pandas安裝成功后&#xff0c;我們就可以導入 pandas 包使用&…

iPhone(iPad)安裝deb文件

最簡單的方法就是把deb相關的文件拖入手機對應的目錄&#xff0c;一般是DynamicLibraries文件夾 參考&#xff1a;探討手機越獄和安裝deb文件的幾種方式研究 1、在 Mac 上安裝 dpkg 命令 打包 deb 教程之在 Mac 上安裝 dpkg 命令_xcode打包root權限deb_qq_34810996的博客-CS…

神經網絡基礎-神經網絡補充概念-26-前向和反向傳播

簡單比較 前向傳播&#xff08;Forward Propagation&#xff09;&#xff1a; 前向傳播是神經網絡中的正向計算過程&#xff0c;用于從輸入數據開始&#xff0c;逐層計算每個神經元的輸出值&#xff0c;直到得到最終的預測值。在前向傳播過程中&#xff0c;我們按以下步驟進行…

驅動DAY4 字符設備驅動分步注冊和ioctl函數點亮LED燈

頭文件 #ifndef __HEAD_H__ #define __HEAD_H__ typedef struct{unsigned int MODER;unsigned int OTYPER;unsigned int OSPEEDR;unsigned int PUPDR;unsigned int IDR;unsigned int ODR; }gpio_t; #define PHY_LED1_ADDR 0X50006000 #define PHY_LED2_ADDR 0X50007000 #d…

一百五十八、Kettle——Kettle各版本及其相關安裝包分享(網盤鏈接,不需積分、不需驗證碼) 持續更新、持續分享

一、目的 最近因為kettle9.3的shim問題看了好多博客&#xff0c;都沒有網盤分享。后來有一位博主分享了kettle9.2的shim安裝包&#xff0c;已經很感謝他&#xff0c;但是是博客分享&#xff0c;下載還需要搞驗證碼下載碼之類的。 kettle9.2的shim安裝包下載好后&#xff0c;一…

圖數據庫_Neo4j基于docker服務版安裝_Neo4j Desktop桌面版安裝---Neo4j圖數據庫工作筆記0004

然后我們來看看如何用docker來安裝Neo4j community server 首先去執行docker pull neo4j:3.5.22-community 去拉取鏡像 然后執行命令就可以安裝了 可以用docker ps查看一下 看看暴露了哪些端口 然后再看一下訪問一下這個時候,要用IP地址了注意 然后再來看一下安裝Desktop 去下…

Sigmastar SSC8826Q 2K行車記錄儀解決方案

一、方案描述 行車記錄儀是智能輔助汽車駕駛&#xff0c;和管理行車生活的車聯網智能終端設備&#xff0c;利用智能芯片處理器、GPS定位、網絡通信、自動控制等技術&#xff0c;將與行車生活有關的各項數據有機地結合在一起。 行車記錄儀如今已經成了必不可少的車載用品之一&…

雙向-->帶頭-->循環鏈表

目錄 一、雙向帶頭循環鏈表概述 1.什么是雙向帶頭循環鏈表 2.雙向帶頭循環鏈表的優勢 3.雙向帶頭循環鏈表簡圖 二、雙向帶頭循環鏈表的增刪查改圖解及代碼實現 1.雙向帶頭循環鏈表的頭插 2.雙向帶頭循環鏈表的尾插 3.雙向帶頭循環鏈表的頭刪 4.雙向帶頭循環鏈表的尾刪…

ATF(TF-A) 威脅模型匯總

安全之安全(security)博客目錄導讀 目錄計劃如下&#xff0c;相關內容補充中&#xff0c;待完成后進行超鏈接&#xff0c;敬請期待&#xff0c;歡迎您的關注 1、通用威脅模型 2、SPMC威脅模型 3、EL3 SPMC威脅模型 4、fvp_r 平臺威脅模型 5、RSS-AP接口威脅模型 威脅建模是安全…

淺學實戰:探索PySpark實踐,解鎖大數據魔法!

文章目錄 Spark和PySpark概述1.1 Spark簡介1.2 PySpark簡介 二 基礎準備2.1 PySpark庫的安裝2.2 構建SparkContext對象2.3 SparkContext和SparkSession2.4 構建SparkSession對象2.5 PySpark的編程模型 三 數據輸入3.1 RDD對象3.2 Python數據容器轉RDD對象3.3 讀取文件轉RDD對象…

IDEA的常用設置,讓你更快速的編程

一、前言 在使用JetBrains的IntelliJ IDEA進行軟件開發時&#xff0c;了解和正確配置一些常用設置是非常重要的。IDEA的強大功能和定制性使得開發過程更加高效和舒適。 在本文中&#xff0c;我們將介紹一些常用的IDEA設置&#xff0c;幫助您更好地利用IDEA進行開發。這些設置包…

Java面向對象——封裝以及this關鍵字

封 裝 封裝是面向對象編程&#xff08;OOP&#xff09;的三大特性之一&#xff0c;它將數據和操作數據的方法組合在一個單元內部&#xff0c;并對外部隱藏其具體實現細節。在Java中&#xff0c;封裝是通過類的訪問控制修飾符&#xff08;如 private、protected、public&#x…

Linux MQTT智能家居項目(智能家居界面布局)

文章目錄 前言一、創建工程項目二、界面布局準備工作三、正式界面布局總結 前言 一、創建工程項目 1.選擇工程名稱和項目保存路徑 2.選擇QWidget 3.添加保存圖片的資源文件&#xff1a; 在工程目錄下添加Icon文件夾保存圖片&#xff1a; 將文件放入目錄中&#xff1a; …

網絡層協議

網絡層協議 IP協議基本概念協議頭格式網段劃分特殊的IP地址IP地址的數量限制私有IP地址和公網IP地址路由IP協議頭格式后續 在復雜的網絡環境中確定一個合適的路徑 IP協議 承接上文&#xff0c;TCP協議并不會直接將數據傳遞給對方&#xff0c;而是交付給下一層協議&#xff0c;…

機器學習基礎(四)

KNN算法 KNN:K-Nearest Neighbor,最近領規則分類。 為了判斷位置實例的類別,以所有已知類別的實例作為參照選擇參數K。計算未知實例與所有已知實例的距離。(一般采用歐氏距離)選擇最近K個已知實例。根據少數服從多數的投票法則,讓未知實例歸類為K個最近鄰樣本中最多數的類…

音視頻FAQ(三):音畫不同步

摘要 本文介紹了音畫不同步問題的五個因素&#xff1a;編碼和封裝階段、網絡傳輸階段、播放器中的處理階段、源內容產生的問題以及轉碼和編輯。針對這些因素&#xff0c;提出了相應的解決方案&#xff0c;如使用標準化工具、選擇強大的傳輸協議、自適應緩沖等。此外&#xff0…

uniapp微信小程序區分正式版,開發版,體驗版

小程序代碼區分是正式版&#xff0c;開發版&#xff0c;還是體驗版 通常正式和開發環境需要調用不同域名接口&#xff0c;發布時需要手動更換 或者有些東西不想在正式版顯示&#xff0c;只在開發版體驗版中顯示&#xff0c;也需要去手動隱藏 官方沒有明確給出判斷環境的方法&a…