00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <boost/test/unit_test.hpp>
00020 #include "torc/physical/Factory.hpp"
00021 #include "torc/physical/XdlExporter.hpp"
00022 #include <iostream>
00023 #include <sstream>
00024 #include <fstream>
00025 #include "torc/generic/om/ObjectFactory.hpp"
00026 #include "torc/generic/om/ScalarNet.hpp"
00027 #include "torc/generic/parser/Linker.hpp"
00028 #include "torc/generic/parser/ParserOptions.hpp"
00029 #include "torc/generic/parser/EdifParser.hpp"
00030 #include "torc/generic/util/MessageTable.hpp"
00031 #include <boost/regex.hpp>
00032 #include <boost/filesystem.hpp>
00033
00034
00035
00036
00037
00038
00039 template <typename T> class VisitorRecursor {
00040 public:
00041 VisitorRecursor(T& inVisitor) : mVisitor(inVisitor) {}
00042 template <typename U> void operator() (const boost::shared_ptr<U>& inVisited)
00043 const throw(torc::generic::Error) {
00044 try {
00045 inVisited->accept(mVisitor);
00046 }
00047 catch(torc::generic::Error& e) {
00048 e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
00049 throw;
00050 }
00051 }
00052 protected:
00053 mutable T& mVisitor;
00054 };
00055
00056 std::string getPropertyString(const std::string& inPropertyName,
00057 torc::generic::PropertyContainer& inPropertyContainer);
00058 std::string getPropertyString(const std::string& inPropertyName,
00059 torc::generic::PropertyContainer& inPropertyContainer) {
00060 const boost::shared_ptr<torc::generic::Property> propertyPtr
00061 = inPropertyContainer.getProperty(inPropertyName);
00062 return propertyPtr->getValue().get<std::string>();
00063 }
00064
00065
00066 class A {};
00067 class Mapper :
00068 public torc::generic::Library::Visitor,
00069 public torc::generic::Cell::Visitor,
00070 public torc::generic::View::Visitor,
00071
00072
00073
00074
00075
00076
00077 public torc::generic::SingleInstance::Visitor,
00078 public torc::generic::ScalarNet::Visitor,
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 public A
00091 {
00092 public:
00093 void visit(torc::generic::Root& inRoot) throw(torc::generic::Error) {
00094 std::cout << "visited the root" << std::endl;
00095 VisitorRecursor<Mapper> recursor(*this);
00096 inRoot.applyOnAllLibraries(recursor);
00097 }
00098 void visit(torc::generic::Library& inLibrary) throw(torc::generic::Error) {
00099 std::cout << "visited library " << inLibrary.getName() << std::endl;
00100 VisitorRecursor<Mapper> recursor(*this);
00101 inLibrary.applyOnAllCells(recursor);
00102 }
00103 void visit(torc::generic::Cell& inCell) throw(torc::generic::Error) {
00104 std::cout << " visited cell " << inCell.getName() << std::endl;
00105 mDesignPtr = torc::physical::Factory::newDesignPtr(inCell.getName(), "experimental",
00106 "package", "speed", "v3.2");
00107 VisitorRecursor<Mapper> recursor(*this);
00108 inCell.applyOnAllViews(recursor);
00109 }
00110 void visit(torc::generic::View& inView) throw(torc::generic::Error) {
00111 std::cout << " visited view " << inView.getName() << std::endl;
00112 VisitorRecursor<Mapper> recursor(*this);
00113 inView.applyOnAllInstances(recursor);
00114 inView.applyOnAllNets(recursor);
00115 }
00116 void visit(torc::generic::SingleInstance& inInstance) throw(torc::generic::Error) {
00117 std::cout << " visited instance " << inInstance.getName() << std::endl;
00118 const boost::shared_ptr<torc::generic::View> masterViewPtr = inInstance.getMaster();
00119 const boost::shared_ptr<torc::generic::Cell> masterCellPtr = masterViewPtr->getParent();
00120 std::string masterName = masterCellPtr->getName();
00121 ELogicType masterType = mLogicTypeMap[masterName];
00122 std::string type;
00123 std::string setting;
00124 std::string mask;
00125 switch(masterType) {
00126 case eLogicTypeGnd:
00127 type = "LUT";
00128 setting = "MASK";
00129 mask = "0000";
00130 break;
00131 case eLogicTypeVcc:
00132 type = "LUT";
00133 setting = "MASK";
00134 mask = "FFFF";
00135 break;
00136 case eLogicTypeLut1:
00137 case eLogicTypeLut2:
00138 case eLogicTypeLut3:
00139 case eLogicTypeLut4:
00140 type = "LUT";
00141 setting = "MASK";
00142 mask = getPropertyString("INIT", inInstance);
00143 if(mask.length() >= 2 && mask[0] == '"' && mask[mask.length() - 1] == '"') {
00144 mask.erase(mask.end() - 1, mask.end());
00145 mask.erase(mask.begin(), mask.begin() + 1);
00146 }
00147 switch(masterType) {
00148 case eLogicTypeLut1: mask = (mask[0] & 1) ? "F" : "0";
00149 case eLogicTypeLut2: mask += mask;
00150 case eLogicTypeLut3: mask += mask;
00151 default: break;
00152 }
00153 break;
00154 case eLogicTypeFF:
00155 type = "FF";
00156 setting = "INIT";
00157 mask = getPropertyString("INIT", inInstance);
00158 if(mask.length() >= 2 && mask[0] == '"' && mask[mask.length() - 1] == '"') {
00159 mask.erase(mask.end() - 1, mask.end());
00160 mask.erase(mask.begin(), mask.begin() + 1);
00161 }
00162 break;
00163 case eLogicTypeUnknown:
00164 type = "";
00165 setting = "";
00166 mask = "";
00167 std::cerr << "Unknown logic type " << masterName << std::endl;
00168 break;
00169 }
00170 mCurrentInstancePtr = torc::physical::Factory::newInstancePtr(inInstance.getName(),
00171 type, "", "");
00172 if(mask.length() != 0) mCurrentInstancePtr->setConfig(setting, "", mask);
00173 mDesignPtr->addInstance(mCurrentInstancePtr);
00174
00175
00176 }
00177 void visit(torc::generic::ScalarNet& inNet) throw(torc::generic::Error) {
00178 std::cout << " visited net " << inNet.getName() << std::endl;
00179 mCurrentNetPtr = torc::physical::Factory::newNetPtr(inNet.getName(),
00180 torc::physical::eNetTypeNormal);
00181 mDesignPtr->addNet(mCurrentNetPtr);
00182
00183
00184 }
00185
00186 Mapper(void) {
00187 mLogicTypeMap["GND"] = eLogicTypeGnd;
00188 mLogicTypeMap["VCC"] = eLogicTypeVcc;
00189 mLogicTypeMap["FDRE"] = eLogicTypeFF;
00190 mLogicTypeMap["LUT1"] = eLogicTypeLut1;
00191 mLogicTypeMap["LUT2"] = eLogicTypeLut2;
00192 mLogicTypeMap["LUT3"] = eLogicTypeLut3;
00193 mLogicTypeMap["LUT4"] = eLogicTypeLut4;
00194 }
00195 ~Mapper() throw() {}
00196 torc::physical::DesignSharedPtr& getDesignPtr(void) { return mDesignPtr; }
00197 protected:
00198 enum ELogicType { eLogicTypeUnknown, eLogicTypeGnd, eLogicTypeVcc, eLogicTypeFF,
00199 eLogicTypeLut1, eLogicTypeLut2, eLogicTypeLut3, eLogicTypeLut4 };
00200 torc::physical::DesignSharedPtr mDesignPtr;
00201 torc::physical::InstanceSharedPtr mCurrentInstancePtr;
00202 torc::physical::NetSharedPtr mCurrentNetPtr;
00203 std::map<std::string, ELogicType> mLogicTypeMap;
00204 };
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 namespace torc {
00557 namespace generic {
00558
00559 BOOST_AUTO_TEST_SUITE(generic)
00560
00561
00562 BOOST_AUTO_TEST_CASE(generic_collatz_mapper) {
00563
00564
00565 std::stringstream generated;
00566 std::stringstream parsed;
00567
00568 boost::filesystem::path collatzEdif = "collatz1.edf";
00569 try {
00570
00571 std::cout << "Parsing " << collatzEdif << " ... ";
00572 boost::shared_ptr<ObjectFactory> factoryPtr(new ObjectFactory());
00573 boost::shared_ptr<Root> rootPtr;
00574 factoryPtr->create(rootPtr);
00575 boost::shared_ptr<Linker> linkerPtr(new Linker(rootPtr));
00576 ParserOptions options;
00577 EdifParser parser;
00578 parser.parse(collatzEdif.string(), rootPtr, linkerPtr, factoryPtr, options);
00579 std::cout << "done." << std::endl;
00580
00581
00582 std::cout << "Mapping ... " << std::endl;
00583 boost::shared_ptr<Library> workLibraryPtr = rootPtr->findLibrary("work");
00584 Mapper mapper;
00585 workLibraryPtr->accept(mapper);
00586 torc::physical::DesignSharedPtr designPtr = mapper.getDesignPtr();
00587
00588
00589 std::string outFileName = "collatz1.xdl";
00590 std::fstream xdlExport(outFileName.c_str(), std::ios_base::out);
00591 torc::physical::XdlExporter fileExporter(xdlExport);
00592 fileExporter(designPtr);
00593 } catch(Error& e) {
00594 std::cerr << MessageTable::instance()->getMessage(e.getErrorMessageId()) << std::endl;
00595 const std::vector<Error::StackFrameInfo> &stack = e.getStackTrace();
00596 for(std::vector<Error::StackFrameInfo>::const_iterator it = stack.begin();
00597 it != stack.end(); it++) {
00598 std::cerr << " " << (*it).getFunction() << "() [" << (*it).getFile() << ":"
00599 << (*it).getLine() << "]" << std::endl;
00600 }
00601 }
00602
00603
00604 BOOST_CHECK_EQUAL(generated.str(), parsed.str());
00605
00606 }
00607
00608 BOOST_AUTO_TEST_SUITE_END()
00609
00610 }
00611 }