Line 48... |
Line 48... |
|
|
entity neorv32_mtime is
|
entity neorv32_mtime is
|
port (
|
port (
|
-- host access --
|
-- host access --
|
clk_i : in std_ulogic; -- global clock line
|
clk_i : in std_ulogic; -- global clock line
|
|
rstn_i : in std_ulogic := '0'; -- global reset, low-active, async
|
addr_i : in std_ulogic_vector(31 downto 0); -- address
|
addr_i : in std_ulogic_vector(31 downto 0); -- address
|
rden_i : in std_ulogic; -- read enable
|
rden_i : in std_ulogic; -- read enable
|
wren_i : in std_ulogic; -- write enable
|
wren_i : in std_ulogic; -- write enable
|
ben_i : in std_ulogic_vector(03 downto 0); -- byte write enable
|
ben_i : in std_ulogic_vector(03 downto 0); -- byte write enable
|
data_i : in std_ulogic_vector(31 downto 0); -- data in
|
data_i : in std_ulogic_vector(31 downto 0); -- data in
|
Line 72... |
Line 73... |
signal acc_en : std_ulogic; -- module access enable
|
signal acc_en : std_ulogic; -- module access enable
|
signal addr : std_ulogic_vector(31 downto 0); -- access address
|
signal addr : std_ulogic_vector(31 downto 0); -- access address
|
signal wren : std_ulogic; -- module access enable
|
signal wren : std_ulogic; -- module access enable
|
|
|
-- accessible regs --
|
-- accessible regs --
|
signal mtime_we : std_ulogic;
|
|
signal mtimecmp : std_ulogic_vector(63 downto 0);
|
signal mtimecmp : std_ulogic_vector(63 downto 0);
|
signal mtime_lo : std_ulogic_vector(32 downto 0);
|
signal mtime_lo : std_ulogic_vector(32 downto 0);
|
signal mtime_lo_msb_ff : std_ulogic;
|
signal mtime_lo_msb_ff : std_ulogic;
|
signal mtime_hi : std_ulogic_vector(31 downto 0);
|
signal mtime_hi : std_ulogic_vector(31 downto 0);
|
|
|
Line 95... |
Line 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';
|
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';
|
addr <= mtime_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
|
addr <= mtime_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
|
wren <= acc_en and wren_i;
|
wren <= acc_en and wren_i;
|
|
|
|
|
|
-- System Time Update ---------------------------------------------------------------------
|
|
-- -------------------------------------------------------------------------------------------
|
|
system_time: process(clk_i)
|
|
begin
|
|
if rising_edge(clk_i) then
|
|
if (rstn_i = '0') then
|
|
mtime_lo <= (others => '0');
|
|
mtime_hi <= (others => '0');
|
|
else
|
|
-- mtime low --
|
|
mtime_lo <= std_ulogic_vector(unsigned(mtime_lo) + 1);
|
|
-- mtime high --
|
|
if ((mtime_lo_msb_ff xor mtime_lo(mtime_lo'left)) = '1') then -- mtime_lo carry?
|
|
mtime_hi <= std_ulogic_vector(unsigned(mtime_hi) + 1);
|
|
end if;
|
|
end if;
|
|
end if;
|
|
end process system_time;
|
|
|
|
|
-- Write Access ---------------------------------------------------------------------------
|
-- Write Access ---------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
wr_access: process(clk_i)
|
wr_access: process(clk_i)
|
begin
|
begin
|
if rising_edge(clk_i) then
|
if rising_edge(clk_i) then
|
Line 118... |
Line 138... |
if (ben_i(i) = '1') then
|
if (ben_i(i) = '1') then
|
mtimecmp(32+7+i*8 downto 32+0+i*8) <= data_i(7+i*8 downto 0+i*8);
|
mtimecmp(32+7+i*8 downto 32+0+i*8) <= data_i(7+i*8 downto 0+i*8);
|
end if;
|
end if;
|
end loop; -- byte enable
|
end loop; -- byte enable
|
end if;
|
end if;
|
|
|
-- any access to mtime at all? --
|
|
mtime_we <= '0';
|
|
if (wren = '1') and ((addr = mtime_time_lo_addr_c) or (addr = mtime_time_hi_addr_c)) then
|
|
mtime_we <= '1';
|
|
end if;
|
|
|
|
-- mtime low --
|
|
mtime_lo_msb_ff <= mtime_lo(mtime_lo'left);
|
|
if (wren = '1') and (addr = mtime_time_lo_addr_c) then
|
|
mtime_lo(mtime_lo'left) <= '0'; -- clear overflow bit on every access
|
|
for i in 0 to 3 loop
|
|
if (ben_i(i) = '1') then
|
|
mtime_lo(00+7+i*8 downto 00+0+i*8) <= data_i(7+i*8 downto 0+i*8);
|
|
end if;
|
|
end loop; -- byte enable
|
|
else -- incrment
|
|
mtime_lo <= std_ulogic_vector(unsigned(mtime_lo) + 1);
|
|
end if;
|
|
|
|
-- mtime high --
|
|
if (wren = '1') and (addr = mtime_time_hi_addr_c) then
|
|
for i in 0 to 3 loop
|
|
if (ben_i(i) = '1') then
|
|
mtime_hi(00+7+i*8 downto 00+0+i*8) <= data_i(7+i*8 downto 0+i*8);
|
|
end if;
|
|
end loop; -- byte enable
|
|
elsif ((mtime_lo_msb_ff xor mtime_lo(mtime_lo'left)) = '1') then -- mtime_lo carry?
|
|
mtime_hi <= std_ulogic_vector(unsigned(mtime_hi) + 1);
|
|
end if;
|
|
end if;
|
end if;
|
end process wr_access;
|
end process wr_access;
|
|
|
|
|
-- Read Access ----------------------------------------------------------------------------
|
-- Read Access ----------------------------------------------------------------------------
|
Line 193... |
Line 183... |
-- Interrupt Logic ------------------------------------------------------------------------
|
-- Interrupt Logic ------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
irq_ctrl: process(clk_i)
|
irq_ctrl: process(clk_i)
|
begin
|
begin
|
if rising_edge(clk_i) then
|
if rising_edge(clk_i) then
|
|
if (rstn_i = '0') then
|
|
irq_flag_ff <= '0';
|
|
irq_flag <= '0';
|
|
else
|
irq_flag_ff <= irq_flag;
|
irq_flag_ff <= irq_flag;
|
if (irq_flag = '0') or (mtime_we = '1') then -- idle or mtime manual write
|
if (irq_flag = '0') then -- idle
|
irq_flag <= '0';
|
irq_flag <= '0';
|
if (cmp_match_ff = '1') then
|
if (cmp_match_ff = '1') then
|
irq_flag <= '1';
|
irq_flag <= '1';
|
end if;
|
end if;
|
elsif (wren = '1') and (addr = mtime_cmp_hi_addr_c) then -- ACK
|
elsif (wren = '1') and (addr = mtime_cmp_hi_addr_c) then -- ACK
|
irq_flag <= '0';
|
irq_flag <= '0';
|
end if;
|
end if;
|
end if;
|
end if;
|
|
end if;
|
end process irq_ctrl;
|
end process irq_ctrl;
|
|
|
-- irq output to CPU --
|
-- irq output to CPU --
|
irq_o <= irq_flag and (not irq_flag_ff); -- rising edge detector
|
irq_o <= irq_flag and (not irq_flag_ff); -- rising edge detector
|
|
|