torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tiles.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Source for the Tiles class.
18 
20 #include <iostream>
21 
22 namespace torc {
23 namespace architecture {
24 
25  size_t Tiles::readTileTypes(DigestStream& inStream) {
26  // prepare to read from the stream
27  size_t bytesReadOffset = inStream.getBytesRead();
28  char scratch[1 << 10]; // scratch read buffer
29  uint16_t nameLength = 0; // length of tile type name
30 
31  // read the section header
32  string sectionName;
33  inStream.readSectionHeader(sectionName);
34  /// \todo Throw a proper exception.
35  if(sectionName != ">>>>TileType>>>>") throw -1;
36 
37  // initialize the tile type array
38  inStream.read(mTileTypeCount);
40  mOut() << "\tReading " << mTileTypeCount << " tile types..." << std::endl;
41  // loop through each tile type
42  for(TileTypeCount i; i < mTileTypeCount; i++) {
43  // read the tile type name
44  inStream.read(nameLength);
45  /// \todo Throw a proper exception.
46  if(nameLength > sizeof(scratch)) throw -1;
47  inStream.read(scratch, nameLength);
48  scratch[nameLength] = 0;
49  const_cast<CharStringWrapper&>(mTileTypeNames[i]) = scratch;
50  //mErr() << "\t\t" << mTileTypeNames[i] << ": "
51  // << (void*) (const char*) mTileTypeNames[i] << std::endl;
52  }
53 
54  // return the number of bytes read
55  return inStream.getBytesRead() - bytesReadOffset;
56  }
57 
59  // prepare to read from the stream
60  size_t bytesReadOffset = inStream.getBytesRead();
61  char scratch[1 << 10]; // scratch read buffer
62  uint16_t nameLength = 0; // length of tile type name
63  TileTypeCount typeCount; // number of tile types
64  WireCount wireCount; // number of wires
65  WireCount sinkCount; // number of sinks or tied sinks
66  WireIndex sink; // wire index of sink or tied sink
67  uint16_t offset = 0; // offset into the arc usage bitmap
68 
69  // read the section header
70  string sectionName;
71  inStream.readSectionHeader(sectionName);
72  /// \todo Throw a proper exception.
73  if(sectionName != ">>>>TileNode>>>>") throw -1;
74 
75  // initialize the tile type array
76  inStream.read(typeCount);
77  /// \todo Throw a proper exception.
78  if(typeCount != mTileTypeCount) throw -1;
79  mWires.setSize(mTileTypeCount);
81  mOut() << "\tReading wire info for " << mTileTypeCount << " tile types..."
82  << std::endl;
83  // loop through each tile type
84  for(TileTypeCount i; i < mTileTypeCount; i++) {
85  //mErr() << "\t\t" << mTileTypeNames[i] << ":" << std::endl;
86  offset = 0;
87  // read the wire count
88  inStream.read(wireCount);
89  mWires[i].setSize(wireCount);
90  mOrderedWireNames[i].setSize(wireCount);
91  // get a reference to this tile type's wire info array
92  Array<const WireInfo>& wires = mWires[i];
93  // get a reference to this tile type's wire name array
94  Array<const WireNameIndexPair>& wireNameIndexPairs = mOrderedWireNames[i];
95  // prepare to track the source and tied source counts for each wire in the tile type
96  Array<uint16_t> sourceCounts(wireCount);
97  Array<uint16_t> tiedSourceCounts(wireCount);
98  Array<uint16_t> irregularSourceCounts(wireCount);
99  Array<uint16_t> routethroughSourceCounts(wireCount);
100  for(WireCount j; j < wireCount; j++) { sourceCounts[j] = tiedSourceCounts[j]
101  = irregularSourceCounts[j] = routethroughSourceCounts[j] = 0; }
102  // loop over all of the wires
103  for(WireIndex j; j < wireCount; j++) {
104  // look up a reference for the wire info, and discard the const trait
105  WireInfo& wireInfo = const_cast<WireInfo&>(wires[j]);
106  // store the cumulative arc offset for this tile type
107  wireInfo.mArcOffset = offset;
108  // read the flags
109  inStream.read(wireInfo.mFlags);
110  // read the wire name
111  inStream.read(nameLength);
112  /// \todo Throw a proper exception.
113  if(nameLength > sizeof(scratch)) throw -1;
114  inStream.read(scratch, nameLength);
115  scratch[nameLength] = 0;
116  wireInfo.mName = strdup(scratch);
117  // also add the entry to the ordered wire names array
118  const_cast<WireNameIndexPair&>(wireNameIndexPairs[j])
119  = WireNameIndexPair(wireInfo.mName, WireIndex(j));
120  // read the tied sinks, and update the tied source count for each one
121  inStream.read(sinkCount);
122  wireInfo.mTiedSinks.setSize(sinkCount);
123  for(WireIndex k; k < sinkCount; k++) {
124  inStream.read(sink);
125  const_cast<WireIndex&>(wireInfo.mTiedSinks[k]) = sink;
126  tiedSourceCounts[sink]++;
127  }
128  // read the regular sinks, and update the source count for each one
129  inStream.read(sinkCount);
130  wireInfo.mSinks.setSize(sinkCount);
131  for(WireIndex k; k < sinkCount; k++) {
132  offset++; // increment the arc count
133  inStream.read(sink);
134  const_cast<WireIndex&>(wireInfo.mSinks[k]) = sink;
135  sourceCounts[sink]++;
136  //// take care to allow for negative numbers in remote arcs
137  //sourceCounts[sink >= 0 ? sink : -1 - sink]++;
138  }
139  // read the irregular sinks, and update the irregular source count for each one
140  inStream.read(sinkCount);
141  wireInfo.mIrregularSinks.setSize(sinkCount);
142  for(WireIndex k; k < sinkCount; k++) {
143  offset++; // increment the arc count
144  inStream.read(sink);
145  const_cast<WireIndex&>(wireInfo.mIrregularSinks[k]) = sink;
146  irregularSourceCounts[sink]++;
147  }
148  // read the routethrough sinks, and update the routethrough source count for each
149  // one
150  inStream.read(sinkCount);
151  wireInfo.mRoutethroughSinks.setSize(sinkCount);
152  for(WireIndex k; k < sinkCount; k++) {
153  offset++; // increment the arc count
154  inStream.read(sink);
155  const_cast<WireIndex&>(wireInfo.mRoutethroughSinks[k]) = sink;
156  routethroughSourceCounts[sink]++;
157  }
158  }
159 
160  // initialize the source arrays
161  for(WireIndex j; j < wireCount; j++) {
162  // look up a reference for the wire info, and discard the const trait
163  WireInfo& wireInfo = const_cast<WireInfo&>(wires[j]);
164  wireInfo.mSources.setSize(sourceCounts[j]);
165  wireInfo.mTiedSources.setSize(tiedSourceCounts[j]);
166  wireInfo.mIrregularSources.setSize(irregularSourceCounts[j]);
167  wireInfo.mRoutethroughSources.setSize(routethroughSourceCounts[j]);
168  }
169 
170  // and infer the source information from the sinks
171  for(WireIndex j; j < wireCount; j++) {
172  // look up a reference for the wire info, and discard the const trait
173  WireInfo& wireInfo = const_cast<WireInfo&>(wires[j]);
174  // handle the tied sources
175  const WireArray& tiedSinks = wireInfo.getTiedSinks();
176  sinkCount = WireIndex(tiedSinks.getSize());
177  for(WireIndex k; k < sinkCount; k++) {
178  const WireIndex tiedSink = tiedSinks[k];
179  tiedSourceCounts[tiedSink]--;
180  const_cast<WireIndex&>(wires[tiedSink]
181  .mTiedSources[tiedSourceCounts[tiedSink]]) = WireIndex(j);
182  }
183  // handle the normal sources
184  const WireArray& sinks = wireInfo.getSinks();
185  sinkCount = WireIndex(sinks.getSize());
186  for(WireIndex k; k < sinkCount; k++) {
187  const WireIndex sink = sinks[k];
188  sourceCounts[sink]--;
189  const_cast<WireIndex&>(wires[sink]
190  .mSources[sourceCounts[sink]]) = WireIndex(j);
191  }
192  // handle the irregular sources
193  const WireArray& irregularSinks = wireInfo.getIrregularSinks();
194  sinkCount = WireIndex(irregularSinks.getSize());
195  for(WireIndex k; k < sinkCount; k++) {
196  const WireIndex irregularSink = irregularSinks[k];
197  irregularSourceCounts[irregularSink]--;
198  const_cast<WireIndex&>(wires[irregularSink]
199  .mIrregularSources[irregularSourceCounts[irregularSink]]) = WireIndex(j);
200  }
201  // handle the routethrough sources
202  const WireArray& routethroughSinks = wireInfo.getRoutethroughSinks();
203  sinkCount = WireIndex(routethroughSinks.getSize());
204  for(WireIndex k; k < sinkCount; k++) {
205  const WireIndex routethroughSink = routethroughSinks[k];
206  routethroughSourceCounts[routethroughSink]--;
207  const_cast<WireIndex&>(wires[routethroughSink]
208  .mRoutethroughSources[routethroughSourceCounts[routethroughSink]])
209  = WireIndex(j);
210  }
211  }
212 
213  // sort the ordered wire names
214  std::sort(
215  const_cast<Array<WireNameIndexPair>::iterator>(wireNameIndexPairs.begin()),
216  const_cast<Array<WireNameIndexPair>::iterator>(wireNameIndexPairs.end()),
218  );
219 
220  }
221 
222  // return the number of bytes read
223  return inStream.getBytesRead() - bytesReadOffset;
224  }
225 
226  size_t Tiles::readTileMap(DigestStream& inStream) {
227  // prepare to read from the stream
228  size_t bytesReadOffset = inStream.getBytesRead();
229  char scratch[1 << 10]; // scratch read buffer
230  uint16_t nameLength = 0; // length of tile type name
231  TileTypeIndex type; // tile type
232  TileRow row; // tile row
233  TileCol col; // tile column
234 
235  // read the section header
236  string sectionName;
237  inStream.readSectionHeader(sectionName);
238  /// \todo Throw a proper exception.
239  if(sectionName != ">>>>TileMap >>>>") throw -1;
240 
241  // read the characteristic dimensions
242  inStream.read(mTileCount);
243  inStream.read(mRowCount);
244  inStream.read(mColCount);
245 
246  // initialize the tile map array
247  mTiles.setSize(mTileCount);
249  mTileMap = new TileIndex*[static_cast<TileRow::pod>(mRowCount)];
250  mTileMap[0] = new TileIndex[static_cast<TileCount::pod>(mTileCount)];
251  for(TileRow i(1); i < mRowCount; i++) mTileMap[i] = mTileMap[i-1] + mColCount;
252  mOut() << "\tReading tile map for " << mTileCount << " tiles (" << mRowCount
253  << " rows x " << mColCount << " columns)..." << std::endl;
254  // loop through each tile
255  for(TileIndex i; i < mTileCount; i++) {
256  // read the info for each tile
257  inStream.read(type);
258  inStream.read(row);
259  inStream.read(col);
260  inStream.read(nameLength);
261  /// \todo Throw a proper exception.
262  if(nameLength > sizeof(scratch)) throw -1;
263  inStream.read(scratch, nameLength);
264  scratch[nameLength] = 0;
265  // and store the info
266  const_cast<TileInfo&>(mTiles[i]).set(type, row, col, scratch);
267  mTileMap[row][col] = i; // point back to the tile index
268  //mErr() << "\t\t" << i << ": [" << row << "," << col << "]: \"" << scratch
269  // << "\" (" << mTileTypeNames[type] << ")" << std::endl;
270  // also add the entry to the ordered tile names array
271  const_cast<TileNameIndexPair&>(mOrderedTileNames[i])
272  = TileNameIndexPair(mTiles[i].getName(), i);
273  }
274 
275  // sort the ordered tile names
279  );
280 
281  // return the number of bytes read
282  return inStream.getBytesRead() - bytesReadOffset;
283  }
284 
285 } // namespace architecture
286 } // namespace torc
WireArray mTiedSinks
The tied sink array for this wire.
Definition: WireInfo.hpp:54
TileRow mRowCount
The tile row count for this device.
Definition: Tiles.hpp:86
Encapsulation of a tile index in an unsigned 32-bit integer.
Encapsulation of a tile row in an unsigned 16-bit integer.
WireArray mRoutethroughSinks
The routethrough sink array for this wire.
Definition: WireInfo.hpp:66
std::istream & read(uint8_t &outValue)
Read and return a uint8_t.
WireArray mSinks
The sink array for this wire.
Definition: WireInfo.hpp:58
Encapsulation of a tile column in an unsigned 16-bit integer.
std::pair< std::string, xilinx::WireIndex > WireNameIndexPair
Wrapper for a wire name with corresponding wire index.
Definition: Tiles.hpp:56
WireArray mIrregularSinks
The irregular sink array for this wire.
Definition: WireInfo.hpp:62
Array of wire indexes.
Definition: WireInfo.hpp:31
static bool CompareWirePairByName(const WireNameIndexPair &inA, const WireNameIndexPair &inB)
Compare wire pairs by name, for ordering purposes.
Definition: Tiles.hpp:117
T * end(void)
Returns the non-constant end iterator.
Definition: Array.hpp:97
Array< const CharStringWrapper > mTileTypeNames
The tile type names for this family.
Definition: Tiles.hpp:98
const WireArray & getIrregularSinks(void) const
Returns the irregular sink array for this wire.
Definition: WireInfo.hpp:119
TileCol mColCount
The tile column count for this device.
Definition: Tiles.hpp:88
WireArray mIrregularSources
The irregular source array for this wire.
Definition: WireInfo.hpp:64
WireArray mSources
The source array for this wire.
Definition: WireInfo.hpp:60
size_t readTileWireInfo(DigestStream &inStream)
Read the family wire info.
Definition: Tiles.cpp:58
size_t readTileTypes(DigestStream &inStream)
Read the family tile types.
Definition: Tiles.cpp:25
TileTypeCount mTileTypeCount
The number of tile types for this family.
Definition: Tiles.hpp:96
Encapsulation of a wire index in an unsigned 16-bit integer.
Wrapper around char* for use with the Array template.
Definition: Tiles.hpp:59
uint16_t mArcOffset
This wire's offset into the arc usage bitset.
Definition: WireInfo.hpp:48
WireFlags mFlags
The attribute flags for this wire.
Definition: WireInfo.hpp:50
Array< const TileInfo > mTiles
The tile information for this device.
Definition: Tiles.hpp:92
WireArray mTiedSources
The tied source array for this wire.
Definition: WireInfo.hpp:56
Encapsulation of a wire count in an unsigned 16-bit integer.
const WireArray & getSinks(void) const
Returns the sink array for this wire.
Definition: WireInfo.hpp:115
Encapsulation of a tile within a device tile map.
Definition: TileInfo.hpp:33
Array2D< const WireNameIndexPair > mOrderedWireNames
The wire name to wire index mapping for each tile type for this family.
Definition: Tiles.hpp:102
TileCount mTileCount
The tile count for this device.
Definition: Tiles.hpp:84
const WireArray & getRoutethroughSinks(void) const
Returns the routethrough sink array for this wire.
Definition: WireInfo.hpp:123
void readSectionHeader(string &outHeader)
Read and return a section header.
ostream & mOut(void)
Returns the database console output stream.
TileIndex ** mTileMap
The two-dimensional tile map for this device.
Definition: Tiles.hpp:90
static bool CompareTilePairByName(const TileNameIndexPair &inA, const TileNameIndexPair &inB)
Compare tile pairs by name, for ordering purposes.
Definition: Tiles.hpp:112
Encapsulation of a tile type count in an unsigned 16-bit integer.
const char * mName
The name for this wire.
Definition: WireInfo.hpp:52
Encapsulation of a tile type index in an unsigned 16-bit integer.
T * begin(void)
Returns the non-constant begin iterator.
Definition: Array.hpp:95
T * iterator
Non-constant T iterator type.
Definition: Array.hpp:85
const WireArray & getTiedSinks(void) const
Returns the tied sink array for this wire.
Definition: WireInfo.hpp:111
void setSize(uint32_t inSize)
Discards all contents and resizes the array.
Definition: Array.hpp:107
Encapsulation of a wire within a tile type.
Definition: WireInfo.hpp:36
Array2D< const WireInfo > mWires
The wire information for this family.
Definition: Tiles.hpp:100
std::pair< std::string, xilinx::TileIndex > TileNameIndexPair
Wrapper for a tile name with corresponding tile index.
Definition: Tiles.hpp:54
Encapsulation of a device or family digest stream.
Header for the Tiles class.
size_t readTileMap(DigestStream &inStream)
Read the device tile map.
Definition: Tiles.cpp:226
size_t getBytesRead(void) const
Returns the number of bytes read.
boost::uint16_t uint16_t
Imported type name.
Definition: Tiles.hpp:43
WireArray mRoutethroughSources
The routethrough source array for this wire.
Definition: WireInfo.hpp:68
xilinx::WireIndex WireIndex
Imported type name.
Definition: Tiles.hpp:52
Array< const TileNameIndexPair > mOrderedTileNames
The tile name to tile index mapping for this device.
Definition: Tiles.hpp:94
Encapsulation of a static array.
Definition: Array.hpp:39
uint32_t getSize(void) const
Returns the array size.
Definition: Array.hpp:104
boost::uint16_t pod
Alias for the encapsulated Plain-Old-Data type.