Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Verilog testbench, write a single byte into an hex file,HOW?

Status
Not open for further replies.

xang

Newbie level 6
Joined
May 22, 2007
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,355
testbench
I want to write a single byte(8 or 16 bits) into an hex file, but the system always replenishes it to a 32bits data with extra zeros.

Part of the verilog file and my analysis are expressed below.

// verilog file
// 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



The expected hex file should be like this (with HEX reader)
offset 0 1 2 3 4 5 6 7 8
42 4D 36 C0 00 00

when only one 16 bits word, H1, is written into the file, i.e. only this sentence executed
$fwrite(fp,"%u",H1); // 16 bits word
The result is:
offset 0 1 2 3 4 5 6 7 8
4D 42 00 00

when writing both H1(16 bits) and H2(32 bits),
offset 0 1 2 3 4 5 6 7 8
4D 42 00 00 00 00 C0 36


according to the verilog standard, “The data shall be written in
units of 32 bits with the word containing the LSB written first. ”. some simulators can be configured to enable %u with unit of byte, But most of them can not. Every time, if the word to be written less than 32bits, it will be added with extra zeros to make a 32bit word and then written into the hex file.

Is there any method to write a single byte(8bit) or 16 bits word into an hex file without extra zeros ?

Thank you very much !
Attached is part of the IEEE Verilog standard file for fwrite and the format %u


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.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top