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

Subversion Repositories neorv32

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

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 4 zero_gravi
    rstn_i    : in  std_ulogic := '0'; -- global reset, low-active, async
54 2 zero_gravi
    addr_i    : in  std_ulogic_vector(31 downto 0); -- address
55
    rden_i    : in  std_ulogic; -- read enable
56
    wren_i    : in  std_ulogic; -- write enable
57
    ben_i     : in  std_ulogic_vector(03 downto 0); -- byte 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
    -- interrupt --
62
    irq_o     : out std_ulogic  -- interrupt request
63
  );
64
end neorv32_mtime;
65
 
66
architecture neorv32_mtime_rtl of neorv32_mtime is
67
 
68
  -- IO space: module base address --
69
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
70
  constant lo_abb_c : natural := index_size_f(mtime_size_c); -- low address boundary bit
71
 
72
  -- access control --
73
  signal acc_en : std_ulogic; -- module access enable
74
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
75
  signal wren   : std_ulogic; -- module access enable
76
 
77
  -- accessible regs --
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 4 zero_gravi
  -- System Time Update ---------------------------------------------------------------------
101
  -- -------------------------------------------------------------------------------------------
102
  system_time: process(clk_i)
103
  begin
104
    if rising_edge(clk_i) then
105
      if (rstn_i = '0') then
106
        mtime_lo <= (others => '0');
107
        mtime_hi <= (others => '0');
108
      else
109
        -- mtime low --
110
        mtime_lo <= std_ulogic_vector(unsigned(mtime_lo) + 1);
111
        -- mtime high --
112
        if ((mtime_lo_msb_ff xor mtime_lo(mtime_lo'left)) = '1') then -- mtime_lo carry?
113
          mtime_hi <= std_ulogic_vector(unsigned(mtime_hi) + 1);
114
        end if;
115
      end if;
116
    end if;
117
  end process system_time;
118
 
119
 
120 2 zero_gravi
  -- Write Access ---------------------------------------------------------------------------
121
  -- -------------------------------------------------------------------------------------------
122
  wr_access: process(clk_i)
123
  begin
124
    if rising_edge(clk_i) then
125
      ack_o <= acc_en and (rden_i or wren_i);
126
      -- mtimecmp low --
127
      if (wren = '1') and (addr = mtime_cmp_lo_addr_c) then
128
        for i in 0 to 3 loop
129
          if (ben_i(i) = '1') then
130
            mtimecmp(00+7+i*8 downto 00+0+i*8) <= data_i(7+i*8 downto 0+i*8);
131
          end if;
132
        end loop; -- byte enable
133
      end if;
134
 
135
      -- mtimecmp high --
136
      if (wren = '1') and (addr = mtime_cmp_hi_addr_c) then
137
        for i in 0 to 3 loop
138
          if (ben_i(i) = '1') then
139
            mtimecmp(32+7+i*8 downto 32+0+i*8) <= data_i(7+i*8 downto 0+i*8);
140
          end if;
141
        end loop; -- byte enable
142
      end if;
143
    end if;
144
  end process wr_access;
145
 
146
 
147
  -- Read Access ----------------------------------------------------------------------------
148
  -- -------------------------------------------------------------------------------------------
149
  rd_access: process(clk_i)
150
  begin
151
    if rising_edge(clk_i) then
152
      data_o <= (others => '0'); -- default
153
      if (rden_i = '1') and (acc_en = '1') then
154
        if (addr = mtime_time_lo_addr_c) then -- mtime LOW
155
          data_o <= mtime_lo(31 downto 00);
156
        elsif (addr = mtime_time_hi_addr_c) then -- mtime HIGH
157
          data_o <= mtime_hi;
158
        elsif (addr = mtime_cmp_lo_addr_c) then -- mtimecmp LOW
159
          data_o <= mtimecmp(31 downto 00);
160
        else -- (addr = mtime_cmp_hi_addr_c) then -- mtimecmp HIGH
161
          data_o <= mtimecmp(63 downto 32);
162
        end if;
163
      end if;
164
    end if;
165
  end process rd_access;
166
 
167
 
168
  -- Comparator -----------------------------------------------------------------------------
169
  -- -------------------------------------------------------------------------------------------
170
  cmp_sync: process(clk_i)
171
  begin
172
    if rising_edge(clk_i) then
173
      cmp_lo_ff    <= cmp_lo;
174
      cmp_match_ff <= cmp_lo_ff and cmp_hi;
175
    end if;
176
  end process cmp_sync;
177
 
178
  -- test words --
179
  cmp_lo <= '1' when (unsigned(mtime_lo(31 downto 00)) >= unsigned(mtimecmp(31 downto 00))) else '0';
180
  cmp_hi <= '1' when (unsigned(mtime_hi(31 downto 00)) >= unsigned(mtimecmp(63 downto 32))) else '0';
181
 
182
 
183
  -- Interrupt Logic ------------------------------------------------------------------------
184
  -- -------------------------------------------------------------------------------------------
185
  irq_ctrl: process(clk_i)
186
  begin
187
    if rising_edge(clk_i) then
188 4 zero_gravi
      if (rstn_i = '0') then
189
        irq_flag_ff <= '0';
190
        irq_flag    <= '0';
191
      else
192
        irq_flag_ff  <= irq_flag;
193
        if (irq_flag = '0') then -- idle
194
          irq_flag <= '0';
195
          if (cmp_match_ff = '1') then
196
            irq_flag <= '1';
197
          end if;
198
        elsif (wren = '1') and (addr = mtime_cmp_hi_addr_c) then -- ACK
199
          irq_flag <= '0';
200 2 zero_gravi
        end if;
201
      end if;
202
    end if;
203
  end process irq_ctrl;
204
 
205
  -- irq output to CPU --
206
  irq_o <= irq_flag and (not irq_flag_ff); -- rising edge detector
207
 
208
 
209
end neorv32_mtime_rtl;

powered by: WebSVN 2.1.0

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