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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_mtime.vhd] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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
-- # BSD 3-Clause License                                                                          #
12
-- #                                                                                               #
13
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
14
-- #                                                                                               #
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_mtime is
50
  port (
51
    -- host access --
52
    clk_i     : in  std_ulogic; -- global clock line
53
    addr_i    : in  std_ulogic_vector(31 downto 0); -- address
54
    rden_i    : in  std_ulogic; -- read enable
55
    wren_i    : in  std_ulogic; -- write enable
56
    ben_i     : in  std_ulogic_vector(03 downto 0); -- byte write enable
57
    data_i    : in  std_ulogic_vector(31 downto 0); -- data in
58
    data_o    : out std_ulogic_vector(31 downto 0); -- data out
59
    ack_o     : out std_ulogic; -- transfer acknowledge
60
    -- interrupt --
61
    irq_o     : out std_ulogic  -- interrupt request
62
  );
63
end neorv32_mtime;
64
 
65
architecture neorv32_mtime_rtl of neorv32_mtime is
66
 
67
  -- IO space: module base address --
68
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
69
  constant lo_abb_c : natural := index_size_f(mtime_size_c); -- low address boundary bit
70
 
71
  -- access control --
72
  signal acc_en : std_ulogic; -- module access enable
73
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
74
  signal wren   : std_ulogic; -- module access enable
75
 
76
  -- accessible regs --
77
  signal mtime_we        : std_ulogic;
78
  signal mtimecmp        : std_ulogic_vector(63 downto 0);
79
  signal mtime_lo        : std_ulogic_vector(32 downto 0);
80
  signal mtime_lo_msb_ff : std_ulogic;
81
  signal mtime_hi        : std_ulogic_vector(31 downto 0);
82
 
83
  -- irq control --
84
  signal cmp_lo       : std_ulogic;
85
  signal cmp_lo_ff    : std_ulogic;
86
  signal cmp_hi       : std_ulogic;
87
  signal cmp_match_ff : std_ulogic;
88
  signal irq_flag     : std_ulogic;
89
  signal irq_flag_ff  : std_ulogic;
90
 
91
begin
92
 
93
  -- Access Control -------------------------------------------------------------------------
94
  -- -------------------------------------------------------------------------------------------
95
  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';
96
  addr   <= mtime_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
97
  wren   <= acc_en and wren_i;
98
 
99
 
100
  -- Write Access ---------------------------------------------------------------------------
101
  -- -------------------------------------------------------------------------------------------
102
  wr_access: process(clk_i)
103
  begin
104
    if rising_edge(clk_i) then
105
      ack_o <= acc_en and (rden_i or wren_i);
106
      -- mtimecmp low --
107
      if (wren = '1') and (addr = mtime_cmp_lo_addr_c) then
108
        for i in 0 to 3 loop
109
          if (ben_i(i) = '1') then
110
            mtimecmp(00+7+i*8 downto 00+0+i*8) <= data_i(7+i*8 downto 0+i*8);
111
          end if;
112
        end loop; -- byte enable
113
      end if;
114
 
115
      -- mtimecmp high --
116
      if (wren = '1') and (addr = mtime_cmp_hi_addr_c) then
117
        for i in 0 to 3 loop
118
          if (ben_i(i) = '1') then
119
            mtimecmp(32+7+i*8 downto 32+0+i*8) <= data_i(7+i*8 downto 0+i*8);
120
          end if;
121
        end loop; -- byte enable
122
      end if;
123
 
124
      -- any access to mtime at all? --
125
      mtime_we <= '0';
126
      if (wren = '1') and ((addr = mtime_time_lo_addr_c) or (addr = mtime_time_hi_addr_c)) then
127
        mtime_we <= '1';
128
      end if;
129
 
130
      -- mtime low --
131
      mtime_lo_msb_ff <= mtime_lo(mtime_lo'left);
132
      if (wren = '1') and (addr = mtime_time_lo_addr_c) then
133
        mtime_lo(mtime_lo'left) <= '0'; -- clear overflow bit on every access
134
        for i in 0 to 3 loop
135
          if (ben_i(i) = '1') then
136
            mtime_lo(00+7+i*8 downto 00+0+i*8) <= data_i(7+i*8 downto 0+i*8);
137
          end if;
138
        end loop; -- byte enable
139
      else -- incrment
140
        mtime_lo <= std_ulogic_vector(unsigned(mtime_lo) + 1);
141
      end if;
142
 
143
      -- mtime high --
144
      if (wren = '1') and (addr = mtime_time_hi_addr_c) then
145
        for i in 0 to 3 loop
146
          if (ben_i(i) = '1') then
147
            mtime_hi(00+7+i*8 downto 00+0+i*8) <= data_i(7+i*8 downto 0+i*8);
148
          end if;
149
        end loop; -- byte enable
150
      elsif ((mtime_lo_msb_ff xor mtime_lo(mtime_lo'left)) = '1') then -- mtime_lo carry?
151
        mtime_hi <= std_ulogic_vector(unsigned(mtime_hi) + 1);
152
      end if;
153
    end if;
154
  end process wr_access;
155
 
156
 
157
  -- Read Access ----------------------------------------------------------------------------
158
  -- -------------------------------------------------------------------------------------------
159
  rd_access: process(clk_i)
160
  begin
161
    if rising_edge(clk_i) then
162
      data_o <= (others => '0'); -- default
163
      if (rden_i = '1') and (acc_en = '1') then
164
        if (addr = mtime_time_lo_addr_c) then -- mtime LOW
165
          data_o <= mtime_lo(31 downto 00);
166
        elsif (addr = mtime_time_hi_addr_c) then -- mtime HIGH
167
          data_o <= mtime_hi;
168
        elsif (addr = mtime_cmp_lo_addr_c) then -- mtimecmp LOW
169
          data_o <= mtimecmp(31 downto 00);
170
        else -- (addr = mtime_cmp_hi_addr_c) then -- mtimecmp HIGH
171
          data_o <= mtimecmp(63 downto 32);
172
        end if;
173
      end if;
174
    end if;
175
  end process rd_access;
176
 
177
 
178
  -- Comparator -----------------------------------------------------------------------------
179
  -- -------------------------------------------------------------------------------------------
180
  cmp_sync: process(clk_i)
181
  begin
182
    if rising_edge(clk_i) then
183
      cmp_lo_ff    <= cmp_lo;
184
      cmp_match_ff <= cmp_lo_ff and cmp_hi;
185
    end if;
186
  end process cmp_sync;
187
 
188
  -- test words --
189
  cmp_lo <= '1' when (unsigned(mtime_lo(31 downto 00)) >= unsigned(mtimecmp(31 downto 00))) else '0';
190
  cmp_hi <= '1' when (unsigned(mtime_hi(31 downto 00)) >= unsigned(mtimecmp(63 downto 32))) else '0';
191
 
192
 
193
  -- Interrupt Logic ------------------------------------------------------------------------
194
  -- -------------------------------------------------------------------------------------------
195
  irq_ctrl: process(clk_i)
196
  begin
197
    if rising_edge(clk_i) then
198
      irq_flag_ff  <= irq_flag;
199
      if (irq_flag = '0') or (mtime_we = '1') then -- idle or mtime manual write
200
        irq_flag <= '0';
201
        if (cmp_match_ff = '1') then
202
          irq_flag <= '1';
203
        end if;
204
      elsif (wren = '1') and (addr = mtime_cmp_hi_addr_c) then -- ACK
205
        irq_flag <= '0';
206
      end if;
207
    end if;
208
  end process irq_ctrl;
209
 
210
  -- irq output to CPU --
211
  irq_o <= irq_flag and (not irq_flag_ff); -- rising edge detector
212
 
213
 
214
end neorv32_mtime_rtl;

powered by: WebSVN 2.1.0

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