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

Subversion Repositories mlite

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

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

powered by: WebSVN 2.1.0

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