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

Subversion Repositories neorv32

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

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 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 60 zero_gravi
    clk_i  : in  std_ulogic; -- global clock line
49
    addr_i : in  std_ulogic_vector(31 downto 0); -- address
50
    rden_i : in  std_ulogic; -- read enable
51
    wren_i : in  std_ulogic; -- write enable
52
    data_i : in  std_ulogic_vector(31 downto 0); -- data in
53
    data_o : out std_ulogic_vector(31 downto 0); -- data out
54
    ack_o  : out std_ulogic; -- transfer acknowledge
55 11 zero_gravi
    -- time output for CPU --
56 60 zero_gravi
    time_o : out std_ulogic_vector(63 downto 0); -- current system time
57 2 zero_gravi
    -- interrupt --
58 60 zero_gravi
    irq_o  : out std_ulogic  -- interrupt request
59 2 zero_gravi
  );
60
end neorv32_mtime;
61
 
62
architecture neorv32_mtime_rtl of neorv32_mtime is
63
 
64
  -- IO space: module base address --
65
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
66
  constant lo_abb_c : natural := index_size_f(mtime_size_c); -- low address boundary bit
67
 
68
  -- access control --
69
  signal acc_en : std_ulogic; -- module access enable
70
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
71
  signal wren   : std_ulogic; -- module access enable
72
 
73 57 zero_gravi
  -- time write access buffer --
74
  signal mtime_lo_we : std_ulogic;
75
  signal mtime_hi_we : std_ulogic;
76
 
77 2 zero_gravi
  -- accessible regs --
78 61 zero_gravi
  signal mtimecmp_lo   : std_ulogic_vector(31 downto 0);
79
  signal mtimecmp_hi   : std_ulogic_vector(31 downto 0);
80
  signal mtime_lo      : std_ulogic_vector(31 downto 0);
81
  signal mtime_lo_nxt  : std_ulogic_vector(32 downto 0);
82
  signal mtime_lo_ovfl : std_ulogic_vector(00 downto 0);
83
  signal mtime_hi      : std_ulogic_vector(31 downto 0);
84 2 zero_gravi
 
85 64 zero_gravi
  -- comparators --
86
  signal cmp_lo_ge    : std_ulogic;
87
  signal cmp_lo_ge_ff : std_ulogic;
88
  signal cmp_hi_eq    : std_ulogic;
89
  signal cmp_hi_gt    : std_ulogic;
90 2 zero_gravi
 
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 22 zero_gravi
  wren   <= acc_en and wren_i;
98 2 zero_gravi
 
99
 
100 11 zero_gravi
  -- Write Access ---------------------------------------------------------------------------
101 4 zero_gravi
  -- -------------------------------------------------------------------------------------------
102 11 zero_gravi
  wr_access: process(clk_i)
103 4 zero_gravi
  begin
104
    if rising_edge(clk_i) then
105 57 zero_gravi
      -- mtimecmp --
106
      if (wren = '1') then
107
        if (addr = mtime_cmp_lo_addr_c) then
108
          mtimecmp_lo <= data_i;
109
        end if;
110
        if (addr = mtime_cmp_hi_addr_c) then
111
          mtimecmp_hi <= data_i;
112
        end if;
113 4 zero_gravi
      end if;
114
 
115 57 zero_gravi
      -- mtime access buffer --
116 64 zero_gravi
--    wdata_buf   <= data_i; -- not required, CPU wdata (=data_i) is stable until transfer is acknowledged
117 57 zero_gravi
      mtime_lo_we <= wren and bool_to_ulogic_f(boolean(addr = mtime_time_lo_addr_c));
118
      mtime_hi_we <= wren and bool_to_ulogic_f(boolean(addr = mtime_time_hi_addr_c));
119 25 zero_gravi
 
120 11 zero_gravi
      -- mtime low --
121 57 zero_gravi
      if (mtime_lo_we = '1') then -- write access
122 61 zero_gravi
        mtime_lo <= data_i;
123 11 zero_gravi
      else -- auto increment
124 61 zero_gravi
        mtime_lo <= mtime_lo_nxt(31 downto 0);
125 2 zero_gravi
      end if;
126 61 zero_gravi
      mtime_lo_ovfl(0) <= mtime_lo_nxt(32); -- overflow (carry)
127 2 zero_gravi
 
128 11 zero_gravi
      -- mtime high --
129 57 zero_gravi
      if (mtime_hi_we = '1') then -- write access
130 61 zero_gravi
        mtime_hi <= data_i;
131 57 zero_gravi
      else -- auto increment (if mtime.low overflows)
132 61 zero_gravi
        mtime_hi <= std_ulogic_vector(unsigned(mtime_hi) + unsigned(mtime_lo_ovfl));
133 2 zero_gravi
      end if;
134
    end if;
135
  end process wr_access;
136
 
137 61 zero_gravi
  -- mtime.time_LO increment --
138
  mtime_lo_nxt <= std_ulogic_vector(unsigned('0' & mtime_lo) + 1);
139 2 zero_gravi
 
140 57 zero_gravi
 
141 2 zero_gravi
  -- Read Access ----------------------------------------------------------------------------
142
  -- -------------------------------------------------------------------------------------------
143
  rd_access: process(clk_i)
144
  begin
145
    if rising_edge(clk_i) then
146 11 zero_gravi
      ack_o  <= acc_en and (rden_i or wren_i);
147 2 zero_gravi
      data_o <= (others => '0'); -- default
148
      if (rden_i = '1') and (acc_en = '1') then
149 25 zero_gravi
        case addr is
150
          when mtime_time_lo_addr_c => -- mtime LOW
151 61 zero_gravi
            data_o <= mtime_lo;
152 25 zero_gravi
          when mtime_time_hi_addr_c => -- mtime HIGH
153
            data_o <= mtime_hi;
154
          when mtime_cmp_lo_addr_c => -- mtimecmp LOW
155
            data_o <= mtimecmp_lo;
156 56 zero_gravi
          when others => -- mtime_cmp_hi_addr_c -- mtimecmp HIGH
157 25 zero_gravi
            data_o <= mtimecmp_hi;
158
        end case;
159 2 zero_gravi
      end if;
160
    end if;
161
  end process rd_access;
162
 
163 26 zero_gravi
  -- system time output for cpu --
164 64 zero_gravi
  time_o <= mtime_hi & mtime_lo; -- NOTE: low and high words are not synchronized here!
165 2 zero_gravi
 
166 11 zero_gravi
 
167 2 zero_gravi
  -- Comparator -----------------------------------------------------------------------------
168
  -- -------------------------------------------------------------------------------------------
169
  cmp_sync: process(clk_i)
170
  begin
171
    if rising_edge(clk_i) then
172 65 zero_gravi
      cmp_lo_ge_ff <= cmp_lo_ge; -- there is one cycle delay between low (earlier) and high (later) word
173 64 zero_gravi
      irq_o        <= cmp_hi_gt or (cmp_hi_eq and cmp_lo_ge_ff);
174 2 zero_gravi
    end if;
175
  end process cmp_sync;
176
 
177 64 zero_gravi
  -- sub-word comparators --
178
  cmp_lo_ge <= '1' when (unsigned(mtime_lo) >= unsigned(mtimecmp_lo)) else '0'; -- low-word: greater than or equal
179
  cmp_hi_eq <= '1' when (unsigned(mtime_hi) =  unsigned(mtimecmp_hi)) else '0'; -- high-word: equal
180
  cmp_hi_gt <= '1' when (unsigned(mtime_hi) >  unsigned(mtimecmp_hi)) else '0'; -- high-word: greater than
181 2 zero_gravi
 
182
 
183
end neorv32_mtime_rtl;

powered by: WebSVN 2.1.0

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