?? memorylib.vhd
字號:
-- -- _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/-- _/ _/ ____________________________________________ -- _/ _/ / / -- _/_/ _/ / NAND01GR3B / -- _/_/_/ _/ / / -- _/_/ _/ / 1Gbit / -- _/_/ _/ / 8 bit, 2112 Byte Page, 1.8 V, NAND / -- _/ _/ / / -- _/ _/ / VHDL Behavioral Model / -- _/ _/ / Version 3.0 / -- _/_/ _/ / /-- _/_/_/ _/ / Copyright (c) 2006 STMicroelectronics / -- _/_/_/ _/ /___________________________________________/ -- _/_/_/_/_/ _/ -- --
----------------------------------------------------------------------------------------
-- MEMORY LIST PACKAGE --
----------------------------------------------------------------------------------------
-- --
-- Implements the memory array by a double list of memory cells. --
-- Each cell is linked to the next cell and to the previous cell. --
-- Is possible to acces to the head of the list, to the tail and also to the middle. --
-- --
----------------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_TextIO.all;
use IEEE.Std_Logic_Arith.all;
library Std;
use STD.TextIO.all;
library Work;
use work.data.all;
use work.StringLib.all;
package MemoryLib is
type memory_cell; -- the memory cell
type cell_pointer is access memory_cell; -- the pointer to the memory cell
type memory_cell is record
prev, nexto: cell_pointer; -- pointers to the previous cell and to the next cell
value : DataMem_type;
endAddr : AddrMem_Type;
end record;
type modality is (reading, writing, erasing); -- reading the memory, writing in the memory or erasing the memory ?
type balance is (left, centre, right); -- middle is balanced ?
type memory_rec is record
head, middle, tail: cell_pointer;
bal_value: balance;
end record;
constant FF: DataMem_type:= (Others => '1');
------------------------------------------------------------------------
------- Public Procedures: they can be used in the model -------------
------------------------------------------------------------------------
-- Used to init the memory list
procedure InitMemory(variable memory: inout memory_rec);
-- Used to write or to erase the memory
procedure putMemory(variable memory: inout memory_rec; variable add: in AddrMem_type; variable data: in DataMem_type);
-- Used to read a memory cell
procedure getMemory(variable memory: in memory_rec; variable add: in AddrMem_type; variable data: out DataMem_type);
-- Used to read a vector of memory cells
procedure getMemoryBuffer(variable memory: in memory_rec; variable firstAdd: in AddrMem_type; variable numData: in natural; variable dataVector: out MemBuffer_type);
-- Used to load a memory file in the memory list
procedure LoadMemoryFile(FileName: in String; variable memory: inout memory_rec);
-- Used to save into a file the memory list
procedure SaveMemoryFile(FileName: in String; variable memory: in memory_rec);
-- Used to display the memory list
procedure PrintList(variable memory: in memory_rec);
-- Used to display the two passed values
procedure PrintData(variable add:in AddrMem_type; variable data: in DataMem_type);
--------------------------------------------------------------------------
------ Private Procedures: they must be used only in this package ------
--------------------------------------------------------------------------
-- Insert a cell to the head of the memory list
procedure AddCellToHead(variable head: inout cell_pointer; variable add: in AddrMem_type; variable data: in DataMem_type; variable insdel_num: inout integer);
-- Add a cell to the tail of the memory list
procedure AddCellToTail(variable tail: inout cell_pointer; variable data: in DataMem_type; variable insdel_num: inout integer);
-- Erase a cell of the memory list
procedure EraseCell(variable memory: inout memory_rec; variable pointer: inout cell_pointer; variable insdel_num: inout integer);
-- Erase the head of the memory list
procedure EraseHead(variable head_pnt: inout cell_pointer; variable insdel_num: inout integer);
-- Erase the tail of the memory list
procedure EraseTail(variable tail: inout cell_pointer; variable insdel_num: inout integer);
-- Insert a cell in the memory list
procedure InsertCell(variable pointer: in cell_pointer; variable add: in AddrMem_type; variable data: in DataMem_type; variable insdel_num: inout integer);
-- Return an error message when you try to write into a not empty cell
procedure WriteError;
-- Return an error message if the specified address is out of range
procedure AddressError;
-- Update the position of the middle pointer of the memory list
procedure UpdateMiddle(variable memory: inout memory_rec; variable add: in AddrMem_type; variable mode: in modality; variable insdel_num: inout integer);
-- Search for the right position for a read or an update operation
procedure SearchForRightPosition(variable middle: in cell_pointer; variable pnt: inout cell_pointer; variable next_pnt: out cell_pointer; variable add: in AddrMem_type; variable mode: in modality; variable writeError: out boolean);
end MemoryLib;
----------------------------------------------------------------
-------- Package Body - procedures implementation ------------
----------------------------------------------------------------
package body MemoryLib is
------------------------------------------------
------- Initialization of the memory ----------
------------------------------------------------
procedure InitMemory(variable memory: inout memory_rec) is
variable pointer: cell_pointer;
begin
pointer:= new memory_cell;
memory.head:= pointer;
memory.middle:= pointer; -- pointer to the middle of the list
memory.tail:= pointer;
memory.bal_value:= centre; -- the memory is balanced
pointer.value:= (others => '1');
pointer.endAddr:= Last_Addr;
pointer.prev:= null;
pointer.nexto:= null;
end;
----------------------------------------------------
--------- Print only a value of the list ---------
----------------------------------------------------
procedure PrintData(variable add: in AddrMem_type; variable data: in DataMem_type) is
variable msg: line;
begin
write(msg, String'(" address = "));
write(msg, add);
write(msg, String'(" "));
write(msg, String'(" data = "));
write(msg, data);
writeline(output, msg);
end;
----------------------------------------------------
--------- Print the values of the list -----------
----------------------------------------------------
procedure PrintList(variable memory: in memory_rec) is
variable pnt: cell_pointer;
variable msg: line;
begin
pnt:= memory.head;
while pnt /= null loop
write(msg, pnt.value);
write(msg, String'(" "));
write(msg, pnt.endAddr);
writeline(output,msg);
pnt:= pnt.nexto;
end loop;
pnt:= memory.middle;
write(msg, String'(" middle.value = "));
write(msg, pnt.value);
write(msg, String'(" middle.endAddr = "));
write(msg, pnt.endAddr);
write(msg, String'(" balance = "));
if memory.bal_value = left then
write(msg, String'("left"));
elsif memory.bal_value = centre then
write(msg, String'("centre"));
else
write(msg, String'("right"));
end if;
writeline(output, msg);
end;
-----------------------------------------------------
---------- Searching in the memory list -----------
-----------------------------------------------------
procedure SearchForRightPosition(variable middle: in cell_pointer; variable pnt: inout cell_pointer; variable next_pnt: out cell_pointer; variable add: in AddrMem_type; variable mode: in modality; variable writeError: out boolean) is
variable search_pnt: cell_pointer;
begin
search_pnt:= middle; -- searching starts from middle of the list
pnt:= search_pnt;
writeError:= false;
if add < middle.endAddr then -- search direction is left
while add < search_pnt.endAddr
loop
search_pnt:= search_pnt.prev;
end loop;
if add > search_pnt.endAddr then
if mode = writing then
pnt:= search_pnt.nexto;
next_pnt := pnt.nexto;
else
pnt:= null; -- mode = erasing or mode = reading; the cell is already to FF
next_pnt := search_pnt.nexto;
end if;
writeError:= false;
else -- add = search_pnt.endAddr
if mode = writing then
if search_pnt.value = FF then
search_pnt.endAddr:= search_pnt.endAddr - 1;
pnt:= search_pnt.nexto;
writeError:= false;
--elsif mode = reading then pnt:= search_pnt; -- the value not is FF
else
pnt:= search_pnt;
writeError:= true; -- address is already written in the memory
end if;
else -- mode = erasing or reading
pnt:= search_pnt;
writeError:= false;
end if;
next_pnt := pnt.nexto;
end if;
else -- search direction is right
while add > search_pnt.endAddr
loop
search_pnt:= search_pnt.nexto;
end loop;
if add < search_pnt.endAddr then -- pnt:= search_pnt;
if mode = writing then
pnt:= search_pnt;
else
pnt:= null; -- mode = erasing or mode = reading; the cell is already to FF
end if;
next_pnt:= search_pnt;
writeError:= false;
else -- add = search_pnt.endAddr
if mode = writing then
if search_pnt.value = FF then
search_pnt.endAddr:= search_pnt.endAddr - 1;
pnt:= search_pnt.nexto;
--elsif mode = reading then pnt:= search_pnt;
else
pnt:= search_pnt;
writeError:= true; -- address is already written in the memory
end if;
else
pnt:= search_pnt; -- mode = erasing or reading
writeError:= false;
end if;
next_pnt := search_pnt.nexto;
end if;
end if;
-- In each case the right position is between pnt and pnt.prev
end;
---------------------------------------------------------
-------- Insert a cell in the memory list -------------
---------------------------------------------------------
procedure InsertCell(variable pointer: in cell_pointer; variable add: in AddrMem_type; variable data: in DataMem_type; variable insdel_num: inout integer) is
variable ins_pnt, prev_pnt: cell_pointer;
variable i: integer;
variable myadd: AddrMem_type;
variable mydata: DataMem_type;
variable mypointer: cell_pointer;
begin
-- Insert the new two cells between pnt and pnt.prev
myadd:= add;
mydata:= data;
mypointer:= pointer;
for i in 1 to 2 loop
ins_pnt:= new memory_cell;
prev_pnt:= mypointer.prev;
ins_pnt.value:= mydata;
ins_pnt.endAddr:= myadd;
ins_pnt.nexto:= mypointer;
ins_pnt.prev:= prev_pnt;
prev_pnt.nexto:= ins_pnt;
mypointer.prev:= ins_pnt;
if (i = 1) then
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -