首页 > 代码库 > sv中$bitstorealshort() 函数

sv中$bitstorealshort() 函数

在sv中用$bitstorealshort()函数可以轻松的将十六进制,二进制数转化成单精度浮点数,以下为源代码,在modelsim10.1c中测试通过,文件需要保存为.sv文件

module h2f;

integer fid_rd;
integer fid_wr;
// integer fid_mif;
  bit [11:0] variable=0;
  bit [31:0] value;
int  h_array[0:1023];
  shortreal fr;
initial begin:file_rd
fid_rd = $fopen("rd_h.txt","r");
if(fid_rd == 0)
disable file_rd;

while(!$feof(fid_rd))begin
 if(!$feof(fid_rd)== -1)
             break;
$fscanf(fid_rd,"%h",value);
h_array[variable] = value;
variable = variable + 1‘b1;
end
$fclose(fid_rd);


fid_wr = $fopen("h2f.txt","w");


foreach(h_array[i])begin
  fr = $bitstoshortreal(h_array[i]);
   $display("%d %f",i,fr);
$fdisplay(fid_wr,"%f",fr);
end
$fclose(fid_wr);
end
  

endmodule


sv中$bitstorealshort() 函数