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

Subversion Repositories yifive

[/] [yifive/] [trunk/] [caravel_yifive/] [verilog/] [rtl/] [spi_master/] [src/] [spim_rx.sv] - Diff between revs 18 and 21

Only display areas with differences | Details | Blame | View Log

Rev 18 Rev 21
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
////                                                              ////
////                                                              ////
////  SPI RX  Module                                              ////
////  SPI RX  Module                                              ////
////                                                              ////
////                                                              ////
////  This file is part of the YIFive cores project               ////
////  This file is part of the YIFive cores project               ////
////  http://www.opencores.org/cores/yifive/                      ////
////  http://www.opencores.org/cores/yifive/                      ////
////                                                              ////
////                                                              ////
////  Description                                                 ////
////  Description                                                 ////
////     SPI RX module                                            ////
////     SPI RX module                                            ////
////                                                              ////
////                                                              ////
////  To Do:                                                      ////
////  To Do:                                                      ////
////    nothing                                                   ////
////    nothing                                                   ////
////                                                              ////
////                                                              ////
////  Author(s):                                                  ////
////  Author(s):                                                  ////
////      - Dinesh Annayya, dinesha@opencores.org                 ////
////      - Dinesh Annayya, dinesha@opencores.org                 ////
////                                                              ////
////                                                              ////
////  Revision :                                                  ////
////  Revision :                                                  ////
////     V.0  -  June 8, 2021                                     ////
////     V.0  -  June 8, 2021                                     ////
////                                                              ////
////                                                              ////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
////                                                              ////
////                                                              ////
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
////                                                              ////
////                                                              ////
//// This source file may be used and distributed without         ////
//// This source file may be used and distributed without         ////
//// restriction provided that this copyright statement is not    ////
//// restriction provided that this copyright statement is not    ////
//// removed from the file and that any derivative work contains  ////
//// removed from the file and that any derivative work contains  ////
//// the original copyright notice and the associated disclaimer. ////
//// the original copyright notice and the associated disclaimer. ////
////                                                              ////
////                                                              ////
//// This source file is free software; you can redistribute it   ////
//// This source file is free software; you can redistribute it   ////
//// and/or modify it under the terms of the GNU Lesser General   ////
//// and/or modify it under the terms of the GNU Lesser General   ////
//// Public License as published by the Free Software Foundation; ////
//// Public License as published by the Free Software Foundation; ////
//// either version 2.1 of the License, or (at your option) any   ////
//// either version 2.1 of the License, or (at your option) any   ////
//// later version.                                               ////
//// later version.                                               ////
////                                                              ////
////                                                              ////
//// This source is distributed in the hope that it will be       ////
//// This source is distributed in the hope that it will be       ////
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
//// PURPOSE.  See the GNU Lesser General Public License for more ////
//// PURPOSE.  See the GNU Lesser General Public License for more ////
//// details.                                                     ////
//// details.                                                     ////
////                                                              ////
////                                                              ////
//// You should have received a copy of the GNU Lesser General    ////
//// You should have received a copy of the GNU Lesser General    ////
//// Public License along with this source; if not, download it   ////
//// Public License along with this source; if not, download it   ////
//// from http://www.opencores.org/lgpl.shtml                     ////
//// from http://www.opencores.org/lgpl.shtml                     ////
////                                                              ////
////                                                              ////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
module spim_rx #(
module spim_rx #(
                parameter ENDIEAN = 0  // 0 - Little, 1 - Big endian, since RISV is Little indian default set 0
                parameter ENDIEAN = 0  // 0 - Little, 1 - Big endian, since RISV is Little indian default set 0
        )
        )
(
(
    input  logic        clk,
    input  logic        clk,
    input  logic        rstn,
    input  logic        rstn,
    input  logic        en,
    input  logic        en,
    input  logic        rx_edge,
    input  logic        rx_edge,
    output logic        rx_done,
    output logic        rx_done,
    input  logic        sdi0,
    input  logic        sdi0,
    input  logic        sdi1,
    input  logic        sdi1,
    input  logic        sdi2,
    input  logic        sdi2,
    input  logic        sdi3,
    input  logic        sdi3,
    input  logic        en_quad_in,
    input  logic        en_quad_in,
    input  logic [15:0] counter_in,
    input  logic [15:0] counter_in,
    input  logic        counter_in_upd,
    input  logic        counter_in_upd,
    output logic [31:0] data,
    output logic [31:0] data,
    input  logic        data_ready,
    input  logic        data_ready,
    output logic        data_valid,
    output logic        data_valid,
    output logic        clk_en_o
    output logic        clk_en_o
);
);
  logic [31:0] data_int;
  logic [31:0] data_int;
  logic [31:0] data_int_next;
  logic [31:0] data_int_next;
  logic [15:0] counter;
  logic [15:0] counter;
  logic [15:0] counter_trgt;
  logic [15:0] counter_trgt;
  logic [15:0] counter_next;
  logic [15:0] counter_next;
  logic [15:0] counter_trgt_next;
 
  logic        reg_done;
  logic        reg_done;
  enum logic [1:0] { IDLE, RECEIVE, WAIT_FIFO, WAIT_FIFO_DONE } rx_CS, rx_NS;
  enum logic [1:0] { IDLE, RECEIVE, WAIT_FIFO, WAIT_FIFO_DONE } rx_CS, rx_NS;
  assign reg_done  = (!en_quad_in && (counter[4:0] == 5'b11111)) || (en_quad_in && (counter[2:0] == 3'b111));
  assign reg_done  = (!en_quad_in && (counter[4:0] == 5'b11111)) || (en_quad_in && (counter[2:0] == 3'b111));
  // RISV is little endian, so data is converted to little endian format
  // RISV is little endian, so data is converted to little endian format
  assign data = (ENDIEAN) ? data_int_next : {data_int_next[7:0],data_int_next[15:8],data_int_next[23:16],data_int_next[31:24]};
  assign data = (ENDIEAN) ? data_int_next : {data_int_next[7:0],data_int_next[15:8],data_int_next[23:16],data_int_next[31:24]};
  assign rx_done = (counter == (counter_trgt-1)) &  rx_edge;
 
 
 
  always_comb
 
  begin
 
    if (counter_in_upd)
 
      counter_trgt_next = (en_quad_in) ? {2'b00,counter_in[15:2]} : counter_in;
 
    else
 
      counter_trgt_next = counter_trgt;
 
  end
 
 
 
  always_comb
  always_comb
  begin
  begin
    rx_NS         = rx_CS;
    rx_NS         = rx_CS;
    clk_en_o      = 1'b0;
 
    data_int_next = data_int;
    data_int_next = data_int;
    data_valid    = 1'b0;
    data_valid    = 1'b0;
    counter_next  = counter;
    counter_next  = counter;
    case (rx_CS)
    case (rx_CS)
      IDLE: begin
      IDLE: begin
        clk_en_o = 1'b0;
 
 
 
        // check first if there is available space instead of later
        // check first if there is available space instead of later
        if (en) begin
        if (en) begin
          rx_NS = RECEIVE;
          rx_NS = RECEIVE;
        end
        end
      end
      end
      RECEIVE: begin
      RECEIVE: begin
        clk_en_o = 1'b1;
 
 
 
        if (rx_edge) begin
        if (rx_edge) begin
          counter_next = counter + 1;
          counter_next = counter + 1;
          if (en_quad_in)
          if (en_quad_in)
             data_int_next = {data_int[27:0],sdi3,sdi2,sdi1,sdi0};
             data_int_next = {data_int[27:0],sdi3,sdi2,sdi1,sdi0};
          else
          else
             data_int_next = {data_int[30:0],sdi1};
             data_int_next = {data_int[30:0],sdi1};
          if (rx_done) begin
          if (rx_done) begin
            counter_next = 0;
            counter_next = 0;
            data_valid   = 1'b1;
            data_valid   = 1'b1;
            if (data_ready)
            if (data_ready)
              rx_NS = IDLE;
              rx_NS = IDLE;
            else
            else
              rx_NS = WAIT_FIFO_DONE;
              rx_NS = WAIT_FIFO_DONE;
          end else if (reg_done) begin
          end else if (reg_done) begin
            data_valid = 1'b1;
            data_valid = 1'b1;
            if (~data_ready) begin
            if (~data_ready) begin
              // no space in the FIFO, wait for free space
              // no space in the FIFO, wait for free space
              clk_en_o = 1'b0;
 
              rx_NS    = WAIT_FIFO;
              rx_NS    = WAIT_FIFO;
            end
            end
          end
          end
        end
        end
      end
      end
      WAIT_FIFO_DONE: begin
      WAIT_FIFO_DONE: begin
        data_valid = 1'b1;
        data_valid = 1'b1;
        if (data_ready)
        if (data_ready)
          rx_NS = IDLE;
          rx_NS = IDLE;
      end
      end
      WAIT_FIFO: begin
      WAIT_FIFO: begin
        data_valid = 1'b1;
        data_valid = 1'b1;
        if (data_ready)
        if (data_ready)
          rx_NS = RECEIVE;
          rx_NS = RECEIVE;
      end
      end
    endcase
    endcase
  end
  end
  always_ff @(posedge clk, negedge rstn)
  always_ff @(posedge clk, negedge rstn)
  begin
  begin
    if (rstn == 0)
    if (rstn == 0)
    begin
    begin
      counter      <= 0;
      counter      <= 0;
      counter_trgt <= 'h8;
      counter_trgt <= 'h8;
      data_int     <= '0;
      data_int     <= '0;
 
      rx_done      <= '0;
 
      clk_en_o     <= '0;
      rx_CS        <= IDLE;
      rx_CS        <= IDLE;
    end
    end
    else
    else
    begin
    begin
 
      if (rx_edge) begin
      counter      <= counter_next;
      counter      <= counter_next;
      counter_trgt <= counter_trgt_next;
 
      data_int     <= data_int_next;
      data_int     <= data_int_next;
      rx_CS        <= rx_NS;
      rx_CS        <= rx_NS;
 
         rx_done      <= (counter_next == (counter_trgt-1)) && (rx_NS == RECEIVE);
 
         clk_en_o     <= (rx_NS == RECEIVE);
 
      end
 
       if (en && counter_in_upd) begin
 
          counter_trgt <= (en_quad_in) ? {2'b00,counter_in[15:2]} : counter_in;
 
        end
    end
    end
  end
  end
endmodule
endmodule
 
 

powered by: WebSVN 2.1.0

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