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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_ram_1k.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_1k
25
-- Description:  Provides a wrapper layer for a 1kx8 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 333 jshamlet
--            :   32, 32-byte regions, corresponding to the 32 bits in the WPR
33 242 jshamlet
--            :   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 333 jshamlet
--   0x02  AAAAAAAA Region Enables 23:16                 (RW)
40
--   0x03  AAAAAAAA Region Enables 31:24                 (RW)
41
--   0x04  AAAAAAAA Fault Address  7:0                   (RW*)
42
--   0x05  AAAAAAAA Fault Address 15:8                   (RW*)
43 242 jshamlet
--
44
-- Revision History
45
-- Author          Date     Change
46
------------------ -------- ---------------------------------------------------
47
-- Seth Henry      04/16/20 Revision block added
48
-- Seth Henry      05/12/20 Added write protect logic
49 333 jshamlet
-- Seth Henry      10/04/23 Modified WPR to match 4K RAM and added fault
50
--                           address capture
51 242 jshamlet
 
52
library ieee;
53
use ieee.std_logic_1164.all;
54
use ieee.std_logic_unsigned.all;
55
use ieee.std_logic_arith.all;
56
 
57
library work;
58
  use work.open8_pkg.all;
59
 
60
entity o8_ram_1k is
61
generic(
62
  Write_Protect              : boolean := FALSE;
63 333 jshamlet
  Default_Mask               : std_logic_vector(31 downto 0) := x"00000000";
64 242 jshamlet
  Address_WPR                : ADDRESS_TYPE := x"0400";
65
  Address_RAM                : ADDRESS_TYPE
66
);
67
port(
68
  Open8_Bus                  : in  OPEN8_BUS_TYPE;
69 251 jshamlet
  Rd_Data                    : out DATA_TYPE;
70
  Write_Fault                : out std_logic
71 242 jshamlet
);
72
end entity;
73
 
74
architecture behave of o8_ram_1k is
75
 
76
  alias  Clock               is Open8_Bus.Clock;
77
  alias  Reset               is Open8_Bus.Reset;
78
  alias  ISR_En              is Open8_Bus.GP_Flags(EXT_ISR);
79 333 jshamlet
  alias  Full_Address        is Open8_Bus.Address;
80 242 jshamlet
  alias  Wr_En               is Open8_Bus.Wr_En;
81
  alias  Rd_En               is Open8_Bus.Rd_En;
82
 
83 333 jshamlet
  constant WPR_User_Addr     : std_logic_vector(15 downto 3)
84
                               := Address_WPR(15 downto 3);
85 242 jshamlet
 
86
  constant RAM_User_Addr     : std_logic_vector(15 downto 10)
87
                               := Address_RAM(15 downto 10);
88
 
89 333 jshamlet
  alias  WPR_Comp_Addr       is Open8_Bus.Address(15 downto 3);
90 242 jshamlet
  signal WPR_Addr_Match      : std_logic := '0';
91
 
92 333 jshamlet
  alias  WPR_Reg_Sel_d       is Open8_Bus.Address(2 downto 0);
93
  signal WPR_Reg_Sel_q       : std_logic_vector(2 downto 0) :=
94
                                (others => '0');
95 242 jshamlet
 
96
  alias  Wr_Data_d           is Open8_Bus.Wr_Data;
97 244 jshamlet
  signal WPR_Wr_Data_q       : DATA_TYPE := x"00";
98 242 jshamlet
 
99 333 jshamlet
  signal Write_Mask          : std_logic_vector(31 downto 0) :=
100
                                x"00000000";
101
  alias  Write_Mask_0        is Write_Mask( 7 downto  0);
102
  alias  Write_Mask_1        is Write_Mask(15 downto  8);
103
  alias  Write_Mask_2        is Write_Mask(23 downto 16);
104
  alias  Write_Mask_3        is Write_Mask(31 downto 24);
105 242 jshamlet
 
106 244 jshamlet
  signal WPR_Wr_En_d         : std_logic := '0';
107
  signal WPR_Wr_En_q         : std_logic := '0';
108
  signal WPR_Rd_En_d         : std_logic := '0';
109
  signal WPR_Rd_En_q         : std_logic := '0';
110 242 jshamlet
 
111
  alias  RAM_Base_Addr       is Open8_Bus.Address(15 downto 10);
112
  alias  RAM_Addr            is Open8_Bus.Address(9 downto 0);
113
 
114 333 jshamlet
  alias  RAM_Rgn_Addr        is Open8_Bus.Address(9 downto 5);
115 242 jshamlet
 
116
  signal RAM_Region_Match    : std_logic := '0';
117
  signal RAM_Addr_Match      : std_logic := '0';
118
 
119 244 jshamlet
  signal RAM_Wr_En_d         : std_logic := '0';
120
  signal RAM_Rd_En_d         : std_logic := '0';
121
  signal RAM_Rd_En_q         : std_logic := '0';
122 242 jshamlet
  signal RAM_Rd_Data         : DATA_TYPE := OPEN8_NULLBUS;
123
 
124 251 jshamlet
  signal Write_Fault_d       : std_logic := '0';
125 333 jshamlet
  signal Write_Fault_q       : std_logic := '0';
126 251 jshamlet
 
127 333 jshamlet
  signal Current_Addr        : ADDRESS_TYPE := x"0000";
128
  signal Fault_Addr          : ADDRESS_TYPE := x"0000";
129
  alias  Fault_Addr_l        is Fault_Addr(7 downto 0);
130
  alias  Fault_Addr_h        is Fault_Addr(15 downto 8);
131
 
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 333 jshamlet
      WPR_Reg_Sel_q          <= (others => '0');
156 244 jshamlet
      WPR_Wr_Data_q          <= x"00";
157 242 jshamlet
 
158 244 jshamlet
      WPR_Wr_En_q            <= '0';
159
      WPR_Rd_En_q            <= '0';
160 242 jshamlet
 
161 244 jshamlet
      Write_Mask             <= Default_Mask;
162 242 jshamlet
 
163 244 jshamlet
      RAM_Rd_En_q            <= '0';
164 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
165 251 jshamlet
 
166
      Write_Fault            <= '0';
167 333 jshamlet
          Current_Addr           <= x"0000";
168 251 jshamlet
 
169 242 jshamlet
    elsif( rising_edge(Clock) )then
170 244 jshamlet
      WPR_Reg_Sel_q          <= WPR_Reg_Sel_d;
171 242 jshamlet
 
172 244 jshamlet
      WPR_Wr_En_q            <= WPR_Wr_En_d;
173
      WPR_Wr_Data_q          <= Wr_Data_d;
174
      if( WPR_Wr_En_q = '1' )then
175
        case( WPR_Reg_Sel_q )is
176 333 jshamlet
          when "000" =>
177 244 jshamlet
            Write_Mask_0     <= WPR_Wr_Data_q;
178 333 jshamlet
          when "001" =>
179 244 jshamlet
            Write_Mask_1     <= WPR_Wr_Data_q;
180 333 jshamlet
          when "010" =>
181
            Write_Mask_2     <= WPR_Wr_Data_q;
182
          when "011" =>
183
            Write_Mask_3     <= WPR_Wr_Data_q;
184
                  when "100" | "101" =>
185
                    Fault_Addr       <= (others => '0');
186 242 jshamlet
          when others =>
187
            null;
188
        end case;
189
      end if;
190
 
191 244 jshamlet
      WPR_Rd_En_q            <= WPR_Rd_En_d;
192
      RAM_Rd_En_q            <= RAM_Rd_En_d;
193 243 jshamlet
 
194 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
195 244 jshamlet
      if( RAM_Rd_En_q = '1' )then
196 243 jshamlet
        Rd_Data              <= RAM_Rd_Data;
197 244 jshamlet
      elsif( WPR_Rd_En_q = '1'  )then
198
        case( WPR_Reg_Sel_q )is
199 333 jshamlet
          when "000" =>
200 242 jshamlet
            Rd_Data          <= Write_Mask_0;
201 333 jshamlet
          when "001" =>
202 242 jshamlet
            Rd_Data          <= Write_Mask_1;
203 333 jshamlet
          when "010" =>
204
            Rd_Data          <= Write_Mask_2;
205
          when "011" =>
206
            Rd_Data          <= Write_Mask_3;
207
                  when "100" =>
208
                    Rd_Data          <= Fault_Addr_l;
209
                  when "101" =>
210
                    Rd_Data          <= Fault_Addr_h;
211 242 jshamlet
          when others =>
212
            null;
213
        end case;
214
      end if;
215 251 jshamlet
 
216 333 jshamlet
      Write_Fault_q          <= Write_Fault_d;
217 251 jshamlet
 
218 333 jshamlet
          Current_Addr           <= Full_Address;
219
          if( Write_Fault_q = '1' )then
220
             Fault_Addr          <= Current_Addr;
221
      end if;
222
 
223 242 jshamlet
    end if;
224
  end process;
225
 
226
end generate;
227
 
228
Write_Protect_Off : if( not Write_Protect )generate
229
 
230 259 jshamlet
  Write_Fault                <= '0';
231
 
232 242 jshamlet
  RAM_Addr_Match             <= '1' when RAM_Base_Addr = RAM_User_Addr else '0';
233
 
234 244 jshamlet
  RAM_Rd_En_d                <= RAM_Addr_Match and Open8_Bus.Rd_En;
235
  RAM_Wr_En_d                <= RAM_Addr_Match and Open8_Bus.Wr_En;
236 242 jshamlet
 
237
  RAM_proc: process( Reset, Clock )
238
  begin
239
    if( Reset = Reset_Level )then
240 244 jshamlet
      RAM_Rd_En_q            <= '0';
241 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
242
    elsif( rising_edge(Clock) )then
243 244 jshamlet
      RAM_Rd_En_q            <= RAM_Rd_En_d;
244 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
245 244 jshamlet
      if( RAM_Rd_En_q = '1' )then
246 242 jshamlet
        Rd_Data              <= RAM_Rd_Data;
247
      end if;
248
    end if;
249
  end process;
250
 
251
end generate;
252
 
253
  -- Note that this RAM should be created without an output FF (unregistered Q)
254
  U_RAM : entity work.ram_1k_core
255
  port map(
256
    address                  => RAM_Addr,
257
    clock                    => Clock,
258
    data                     => Wr_Data_d,
259 244 jshamlet
    wren                     => RAM_Wr_En_d,
260 242 jshamlet
    q                        => RAM_Rd_Data
261
  );
262
 
263
end architecture;

powered by: WebSVN 2.1.0

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