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