BBCプログラムの抜粋です
//--------------------------------------------------------------------------- void __fastcall TForm2::Button_DownloadClick(TObject *Sender) { if(PortOpen() != -1){ if(Configuration() == 0){ Label_Status->Caption = "SUCCESS"; }else{ Label_Status->Caption = "FAILURE"; } PortClose(); } return; } //--------------------------------------------------------------------------- int __fastcall TForm2::Configuration() { HANDLE hFile; int selectDevice; int sizeBitstream; int sizeSendData; int tx_loop; int tx_loop_tail; int ret; BYTE *bufBitstream; BYTE *bufSendData; BYTE byDone; DWORD numOfBytesRead; String fileExt, str; hFile = CreateFileA(Edit_Bitstream->Text.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); if(hFile == INVALID_HANDLE_VALUE){ MessageBox(Handle, "File open error", "NOTICE", MB_OK); return -1; } selectDevice = ComboBox_Device->ItemIndex + 1; fileExt = ExtractFileExt(Edit_Bitstream->Text); // // XILINX // if(gDetectDevices[selectDevice].deviceVendor == "XILINX"){ if( (fileExt != ".bit")&&(fileExt != ".bin") ){ MessageBox(Handle, "Invalid file for this device", "NOTICE", MB_OK); return -1; } // // ALTERA // }else if(gDetectDevices[selectDevice].deviceVendor == "ALTERA"){ if(fileExt != ".rbf"){ MessageBox(Handle, "Invalid file for this device", "NOTICE", MB_OK); return -1; } // // XILINX or ALTERA // }else{ //Do nothing } sizeBitstream = GetFileSize(hFile, NULL); sizeSendData = (sizeBitstream*8*2) + (CCLK_ADDITIONAL*2); //BYTE*CLOCK(HIGH & LOW) bufBitstream = new BYTE[sizeBitstream+1]; bufSendData = new BYTE[sizeSendData+1]; ZeroMemory(bufBitstream, sizeBitstream+1); ZeroMemory(bufSendData, sizeSendData+1); ReadFile(hFile, bufBitstream, sizeBitstream, &numOfBytesRead, NULL); ConvertBitstream(bufBitstream, bufSendData, sizeBitstream, sizeSendData, fileExt); AddMemo("Start Configuration to " + gDetectDevices[selectDevice].name); ResetFPGA(); Sleep(3*2); // Wait for T_PL(Program Latency) SendConfigData(bufSendData, sizeSendData); AddMemo("Configuration End"); CloseHandle(hFile); delete []bufBitstream; delete []bufSendData; FT_GetBitMode(gFTHandle, &byDone); byDone &= PIN_DONE; if(byDone){ return 0; }else{ return -1; } } //--------------------------------------------------------------------------- void __fastcall TForm2::ConvertBitstream(BYTE *bufBitstream, BYTE *bufSendData, int sizeBitstream, int sizeSendData, String fileExt) { int sendBytePos; int txLoop, txTail; BYTE byteTemp; sendBytePos = 0; for(int bytePos=0; bytePos<sizeBitstream; bytePos++) { byteTemp = bufBitstream[bytePos]; // // XILINX // if( (fileExt == ".bit")||(fileExt == ".bin") ){ for(int bitPos=0; bitPos<8; bitPos++){ if(byteTemp & 0x80){ bufSendData[sendBytePos++] = MASK_SEND_DATA | PIN_DATA; bufSendData[sendBytePos++] = MASK_SEND_DATA | PIN_DATA | PIN_CLK; } else{ bufSendData[sendBytePos++] = MASK_SEND_DATA; bufSendData[sendBytePos++] = MASK_SEND_DATA | PIN_CLK; } byteTemp <<= 1; } } // // ALTERA // else if(fileExt == ".rbf"){ for(int bitPos=0; bitPos<8; bitPos++){ if(byteTemp & 0x01){ bufSendData[sendBytePos++] = MASK_SEND_DATA | PIN_DATA; bufSendData[sendBytePos++] = MASK_SEND_DATA | PIN_DATA | PIN_CLK; } else{ bufSendData[sendBytePos++] = MASK_SEND_DATA; bufSendData[sendBytePos++] = MASK_SEND_DATA | PIN_CLK; } byteTemp >>= 1; } } } // // Extra clocking // for(int bytePos=0; bytePos<CCLK_ADDITIONAL; bytePos++){ bufSendData[sendBytePos++] = MASK_SEND_DATA | PIN_DATA; bufSendData[sendBytePos++] = MASK_SEND_DATA | PIN_DATA | PIN_CLK; } return; } //--------------------------------------------------------------------------- void __fastcall TForm2::SendConfigData(BYTE *bufData, int size) { int txLoop, txTail; DWORD dwBytesWritten; DWORD startTime, diffTime; String str; FT_STATUS ftStatus; txLoop = size/FT_BUFF_SIZE_OUT; txTail = size%FT_BUFF_SIZE_OUT; startTime = timeGetTime(); for(int i=0; i<txLoop; i++){ FT_Write(gFTHandle, bufData, FT_BUFF_SIZE_OUT, &dwBytesWritten); bufData += FT_BUFF_SIZE_OUT; } if(txTail > 0){ FT_Write(gFTHandle, bufData, txTail, &dwBytesWritten); ProgressBar->StepBy(txTail); } diffTime = timeGetTime() - startTime; Label_Time->Caption = str.sprintf("%d ms", diffTime); return; }