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

Subversion Repositories plasma

[/] [plasma/] [trunk/] [vhdl/] [mem_ctrl.vhd] - Blame information for rev 43

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
   subtype setup_state_type is std_logic_vector(1 downto 0);
52
   signal setup_state : setup_state_type;
53
   constant STATE_FETCH  : setup_state_type := "00";
54
   constant STATE_ADDR   : setup_state_type := "01";
55
   constant STATE_WRITE  : setup_state_type := "10";
56
   constant STATE_PAUSE  : setup_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 7 rhoads
                  opcode_reg, next_opcode_reg, setup_state)
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
   variable setup_state_next : setup_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 7 rhoads
   setup_state_next := setup_state;
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 2 rhoads
   if mem_source = mem_none then
142 7 rhoads
      setup_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
   else
147 7 rhoads
      if setup_state = STATE_FETCH then
148
         pause := '1';
149
         byte_sel_next := "0000";
150
         setup_state_next := STATE_ADDR;
151
      elsif setup_state = STATE_ADDR then
152
         address_next := address_data;
153
         if write_next ='1' and address_data(31) = '0' then
154
            pause := '1';
155
            byte_sel_next := "0000";
156
            setup_state_next := STATE_WRITE;       --4 cycle access
157
         else
158
            if mem_pause = '0' then
159
               opcode_next := next_opcode_reg;
160
               setup_state_next := STATE_FETCH;    --2 cycle access
161
            end if;
162
         end if;
163
      elsif setup_state = STATE_WRITE then
164
         pause := '1';
165
         address_next := address_data;
166 2 rhoads
         if mem_pause = '0' then
167 7 rhoads
            setup_state_next := STATE_PAUSE;
168 2 rhoads
         end if;
169 7 rhoads
      elsif setup_state = STATE_PAUSE then
170
         address_next := address_data;
171
         byte_sel_next := "0000";
172
         opcode_next := next_opcode_reg;
173
         setup_state_next := STATE_FETCH;
174 2 rhoads
      end if;
175
   end if;
176 7 rhoads
 
177 6 rhoads
   if nullify_op = '1' then
178 7 rhoads
      opcode_next := ZERO;  --NOP
179 6 rhoads
   end if;
180 2 rhoads
   if reset_in = '1' then
181 7 rhoads
      setup_state_next := STATE_FETCH;
182
      opcode_next := ZERO;
183 2 rhoads
   end if;
184
 
185
   if rising_edge(clk) then
186 7 rhoads
      opcode_reg <= opcode_next;
187
      if setup_state = STATE_FETCH then
188 2 rhoads
         next_opcode_reg <= data;
189
      end if;
190 7 rhoads
      setup_state <= setup_state_next;
191 2 rhoads
   end if;
192
 
193 8 rhoads
   if reset_in = '0' then
194
      opcode_out <= opcode_reg;
195
   else
196
      opcode_out <= ZERO;
197
   end if;
198 2 rhoads
   data_read <= datab;
199
   pause_out <= mem_pause or pause;
200 7 rhoads
   mem_byte_sel <= byte_sel_next;
201
   mem_address <= address_next;
202
   if write_next = '1' and setup_state /= STATE_FETCH then
203
      mem_write <= '1';
204
      mem_data_w <= mem_data_w_v;
205
   else
206
      mem_write <= '0';
207
      mem_data_w <= HIGH_Z; --ZERO;
208
   end if;
209 2 rhoads
 
210
end process; --data_proc
211
 
212
end; --architecture logic
213
 

powered by: WebSVN 2.1.0

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