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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_ram_4k.vhd] - Blame information for rev 333

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 242 jshamlet
-- Copyright (c)2013, 2020 Jeremy Seth Henry
2
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
--
24
-- VHDL Units :  o8_ram_4k
25
-- Description:  Provides a wrapper layer for a 4kx8 RAM model with interface
26
--            :   logic for the Open8 CPU. Also provides an optional write
27
--            :   enable register that prevents regions from being written
28
--            :   by non-ISR code (uses the I flag) as a way to prevent tasks
29
--            :   from inadvertently writing outside of their designated
30
--            :   memory space.
31
--            :  When enabled, the write mask logically divides the memory into
32
--            :   32, 128 byte regions, corresponding to the 32 bits in the WPR
33
--            :   register.
34
--
35
-- WP Register Map:
36
-- Offset  Bitfield Description                        Read/Write
37
--   0x00  AAAAAAAA Region Enables  7:0                  (RW)
38
--   0x01  AAAAAAAA Region Enables 15:8                  (RW)
39
--   0x02  AAAAAAAA Region Enables 23:16                 (RW)
40
--   0x03  AAAAAAAA Region Enables 31:24                 (RW)
41 333 jshamlet
--   0x04  AAAAAAAA Fault Address  7:0                   (RW*)
42
--   0x05  AAAAAAAA Fault Address 15:8                   (RW*)
43 242 jshamlet
--
44 333 jshamlet
-- Note: Writing to 0x04 or 0x05 will reset the faulting address to 0x0000
45
--
46 242 jshamlet
-- Revision History
47
-- Author          Date     Change
48
------------------ -------- ---------------------------------------------------
49
-- Seth Henry      04/16/20 Revision block added
50
-- Seth Henry      05/12/20 Added write protect logic
51 333 jshamlet
-- Seth Henry      10/04/23 Added faulting address register
52 242 jshamlet
 
53
library ieee;
54
use ieee.std_logic_1164.all;
55
use ieee.std_logic_unsigned.all;
56
use ieee.std_logic_arith.all;
57
 
58
library work;
59
  use work.open8_pkg.all;
60
 
61
entity o8_ram_4k is
62
generic(
63
  Write_Protect              : boolean := FALSE;
64
  Default_Mask               : std_logic_vector(31 downto 0) := x"00000000";
65
  Address_WPR                : ADDRESS_TYPE := x"1000";
66
  Address_RAM                : ADDRESS_TYPE
67
);
68
port(
69
  Open8_Bus                  : in  OPEN8_BUS_TYPE;
70 251 jshamlet
  Rd_Data                    : out DATA_TYPE;
71
  Write_Fault                : out std_logic
72 242 jshamlet
);
73
end entity;
74
 
75
architecture behave of o8_ram_4k is
76
 
77
  alias  Clock               is Open8_Bus.Clock;
78
  alias  Reset               is Open8_Bus.Reset;
79
  alias  ISR_En              is Open8_Bus.GP_Flags(EXT_ISR);
80 333 jshamlet
  alias  Full_Address        is Open8_Bus.Address;
81 242 jshamlet
  alias  Wr_En               is Open8_Bus.Wr_En;
82
  alias  Rd_En               is Open8_Bus.Rd_En;
83
 
84 333 jshamlet
  constant WPR_User_Addr     : std_logic_vector(15 downto 3)
85
                               := Address_WPR(15 downto 3);
86 242 jshamlet
 
87
  constant RAM_User_Addr     : std_logic_vector(15 downto 12)
88
                               := Address_RAM(15 downto 12);
89
 
90 333 jshamlet
  alias  WPR_Comp_Addr       is Open8_Bus.Address(15 downto 3);
91 242 jshamlet
  signal WPR_Addr_Match      : std_logic := '0';
92
 
93 333 jshamlet
  alias  WPR_Reg_Sel_d       is Open8_Bus.Address(2 downto 0);
94
  signal WPR_Reg_Sel_q       : std_logic_vector(2 downto 0) :=
95 242 jshamlet
                                (others => '0');
96
 
97
  alias  Wr_Data_d           is Open8_Bus.Wr_Data;
98 244 jshamlet
  signal WPR_Wr_Data_q       : DATA_TYPE := x"00";
99 242 jshamlet
 
100
  signal Write_Mask          : std_logic_vector(31 downto 0) :=
101
                                x"00000000";
102
  alias  Write_Mask_0        is Write_Mask( 7 downto  0);
103
  alias  Write_Mask_1        is Write_Mask(15 downto  8);
104
  alias  Write_Mask_2        is Write_Mask(23 downto 16);
105
  alias  Write_Mask_3        is Write_Mask(31 downto 24);
106
 
107
  signal WPR_Wr_En_d         : std_logic := '0';
108 244 jshamlet
  signal WPR_Wr_En_q         : std_logic := '0';
109 242 jshamlet
  signal WPR_Rd_En_d         : std_logic := '0';
110 244 jshamlet
  signal WPR_Rd_En_q         : std_logic := '0';
111 242 jshamlet
 
112
  alias  RAM_Base_Addr       is Open8_Bus.Address(15 downto 12);
113
  alias  RAM_Addr            is Open8_Bus.Address(11 downto 0);
114
 
115
  alias  RAM_Rgn_Addr        is Open8_Bus.Address(11 downto 7);
116
 
117
  signal RAM_Region_Match    : std_logic := '0';
118
  signal RAM_Addr_Match      : std_logic := '0';
119
 
120 244 jshamlet
  signal RAM_Wr_En_d         : std_logic := '0';
121
  signal RAM_Rd_En_d         : std_logic := '0';
122
  signal RAM_Rd_En_q         : std_logic := '0';
123 242 jshamlet
  signal RAM_Rd_Data         : DATA_TYPE := OPEN8_NULLBUS;
124
 
125 251 jshamlet
  signal Write_Fault_d       : std_logic := '0';
126 333 jshamlet
  signal Write_Fault_q       : std_logic := '0';
127 251 jshamlet
 
128 333 jshamlet
  signal Current_Addr        : ADDRESS_TYPE := x"0000";
129
  signal Fault_Addr          : ADDRESS_TYPE := x"0000";
130
  alias  Fault_Addr_l        is Fault_Addr(7 downto 0);
131
  alias  Fault_Addr_h        is Fault_Addr(15 downto 8);
132 242 jshamlet
begin
133
 
134
Write_Protect_On : if( Write_Protect )generate
135
 
136
  WPR_Addr_Match             <= '1' when WPR_Comp_Addr = WPR_User_Addr else '0';
137 244 jshamlet
  WPR_Wr_En_d                <= WPR_Addr_Match and Wr_En and ISR_En;
138
  WPR_Rd_En_d                <= WPR_Addr_Match and Rd_En;
139 242 jshamlet
 
140
  RAM_Addr_Match             <= '1' when RAM_Base_Addr = RAM_User_Addr else '0';
141
 
142
  RAM_Region_Match           <= Write_Mask(conv_integer(RAM_Rgn_Addr)) or
143
                                ISR_En;
144
 
145 244 jshamlet
  RAM_Rd_En_d                <= RAM_Addr_Match and Rd_En;
146
  RAM_Wr_En_d                <= RAM_Addr_Match and RAM_Region_Match and Wr_En;
147 242 jshamlet
 
148 251 jshamlet
  Write_Fault_d              <= RAM_Addr_Match and (not RAM_Region_Match) and Wr_En;
149
 
150 333 jshamlet
  Write_Fault                <= Write_Fault_q;
151
 
152 242 jshamlet
  RAM_proc: process( Reset, Clock )
153
  begin
154
    if( Reset = Reset_Level )then
155
 
156 244 jshamlet
      WPR_Reg_Sel_q          <= (others => '0');
157
      WPR_Wr_Data_q          <= x"00";
158 242 jshamlet
 
159 244 jshamlet
      WPR_Wr_En_q            <= '0';
160
      WPR_Rd_En_q            <= '0';
161 242 jshamlet
 
162 244 jshamlet
      Write_Mask             <= Default_Mask;
163
 
164
      RAM_Rd_En_q            <= '0';
165 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
166 251 jshamlet
 
167 333 jshamlet
      Write_Fault_q          <= '0';
168
          Current_Addr           <= x"0000";
169 251 jshamlet
 
170 242 jshamlet
    elsif( rising_edge(Clock) )then
171 244 jshamlet
      WPR_Reg_Sel_q          <= WPR_Reg_Sel_d;
172 242 jshamlet
 
173 244 jshamlet
      WPR_Wr_En_q            <= WPR_Wr_En_d;
174
      WPR_Wr_Data_q          <= Wr_Data_d;
175
      if( WPR_Wr_En_q = '1' )then
176
        case( WPR_Reg_Sel_q )is
177 333 jshamlet
          when "000" =>
178 244 jshamlet
            Write_Mask_0     <= WPR_Wr_Data_q;
179 333 jshamlet
          when "001" =>
180 244 jshamlet
            Write_Mask_1     <= WPR_Wr_Data_q;
181 333 jshamlet
          when "010" =>
182 244 jshamlet
            Write_Mask_2     <= WPR_Wr_Data_q;
183 333 jshamlet
          when "011" =>
184 244 jshamlet
            Write_Mask_3     <= WPR_Wr_Data_q;
185 333 jshamlet
                  when "100" | "101" =>
186
                    Fault_Addr       <= (others => '0');
187 242 jshamlet
          when others =>
188
            null;
189
        end case;
190
      end if;
191
 
192 244 jshamlet
      WPR_Rd_En_q            <= WPR_Rd_En_d;
193
      RAM_Rd_En_q            <= RAM_Rd_En_d;
194 243 jshamlet
 
195 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
196 244 jshamlet
      if( RAM_Rd_En_q = '1' )then
197 243 jshamlet
        Rd_Data              <= RAM_Rd_Data;
198 244 jshamlet
      elsif( WPR_Rd_En_q = '1'  )then
199
        case( WPR_Reg_Sel_q )is
200 333 jshamlet
          when "000" =>
201 242 jshamlet
            Rd_Data          <= Write_Mask_0;
202 333 jshamlet
          when "001" =>
203 242 jshamlet
            Rd_Data          <= Write_Mask_1;
204 333 jshamlet
          when "010" =>
205 242 jshamlet
            Rd_Data          <= Write_Mask_2;
206 333 jshamlet
          when "011" =>
207 242 jshamlet
            Rd_Data          <= Write_Mask_3;
208 333 jshamlet
                  when "100" =>
209
                    Rd_Data          <= Fault_Addr_l;
210
                  when "101" =>
211
                    Rd_Data          <= Fault_Addr_h;
212 242 jshamlet
          when others =>
213
            null;
214
        end case;
215
      end if;
216 251 jshamlet
 
217 333 jshamlet
      Write_Fault_q          <= Write_Fault_d;
218 251 jshamlet
 
219 333 jshamlet
          Current_Addr           <= Full_Address;
220
          if( Write_Fault_q = '1' )then
221
             Fault_Addr          <= Current_Addr;
222
      end if;
223
 
224 242 jshamlet
    end if;
225
  end process;
226
 
227
end generate;
228
 
229
Write_Protect_Off : if( not Write_Protect )generate
230
 
231 259 jshamlet
  Write_Fault                <= '0';
232
 
233 242 jshamlet
  RAM_Addr_Match             <= '1' when RAM_Base_Addr = RAM_User_Addr else '0';
234
 
235 244 jshamlet
  RAM_Rd_En_d                <= RAM_Addr_Match and Open8_Bus.Rd_En;
236
  RAM_Wr_En_d                <= RAM_Addr_Match and Open8_Bus.Wr_En;
237 242 jshamlet
 
238
  RAM_proc: process( Reset, Clock )
239
  begin
240
    if( Reset = Reset_Level )then
241 244 jshamlet
      RAM_Rd_En_q            <= '0';
242 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
243
    elsif( rising_edge(Clock) )then
244 244 jshamlet
      RAM_Rd_En_q            <= RAM_Rd_En_d;
245 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
246 244 jshamlet
      if( RAM_Rd_En_q = '1' )then
247 242 jshamlet
        Rd_Data              <= RAM_Rd_Data;
248
      end if;
249
    end if;
250
  end process;
251
 
252
end generate;
253
 
254
  -- Note that this RAM should be created without an output FF (unregistered Q)
255
  U_RAM : entity work.ram_4k_core
256
  port map(
257
    address                  => RAM_Addr,
258
    clock                    => Clock,
259
    data                     => Wr_Data_d,
260 244 jshamlet
    wren                     => RAM_Wr_En_d,
261 242 jshamlet
    q                        => RAM_Rd_Data
262
  );
263
 
264
end architecture;

powered by: WebSVN 2.1.0

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