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

Subversion Repositories mlite

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

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
   port(clk          : in std_logic;
25
        reset_in     : in std_logic;
26
        pause_in     : in std_logic;
27
        nullify_op   : in std_logic;
28
        address_pc   : in std_logic_vector(31 downto 0);
29
        opcode_out   : out std_logic_vector(31 downto 0);
30
 
31
        address_data : in std_logic_vector(31 downto 0);
32
        mem_source   : in mem_source_type;
33
        data_write   : in std_logic_vector(31 downto 0);
34
        data_read    : out std_logic_vector(31 downto 0);
35
        pause_out    : out std_logic;
36
 
37
        mem_address  : out std_logic_vector(31 downto 0);
38
        mem_data_w   : out std_logic_vector(31 downto 0);
39
        mem_data_r   : in std_logic_vector(31 downto 0);
40
        mem_byte_sel : out std_logic_vector(3 downto 0);
41
        mem_write    : out std_logic;
42
        mem_pause    : in std_logic);
43
end; --entity mem_ctrl
44
 
45
architecture logic of mem_ctrl is
46
   --"00" = big_endian; "11" = little_endian
47
   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 2 rhoads
begin
58
 
59 6 rhoads
mem_proc: process(clk, reset_in, pause_in, nullify_op,
60
                  address_pc, address_data, mem_source, data_write,
61
                  mem_data_r, mem_pause,
62 49 rhoads
                  opcode_reg, next_opcode_reg, mem_state_reg)
63 2 rhoads
   variable data, datab   : std_logic_vector(31 downto 0);
64 7 rhoads
   variable opcode_next   : std_logic_vector(31 downto 0);
65
   variable byte_sel_next : std_logic_vector(3 downto 0);
66
   variable write_next    : std_logic;
67 49 rhoads
   variable mem_state_next : mem_state_type;
68 2 rhoads
   variable pause         : std_logic;
69 7 rhoads
   variable address_next  : std_logic_vector(31 downto 0);
70 2 rhoads
   variable bits          : std_logic_vector(1 downto 0);
71
   variable mem_data_w_v  : std_logic_vector(31 downto 0);
72
begin
73 7 rhoads
   byte_sel_next := "0000";
74
   write_next := '0';
75 2 rhoads
   pause := '0';
76 49 rhoads
   mem_state_next := mem_state_reg;
77 2 rhoads
 
78 7 rhoads
   address_next := address_pc;
79 2 rhoads
   data := mem_data_r;
80
   datab := ZERO;
81 7 rhoads
   mem_data_w_v := ZERO;
82 2 rhoads
 
83
   case mem_source is
84
   when mem_read32 =>
85
      datab := data;
86
   when mem_read16 | mem_read16s =>
87
      if address_data(1) = little_endian(1) then
88
         datab(15 downto 0) := data(31 downto 16);
89
      else
90
         datab(15 downto 0) := data(15 downto 0);
91
      end if;
92
      if mem_source = mem_read16 or datab(15) = '0' then
93
         datab(31 downto 16) := ZERO(31 downto 16);
94
      else
95
         datab(31 downto 16) := ONES(31 downto 16);
96
      end if;
97
   when mem_read8 | mem_read8s =>
98
      bits := address_data(1 downto 0) xor little_endian;
99
      case bits is
100
      when "00" => datab(7 downto 0) := data(31 downto 24);
101
      when "01" => datab(7 downto 0) := data(23 downto 16);
102
      when "10" => datab(7 downto 0) := data(15 downto 8);
103
      when others => datab(7 downto 0) := data(7 downto 0);
104
      end case;
105
      if mem_source = mem_read8 or datab(7) = '0' then
106
         datab(31 downto 8) := ZERO(31 downto 8);
107
      else
108
         datab(31 downto 8) := ONES(31 downto 8);
109
      end if;
110
   when mem_write32 =>
111 7 rhoads
      write_next := '1';
112 2 rhoads
      mem_data_w_v := data_write;
113 7 rhoads
      byte_sel_next := "1111";
114 2 rhoads
   when mem_write16 =>
115 7 rhoads
      write_next := '1';
116 2 rhoads
      mem_data_w_v := data_write(15 downto 0) & data_write(15 downto 0);
117
      if address_data(1) = little_endian(1) then
118 7 rhoads
         byte_sel_next := "1100";
119 2 rhoads
      else
120 7 rhoads
         byte_sel_next := "0011";
121 2 rhoads
      end if;
122
   when mem_write8 =>
123 7 rhoads
      write_next := '1';
124 2 rhoads
      mem_data_w_v := data_write(7 downto 0) & data_write(7 downto 0) &
125
                  data_write(7 downto 0) & data_write(7 downto 0);
126
      bits := address_data(1 downto 0) xor little_endian;
127
      case bits is
128
      when "00" =>
129 7 rhoads
         byte_sel_next := "1000";
130 2 rhoads
      when "01" =>
131 7 rhoads
         byte_sel_next := "0100";
132 2 rhoads
      when "10" =>
133 7 rhoads
         byte_sel_next := "0010";
134 2 rhoads
      when others =>
135 7 rhoads
         byte_sel_next := "0001";
136 2 rhoads
      end case;
137
   when others =>
138
   end case;
139
 
140 7 rhoads
   opcode_next := opcode_reg;
141 49 rhoads
   if mem_source = mem_none then --opcode fetch
142
      mem_state_next := STATE_FETCH;
143 2 rhoads
      if pause_in = '0' and mem_pause = '0' then
144 7 rhoads
         opcode_next := data;
145 2 rhoads
      end if;
146 49 rhoads
   else  --data read or write (not opcode fetch)
147
      case mem_state_reg is
148
      when STATE_FETCH =>
149 7 rhoads
         pause := '1';
150
         byte_sel_next := "0000";
151 47 rhoads
         if mem_pause = '0' then
152 49 rhoads
            mem_state_next := STATE_ADDR;
153 47 rhoads
         end if;
154 49 rhoads
      when STATE_ADDR =>  --address lines pre-hold
155 7 rhoads
         address_next := address_data;
156 49 rhoads
         if write_next = '1' and address_data(31) = '0' then
157 7 rhoads
            pause := '1';
158
            byte_sel_next := "0000";
159 47 rhoads
            if mem_pause = '0' then
160 49 rhoads
               mem_state_next := STATE_WRITE;    --4 cycle access
161 47 rhoads
            end if;
162 7 rhoads
         else
163
            if mem_pause = '0' then
164
               opcode_next := next_opcode_reg;
165 49 rhoads
               mem_state_next := STATE_FETCH;    --2 cycle access
166 7 rhoads
            end if;
167
         end if;
168 49 rhoads
      when STATE_WRITE =>
169 7 rhoads
         pause := '1';
170
         address_next := address_data;
171 2 rhoads
         if mem_pause = '0' then
172 49 rhoads
            mem_state_next := STATE_PAUSE;
173 2 rhoads
         end if;
174 49 rhoads
      when OTHERS =>  --STATE_PAUSE address lines post-hold
175 7 rhoads
         address_next := address_data;
176
         byte_sel_next := "0000";
177 47 rhoads
         if mem_pause = '0' then
178 49 rhoads
            opcode_next := next_opcode_reg;
179
            mem_state_next := STATE_FETCH;
180 47 rhoads
         end if;
181 49 rhoads
      end case;
182 2 rhoads
   end if;
183 7 rhoads
 
184 6 rhoads
   if nullify_op = '1' then
185 7 rhoads
      opcode_next := ZERO;  --NOP
186 6 rhoads
   end if;
187 2 rhoads
   if reset_in = '1' then
188 49 rhoads
      mem_state_next := STATE_FETCH;
189 7 rhoads
      opcode_next := ZERO;
190 2 rhoads
   end if;
191
 
192
   if rising_edge(clk) then
193 7 rhoads
      opcode_reg <= opcode_next;
194 49 rhoads
      if mem_state_reg = STATE_FETCH then
195 2 rhoads
         next_opcode_reg <= data;
196
      end if;
197 49 rhoads
      mem_state_reg <= mem_state_next;
198 2 rhoads
   end if;
199
 
200 8 rhoads
   if reset_in = '0' then
201
      opcode_out <= opcode_reg;
202
   else
203
      opcode_out <= ZERO;
204
   end if;
205 2 rhoads
   data_read <= datab;
206
   pause_out <= mem_pause or pause;
207 7 rhoads
   mem_byte_sel <= byte_sel_next;
208
   mem_address <= address_next;
209 49 rhoads
   if write_next = '1' and mem_state_reg /= STATE_FETCH then
210 7 rhoads
      mem_write <= '1';
211
      mem_data_w <= mem_data_w_v;
212
   else
213
      mem_write <= '0';
214
      mem_data_w <= HIGH_Z; --ZERO;
215
   end if;
216 2 rhoads
 
217
end process; --data_proc
218
 
219
end; --architecture logic
220
 

powered by: WebSVN 2.1.0

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