SlideShare a Scribd company logo
--------------------------------------------------------------------------<br />-- Download and rename the file to auxiliary.vhd<br />-- AUB<br />-- EECE 321 - Computer Organization, Spring 2009<br />-- Author: M. M.<br />--------------------------------------------------------------------------<br />-- Description: Auxiliary package including user-defined functions<br />--------------------------------------------------------------------------<br />library ieee;<br />use ieee.std_logic_1164.all;<br />use ieee.std_logic_unsigned.all;<br />use ieee.std_logic_arith.all;<br />use ieee.std_logic_misc.all;<br />USE ieee.std_logic_textio.all;<br />USE std.textio.all;<br />-- ---------------------------------------------------------------------------------<br />-- NOTES:<br />-- ---------------------------------------------------------------------------------<br />-- To convert STD_LOGIC_VECTOR to INTEGER, use the built-in function \"
conv_integer\"
<br />-- CONV_INTEGER(ARG: STD_LOGIC_VECTOR) return INTEGER;<br />-- Ex: if x is a STD_LOGIC_VECTOR, then<br />--     y := conv_integer(x) converts x to unsigned integer<br />-- ---------------------------------------------------------------------------------<br />--<br />-- To convert from INTEGER to STD_LOGIC_VECTOR, use the built-in function \"
conv_std_logic_vector\"
<br />-- CONV_STD_LOGIC_VECTOR(ARG: INTEGER; SIZE: INTEGER) return STD_LOGIC_VECTOR;<br />-- Ex: if y is an integer, then<br />--     x := conv_std_logic_vector(y,4) converts the integer y into a std_logic_vector of width 4<br />-- ---------------------------------------------------------------------------------<br />--<br />-- declare functions and data types here<br />package auxiliary is<br />----------------------------------------------------------------------------------<br />  --  CONSTANTS<br />  ----------------------------------------------------------------------------------<br />CONSTANT MIPS_WORD_SIZE_IN_BYTES : NATURAL := 4;<br />----------------------------------------------------------------------------------<br />  --  DATA TYPES<br />  ----------------------------------------------------------------------------------<br />SUBTYPE MIPS_WORD is STD_LOGIC_VECTOR(MIPS_WORD_SIZE_IN_BYTES * 8 - 1 DOWNTO 0);<br />TYPE MEM_ARRAY is array (INTEGER range <>) of MIPS_WORD;<br />----------------------------------------------------------------------------------<br />  --  USER-DEFINED FUNCTIONS AND PROCEDURES<br />  ----------------------------------------------------------------------------------<br />PROCEDURE read_hex_2_natural(L: INOUT LINE; width: IN NATURAL; natnum : OUT NATURAL);<br />PROCEDURE read_hex_2_word(L: INOUT LINE; width: IN NATURAL; WORD : OUT STD_LOGIC_VECTOR);<br />PROCEDURE init_mem(fname : IN STRING; mem : OUT MEM_ARRAY);<br />PROCEDURE copy_mem_2_file(fname : IN STRING; mem : IN MEM_ARRAY);<br />FUNCTION load_word(mem : IN MEM_ARRAY; byte_addr : IN NATURAL) RETURN MIPS_WORD;<br />PROCEDURE store_word(mem : OUT MEM_ARRAY; word : MIPS_WORD;  byte_addr : IN NATURAL);<br />-- add your functions below ...<br />end auxiliary;<br />package body auxiliary is<br />----------------------------------------------------------------<br />-- Read a hexadecimal string and converts it to a natural number<br />----------------------------------------------------------------<br />PROCEDURE read_hex_2_natural(L: INOUT LINE; width: IN NATURAL; natnum : OUT NATURAL) IS<br />VARIABLE result : NATURAL := 0;<br />VARIABLE ch : CHARACTER;<br />BEGIN<br />FOR i in 1 to width LOOP<br />read(L,ch);<br />IF '0' <= ch AND ch <= '9' THEN<br />result := result*16 + character'pos(ch) - character'pos('0');<br />ELSIF 'A' <= ch AND ch <= 'F' THEN<br />result := result*16 + character'pos(ch) - character'pos('A') + 10;<br />ELSIF 'a' <= ch AND ch <= 'f' THEN<br />result := result*16 + character'pos(ch) - character'pos('a') + 10;<br />ELSE<br />report \"
Format error in input file. Line \"
 & integer'image(i) & \"
. \"
 severity failure;<br />END IF; <br />END LOOP;<br />natnum := result;<br />END read_hex_2_natural;<br />----------------------------------------------------------------<br />-- Read a hexadecimal string and converts it to a STD_LOGIC_VECTOR<br />----------------------------------------------------------------<br />PROCEDURE read_hex_2_word(L: INOUT LINE; width: IN NATURAL; WORD : OUT STD_LOGIC_VECTOR) IS<br />VARIABLE digit : NATURAL;<br />VARIABLE r : NATURAL := 0;<br />VARIABLE ch : CHARACTER;<br />BEGIN<br />-- first check if width of 'word' is equal to width<br />IF width /= (WORD'length)/4 THEN<br />report \"
Input argument width in function 'read_hex_2_vector' incompatible with output width.\"
 severity failure;<br />END IF; <br />FOR i in 1 to width LOOP<br />read(L,ch);<br />IF '0' <= ch AND ch <= '9' THEN<br />digit := character'pos(ch) - character'pos('0');<br />ELSIF 'A' <= ch AND ch <= 'F' THEN<br />digit := character'pos(ch) - character'pos('A') + 10;<br />ELSIF 'a' <= ch AND ch <= 'f' THEN<br />digit := character'pos(ch) - character'pos('a') + 10;<br />ELSE<br />report \"
Format error in input file.\"
 severity failure;<br />END IF; <br />WORD(31-r DOWNTO 28-r) := conv_std_logic_vector(digit,4);<br />r := r + 4;<br />END LOOP;<br />END read_hex_2_word;<br />----------------------------------------------------------------<br />-- Initializes memory from a file<br />----------------------------------------------------------------<br />PROCEDURE init_mem(fname : IN STRING; mem : OUT MEM_ARRAY) IS<br />FILE binary_file: text;<br />VARIABLE open_status : file_open_status;<br />VARIABLE L: LINE;<br />VARIABLE ch : CHARACTER;<br />VARIABLE  line_number   : NATURAL := 0;<br />VARIABLE  addr   : NATURAL;<br />VARIABLE  word          : MIPS_WORD;<br />BEGIN<br />-- open file<br />file_open(open_status, binary_file, fname, read_mode);<br />-- check status, report error in case something is wrong with input simulation file<br />IF open_status /= open_ok then<br />report file_open_status'image(open_status) & \"
 WHILE opening file \"
 & fname<br />      severity failure;   <br />    END IF;<br />-- First, write zeros in all entries<br />FOR i in mem'RANGE LOOP     <br />mem(i) := (OTHERS => '0');<br />END LOOP;<br />-- Fill entries of memory from the input file<br />WHILE NOT endfile(binary_file) LOOP<br />readline(binary_file, L);<br />line_number := line_number + 1;<br />read_hex_2_natural(L,8,addr);<br />read(L,ch); -- space between address and data<br />read_hex_2_word(L,8,word);<br />mem(addr/4) := word;<br />END LOOP;<br />    <br />END init_mem;<br />----------------------------------------------------------------<br />-- Copies the contents of memory into a file<br />----------------------------------------------------------------<br />PROCEDURE copy_mem_2_file(fname : IN STRING; mem : IN MEM_ARRAY) IS<br />FILE binary_file: text;<br />VARIABLE open_status : file_open_status;<br />VARIABLE L: LINE;<br />VARIABLE  line_number   : NATURAL := 0;<br />VARIABLE  addr   : MIPS_WORD;<br />VARIABLE  word          : MIPS_WORD;<br />VARIABLE  nibble        : STD_LOGIC_VECTOR(3 DOWNTO 0);<br />BEGIN<br />-- open file<br />file_open(open_status, binary_file, fname, write_mode);<br />-- check status, report error in case something is wrong with input simulation file<br />IF open_status /= open_ok then<br />report file_open_status'image(open_status) & \"
 WHILE opening file \"
 & fname<br />      severity failure;   <br />    END IF;<br />-- Fill entries of memory from the input file<br />FOR j in mem'range LOOP<br />-- assemble address field in hexadecimal<br />addr := conv_std_logic_vector(4*line_number,addr'length);<br />for i in 0 to 7 loop<br />nibble := addr(31-4*i downto 28-4*i); <br />hwrite(L,nibble,FIELD=>1,JUSTIFIED=>left);<br />end loop;<br />-- fill space<br />write(L,STRING'(\"
 \"
));<br />-- assemble data field in hexadecimal<br />word := mem(line_number);<br />for i in 0 to 7 loop<br />nibble := word(31-4*i downto 28-4*i);<br />hwrite(L,nibble,FIELD=>1,JUSTIFIED=>left);<br />end loop;<br />-- advance line number<br />line_number := line_number + 1;<br />-- print line to output file<br />writeline(binary_file,L);<br />END LOOP;<br />    <br />END copy_mem_2_file;<br />----------------------------------------------------------------<br />-- Loads a word from memory (address is byte address)<br />----------------------------------------------------------------<br />FUNCTION load_word(mem : IN MEM_ARRAY; byte_addr : IN NATURAL) RETURN MIPS_WORD IS<br />VARIABLE  word  : MIPS_WORD;<br />BEGIN<br />-- check byte address falls in memory range or is a multiple of the number of bytes within a word<br />IF (byte_addr < 0) OR <br />(byte_addr >= MIPS_WORD_SIZE_IN_BYTES * mem'length) OR<br />(byte_addr mod MIPS_WORD_SIZE_IN_BYTES /= 0)THEN<br />report \"
 Invalid byte address in function 'load_word': \"
 & integer'image(byte_addr)<br />      severity failure;   <br />    END IF; <br />word := mem(byte_addr/4);  <br />return word;<br />    <br />END load_word;<br />----------------------------------------------------------------<br />-- Stores a word into memory (address is byte address)<br />----------------------------------------------------------------<br />PROCEDURE store_word(mem : OUT MEM_ARRAY; word : MIPS_WORD;  byte_addr : IN NATURAL) IS<br />BEGIN<br />-- check byte address falls in memory range or is a multiple of the number of bytes within a word<br />IF (byte_addr < 0) OR <br />(byte_addr >= MIPS_WORD_SIZE_IN_BYTES * mem'length) OR<br />(byte_addr mod MIPS_WORD_SIZE_IN_BYTES /= 0)THEN<br />report \"
 Invalid byte address in function 'store_word': \"
 & integer'image(byte_addr)<br />      severity failure;   <br />    END IF; <br />mem(byte_addr/4) := word;  <br />END store_word;<br />END auxiliary;<br />
Auxiliary
Auxiliary
Auxiliary
Auxiliary
Auxiliary
Auxiliary
Auxiliary
Auxiliary
Auxiliary

More Related Content

PPT
01c shell
PPTX
File handling in c language
ODP
The bones of a nice Python script
PPT
Unit vii wp ppt
PDF
Regexp Master
PDF
runtimestack
DOCX
Data structures
01c shell
File handling in c language
The bones of a nice Python script
Unit vii wp ppt
Regexp Master
runtimestack
Data structures

What's hot (20)

PPT
Learning sed and awk
PDF
Advanced perl finer points ,pack&amp;unpack,eval,files
DOC
Unit v
PPTX
Raspberry pi Part 25
PDF
Perl6 for-beginners
PDF
CyberLink LabelPrint 2.5 Exploitation Process
PPT
PDF
Implementing Software Machines in Go and C
PDF
Python and sysadmin I
PDF
Foxpro (1)
PDF
Argparse: Python command line parser
PPTX
Exploit Development: EzServer Buffer Overflow oleh Tom Gregory
DOCX
32 shell-programming
PPT
101 3.1 gnu and unix commands v4
TXT
Newdoc maxis
PPT
3.1 gnu and unix commands v4
PPT
Unit5
PPTX
C Homework Help
Learning sed and awk
Advanced perl finer points ,pack&amp;unpack,eval,files
Unit v
Raspberry pi Part 25
Perl6 for-beginners
CyberLink LabelPrint 2.5 Exploitation Process
Implementing Software Machines in Go and C
Python and sysadmin I
Foxpro (1)
Argparse: Python command line parser
Exploit Development: EzServer Buffer Overflow oleh Tom Gregory
32 shell-programming
101 3.1 gnu and unix commands v4
Newdoc maxis
3.1 gnu and unix commands v4
Unit5
C Homework Help
Ad

Similar to Auxiliary (20)

PPT
Python - Getting to the Essence - Points.com - Dave Park
DOCX
Lab11.cppLab11.cpp.docx
PPT
Introduction to Assembly Language
PPTX
C to perl binding
PDF
IO Streams, Files and Directories
DOC
Program For Parsing2
PDF
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
ODP
perl usage at database applications
PDF
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
PPT
file_handling_in_c.ppt
PPT
The Ruby On Rails I18n Core Api
PPTX
Apache pig
PDF
Getting Started with Raspberry Pi - DCC 2013.1
PDF
Phil Bartie QGIS PLPython
ODP
Lua by Ong Hean Kuan
PPTX
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
PDF
Having Fun Programming!
PPTX
AWS Hadoop and PIG and overview
PPT
About Go
PPT
Perl Presentation
Python - Getting to the Essence - Points.com - Dave Park
Lab11.cppLab11.cpp.docx
Introduction to Assembly Language
C to perl binding
IO Streams, Files and Directories
Program For Parsing2
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
perl usage at database applications
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
file_handling_in_c.ppt
The Ruby On Rails I18n Core Api
Apache pig
Getting Started with Raspberry Pi - DCC 2013.1
Phil Bartie QGIS PLPython
Lua by Ong Hean Kuan
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Having Fun Programming!
AWS Hadoop and PIG and overview
About Go
Perl Presentation
Ad

More from ececourse (14)

PPT
Chapter 5 c
PPT
Chapter 5 b
PPT
Chapter 5 a
PPT
Chapter 4
PPT
Chapter 3
DOCX
Mem Tb
DOC
Machine Problem 2
DOC
Machine Problem 1
PPT
Chapter 2 Hw
PPT
Chapter 2 Part2 C
PPT
C:\Fakepath\Chapter 2 Part2 B
PPT
Chapter 2 Part2 A
PPT
Chapter1
PPT
Chapter 2 Part1
Chapter 5 c
Chapter 5 b
Chapter 5 a
Chapter 4
Chapter 3
Mem Tb
Machine Problem 2
Machine Problem 1
Chapter 2 Hw
Chapter 2 Part2 C
C:\Fakepath\Chapter 2 Part2 B
Chapter 2 Part2 A
Chapter1
Chapter 2 Part1

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Advanced IT Governance
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
Modernizing your data center with Dell and AMD
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Cloud computing and distributed systems.
20250228 LYD VKU AI Blended-Learning.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Advanced Soft Computing BINUS July 2025.pdf
Advanced IT Governance
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Machine learning based COVID-19 study performance prediction
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Modernizing your data center with Dell and AMD
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Chapter 3 Spatial Domain Image Processing.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Review of recent advances in non-invasive hemoglobin estimation
GamePlan Trading System Review: Professional Trader's Honest Take
NewMind AI Monthly Chronicles - July 2025
Diabetes mellitus diagnosis method based random forest with bat algorithm
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Auxiliary

  • 1. --------------------------------------------------------------------------<br />-- Download and rename the file to auxiliary.vhd<br />-- AUB<br />-- EECE 321 - Computer Organization, Spring 2009<br />-- Author: M. M.<br />--------------------------------------------------------------------------<br />-- Description: Auxiliary package including user-defined functions<br />--------------------------------------------------------------------------<br />library ieee;<br />use ieee.std_logic_1164.all;<br />use ieee.std_logic_unsigned.all;<br />use ieee.std_logic_arith.all;<br />use ieee.std_logic_misc.all;<br />USE ieee.std_logic_textio.all;<br />USE std.textio.all;<br />-- ---------------------------------------------------------------------------------<br />-- NOTES:<br />-- ---------------------------------------------------------------------------------<br />-- To convert STD_LOGIC_VECTOR to INTEGER, use the built-in function \" conv_integer\" <br />-- CONV_INTEGER(ARG: STD_LOGIC_VECTOR) return INTEGER;<br />-- Ex: if x is a STD_LOGIC_VECTOR, then<br />-- y := conv_integer(x) converts x to unsigned integer<br />-- ---------------------------------------------------------------------------------<br />--<br />-- To convert from INTEGER to STD_LOGIC_VECTOR, use the built-in function \" conv_std_logic_vector\" <br />-- CONV_STD_LOGIC_VECTOR(ARG: INTEGER; SIZE: INTEGER) return STD_LOGIC_VECTOR;<br />-- Ex: if y is an integer, then<br />-- x := conv_std_logic_vector(y,4) converts the integer y into a std_logic_vector of width 4<br />-- ---------------------------------------------------------------------------------<br />--<br />-- declare functions and data types here<br />package auxiliary is<br />----------------------------------------------------------------------------------<br /> -- CONSTANTS<br /> ----------------------------------------------------------------------------------<br />CONSTANT MIPS_WORD_SIZE_IN_BYTES : NATURAL := 4;<br />----------------------------------------------------------------------------------<br /> -- DATA TYPES<br /> ----------------------------------------------------------------------------------<br />SUBTYPE MIPS_WORD is STD_LOGIC_VECTOR(MIPS_WORD_SIZE_IN_BYTES * 8 - 1 DOWNTO 0);<br />TYPE MEM_ARRAY is array (INTEGER range <>) of MIPS_WORD;<br />----------------------------------------------------------------------------------<br /> -- USER-DEFINED FUNCTIONS AND PROCEDURES<br /> ----------------------------------------------------------------------------------<br />PROCEDURE read_hex_2_natural(L: INOUT LINE; width: IN NATURAL; natnum : OUT NATURAL);<br />PROCEDURE read_hex_2_word(L: INOUT LINE; width: IN NATURAL; WORD : OUT STD_LOGIC_VECTOR);<br />PROCEDURE init_mem(fname : IN STRING; mem : OUT MEM_ARRAY);<br />PROCEDURE copy_mem_2_file(fname : IN STRING; mem : IN MEM_ARRAY);<br />FUNCTION load_word(mem : IN MEM_ARRAY; byte_addr : IN NATURAL) RETURN MIPS_WORD;<br />PROCEDURE store_word(mem : OUT MEM_ARRAY; word : MIPS_WORD; byte_addr : IN NATURAL);<br />-- add your functions below ...<br />end auxiliary;<br />package body auxiliary is<br />----------------------------------------------------------------<br />-- Read a hexadecimal string and converts it to a natural number<br />----------------------------------------------------------------<br />PROCEDURE read_hex_2_natural(L: INOUT LINE; width: IN NATURAL; natnum : OUT NATURAL) IS<br />VARIABLE result : NATURAL := 0;<br />VARIABLE ch : CHARACTER;<br />BEGIN<br />FOR i in 1 to width LOOP<br />read(L,ch);<br />IF '0' <= ch AND ch <= '9' THEN<br />result := result*16 + character'pos(ch) - character'pos('0');<br />ELSIF 'A' <= ch AND ch <= 'F' THEN<br />result := result*16 + character'pos(ch) - character'pos('A') + 10;<br />ELSIF 'a' <= ch AND ch <= 'f' THEN<br />result := result*16 + character'pos(ch) - character'pos('a') + 10;<br />ELSE<br />report \" Format error in input file. Line \" & integer'image(i) & \" . \" severity failure;<br />END IF; <br />END LOOP;<br />natnum := result;<br />END read_hex_2_natural;<br />----------------------------------------------------------------<br />-- Read a hexadecimal string and converts it to a STD_LOGIC_VECTOR<br />----------------------------------------------------------------<br />PROCEDURE read_hex_2_word(L: INOUT LINE; width: IN NATURAL; WORD : OUT STD_LOGIC_VECTOR) IS<br />VARIABLE digit : NATURAL;<br />VARIABLE r : NATURAL := 0;<br />VARIABLE ch : CHARACTER;<br />BEGIN<br />-- first check if width of 'word' is equal to width<br />IF width /= (WORD'length)/4 THEN<br />report \" Input argument width in function 'read_hex_2_vector' incompatible with output width.\" severity failure;<br />END IF; <br />FOR i in 1 to width LOOP<br />read(L,ch);<br />IF '0' <= ch AND ch <= '9' THEN<br />digit := character'pos(ch) - character'pos('0');<br />ELSIF 'A' <= ch AND ch <= 'F' THEN<br />digit := character'pos(ch) - character'pos('A') + 10;<br />ELSIF 'a' <= ch AND ch <= 'f' THEN<br />digit := character'pos(ch) - character'pos('a') + 10;<br />ELSE<br />report \" Format error in input file.\" severity failure;<br />END IF; <br />WORD(31-r DOWNTO 28-r) := conv_std_logic_vector(digit,4);<br />r := r + 4;<br />END LOOP;<br />END read_hex_2_word;<br />----------------------------------------------------------------<br />-- Initializes memory from a file<br />----------------------------------------------------------------<br />PROCEDURE init_mem(fname : IN STRING; mem : OUT MEM_ARRAY) IS<br />FILE binary_file: text;<br />VARIABLE open_status : file_open_status;<br />VARIABLE L: LINE;<br />VARIABLE ch : CHARACTER;<br />VARIABLE line_number : NATURAL := 0;<br />VARIABLE addr : NATURAL;<br />VARIABLE word : MIPS_WORD;<br />BEGIN<br />-- open file<br />file_open(open_status, binary_file, fname, read_mode);<br />-- check status, report error in case something is wrong with input simulation file<br />IF open_status /= open_ok then<br />report file_open_status'image(open_status) & \" WHILE opening file \" & fname<br /> severity failure; <br /> END IF;<br />-- First, write zeros in all entries<br />FOR i in mem'RANGE LOOP <br />mem(i) := (OTHERS => '0');<br />END LOOP;<br />-- Fill entries of memory from the input file<br />WHILE NOT endfile(binary_file) LOOP<br />readline(binary_file, L);<br />line_number := line_number + 1;<br />read_hex_2_natural(L,8,addr);<br />read(L,ch); -- space between address and data<br />read_hex_2_word(L,8,word);<br />mem(addr/4) := word;<br />END LOOP;<br /> <br />END init_mem;<br />----------------------------------------------------------------<br />-- Copies the contents of memory into a file<br />----------------------------------------------------------------<br />PROCEDURE copy_mem_2_file(fname : IN STRING; mem : IN MEM_ARRAY) IS<br />FILE binary_file: text;<br />VARIABLE open_status : file_open_status;<br />VARIABLE L: LINE;<br />VARIABLE line_number : NATURAL := 0;<br />VARIABLE addr : MIPS_WORD;<br />VARIABLE word : MIPS_WORD;<br />VARIABLE nibble : STD_LOGIC_VECTOR(3 DOWNTO 0);<br />BEGIN<br />-- open file<br />file_open(open_status, binary_file, fname, write_mode);<br />-- check status, report error in case something is wrong with input simulation file<br />IF open_status /= open_ok then<br />report file_open_status'image(open_status) & \" WHILE opening file \" & fname<br /> severity failure; <br /> END IF;<br />-- Fill entries of memory from the input file<br />FOR j in mem'range LOOP<br />-- assemble address field in hexadecimal<br />addr := conv_std_logic_vector(4*line_number,addr'length);<br />for i in 0 to 7 loop<br />nibble := addr(31-4*i downto 28-4*i); <br />hwrite(L,nibble,FIELD=>1,JUSTIFIED=>left);<br />end loop;<br />-- fill space<br />write(L,STRING'(\" \" ));<br />-- assemble data field in hexadecimal<br />word := mem(line_number);<br />for i in 0 to 7 loop<br />nibble := word(31-4*i downto 28-4*i);<br />hwrite(L,nibble,FIELD=>1,JUSTIFIED=>left);<br />end loop;<br />-- advance line number<br />line_number := line_number + 1;<br />-- print line to output file<br />writeline(binary_file,L);<br />END LOOP;<br /> <br />END copy_mem_2_file;<br />----------------------------------------------------------------<br />-- Loads a word from memory (address is byte address)<br />----------------------------------------------------------------<br />FUNCTION load_word(mem : IN MEM_ARRAY; byte_addr : IN NATURAL) RETURN MIPS_WORD IS<br />VARIABLE word : MIPS_WORD;<br />BEGIN<br />-- check byte address falls in memory range or is a multiple of the number of bytes within a word<br />IF (byte_addr < 0) OR <br />(byte_addr >= MIPS_WORD_SIZE_IN_BYTES * mem'length) OR<br />(byte_addr mod MIPS_WORD_SIZE_IN_BYTES /= 0)THEN<br />report \" Invalid byte address in function 'load_word': \" & integer'image(byte_addr)<br /> severity failure; <br /> END IF; <br />word := mem(byte_addr/4); <br />return word;<br /> <br />END load_word;<br />----------------------------------------------------------------<br />-- Stores a word into memory (address is byte address)<br />----------------------------------------------------------------<br />PROCEDURE store_word(mem : OUT MEM_ARRAY; word : MIPS_WORD; byte_addr : IN NATURAL) IS<br />BEGIN<br />-- check byte address falls in memory range or is a multiple of the number of bytes within a word<br />IF (byte_addr < 0) OR <br />(byte_addr >= MIPS_WORD_SIZE_IN_BYTES * mem'length) OR<br />(byte_addr mod MIPS_WORD_SIZE_IN_BYTES /= 0)THEN<br />report \" Invalid byte address in function 'store_word': \" & integer'image(byte_addr)<br /> severity failure; <br /> END IF; <br />mem(byte_addr/4) := word; <br />END store_word;<br />END auxiliary;<br />