--------------------------------------------------------------------------<br />-- Download and rename the file to auxilia...
Auxiliary
Auxiliary
Auxiliary
Auxiliary
Auxiliary
Auxiliary
Auxiliary
Auxiliary
Auxiliary
Upcoming SlideShare
Loading in …5
×

Auxiliary

392 views
364 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
392
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Auxiliary

  1. 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 />

×