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

Subversion Repositories neorv32

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

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 6 zero_gravi
        mtime_lo_msb_ff <= mtime_lo(mtime_lo'left);
112 4 zero_gravi
        -- mtime high --
113
        if ((mtime_lo_msb_ff xor mtime_lo(mtime_lo'left)) = '1') then -- mtime_lo carry?
114
          mtime_hi <= std_ulogic_vector(unsigned(mtime_hi) + 1);
115
        end if;
116
      end if;
117
    end if;
118
  end process system_time;
119
 
120
 
121 2 zero_gravi
  -- Write Access ---------------------------------------------------------------------------
122
  -- -------------------------------------------------------------------------------------------
123
  wr_access: process(clk_i)
124
  begin
125
    if rising_edge(clk_i) then
126
      ack_o <= acc_en and (rden_i or wren_i);
127
      -- mtimecmp low --
128
      if (wren = '1') and (addr = mtime_cmp_lo_addr_c) then
129
        for i in 0 to 3 loop
130
          if (ben_i(i) = '1') then
131
            mtimecmp(00+7+i*8 downto 00+0+i*8) <= data_i(7+i*8 downto 0+i*8);
132
          end if;
133
        end loop; -- byte enable
134
      end if;
135
 
136
      -- mtimecmp high --
137
      if (wren = '1') and (addr = mtime_cmp_hi_addr_c) then
138
        for i in 0 to 3 loop
139
          if (ben_i(i) = '1') then
140
            mtimecmp(32+7+i*8 downto 32+0+i*8) <= data_i(7+i*8 downto 0+i*8);
141
          end if;
142
        end loop; -- byte enable
143
      end if;
144
    end if;
145
  end process wr_access;
146
 
147
 
148
  -- Read Access ----------------------------------------------------------------------------
149
  -- -------------------------------------------------------------------------------------------
150
  rd_access: process(clk_i)
151
  begin
152
    if rising_edge(clk_i) then
153
      data_o <= (others => '0'); -- default
154
      if (rden_i = '1') and (acc_en = '1') then
155
        if (addr = mtime_time_lo_addr_c) then -- mtime LOW
156
          data_o <= mtime_lo(31 downto 00);
157
        elsif (addr = mtime_time_hi_addr_c) then -- mtime HIGH
158
          data_o <= mtime_hi;
159
        elsif (addr = mtime_cmp_lo_addr_c) then -- mtimecmp LOW
160
          data_o <= mtimecmp(31 downto 00);
161
        else -- (addr = mtime_cmp_hi_addr_c) then -- mtimecmp HIGH
162
          data_o <= mtimecmp(63 downto 32);
163
        end if;
164
      end if;
165
    end if;
166
  end process rd_access;
167
 
168
 
169
  -- Comparator -----------------------------------------------------------------------------
170
  -- -------------------------------------------------------------------------------------------
171
  cmp_sync: process(clk_i)
172
  begin
173
    if rising_edge(clk_i) then
174
      cmp_lo_ff    <= cmp_lo;
175
      cmp_match_ff <= cmp_lo_ff and cmp_hi;
176
    end if;
177
  end process cmp_sync;
178
 
179
  -- test words --
180
  cmp_lo <= '1' when (unsigned(mtime_lo(31 downto 00)) >= unsigned(mtimecmp(31 downto 00))) else '0';
181
  cmp_hi <= '1' when (unsigned(mtime_hi(31 downto 00)) >= unsigned(mtimecmp(63 downto 32))) else '0';
182
 
183
 
184
  -- Interrupt Logic ------------------------------------------------------------------------
185
  -- -------------------------------------------------------------------------------------------
186
  irq_ctrl: process(clk_i)
187
  begin
188
    if rising_edge(clk_i) then
189 4 zero_gravi
      if (rstn_i = '0') then
190
        irq_flag_ff <= '0';
191
        irq_flag    <= '0';
192
      else
193
        irq_flag_ff  <= irq_flag;
194
        if (irq_flag = '0') then -- idle
195
          irq_flag <= '0';
196
          if (cmp_match_ff = '1') then
197
            irq_flag <= '1';
198
          end if;
199
        elsif (wren = '1') and (addr = mtime_cmp_hi_addr_c) then -- ACK
200
          irq_flag <= '0';
201 2 zero_gravi
        end if;
202
      end if;
203
    end if;
204
  end process irq_ctrl;
205
 
206
  -- irq output to CPU --
207
  irq_o <= irq_flag and (not irq_flag_ff); -- rising edge detector
208
 
209
 
210
end neorv32_mtime_rtl;

powered by: WebSVN 2.1.0

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