1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - Machine System Timer (MTIME) >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
56 |
zero_gravi |
-- # Compatible to RISC-V spec's 64-bit MACHINE system timer including "mtime[h]" & "mtimecmp[h]". #
|
5 |
57 |
zero_gravi |
-- # Note: The 64-bit counter and compare systems are de-coupled into two 32-bit systems. #
|
6 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
7 |
|
|
-- # BSD 3-Clause License #
|
8 |
|
|
-- # #
|
9 |
56 |
zero_gravi |
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
10 |
2 |
zero_gravi |
-- # #
|
11 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
12 |
|
|
-- # permitted provided that the following conditions are met: #
|
13 |
|
|
-- # #
|
14 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
15 |
|
|
-- # conditions and the following disclaimer. #
|
16 |
|
|
-- # #
|
17 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
18 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
19 |
|
|
-- # provided with the distribution. #
|
20 |
|
|
-- # #
|
21 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
22 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
23 |
|
|
-- # permission. #
|
24 |
|
|
-- # #
|
25 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
26 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
27 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
28 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
29 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
30 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
31 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
32 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
33 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
34 |
|
|
-- # ********************************************************************************************* #
|
35 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
36 |
|
|
-- #################################################################################################
|
37 |
|
|
|
38 |
|
|
library ieee;
|
39 |
|
|
use ieee.std_logic_1164.all;
|
40 |
|
|
use ieee.numeric_std.all;
|
41 |
|
|
|
42 |
|
|
library neorv32;
|
43 |
|
|
use neorv32.neorv32_package.all;
|
44 |
|
|
|
45 |
|
|
entity neorv32_mtime is
|
46 |
|
|
port (
|
47 |
|
|
-- host access --
|
48 |
|
|
clk_i : in std_ulogic; -- global clock line
|
49 |
4 |
zero_gravi |
rstn_i : in std_ulogic := '0'; -- global reset, low-active, async
|
50 |
2 |
zero_gravi |
addr_i : in std_ulogic_vector(31 downto 0); -- address
|
51 |
|
|
rden_i : in std_ulogic; -- read enable
|
52 |
|
|
wren_i : in std_ulogic; -- write enable
|
53 |
|
|
data_i : in std_ulogic_vector(31 downto 0); -- data in
|
54 |
|
|
data_o : out std_ulogic_vector(31 downto 0); -- data out
|
55 |
|
|
ack_o : out std_ulogic; -- transfer acknowledge
|
56 |
11 |
zero_gravi |
-- time output for CPU --
|
57 |
|
|
time_o : out std_ulogic_vector(63 downto 0); -- current system time
|
58 |
2 |
zero_gravi |
-- interrupt --
|
59 |
|
|
irq_o : out std_ulogic -- interrupt request
|
60 |
|
|
);
|
61 |
|
|
end neorv32_mtime;
|
62 |
|
|
|
63 |
|
|
architecture neorv32_mtime_rtl of neorv32_mtime is
|
64 |
|
|
|
65 |
|
|
-- IO space: module base address --
|
66 |
|
|
constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
|
67 |
|
|
constant lo_abb_c : natural := index_size_f(mtime_size_c); -- low address boundary bit
|
68 |
|
|
|
69 |
|
|
-- access control --
|
70 |
|
|
signal acc_en : std_ulogic; -- module access enable
|
71 |
|
|
signal addr : std_ulogic_vector(31 downto 0); -- access address
|
72 |
|
|
signal wren : std_ulogic; -- module access enable
|
73 |
|
|
|
74 |
57 |
zero_gravi |
-- time write access buffer --
|
75 |
|
|
signal wdata_buf : std_ulogic_vector(31 downto 0);
|
76 |
|
|
signal mtime_lo_we : std_ulogic;
|
77 |
|
|
signal mtime_hi_we : std_ulogic;
|
78 |
|
|
|
79 |
2 |
zero_gravi |
-- accessible regs --
|
80 |
11 |
zero_gravi |
signal mtimecmp_lo : std_ulogic_vector(31 downto 0);
|
81 |
|
|
signal mtimecmp_hi : std_ulogic_vector(31 downto 0);
|
82 |
2 |
zero_gravi |
signal mtime_lo : std_ulogic_vector(32 downto 0);
|
83 |
|
|
signal mtime_lo_msb_ff : std_ulogic;
|
84 |
|
|
signal mtime_hi : std_ulogic_vector(31 downto 0);
|
85 |
57 |
zero_gravi |
signal inc_hi : std_ulogic_vector(31 downto 0);
|
86 |
2 |
zero_gravi |
|
87 |
|
|
-- irq control --
|
88 |
|
|
signal cmp_lo : std_ulogic;
|
89 |
|
|
signal cmp_lo_ff : std_ulogic;
|
90 |
|
|
signal cmp_hi : std_ulogic;
|
91 |
|
|
signal cmp_match_ff : std_ulogic;
|
92 |
|
|
|
93 |
|
|
begin
|
94 |
|
|
|
95 |
|
|
-- Access Control -------------------------------------------------------------------------
|
96 |
|
|
-- -------------------------------------------------------------------------------------------
|
97 |
|
|
acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = mtime_base_c(hi_abb_c downto lo_abb_c)) else '0';
|
98 |
|
|
addr <= mtime_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
|
99 |
22 |
zero_gravi |
wren <= acc_en and wren_i;
|
100 |
2 |
zero_gravi |
|
101 |
|
|
|
102 |
11 |
zero_gravi |
-- Write Access ---------------------------------------------------------------------------
|
103 |
4 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
104 |
11 |
zero_gravi |
wr_access: process(clk_i)
|
105 |
4 |
zero_gravi |
begin
|
106 |
|
|
if rising_edge(clk_i) then
|
107 |
57 |
zero_gravi |
-- mtimecmp --
|
108 |
|
|
if (wren = '1') then
|
109 |
|
|
if (addr = mtime_cmp_lo_addr_c) then
|
110 |
|
|
mtimecmp_lo <= data_i;
|
111 |
|
|
end if;
|
112 |
|
|
if (addr = mtime_cmp_hi_addr_c) then
|
113 |
|
|
mtimecmp_hi <= data_i;
|
114 |
|
|
end if;
|
115 |
4 |
zero_gravi |
end if;
|
116 |
|
|
|
117 |
57 |
zero_gravi |
-- mtime access buffer --
|
118 |
|
|
wdata_buf <= data_i;
|
119 |
|
|
mtime_lo_we <= wren and bool_to_ulogic_f(boolean(addr = mtime_time_lo_addr_c));
|
120 |
|
|
mtime_hi_we <= wren and bool_to_ulogic_f(boolean(addr = mtime_time_hi_addr_c));
|
121 |
25 |
zero_gravi |
|
122 |
11 |
zero_gravi |
-- mtime low --
|
123 |
57 |
zero_gravi |
if (mtime_lo_we = '1') then -- write access
|
124 |
11 |
zero_gravi |
mtime_lo_msb_ff <= '0';
|
125 |
57 |
zero_gravi |
mtime_lo <= '0' & wdata_buf;
|
126 |
11 |
zero_gravi |
else -- auto increment
|
127 |
|
|
mtime_lo_msb_ff <= mtime_lo(mtime_lo'left);
|
128 |
|
|
mtime_lo <= std_ulogic_vector(unsigned(mtime_lo) + 1);
|
129 |
2 |
zero_gravi |
end if;
|
130 |
|
|
|
131 |
11 |
zero_gravi |
-- mtime high --
|
132 |
57 |
zero_gravi |
if (mtime_hi_we = '1') then -- write access
|
133 |
|
|
mtime_hi <= wdata_buf;
|
134 |
|
|
else -- auto increment (if mtime.low overflows)
|
135 |
|
|
mtime_hi <= std_ulogic_vector(unsigned(mtime_hi) + unsigned(inc_hi));
|
136 |
2 |
zero_gravi |
end if;
|
137 |
|
|
end if;
|
138 |
|
|
end process wr_access;
|
139 |
|
|
|
140 |
57 |
zero_gravi |
-- mtime.time_HI increment (0 or 1) --
|
141 |
|
|
inc_hi(0) <= mtime_lo_msb_ff xor mtime_lo(mtime_lo'left);
|
142 |
|
|
inc_hi(31 downto 1) <= (others => '0');
|
143 |
2 |
zero_gravi |
|
144 |
57 |
zero_gravi |
|
145 |
2 |
zero_gravi |
-- Read Access ----------------------------------------------------------------------------
|
146 |
|
|
-- -------------------------------------------------------------------------------------------
|
147 |
|
|
rd_access: process(clk_i)
|
148 |
|
|
begin
|
149 |
|
|
if rising_edge(clk_i) then
|
150 |
11 |
zero_gravi |
ack_o <= acc_en and (rden_i or wren_i);
|
151 |
2 |
zero_gravi |
data_o <= (others => '0'); -- default
|
152 |
|
|
if (rden_i = '1') and (acc_en = '1') then
|
153 |
25 |
zero_gravi |
case addr is
|
154 |
|
|
when mtime_time_lo_addr_c => -- mtime LOW
|
155 |
|
|
data_o <= mtime_lo(31 downto 00);
|
156 |
|
|
when mtime_time_hi_addr_c => -- mtime HIGH
|
157 |
|
|
data_o <= mtime_hi;
|
158 |
|
|
when mtime_cmp_lo_addr_c => -- mtimecmp LOW
|
159 |
|
|
data_o <= mtimecmp_lo;
|
160 |
56 |
zero_gravi |
when others => -- mtime_cmp_hi_addr_c -- mtimecmp HIGH
|
161 |
25 |
zero_gravi |
data_o <= mtimecmp_hi;
|
162 |
|
|
end case;
|
163 |
2 |
zero_gravi |
end if;
|
164 |
|
|
end if;
|
165 |
|
|
end process rd_access;
|
166 |
|
|
|
167 |
26 |
zero_gravi |
-- system time output for cpu --
|
168 |
11 |
zero_gravi |
time_o <= mtime_hi & mtime_lo(31 downto 00);
|
169 |
2 |
zero_gravi |
|
170 |
11 |
zero_gravi |
|
171 |
2 |
zero_gravi |
-- Comparator -----------------------------------------------------------------------------
|
172 |
|
|
-- -------------------------------------------------------------------------------------------
|
173 |
|
|
cmp_sync: process(clk_i)
|
174 |
|
|
begin
|
175 |
|
|
if rising_edge(clk_i) then
|
176 |
|
|
cmp_lo_ff <= cmp_lo;
|
177 |
|
|
cmp_match_ff <= cmp_lo_ff and cmp_hi;
|
178 |
11 |
zero_gravi |
irq_o <= cmp_lo_ff and cmp_hi and (not cmp_match_ff);
|
179 |
2 |
zero_gravi |
end if;
|
180 |
|
|
end process cmp_sync;
|
181 |
|
|
|
182 |
|
|
-- test words --
|
183 |
11 |
zero_gravi |
cmp_lo <= '1' when (unsigned(mtime_lo(31 downto 00)) >= unsigned(mtimecmp_lo)) else '0';
|
184 |
|
|
cmp_hi <= '1' when (unsigned(mtime_hi(31 downto 00)) >= unsigned(mtimecmp_hi)) else '0';
|
185 |
2 |
zero_gravi |
|
186 |
|
|
|
187 |
|
|
end neorv32_mtime_rtl;
|