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