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

Subversion Repositories plasma

[/] [plasma/] [tags/] [V3_0/] [vhdl/] [mem_ctrl.vhd] - Blame information for rev 95

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 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 95 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
 
72 2 rhoads
begin
73
 
74 6 rhoads
mem_proc: process(clk, reset_in, pause_in, nullify_op,
75
                  address_pc, address_data, mem_source, data_write,
76 72 rhoads
                  mem_data_r,
77 56 rhoads
                  opcode_reg, next_opcode_reg, mem_state_reg,
78 95 rhoads
                  address_reg, write_reg, byte_sel_reg)
79 72 rhoads
   variable data           : std_logic_vector(31 downto 0);
80
   variable opcode_next    : std_logic_vector(31 downto 0);
81 95 rhoads
   variable byte_sel_next  : std_logic_vector(3 downto 0);
82 72 rhoads
   variable byte_sel       : std_logic_vector(3 downto 0);
83 95 rhoads
   variable write_next     : std_logic;
84 72 rhoads
   variable write_line     : std_logic;
85 49 rhoads
   variable mem_state_next : mem_state_type;
86 72 rhoads
   variable pause          : std_logic;
87
   variable address        : std_logic_vector(31 downto 0);
88
   variable bits           : std_logic_vector(1 downto 0);
89
   variable mem_data_w_v   : std_logic_vector(31 downto 0);
90 2 rhoads
begin
91 95 rhoads
   byte_sel_next := "0000";
92
   write_next := '0';
93 2 rhoads
   pause := '0';
94 49 rhoads
   mem_state_next := mem_state_reg;
95 2 rhoads
 
96 72 rhoads
   data := ZERO;
97 7 rhoads
   mem_data_w_v := ZERO;
98 2 rhoads
 
99
   case mem_source is
100
   when mem_read32 =>
101 72 rhoads
      data := mem_data_r;
102 2 rhoads
   when mem_read16 | mem_read16s =>
103 72 rhoads
      if address_reg(1) = little_endian(1) then
104
         data(15 downto 0) := mem_data_r(31 downto 16);
105 2 rhoads
      else
106 72 rhoads
         data(15 downto 0) := mem_data_r(15 downto 0);
107 2 rhoads
      end if;
108 72 rhoads
      if mem_source = mem_read16 or data(15) = '0' then
109
         data(31 downto 16) := ZERO(31 downto 16);
110 2 rhoads
      else
111 72 rhoads
         data(31 downto 16) := ONES(31 downto 16);
112 2 rhoads
      end if;
113
   when mem_read8 | mem_read8s =>
114 72 rhoads
      bits := address_reg(1 downto 0) xor little_endian;
115 2 rhoads
      case bits is
116 72 rhoads
      when "00" => data(7 downto 0) := mem_data_r(31 downto 24);
117
      when "01" => data(7 downto 0) := mem_data_r(23 downto 16);
118
      when "10" => data(7 downto 0) := mem_data_r(15 downto 8);
119
      when others => data(7 downto 0) := mem_data_r(7 downto 0);
120 2 rhoads
      end case;
121 72 rhoads
      if mem_source = mem_read8 or data(7) = '0' then
122
         data(31 downto 8) := ZERO(31 downto 8);
123 2 rhoads
      else
124 72 rhoads
         data(31 downto 8) := ONES(31 downto 8);
125 2 rhoads
      end if;
126
   when mem_write32 =>
127 95 rhoads
      write_next := '1';
128 2 rhoads
      mem_data_w_v := data_write;
129 95 rhoads
      byte_sel_next := "1111";
130 2 rhoads
   when mem_write16 =>
131 95 rhoads
      write_next := '1';
132 2 rhoads
      mem_data_w_v := data_write(15 downto 0) & data_write(15 downto 0);
133 95 rhoads
      if address_data(1) = little_endian(1) then
134
         byte_sel_next := "1100";
135 2 rhoads
      else
136 95 rhoads
         byte_sel_next := "0011";
137 2 rhoads
      end if;
138
   when mem_write8 =>
139 95 rhoads
      write_next := '1';
140 2 rhoads
      mem_data_w_v := data_write(7 downto 0) & data_write(7 downto 0) &
141
                  data_write(7 downto 0) & data_write(7 downto 0);
142 95 rhoads
      bits := address_data(1 downto 0) xor little_endian;
143 2 rhoads
      case bits is
144
      when "00" =>
145 95 rhoads
         byte_sel_next := "1000";
146 2 rhoads
      when "01" =>
147 95 rhoads
         byte_sel_next := "0100";
148 2 rhoads
      when "10" =>
149 95 rhoads
         byte_sel_next := "0010";
150 2 rhoads
      when others =>
151 95 rhoads
         byte_sel_next := "0001";
152 2 rhoads
      end case;
153
   when others =>
154
   end case;
155
 
156 7 rhoads
   opcode_next := opcode_reg;
157 95 rhoads
   case mem_state_reg is             --State Machine
158
   when STATE_FETCH =>
159
      address := address_pc;
160
      write_line := '0';
161
      byte_sel := "0000";
162
      if mem_source = mem_fetch then --opcode fetch
163
         mem_state_next := STATE_FETCH;
164
         if pause_in = '0' then
165
            opcode_next := mem_data_r;
166
         end if;
167
      else                           --memory read or write
168 7 rhoads
         pause := '1';
169 72 rhoads
         if pause_in = '0' then
170 49 rhoads
            mem_state_next := STATE_ADDR;
171 47 rhoads
         end if;
172 95 rhoads
      end if;
173
   when STATE_ADDR =>  --address lines pre-hold
174
      address := address_reg;
175
      write_line := write_reg;
176
      if write_reg = '1' and address_reg(31) = '1' then
177 7 rhoads
         pause := '1';
178 95 rhoads
         byte_sel := "0000";
179 72 rhoads
         if pause_in = '0' then
180 95 rhoads
            mem_state_next := STATE_WRITE;    --4 cycle access
181 2 rhoads
         end if;
182 95 rhoads
      else
183
         byte_sel := byte_sel_reg;
184 72 rhoads
         if pause_in = '0' then
185 49 rhoads
            opcode_next := next_opcode_reg;
186 95 rhoads
            mem_state_next := STATE_FETCH;    --2 cycle access
187 47 rhoads
         end if;
188 95 rhoads
      end if;
189
   when STATE_WRITE =>
190
      pause := '1';
191
      address := address_reg;
192
      write_line := write_reg;
193
      byte_sel := byte_sel_reg;
194
      if pause_in = '0' then
195
         mem_state_next := STATE_PAUSE;
196
      end if;
197
   when OTHERS =>  --STATE_PAUSE address lines post-hold
198
      address := address_reg;
199
      write_line := write_reg;
200
      byte_sel := "0000";
201
      if pause_in = '0' then
202
         opcode_next := next_opcode_reg;
203
         mem_state_next := STATE_FETCH;
204
      end if;
205
   end case;
206 7 rhoads
 
207 95 rhoads
   if nullify_op = '1' and pause_in = '0' then
208 89 rhoads
      opcode_next := ZERO;  --NOP after beql
209 6 rhoads
   end if;
210 56 rhoads
 
211 2 rhoads
   if reset_in = '1' then
212 56 rhoads
      mem_state_reg <= STATE_FETCH;
213
      opcode_reg <= ZERO;
214 72 rhoads
      next_opcode_reg <= ZERO;
215 56 rhoads
      write_line := '0';
216
   elsif rising_edge(clk) then
217
      mem_state_reg <= mem_state_next;
218 7 rhoads
      opcode_reg <= opcode_next;
219 49 rhoads
      if mem_state_reg = STATE_FETCH then
220 72 rhoads
         next_opcode_reg <= mem_data_r;
221 2 rhoads
      end if;
222
   end if;
223
 
224 72 rhoads
   if rising_edge(clk) then
225
      if ACCURATE_TIMING then
226
         address_reg <= address_data;
227 95 rhoads
         write_reg <= write_next;
228
         byte_sel_reg <= byte_sel_next;
229 72 rhoads
      end if;
230
   end if;
231
   if not ACCURATE_TIMING then
232
      address_reg <= address_data;
233 95 rhoads
      write_reg <= write_next;
234
      byte_sel_reg <= byte_sel_next;
235 72 rhoads
   end if;
236
 
237 56 rhoads
   opcode_out <= opcode_reg;
238 72 rhoads
   data_read <= data;
239
   pause_out <= pause;
240
   mem_byte_sel <= byte_sel;
241
   mem_address <= address;
242 56 rhoads
   mem_write <= write_line;
243
 
244
   if write_line = '1' then
245 7 rhoads
      mem_data_w <= mem_data_w_v;
246
   else
247
      mem_data_w <= HIGH_Z; --ZERO;
248
   end if;
249 2 rhoads
 
250
end process; --data_proc
251
 
252
end; --architecture logic
253
 

powered by: WebSVN 2.1.0

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