00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "torc/architecture/Tiles.hpp"
00020 #include <iostream>
00021
00022 namespace torc {
00023 namespace architecture {
00024
00025 size_t Tiles::readTileTypes(DigestStream& inStream) {
00026
00027 size_t bytesReadOffset = inStream.getBytesRead();
00028 char scratch[1 << 10];
00029 uint16_t nameLength = 0;
00030
00031
00032 string sectionName;
00033 inStream.readSectionHeader(sectionName);
00034
00035 if(sectionName != ">>>>TileType>>>>") throw -1;
00036
00037
00038 inStream.read(mTileTypeCount);
00039 mTileTypeNames.setSize(mTileTypeCount);
00040 std::cout << "\tReading " << mTileTypeCount << " tile types..." << std::endl;
00041
00042 for(TileTypeCount i; i < mTileTypeCount; i++) {
00043
00044 inStream.read(nameLength);
00045
00046 if(nameLength > sizeof(scratch)) throw -1;
00047 inStream.read(scratch, nameLength);
00048 scratch[nameLength] = 0;
00049 const_cast<CharStringWrapper&>(mTileTypeNames[i]) = scratch;
00050
00051
00052 }
00053
00054
00055 return inStream.getBytesRead() - bytesReadOffset;
00056 }
00057
00058 size_t Tiles::readTileWireInfo(DigestStream& inStream) {
00059
00060 size_t bytesReadOffset = inStream.getBytesRead();
00061 char scratch[1 << 10];
00062 uint16_t nameLength = 0;
00063 TileTypeCount typeCount;
00064 WireCount wireCount;
00065 WireCount sinkCount;
00066 WireIndex sink;
00067 uint16_t offset = 0;
00068
00069
00070 string sectionName;
00071 inStream.readSectionHeader(sectionName);
00072
00073 if(sectionName != ">>>>TileNode>>>>") throw -1;
00074
00075
00076 inStream.read(typeCount);
00077
00078 if(typeCount != mTileTypeCount) throw -1;
00079 mWires.setSize(mTileTypeCount);
00080 mOrderedWireNames.setSize(mTileTypeCount);
00081 std::cout << "\tReading wire info for " << mTileTypeCount << " tile types..."
00082 << std::endl;
00083
00084 for(TileTypeCount i; i < mTileTypeCount; i++) {
00085
00086 offset = 0;
00087
00088 inStream.read(wireCount);
00089 mWires[i].setSize(wireCount);
00090 mOrderedWireNames[i].setSize(wireCount);
00091
00092 Array<const WireInfo>& wires = mWires[i];
00093
00094 Array<const WireNameIndexPair>& wireNameIndexPairs = mOrderedWireNames[i];
00095
00096 Array<uint16_t> sourceCounts(wireCount);
00097 Array<uint16_t> tiedSourceCounts(wireCount);
00098 Array<uint16_t> irregularSourceCounts(wireCount);
00099 Array<uint16_t> routethroughSourceCounts(wireCount);
00100 for(WireCount j; j < wireCount; j++) { sourceCounts[j] = tiedSourceCounts[j]
00101 = irregularSourceCounts[j] = routethroughSourceCounts[j] = 0; }
00102
00103 for(WireIndex j; j < wireCount; j++) {
00104
00105 WireInfo& wireInfo = const_cast<WireInfo&>(wires[j]);
00106
00107 wireInfo.mArcOffset = offset;
00108
00109 inStream.read(wireInfo.mFlags);
00110
00111 inStream.read(nameLength);
00112
00113 if(nameLength > sizeof(scratch)) throw -1;
00114 inStream.read(scratch, nameLength);
00115 scratch[nameLength] = 0;
00116 wireInfo.mName = strdup(scratch);
00117
00118 const_cast<WireNameIndexPair&>(wireNameIndexPairs[j])
00119 = WireNameIndexPair(wireInfo.mName, WireIndex(j));
00120
00121 inStream.read(sinkCount);
00122 wireInfo.mTiedSinks.setSize(sinkCount);
00123 for(WireIndex k; k < sinkCount; k++) {
00124 inStream.read(sink);
00125 const_cast<WireIndex&>(wireInfo.mTiedSinks[k]) = sink;
00126 tiedSourceCounts[sink]++;
00127 }
00128
00129 inStream.read(sinkCount);
00130 wireInfo.mSinks.setSize(sinkCount);
00131 for(WireIndex k; k < sinkCount; k++) {
00132 offset++;
00133 inStream.read(sink);
00134 const_cast<WireIndex&>(wireInfo.mSinks[k]) = sink;
00135 sourceCounts[sink]++;
00136
00137
00138 }
00139
00140 inStream.read(sinkCount);
00141 wireInfo.mIrregularSinks.setSize(sinkCount);
00142 for(WireIndex k; k < sinkCount; k++) {
00143 offset++;
00144 inStream.read(sink);
00145 const_cast<WireIndex&>(wireInfo.mIrregularSinks[k]) = sink;
00146 irregularSourceCounts[sink]++;
00147 }
00148
00149
00150 inStream.read(sinkCount);
00151 wireInfo.mRoutethroughSinks.setSize(sinkCount);
00152 for(WireIndex k; k < sinkCount; k++) {
00153 offset++;
00154 inStream.read(sink);
00155 const_cast<WireIndex&>(wireInfo.mRoutethroughSinks[k]) = sink;
00156 routethroughSourceCounts[sink]++;
00157 }
00158 }
00159
00160
00161 for(WireIndex j; j < wireCount; j++) {
00162
00163 WireInfo& wireInfo = const_cast<WireInfo&>(wires[j]);
00164 wireInfo.mSources.setSize(sourceCounts[j]);
00165 wireInfo.mTiedSources.setSize(tiedSourceCounts[j]);
00166 wireInfo.mIrregularSources.setSize(irregularSourceCounts[j]);
00167 wireInfo.mRoutethroughSources.setSize(routethroughSourceCounts[j]);
00168 }
00169
00170
00171 for(WireIndex j; j < wireCount; j++) {
00172
00173 WireInfo& wireInfo = const_cast<WireInfo&>(wires[j]);
00174
00175 const WireArray& tiedSinks = wireInfo.getTiedSinks();
00176 sinkCount = WireIndex(tiedSinks.getSize());
00177 for(WireIndex k; k < sinkCount; k++) {
00178 const WireIndex tiedSink = tiedSinks[k];
00179 tiedSourceCounts[tiedSink]--;
00180 const_cast<WireIndex&>(wires[tiedSink]
00181 .mTiedSources[tiedSourceCounts[tiedSink]]) = WireIndex(j);
00182 }
00183
00184 const WireArray& sinks = wireInfo.getSinks();
00185 sinkCount = WireIndex(sinks.getSize());
00186 for(WireIndex k; k < sinkCount; k++) {
00187 const WireIndex sink = sinks[k];
00188 sourceCounts[sink]--;
00189 const_cast<WireIndex&>(wires[sink]
00190 .mSources[sourceCounts[sink]]) = WireIndex(j);
00191 }
00192
00193 const WireArray& irregularSinks = wireInfo.getIrregularSinks();
00194 sinkCount = WireIndex(irregularSinks.getSize());
00195 for(WireIndex k; k < sinkCount; k++) {
00196 const WireIndex irregularSink = irregularSinks[k];
00197 irregularSourceCounts[irregularSink]--;
00198 const_cast<WireIndex&>(wires[irregularSink]
00199 .mIrregularSources[irregularSourceCounts[irregularSink]]) = WireIndex(j);
00200 }
00201
00202 const WireArray& routethroughSinks = wireInfo.getRoutethroughSinks();
00203 sinkCount = WireIndex(routethroughSinks.getSize());
00204 for(WireIndex k; k < sinkCount; k++) {
00205 const WireIndex routethroughSink = routethroughSinks[k];
00206 routethroughSourceCounts[routethroughSink]--;
00207 const_cast<WireIndex&>(wires[routethroughSink]
00208 .mRoutethroughSources[routethroughSourceCounts[routethroughSink]])
00209 = WireIndex(j);
00210 }
00211 }
00212
00213
00214 std::sort(
00215 const_cast<Array<WireNameIndexPair>::iterator>(wireNameIndexPairs.begin()),
00216 const_cast<Array<WireNameIndexPair>::iterator>(wireNameIndexPairs.end()),
00217 &CompareWirePairByName
00218 );
00219
00220 }
00221
00222
00223 return inStream.getBytesRead() - bytesReadOffset;
00224 }
00225
00226 size_t Tiles::readTileMap(DigestStream& inStream) {
00227
00228 size_t bytesReadOffset = inStream.getBytesRead();
00229 char scratch[1 << 10];
00230 uint16_t nameLength = 0;
00231 TileTypeIndex type;
00232 TileRow row;
00233 TileCol col;
00234
00235
00236 string sectionName;
00237 inStream.readSectionHeader(sectionName);
00238
00239 if(sectionName != ">>>>TileMap >>>>") throw -1;
00240
00241
00242 inStream.read(mTileCount);
00243 inStream.read(mRowCount);
00244 inStream.read(mColCount);
00245
00246
00247 mTiles.setSize(mTileCount);
00248 mOrderedTileNames.setSize(mTileCount);
00249 mTileMap = new TileIndex*[static_cast<TileRow::pod>(mRowCount)];
00250 mTileMap[0] = new TileIndex[static_cast<TileCount::pod>(mTileCount)];
00251 for(TileRow i(1); i < mRowCount; i++) mTileMap[i] = mTileMap[i-1] + mColCount;
00252 std::cout << "\tReading tile map for " << mTileCount << " tiles (" << mRowCount
00253 << " rows x " << mColCount << " columns)..." << std::endl;
00254
00255 for(TileIndex i; i < mTileCount; i++) {
00256
00257 inStream.read(type);
00258 inStream.read(row);
00259 inStream.read(col);
00260 inStream.read(nameLength);
00261
00262 if(nameLength > sizeof(scratch)) throw -1;
00263 inStream.read(scratch, nameLength);
00264 scratch[nameLength] = 0;
00265
00266 const_cast<TileInfo&>(mTiles[i]).set(type, row, col, scratch);
00267 mTileMap[row][col] = i;
00268
00269
00270
00271 const_cast<TileNameIndexPair&>(mOrderedTileNames[i])
00272 = TileNameIndexPair(mTiles[i].getName(), i);
00273 }
00274
00275
00276 std::sort(const_cast<Array<TileNameIndexPair>::iterator>(mOrderedTileNames.begin()),
00277 const_cast<Array<TileNameIndexPair>::iterator>(mOrderedTileNames.end()),
00278 &CompareTilePairByName
00279 );
00280
00281
00282 return inStream.getBytesRead() - bytesReadOffset;
00283 }
00284
00285 }
00286 }