byteman  1.3 (Build #225)
Bitstream relocation and manipulation tool
XUSP_Output.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2022 Kristiyan Manev (University of Manchester)
3  *
4  * Licensed under the Apache License, Version 2.0(the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #include<iostream>
18 #include<string>
19 #include<stdexcept>
20 #include<fstream>
21 #include<cstring>
22 
23 #include "XilinxUltraScalePlus.h"
24 #include "../../../Common/FileIO.h"
25 #include "../../../Common/str.h"
26 
27 using namespace std;
28 
29 void XilinxUltraScalePlus::outputBitstreamGlobalHeaderSequence(ofstream& fout, bool fullBitstream, Endianness e)
30 {
31 
32 }
33 void XilinxUltraScalePlus::outputBitstreamGlobalFooterSequence(ofstream& fout, bool fullBitstream, Endianness e)
34 {
35  XCAP_writeNOP(fout, 400, 0, loadedBitstreamEndianness);
36 }
37 void XilinxUltraScalePlus::outputBitstreamSLRHeaderBitstreamSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
38 {
39  XCAP_writeNOP(fout, 132, 0, e);
40  //Optional Shutdown + 1 NOP ?
42  XCAP_writeNOP(fout, 2, 0, e);
43  XCAP_writeRegister(fout, XCAP::Register::IDCODE, SLRinfo[slr].IDCODE, e);
45  uint32_t ctrl0 = XCAP_getCTRL0value(0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0); //disable fallback bitstream, enable overwriting of LUTs
46  XCAP_writeMaskAndRegister(fout, XCAP::Register::CTRL0, ctrl0, ctrl0, e);
48 }
49 void XilinxUltraScalePlus::outputBitstreamSLRFooterBitstreamSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
50 {
51  if(fullBitstream) // Add GRESTORE since it is full device bitstream.
52  {
54  XCAP_writeNOP(fout, 1, 0, e);
55  }
56 
57  XCAP_writeNOP(fout, 1, 0, e);
58  uint32_t ctrl0 = XCAP_getCTRL0value(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0);
59  XCAP_writeMaskAndRegister(fout, XCAP::Register::CTRL0, ctrl0, ctrl0, e);
60 
62  XCAP_writeNOP(fout, 20, 0, e);
63 
65  XCAP_writeNOP(fout, 1, 0, e);
66 
68  XCAP_writeNOP(fout, 1, 0, e);
70  XCAP_writeNOP(fout, 1, 0, e);
72  XCAP_writeNOP(fout, 16, 0, e);
73 }
74 void XilinxUltraScalePlus::outputBitstreamSLRHeaderAfterBitstreamSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
75 {
76  if((slr + 1) < numberOfSLRs) { // If there are more SLRs, output the magic
77  XCAP_writeNOP(fout, 400, 0, e);
78  XCAP_writeSYNQ(fout, e);
79  XCAP_writeNOP(fout, 1, 0, e);
81  XCAP_writeNOP(fout, 2, 0, e);
82 
84  slrMagicInstrLocation[slr] = fout.tellp();
85  XCAP_writeType2(fout, 0, e);
86  }
87 }
88 void XilinxUltraScalePlus::outputBitstreamSLRWrapUpSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
89 {
90  streamoff currentStreamHead = fout.tellp();
91  streamoff bypassSize = (currentStreamHead - slrMagicInstrLocation[slr])/4 - 1;
92  fout.seekp(slrMagicInstrLocation[slr]);
93  FileIO::write32(fout, static_cast<uint32_t>(bypassSize), e);
94  fout.seekp(currentStreamHead);
95  XCAP_writeNOP(fout, 16, 0, loadedBitstreamEndianness);
96  XCAP_writeCommand(fout, XCAP::Command::START, loadedBitstreamEndianness);
97  XCAP_writeNOP(fout, 1, 0, loadedBitstreamEndianness);
98  XCAP_writeCommand(fout, XCAP::Command::DESYNC, loadedBitstreamEndianness);
99  XCAP_writeNOP(fout, 16, 0, loadedBitstreamEndianness);
100 }
101 void XilinxUltraScalePlus::outputBitstreamEmptySLRHeaderSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
102 {
103  XCAP_writeNOP(fout, 132, 0, e);
104  //Optional Shutdown + 1 NOP ?
106  XCAP_writeNOP(fout, 2, 0, e);
107  XCAP_writeRegister(fout, XCAP::Register::IDCODE, SLRinfo[slr].IDCODE, e);
109 
110  if((slr + 1) < numberOfSLRs) { // If there are more SLRs, output the magic
112  slrMagicInstrLocation[slr] = fout.tellp();
113  XCAP_writeType2(fout, 0, e);
114  }
115 }
116 void XilinxUltraScalePlus::outputBitstreamEmptySLRWrapUpSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
117 {
118  streamoff currentStreamHead = fout.tellp();
119  streamoff bypassSize = (currentStreamHead - slrMagicInstrLocation[slr])/4 - 1;
120  fout.seekp(slrMagicInstrLocation[slr]);
121  FileIO::write32(fout, static_cast<uint32_t>(bypassSize), e);
122  fout.seekp(currentStreamHead);
123  XCAP_writeNOP(fout, 16, 0, loadedBitstreamEndianness);
124  XCAP_writeCommand(fout, XCAP::Command::START, loadedBitstreamEndianness);
125  XCAP_writeNOP(fout, 1, 0, loadedBitstreamEndianness);
126  XCAP_writeCommand(fout, XCAP::Command::DESYNC, loadedBitstreamEndianness);
127  XCAP_writeNOP(fout, 16, 0, loadedBitstreamEndianness);
128 }
129 
130 /**************************************************************************/
137 void XilinxUltraScalePlus::writeBitstream(string filename, string params, Rect2D cmdRect)
138 {
139  size_t dotpos = filename.rfind(".");
140  if(dotpos == string::npos)
141  throw runtime_error(string("Invalid file name: \"").append(filename).append("\"!\n"));
142  designName = filename.substr(0, dotpos);
143 
144  log("Writing Xilinx UltraScale+ bitstream to file \"" + filename + "\":");
145 
146  parseParams(params);
147 
148  if(selectedOptions.partialNotFull) {
149  if((cmdRect.position.row % XUSP_CLB_PER_CLOCK_REGION != 0) || (cmdRect.size.row % XUSP_CLB_PER_CLOCK_REGION != 0))
150  throw runtime_error("Currently only full clock region height relocations are supported (use row numbers multiple of 60).");
151  if(cmdRect.size.row <= 0 || cmdRect.size.col <= 0)
152  throw runtime_error("Invalid output size dimentions.");
153  }
154 
155  ofstream fout (filename, ofstream::binary | ofstream::trunc);
156  if(!fout.good())
157  throw runtime_error(string("Could not open file: \"").append(filename).append("\"!\n"));
158  if(str::iff::stringEndsWith(filename, ".bit"))
159  writeBitstreamBIT(fout, cmdRect);
160  else if(str::iff::stringEndsWith(filename, ".bin"))
161  writeBitstreamBIN(fout, cmdRect);
162  else
163  throw runtime_error(string("Unknown Xilinx UltraScale+ file format tried to be written.\n"));
164  fout.close();
165  log("Xilinx UltraScale+ bitstream file \"" + filename + "\" written successfully.");
166 }
Endianness
< Endianness in byteman is represented not only by big/little endian, but also by potential bit swapp...
Definition: Endianness.h:47
#define XUSP_CLB_PER_CLOCK_REGION
Definition: XUSP_Fabric.h:41
void outputBitstreamSLRFooterBitstreamSequence(std::ofstream &, int, bool, Endianness) override
Definition: XUSP_Output.cpp:49
void outputBitstreamSLRHeaderAfterBitstreamSequence(std::ofstream &, int, bool, Endianness) override
Definition: XUSP_Output.cpp:74
void outputBitstreamEmptySLRHeaderSequence(std::ofstream &, int, bool, Endianness) override
void outputBitstreamGlobalHeaderSequence(std::ofstream &, bool, Endianness) override
Written by outputBitstreamSLRHeaderAfterBitstreamSequence(), outputBitstreamEmptySLRHeaderSequence() ...
Definition: XUSP_Output.cpp:29
void writeBitstream(std::string, std::string, Rect2D)
void outputBitstreamEmptySLRWrapUpSequence(std::ofstream &, int, bool, Endianness) override
void outputBitstreamGlobalFooterSequence(std::ofstream &, bool, Endianness) override
Definition: XUSP_Output.cpp:33
void outputBitstreamSLRWrapUpSequence(std::ofstream &, int, bool, Endianness) override
Definition: XUSP_Output.cpp:88
void outputBitstreamSLRHeaderBitstreamSequence(std::ofstream &, int, bool, Endianness) override
Definition: XUSP_Output.cpp:37
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.
Definition: inlineCAP.h:382
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.
Definition: inlineCAP.h:390
void XCAP_writeSYNQ(std::ofstream &fout, Endianness e)
Generate and write an SYNQ command.
Definition: inlineCAP.h:436
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.
Definition: inlineCAP.h:368
void XCAP_writeType2(std::ofstream &fout, int wordCount, Endianness e)
Generate and write only a type 2 FDRI command.
Definition: inlineCAP.h:410
void XCAP_writeMaskAndRegister(std::ofstream &fout, XCAP::Register reg, int writeMask, int writeValue, Endianness e)
Generate the encoding for writing a CAP register with a mask and write it to file ofstream.
Definition: inlineCAP.h:396
void XCAP_writeNOP(std::ofstream &fout, int cnt, int payload, Endianness e)
Generate the encoding for NOP instructions and write them to file ofstream.
Definition: inlineCAP.h:352
uint32_t XCAP_getCTRL0value(int EFUSE_KEY, int ICAP_SELECT, int Reserved_29_13, int OverTempShutDown, int Reserved_11, int ConfigFallback, int Reserved_9, int GLUTMASK_B, int Reserved_7, int DEC, int SBITS, int PERSIST, int Reserved_2_1, int GTS_USR_B)
Generate CTRL0 register write value.
Definition: inlineCAP.h:513
uint32_t XCAP_getFarFarInstruction()
Generate and return the encoding for a Far FAR instruction.
Definition: inlineFAR.h:194
void writeBitstreamBIT(std::ofstream &fout, Rect2D cmdRect)
Definition: inlineOutput.h:189
void writeBitstreamBIN(std::ofstream &fout, Rect2D cmdRect)
Definition: inlineOutput.h:207
void parseParams(std::string params)
void write32(std::ofstream &fout, uint32_t writeValue, Endianness e=Endianness::NATIVE)
Definition: FileIO.h:419
bool stringEndsWith(std::string checkedString)
Returns false. End of recursion for template.
Definition: iff.h:32
int col
Definition: Coords.h:25
int row
Definition: Coords.h:24
Definition: Coords.h:29
Coord2D size
Definition: Coords.h:31
Coord2D position
Definition: Coords.h:30