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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [vhdl/] [mem_ctrl.vhd] - Blame information for rev 128

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 rhoads
---------------------------------------------------------------------
2
-- TITLE: Memory Controller
3
-- AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
-- DATE CREATED: 1/31/01
5
-- FILENAME: mem_ctrl.vhd
6 43 rhoads
-- PROJECT: Plasma CPU core
7 2 rhoads
-- COPYRIGHT: Software placed into the public domain by the author.
8
--    Software 'as is' without warranty.  Author liable for nothing.
9
-- DESCRIPTION:
10 43 rhoads
--    Memory controller for the Plasma CPU.
11 2 rhoads
--    Supports Big or Little Endian mode.
12 7 rhoads
--    Four cycles for a write unless a(31)='1' then two cycles.
13 2 rhoads
--    This entity could implement interfaces to:
14
--       Data cache
15
--       Address cache
16
--       Memory management unit (MMU)
17
--       DRAM controller
18
---------------------------------------------------------------------
19
library ieee;
20
use ieee.std_logic_1164.all;
21 39 rhoads
use work.mlite_pack.all;
22 2 rhoads
 
23
entity mem_ctrl is
24 72 rhoads
   generic(ACCURATE_TIMING : boolean := false);
25 2 rhoads
   port(clk          : in std_logic;
26
        reset_in     : in std_logic;
27
        pause_in     : in std_logic;
28
        nullify_op   : in std_logic;
29
        address_pc   : in std_logic_vector(31 downto 0);
30
        opcode_out   : out std_logic_vector(31 downto 0);
31
 
32
        address_data : in std_logic_vector(31 downto 0);
33
        mem_source   : in mem_source_type;
34
        data_write   : in std_logic_vector(31 downto 0);
35
        data_read    : out std_logic_vector(31 downto 0);
36
        pause_out    : out std_logic;
37
 
38
        mem_address  : out std_logic_vector(31 downto 0);
39
        mem_data_w   : out std_logic_vector(31 downto 0);
40
        mem_data_r   : in std_logic_vector(31 downto 0);
41
        mem_byte_sel : out std_logic_vector(3 downto 0);
42 72 rhoads
        mem_write    : out std_logic);
43 2 rhoads
end; --entity mem_ctrl
44
 
45
architecture logic of mem_ctrl is
46
   --"00" = big_endian; "11" = little_endian
47 128 rhoads
   constant ENDIAN_MODE    : std_logic_vector(1 downto 0) := "00";
48 56 rhoads
   signal opcode_reg       : std_logic_vector(31 downto 0);
49
   signal next_opcode_reg  : std_logic_vector(31 downto 0);
50 7 rhoads
 
51 49 rhoads
   subtype mem_state_type is std_logic_vector(1 downto 0);
52
   signal mem_state_reg  : mem_state_type;
53
   constant STATE_FETCH  : mem_state_type := "00";
54
   constant STATE_ADDR   : mem_state_type := "01";
55
   constant STATE_WRITE  : mem_state_type := "10";
56
   constant STATE_PAUSE  : mem_state_type := "11";
57 72 rhoads
 
58
   --ACCURATE_TIMING notes:
59
   --The VHDL compiler's timing calculation isn't able to realize that
60
   --memory reads take two clock cycles.  It notices that reg_bank:reg_dest
61
   --is dependent on mem_ctrl:mem_data_r which is dependent on 
62
   --mem_ctrl:mem_address which is dependent on alu:c_alu.  However,
63
   --this dependency is only true for memory read or write cycles
64
   --which are multiple clock cycles.  Enabling ACCURATE_TIMING
65
   --creates an additional 32-bit register that does nothing other
66
   --than letting the VHDL compiler accurately predict the maximum
67
   --clock speed.
68 114 rhoads
   signal address_reg        : std_logic_vector(31 downto 0);
69
   signal write_reg          : std_logic;
70
   signal byte_sel_reg       : std_logic_vector(3 downto 0);
71
   signal mem_state_next_sig : mem_state_type;
72
   signal opcode_next_sig    : std_logic_vector(31 downto 0);
73
   signal write_next_sig     : std_logic;
74
   signal byte_sel_next_sig  : std_logic_vector(3 downto 0);
75
 
76
begin
77 95 rhoads
 
78 114 rhoads
GEN_REGS: process(clk, reset_in)
79 2 rhoads
begin
80 114 rhoads
   if reset_in = '1' then
81
      mem_state_reg <= STATE_FETCH;
82
      opcode_reg <= ZERO;
83
      next_opcode_reg <= ZERO;
84
   elsif rising_edge(clk) then
85
      mem_state_reg <= mem_state_next_sig;
86
      opcode_reg <= opcode_next_sig;
87
      if mem_state_reg = STATE_FETCH then
88
         next_opcode_reg <= mem_data_r;
89
      end if;
90
   end if;
91
end process;
92 2 rhoads
 
93 114 rhoads
GEN_REGS2: process(clk, address_data, write_next_sig, byte_sel_next_sig)
94
begin
95 128 rhoads
   if reset_in = '1' then
96 114 rhoads
      if ACCURATE_TIMING then
97 128 rhoads
         address_reg <= ZERO;
98
         write_reg <= '0';
99
         byte_sel_reg <= "0000";
100
      end if;
101
   elsif rising_edge(clk) then
102
      if ACCURATE_TIMING then
103 114 rhoads
         address_reg <= address_data;
104
         write_reg <= write_next_sig;
105
         byte_sel_reg <= byte_sel_next_sig;
106
      end if;
107
   end if;
108
   if not ACCURATE_TIMING then
109
      address_reg <= address_data;
110
      write_reg <= write_next_sig;
111
      byte_sel_reg <= byte_sel_next_sig;
112
   end if;
113
end process;
114
 
115 6 rhoads
mem_proc: process(clk, reset_in, pause_in, nullify_op,
116
                  address_pc, address_data, mem_source, data_write,
117 72 rhoads
                  mem_data_r,
118 56 rhoads
                  opcode_reg, next_opcode_reg, mem_state_reg,
119 95 rhoads
                  address_reg, write_reg, byte_sel_reg)
120 72 rhoads
   variable data           : std_logic_vector(31 downto 0);
121
   variable opcode_next    : std_logic_vector(31 downto 0);
122 95 rhoads
   variable byte_sel_next  : std_logic_vector(3 downto 0);
123 72 rhoads
   variable byte_sel       : std_logic_vector(3 downto 0);
124 95 rhoads
   variable write_next     : std_logic;
125 72 rhoads
   variable write_line     : std_logic;
126 49 rhoads
   variable mem_state_next : mem_state_type;
127 72 rhoads
   variable pause          : std_logic;
128
   variable address        : std_logic_vector(31 downto 0);
129
   variable bits           : std_logic_vector(1 downto 0);
130
   variable mem_data_w_v   : std_logic_vector(31 downto 0);
131 2 rhoads
begin
132 95 rhoads
   byte_sel_next := "0000";
133
   write_next := '0';
134 2 rhoads
   pause := '0';
135 49 rhoads
   mem_state_next := mem_state_reg;
136 2 rhoads
 
137 72 rhoads
   data := ZERO;
138 7 rhoads
   mem_data_w_v := ZERO;
139 2 rhoads
 
140
   case mem_source is
141 128 rhoads
   when MEM_READ32 =>
142 72 rhoads
      data := mem_data_r;
143 128 rhoads
 
144
   when MEM_READ16 | MEM_READ16S =>
145
      if address_reg(1) = ENDIAN_MODE(1) then
146 72 rhoads
         data(15 downto 0) := mem_data_r(31 downto 16);
147 2 rhoads
      else
148 72 rhoads
         data(15 downto 0) := mem_data_r(15 downto 0);
149 2 rhoads
      end if;
150 72 rhoads
      if mem_source = mem_read16 or data(15) = '0' then
151
         data(31 downto 16) := ZERO(31 downto 16);
152 2 rhoads
      else
153 72 rhoads
         data(31 downto 16) := ONES(31 downto 16);
154 2 rhoads
      end if;
155 128 rhoads
 
156
   when MEM_READ8 | MEM_READ8s =>
157
      bits := address_reg(1 downto 0) xor ENDIAN_MODE;
158 2 rhoads
      case bits is
159 72 rhoads
      when "00" => data(7 downto 0) := mem_data_r(31 downto 24);
160
      when "01" => data(7 downto 0) := mem_data_r(23 downto 16);
161
      when "10" => data(7 downto 0) := mem_data_r(15 downto 8);
162
      when others => data(7 downto 0) := mem_data_r(7 downto 0);
163 2 rhoads
      end case;
164 128 rhoads
      if mem_source = MEM_READ8 or data(7) = '0' then
165 72 rhoads
         data(31 downto 8) := ZERO(31 downto 8);
166 2 rhoads
      else
167 72 rhoads
         data(31 downto 8) := ONES(31 downto 8);
168 2 rhoads
      end if;
169 128 rhoads
 
170
   when MEM_WRITE32 =>
171 95 rhoads
      write_next := '1';
172 2 rhoads
      mem_data_w_v := data_write;
173 95 rhoads
      byte_sel_next := "1111";
174 128 rhoads
 
175
   when MEM_WRITE16 =>
176 95 rhoads
      write_next := '1';
177 2 rhoads
      mem_data_w_v := data_write(15 downto 0) & data_write(15 downto 0);
178 128 rhoads
      if address_data(1) = ENDIAN_MODE(1) then
179 95 rhoads
         byte_sel_next := "1100";
180 2 rhoads
      else
181 95 rhoads
         byte_sel_next := "0011";
182 2 rhoads
      end if;
183 128 rhoads
 
184
   when MEM_WRITE8 =>
185 95 rhoads
      write_next := '1';
186 2 rhoads
      mem_data_w_v := data_write(7 downto 0) & data_write(7 downto 0) &
187
                  data_write(7 downto 0) & data_write(7 downto 0);
188 128 rhoads
      bits := address_data(1 downto 0) xor ENDIAN_MODE;
189 2 rhoads
      case bits is
190
      when "00" =>
191 95 rhoads
         byte_sel_next := "1000";
192 2 rhoads
      when "01" =>
193 95 rhoads
         byte_sel_next := "0100";
194 2 rhoads
      when "10" =>
195 95 rhoads
         byte_sel_next := "0010";
196 2 rhoads
      when others =>
197 95 rhoads
         byte_sel_next := "0001";
198 2 rhoads
      end case;
199 128 rhoads
 
200 2 rhoads
   when others =>
201
   end case;
202 128 rhoads
 
203 114 rhoads
   byte_sel_next_sig <= byte_sel_next;
204
   write_next_sig <= write_next;
205
 
206 7 rhoads
   opcode_next := opcode_reg;
207 95 rhoads
   case mem_state_reg is             --State Machine
208
   when STATE_FETCH =>
209
      address := address_pc;
210
      write_line := '0';
211
      byte_sel := "0000";
212
      if mem_source = mem_fetch then --opcode fetch
213
         mem_state_next := STATE_FETCH;
214
         if pause_in = '0' then
215
            opcode_next := mem_data_r;
216
         end if;
217
      else                           --memory read or write
218 7 rhoads
         pause := '1';
219 72 rhoads
         if pause_in = '0' then
220 49 rhoads
            mem_state_next := STATE_ADDR;
221 47 rhoads
         end if;
222 95 rhoads
      end if;
223 128 rhoads
 
224 95 rhoads
   when STATE_ADDR =>  --address lines pre-hold
225
      address := address_reg;
226
      write_line := write_reg;
227
      if write_reg = '1' and address_reg(31) = '1' then
228 7 rhoads
         pause := '1';
229 95 rhoads
         byte_sel := "0000";
230 72 rhoads
         if pause_in = '0' then
231 95 rhoads
            mem_state_next := STATE_WRITE;    --4 cycle access
232 2 rhoads
         end if;
233 95 rhoads
      else
234
         byte_sel := byte_sel_reg;
235 72 rhoads
         if pause_in = '0' then
236 49 rhoads
            opcode_next := next_opcode_reg;
237 95 rhoads
            mem_state_next := STATE_FETCH;    --2 cycle access
238 47 rhoads
         end if;
239 95 rhoads
      end if;
240 128 rhoads
 
241 95 rhoads
   when STATE_WRITE =>
242
      pause := '1';
243
      address := address_reg;
244
      write_line := write_reg;
245
      byte_sel := byte_sel_reg;
246
      if pause_in = '0' then
247
         mem_state_next := STATE_PAUSE;
248
      end if;
249 128 rhoads
 
250 95 rhoads
   when OTHERS =>  --STATE_PAUSE address lines post-hold
251
      address := address_reg;
252
      write_line := write_reg;
253
      byte_sel := "0000";
254
      if pause_in = '0' then
255
         opcode_next := next_opcode_reg;
256
         mem_state_next := STATE_FETCH;
257
      end if;
258
   end case;
259 114 rhoads
 
260 95 rhoads
   if nullify_op = '1' and pause_in = '0' then
261 89 rhoads
      opcode_next := ZERO;  --NOP after beql
262 6 rhoads
   end if;
263 56 rhoads
 
264 114 rhoads
   mem_state_next_sig <= mem_state_next;
265
   opcode_next_sig <= opcode_next;
266
 
267 2 rhoads
   if reset_in = '1' then
268 114 rhoads
      write_line := '0';
269 2 rhoads
   end if;
270
 
271 56 rhoads
   opcode_out <= opcode_reg;
272 72 rhoads
   data_read <= data;
273
   pause_out <= pause;
274
   mem_byte_sel <= byte_sel;
275
   mem_address <= address;
276 56 rhoads
   mem_write <= write_line;
277
 
278
   if write_line = '1' then
279 7 rhoads
      mem_data_w <= mem_data_w_v;
280
   else
281
      mem_data_w <= HIGH_Z; --ZERO;
282
   end if;
283 2 rhoads
 
284
end process; --data_proc
285
 
286
end; --architecture logic
287
 

powered by: WebSVN 2.1.0

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