⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smarti.v

📁 实现USB接口功能的VHDL和verilog完整源代码
💻 V
📖 第 1 页 / 共 2 页
字号:
      num_sof  <= 'h0;
    else if (  sm_state == RD_CMD || card_ready_d && !escape_busy  )
      num_sof  <= 'h0;
    else if (  !card_ready_d && sof && !escape_busy ) 
      num_sof  <= num_sof +1;
      //num_sof  <= ( sm_state != WAIT_SM) ? 0 : ((sm_state == WAIT_SM && sof ) ? num_sof +1 : num_sof);
  end

  always @( posedge mcclock ) begin
    if ( mcreset ) begin
      clk_cnt <= 0; 
      command  <= 'h0;
      st_cnt   <= 'h0;
      num_sent  <= 'h0;
      new_page  <= 'h0;
    end
    else begin
      clk_cnt <= (reset_cnt)? 0 :clk_cnt+1; 
      num_sent <= ( sm_state == CMD || sm_state == DUMMY_RD? 0 :
                    ( clk_cnt =='b1 && (sm_state == READ || sm_state == WRITE ) ? 
                     num_sent + 1: num_sent ));  
      command <= command_c;
      st_cnt  <= (sm_state != ADDR && sm_state != WR_STATUS && sm_state != GET_STATUS && sm_state != HIDDEN_CMD ) ? 0 :
                  ( clk_cnt =='b10 ? st_cnt + 1 : st_cnt ) ;
      new_page  <= ( sm_state == RD_CMD || sm_state == ADDR ) ? 0 :
                   ( ( command == DATA_IN &&  num_sent == num_send && clk_cnt == 'b11 ) ?
                      1 : new_page );
    end
  end

  always @( posedge mcclock ) begin
    if ( mcreset )  
      addr     <= 'h0;
    else begin
      if ( sm_state == RD_CMD && 
           ( cmd_num[2] == 1 || cmd_num ==3'h3 ))
           //( cmd_num == 3'b01 || cmd_num ==3'b10 || cmd_num == 3'b11 ))
        addr <= { cmddata, addr[31:8]}; 
      /*else if  ( nxt_sm_state == WAIT_SM && sm_state == DUMMY_WR  )  
        addr <= addr + 'h100; */
      else if  ( nxt_sm_state == WAIT_SM && sm_state == DUMMY_WR  )  begin
        addr[31:8] <= addr[31:8] + 'h1;
        addr[7:0] <= addr[7:0] ;
      end
      else 
        addr <= addr;
    end
  end 
  always @( posedge mcclock ) begin
    if ( mcreset )  begin
      hidden_cmd0 <= 0;
      hidden_cmd1 <= 0;
    end 
    else begin
      hidden_cmd0 <= ( sm_state == RD_CMD && cmd_num =='h0 )? cmddata : hidden_cmd0;
      hidden_cmd1 <= ( sm_state == RD_CMD && cmd_num =='h1 )? cmddata : hidden_cmd1;
    end 
  end 

  assign reset_cmd_num = ( cmd_num == 'b111 )? 1:0; 

  assign  num_addr = ( command == ERASE_SETUP ) ? 
                      ( sm64_en_d ? 'b10:   'b01 ):
                      ( command == READ_ID ? 'b00 : 
                       (sm64_en_d ? 'b11 :  'b10) );

  
  always @( posedge mcclock )begin
    if ( mcreset ) 
      sm_state <= IDLE;
    else if ( sm_enable ) 
      sm_state <= nxt_sm_state;
  end

  always  @( getdata or num_sent or hidden_cmd0 or new_page or mcreset or  
             clk_cnt or sm_state or getdata_d or getbusy or num_addr or 
             datardready or  datawrready or  statuswrready or 
             st_cnt or cmddata or  getstatus or command or cmdrdready or 
             card_ok or cmd_num or statusrd_done ) begin 
    case ( sm_state) // synopsys full_case parallel_case
      IDLE: begin
        reset_cnt  = 1;
        command_c = command ;
        if ( cmdrdready ) begin 
          nxt_sm_state  = RD_CMD;  
          cmdrd_c  = 1;
        end
        else if ( getdata_d && datawrready && card_ok ) begin 
          nxt_sm_state  = READ;  
          cmdrd_c  = 0;
        end
        else if ( datardready  && !new_page  && card_ok) begin // rha 12.28
          nxt_sm_state  = WRITE ;  
          cmdrd_c  = 0;
        end 
        else if (( getstatus || getbusy ) && statuswrready && !statusrd_done ) begin
          nxt_sm_state  = WR_STATUS ;  
          cmdrd_c  = 0;
        end 
        else if ( datardready && new_page && card_ok  )  begin
          nxt_sm_state  = CMD;  
          cmdrd_c  = 0;
        end 
        else begin
          nxt_sm_state  = IDLE;  
          cmdrd_c  = 0;
        end
      end
      RD_CMD: begin
        reset_cnt  = 1;
        command_c = ( cmd_num == 3'h2 ) ? cmddata:command ;
        if ( cmd_num == 'h6  &&  hidden_cmd0 == 0 )begin
          cmdrd_c  = 0;
          nxt_sm_state  = CMD;  
        end
        else if ( cmd_num == 'h6  )begin
          cmdrd_c  = 0;
          nxt_sm_state  = HIDDEN_CMD;  
        end
        else begin
          cmdrd_c  = 1;
          nxt_sm_state  = RD_CMD;  
        end
      end
      HIDDEN_CMD: begin
        cmdrd_c = 0;
        reset_cnt = ( clk_cnt == 'b11 )? 1 : 0;
        command_c = command ;
        if ( clk_cnt == 'b11 && st_cnt == 2 ) 
          nxt_sm_state  = CMD ;
        else 
          nxt_sm_state  = HIDDEN_CMD ;
      end 
      CMD: begin
        cmdrd_c = 0;
        reset_cnt = ( clk_cnt == 'b11 )? 1 : 0;
        command_c = command ;
        if ( clk_cnt == 'b11 )begin
          if ( command == READ11  || command == DATA_IN ||
               command == READ12  || command == READ2 ||
          // if ( command == READ11  || command == DATA_IN || 11.30
               command == READ_ID || command == ERASE_SETUP )  begin
            nxt_sm_state  = ADDR ;
          end
          else if ( command == PROGRAM ) begin
            nxt_sm_state  = DUMMY_WR ;
          end
          else if ( command == READ_STATUS ) begin
            nxt_sm_state  = GET_STATUS ;
          end
          else begin
            nxt_sm_state  = IDLE ;
          end
        end
        else begin
          nxt_sm_state  = CMD ;
        end
      end
      ADDR: begin
        cmdrd_c = 0;
        reset_cnt = ( clk_cnt == 'b10 )? 1 : 0;
        if ( st_cnt == num_addr && command == ERASE_SETUP && clk_cnt == 'b10 )begin
          command_c = ERASE ;
          nxt_sm_state  = CMD;
        end
        else if ( st_cnt == num_addr && clk_cnt == 'b10 )  begin
          command_c = command ;
          nxt_sm_state  = IDLE;
        end
        else begin
          command_c = command ;
          nxt_sm_state  = ADDR;
        end
      end
      WRITE: begin
        cmdrd_c = 0;
        reset_cnt = ( clk_cnt == 'b11 )? 1 : 0;
        if ( !datardready ) begin
          command_c = command ;
          nxt_sm_state  = IDLE;
        end
        else if ( datardready && num_sent == num_send && clk_cnt == 'b11 ) begin
          command_c = PROGRAM ;
          nxt_sm_state  = CMD;
        end
        else begin
          command_c = command ;
          nxt_sm_state  = WRITE;
        end
      end  
      READ: begin
        cmdrd_c = 0;
        command_c = command ;
        //if ( !datawrready || !getdata || !card_ok ) begin 
        if ( !datawrready || !getdata ) begin  //11.30
          nxt_sm_state  = IDLE;
          reset_cnt  = 1;
        end
        else if ( !card_ok ) begin 
          nxt_sm_state  = WAIT_SM;
          reset_cnt  = 1;
        end
        else if ( num_sent == num_send && clk_cnt == 'b11 ) begin
          nxt_sm_state  = DUMMY_RD;
          reset_cnt  = 1;
        end
        else if ( clk_cnt == 'b11 ) begin
          reset_cnt  = 1;
          nxt_sm_state  = READ;
        end
        else begin
          reset_cnt  = 0;
          nxt_sm_state  = READ;
        end
      end  
      DUMMY_RD: begin
        cmdrd_c = 0;
        command_c = command ;
        reset_cnt = ( clk_cnt == 'b11 )? 1 : 0;
        if ( !datawrready  ) 
          nxt_sm_state  = IDLE;
        else
          nxt_sm_state  = DUMMY_RD;
      end
      DUMMY_WR: begin
        cmdrd_c = 0;
        reset_cnt = ( clk_cnt == 'b11 )? 1 : 0;
        if ( !datardready ) begin
          nxt_sm_state  = WAIT_SM;
          command_c = READ_STATUS ;
        end
        else begin
          nxt_sm_state  = DUMMY_WR ;
          command_c = command ;
        end
      end
      WAIT_SM: begin
        cmdrd_c = 0;
        command_c = command ;
        reset_cnt =  1 ;
        if ( card_ok && ( command == READ11 || 
             command == READ12 || command == READ2)  && num_sent == num_send )
          nxt_sm_state  = DUMMY_RD ;
        else if ( card_ok && ( command == READ11 ||
             command == READ12 || command == READ2 ) ) 
          nxt_sm_state  = READ ;
        else if ( card_ok ) 
          nxt_sm_state  = CMD ;
        else if ( cmdrdready ) 
          nxt_sm_state  = IDLE;
        else
          nxt_sm_state  = WAIT_SM ;
      end
      GET_STATUS: begin
        cmdrd_c = 0;
        reset_cnt = ( clk_cnt == 'b10 )? 1 : 0;
        if ( clk_cnt == 2'b10 ) begin
          nxt_sm_state  = IDLE;
          command_c = DATA_IN ;
        end
        else begin
          nxt_sm_state  = GET_STATUS;
          command_c = command ;
        end
      end
      WR_STATUS: begin
        command_c = command ;
        cmdrd_c = 0;
        reset_cnt = ( clk_cnt == 'b10 )? 1 : 0;
        if ( st_cnt == 'b10 && clk_cnt == 2'b10)
        //if ( st_cnt == 'b1 && clk_cnt == 2'b10)
          nxt_sm_state  = IDLE ;
        else
          nxt_sm_state  = WR_STATUS ;
      end  
    endcase
  end
endmodule

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -