Jump to content

    

Recommended Posts

Добрый день

Есть результаты в виде 128 битных данных, мне их надо записать в файл в hex формате.

Написал функции для этого

----------------------------------------------------------------------

-- convert an unsigned value(4 bit) to a HEX digit (0-F)
function to_HexChar(Value : UNSIGNED) return CHARACTER is
  constant HEX : STRING := "0123456789ABCDEF";
begin
  if (Value < 16) then
    return HEX(to_integer(Value)+1);
  else
    return 'X';
  end if;
end function;

-- return TRUE, if input is a power of 2
function div_ceil(a : NATURAL; b : POSITIVE) return NATURAL is  -- calculates: ceil(a / b)
begin
  return (a + (b - 1)) / b;
end function;

-- return log2; always rounded up
function log2ceil(arg : positive) return natural is
  variable tmp : positive;
  variable log : natural;
begin
  if arg = 1 then   return 0; end if;
  tmp := 1;
  log := 0;
  while arg > tmp loop
    tmp := tmp * 2;
    log := log + 1;
  end loop;
  return log;
end function;

-- format a std_logic_vector as HEX string
function raw_format_slv_hex(slv : STD_LOGIC_VECTOR) return STRING is
  variable Value                : STD_LOGIC_VECTOR(4*div_ceil(slv'length, 4) - 1 downto 0);
  variable Digit                : STD_LOGIC_VECTOR(3 downto 0);
  variable Result               : STRING(1 to div_ceil(slv'length, 4));
  variable j                    : NATURAL;
begin
  Value := STD_LOGIC_VECTOR (resize(unsigned(slv), Value'length));
  j             := 0;
  for i in Result'reverse_range loop
    Digit       := Value((j * 4) + 3 downto (j * 4));
    Result(i)   := to_HexChar(unsigned(Digit));
    j           := j + 1;
  end loop;
  return Result;
end function;

но моделсим ругается

(vcom-1360) Array type of "outdata" does not have an index constraint.

вот процесс

  LIBRARY ieee;                                               
USE ieee.std_logic_1164.all;  
use ieee.numeric_std.all;
use	ieee.math_real.all;
use std.textio.all;  

.......

 write_to_file0: process(all)
	variable outdata_line: line;
	variable outdata : STRING;
	file output_data_file: text open write_mode is "final_out.txt";
 begin
 if rising_edge(clk) then
	
	if huffman_bitsream_final_out.valid = '1' then
		outdata := raw_format_slv_hex( STD_LOGIC_VECTOR (bitsream_final_out.bitstream));
 
	write(outdata_line,outdata);
	writeline(output_data_file,outdata_line);
	
	end if;
	
 end if;
 end process;

Подскажите пожалуйста, что нужно сделать чтобы была заись в файл в hex формате?

Share this post


Link to post
Share on other sites

вот другая функция

function hex (lvec: in std_logic_vector) return string is
		variable text: string(lvec'length / 4 - 1 downto 0) := (others => '9');
		subtype halfbyte is std_logic_vector(4-1 downto 0);
	begin
		assert lvec'length mod 4 = 0
			report "hex() works only with vectors whose length is a multiple of 4"
			severity FAILURE;
		for k in text'range loop
			case halfbyte'(lvec(4 * k + 3 downto 4 * k)) is
				when "0000" => text(k) := '0';
				when "0001" => text(k) := '1';
				when "0010" => text(k) := '2';
				when "0011" => text(k) := '3';
				when "0100" => text(k) := '4';
				when "0101" => text(k) := '5';
				when "0110" => text(k) := '6';
				when "0111" => text(k) := '7';
				when "1000" => text(k) := '8';
				when "1001" => text(k) := '9';
				when "1010" => text(k) := 'A';
				when "1011" => text(k) := 'B';
				when "1100" => text(k) := 'C';
				when "1101" => text(k) := 'D';
				when "1110" => text(k) := 'E';
				when "1111" => text(k) := 'F';
				when others => text(k) := '!';
			end case;
		end loop;
		return text;
	end function;

все равно ошибка та же самая - записівать файл не хочет

(vcom-1360) Array type of "outdata" does not have an index constraint.

сам процесс стал

  write_to_file0: process(all)
	variable outdata_line: line;
	variable outdata : STRING;
	file output_data_file: text open write_mode is "bitsream_final_out.txt";
 begin
 if rising_edge(clk) then
	
	if huffman_bitsream_final_out.valid = '1' then
		
		outdata := hex( STD_LOGIC_VECTOR (bitsream_final_out.bitstream));
 
	write(outdata_line,outdata);
	writeline(output_data_file,outdata_line);
	
	end if;
	
 end if;
 end process;

 

 

Share this post


Link to post
Share on other sites

string -- это массив. Вы должны задать длину строки при её определении:

variable outdata : STRING (1 to 128);

Если длина неизвестна, тогда надо использовать указатели. Сначала рассчитать длину строки, а потом создать её динамически.

function raw_format_slv_hex(slv : STD_LOGIC_VECTOR) return LINE is
  variable Value                : STD_LOGIC_VECTOR(4*div_ceil(slv'length, 4) - 1 downto 0);
  variable Digit                : STD_LOGIC_VECTOR(3 downto 0);
  variable Result               : LINE;
  variable j                    : NATURAL;
begin
  Value := STD_LOGIC_VECTOR (resize(unsigned(slv), Value'length));
  j             := 0;
  Result := new string (1 to div_ceil(slv'length, 4));
  for i in Result'reverse_range loop
    Digit       := Value((j * 4) + 3 downto (j * 4));
    Result(i)   := to_HexChar(unsigned(Digit));
    j           := j + 1;
  end loop;
  return Result;
end function;


...


 write_to_file0: process(all)
	variable outdata_line: line;
	variable outdata : line;
	file output_data_file: text open write_mode is "final_out.txt";
 begin
 if rising_edge(clk) then
	
	if huffman_bitsream_final_out.valid = '1' then
		outdata := raw_format_slv_hex( STD_LOGIC_VECTOR (bitsream_final_out.bitstream));
 
		write(outdata_line,outdata.all);
		writeline(output_data_file,outdata_line);
		Deallocate (outdata);
	end if;
	
 end if;
 end process;

С другой стороны, а зачем тут переменная?

 write_to_file0: process(all)
	variable outdata_line: line;
	file output_data_file: text open write_mode is "final_out.txt";
 begin
 if rising_edge(clk) then
	
	if huffman_bitsream_final_out.valid = '1' then
		write(outdata_line, raw_format_slv_hex( STD_LOGIC_VECTOR (bitsream_final_out.bitstream)));
		writeline(output_data_file,outdata_line);
	end if;
	
 end if;
 end process;

 

 

 

 

 

Share this post


Link to post
Share on other sites
3 hours ago, andrew_b said:

string -- это массив. Вы должны задать длину строки при её определении:


variable outdata : STRING (1 to 128);

 

 

Спасибо за помощь

Вроде работает сделал следующим образом

LIBRARY ieee;                                               
USE ieee.std_logic_1164.all;  
use ieee.numeric_std.all;
use	ieee.math_real.all;
use std.textio.all;  

.....

-- convert an unsigned value(4 bit) to a HEX digit (0-F)
function to_HexChar(Value : UNSIGNED) return CHARACTER is
  constant HEX : STRING := "0123456789ABCDEF";
begin
  if (Value < 16) then
    return HEX(to_integer(Value)+1);
  else
    return 'X';
  end if;
end function;

-- return TRUE, if input is a power of 2
function div_ceil(a : NATURAL; b : POSITIVE) return NATURAL is  -- calculates: ceil(a / b)
begin
  return (a + (b - 1)) / b;
end function;

-- return log2; always rounded up
function log2ceil(arg : positive) return natural is
  variable tmp : positive;
  variable log : natural;
begin
  if arg = 1 then   return 0; end if;
  tmp := 1;
  log := 0;
  while arg > tmp loop
    tmp := tmp * 2;
    log := log + 1;
  end loop;
  return log;
end function;

-- format a std_logic_vector as HEX string
function raw_format_slv_hex(slv : STD_LOGIC_VECTOR) return STRING is
  variable Value                : STD_LOGIC_VECTOR(4*div_ceil(slv'length, 4) - 1 downto 0);
  variable Digit                : STD_LOGIC_VECTOR(3 downto 0);
  variable Result               : STRING(1 to div_ceil(slv'length, 4));
  variable j                    : NATURAL;
begin
  Value := STD_LOGIC_VECTOR (resize(unsigned(slv), Value'length));
  j             := 0;
  for i in Result'reverse_range loop
    Digit       := Value((j * 4) + 3 downto (j * 4));
    Result(i)   := to_HexChar(unsigned(Digit));
    j           := j + 1;
  end loop;
  return Result;
end function;
                                    
и сам процесс
                                    
 write_to_file0: process(all)
	variable outdata_line: line;
	variable outdata : STRING(1 to 32);
	file output_data_file: text open write_mode is "huffman_bitsream_final_out.txt";
 begin
 if rising_edge(clk) then
	
	if huffman_bitsream_final_out.valid = '1' then
		outdata := raw_format_slv_hex( STD_LOGIC_VECTOR (huffman_bitsream_final_out.bitstream));
 
	write(outdata_line,outdata);
	writeline(output_data_file,outdata_line);
	
	end if;
	
 end if;
 end process;                                    
                                    
                                    

 

 

 

Share this post


Link to post
Share on other sites
16.10.2021 в 15:13, Maverick_ сказал:

Добрый день

Есть результаты в виде 128 битных данных, мне их надо записать в файл в hex формате.

Написал функции для этого


----------------------------------------------------------------------

-- convert an unsigned value(4 bit) to a HEX digit (0-F)
function to_HexChar(Value : UNSIGNED) return CHARACTER is
  constant HEX : STRING := "0123456789ABCDEF";
begin
  if (Value < 16) then
    return HEX(to_integer(Value)+1);
  else
    return 'X';
  end if;
end function;

-- return TRUE, if input is a power of 2
function div_ceil(a : NATURAL; b : POSITIVE) return NATURAL is  -- calculates: ceil(a / b)
begin
  return (a + (b - 1)) / b;
end function;

-- return log2; always rounded up
function log2ceil(arg : positive) return natural is
  variable tmp : positive;
  variable log : natural;
begin
  if arg = 1 then   return 0; end if;
  tmp := 1;
  log := 0;
  while arg > tmp loop
    tmp := tmp * 2;
    log := log + 1;
  end loop;
  return log;
end function;

-- format a std_logic_vector as HEX string
function raw_format_slv_hex(slv : STD_LOGIC_VECTOR) return STRING is
  variable Value                : STD_LOGIC_VECTOR(4*div_ceil(slv'length, 4) - 1 downto 0);
  variable Digit                : STD_LOGIC_VECTOR(3 downto 0);
  variable Result               : STRING(1 to div_ceil(slv'length, 4));
  variable j                    : NATURAL;
begin
  Value := STD_LOGIC_VECTOR (resize(unsigned(slv), Value'length));
  j             := 0;
  for i in Result'reverse_range loop
    Digit       := Value((j * 4) + 3 downto (j * 4));
    Result(i)   := to_HexChar(unsigned(Digit));
    j           := j + 1;
  end loop;
  return Result;
end function;

но моделсим ругается

(vcom-1360) Array type of "outdata" does not have an index constraint.

вот процесс


  LIBRARY ieee;                                               
USE ieee.std_logic_1164.all;  
use ieee.numeric_std.all;
use	ieee.math_real.all;
use std.textio.all;  

.......

 write_to_file0: process(all)
	variable outdata_line: line;
	variable outdata : STRING;
	file output_data_file: text open write_mode is "final_out.txt";
 begin
 if rising_edge(clk) then
	
	if huffman_bitsream_final_out.valid = '1' then
		outdata := raw_format_slv_hex( STD_LOGIC_VECTOR (bitsream_final_out.bitstream));
 
	write(outdata_line,outdata);
	writeline(output_data_file,outdata_line);
	
	end if;
	
 end if;
 end process;

Подскажите пожалуйста, что нужно сделать чтобы была заись в файл в hex формате?


 process(clk,rst)       
 variable i : integer := 0;
 variable c : std_logic_vector(127 downto 0);   
 type t_ch is array (0 to 15) of character; 
 constant hex_c : t_ch := ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
 file wfile : text open write_mode is "write.log";
 variable l : line; 
 begin           
     if rst = '1' then    
         c := x"12000000000000000000000000000000";
     elsif clk = '1' and clk'event then     
         c:= c + 1;      
         write(l, 'x'); write(l, '"');
         for j in 0 to 31 loop      
             i := conv_integer(c(127 - j*4 downto 124  - j*4));     
             write(l, hex_c(i)); 
         end loop; 
         write(l, '"');write(l, ',');writeline(wfile, l);   

     end if;
 end process;               

Записываем переменную с. 

Share this post


Link to post
Share on other sites

Вот процедурка. Я до конца её не дописал, ибо не было нужды. Вроде работала.

Библиотеки такие:

    use IEEE.std_logic_textio.all;
    use std.env.stop;
    use std.textio.all;
    --===========================================================================
    -- Процедура для записи в файл
    --===========================================================================
    procedure writing_to_file 
    (
        path_file             : in string;
        signal clk            : in std_logic;
        signal data_in        : in std_logic_vector;
        signal data_in_valid  : in std_logic;
        constant radix        : in string; 
        constant line_length  : in positive; 
        constant debug_length : in positive
    ) is
        file write_data_table     : text open write_mode is path_file;
        variable buf              : line; 
        variable i                : integer := 0;
        variable j                : integer := 0;
    begin
        while i <= debug_length - 1 loop
            wait until falling_edge(clk);
            if (data_in_valid = '1') then
                
                if (radix = "Hex") then 
                    hwrite(buf, data_in);
                end if;
                
                if (radix = "Integer") then 
                    write(buf, to_integer(unsigned(data_in)) );
                end if;
                
                if (radix = "Singed") then 
                    hwrite(buf, data_in);
                end if;
                
                if (radix = "Unsigned") then 
                    hwrite(buf, data_in);
                end if;
                
                if (j = line_length - 1) then 
                    j := 0;
                    writeline(write_data_table, buf);
                else 
                    j := j + 1;
                end if;
                
                if (i = debug_length - 1) then 
                    i := debug_length - 1;
                    report LF & LF & LF &
                    "==================================================================================================================="& LF &
                    "                                                    STOP SIMULATION                                                "& LF &
                    "====================================================================================================================";
                    stop;
                else
                    i := i + 1;
                end if;
            end if;
        end loop;
    end procedure; 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this