verilog读入.txt的有符号十进制数,把有符号十进制数写入到.txt文件中

在进行功能仿真时,经常需要使用外部的数据作为输入,而数据经常存放在.txt,.dat等文本文件中;

本文介绍一种从.txt中读取有符号十进制数的方式和写入有符号数到.txt文件的方式:

代码如下:

一、从.TXT中读取有符号十进制数:

reg signed [8:0] dataa1[1:784];
reg signed [8:0] datab[1:36];
reg signed [8:0] data1;
integer i;
integer s;
integer m;
integer n;
integer fid1;
integer fid2;
initial
begin
   fid1 = $fopen("C:/Users/Desktop/conv_data.txt","r");//这里的斜杠与计算机里面的斜杠不一样
                                                       //计算机里面的斜杠为‘\’,这里为‘/’
   for(i=1;i<=784;i=i+1)
       begin
        s= $fscanf(fid1,"%d",dataa1[i]);
       end
    $fclose(fid1);
end

initial
begin
   //fid = $fopen("C:\Users\CONV_project2\Wc1_8.dat","r");  这样是会报错的
   fid2 = $fopen("C:/Users/15003813081/Desktop/ZYK/CONV_project2/core_data.txt","r");
   for(m=1;m<=36;m=m+1)
       begin
        n= $fscanf(fid2,"%d",datab[m]);
       end
   $fclose(fid2);
end
reg[11:0] count1;
reg[6:0] count2;
always@(posedge clk or negedge rst)
begin
   if(rst)
      begin
         count1 <= 0;
      end
    else
      begin
          if(count1 == 12'd2000)
             count1 <= count1;
          else
             count1 <= count1 + 1'b1;
      end
end

always@(posedge clk or negedge rst)
begin
  if(rst)
     begin
        dataa <= 16'd0;
        dataa_valid <= 1'b0;
     end
  else
     begin
        if(count1 >= 200 && count1 <= 983 )
            begin
               dataa <= dataa1[count1-199];
               dataa_valid <= 1'b1;
            end
        else 
            begin
              dataa <= 9'd0;
              dataa_valid <= 1'b0;
            end
     end
end

always@(posedge clk or negedge rst)
begin
   if(rst)
      begin
         count2 <= 0;
      end
    else
      begin
          if(count2 == 7'd100)
             count2 <= count2;
          else
             count2 <= count2 + 1'b1;
      end
end

always@(posedge clk or negedge rst)
begin
  if(rst)
     begin
        datab1 <= 9'd0;
        datab_valid1 <= 1'b0;
        datab2 <= 9'd0;
        datab_valid2 <= 1'b0;
        datab3 <= 9'd0;
        datab_valid3 <= 1'b0;
        datab4 <= 9'd0;
        datab_valid4 <= 1'b0;
     end
  else
     begin
        if(count2 >= 2 && count2 <= 10)
            begin
               datab1 <= datab[count2-1];
               datab_valid1 <= 1'b1;
               datab2 <= datab[count2+8];
               datab_valid2 <= 1'b1;
               datab3 <= datab[count2+17];
               datab_valid3 <= 1'b1;
               datab4 <= datab[count2+26];
               datab_valid4 <= 1'b1;
            end
        else 
            begin
              datab1 <= 9'd0;
              datab_valid1 <= 1'b0;
              datab2 <= 9'd0;
              datab_valid2 <= 1'b0;
              datab3 <= 9'd0;
              datab_valid3 <= 1'b0;
              datab4 <= 9'd0;
              datab_valid4 <= 1'b0;
            end
     end
end

二、把有符号数写入.TXT文件:

 

integer fid3,fid4,fid5,fid6;
initial
begin
   fid3 = $fopen("C:/Users/pooling1.txt","w");
   fid4 = $fopen("C:/Users/pooling2.txt","w");
   fid5 = $fopen("C:/Users/pooling3.txt","w");
   fid6 = $fopen("C:/Users/pooling4.txt","w"); 
   #20000
   $fclose(fid3) ;
   $fclose(fid4) ;
   $fclose(fid5) ;
   $fclose(fid6) ;
end

always@(posedge clk2)
begin
   if(result_pooling_valid1)
    begin
        // $fdisplay(fid3,"%d",result_pooling1);
        $fwrite(fid3,"%d\n",$signed(result_pooling1));
    end 
end

always@(posedge clk2)
begin
   if(result_pooling_valid2)
     begin
        // $fdisplay(fid4,"%d",result_pooling2);
        $fwrite(fid4,"%d\n",$signed(result_pooling2));
     end
end

always@(posedge clk2)
begin
   if(result_pooling_valid3)
     begin
         //$fdisplay(fid5,"%d",result_pooling3);   默认写入换行
         $fwrite(fid5,"%d\n",$signed(result_pooling3));
     end
end

always@(posedge clk)
begin
   if(result_pooling_valid4)
     begin
         //$fdisplay(fid6,"%d",result_pooling4);
         $fwrite(fid6,"%d\n",$signed(result_pooling4));
     end
end
  1.  $fdisplay(fid4,"%d",result_pooling2);                       这个语句写入时,会把结果变成无符号数;
  2.  $fdisplay(fid4,"%d",$signed(result_pooling2));     这样应该就可以按照有符号写入了,是否可以我还没尝试
  3.  $fwrite(fid4,"%d\n",$signed(result_pooling2));         这个语句写入时,会把结果按成有符号数写入文件
  4. fdispaly与fwriter的区别应该是fdisplay会在写入一个数据之后自动换行,但是fwriter不会

关于verilog中其他的读写文件方式,再以后的博客中继续更新;


你可能感兴趣的:(FPGA,verilog)