網站首頁 編程語言 正文
??????? 在我之前的一個仿真中,需要從一個txt文本中提取發送數據的文本描述,然后轉化為二進制數據作為被測試module的輸入。以下就是文本描述的大致格式。
K0.0
K0.7
K16.0
D28.5
K8.4
??????? 在testbench中,需要識別文本中的編碼類型,如K碼或D碼。同時把文本中的碼轉換為8bits的二進制數。我通過string變量讀入每一行的信息,然后使用以下的方式來識別編碼類型。
$fgets(str_data, file);
i = str_data.len();
if(str_data.getc(0) == "K")
is_k = 1'b1;
else if(str_data.getc(0) == "D")
is_k = 1'b0;
else if(str_data == "") begin
end else begin
$display(" Source data syntax error, code -1!");
$finish;
end
??????? 需要注意的是,str.len()返回的長度包含換行符等,比如第一行的k0.0的長度就是5,而不是4。
??????? 接下來需要將其轉換為二進制。由于當時對SV中的string不熟悉,我最開始使用了str.atohex,其轉換結果如下。
????????
Data is 00000000, Type is 1
Data is 11100000, Type is 1
Data is 00010110, Type is 1
Data is 10101000, Type is 0
Data is 10001000, Type is 1
All data has been read.
??????? 上圖中,前兩行文本轉換正確。但是第三行的K16.0應該轉換為00010000,實際結果卻是00010110。說明atohex函數是將字符串中的每個數字字符獨立的轉換為其對應的16進制表示,所以16中的1轉換為0001,6轉換為0110。
??????? 接下來我又測試了atobin函數,其結果如下。可以看到,除了K16.0轉換的二進制數據中出現了1,其他全為0。這說明atobin函數會將每個數字轉換成1bit的二進制,所以除了1和0之外的所有數字都無法正確轉換。
Data is 00000000, Type is 1
Data is 00000000, Type is 1
Data is 00000001, Type is 1
Data is 00000000, Type is 0
Data is 00000000, Type is 1
All data has been read.
??????? 最后測試了atoi函數,該函數能正確工作,他將整個字符串當做一個完整的10進制數進行轉換,結果如下。
Data is 00000000, Type is 1
Data is 11100000, Type is 1
Data is 00010000, Type is 1
Data is 10111100, Type is 0
Data is 10001000, Type is 1
All data has been read.
??????? 用于轉換的task如下。
task read_file;
output [DATA_WIDTH-1:0] rd_data;
output is_k;
output last_data;
integer i;
reg [ 4: 0] x;
reg [ 2: 0] y;
string strx, stry;
string str_data;
last_data = 1'b0;
$fgets(str_data, file);
i = str_data.len();
if(str_data.getc(0) == "K")
is_k = 1'b1;
else if(str_data.getc(0) == "D")
is_k = 1'b0;
else if(str_data == "") begin
end else begin
$display(" Source data syntax error, code -1!");
$finish;
end
if(i==6) begin
stry = str_data.substr(i-2, i-2);
strx = str_data.substr(1, 2);
x = strx.atoi();
y = stry.atoi();
end else if(i==5) begin
stry = str_data.substr(i-2, i-2);
strx = str_data.substr(1, 1);
x = strx.atoi();
y = stry.atoi();
end else if(str_data=="") begin
$display(" All data has been read.");
last_data = 1'b1;
end else begin
$display(" Source data syntax error, code -2!");
$finish;
end
rd_data = {y, x};
#SP;
endtask : read_file
原文鏈接:https://blog.csdn.net/yinyeyy/article/details/125780944
相關推薦
- 2022-03-26 Unity實現坦克模型_C#教程
- 2022-06-12 Python使用matplotlib.pyplot?as?plt繪圖圖層優先級問題_python
- 2022-08-19 python中的函數嵌套和嵌套調用_python
- 2021-12-02 Spring?Boot?分層打包?Docker?鏡像實踐及分析(推薦)_docker
- 2022-06-14 C#獲取指定目錄下指定文件的方法_C#教程
- 2022-07-14 Python中添加搜索路徑的方法實例_python
- 2022-07-10 理解setuid()、setgid()和sticky位
- 2023-01-12 pandas中的DataFrame數據遍歷解讀_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支