byteman  1.3 (Build #225)
Bitstream relocation and manipulation tool
XS7_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 "XilinxSeries7.h"
24 #include "../../../Common/FileIO.h"
25 #include "../../../Common/str.h"
26 
27 using namespace std;
28 
29 void XilinxSeries7::outputBitstreamGlobalHeaderSequence(ofstream& fout, bool fullBitstream, Endianness e)
30 {
31 
32 }
33 
34 void XilinxSeries7::outputBitstreamGlobalFooterSequence(ofstream& fout, bool fullBitstream, Endianness e)
35 {
36  XCAP_writeNOP(fout, 100, 0, loadedBitstreamEndianness);
37 }
38 
39 void XilinxSeries7::outputBitstreamSLRHeaderBitstreamSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
40 {
41  XCAP_writeNOP(fout, 132, 0, e);
42  //Optional Shutdown + 1 NOP ?
44  XCAP_writeNOP(fout, 2, 0, e);
45  XCAP_writeRegister(fout, XCAP::Register::IDCODE, SLRinfo[slr].IDCODE, e);
47  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
48  XCAP_writeMaskAndRegister(fout, XCAP::Register::CTRL0, ctrl0, ctrl0, e);
50 }
51 
52 void XilinxSeries7::outputBitstreamSLRFooterBitstreamSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
53 {
54  if(fullBitstream) // Add GRESTORE since it is full device bitstream.
55  {
57  XCAP_writeNOP(fout, 1, 0, e);
58  }
59 
60  XCAP_writeNOP(fout, 1, 0, e);
61  uint32_t ctrl0 = XCAP_getCTRL0value(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0);
62  XCAP_writeMaskAndRegister(fout, XCAP::Register::CTRL0, ctrl0, ctrl0, e);
63 
65  XCAP_writeNOP(fout, 100, 0, e);
66 
68  XCAP_writeNOP(fout, 1, 0, e);
69 
71  XCAP_writeNOP(fout, 1, 0, e);
73  XCAP_writeNOP(fout, 1, 0, e);
75  XCAP_writeNOP(fout, 16, 0, e);
76 }
77 
78 void XilinxSeries7::outputBitstreamSLRHeaderAfterBitstreamSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
79 {
80  if((slr + 1) < numberOfSLRs) { // If there are more SLRs, output the magic
81  XCAP_writeNOP(fout, 400, 0, e);
82  XCAP_writeSYNQ(fout, e);
83  XCAP_writeNOP(fout, 1, 0, e);
85  XCAP_writeNOP(fout, 2, 0, e);
86 
88  slrMagicInstrLocation[slr] = fout.tellp();
89  XCAP_writeType2(fout, 0, e);
90  }
91 }
92 
93 void XilinxSeries7::outputBitstreamSLRWrapUpSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
94 {
95  streamoff currentStreamHead = fout.tellp();
96  streamoff bypassSize = (currentStreamHead - slrMagicInstrLocation[slr])/4 - 1;
97  fout.seekp(slrMagicInstrLocation[slr]);
98  FileIO::write32(fout, static_cast<uint32_t>(bypassSize), e);
99  fout.seekp(currentStreamHead);
100  XCAP_writeNOP(fout, 16, 0, loadedBitstreamEndianness);
101  XCAP_writeCommand(fout, XCAP::Command::START, loadedBitstreamEndianness);
102  XCAP_writeNOP(fout, 1, 0, loadedBitstreamEndianness);
103  XCAP_writeCommand(fout, XCAP::Command::DESYNC, loadedBitstreamEndianness);
104  XCAP_writeNOP(fout, 16, 0, loadedBitstreamEndianness);
105 }
106 
107 void XilinxSeries7::outputBitstreamEmptySLRHeaderSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
108 {
109  XCAP_writeNOP(fout, 132, 0, e);
110  //Optional Shutdown + 1 NOP ?
112  XCAP_writeNOP(fout, 2, 0, e);
113  XCAP_writeRegister(fout, XCAP::Register::IDCODE, SLRinfo[slr].IDCODE, e);
115 
116  if((slr + 1) < numberOfSLRs) { // If there are more SLRs, output the magic
118  slrMagicInstrLocation[slr] = fout.tellp();
119  XCAP_writeType2(fout, 0, e);
120  }
121 }
122 
123 void XilinxSeries7::outputBitstreamEmptySLRWrapUpSequence(ofstream& fout, int slr, bool fullBitstream, Endianness e)
124 {
125  streamoff currentStreamHead = fout.tellp();
126  streamoff bypassSize = (currentStreamHead - slrMagicInstrLocation[slr])/4 - 1;
127  fout.seekp(slrMagicInstrLocation[slr]);
128  FileIO::write32(fout, static_cast<uint32_t>(bypassSize), e);
129  fout.seekp(currentStreamHead);
130  XCAP_writeNOP(fout, 16, 0, loadedBitstreamEndianness);
131  XCAP_writeCommand(fout, XCAP::Command::START, loadedBitstreamEndianness);
132  XCAP_writeNOP(fout, 1, 0, loadedBitstreamEndianness);
133  XCAP_writeCommand(fout, XCAP::Command::DESYNC, loadedBitstreamEndianness);
134  XCAP_writeNOP(fout, 16, 0, loadedBitstreamEndianness);
135 }
136 
137 /**************************************************************************/
144 void XilinxSeries7::writeBitstream(string filename, string params, Rect2D cmdRect)
145 {
146  size_t dotpos = filename.rfind(".");
147  if(dotpos == string::npos)
148  throw runtime_error(string("Invalid file name: \"").append(filename).append("\"!\n"));
149  designName = filename.substr(0, dotpos);
150 
151  log("Writing Xilinx Series 7 bitstream to file \"" + filename + "\":");
152 
153  parseParams(params);
154 
155  if(selectedOptions.partialNotFull) {
156  if((cmdRect.position.row % XS7_CLB_PER_CLOCK_REGION != 0) || (cmdRect.size.row % XS7_CLB_PER_CLOCK_REGION != 0))
157  throw runtime_error("Currently only full clock region height relocations are supported (use row numbers multiple of 60).");
158  if(cmdRect.size.row <= 0 || cmdRect.size.col <= 0)
159  throw runtime_error("Invalid output size dimentions.");
160  }
161 
162  ofstream fout (filename, ofstream::binary | ofstream::trunc);
163  if(!fout.good())
164  throw runtime_error(string("Could not open file: \"").append(filename).append("\"!\n"));
165  if(str::iff::stringEndsWith(filename, ".bit"))
166  writeBitstreamBIT(fout, cmdRect);
167  else if(str::iff::stringEndsWith(filename, ".bin"))
168  writeBitstreamBIN(fout, cmdRect);
169  else
170  throw runtime_error(string("Unknown Xilinx Series 7 file format tried to be written.\n"));
171  fout.close();
172  log("Xilinx Series 7 bitstream file \"" + filename + "\" written successfully.");
173 }
Endianness
< Endianness in byteman is represented not only by big/little endian, but also by potential bit swapp...
Definition: Endianness.h:47
#define XS7_CLB_PER_CLOCK_REGION
Definition: XS7_Fabric.h:43
void outputBitstreamSLRHeaderAfterBitstreamSequence(std::ofstream &, int, bool, Endianness) override
Definition: XS7_Output.cpp:78
void outputBitstreamSLRWrapUpSequence(std::ofstream &, int, bool, Endianness) override
Definition: XS7_Output.cpp:93
void outputBitstreamGlobalHeaderSequence(std::ofstream &, bool, Endianness) override
Written by outputBitstreamSLRHeaderAfterBitstreamSequence(), outputBitstreamEmptySLRHeaderSequence() ...
Definition: XS7_Output.cpp:29
void outputBitstreamSLRFooterBitstreamSequence(std::ofstream &, int, bool, Endianness) override
Definition: XS7_Output.cpp:52
void outputBitstreamEmptySLRWrapUpSequence(std::ofstream &, int, bool, Endianness) override
Definition: XS7_Output.cpp:123
void writeBitstream(std::string, std::string, Rect2D)
Definition: XS7_Output.cpp:144
void outputBitstreamEmptySLRHeaderSequence(std::ofstream &, int, bool, Endianness) override
Definition: XS7_Output.cpp:107
void outputBitstreamSLRHeaderBitstreamSequence(std::ofstream &, int, bool, Endianness) override
Definition: XS7_Output.cpp:39
void outputBitstreamGlobalFooterSequence(std::ofstream &, bool, Endianness) override
Definition: XS7_Output.cpp:34
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