-- Model Name : Concurrent - mem_pack_con -- Author : Armita Peymandoust -- Last Updated : 09 / 15 / 1996 -- This document is © copyrighted by the Author.

USE WORK.synthesis_utilities.ALL;

LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;

LIBRARY STD;
USE STD.textio.ALL;

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

PACKAGE mem_pack_con IS

  PROCEDURE int2std_logic (int : IN INTEGER; std : OUT std_logic_vector);
  FUNCTION std_logic2int (stdin : IN std_logic_vector) RETURN INTEGER;

  PROCEDURE  write_mem_con(l : INOUT LINE; VARIABLE m : IN byte_node_ptr); 
  PROCEDURE  read_mem_con(l : INOUT LINE; m : OUT byte_node_ptr:=NULL); --*********

  FUNCTION int2str (int : INTEGER) RETURN STRING;

END mem_pack_con;

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

PACKAGE BODY mem_pack_con IS

--**********************************************************************

  FUNCTION int2str (int : INTEGER) RETURN STRING IS

    VARIABLE str  : STRING(4 DOWNTO 1) := "    ";
    VARIABLE temp : INTEGER := int;

  BEGIN

    FOR i IN 1 TO 4 LOOP
      str(i) := character'val(character'pos('0')+(temp MOD 10));
      temp:=temp/10;
    END LOOP;

    RETURN str;

  END int2str; 

--**********************************************************************   

  FUNCTION std_logic2int (stdin : IN std_logic_vector) RETURN INTEGER IS

    VARIABLE result : INTEGER;

  BEGIN

    result := 0;

    FOR i IN stdin'RIGHT TO stdin'LEFT LOOP
      IF stdin(i) = '1' THEN
        result := result + 2**(i-stdin'RIGHT);
      END IF;
    END LOOP;

    RETURN result;

  END std_logic2int;

--***********************************************************************
  PROCEDURE int2std_logic (int : IN INTEGER; std : OUT std_logic_vector) IS

    VARIABLE tmp : INTEGER;

  BEGIN

    tmp := int;

    FOR i IN std'RIGHT TO std'LEFT LOOP
      IF (tmp MOD 2  =  1) THEN 
        std (i) := '1';
      ELSE std (i) := '0';
      END IF;
      tmp := tmp / 2;
    END LOOP;

  END int2std_logic;

--************************************************************************

  PROCEDURE write_mem_con(l : INOUT LINE; VARIABLE m : IN byte_node_ptr) IS

    VARIABLE str : STRING(1 TO 9);
    VARIABLE t : byte_node_ptr := m;

  BEGIN
    WHILE t/=NULL LOOP
      FOR i IN 0 TO 7 LOOP
        IF t.val(i)='1' THEN
          str(8-i) := '1';
        ELSIF t.val(i)='0' THEN
          str(8-i) := '0';
        ELSE
          str(8-i) := 'U';
        END IF;
      END LOOP;
      IF t.link/= NULL THEN 
        str(9) := ':';
      ELSE 
        str(9) := ' ';
      END IF;
      write(l,str,RIGHT);
      t:=t.link;
    END LOOP;   
  END write_mem_con;
 
--************************************************************************

  PROCEDURE  read_mem_con(l : INOUT LINE; m : OUT byte_node_ptr:=NULL) IS

    VARIABLE str : STRING(1 TO 9);
    VARIABLE t : byte_node_ptr := NULL;

  BEGIN

    t := NEW byte_node;
    m := t;
    LOOP
      read(l,str);       
      FOR i IN 1 TO 8 LOOP
        IF str(i)='1' THEN
          t.val(8-i) := '1';
        ELSIF str(i)='0' THEN
          t.val(8-i) := '0';
        ELSIF str(i)='U' THEN
          t.val(8-i) := 'U';
        END IF;
      END LOOP;
      IF str(9)=' ' THEN EXIT; END IF;
      t.link := NEW byte_node;
      t := t.link;
    END LOOP;
    t.link := NULL;

  END read_mem_con;

--************************************************************************

END mem_pack_con;