1 |
2 |
Bregalad |
-- This file is part of ARM4U CPU
|
2 |
|
|
--
|
3 |
|
|
-- This is a creation of the Laboratory of Processor Architecture
|
4 |
|
|
-- of Ecole Polytechnique Fédérale de Lausanne ( http://lap.epfl.ch )
|
5 |
|
|
--
|
6 |
|
|
-- memory.vhd -- Describes the memory pipeline stage
|
7 |
|
|
--
|
8 |
|
|
-- Written By - Jonathan Masur and Xavier Jimenez (2013)
|
9 |
|
|
--
|
10 |
|
|
-- This program is free software; you can redistribute it and/or modify it
|
11 |
|
|
-- under the terms of the GNU General Public License as published by the
|
12 |
|
|
-- Free Software Foundation; either version 2, or (at your option) any
|
13 |
|
|
-- later version.
|
14 |
|
|
--
|
15 |
|
|
-- This program is distributed in the hope that it will be useful,
|
16 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
|
|
-- GNU General Public License for more details.
|
19 |
|
|
--
|
20 |
|
|
-- In other words, you are welcome to use, share and improve this program.
|
21 |
|
|
-- You are forbidden to forbid anyone else to use, share and improve
|
22 |
|
|
-- what you give them. Help stamp out software-hoarding!
|
23 |
|
|
|
24 |
|
|
library ieee;
|
25 |
|
|
use ieee.std_logic_1164.all;
|
26 |
|
|
use ieee.numeric_std.all;
|
27 |
|
|
use work.arm_types.all;
|
28 |
|
|
|
29 |
|
|
entity memory is
|
30 |
|
|
port(
|
31 |
|
|
clk : in std_logic;
|
32 |
|
|
reset_n : in std_logic;
|
33 |
|
|
|
34 |
|
|
mem_stage_valid : in std_logic;
|
35 |
|
|
mem_rdest_wren : in std_logic;
|
36 |
|
|
mem_rdest_adr : in std_logic_vector(4 downto 0);
|
37 |
|
|
mem_branch_en : in std_logic;
|
38 |
|
|
mem_wb_sel : in std_logic;
|
39 |
|
|
mem_exe_data : in std_logic_vector(31 downto 0);
|
40 |
|
|
mem_wrdata : in std_logic_vector(31 downto 0);
|
41 |
|
|
mem_mem_ctrl : in MEM_OPERATION;
|
42 |
|
|
mem_mem_burstcount : in std_logic_vector(3 downto 0);
|
43 |
|
|
|
44 |
|
|
wb_stage_valid : out std_logic;
|
45 |
|
|
wb_rdest_wren : out std_logic;
|
46 |
|
|
wb_rdest_adr : out std_logic_vector(4 downto 0);
|
47 |
|
|
wb_branch_en : out std_logic;
|
48 |
|
|
wb_wb_sel : out std_logic;
|
49 |
|
|
wb_exe_data : out std_logic_vector(31 downto 0);
|
50 |
|
|
wb_mem_ctrl : out MEM_OPERATION;
|
51 |
|
|
|
52 |
|
|
fwd_mem_enable : out std_logic;
|
53 |
|
|
fwd_mem_address : out std_logic_vector(4 downto 0);
|
54 |
|
|
fwd_mem_data : out std_logic_vector(31 downto 0);
|
55 |
|
|
|
56 |
|
|
avm_data_waitrequest : in std_logic;
|
57 |
|
|
avm_data_read : out std_logic;
|
58 |
|
|
avm_data_writedata : out std_logic_vector(31 downto 0);
|
59 |
|
|
avm_data_write : out std_logic;
|
60 |
|
|
avm_data_byteen : out std_logic_vector(3 downto 0);
|
61 |
|
|
avm_data_burstcount : out std_logic_vector(4 downto 0);
|
62 |
|
|
avm_data_address : out std_logic_vector(31 downto 0);
|
63 |
|
|
|
64 |
|
|
mem_blocked_n : out std_logic;
|
65 |
|
|
mem_latch_enable : in std_logic
|
66 |
|
|
);
|
67 |
|
|
end entity;
|
68 |
|
|
|
69 |
|
|
architecture rtl of memory is
|
70 |
|
|
|
71 |
|
|
signal avalon_acknowledge : std_logic;
|
72 |
|
|
|
73 |
|
|
function get_byteen(adr: std_logic_vector) return std_logic_vector is
|
74 |
|
|
begin
|
75 |
|
|
-- Assuming little endian memory
|
76 |
|
|
case adr(1 downto 0) is
|
77 |
|
|
when "00" => return "0001";
|
78 |
|
|
when "01" => return "0010";
|
79 |
|
|
when "10" => return "0100";
|
80 |
|
|
when others => return "1000";
|
81 |
|
|
end case;
|
82 |
|
|
end;
|
83 |
|
|
|
84 |
|
|
begin
|
85 |
|
|
process(clk, reset_n) is
|
86 |
|
|
begin
|
87 |
|
|
if reset_n = '0'
|
88 |
|
|
then
|
89 |
|
|
wb_stage_valid <= '0';
|
90 |
|
|
elsif rising_edge(clk)
|
91 |
|
|
then
|
92 |
|
|
if mem_latch_enable = '1'
|
93 |
|
|
then
|
94 |
|
|
if mem_mem_ctrl = NO_MEM_OP or mem_mem_ctrl = LOAD_BURST
|
95 |
|
|
then
|
96 |
|
|
wb_stage_valid <= mem_stage_valid;
|
97 |
|
|
else
|
98 |
|
|
wb_stage_valid <= mem_stage_valid and (not avm_data_waitrequest or avalon_acknowledge);
|
99 |
|
|
end if;
|
100 |
|
|
end if;
|
101 |
|
|
end if;
|
102 |
|
|
end process;
|
103 |
|
|
|
104 |
|
|
-- output latch
|
105 |
|
|
process(clk) is
|
106 |
|
|
begin
|
107 |
|
|
if rising_edge(clk)
|
108 |
|
|
then
|
109 |
|
|
if mem_latch_enable = '1'
|
110 |
|
|
then
|
111 |
|
|
wb_rdest_wren <= mem_rdest_wren;
|
112 |
|
|
wb_rdest_adr <= mem_rdest_adr;
|
113 |
|
|
wb_branch_en <= mem_branch_en;
|
114 |
|
|
wb_wb_sel <= mem_wb_sel;
|
115 |
|
|
wb_exe_data <= mem_exe_data;
|
116 |
|
|
wb_mem_ctrl <= mem_mem_ctrl;
|
117 |
|
|
end if;
|
118 |
|
|
end if;
|
119 |
|
|
end process;
|
120 |
|
|
|
121 |
|
|
-- forwarding
|
122 |
|
|
fwd_mem_enable <= mem_rdest_wren and mem_stage_valid;
|
123 |
|
|
fwd_mem_address <= mem_rdest_adr;
|
124 |
|
|
fwd_mem_data <= mem_exe_data;
|
125 |
|
|
|
126 |
|
|
-- avalon master
|
127 |
|
|
avm_data_address <= mem_exe_data;
|
128 |
|
|
process(mem_mem_ctrl, mem_mem_burstcount, mem_wrdata, mem_stage_valid, mem_exe_data, avm_data_waitrequest, avalon_acknowledge) is
|
129 |
|
|
begin
|
130 |
|
|
avm_data_read <= '0';
|
131 |
|
|
avm_data_write <= '0';
|
132 |
|
|
avm_data_writedata <= (others => '-');
|
133 |
|
|
avm_data_byteen <= (others => '-');
|
134 |
|
|
mem_blocked_n <= '1';
|
135 |
|
|
|
136 |
|
|
-- 0 actually means a burst count of 16 bytes
|
137 |
|
|
if mem_mem_burstcount = "0000"
|
138 |
|
|
then
|
139 |
|
|
avm_data_burstcount <= "10000";
|
140 |
|
|
else
|
141 |
|
|
avm_data_burstcount <= '0' & mem_mem_burstcount;
|
142 |
|
|
end if;
|
143 |
|
|
|
144 |
|
|
if avalon_acknowledge = '0'
|
145 |
|
|
then
|
146 |
|
|
case mem_mem_ctrl is
|
147 |
|
|
when NO_MEM_OP => null;
|
148 |
|
|
|
149 |
|
|
when LOAD_WORD =>
|
150 |
|
|
avm_data_read <= mem_stage_valid;
|
151 |
|
|
avm_data_write <= '0';
|
152 |
|
|
avm_data_byteen <= "1111";
|
153 |
|
|
|
154 |
|
|
mem_blocked_n <= not avm_data_waitrequest or not mem_stage_valid;
|
155 |
|
|
|
156 |
|
|
when LOAD_BYTE =>
|
157 |
|
|
avm_data_read <= mem_stage_valid;
|
158 |
|
|
avm_data_write <= '0';
|
159 |
|
|
avm_data_byteen <= get_byteen(mem_exe_data(1 downto 0));
|
160 |
|
|
mem_blocked_n <= not avm_data_waitrequest or not mem_stage_valid;
|
161 |
|
|
|
162 |
|
|
when LOAD_BURST => null;
|
163 |
|
|
|
164 |
|
|
when STORE_WORD =>
|
165 |
|
|
avm_data_read <= '0';
|
166 |
|
|
avm_data_write <= mem_stage_valid;
|
167 |
|
|
avm_data_writedata <= mem_wrdata;
|
168 |
|
|
avm_data_byteen <= "1111";
|
169 |
|
|
|
170 |
|
|
mem_blocked_n <= not avm_data_waitrequest or not mem_stage_valid;
|
171 |
|
|
|
172 |
|
|
when STORE_BYTE =>
|
173 |
|
|
avm_data_read <= '0';
|
174 |
|
|
avm_data_write <= mem_stage_valid;
|
175 |
|
|
-- Byte enable signals
|
176 |
|
|
avm_data_byteen <= get_byteen(mem_exe_data(1 downto 0));
|
177 |
|
|
-- Byte repetition
|
178 |
|
|
avm_data_writedata <= mem_wrdata(7 downto 0) & mem_wrdata(7 downto 0) & mem_wrdata(7 downto 0) & mem_wrdata(7 downto 0);
|
179 |
|
|
mem_blocked_n <= not avm_data_waitrequest or not mem_stage_valid;
|
180 |
|
|
end case;
|
181 |
|
|
end if;
|
182 |
|
|
end process;
|
183 |
|
|
|
184 |
|
|
-- Is nessesary to prevent multiple reads/writes to avalon bus when the WB stage is blocked (mem latch disabled)
|
185 |
|
|
process(clk) is
|
186 |
|
|
begin
|
187 |
|
|
if rising_edge(clk)
|
188 |
|
|
then
|
189 |
|
|
if mem_latch_enable = '1'
|
190 |
|
|
then
|
191 |
|
|
avalon_acknowledge <= '0';
|
192 |
|
|
else
|
193 |
|
|
avalon_acknowledge <= not avm_data_waitrequest or avalon_acknowledge;
|
194 |
|
|
end if;
|
195 |
|
|
end if;
|
196 |
|
|
end process;
|
197 |
|
|
|
198 |
|
|
end architecture;
|