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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_wishbone.vhd] - Diff between revs 31 and 35

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 31 Rev 35
Line 1... Line 1...
-- #################################################################################################
-- #################################################################################################
-- # << NEORV32 - External Bus Interface (WISHBONE) >>                                             #
-- # << NEORV32 - External Bus Interface (WISHBONE) >>                                             #
-- # ********************************************************************************************* #
-- # ********************************************************************************************* #
-- # The interface is either unregistered (INTERFACE_REG_STAGES = 0), only outgoing signals are    #
-- # The interface provides registers for all outgoing signals. If the host cancels a running      #
-- # registered (INTERFACE_REG_STAGES = 1) or incoming and outgoing signals are registered         #
-- # transfer, the Wishbone arbiter still waits some time for the bus system to ACK to transfer.   #
-- # (INTERFACE_REG_STAGES = 2). This interface supports classic/standard Wishbone transactions    #
 
-- # (WB_PIPELINED_MODE = false) and also pipelined transactions for improved timing               #
 
-- # (WB_PIPELINED_MODE = true).                                                                   #
 
-- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
-- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
-- # All bus accesses from the CPU, which do not target the internal IO region, the internal boot- #
-- # All bus accesses from the CPU, which do not target the internal IO region, the internal boot- #
-- # loader or the internal instruction or data memories (if implemented), are delegated via this  #
-- # loader or the internal instruction or data memories (if implemented), are delegated via this  #
-- # Wishbone gateway to the external bus interface.                                               #
-- # Wishbone gateway to the external bus interface.                                               #
 
-- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
 
-- # This interface supports classic/standard Wishbone transactions (WB_PIPELINED_MODE = false)    #
 
-- # and also pipelined transactions (WB_PIPELINED_MODE = true).                                   #
-- # ********************************************************************************************* #
-- # ********************************************************************************************* #
-- # BSD 3-Clause License                                                                          #
-- # BSD 3-Clause License                                                                          #
-- #                                                                                               #
-- #                                                                                               #
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
-- #                                                                                               #
-- #                                                                                               #
Line 49... Line 49...
library neorv32;
library neorv32;
use neorv32.neorv32_package.all;
use neorv32.neorv32_package.all;
 
 
entity neorv32_wishbone is
entity neorv32_wishbone is
  generic (
  generic (
    INTERFACE_REG_STAGES : natural := 2; -- number of interface register stages (0,1,2)
 
    WB_PIPELINED_MODE    : boolean := false; -- false: classic/standard wishbone mode, true: pipelined wishbone mode
    WB_PIPELINED_MODE    : boolean := false; -- false: classic/standard wishbone mode, true: pipelined wishbone mode
    -- Internal instruction memory --
    -- Internal instruction memory --
    MEM_INT_IMEM_USE     : boolean := true;   -- implement processor-internal instruction memory
    MEM_INT_IMEM_USE     : boolean := true;   -- implement processor-internal instruction memory
    MEM_INT_IMEM_SIZE    : natural := 8*1024; -- size of processor-internal instruction memory in bytes
    MEM_INT_IMEM_SIZE    : natural := 8*1024; -- size of processor-internal instruction memory in bytes
    -- Internal data memory --
    -- Internal data memory --
Line 87... Line 86...
  );
  );
end neorv32_wishbone;
end neorv32_wishbone;
 
 
architecture neorv32_wishbone_rtl of neorv32_wishbone is
architecture neorv32_wishbone_rtl of neorv32_wishbone is
 
 
 
  -- constants --
 
  constant wb_timeout_c : natural := bus_timeout_c/2;
 
 
  -- access control --
  -- access control --
  signal int_imem_acc, int_imem_acc_real : std_ulogic;
  signal int_imem_acc, int_imem_acc_real : std_ulogic;
  signal int_dmem_acc, int_dmem_acc_real : std_ulogic;
  signal int_dmem_acc, int_dmem_acc_real : std_ulogic;
  signal int_boot_acc                    : std_ulogic;
  signal int_boot_acc                    : std_ulogic;
  signal wb_access                       : std_ulogic;
  signal wb_access                       : std_ulogic;
  signal wb_access_ff, wb_access_ff_ff   : std_ulogic;
 
  signal rb_en                           : std_ulogic;
 
 
 
  -- bus arbiter --
  -- bus arbiter
  signal wb_we_ff   : std_ulogic;
  type ctrl_state_t is (IDLE, BUSY, CANCELED);
  signal wb_stb_ff0 : std_ulogic;
  type ctrl_t is record
  signal wb_stb_ff1 : std_ulogic;
    state      : ctrl_state_t;
  signal wb_cyc_ff  : std_ulogic;
    state_prev : ctrl_state_t;
  signal wb_ack_ff  : std_ulogic;
    we         : std_ulogic;
  signal wb_err_ff  : std_ulogic;
    rd_req     : std_ulogic;
 
    wr_req     : std_ulogic;
  -- wishbone mode: standard / pipelined --
    adr        : std_ulogic_vector(31 downto 0);
  signal stb_int_std  : std_ulogic;
    wdat       : std_ulogic_vector(31 downto 0);
  signal stb_int_pipe : std_ulogic;
    rdat       : std_ulogic_vector(31 downto 0);
 
    sel        : std_ulogic_vector(3 downto 0);
 
    ack        : std_ulogic;
 
    err        : std_ulogic;
 
    timeout    : std_ulogic_vector(index_size_f(wb_timeout_c)-1 downto 0);
 
  end record;
 
  signal ctrl : ctrl_t;
 
 
  -- data read-back --
  signal stb_int, cyc_int : std_ulogic;
  signal wb_rdata : std_ulogic_vector(31 downto 0);
 
 
 
begin
begin
 
 
  -- Sanity Check ---------------------------------------------------------------------------
  -- Sanity Checks --------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  assert (INTERFACE_REG_STAGES <= 2) report "NEORV32 CONFIG ERROR! Number of external memory interface buffer stages must be 0, 1 or 2." severity error;
  assert not (bus_timeout_c <= 15) report "NEORV32 PROCESSOR CONFIG ERROR: Bus timeout (bus_timeout_c) should be >16 for interfacing external modules." severity error;
  assert (INTERFACE_REG_STAGES /= 0) report "NEORV32 CONFIG WARNING! External memory interface without register stages is still experimental for peripherals with more than 1 cycle latency." severity warning;
 
 
 
 
 
  -- Access Control -------------------------------------------------------------------------
  -- Access Control -------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- access to internal IMEM or DMEM? --
  -- access to internal IMEM or DMEM? --
Line 132... Line 136...
  int_boot_acc <= '1' when (addr_i >= boot_rom_base_c) else '0'; -- this also covers access to the IO space
  int_boot_acc <= '1' when (addr_i >= boot_rom_base_c) else '0'; -- this also covers access to the IO space
--int_boot_acc <= '1' when (addr_i(31 downto index_size_f(2*boot_rom_max_size_c)) = boot_rom_base_c(31 downto index_size_f(2*boot_rom_max_size_c))) else '0'; -- this also covers access to the IO space
--int_boot_acc <= '1' when (addr_i(31 downto index_size_f(2*boot_rom_max_size_c)) = boot_rom_base_c(31 downto index_size_f(2*boot_rom_max_size_c))) else '0'; -- this also covers access to the IO space
--int_io_acc   <= '1' when (addr_i >= io_base_c) else '0';
--int_io_acc   <= '1' when (addr_i >= io_base_c) else '0';
 
 
  -- actual external bus access? --
  -- actual external bus access? --
  wb_access <= (not int_imem_acc_real) and (not int_dmem_acc_real) and (not int_boot_acc) and (wren_i or rden_i);
  wb_access <= (not int_imem_acc_real) and (not int_dmem_acc_real) and (not int_boot_acc);
 
 
 
 
  -- Bus Arbiter -----------------------------------------------------------------------------
  -- Bus Arbiter -----------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  bus_arbiter: process(rstn_i, clk_i)
  bus_arbiter: process(rstn_i, clk_i)
  begin
  begin
    if (rstn_i = '0') then
    if (rstn_i = '0') then
      wb_we_ff        <= '0';
      ctrl.state      <= IDLE;
      wb_cyc_ff       <= '0';
      ctrl.state_prev <= IDLE;
      wb_stb_ff1      <= '0';
      ctrl.we         <= '0';
      wb_stb_ff0      <= '0';
      ctrl.rd_req     <= '0';
      wb_ack_ff       <= '0';
      ctrl.wr_req     <= '0';
      wb_err_ff       <= '0';
      ctrl.adr        <= (others => '0');
      wb_access_ff    <= '0';
      ctrl.wdat       <= (others => '0');
      wb_access_ff_ff <= '0';
      ctrl.rdat       <= (others => '0');
 
      ctrl.sel        <= (others => '0');
 
      ctrl.timeout    <= (others => '0');
 
      ctrl.ack        <= '0';
 
      ctrl.err        <= '0';
    elsif rising_edge(clk_i) then
    elsif rising_edge(clk_i) then
      -- read/write --
      -- defaults --
      wb_we_ff <= (wb_we_ff or wren_i) and wb_access and (not wb_ack_i) and (not wb_err_i) and (not cancel_i);
      ctrl.state_prev <= ctrl.state;
      -- bus cycle --
      ctrl.rdat       <= (others => '0');
      if (INTERFACE_REG_STAGES = 0) then
      ctrl.ack        <= '0';
        wb_cyc_ff <= '0'; -- unused
      ctrl.err        <= '0';
      else
      ctrl.timeout    <= std_ulogic_vector(to_unsigned(wb_timeout_c, index_size_f(wb_timeout_c)));
        wb_cyc_ff <= (wb_cyc_ff or wb_access) and (not wb_ack_i) and (not wb_err_i) and (not cancel_i);
 
      end if;
      -- state machine --
      -- bus strobe --
      case ctrl.state is
      wb_stb_ff1 <= wb_stb_ff0;
 
      wb_stb_ff0 <= wb_access;
        when IDLE => -- waiting for host request
      -- bus ack --
        -- ------------------------------------------------------------
      wb_ack_ff <= wb_ack_i;
          ctrl.rd_req <= '0';
      -- bus err --
          ctrl.wr_req <= '0';
      wb_err_ff <= wb_err_i;
          -- buffer all outgoing signals --
      -- access still active? --
          ctrl.we   <= wren_i;
      wb_access_ff_ff <= wb_access_ff;
          ctrl.adr  <= addr_i;
      if (wb_access = '1') then
          ctrl.wdat <= data_i;
        wb_access_ff <= '1';
          ctrl.sel  <= ben_i;
      elsif ((wb_ack_i or wb_err_i or cancel_i) = '1') then
          -- valid read/write access --
        wb_access_ff <= '0';
          if ((wb_access and (wren_i or ctrl.wr_req or rden_i or ctrl.rd_req)) = '1') then
      end if;
            ctrl.state <= BUSY;
 
          end if;
 
 
 
        when BUSY => -- transfer in progress
 
        -- ------------------------------------------------------------
 
          ctrl.rdat <= wb_dat_i;
 
          if (cancel_i = '1') then -- transfer canceled by host
 
            ctrl.state <= CANCELED;
 
          elsif (wb_err_i = '1') then -- abnormal bus termination
 
            ctrl.err   <= '1';
 
            ctrl.state <= CANCELED;
 
          elsif (wb_ack_i = '1') then -- normal bus termination
 
            ctrl.ack   <= '1';
 
            ctrl.state <= IDLE;
 
          end if;
 
 
 
        when CANCELED => -- 
 
        -- ------------------------------------------------------------
 
          ctrl.wr_req <= ctrl.wr_req or wren_i; -- buffer new request
 
          ctrl.rd_req <= ctrl.rd_req or rden_i; -- buffer new request
 
          -- wait for bus.peripheral to ACK transfer (as "aborted" but still somehow "completed")
 
          -- or wait for a timeout and force termination
 
          ctrl.timeout <= std_ulogic_vector(unsigned(ctrl.timeout) - 1); -- timeout counter
 
          if (wb_ack_i = '1') or (or_all_f(ctrl.timeout) = '0') then
 
            ctrl.state <= IDLE;
 
          end if;
 
 
 
        when others => -- undefined
 
        -- ------------------------------------------------------------
 
          ctrl.state <= IDLE;
 
 
 
      end case;
    end if;
    end if;
  end process bus_arbiter;
  end process bus_arbiter;
 
 
  -- valid bus cycle --
 
  wb_cyc_o <= wb_access when (INTERFACE_REG_STAGES = 0) else wb_cyc_ff;
 
 
 
  -- bus strobe --
 
  stb_int_std  <= wb_access when (INTERFACE_REG_STAGES = 0) else wb_cyc_ff; -- same as wb_cyc
 
  stb_int_pipe <= (wb_access and (not wb_stb_ff0)) when (INTERFACE_REG_STAGES = 0) else (wb_stb_ff0 and (not wb_stb_ff1)); -- wb_access rising edge detector
 
  --
 
  wb_stb_o <= stb_int_std when (WB_PIPELINED_MODE = false) else stb_int_pipe; -- standard or pipelined mode
 
 
 
  -- cpu ack --
 
  ack_o <= wb_ack_ff when (INTERFACE_REG_STAGES = 2) else wb_ack_i;
 
 
 
  -- cpu err --
  -- host access --
  err_o <= wb_err_ff when (INTERFACE_REG_STAGES = 2) else wb_err_i;
  data_o   <= ctrl.rdat;
 
  ack_o    <= ctrl.ack;
  -- cpu read-data --
  err_o    <= ctrl.err;
  rb_en  <= wb_access_ff_ff when (INTERFACE_REG_STAGES = 2) else wb_access_ff;
 
  data_o <= wb_rdata when (rb_en = '1') else (others => '0');
 
 
 
 
  -- wishbone interface --
 
  wb_adr_o <= ctrl.adr;
 
  wb_dat_o <= ctrl.wdat;
 
  wb_we_o  <= ctrl.we;
 
  wb_sel_o <= ctrl.sel;
 
  wb_stb_o <= stb_int when (WB_PIPELINED_MODE = true) else cyc_int;
 
  wb_cyc_o <= cyc_int;
 
 
  -- Bus Buffer -----------------------------------------------------------------------------
  stb_int  <= '1' when ((ctrl.state = BUSY) and (ctrl.state_prev = IDLE)) else '0';
  -- -------------------------------------------------------------------------------------------
  cyc_int  <= '0' when (ctrl.state = IDLE) else '1';
  interface_reg_level_zero:
 
  if (INTERFACE_REG_STAGES = 0) generate -- 0 register levels: direct connection
 
    wb_rdata <= wb_dat_i;
 
    wb_adr_o <= addr_i;
 
    wb_dat_o <= data_i;
 
    wb_sel_o <= ben_i;
 
    wb_we_o  <= wren_i or wb_we_ff;
 
  end generate;
 
 
 
  interface_reg_level_one:
 
  if (INTERFACE_REG_STAGES = 1) generate -- 1 register levels: buffer outgoing signals
 
    buffer_stages_one: process(clk_i)
 
    begin
 
      if rising_edge(clk_i) then
 
        if (wb_cyc_ff = '0') then
 
          wb_adr_o <= addr_i;
 
          wb_dat_o <= data_i;
 
          wb_sel_o <= ben_i;
 
          wb_we_o  <= wren_i or wb_we_ff;
 
        end if;
 
      end if;
 
    end process buffer_stages_one;
 
    wb_rdata <= wb_dat_i;
 
  end generate;
 
 
 
  interface_reg_level_two:
 
  if (INTERFACE_REG_STAGES = 2) generate -- 2 register levels: buffer incoming and outgoing signals
 
    buffer_stages_two: process(clk_i)
 
    begin
 
      if rising_edge(clk_i) then
 
        if (wb_cyc_ff = '0') then
 
          wb_adr_o <= addr_i;
 
          wb_dat_o <= data_i;
 
          wb_sel_o <= ben_i;
 
          wb_we_o  <= wren_i or wb_we_ff;
 
        end if;
 
        if (wb_ack_i = '1') then
 
          wb_rdata <= wb_dat_i;
 
        end if;
 
      end if;
 
    end process buffer_stages_two;
 
  end generate;
 
 
 
 
 
end neorv32_wishbone_rtl;
end neorv32_wishbone_rtl;
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

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