微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 微电子和IC设计 > 微电子学习交流 > verilog写入单字节数据到HEX文件的问题

verilog写入单字节数据到HEX文件的问题

时间:12-12 整理:3721RD 点击:
写testbench,希望 写入单字节数据到HEX文件,但是总是把每个数据补充成 32bit写
入,阅读了一些文献,依然不对,请教各位!
       // Open File
                fp = $fopen("AF.hex", "wb");//must be binary read mode
                if (!fp) begin
                        $display("writeHEX: Open error!\n");
                        $finish;
                end
                $display("output file : AF.hex",);
// some regs have been assigned values somewhere, here for example,
//H1 = 16’h4D42;          // offset 0-1
//H2 = 32’h36C00000;     // offset 2-5
//        // Write Header Informations
                $fwrite(fp,"%u",H1);       // 16 bits word
                $fwrite(fp,"%u",H2);       // 32 bit word
期望的hex文件如下: (用16进制方式查看)
offset 0     1     2     3     4     5     6     7     8    
9     a     b     c     d
        42     4D    36    C0   00     00  
当写入一个数据,即仅执行一句  $fwrite(fp,"%u",H1);       // 16 bits word
offset 0     1     2     3     4     5     6     7     8    
9     a     b     c     d
        4D     42    00    00  
当写入一个16bit数据,另外再写入1个32bit数据时,
offset 0     1     2     3     4     5     6     7     8    
9     a     b     c     d
        4D     42    00    00   00  00   C0   36
实际情况对照Verilog的标准,发现如红色部分所说,是以32bit为单位写入的额,
units of 32 bits, 有的仿真器能够修改这个。 但是modelsim等却无此选项。
4D42只有16bit,所以后面补入16bit的 00 00,占据offset为2-3的位置。
不知道大家有何高见? 怎样才能写入单字节,后面还希望写入8bit的数据,现在如何
控制数据写入的宽度?
The formatting specification %u (or %U) is defined for writing data without
formatting (binary values). The application shall transfer the 2 value
binary representation of the specified data to the output stream. Thisescape
sequence can be used with any of the existing display system tasks, although
$fwrite should be the preferred  one  to  use.  Any  unknown  or  high-
impedance  bits  in  the  source  shall  be  treated  as  zero.  This
formatting specifier is intended to be used to support transferring data to
and from external programs that have  no concept of x and z. Applications
that require preservation of x and z are encouraged to use the %z I/O format
specification.
=============================================================
The data shall be written to the file in the native endian format of the
underlying system (i.e., in the same endian order as if the PLI was used and
the C language write (2) system call was used). The data shall be written in
units of 32 bits with the word containing the LSB written first.
===============================================================
NOTE—For  POSIX  applications,  it  might  be  necessary  to  open  files  
for  unformatted  I/O  with  the  wb ,  wb+ ,  or  w+b specifiers  to  avoid  
the  systems  implementation  of  I/O  altering  patterns  in  the  
unformatted  stream  that  match  special characters.

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top