OpenCores
URL https://opencores.org/ocsvn/spacewire_light/spacewire_light/trunk

Subversion Repositories spacewire_light

[/] [spacewire_light/] [trunk/] [bench/] [vhdl/] [ahbram_loadfile.vhd] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 jorisvr
--
2
-- AHB slave simulating random access memory.
3
-- Initial contents are loaded from an SREC file at the start of the simulation.
4
--
5
 
6
 
7
library ieee;
8
use ieee.std_logic_1164.all, ieee.numeric_std.all;
9
use std.textio.all;
10
library grlib;
11
use grlib.amba.all;
12
use grlib.devices.all;
13
use grlib.stdlib.all;
14
 
15
 
16
entity ahbram_loadfile is
17
 
18
    generic (
19
        hindex:     integer;
20
        haddr:      integer;
21
        hmask:      integer := 16#fff#;
22
        abits:      integer range 10 to 24;
23
        fname:      string );
24
 
25
    port (
26
        rstn:       in  std_logic;
27
        clk:        in  std_logic;
28
        ahbi:       in  ahb_slv_in_type;
29
        ahbo:       out ahb_slv_out_type );
30
 
31
end entity ahbram_loadfile;
32
 
33
architecture ahbram_arch of ahbram_loadfile is
34
 
35
    type mem_type is array(natural range <>) of std_logic_vector(31 downto 0);
36
    signal mem: mem_type(0 to (2**(abits-2)-1));
37
 
38
    signal s_load:  std_ulogic := '1';
39
    signal s_rdata: std_logic_vector(31 downto 0) := (others => '0');
40 12 jorisvr
    signal s_wdata: std_logic_vector(31 downto 0) := (others => '0');
41 5 jorisvr
    signal s_ready: std_ulogic := '0';
42
    signal s_write: std_ulogic := '0';
43
    signal s_waddr: std_logic_vector(31 downto 0) := (others => '0');
44
    signal s_wsize: std_logic_vector(2 downto 0)  := "000";
45
 
46
    constant hconfig : ahb_config_type := (
47
 
48
        4 => ahb_membar(haddr, '1', '1', hmask),
49
        others => zero32);
50
 
51
    function fromhex(s: string) return unsigned is
52
        variable v: unsigned(31 downto 0);
53
        variable t: unsigned(3 downto 0);
54
    begin
55
        v := to_unsigned(0, 32);
56
        for i in s'range loop
57
            case s(i) is
58
                when '0' => t := "0000";
59
                when '1' => t := "0001";
60
                when '2' => t := "0010";
61
                when '3' => t := "0011";
62
                when '4' => t := "0100";
63
                when '5' => t := "0101";
64
                when '6' => t := "0110";
65
                when '7' => t := "0111";
66
                when '8' => t := "1000";
67
                when '9' => t := "1001";
68
                when 'a' => t := "1010";
69
                when 'A' => t := "1010";
70
                when 'b' => t := "1011";
71
                when 'B' => t := "1011";
72
                when 'c' => t := "1100";
73
                when 'C' => t := "1100";
74
                when 'd' => t := "1101";
75
                when 'D' => t := "1101";
76
                when 'e' => t := "1110";
77
                when 'E' => t := "1110";
78
                when 'f' => t := "1111";
79
                when 'F' => t := "1111";
80
                when others => assert false report "invalid syntax in SREC file";
81
            end case;
82
            v := v(27 downto 0) & t;
83
        end loop;
84
        return v;
85
    end function;
86
 
87
begin
88
 
89
    ahbo.hready     <= s_ready;
90
    ahbo.hresp      <= HRESP_OKAY;
91 12 jorisvr
    ahbo.hrdata     <= ahbdrivedata(s_rdata);
92 5 jorisvr
    ahbo.hsplit     <= (others => '0');
93
    ahbo.hirq       <= (others => '0');
94
    ahbo.hconfig    <= hconfig;
95
    ahbo.hindex     <= hindex;
96
 
97 12 jorisvr
    s_wdata         <= ahbreadword(ahbi.hwdata, s_waddr(4 downto 2));
98
 
99 5 jorisvr
    process (clk) is
100
 
101
        procedure loadfile is
102
            file fd: text open read_mode is fname;
103
            variable lin: line;
104
            variable c0, c1, c2, c3, c4, c5, c6, c7: character;
105
            variable n, t: integer;
106
            variable adr: unsigned(31 downto 0);
107
            variable dat: unsigned(31 downto 0);
108
        begin
109
            for i in mem'range loop
110
                mem(i) <= zero32;
111
            end loop;
112
            while not endfile(fd) loop
113
                readline(fd, lin);
114
                read(lin, c0);
115
                if c0 = 'S' then
116
                    read(lin, c0);
117
                    if c0 = '1' or c0 = '2' or c0 = '3' then
118
                        t := to_integer(fromhex(c0 & ""));
119
                        read(lin, c0);
120
                        read(lin, c1);
121
                        n := to_integer(fromhex((c0, c1))) - t - 2;
122
                        assert n >= 0 and (n rem 4) = 0 report "invalid record length in SREC file";
123
                        read(lin, c0);
124
                        read(lin, c1);
125
                        read(lin, c2);
126
                        read(lin, c3);
127
                        if t = 2 then
128
                            read(lin, c4);
129
                            read(lin, c5);
130
                            adr := fromhex((c0, c1, c2, c3, c4, c5));
131
                        elsif t = 3 then
132
                            read(lin, c4);
133
                            read(lin, c5);
134
                            read(lin, c6);
135
                            read(lin, c7);
136
                            adr := fromhex((c0, c1, c2, c3, c4, c5, c6, c7));
137
                        else
138
                            adr := fromhex((c0, c1, c2, c3));
139
                        end if;
140
                        assert adr(1 downto 0) = "00" report "invalid address in SREC file";
141
                        for i in 0 to (n-4) / 4 loop
142
                            read(lin, c0);
143
                            read(lin, c1);
144
                            read(lin, c2);
145
                            read(lin, c3);
146
                            read(lin, c4);
147
                            read(lin, c5);
148
                            read(lin, c6);
149
                            read(lin, c7);
150
                            dat := fromhex((c0, c1, c2, c3, c4, c5, c6, c7));
151
                            mem(to_integer(adr(abits-1 downto 2)) + i) <= std_logic_vector(dat);
152
                        end loop;
153
                    end if;
154
                end if;
155
            end loop;
156
            report "Loaded AHBRAM contents";
157
        end procedure;
158
 
159
        variable wa: integer;
160
    begin
161
        if s_load = '1' then
162
 
163
            -- Load RAM contents at start of simulation.
164
            s_load  <= '0';
165
            loadfile;
166
 
167
        elsif rising_edge(clk) then
168
 
169
            -- Clock tick.
170
 
171
            s_ready <= '1';
172
            s_rdata <= mem(to_integer(unsigned(ahbi.haddr(abits-1 downto 2))));
173
 
174
            if ahbi.hready = '1' then
175
                s_write <= ahbi.hsel(hindex) and ahbi.htrans(1) and ahbi.hwrite;
176
                s_waddr <= ahbi.haddr;
177
                s_wsize <= ahbi.hsize;
178
                s_ready <= not (s_ready and ahbi.hsel(hindex) and ahbi.htrans(1) and ahbi.hwrite);
179
            end if;
180
 
181
            wa := to_integer(unsigned(s_waddr(abits-1 downto 2)));
182
            if s_write = '1' and s_ready = '1' then
183
                case s_wsize is
184
                    when HSIZE_BYTE =>
185
                        case s_waddr(1 downto 0) is
186
                            when "00" =>
187 12 jorisvr
                                mem(wa)(31 downto 24) <= s_wdata(31 downto 24);
188 5 jorisvr
                            when "01" =>
189 12 jorisvr
                                mem(wa)(23 downto 16) <= s_wdata(23 downto 16);
190 5 jorisvr
                            when "10" =>
191 12 jorisvr
                                mem(wa)(15 downto 8)  <= s_wdata(15 downto 8);
192 5 jorisvr
                            when others =>
193 12 jorisvr
                                mem(wa)(7 downto 0)   <= s_wdata(7 downto 0);
194 5 jorisvr
                        end case;
195
                    when HSIZE_HWORD =>
196
                        if s_waddr(1) = '1' then
197 12 jorisvr
                            mem(wa)(15 downto 0)  <= s_wdata(15 downto 0);
198 5 jorisvr
                        else
199 12 jorisvr
                            mem(wa)(31 downto 16) <= s_wdata(31 downto 16);
200 5 jorisvr
                        end if;
201
                    when others =>
202 12 jorisvr
                        mem(wa) <= s_wdata;
203 5 jorisvr
                end case;
204
            end if;
205
 
206
            if rstn = '0' then
207
                s_ready <= '0';
208
                s_rdata <= (others => '0');
209
                s_write <= '0';
210
            end if;
211
 
212
        end if;
213
    end process;
214
 
215
-- pragma translate_off
216
    bootmsg : report_version
217
        generic map ( "ahbram_loadfile: 32-bit AHB RAM module, hindex=" & tost(hindex) & ", abits=" & tost(abits) & ", fname=" & fname);
218
-- pragma translate_on
219
 
220
end architecture ahbram_arch;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.