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

Subversion Repositories mlite

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

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 56 rhoads
   constant little_endian  : std_logic_vector(1 downto 0) := "00";
48
   signal opcode_reg       : std_logic_vector(31 downto 0);
49
   signal next_opcode_reg  : std_logic_vector(31 downto 0);
50 72 rhoads
   signal address_reg      : std_logic_vector(31 downto 0);
51 7 rhoads
 
52 49 rhoads
   subtype mem_state_type is std_logic_vector(1 downto 0);
53
   signal mem_state_reg  : mem_state_type;
54
   constant STATE_FETCH  : mem_state_type := "00";
55
   constant STATE_ADDR   : mem_state_type := "01";
56
   constant STATE_WRITE  : mem_state_type := "10";
57
   constant STATE_PAUSE  : mem_state_type := "11";
58 72 rhoads
 
59
   --ACCURATE_TIMING notes:
60
   --The VHDL compiler's timing calculation isn't able to realize that
61
   --memory reads take two clock cycles.  It notices that reg_bank:reg_dest
62
   --is dependent on mem_ctrl:mem_data_r which is dependent on 
63
   --mem_ctrl:mem_address which is dependent on alu:c_alu.  However,
64
   --this dependency is only true for memory read or write cycles
65
   --which are multiple clock cycles.  Enabling ACCURATE_TIMING
66
   --creates an additional 32-bit register that does nothing other
67
   --than letting the VHDL compiler accurately predict the maximum
68
   --clock speed.
69 2 rhoads
begin
70
 
71 6 rhoads
mem_proc: process(clk, reset_in, pause_in, nullify_op,
72
                  address_pc, address_data, mem_source, data_write,
73 72 rhoads
                  mem_data_r,
74 56 rhoads
                  opcode_reg, next_opcode_reg, mem_state_reg,
75 72 rhoads
                  address_reg)
76
   variable data           : std_logic_vector(31 downto 0);
77
   variable opcode_next    : std_logic_vector(31 downto 0);
78
   variable byte_sel       : std_logic_vector(3 downto 0);
79
   variable write_line     : std_logic;
80 49 rhoads
   variable mem_state_next : mem_state_type;
81 72 rhoads
   variable pause          : std_logic;
82
   variable address        : std_logic_vector(31 downto 0);
83
   variable bits           : std_logic_vector(1 downto 0);
84
   variable mem_data_w_v   : std_logic_vector(31 downto 0);
85 2 rhoads
begin
86 72 rhoads
   byte_sel := "0000";
87 56 rhoads
   write_line := '0';
88 2 rhoads
   pause := '0';
89 49 rhoads
   mem_state_next := mem_state_reg;
90 2 rhoads
 
91 72 rhoads
   address := address_pc;
92
   data := ZERO;
93 7 rhoads
   mem_data_w_v := ZERO;
94 2 rhoads
 
95
   case mem_source is
96
   when mem_read32 =>
97 72 rhoads
      data := mem_data_r;
98 2 rhoads
   when mem_read16 | mem_read16s =>
99 72 rhoads
      if address_reg(1) = little_endian(1) then
100
         data(15 downto 0) := mem_data_r(31 downto 16);
101 2 rhoads
      else
102 72 rhoads
         data(15 downto 0) := mem_data_r(15 downto 0);
103 2 rhoads
      end if;
104 72 rhoads
      if mem_source = mem_read16 or data(15) = '0' then
105
         data(31 downto 16) := ZERO(31 downto 16);
106 2 rhoads
      else
107 72 rhoads
         data(31 downto 16) := ONES(31 downto 16);
108 2 rhoads
      end if;
109
   when mem_read8 | mem_read8s =>
110 72 rhoads
      bits := address_reg(1 downto 0) xor little_endian;
111 2 rhoads
      case bits is
112 72 rhoads
      when "00" => data(7 downto 0) := mem_data_r(31 downto 24);
113
      when "01" => data(7 downto 0) := mem_data_r(23 downto 16);
114
      when "10" => data(7 downto 0) := mem_data_r(15 downto 8);
115
      when others => data(7 downto 0) := mem_data_r(7 downto 0);
116 2 rhoads
      end case;
117 72 rhoads
      if mem_source = mem_read8 or data(7) = '0' then
118
         data(31 downto 8) := ZERO(31 downto 8);
119 2 rhoads
      else
120 72 rhoads
         data(31 downto 8) := ONES(31 downto 8);
121 2 rhoads
      end if;
122
   when mem_write32 =>
123 56 rhoads
      write_line := '1';
124 2 rhoads
      mem_data_w_v := data_write;
125 72 rhoads
      byte_sel := "1111";
126 2 rhoads
   when mem_write16 =>
127 56 rhoads
      write_line := '1';
128 2 rhoads
      mem_data_w_v := data_write(15 downto 0) & data_write(15 downto 0);
129
      if address_data(1) = little_endian(1) then
130 72 rhoads
         byte_sel := "1100";
131 2 rhoads
      else
132 72 rhoads
         byte_sel := "0011";
133 2 rhoads
      end if;
134
   when mem_write8 =>
135 56 rhoads
      write_line := '1';
136 2 rhoads
      mem_data_w_v := data_write(7 downto 0) & data_write(7 downto 0) &
137
                  data_write(7 downto 0) & data_write(7 downto 0);
138
      bits := address_data(1 downto 0) xor little_endian;
139
      case bits is
140
      when "00" =>
141 72 rhoads
         byte_sel := "1000";
142 2 rhoads
      when "01" =>
143 72 rhoads
         byte_sel := "0100";
144 2 rhoads
      when "10" =>
145 72 rhoads
         byte_sel := "0010";
146 2 rhoads
      when others =>
147 72 rhoads
         byte_sel := "0001";
148 2 rhoads
      end case;
149
   when others =>
150
   end case;
151
 
152 7 rhoads
   opcode_next := opcode_reg;
153 56 rhoads
   if mem_source = mem_fetch then --opcode fetch
154 49 rhoads
      mem_state_next := STATE_FETCH;
155 72 rhoads
      if pause_in = '0' then
156
         opcode_next := mem_data_r;
157 2 rhoads
      end if;
158 49 rhoads
   else  --data read or write (not opcode fetch)
159 72 rhoads
 
160
      --State Machine
161 49 rhoads
      case mem_state_reg is
162
      when STATE_FETCH =>
163 56 rhoads
         write_line := '0';
164 7 rhoads
         pause := '1';
165 72 rhoads
         byte_sel := "0000";
166
         if pause_in = '0' then
167 49 rhoads
            mem_state_next := STATE_ADDR;
168 47 rhoads
         end if;
169 49 rhoads
      when STATE_ADDR =>  --address lines pre-hold
170 72 rhoads
         address := address_reg;
171
         if write_line = '1' and address_reg(31) = '1' then
172 7 rhoads
            pause := '1';
173 72 rhoads
            byte_sel := "0000";
174
            if pause_in = '0' then
175 49 rhoads
               mem_state_next := STATE_WRITE;    --4 cycle access
176 47 rhoads
            end if;
177 7 rhoads
         else
178 72 rhoads
            if pause_in = '0' then
179 7 rhoads
               opcode_next := next_opcode_reg;
180 49 rhoads
               mem_state_next := STATE_FETCH;    --2 cycle access
181 7 rhoads
            end if;
182
         end if;
183 49 rhoads
      when STATE_WRITE =>
184 7 rhoads
         pause := '1';
185 72 rhoads
         address := address_reg;
186
         if pause_in = '0' then
187 49 rhoads
            mem_state_next := STATE_PAUSE;
188 2 rhoads
         end if;
189 49 rhoads
      when OTHERS =>  --STATE_PAUSE address lines post-hold
190 72 rhoads
         address := address_reg;
191
         byte_sel := "0000";
192
         if pause_in = '0' then
193 49 rhoads
            opcode_next := next_opcode_reg;
194
            mem_state_next := STATE_FETCH;
195 47 rhoads
         end if;
196 49 rhoads
      end case;
197 2 rhoads
   end if;
198 7 rhoads
 
199 6 rhoads
   if nullify_op = '1' then
200 7 rhoads
      opcode_next := ZERO;  --NOP
201 6 rhoads
   end if;
202 56 rhoads
 
203 2 rhoads
   if reset_in = '1' then
204 56 rhoads
      mem_state_reg <= STATE_FETCH;
205
      opcode_reg <= ZERO;
206 72 rhoads
      next_opcode_reg <= ZERO;
207 56 rhoads
      write_line := '0';
208
   elsif rising_edge(clk) then
209
      mem_state_reg <= mem_state_next;
210 7 rhoads
      opcode_reg <= opcode_next;
211 49 rhoads
      if mem_state_reg = STATE_FETCH then
212 72 rhoads
         next_opcode_reg <= mem_data_r;
213 2 rhoads
      end if;
214
   end if;
215
 
216 72 rhoads
   if rising_edge(clk) then
217
      if ACCURATE_TIMING then
218
         address_reg <= address_data;
219
      end if;
220
   end if;
221
   if not ACCURATE_TIMING then
222
      address_reg <= address_data;
223
   end if;
224
 
225 56 rhoads
   opcode_out <= opcode_reg;
226 72 rhoads
   data_read <= data;
227
   pause_out <= pause;
228
   mem_byte_sel <= byte_sel;
229
   mem_address <= address;
230 56 rhoads
   mem_write <= write_line;
231
 
232
   if write_line = '1' then
233 7 rhoads
      mem_data_w <= mem_data_w_v;
234
   else
235
      mem_data_w <= HIGH_Z; --ZERO;
236
   end if;
237 2 rhoads
 
238
end process; --data_proc
239
 
240
end; --architecture logic
241
 

powered by: WebSVN 2.1.0

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