26 #include "../../../Common/FileIO.h"
27 #include "../../../Common/str.h"
28 #include "../../../Common/Endianness.h"
34 enum FILEFORMAT {FILE_NULL = 0, FILE_BIT, FILE_BIN, FILE_BIT_ASM};
35 FILEFORMAT fileformatIn = FILE_NULL, fileformatOut = FILE_NULL;
38 fileformatIn = FILE_BIT;
40 fileformatIn = FILE_BIN;
42 fileformatIn = FILE_BIT_ASM;
44 fileformatOut = FILE_BIT;
46 fileformatOut = FILE_BIN;
48 fileformatOut = FILE_BIT_ASM;
50 ifstream fin (filenameIn, ifstream::binary);
52 throw runtime_error(
string(
"Could not open file: \"").append(filenameIn).append(
"\" .\n"));
54 ofstream fout (filenameOut, ofstream::binary | ofstream::trunc);
56 throw runtime_error(
string(
"Could not open file: \"").append(filenameOut).append(
"\"!\n"));
58 if(fileformatIn == FILE_BIT && fileformatOut == FILE_BIT_ASM)
59 disassemblerBitToAsm(fin, fout);
60 else if(fileformatIn == FILE_BIN && fileformatOut == FILE_BIT_ASM)
61 disassemblerBinToAsm(filenameIn, fin, fout);
62 else if(fileformatIn == FILE_BIT_ASM && fileformatOut == FILE_BIT)
63 assemblerAsmToBit(fin, fout);
64 else if(fileformatIn == FILE_BIT_ASM && fileformatOut == FILE_BIN)
65 assemblerAsmToBin(fin, fout);
67 throw runtime_error(
string(
"Unknown Xilinx US assembler operation between file formats. See \"byteman -h assembly\".\n"));
75 setDeviceByIDCODEOrThrow(idcode);
76 designName = filenameIn;
79 disassemblerToAsm(fin, fout);
85 setDeviceByPartNameOrThrow();
87 disassemblerToAsm(fin, fout);
90 assemblerParseHeader(fin);
92 setDeviceByPartNameOrThrow();
93 initializeResourceStringParameters();
96 assemblerAsmTo(fin, fout);
99 assemblerParseHeader(fin);
101 setDeviceByPartNameOrThrow();
102 initializeResourceStringParameters();
104 assemblerAsmTo(fin, fout);
109 for (
string line; getline(fin, line); ) {
110 auto firstEqPos = line.find_first_of(
'=');
111 if(firstEqPos != string::npos)
112 line.replace(firstEqPos, 1,
'=',
' ');
120 log(
"Design name: " + designName);
121 log(
"FPGA: " + partName);
122 log(
"Date: " + fileDate);
123 log(
"Time: " + fileTime);
131 int slr = 0, b = 7, r = 0, c = 0, m = 0;
132 for (
string line; getline(fin, line); ) {
133 if(line.at(line.find_first_not_of(
" \t")) ==
'#')
135 transform(line.begin(), line.end(), line.begin(), ::toupper);
136 replace(line.begin(), line.end(),
'=',
' ');
137 replace(line.begin(), line.end(),
'#',
' ');
138 replace(line.begin(), line.end(),
',',
' ');
150 uint32_t byteValue = value % 256;
156 XCAP_writeNOP(fout, 1, nopHiddenValue, loadedBitstreamEndianness);
159 int reservedHiddenValue;
179 throw runtime_error(
string(
"Couldn't parse assembly command: \"").append(line).append(
"\"!"));
184 throw runtime_error(
string(
"FDRI command needs size: \"").append(line).append(
"\"!"));
193 for (
string frameLine; getline(fin, frameLine); ) {
194 if(frameLine.at(frameLine.find_first_not_of(
" \t")) ==
'#')
198 throw runtime_error(
string(
"Was expecting the data for a full frame on this line: \"").append(frameLine).append(
"\", because I think there are ").append(
to_string(frameCount)).append(
" frames left."));
215 throw runtime_error(
string(
"End of file reached while performing FDRI frame writes.!"));
219 throw runtime_error(
string(
"Could not parse the new FAR value: \"").append(line).append(
"\"!"));
220 r += SLRinfo[slr].fromRow;
226 throw runtime_error(
string(
"Could not parse the new register value: \"").append(line).append(
"\"!"));
230 regAddr = newRegAddr;
235 throw runtime_error(
string(
"Couldn't parse assembly command: \"").append(line).append(
"\"!"));
244 fout <<
"--- HEADER BEGIN ---" << endl;
245 fout <<
"Name = \"" << designName <<
"\"" << endl;
246 fout <<
"FPGA = \"" << partName <<
"\"" << endl;
247 fout <<
"Date = \"" << fileDate <<
"\"" << endl;
248 fout <<
"Time = \"" << fileTime <<
"\"" << endl;
249 fout <<
"--- HEADER END ---" << endl;
253 initializeResourceStringParameters();
254 disassemblerWriteHeader(fout);
257 bool synched =
false;
258 bool aligned =
false;
260 int shadowFrameValid = 0;
261 int slr = 0, b = 7, r = 0, c = 0, m = 0;
268 streamoff startFileOffset = fin.tellg();
269 streamoff endFileOffset;
272 endFileOffset = fin.tellg() - (streamoff)4;
276 endFileOffset = fin.tellg();
280 endFileOffset = fin.tellg() - (streamoff)4;
283 endFileOffset = fin.tellg();
286 fin.seekg(startFileOffset, fin.beg);
290 fout <<
"SYNC" << endl;
295 uint32_t instruction =
FileIO::read32(fin, loadedBitstreamEndianness);
299 if(instructionType == 1) {
302 }
else if(instructionType == 2) {
303 wordCount = instructionPayload;
305 fout <<
"0x" << uppercase << hex << setw(8) << setfill(
'0') << instruction <<
" (Invalid instruction [invalid type])" << endl;
310 if(instructionPayload != 0)
311 fout <<
"NOP #" << instructionPayload << endl;
313 fout <<
"NOP" << endl;
315 if(instructionPayload != 0)
316 fout <<
"RESERVED #" << instructionPayload << endl;
318 fout <<
"RESERVED" << endl;
320 fout <<
"Read Reg @";
322 fout <<
" for length #" << wordCount << endl;
325 if(shadowFrameValid) {
326 fout << dec <<
"# Shadow register contents are written to frame (BlockType=" << b <<
", GlobalRowAddress=" << r <<
", MajorAddress=" << c <<
", MinorAddress=" << m <<
") (Frame type: " << getFrameType(b, r, c) <<
")." << endl;
329 shadowFrameValid = 1;
331 fout << dec <<
"@FDRI for #" << frameCount <<
" frames:" << endl;
332 for(
int i = 0 ; i < frameCount ; i++){
334 if(i == (frameCount-1)) fout <<
"(This frame data is written to shadow register!)";
335 fout << dec <<
"Writing frame #" << i <<
" (BlockType=" << b <<
", GlobalRowAddress=" << r <<
", MajorAddress=" << c <<
", MinorAddress=" << m <<
") (Frame type: " << getFrameType(b, r, c) <<
") hex data:" << endl;
342 fout <<
"0x" << uppercase << hex << setw(8) << setfill(
'0') << frameData[w] <<
" ";
346 fout <<
"0x" << uppercase << hex << setw(8) << setfill(
'0') << frameData[w] <<
" ";
349 fout <<
"0x" << uppercase << hex << setw(8) << setfill(
'0') << frameData[w] <<
" ";
355 uint32_t writeData =
FileIO::read32(fin, loadedBitstreamEndianness);
359 shadowFrameValid = 0;
363 shadowFrameValid = 0;
364 fout <<
" (also clears shadow register)";
368 uint32_t nextInstr =
FileIO::read32(fin, loadedBitstreamEndianness);
376 fout <<
"Select next SLR for the next #" << dec << nextInstrPayload <<
" words." << endl;
378 fout <<
"Bad MAGIC1 instruction" << endl;
379 fin.seekg(-4, ios::cur);
381 }
else if((instructionType == 1) && (wordCount == 0)){
382 fout <<
"Select register @";
385 }
else if(wordCount == 1){
386 uint32_t writeData =
FileIO::read32(fin, loadedBitstreamEndianness);
391 fout <<
" = BlockType=" << dec << b <<
" RowAddress=" << (r-SLRinfo[slr].fromRow) <<
" MajorAddress=" << c <<
" MinorAddress=" << m << endl;
393 fout <<
" = 0x" << uppercase << hex << setw(8) << setfill(
'0') << writeData << endl;
396 fout <<
"0x" << uppercase << hex << setw(8) << setfill(
'0') << instruction <<
"(Bad instruction)" << endl;
#define XUS_WORDS_BEFORE_CLK
#define XUS_WORDS_PER_FRAME
void assemblerParseHeader(std::ifstream &)
void assemblerAsmToBit(std::ifstream &, std::ofstream &)
void disassemblerToAsm(std::ifstream &, std::ofstream &)
void disassemblerBitToAsm(std::ifstream &, std::ofstream &)
void disassemblerBinToAsm(std::string, std::ifstream &, std::ofstream &)
void assembler(std::string, std::string)
void assemblerAsmToBin(std::ifstream &, std::ofstream &)
void disassemblerWriteHeader(std::ofstream &)
void assemblerAsmTo(std::ifstream &, std::ofstream &)
void assemblyOutputData(std::ifstream &fin, std::ofstream &fout, std::streamoff sizeInBytes)
XCAP::Register XCAP_getInstructionRegister(uint32_t instruction)
Parses and returns instruction register. This is the register being addressed if the instruction is o...
XCAP::Operation XCAP_getInstructionOperation(uint32_t instruction)
Parses and returns instruction operation. Most Xil instructions will NOP or write.
uint32_t XCAP_getInstructionPayload(uint32_t instruction)
Parses and returns instruction payload. This is the immediate value after instruction type and operat...
void XCAP_writeRegister(std::ofstream &fout, XCAP::Register reg, int writeValue, Endianness e)
Generate the encoding for writing a CAP register and write it to file ofstream.
uint32_t XCAP_getInstructionWordCount(uint32_t instruction)
Parses and returns instruction word count. This is the number of words to be read/written if the inst...
void XCAP_writeFDRI1(std::ofstream &fout, int wordCount, Endianness e)
Generate and write only a type 1 FDRI command.
XCAP::Command getXCAPcommand(std::string s)
void XCAP_writeCommand(std::ofstream &fout, XCAP::Command cmd, Endianness e)
Generate the encoding for writing a CAP command and write it to file ofstream.
void XCAP_writeSYNQ(std::ofstream &fout, Endianness e)
Generate and write an SYNQ command.
void XCAP_writeSelectRegister(std::ofstream &fout, XCAP::Register reg, Endianness e)
Generate the encoding for "selecting" a CAP register and write it to file ofstream.
void XCAP_writeRESERVED(std::ofstream &fout, int cnt, int payload, Endianness e)
Generate the encoding for Reserved instructions and write them to file ofstream.
void XCAP_writeType2(std::ofstream &fout, int wordCount, Endianness e)
Generate and write only a type 2 FDRI command.
uint32_t XCAP_getInstructionType(uint32_t instruction)
Parses and returns instruction type. Valid Xil instructions will be of types 1 and 2.
void writeXCAPcommandName(std::ofstream &fout, XCAP::Command commandID)
void writeXCAPregisterName(std::ofstream &fout, XCAP::Register registerID)
void XCAP_writeNOP(std::ofstream &fout, int cnt, int payload, Endianness e)
Generate the encoding for NOP instructions and write them to file ofstream.
void XCAP_writeReadRegister(std::ofstream &fout, XCAP::Register reg, int readLength, Endianness e)
Generate the encoding for reading a CAP register and write it to file ofstream.
XCAP::Register getXCAPregister(std::string s)
uint32_t XCAP_getFAR(int slr, int blockType, int globalRowAddress, int columnAddress, int minorAddress)
void XCAP_parseFAR(int farValue, int slr, int &blockType, int &globalRowAddress, int &columnAddress, int &minorAddress)
void XCAP_IncrementFAR(int slrID, int &blockType, int &globalRowAddress, int &columnAddress, int &minorAddress)
void outputBITheader(std::ofstream &fout, Endianness e)
void outputCAPheaderConstant(std::ofstream &fout, Endianness e)
void outputBITheaderLengthField(std::ofstream &fout, Endianness e)
std::string to_string(Endianness e)
void write8(std::ofstream &fout, uint8_t writeValue, Endianness e=Endianness::NATIVE)
uint32_t read32(std::ifstream &fin, Endianness e=Endianness::NATIVE)
void write32(std::ofstream &fout, uint32_t writeValue, Endianness e=Endianness::NATIVE)
bool stringEndsWith(std::string checkedString)
Returns false. End of recursion for template.
bool stringContains(std::string checkedString)
Returns false. End of recursion for template.
std::string lastStringWord(std::string s)
Parses a string s, removes all integers and returns the last of all string words.
bool arrayOfUints(std::string s, int arrsize, uint32_t *arr)
Removes all string words from a given string s and returns the parsed arrsize number of integers into...
bool multipleUints(std::stringstream &ss)
bool multipleInts(std::stringstream &ss)
std::string replace(std::string str, char oldChar, char newChar)
Replaces all instances of oldChar in string str with newChar and returns the resulting string.