OSDN Git Service

2dededc86d83c4d9bdceaaf8b6216a11a3409c49
[motonesfpga/motonesfpga.git] / de1_nes / mem / chr_rom.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_unsigned.conv_integer;
4 use ieee.std_logic_arith.conv_std_logic_vector;
5 use std.textio.all;
6 use work.motonesfpga_common.all;
7
8 --asyncronous rom
9 entity chr_rom is 
10     generic (abus_size : integer := 13; dbus_size : integer := 8);
11     port (  
12             clk             : in std_logic;
13             ce_n            : in std_logic;     --active low.
14             addr            : in std_logic_vector (abus_size - 1 downto 0);
15             data            : out std_logic_vector (dbus_size - 1 downto 0);
16             nt_v_mirror     : out std_logic
17         );
18 end chr_rom;
19
20 architecture rtl of chr_rom is
21
22 --CHR ROM is 8k
23 subtype rom_data is std_logic_vector (dbus_size -1 downto 0);
24 type rom_array is array (0 to 2**abus_size - 1) of rom_data;
25
26 --function is called only once at the array initialize.
27 function rom_fill return rom_array is 
28     type binary_file is file of character;
29     FILE nes_file : binary_file OPEN read_mode IS "rom-file.nes" ;
30     variable read_data : character;
31     variable i : integer;
32     variable ret : rom_array;
33     begin
34         --skip first 16 bit data(NES cardridge header part.)
35         for i in 0 to 15 loop
36             read(nes_file, read_data);
37         end loop;
38         --skip program ROM part
39         for i in 0 to 16#8000# - 1 loop
40             read(nes_file, read_data);
41         end loop;
42
43         for i in ret'range loop
44             read(nes_file, read_data);
45             ret(i) :=
46                 conv_std_logic_vector(character'pos(read_data), 8);
47         end loop;
48         d_print("chr rom file load success.");
49         return ret;
50     end rom_fill;
51
52 function vmirror return std_logic is 
53     type binary_file is file of character;
54     FILE nes_file : binary_file OPEN read_mode IS "rom-file.nes" ;
55     variable read_data : character;
56     variable i : integer;
57     variable ret : std_logic_vector(7 downto 0);
58     begin
59         --read NES cardridge header part
60         for i in 0 to 15 loop
61             read(nes_file, read_data);
62             if (i = 6) then
63                 ret :=
64                     conv_std_logic_vector(character'pos(read_data), 8);
65             end if;
66         end loop;
67         d_print("nes header read ok.");
68         return ret(0);
69     end vmirror;
70
71 --for GHDL environment
72 --itinialize with the rom_fill function.
73 --signal p_rom : rom_array := rom_fill;
74
75 --for Quartus II environment
76 signal p_rom : rom_array;
77 attribute ram_init_file : string;
78 attribute ram_init_file of p_rom : signal is "sample1-chr.hex";
79
80 begin
81     
82     --nt_v_mirror <= vmirror;
83     nt_v_mirror <= '1';
84
85     p : process (clk)
86     begin
87     if (rising_edge(clk)) then
88         if (ce_n = '0') then
89             data <= p_rom(conv_integer(addr));
90         else
91             data <= (others => 'Z');
92         end if;
93     end if;
94     end process;
95 end rtl;
96