OpenCores
URL https://opencores.org/ocsvn/xucpu/xucpu/trunk

Subversion Repositories xucpu

[/] [xucpu/] [trunk/] [src/] [util/] [file/] [hexio.vhdl] - Rev 27

Compare with Previous | Blame | View Log

-- Copyright 2015, Jürgen Defurne
--
-- This file is part of the Experimental Unstable CPU System.
--
-- The Experimental Unstable CPU System Is free software: you can redistribute
-- it and/or modify it under the terms of the GNU Lesser General Public License
-- as published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
--
-- The Experimental Unstable CPU System is distributed in the hope that it will
-- be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
-- General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License
-- along with Experimental Unstable CPU System. If not, see
-- http://www.gnu.org/licenses/lgpl.txt.
 
 
LIBRARY std;
USE std.textio.ALL;
 
PACKAGE hexio IS
 
  -- This type must be used for base memory arrays
  TYPE cstr_array_type IS ARRAY(INTEGER RANGE <>) OF INTEGER RANGE 0 TO 65535;
  TYPE B32K_array_type IS ARRAY(0 TO 3) OF cstr_array_type(0 TO 8191);
 
  FUNCTION init_cstr (
    CONSTANT array_size : IN INTEGER;
    CONSTANT input_file : IN STRING)
    RETURN cstr_array_type;
 
  FUNCTION init_b32k (
    CONSTANT input_file : IN STRING)
    RETURN B32K_array_type;
 
  PROCEDURE init_b32k_array (
    VARIABLE array_io : INOUT B32K_array_type;
    CONSTANT filename : IN    STRING);
 
  PROCEDURE init_var_array (
    VARIABLE array_in : INOUT cstr_array_type;
    CONSTANT filename : IN    STRING);
 
  PROCEDURE init_sig_array (
    SIGNAL array_in   : INOUT cstr_array_type;
    CONSTANT filename : IN    STRING);
 
  PROCEDURE dump_array (
    CONSTANT array_in : IN cstr_array_type);
 
  PROCEDURE notify (
    CONSTANT message : IN STRING);
 
  FUNCTION notify_f (
    CONSTANT message : IN STRING)
    RETURN INTEGER;
 
END PACKAGE hexio;
 
PACKAGE BODY hexio IS
 
  -- Private declarations
  PROCEDURE read_hex (
    VARIABLE input_line : IN  STRING;
    VARIABLE hex_value  : OUT INTEGER);
 
  FUNCTION hex_char_to_value (
    CONSTANT chr : IN CHARACTER)
    RETURN INTEGER;
 
  PROCEDURE fill_var_array (
    CONSTANT value    : IN    INTEGER;
    VARIABLE in_array : INOUT cstr_array_type);
 
  PROCEDURE fill_sig_array (
    CONSTANT value  : IN    INTEGER;
    SIGNAL in_array : INOUT cstr_array_type);
 
  PROCEDURE read_file_into_var_array (
    VARIABLE array_in : INOUT cstr_array_type;
    CONSTANT filename : IN    STRING);
 
  PROCEDURE read_file_into_sig_array (
    SIGNAL array_in   : INOUT cstr_array_type;
    CONSTANT filename : IN    STRING);
 
  -- Procedure and function body definitions
  PROCEDURE notify (
    CONSTANT message : IN STRING) IS
 
    VARIABLE output_line : LINE;
  BEGIN
    write(output_line, message);
    writeline(OUTPUT, output_line);
  END;
 
  FUNCTION notify_f (
    CONSTANT message : IN STRING)
    RETURN INTEGER IS
  BEGIN
    notify(message);
 
    RETURN 0;
  END;
 
  FUNCTION init_cstr (
    CONSTANT array_size : IN INTEGER;
    CONSTANT input_file : IN STRING)
    RETURN cstr_array_type IS
 
    VARIABLE rv : cstr_array_type(0 TO array_size - 1) := (OTHERS => 0);
 
  BEGIN  -- FUNCTION init_cstr
 
    notify("Initialising memory");
    init_var_array(rv, input_file);
 
    RETURN rv;
  END FUNCTION init_cstr;
 
  FUNCTION init_b32k (
    CONSTANT input_file : IN STRING)
    RETURN B32K_array_type IS
 
    VARIABLE rv : B32K_array_type;
 
  BEGIN  -- FUNCTION init_b32K
 
    init_b32k_array(rv, input_file);
 
    RETURN rv;
  END FUNCTION init_b32K;
 
  PROCEDURE init_b32k_array (
    VARIABLE array_io : INOUT B32K_array_type;
    CONSTANT filename : IN    STRING) IS
 
    FILE input_file : TEXT;
 
    VARIABLE input_line : LINE;
    VARIABLE fstatus    : FILE_OPEN_STATUS;
 
    VARIABLE a_index : INTEGER := 0;
    VARIABLE i_value : INTEGER := 0;
 
    VARIABLE output_line : LINE;
    VARIABLE line_value  : STRING(1 TO 4);
 
    VARIABLE memory_block : INTEGER RANGE 0 TO 3    := 0;
    VARIABLE address      : INTEGER RANGE 0 TO 8191 := 0;
 
    VARIABLE end_of_mem : BOOLEAN := FALSE;
 
  BEGIN
 
    FOR i IN 0 TO 3 LOOP
      array_io(i) := (OTHERS => 0);
    END LOOP;
 
    file_open(fstatus, input_file, filename, READ_MODE);
 
    IF fstatus = OPEN_OK THEN
      WHILE NOT endfile(input_file) AND NOT end_of_mem LOOP
        -- Read the next line and put its contents in a string
        readline(input_file, input_line);
        read(input_line, line_value);
 
        -- Current debugging feedback
        write(output_line, line_value);
        writeline(OUTPUT, output_line);
 
        -- Turn a hex value into an integer value
        read_hex(line_value, i_value);
 
        array_io(memory_block)(address) := i_value;
 
        IF memory_block = 3 AND address = 8191 THEN
          end_of_mem := TRUE;
        ELSE
          IF address = 8191 THEN
            memory_block := memory_block + 1;
            address      := 0;
          ELSE
            address := address + 1;
          END IF;
        END IF;
 
        write(output_line, STRING'("Index :"));
        write(output_line, memory_block*8192+address);
        write(output_line, STRING'(" Value: "));
        write(output_line, i_value);
        writeline(OUTPUT, output_line);
      END LOOP;
 
      file_close(input_file);
 
    END IF;
 
  END PROCEDURE init_b32k_array;
 
-- Fill a signal array with the contents of a file
  PROCEDURE init_sig_array (
    SIGNAL array_in   : INOUT cstr_array_type;
    CONSTANT filename : IN    STRING) IS
 
  BEGIN
 
    fill_sig_array(0, array_in);
    read_file_into_sig_array(array_in, filename);
 
  END PROCEDURE init_sig_array;
 
-- General procedure to fill an array of integers. This is to make sure that
-- the array does not contain any meta-data any more.
  PROCEDURE fill_sig_array (
    CONSTANT value  : IN    INTEGER;
    SIGNAL in_array : INOUT cstr_array_type) IS
 
  BEGIN  -- PROCEDURE fill_array
    FOR i IN in_array'RANGE LOOP
      in_array(i) <= value;
    END LOOP;  -- i
  END PROCEDURE fill_sig_array;
 
-- Read the file into the signal array
  PROCEDURE read_file_into_sig_array (
    SIGNAL array_in   : INOUT cstr_array_type;
    CONSTANT filename : IN    STRING) IS
 
    FILE input_file : TEXT;
 
    VARIABLE input_line : LINE;
    VARIABLE fstatus    : FILE_OPEN_STATUS;
 
    VARIABLE a_index : INTEGER := 0;
    VARIABLE i_value : INTEGER := 0;
 
    VARIABLE output_line : LINE;
    VARIABLE line_value  : STRING(1 TO 4);
 
  BEGIN  -- PROCEDURE read_file_into_sig_array
 
    file_open(fstatus, input_file, filename, READ_MODE);
 
    IF fstatus = OPEN_OK THEN
      WHILE NOT endfile(input_file) LOOP
        -- Read the next line and put its contents in a string
        readline(input_file, input_line);
        read(input_line, line_value);
 
        -- Current debugging feedback
        write(output_line, line_value);
        writeline(OUTPUT, output_line);
 
        -- Turn a hex value into an integer value
        read_hex(line_value, i_value);
 
        array_in(a_index) <= i_value;
        a_index           := a_index + 1;
 
        write(output_line, STRING'("Index :"));
        write(output_line, a_index);
        write(output_line, STRING'(" Value: "));
        write(output_line, i_value);
        writeline(OUTPUT, output_line);
      END LOOP;
 
      file_close(input_file);
 
    END IF;
 
  END PROCEDURE read_file_into_sig_array;
 
-- Initialise a variable array
  PROCEDURE init_var_array (
    VARIABLE array_in : INOUT cstr_array_type;
    CONSTANT filename : IN    STRING) IS
 
  BEGIN
 
    fill_var_array(0, array_in);
    read_file_into_var_array(array_in, filename);
 
  END PROCEDURE init_var_array;
 
-- General procedure to fill a variable array of integers. This is to make
-- sure that the array does not contain any meta-data any more.
  PROCEDURE fill_var_array (
    CONSTANT value    : IN    INTEGER;
    VARIABLE in_array : INOUT cstr_array_type) IS
 
  BEGIN  -- PROCEDURE fill_array
 
    FOR i IN in_array'RANGE LOOP
      in_array(i) := value;
    END LOOP;  -- i
 
  END PROCEDURE fill_var_array;
 
  PROCEDURE read_file_into_var_array (
    VARIABLE array_in : INOUT cstr_array_type;
    CONSTANT filename : IN    STRING) IS
 
    FILE input_file : TEXT;
 
    VARIABLE input_line : LINE;
    VARIABLE fstatus    : FILE_OPEN_STATUS;
 
    VARIABLE a_index : INTEGER := 0;
    VARIABLE i_value : INTEGER := 0;
 
    VARIABLE output_line : LINE;
    VARIABLE line_value  : STRING(1 TO 4);
 
  BEGIN  -- PROCEDURE read_file
 
    file_open(fstatus, input_file, filename, READ_MODE);
 
    IF fstatus = OPEN_OK THEN
      WHILE NOT endfile(input_file) LOOP
        -- Read the next line and put its contents in a string
        readline(input_file, input_line);
        read(input_line, line_value);
 
        -- Current debugging feedback
        write(output_line, line_value);
        writeline(OUTPUT, output_line);
 
        -- Turn a hex value into an integer value
        read_hex(line_value, i_value);
 
        array_in(a_index) := i_value;
        a_index           := a_index + 1;
 
        write(output_line, STRING'("Index :"));
        write(output_line, a_index);
        write(output_line, STRING'(" Value: "));
        write(output_line, i_value);
        writeline(OUTPUT, output_line);
      END LOOP;
 
      file_close(input_file);
 
    END IF;
 
  END PROCEDURE read_file_into_var_array;
 
-- Shared and generic procedures
 
-- Read a hexadecimal value from the input string and turn it into an integer.
  PROCEDURE read_hex (
    VARIABLE input_line : IN  STRING;
    VARIABLE hex_value  : OUT INTEGER) IS
 
    VARIABLE input_length : INTEGER := input_line'LENGTH;
    VARIABLE chr          : CHARACTER;
    VARIABLE output_line  : LINE;
 
    VARIABLE chr_value : INTEGER := 0;
    VARIABLE radix     : INTEGER := 1;
    VARIABLE result    : INTEGER := 0;
 
  BEGIN  -- PROCEDURE read_hex
 
    FOR i IN input_line'REVERSE_RANGE LOOP
      chr       := input_line(i);
      chr_value := hex_char_to_value(chr);
      result    := chr_value * radix + result;
      radix     := radix * 16;
    END LOOP;
 
    hex_value := result;
 
  END PROCEDURE read_hex;
 
-- Return the integer value matching with the hexadecimal character
  FUNCTION hex_char_to_value (
    CONSTANT chr : IN CHARACTER)
    RETURN INTEGER IS
 
    VARIABLE digit : INTEGER := 0;
  BEGIN  -- PROCEDURE hex_char_to_value
 
    CASE chr IS
      WHEN '0'    => digit := 0;
      WHEN '1'    => digit := 1;
      WHEN '2'    => digit := 2;
      WHEN '3'    => digit := 3;
      WHEN '4'    => digit := 4;
      WHEN '5'    => digit := 5;
      WHEN '6'    => digit := 6;
      WHEN '7'    => digit := 7;
      WHEN '8'    => digit := 8;
      WHEN '9'    => digit := 9;
      WHEN 'A'    => digit := 10;
      WHEN 'B'    => digit := 11;
      WHEN 'C'    => digit := 12;
      WHEN 'D'    => digit := 13;
      WHEN 'E'    => digit := 14;
      WHEN 'F'    => digit := 15;
      WHEN OTHERS => digit := 0;
    END CASE;
 
    RETURN digit;
 
  END FUNCTION hex_char_to_value;
 
  PROCEDURE dump_array (
    CONSTANT array_in : IN cstr_array_type) IS
 
    VARIABLE output_line : LINE;
  BEGIN  -- PROCEDURE dump_array
 
    FOR i IN array_in'RANGE LOOP
 
      write(output_line, STRING'("Index: "));
      write(output_line, i);
 
      write(output_line, STRING'(" Value: "));
      write(output_line, array_in(i));
 
      writeline(OUTPUT, output_line);
 
    END LOOP;  -- i
 
 
  END PROCEDURE dump_array;
 
END PACKAGE BODY hexio;
 

Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.