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 244

Go to most recent revision | 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
--
42
-- Revision History
43
-- Author          Date     Change
44
------------------ -------- ---------------------------------------------------
45
-- Seth Henry      04/16/20 Revision block added
46
-- Seth Henry      05/12/20 Added write protect logic
47
 
48
library ieee;
49
use ieee.std_logic_1164.all;
50
use ieee.std_logic_unsigned.all;
51
use ieee.std_logic_arith.all;
52
 
53
library work;
54
  use work.open8_pkg.all;
55
 
56
entity o8_ram_4k is
57
generic(
58
  Write_Protect              : boolean := FALSE;
59
  Default_Mask               : std_logic_vector(31 downto 0) := x"00000000";
60
  Address_WPR                : ADDRESS_TYPE := x"1000";
61
  Address_RAM                : ADDRESS_TYPE
62
);
63
port(
64
  Open8_Bus                  : in  OPEN8_BUS_TYPE;
65
  Rd_Data                    : out DATA_TYPE
66
);
67
end entity;
68
 
69
architecture behave of o8_ram_4k is
70
 
71
  alias  Clock               is Open8_Bus.Clock;
72
  alias  Reset               is Open8_Bus.Reset;
73
  alias  ISR_En              is Open8_Bus.GP_Flags(EXT_ISR);
74
  alias  Wr_En               is Open8_Bus.Wr_En;
75
  alias  Rd_En               is Open8_Bus.Rd_En;
76
 
77
  constant WPR_User_Addr     : std_logic_vector(15 downto 2)
78
                               := Address_WPR(15 downto 2);
79
 
80
  constant RAM_User_Addr     : std_logic_vector(15 downto 12)
81
                               := Address_RAM(15 downto 12);
82
 
83
  alias  WPR_Comp_Addr       is Open8_Bus.Address(15 downto 2);
84
  signal WPR_Addr_Match      : std_logic := '0';
85
 
86
  alias  WPR_Reg_Sel_d       is Open8_Bus.Address(1 downto 0);
87 244 jshamlet
  signal WPR_Reg_Sel_q       : std_logic_vector(1 downto 0) :=
88 242 jshamlet
                                (others => '0');
89
 
90
  alias  Wr_Data_d           is Open8_Bus.Wr_Data;
91 244 jshamlet
  signal WPR_Wr_Data_q       : DATA_TYPE := x"00";
92 242 jshamlet
 
93
  signal Write_Mask          : std_logic_vector(31 downto 0) :=
94
                                x"00000000";
95
  alias  Write_Mask_0        is Write_Mask( 7 downto  0);
96
  alias  Write_Mask_1        is Write_Mask(15 downto  8);
97
  alias  Write_Mask_2        is Write_Mask(23 downto 16);
98
  alias  Write_Mask_3        is Write_Mask(31 downto 24);
99
 
100
  signal WPR_Wr_En_d         : std_logic := '0';
101 244 jshamlet
  signal WPR_Wr_En_q         : std_logic := '0';
102 242 jshamlet
  signal WPR_Rd_En_d         : std_logic := '0';
103 244 jshamlet
  signal WPR_Rd_En_q         : std_logic := '0';
104 242 jshamlet
 
105
  alias  RAM_Base_Addr       is Open8_Bus.Address(15 downto 12);
106
  alias  RAM_Addr            is Open8_Bus.Address(11 downto 0);
107
 
108
  alias  RAM_Rgn_Addr        is Open8_Bus.Address(11 downto 7);
109
 
110
  signal RAM_Region_Match    : std_logic := '0';
111
  signal RAM_Addr_Match      : std_logic := '0';
112
 
113 244 jshamlet
  signal RAM_Wr_En_d         : std_logic := '0';
114
  signal RAM_Rd_En_d         : std_logic := '0';
115
  signal RAM_Rd_En_q         : std_logic := '0';
116 242 jshamlet
  signal RAM_Rd_Data         : DATA_TYPE := OPEN8_NULLBUS;
117
 
118
begin
119
 
120
Write_Protect_On : if( Write_Protect )generate
121
 
122
  WPR_Addr_Match             <= '1' when WPR_Comp_Addr = WPR_User_Addr else '0';
123 244 jshamlet
  WPR_Wr_En_d                <= WPR_Addr_Match and Wr_En and ISR_En;
124
  WPR_Rd_En_d                <= WPR_Addr_Match and Rd_En;
125 242 jshamlet
 
126
  RAM_Addr_Match             <= '1' when RAM_Base_Addr = RAM_User_Addr else '0';
127
 
128
  RAM_Region_Match           <= Write_Mask(conv_integer(RAM_Rgn_Addr)) or
129
                                ISR_En;
130
 
131 244 jshamlet
  RAM_Rd_En_d                <= RAM_Addr_Match and Rd_En;
132
  RAM_Wr_En_d                <= RAM_Addr_Match and RAM_Region_Match and Wr_En;
133 242 jshamlet
 
134
  RAM_proc: process( Reset, Clock )
135
  begin
136
    if( Reset = Reset_Level )then
137
 
138 244 jshamlet
      WPR_Reg_Sel_q          <= (others => '0');
139
      WPR_Wr_Data_q          <= x"00";
140 242 jshamlet
 
141 244 jshamlet
      WPR_Wr_En_q            <= '0';
142
      WPR_Rd_En_q            <= '0';
143 242 jshamlet
 
144 244 jshamlet
      Write_Mask             <= Default_Mask;
145
 
146
      RAM_Rd_En_q            <= '0';
147 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
148
    elsif( rising_edge(Clock) )then
149 244 jshamlet
      WPR_Reg_Sel_q          <= WPR_Reg_Sel_d;
150 242 jshamlet
 
151 244 jshamlet
      WPR_Wr_En_q            <= WPR_Wr_En_d;
152
      WPR_Wr_Data_q          <= Wr_Data_d;
153
      if( WPR_Wr_En_q = '1' )then
154
        case( WPR_Reg_Sel_q )is
155 242 jshamlet
          when "00" =>
156 244 jshamlet
            Write_Mask_0     <= WPR_Wr_Data_q;
157 242 jshamlet
          when "01" =>
158 244 jshamlet
            Write_Mask_1     <= WPR_Wr_Data_q;
159 242 jshamlet
          when "10" =>
160 244 jshamlet
            Write_Mask_2     <= WPR_Wr_Data_q;
161 242 jshamlet
          when "11" =>
162 244 jshamlet
            Write_Mask_3     <= WPR_Wr_Data_q;
163 242 jshamlet
          when others =>
164
            null;
165
        end case;
166
      end if;
167
 
168 244 jshamlet
      WPR_Rd_En_q            <= WPR_Rd_En_d;
169
      RAM_Rd_En_q            <= RAM_Rd_En_d;
170 243 jshamlet
 
171 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
172 244 jshamlet
      if( RAM_Rd_En_q = '1' )then
173 243 jshamlet
        Rd_Data              <= RAM_Rd_Data;
174 244 jshamlet
      elsif( WPR_Rd_En_q = '1'  )then
175
        case( WPR_Reg_Sel_q )is
176 242 jshamlet
          when "00" =>
177
            Rd_Data          <= Write_Mask_0;
178
          when "01" =>
179
            Rd_Data          <= Write_Mask_1;
180
          when "10" =>
181
            Rd_Data          <= Write_Mask_2;
182
          when "11" =>
183
            Rd_Data          <= Write_Mask_3;
184
          when others =>
185
            null;
186
        end case;
187
      end if;
188
    end if;
189
  end process;
190
 
191
end generate;
192
 
193
Write_Protect_Off : if( not Write_Protect )generate
194
 
195
  RAM_Addr_Match             <= '1' when RAM_Base_Addr = RAM_User_Addr else '0';
196
 
197 244 jshamlet
  RAM_Rd_En_d                <= RAM_Addr_Match and Open8_Bus.Rd_En;
198
  RAM_Wr_En_d                <= RAM_Addr_Match and Open8_Bus.Wr_En;
199 242 jshamlet
 
200
  RAM_proc: process( Reset, Clock )
201
  begin
202
    if( Reset = Reset_Level )then
203 244 jshamlet
      RAM_Rd_En_q            <= '0';
204 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
205
    elsif( rising_edge(Clock) )then
206 244 jshamlet
      RAM_Rd_En_q            <= RAM_Rd_En_d;
207 242 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
208 244 jshamlet
      if( RAM_Rd_En_q = '1' )then
209 242 jshamlet
        Rd_Data              <= RAM_Rd_Data;
210
      end if;
211
    end if;
212
  end process;
213
 
214
end generate;
215
 
216
  -- Note that this RAM should be created without an output FF (unregistered Q)
217
  U_RAM : entity work.ram_4k_core
218
  port map(
219
    address                  => RAM_Addr,
220
    clock                    => Clock,
221
    data                     => Wr_Data_d,
222 244 jshamlet
    wren                     => RAM_Wr_En_d,
223 242 jshamlet
    q                        => RAM_Rd_Data
224
  );
225
 
226
end architecture;

powered by: WebSVN 2.1.0

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