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

📄 udevctl.v

📁 实现USB接口功能的VHDL和verilog完整源代码
💻 V
📖 第 1 页 / 共 4 页
字号:
				|| ( (ctlst == CTLSETUP) && (nextctlst == CTLWAIT4SETUP)) )
		   getbusy <=1'b0;
	end

/* 
This file includes the read and write strobes as well as the
read multiplexor which is specific to the endpoint configuration.
*/

// READ PATH
// create endpoint write signals here

assign corecommand =  ( (setuprequest == GETDESCRIPTOR)&&(setuptype == 2'b00) ) || 
		( (setuprequest == SETADDRESS)&&(setuptype == 2'b00) ) ||
		( (setuprequest == SETINTERFACE)&&(setuptype == 2'b00) ) ||
		( (setuprequest == SETCONFIGURATION)&&(setuptype == 2'b00) ) ||
		( (setuprequest == GETINTERFACE)&&(setuptype == 2'b00) ) ||
		( (setuprequest == GETCONFIGURATION)&&(setuptype == 2'b00) ) ||
		( (setuprequest == GETSTATUS)&&(setuptype == 2'b00) ) ||
		( (setuprequest == CLEARFEATURE) && (setuptype == 2'b00) ) || 
		( (setuprequest == SETFEATURE) && (setuptype == 2'b00) );

// wire	endp0noncorecmd =  (~((setuptype == 2'b00) && corecommand) &&	// not a  standard corecommand
//			setupcycle && setupinfovalid && (currentendp == 4'h0));

 wire	endp0noncorecmd =  ((setuptype == 2'b10) &&	// not a  standard corecommand
			setupcycle && setupinfovalid && (currentendp == 4'h0));
// only generate the write if the endpoint is ready, don't overflow

wire	endp0wr = (currentendp == 4'h0) ? (grabdata && secondbytevalid && endp0wrready && endp0noncorecmd) : 1'b0;
wire    endp1wr = (currentendp == 4'h1) ? (grabdata && secondbytevalid && endp1wrready ) : 1'b0; 
//wire    endp0wr = (currentendp == 4'h0) ? ( (grabdata || grabsetup) && secondbytevalid && endp0wrready && ~corecommand) : 1'b0;
 wire	endp0rd = (currentendp == 4'h0) ? (nextdata && endp0noncorecmd) : 1'b0;
// wire endp0rd = (currentendp == 4'h0) ? (nextdata && ~corecommand) : 1'b0; //JAKE
wire	endp2rd = (currentendp == 4'h2) ?  nextdata : 1'b0;
wire    endp3rd = (currentendp == 4'h3) ?  nextdata : 1'b0;

wire    envid0 = (nextctlst == CTLSENDDATA && totalbytecount == 'h8 && getnoncfgdesccycleflag && corecommand && vidflag);
wire    envid1 = (nextctlst == CTLSENDDATA && totalbytecount == 'h9 && getnoncfgdesccycleflag && corecommand && vidflag);
wire    enpid0 = (nextctlst == CTLSENDDATA && totalbytecount == 'h0a && getnoncfgdesccycleflag && corecommand && vidflag);
wire    enpid1 = (nextctlst == CTLSENDDATA && totalbytecount == 'h0b && getnoncfgdesccycleflag && corecommand && vidflag);

// create the read back data, ready signals JAKE

always @(romdevsetupdata or setupcycle or 
        currentendp or bytecount or totalbytecount or
		endp0noncorecmd or
		endp0wrready or endp0wrstall or
		endp0rdready or endp0rddata or endp0rdstall or
		endp1wrready or endp1wrstall or endp2rddata or endp2rdready or endp2rdstall or 
		endp3rddata or endp3rdready or endp3rdstall or setupwindex or setuprecipient or setuprequest or setuplength or
                corecommand or currentalternatesetting or vidreg0 or vidreg1 or pidreg0 or
				pidreg1 or setuptype or envid0 or envid1 or enpid0 or enpid1 or
		setupwindexin or ourconfiguration or sendzerolengthpkt or gpiord or setupvalue0 or gpiopin0 or gpiopin1 or gpiopin2)
  begin
    case (currentendp)	// synopsys parallel_case full_case
      4'h0:	
	begin
	  if (setuprequest == GETSTATUS && setuptype == TYPESTD) 
	    begin
	      case (bytecount)	// synopsys parallel_case full_case
		  16'h0: 
		     if (setuprecipient == REQENDPOINT)
		        begin
		          xmitdatabyte[7:1] = 7'h00;
		          case ({setupwindexin,setupwindex[3:0]})	// 1 for rd, endpoint index
			            5'h00:	xmitdatabyte[0] = 1'b0;	// endpoint 0 never stalls   confirm ? <= Ch9 test
			            5'h10:	xmitdatabyte[0] = 1'b0;	// endpoint 0 never stalls
			            5'h01:	xmitdatabyte[0] = endp1wrstall;  // 1'b0;  // endpoint 1 wr stall
			            5'h11:	xmitdatabyte[0] = 1'b0;
			            5'h02:	xmitdatabyte[0] = 1'b0;
			            5'h12:	xmitdatabyte[0] = endp2rdstall;	// non-existent, never stalls
			            5'h03:	xmitdatabyte[0] = 1'b0;	// non-existent, never stalls
			            5'h13:	xmitdatabyte[0] = endp3rdstall; //endp3rdstall;
			            default: xmitdatabyte[0] = 1'b0;	// no stall
		         endcase
		       end
		     else
		        if (setuprecipient == REQDEVICE)
		            xmitdatabyte = 8'h00;
		     else
		         xmitdatabyte = {6'h00,REMOTEWAKEUP,SELFPOWERED};
		  5'h01:
		       xmitdatabyte = 8'h00;	// always all zeros
	      endcase
	    end
	  else if (setuprequest == GETCONFIGURATION && setuptype == TYPESTD)
	    xmitdatabyte = {7'h0,ourconfiguration};
	  else if (setuprequest == GETINTERFACE && setuptype == TYPESTD)
	    xmitdatabyte = currentalternatesetting;	// send alternate interface setting
	 
	  else if(setuprequest == GETDESCRIPTOR && setuptype == TYPESTD)  
		   xmitdatabyte = (envid0) ? vidreg0 : (envid1) ? vidreg1 : (enpid0) ? pidreg0
						  : (enpid1) ? pidreg1 :romdevsetupdata;
	  
	 else if(gpiord)
		   xmitdatabyte =(setupvalue0 =='h00) ? { 7'h00,gpiopin0} : (setupvalue0 =='h01) ? {7'h00,gpiopin1} :
						 (setupvalue0 =='h02) ? {7'h00,gpiopin2} : 8'h00;
	  else              // Vendor CMD DATA raad
		xmitdatabyte = endp0rddata;
          
	 // JAKE have a doubt that default endpoint is not stalled ? 
	  endpointrdready = (~corecommand && ~gpiord) ? endp0rdready : 1'b1;
	  endpointwrready = (~corecommand) ? endp0wrready : 1'b1;
	  endpointrdstall =  1'b0;
	  endpointwrstall = 1'b0;
	end
      4'h1:	// this is a write-only endpoint, use 1 for default
	begin
	  xmitdatabyte = 8'h00;
	  endpointrdready = 1'b0;
	  endpointwrready = endp1wrready;
	  endpointrdstall = 1'b0;
	  endpointwrstall = endp1wrstall; //JAKE 1'b1;
	end
      4'h2:	// this is a read-only endpoint, use 1 for default
	begin
	  xmitdatabyte = endp2rddata;
	  endpointrdready = endp2rdready;
	  endpointwrready = 1'b0;
	  endpointrdstall = endp2rdstall;
	  endpointwrstall = 1'b1; //JAKE 1'b1;
	end
      4'h3:	// this is a read-only endpoint, use 1 for default
	begin
	  xmitdatabyte = endp3rddata;
	  endpointrdready = endp3rdready;
	  endpointwrready = 1'b0;
	  endpointrdstall = endp3rdstall;
	  endpointwrstall = 1'b1; //JAKE 1'b1;
	end
	default:
	begin
	  xmitdatabyte = 8'h00;
	  endpointrdready = 1'b0;
	  endpointwrready = 1'b0;
	  endpointrdstall = 1'b1;
	  endpointwrstall = 1'b1;
	end
    endcase
  end // always @ (romdevsetupdata or setupcycle or...
  
////////////////////// End of READ DATA PATH //////////////////////////////


  always @(currentendp or setuplength or setupcycle or
	   cfgdesclength or sendzerolengthpkt or bytecountd or
	   totalbytecountd or corecommand or setuprequest or endp2rdready  or endp3rdready or
	   xmitlastbyte or ctlst or getcfgdesccycleflag)
    begin
      if (ctlst == CTLDIN)
	xmitlastbyted = 1'b0;
//      else if (xmitlastbyte && ctlst != CTLIDLE)
//	xmitlastbyted = 1'b1;
      else
	case (currentendp) // synopsys parallel_case
	  4'h0 : xmitlastbyted = sendzerolengthpkt || 
	    (bytecountd == MAXPACKET0) ||
	    (totalbytecountd == cfgdesclength && getcfgdesccycleflag) || 
	    (totalbytecountd == setuplength) ; 
		
	  4'h1 : xmitlastbyted = (bytecountd == MAXPACKET1);  //  || ~endp1rdready; JAKE 
	  4'h2 : xmitlastbyted = (bytecountd == MAXPACKET2) || ~endp2rdready;
	  4'h3 : xmitlastbyted = (bytecountd == MAXPACKET3) || ~endp3rdready;
	  default : xmitlastbyted = 1'b1;
	endcase // case (currentendp)
    end // always @ (currentendp or setuplength or setupcycle or...


always @(rcvsetup or rcvdatain or rcvdataout or setupcycle or firsttimethrough or
	 nextxmitbyte or ctlst or rcvpacketnotok or rcvpacketok or
	grabsetup or grabdata or secondbytevalid or currentendp or
	 turnaround or totalbytecount or prevtotalbytecount or endpoint0active)
  begin
    if ( ( rcvsetup && ~setupcycle)  ||  
	// need to not reset byte count if setupcycle and not first time of this setup 
	((rcvdatain || rcvdataout) && (~setupcycle || firsttimethrough)))
      totalbytecountd = 16'h0000;
    else if (endpoint0active && (nextxmitbyte || grabsetup || (grabdata && secondbytevalid)))
      totalbytecountd = totalbytecount + 1'b1;
    // we missed the ack
    else if ((ctlst == CTLWAIT4ACK) && endpoint0active && (rcvpacketnotok || rcvpacketok || turnaround))
      totalbytecountd = prevtotalbytecount;
    else
      totalbytecountd = totalbytecount;
  end // always @ (rcvsetup or rcvdatain or rcvdataout or setupcycle or firsttimethrough or...
  
  
  always @(rcvsetup or rcvdatain or rcvdataout or 
        grabsetup or grabdata or secondbytevalid or
	   nextxmitbyte or bytecount or setupcycle)
    begin
      if ( (rcvsetup && ~setupcycle) || rcvdatain || rcvdataout)
	bytecountd = 16'h0000;
      else if (nextxmitbyte || (grabsetup || (grabdata && secondbytevalid)) )
	bytecountd = bytecount + 1'b1;
      else
	bytecountd = bytecount;
    end // always @ (rcvsetup or rcvdatain or rcvdataout or...
  
// create all the real flip-flop signals
  always @(posedge usbclock)
    begin
      bytecount <= (usbreset) ? 16'h0 : bytecountd;
      totalbytecount <= (usbreset) ? 16'h0 : totalbytecountd;

      // need firsttimethrough to know if this is part of a longer setup cycle
      if (usbreset || (rcvsetup && ~setupcycle) )     // each setup transaction reset first time through
        firsttimethrough <= 1'b1;
      else if (rcvdatain || rcvdataout)     // once here, it is no longer first time through
        firsttimethrough <= 1'b0;
       
      if ( (rcvsetup && ~setupcycle) || (rcvdatain || rcvdataout) && (~setupcycle || firsttimethrough))
        prevtotalbytecount <= 16'h0;
      else if (ctlst == CTLWAIT4ACK && rcvack)
        prevtotalbytecount <= totalbytecount;      
      
      if (usbreset)
        setupinfovalid <= 1'b0;
      else if ( (ctlst == CTLSETUP) && (nextctlst ==CTLWAIT4SETUP)) 	// always remove setupinfovalid when SETUP comes
        setupinfovalid <= 1'b0;
      //else if (ctlst == CTLGRABSETUP)       // clean setup info received
      else if ((ctlst == CTLGRABSETUP) && (nextctlst == CTLSENDACK))       // clean setup info received
        setupinfovalid <= 1'b1;
      //else if (endpoint0active && 
      //		((ctlst == CTLDOUT && nextctlst == CTLWAIT4DATA && (setupdirection == DEVTOHOST)) ||
      //          (ctlst == CTLDIN && rcvpacketok && setupdirection == HOSTTODEV)))
      
      else if (endpoint0active && addressmatch && 
      		((ctlst == CTLWAIT4DATA && nextctlst == CTLSENDACK && (setupdirection == DEVTOHOST)) ||
            (ctlst == CTLWAIT4ACK && rcvack && setupdirection == HOSTTODEV)))
	  setupinfovalid <= 1'b0;

      if (usbreset)
        setupcycle <= 1'b0;
     // else if (nextctlst == CTLSETUP) //JAKE 4.24 	// always remove setupcycle when SETUP comes
     //   setupcycle <= 1'b0;
      else if ((ctlst == CTLSETUP) && (nextctlst == CTLWAIT4SETUP))       // clean setup packet and for us
        setupcycle <= 1'b1;
      // I want to keep setupcycle asserted throughout the whole thing.
      // setup packet, whatever outs/ins and the zero length terminator
      // the change in direction st==DOUT && direction = IN denotes the zero length
      else if (endpoint0active && addressmatch &&
		//((ctlst == CTLDOUT && nextctlst == CTLWAIT4DATA && (setupdirection == DEVTOHOST)) ||
		((ctlst == CTLWAIT4DATA && nextctlst == CTLSENDACK  && (setupdirection == DEVTOHOST)) ||
        (ctlst == CTLWAIT4ACK && rcvack && setupdirection == HOSTTODEV)))
        setupcycle <= 1'b0;
      
      //                 rcvsof   ends setup - jve took this case out. i dont think
      // sof can end setup
      
      if (usbreset)
        endpointzerotogglebit <= 1'b0;       // clear during reset
      else if (sendzerolengthpkt)
        endpointzerotogglebit <= 1'b1;  // always do data1 on the zero length setup
      // terminator
      else if ((ctlst == CTLGRABSETUP) && (nextctlst == CTLSENDACK))     // setup always goes to 1
        endpointzerotogglebit <=  1'b1;
      else if (endpoint0active && (ctlst == CTLGRABDATA) && (nextctlst == CTLSENDACK))     // toggle on receipt of good data
        endpointzerotogglebit <= ~endpointzerotogglebit;
      else if (endpoint0active && (ctlst == CTLWAIT4ACK) && rcvack)    // toggle once good data has been sent and seen
        endpointzerotogglebit <= ~endpointzerotogglebit;
 
      if (usbreset)
        endpointintogglebits[MAXENDPOINTS:1] <= 0;       // clear em all during reset
      else if (~configured || clearfeature)             // not configured, or configuration has been reset
														 // JAKE For ClearfeaTURE, DAta toggle 0 
        endpointintogglebits[MAXENDPOINTS:1] <= 0;
      else if (endpoint0active)
        endpointintogglebits <= endpointintogglebits;       // hold during endpoint0 stuff
      else if ((ctlst == CTLWAIT4ACK) && rcvack && ~isoendpoint)    // toggle once good data has been sent and seen
//        endpointintogglebits[currentendp] <= ~endpointintogglebits[currentendp];
        endpointintogglebits[currentendp[1:0]] <= ~endpointintogglebits[currentendp[1:0]];
 
      if (usbreset)
        endpointouttogglebits[MAXENDPOINTS:1] <= 0;       // clear em all during reset
      else if (~configured || clearfeature)             // not configured, or configuration has been reset
														//JAKE
        endpointouttogglebits[MAXENDPOINTS:1] <= 0;
      else if (endpoint0active)
        endpointouttogglebits <= endpointouttogglebits;       // hold during endpoint0 stuff
      else if ((ctlst == CTLGRABDATA) && (nextctlst == CTLSENDACK) && ~isoendpoint)      // toggle on receipt of good data
//        endpointouttogglebits[currentendp] <= ~endpointouttogglebits[currentendp];
        endpointouttogglebits[currentendp[1:0]] <= ~endpointouttogglebits[currentendp[1:0]];

      if (usbreset)
        sendstall <= 1'b0;
      else if (nextctlst == CTLSENDSTALL)
        sendstall <= 1'b1;
      else if (stallsent)
        sendstall <= 1'b0;
      
      if (usbreset)
        sendnak <= 1'b0;
      else if (nextctlst == CTLSENDNAK)
        sendnak <= 1'b1;
      else if (naksent)

⌨️ 快捷键说明

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