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

LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;

LIBRARY WORK;
USE WORK.mem_pack_con.ALL;
USE WORK.synthesis_utilities.ALL;
USE WORK.global_environment.ALL;
USE WORK.PackCon.ALL;

LIBRARY STD;
USE STD.textio.ALL;

ENTITY ram IS
  
  GENERIC (
            file_name          : STRING (1 TO 11) := "LOOPING.RAM"
          );

  PORT ( read      : IN std_logic := '1';
         write     : IN std_logic := '0';
         dump      : IN std_logic := '0'
       );

  TYPE byte_ptr_vec IS ARRAY (NATURAL RANGE <>) OF byte_node_ptr;

  CONSTANT max : INTEGER := (2**12)-1;
END ram;

ARCHITECTURE mem_con OF ram IS
    
BEGIN
  
  PROCESS (read, write, dump)
    VARIABLE memory : byte_ptr_vec (0 TO max) := (OTHERS => NULL);
    VARIABLE temp, temp2 : byte_node_ptr := NULL;

    VARIABLE first_access  : BOOLEAN :=TRUE;
    FILE     f             : TEXT;  
    VARIABLE l             : LINE;
    VARIABLE status        : FILE_OPEN_STATUS := NAME_ERROR;
       
  BEGIN

    IF dump='1' THEN

      FILE_OPEN(f, file_name, WRITE_MODE);
      FOR i IN 0 TO max LOOP 
        write_mem_con(l,memory(i));
        writeline(f, l);          
      END LOOP;
      FILE_CLOSE(F);                                          
      
    ELSE
      IF first_access THEN 
        FILE_OPEN(status,f, file_name, READ_MODE);
        FOR i IN 0 TO max LOOP
          readline(f, l);  
          read_mem_con(l, memory(i));  
          DEALLOCATE(l); 
        END LOOP;
        FILE_CLOSE(F);
        first_access := FALSE;  
      END IF;

      IF (read ='1') THEN
      --********** just a pointer assignment
        databus := memory(std_logic2int(adbus.val));
      ELSIF (write ='1') THEN
      -- first free
        DeAlloc(memory(std_logic2int(adbus.val)));
        
      -- allocate
        Equate(databus, memory(std_logic2int(adbus.val)));
      END IF;
    END IF; 
  END PROCESS;
END mem_con;