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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cfs.vhd] - Blame information for rev 73

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 47 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - Custom Functions Subsystem (CFS) >>                                              #
3
-- # ********************************************************************************************* #
4 70 zero_gravi
-- # Intended for tightly-coupled, application-specific custom co-processors. This module provides #
5
-- # 32x 32-bit memory-mapped interface registers, one interrupt request signal and custom IO      #
6
-- # conduits for processor-external or chip-external interface.                                   #
7
-- #                                                                                               #
8
-- # NOTE: This is just an example/illustration template. Modify/replace this file to implement    #
9
-- #       your own custom design logic.                                                           #
10 47 zero_gravi
-- # ********************************************************************************************* #
11
-- # BSD 3-Clause License                                                                          #
12
-- #                                                                                               #
13 70 zero_gravi
-- # Copyright (c) 2022, Stephan Nolting. All rights reserved.                                     #
14 47 zero_gravi
-- #                                                                                               #
15
-- # Redistribution and use in source and binary forms, with or without modification, are          #
16
-- # permitted provided that the following conditions are met:                                     #
17
-- #                                                                                               #
18
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
19
-- #    conditions and the following disclaimer.                                                   #
20
-- #                                                                                               #
21
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
22
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
23
-- #    provided with the distribution.                                                            #
24
-- #                                                                                               #
25
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
26
-- #    endorse or promote products derived from this software without specific prior written      #
27
-- #    permission.                                                                                #
28
-- #                                                                                               #
29
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
30
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
31
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
32
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
33
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
34
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
35
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
36
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
37
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
38
-- # ********************************************************************************************* #
39
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
40
-- #################################################################################################
41
 
42
library ieee;
43
use ieee.std_logic_1164.all;
44
use ieee.numeric_std.all;
45
 
46
library neorv32;
47
use neorv32.neorv32_package.all;
48
 
49
entity neorv32_cfs is
50
  generic (
51 52 zero_gravi
    CFS_CONFIG   : std_ulogic_vector(31 downto 0); -- custom CFS configuration generic
52 62 zero_gravi
    CFS_IN_SIZE  : positive; -- size of CFS input conduit in bits
53
    CFS_OUT_SIZE : positive  -- size of CFS output conduit in bits
54 47 zero_gravi
  );
55
  port (
56
    -- host access --
57
    clk_i       : in  std_ulogic; -- global clock line
58
    rstn_i      : in  std_ulogic; -- global reset line, low-active, use as async
59
    addr_i      : in  std_ulogic_vector(31 downto 0); -- address
60
    rden_i      : in  std_ulogic; -- read enable
61
    wren_i      : in  std_ulogic; -- word write enable
62
    data_i      : in  std_ulogic_vector(31 downto 0); -- data in
63
    data_o      : out std_ulogic_vector(31 downto 0); -- data out
64
    ack_o       : out std_ulogic; -- transfer acknowledge
65 68 zero_gravi
    err_o       : out std_ulogic; -- transfer error
66 47 zero_gravi
    -- clock generator --
67
    clkgen_en_o : out std_ulogic; -- enable clock generator
68
    clkgen_i    : in  std_ulogic_vector(07 downto 0); -- "clock" inputs
69
    -- interrupt --
70
    irq_o       : out std_ulogic; -- interrupt request
71
    -- custom io (conduits) --
72 52 zero_gravi
    cfs_in_i    : in  std_ulogic_vector(CFS_IN_SIZE-1 downto 0);  -- custom inputs
73
    cfs_out_o   : out std_ulogic_vector(CFS_OUT_SIZE-1 downto 0)  -- custom outputs
74 47 zero_gravi
  );
75
end neorv32_cfs;
76
 
77
architecture neorv32_cfs_rtl of neorv32_cfs is
78
 
79 73 zero_gravi
  -- IO space: module base address --
80
  -- WARNING: Do not modify the CFS base address or the CFS' occupied address
81
  -- space as this might cause access collisions with other processor modules.
82 47 zero_gravi
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
83
  constant lo_abb_c : natural := index_size_f(cfs_size_c); -- low address boundary bit
84
 
85
  -- access control --
86
  signal acc_en : std_ulogic; -- module access enable
87
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
88
  signal wren   : std_ulogic; -- word write enable
89
  signal rden   : std_ulogic; -- read enable
90
 
91
  -- default CFS interface registers --
92
  type cfs_regs_t is array (0 to 3) of std_ulogic_vector(31 downto 0); -- just implement 4 registers for this example
93
  signal cfs_reg_wr : cfs_regs_t; -- interface registers for WRITE accesses
94
  signal cfs_reg_rd : cfs_regs_t; -- interface registers for READ accesses
95
 
96
begin
97
 
98
  -- Access Control -------------------------------------------------------------------------
99
  -- -------------------------------------------------------------------------------------------
100 73 zero_gravi
  -- This logic is required to handle the CPU accesses - DO NOT MODIFY!
101 47 zero_gravi
  acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = cfs_base_c(hi_abb_c downto lo_abb_c)) else '0';
102
  addr   <= cfs_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
103 73 zero_gravi
  wren   <= acc_en and wren_i; -- only full-word write accesses are supported
104 70 zero_gravi
  rden   <= acc_en and rden_i; -- read accesses always return a full 32-bit word
105 47 zero_gravi
 
106
 
107 52 zero_gravi
  -- CFS Generics ---------------------------------------------------------------------------
108 47 zero_gravi
  -- -------------------------------------------------------------------------------------------
109 70 zero_gravi
  -- In it's default version the CFS provides three configuration generics:
110 73 zero_gravi
  -- > CFS_IN_SIZE  - configures the size (in bits) of the CFS input conduit cfs_in_i
111
  -- > CFS_OUT_SIZE - configures the size (in bits) of the CFS output conduit cfs_out_o
112
  -- > CFS_CONFIG   - is a blank 32-bit generic. It is intended as a "generic conduit" to propagate
113
  --                  custom configuration flags from the top entity down to this module.
114 47 zero_gravi
 
115
 
116
  -- CFS IOs --------------------------------------------------------------------------------
117
  -- -------------------------------------------------------------------------------------------
118 70 zero_gravi
  -- By default, the CFS provides two IO signals (cfs_in_i and cfs_out_o) that are available at the processor's top entity.
119
  -- These are intended as "conduits" to propagate custom signals from this module and the processor top entity.
120 47 zero_gravi
 
121
  cfs_out_o <= (others => '0'); -- not used for this minimal example
122
 
123
 
124
  -- Reset System ---------------------------------------------------------------------------
125
  -- -------------------------------------------------------------------------------------------
126
  -- The CFS can be reset using the global rstn_i signal. This signal should be used as asynchronous reset and is active-low.
127 70 zero_gravi
  -- Note that rstn_i can be asserted by a processor-external reset, the on-chip debugger and also by the watchdog.
128 47 zero_gravi
  --
129 70 zero_gravi
  -- Most default peripheral devices of the NEORV32 do NOT use a dedicated hardware reset at all. Instead, these units are
130
  -- reset by writing ZERO to a specific "control register" located right at the beginning of the device's address space
131
  -- (so this register is cleared at first). The crt0 start-up code writes ZERO to every single address in the processor's
132
  -- IO space - including the CFS. Make sure that this initial clearing does not cause any unintended CFS actions.
133 47 zero_gravi
 
134
 
135
  -- Clock System ---------------------------------------------------------------------------
136
  -- -------------------------------------------------------------------------------------------
137 70 zero_gravi
  -- The processor top unit implements a clock generator providing 8 "derived clocks".
138 47 zero_gravi
  -- Actually, these signals should not be used as direct clock signals, but as *clock enable* signals.
139
  -- clkgen_i is always synchronous to the main system clock (clk_i).
140
  --
141 70 zero_gravi
  -- The following clock dividers are available:
142 73 zero_gravi
  -- > clkgen_i(clk_div2_c)    -> MAIN_CLK/2
143
  -- > clkgen_i(clk_div4_c)    -> MAIN_CLK/4
144
  -- > clkgen_i(clk_div8_c)    -> MAIN_CLK/8
145
  -- > clkgen_i(clk_div64_c)   -> MAIN_CLK/64
146
  -- > clkgen_i(clk_div128_c)  -> MAIN_CLK/128
147
  -- > clkgen_i(clk_div1024_c) -> MAIN_CLK/1024
148
  -- > clkgen_i(clk_div2048_c) -> MAIN_CLK/2048
149
  -- > clkgen_i(clk_div4096_c) -> MAIN_CLK/4096
150 47 zero_gravi
  --
151
  -- For instance, if you want to drive a clock process at MAIN_CLK/8 clock speed you can use the following construct:
152
  --
153
  --   if (rstn_i = '0') then -- async and low-active reset (if required at all)
154
  --   ...
155 73 zero_gravi
  --   elsif rising_edge(clk_i) then -- always use the main clock for all clock processes
156 47 zero_gravi
  --     if (clkgen_i(clk_div8_c) = '1') then -- the div8 "clock" is actually a clock enable
157
  --       ...
158
  --     end if;
159
  --   end if;
160
  --
161 73 zero_gravi
  -- The clkgen_i input clocks are available when at least one IO/peripheral device (for example UART0) requires the clocks
162 70 zero_gravi
  -- generated by the clock generator. The CFS can enable the clock generator by itself by setting the clkgen_en_o signal high.
163
  -- The CFS cannot ensure to deactivate the clock generator by setting the clkgen_en_o signal low as other peripherals might
164
  -- still keep the generator activated. Make sure to deactivate the CFS's clkgen_en_o if no clocks are required in here to
165
  -- reduce dynamic power consumption.
166 47 zero_gravi
 
167
  clkgen_en_o <= '0'; -- not used for this minimal example
168
 
169
 
170
  -- Interrupt ------------------------------------------------------------------------------
171
  -- -------------------------------------------------------------------------------------------
172 70 zero_gravi
  -- The CFS features a single interrupt signal, which is connected to the CPU's "fast interrupt" channel 1 (FIRQ1).
173
  -- The interrupt is triggered by a one-cycle high-level. After triggering, the interrupt appears as "pending" in the CPU's
174
  -- mip CSR ready to trigger execution of the according interrupt handler. It is the task of the application to programmer
175
  -- to enable/clear the CFS interrupt using the CPU's mie and mip registers when required.
176 47 zero_gravi
 
177
  irq_o <= '0'; -- not used for this minimal example
178
 
179
 
180
  -- Read/Write Access ----------------------------------------------------------------------
181
  -- -------------------------------------------------------------------------------------------
182 73 zero_gravi
  -- Here we are reading/writing from/to the interface registers of the module and generate the CPU access handshake (bus response).
183 47 zero_gravi
  --
184 73 zero_gravi
  -- The CFS provides up to 32 memory-mapped 32-bit interface registers. For instance, these could be used to provide a
185 70 zero_gravi
  -- <control register> for global control of the unit, a <data register> for reading/writing from/to a data FIFO, a
186
  -- <command register> for issuing commands and a <status register> for status information.
187 47 zero_gravi
  --
188 70 zero_gravi
  -- Following the interface protocol, each read or write access has to be acknowledged in the following cycle using the ack_o
189 73 zero_gravi
  -- signal (or even later if the module needs additional time). If no ACK is generated at all, the bus access will time out
190
  -- and cause a bus access fault exception.
191 70 zero_gravi
  --
192
  -- This module also provides an optional ERROR signal to indicate a faulty access operation (for example when accessing an
193
  -- unused, read-only or "locked" CFS register address). This signal may only be set when the module is actually accessed
194 73 zero_gravi
  -- and is set INSTEAD of the ACK signal. Setting the ERR signal will raise a bus access exception with a "Device Error" qualifier
195
  -- that can be handled by the application software.
196 47 zero_gravi
 
197 70 zero_gravi
  err_o <= '0'; -- Tie to zero if not explicitly used.
198 47 zero_gravi
 
199 70 zero_gravi
 
200 73 zero_gravi
  -- Host access example: Read and write access to the interface registers + bus transfer acknowledge. This example only
201
  -- implements four physical r/w register (the four lowest CFS registers). The remaining addresses of the CFS are not associated
202
  -- with any physical registers - any access to those is simply ignored but still acknowledged. Only full-word write accesses are
203
  -- supported (and acknowledged) by this example. Sub-word write access will not alter any CFS register state and will cause
204
  -- a "bus store access" exception (with a "Device Timeout" qualifier as not ACK is generated in that case).
205
 
206 47 zero_gravi
  host_access: process(clk_i)
207
  begin
208 70 zero_gravi
    if rising_edge(clk_i) then -- synchronous interface for read and write accesses
209 47 zero_gravi
      -- transfer/access acknowledge --
210 73 zero_gravi
      -- default: required for the CPU to check the CFS is answering a bus read OR write request;
211
      -- all read and write accesses (to any cfs_reg, even if there is no according physical register implemented) will succeed.
212
      ack_o <= rden or wren;
213 47 zero_gravi
 
214
      -- write access --
215 73 zero_gravi
      if (wren = '1') then -- full-word write access, high for one cycle if there is an actual write access
216 64 zero_gravi
        if (addr = cfs_reg0_addr_c) then -- make sure to use the internal "addr" signal for the read/write interface
217 73 zero_gravi
          cfs_reg_wr(0) <= data_i; -- some physical register, for example: control register
218 64 zero_gravi
        end if;
219
        if (addr = cfs_reg1_addr_c) then
220 73 zero_gravi
          cfs_reg_wr(1) <= data_i; -- some physical register, for example: data in/out fifo
221 64 zero_gravi
        end if;
222
        if (addr = cfs_reg2_addr_c) then
223 73 zero_gravi
          cfs_reg_wr(2) <= data_i; -- some physical register, for example: command fifo
224 64 zero_gravi
        end if;
225
        if (addr = cfs_reg3_addr_c) then
226 73 zero_gravi
          cfs_reg_wr(3) <= data_i; -- some physical register, for example: status register
227 64 zero_gravi
        end if;
228 60 zero_gravi
      end if;
229 47 zero_gravi
 
230
      -- read access --
231 73 zero_gravi
      data_o <= (others => '0'); -- the output HAS TO BE ZERO if there is no actual read access
232
      if (rden = '1') then -- the read access is always 32-bit wide, high for one cycle if there is an actual read access
233 47 zero_gravi
        case addr is -- make sure to use the internal 'addr' signal for the read/write interface
234
          when cfs_reg0_addr_c => data_o <= cfs_reg_rd(0);
235
          when cfs_reg1_addr_c => data_o <= cfs_reg_rd(1);
236
          when cfs_reg2_addr_c => data_o <= cfs_reg_rd(2);
237
          when cfs_reg3_addr_c => data_o <= cfs_reg_rd(3);
238
          when others          => data_o <= (others => '0'); -- the remaining registers are not implemented and will read as zero
239
        end case;
240
      end if;
241
    end if;
242
  end process host_access;
243
 
244
 
245
  -- CFS Function Core ----------------------------------------------------------------------
246
  -- -------------------------------------------------------------------------------------------
247 71 zero_gravi
 
248 47 zero_gravi
  -- This is where the actual functionality can be implemented.
249 71 zero_gravi
  -- The logic below is just a very simple example that transforms data
250 73 zero_gravi
  -- from an input register into data in an output register.
251 47 zero_gravi
 
252 73 zero_gravi
  cfs_reg_rd(0) <= bin_to_gray_f(cfs_reg_wr(0)); -- convert binary to gray code
253
  cfs_reg_rd(1) <= gray_to_bin_f(cfs_reg_wr(1)); -- convert gray to binary code
254
  cfs_reg_rd(2) <= bit_rev_f(cfs_reg_wr(2)); -- bit reversal
255
  cfs_reg_rd(3) <= bswap32_f(cfs_reg_wr(3)); -- byte swap (endianness conversion)
256 47 zero_gravi
 
257 73 zero_gravi
 
258 47 zero_gravi
end neorv32_cfs_rtl;

powered by: WebSVN 2.1.0

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