PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/base/abc/abcSop.c

https://bitbucket.org/alanmi/abc/
C | 1322 lines | 697 code | 64 blank | 561 comment | 246 complexity | 22c0fa41d381fe34fb803e81e02479d1 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /**CFile****************************************************************
  2. FileName [abcSop.c]
  3. SystemName [ABC: Logic synthesis and verification system.]
  4. PackageName [Network and node package.]
  5. Synopsis [Implementation of a simple SOP representation of nodes.]
  6. Author [Alan Mishchenko]
  7. Affiliation [UC Berkeley]
  8. Date [Ver. 1.0. Started - June 20, 2005.]
  9. Revision [$Id: abcSop.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
  10. ***********************************************************************/
  11. #include "abc.h"
  12. ABC_NAMESPACE_IMPL_START
  13. /*
  14. The SOPs in this package are represented using char * strings.
  15. For example, the SOP of the node:
  16. .names c d0 d1 MUX
  17. 01- 1
  18. 1-1 1
  19. is the string: "01- 1\n1-1 1\n" where '\n' is a single char.
  20. */
  21. ////////////////////////////////////////////////////////////////////////
  22. /// DECLARATIONS ///
  23. ////////////////////////////////////////////////////////////////////////
  24. ////////////////////////////////////////////////////////////////////////
  25. /// FUNCTION DEFINITIONS ///
  26. ////////////////////////////////////////////////////////////////////////
  27. /**Function*************************************************************
  28. Synopsis [Registers the cube string with the network.]
  29. Description []
  30. SideEffects []
  31. SeeAlso []
  32. ***********************************************************************/
  33. char * Abc_SopRegister( Mem_Flex_t * pMan, const char * pName )
  34. {
  35. char * pRegName;
  36. if ( pName == NULL ) return NULL;
  37. pRegName = Mem_FlexEntryFetch( pMan, strlen(pName) + 1 );
  38. strcpy( pRegName, pName );
  39. return pRegName;
  40. }
  41. /**Function*************************************************************
  42. Synopsis [Creates the constant 1 cover with the given number of variables and cubes.]
  43. Description []
  44. SideEffects []
  45. SeeAlso []
  46. ***********************************************************************/
  47. char * Abc_SopStart( Mem_Flex_t * pMan, int nCubes, int nVars )
  48. {
  49. char * pSopCover, * pCube;
  50. int i, Length;
  51. Length = nCubes * (nVars + 3);
  52. pSopCover = Mem_FlexEntryFetch( pMan, Length + 1 );
  53. memset( pSopCover, '-', Length );
  54. pSopCover[Length] = 0;
  55. for ( i = 0; i < nCubes; i++ )
  56. {
  57. pCube = pSopCover + i * (nVars + 3);
  58. pCube[nVars + 0] = ' ';
  59. pCube[nVars + 1] = '1';
  60. pCube[nVars + 2] = '\n';
  61. }
  62. return pSopCover;
  63. }
  64. /**Function*************************************************************
  65. Synopsis [Creates the constant 1 cover with 0 variables.]
  66. Description []
  67. SideEffects []
  68. SeeAlso []
  69. ***********************************************************************/
  70. char * Abc_SopCreateConst1( Mem_Flex_t * pMan )
  71. {
  72. return Abc_SopRegister( pMan, " 1\n" );
  73. }
  74. /**Function*************************************************************
  75. Synopsis [Creates the constant 1 cover with 0 variables.]
  76. Description []
  77. SideEffects []
  78. SeeAlso []
  79. ***********************************************************************/
  80. char * Abc_SopCreateConst0( Mem_Flex_t * pMan )
  81. {
  82. return Abc_SopRegister( pMan, " 0\n" );
  83. }
  84. /**Function*************************************************************
  85. Synopsis [Creates the AND2 cover.]
  86. Description []
  87. SideEffects []
  88. SeeAlso []
  89. ***********************************************************************/
  90. char * Abc_SopCreateAnd2( Mem_Flex_t * pMan, int fCompl0, int fCompl1 )
  91. {
  92. char Buffer[6];
  93. Buffer[0] = '1' - fCompl0;
  94. Buffer[1] = '1' - fCompl1;
  95. Buffer[2] = ' ';
  96. Buffer[3] = '1';
  97. Buffer[4] = '\n';
  98. Buffer[5] = 0;
  99. return Abc_SopRegister( pMan, Buffer );
  100. }
  101. /**Function*************************************************************
  102. Synopsis [Creates the multi-input AND cover.]
  103. Description []
  104. SideEffects []
  105. SeeAlso []
  106. ***********************************************************************/
  107. char * Abc_SopCreateAnd( Mem_Flex_t * pMan, int nVars, int * pfCompl )
  108. {
  109. char * pSop;
  110. int i;
  111. pSop = Abc_SopStart( pMan, 1, nVars );
  112. for ( i = 0; i < nVars; i++ )
  113. pSop[i] = '1' - (pfCompl? pfCompl[i] : 0);
  114. pSop[nVars + 1] = '1';
  115. return pSop;
  116. }
  117. /**Function*************************************************************
  118. Synopsis [Creates the multi-input NAND cover.]
  119. Description []
  120. SideEffects []
  121. SeeAlso []
  122. ***********************************************************************/
  123. char * Abc_SopCreateNand( Mem_Flex_t * pMan, int nVars )
  124. {
  125. char * pSop;
  126. int i;
  127. pSop = Abc_SopStart( pMan, 1, nVars );
  128. for ( i = 0; i < nVars; i++ )
  129. pSop[i] = '1';
  130. pSop[nVars + 1] = '0';
  131. return pSop;
  132. }
  133. /**Function*************************************************************
  134. Synopsis [Creates the multi-input OR cover.]
  135. Description []
  136. SideEffects []
  137. SeeAlso []
  138. ***********************************************************************/
  139. char * Abc_SopCreateOr( Mem_Flex_t * pMan, int nVars, int * pfCompl )
  140. {
  141. char * pSop;
  142. int i;
  143. pSop = Abc_SopStart( pMan, 1, nVars );
  144. for ( i = 0; i < nVars; i++ )
  145. pSop[i] = '0' + (pfCompl? pfCompl[i] : 0);
  146. pSop[nVars + 1] = '0';
  147. return pSop;
  148. }
  149. /**Function*************************************************************
  150. Synopsis [Creates the multi-input OR cover.]
  151. Description []
  152. SideEffects []
  153. SeeAlso []
  154. ***********************************************************************/
  155. char * Abc_SopCreateOrMultiCube( Mem_Flex_t * pMan, int nVars, int * pfCompl )
  156. {
  157. char * pSop, * pCube;
  158. int i;
  159. pSop = Abc_SopStart( pMan, nVars, nVars );
  160. i = 0;
  161. Abc_SopForEachCube( pSop, nVars, pCube )
  162. {
  163. pCube[i] = '1' - (pfCompl? pfCompl[i] : 0);
  164. i++;
  165. }
  166. return pSop;
  167. }
  168. /**Function*************************************************************
  169. Synopsis [Creates the multi-input NOR cover.]
  170. Description []
  171. SideEffects []
  172. SeeAlso []
  173. ***********************************************************************/
  174. char * Abc_SopCreateNor( Mem_Flex_t * pMan, int nVars )
  175. {
  176. char * pSop;
  177. int i;
  178. pSop = Abc_SopStart( pMan, 1, nVars );
  179. for ( i = 0; i < nVars; i++ )
  180. pSop[i] = '0';
  181. return pSop;
  182. }
  183. /**Function*************************************************************
  184. Synopsis [Creates the multi-input XOR cover.]
  185. Description []
  186. SideEffects []
  187. SeeAlso []
  188. ***********************************************************************/
  189. char * Abc_SopCreateXor( Mem_Flex_t * pMan, int nVars )
  190. {
  191. assert( nVars == 2 );
  192. return Abc_SopRegister(pMan, "01 1\n10 1\n");
  193. }
  194. /**Function*************************************************************
  195. Synopsis [Creates the multi-input XOR cover (special case).]
  196. Description []
  197. SideEffects []
  198. SeeAlso []
  199. ***********************************************************************/
  200. char * Abc_SopCreateXorSpecial( Mem_Flex_t * pMan, int nVars )
  201. {
  202. char * pSop;
  203. pSop = Abc_SopCreateAnd( pMan, nVars, NULL );
  204. pSop[nVars+1] = 'x';
  205. assert( pSop[nVars+2] == '\n' );
  206. return pSop;
  207. }
  208. /**Function*************************************************************
  209. Synopsis [Creates the multi-input XNOR cover.]
  210. Description []
  211. SideEffects []
  212. SeeAlso []
  213. ***********************************************************************/
  214. char * Abc_SopCreateNxor( Mem_Flex_t * pMan, int nVars )
  215. {
  216. assert( nVars == 2 );
  217. return Abc_SopRegister(pMan, "11 1\n00 1\n");
  218. }
  219. /**Function*************************************************************
  220. Synopsis [Creates the MUX cover.]
  221. Description [The first input of MUX is the control. The second input
  222. is DATA1. The third input is DATA0.]
  223. SideEffects []
  224. SeeAlso []
  225. ***********************************************************************/
  226. char * Abc_SopCreateMux( Mem_Flex_t * pMan )
  227. {
  228. return Abc_SopRegister(pMan, "11- 1\n0-1 1\n");
  229. }
  230. /**Function*************************************************************
  231. Synopsis [Creates the inv cover.]
  232. Description []
  233. SideEffects []
  234. SeeAlso []
  235. ***********************************************************************/
  236. char * Abc_SopCreateInv( Mem_Flex_t * pMan )
  237. {
  238. return Abc_SopRegister(pMan, "0 1\n");
  239. }
  240. /**Function*************************************************************
  241. Synopsis [Creates the buf cover.]
  242. Description []
  243. SideEffects []
  244. SeeAlso []
  245. ***********************************************************************/
  246. char * Abc_SopCreateBuf( Mem_Flex_t * pMan )
  247. {
  248. return Abc_SopRegister(pMan, "1 1\n");
  249. }
  250. /**Function*************************************************************
  251. Synopsis [Creates the arbitrary cover from the truth table.]
  252. Description []
  253. SideEffects []
  254. SeeAlso []
  255. ***********************************************************************/
  256. char * Abc_SopCreateFromTruth( Mem_Flex_t * pMan, int nVars, unsigned * pTruth )
  257. {
  258. char * pSop, * pCube;
  259. int nMints, Counter, i, k;
  260. // count the number of true minterms
  261. Counter = 0;
  262. nMints = (1 << nVars);
  263. for ( i = 0; i < nMints; i++ )
  264. Counter += ((pTruth[i>>5] & (1 << (i&31))) > 0);
  265. // SOP is not well-defined if the truth table is constant 0
  266. assert( Counter > 0 );
  267. if ( Counter == 0 )
  268. return NULL;
  269. // start the cover
  270. pSop = Abc_SopStart( pMan, Counter, nVars );
  271. // create true minterms
  272. Counter = 0;
  273. for ( i = 0; i < nMints; i++ )
  274. if ( (pTruth[i>>5] & (1 << (i&31))) > 0 )
  275. {
  276. pCube = pSop + Counter * (nVars + 3);
  277. for ( k = 0; k < nVars; k++ )
  278. pCube[k] = '0' + ((i & (1 << k)) > 0);
  279. Counter++;
  280. }
  281. return pSop;
  282. }
  283. /**Function*************************************************************
  284. Synopsis [Creates the cover from the ISOP computed from TT.]
  285. Description []
  286. SideEffects []
  287. SeeAlso []
  288. ***********************************************************************/
  289. char * Abc_SopCreateFromIsop( Mem_Flex_t * pMan, int nVars, Vec_Int_t * vCover )
  290. {
  291. char * pSop, * pCube;
  292. int i, k, Entry, Literal;
  293. assert( Vec_IntSize(vCover) > 0 );
  294. if ( Vec_IntSize(vCover) == 0 )
  295. return NULL;
  296. // start the cover
  297. pSop = Abc_SopStart( pMan, Vec_IntSize(vCover), nVars );
  298. // create cubes
  299. Vec_IntForEachEntry( vCover, Entry, i )
  300. {
  301. pCube = pSop + i * (nVars + 3);
  302. for ( k = 0; k < nVars; k++ )
  303. {
  304. Literal = 3 & (Entry >> (k << 1));
  305. if ( Literal == 1 )
  306. pCube[k] = '0';
  307. else if ( Literal == 2 )
  308. pCube[k] = '1';
  309. else if ( Literal != 0 )
  310. assert( 0 );
  311. }
  312. }
  313. return pSop;
  314. }
  315. /**Function*************************************************************
  316. Synopsis [Creates the cover from the ISOP computed from TT.]
  317. Description []
  318. SideEffects []
  319. SeeAlso []
  320. ***********************************************************************/
  321. void Abc_SopToIsop( char * pSop, Vec_Int_t * vCover )
  322. {
  323. char * pCube;
  324. int k, nVars, Entry;
  325. nVars = Abc_SopGetVarNum( pSop );
  326. assert( nVars > 0 );
  327. // create cubes
  328. Vec_IntClear( vCover );
  329. for ( pCube = pSop; *pCube; pCube += nVars + 3 )
  330. {
  331. Entry = 0;
  332. for ( k = nVars - 1; k >= 0; k-- )
  333. if ( pCube[k] == '0' )
  334. Entry = (Entry << 2) | 1;
  335. else if ( pCube[k] == '1' )
  336. Entry = (Entry << 2) | 2;
  337. else if ( pCube[k] == '-' )
  338. Entry = (Entry << 2);
  339. else
  340. assert( 0 );
  341. Vec_IntPush( vCover, Entry );
  342. }
  343. }
  344. /**Function*************************************************************
  345. Synopsis [Reads the number of cubes in the cover.]
  346. Description []
  347. SideEffects []
  348. SeeAlso []
  349. ***********************************************************************/
  350. int Abc_SopGetCubeNum( char * pSop )
  351. {
  352. char * pCur;
  353. int nCubes = 0;
  354. if ( pSop == NULL )
  355. return 0;
  356. for ( pCur = pSop; *pCur; pCur++ )
  357. nCubes += (*pCur == '\n');
  358. return nCubes;
  359. }
  360. /**Function*************************************************************
  361. Synopsis [Reads the number of SOP literals in the cover.]
  362. Description []
  363. SideEffects []
  364. SeeAlso []
  365. ***********************************************************************/
  366. int Abc_SopGetLitNum( char * pSop )
  367. {
  368. char * pCur;
  369. int nLits = 0;
  370. if ( pSop == NULL )
  371. return 0;
  372. for ( pCur = pSop; *pCur; pCur++ )
  373. {
  374. nLits -= (*pCur == '\n');
  375. nLits += (*pCur == '0' || *pCur == '1');
  376. }
  377. return nLits;
  378. }
  379. /**Function*************************************************************
  380. Synopsis [Reads the number of variables in the cover.]
  381. Description []
  382. SideEffects []
  383. SeeAlso []
  384. ***********************************************************************/
  385. int Abc_SopGetVarNum( char * pSop )
  386. {
  387. char * pCur;
  388. for ( pCur = pSop; *pCur != '\n'; pCur++ )
  389. if ( *pCur == 0 )
  390. return -1;
  391. return pCur - pSop - 2;
  392. }
  393. /**Function*************************************************************
  394. Synopsis [Reads the phase of the cover.]
  395. Description []
  396. SideEffects []
  397. SeeAlso []
  398. ***********************************************************************/
  399. int Abc_SopGetPhase( char * pSop )
  400. {
  401. int nVars = Abc_SopGetVarNum( pSop );
  402. if ( pSop[nVars+1] == '0' || pSop[nVars+1] == 'n' )
  403. return 0;
  404. if ( pSop[nVars+1] == '1' || pSop[nVars+1] == 'x' )
  405. return 1;
  406. assert( 0 );
  407. return -1;
  408. }
  409. /**Function*************************************************************
  410. Synopsis [Returns the i-th literal of the cover.]
  411. Description []
  412. SideEffects []
  413. SeeAlso []
  414. ***********************************************************************/
  415. int Abc_SopGetIthCareLit( char * pSop, int i )
  416. {
  417. char * pCube;
  418. int nVars;
  419. nVars = Abc_SopGetVarNum( pSop );
  420. Abc_SopForEachCube( pSop, nVars, pCube )
  421. if ( pCube[i] != '-' )
  422. return pCube[i] - '0';
  423. return -1;
  424. }
  425. /**Function*************************************************************
  426. Synopsis []
  427. Description []
  428. SideEffects []
  429. SeeAlso []
  430. ***********************************************************************/
  431. void Abc_SopComplement( char * pSop )
  432. {
  433. char * pCur;
  434. for ( pCur = pSop; *pCur; pCur++ )
  435. if ( *pCur == '\n' )
  436. {
  437. if ( *(pCur - 1) == '0' )
  438. *(pCur - 1) = '1';
  439. else if ( *(pCur - 1) == '1' )
  440. *(pCur - 1) = '0';
  441. else if ( *(pCur - 1) == 'x' )
  442. *(pCur - 1) = 'n';
  443. else if ( *(pCur - 1) == 'n' )
  444. *(pCur - 1) = 'x';
  445. else
  446. assert( 0 );
  447. }
  448. }
  449. /**Function*************************************************************
  450. Synopsis []
  451. Description []
  452. SideEffects []
  453. SeeAlso []
  454. ***********************************************************************/
  455. void Abc_SopComplementVar( char * pSop, int iVar )
  456. {
  457. char * pCube;
  458. int nVars = Abc_SopGetVarNum(pSop);
  459. assert( iVar < nVars );
  460. Abc_SopForEachCube( pSop, nVars, pCube )
  461. {
  462. if ( pCube[iVar] == '0' )
  463. pCube[iVar] = '1';
  464. else if ( pCube[iVar] == '1' )
  465. pCube[iVar] = '0';
  466. }
  467. }
  468. /**Function*************************************************************
  469. Synopsis []
  470. Description []
  471. SideEffects []
  472. SeeAlso []
  473. ***********************************************************************/
  474. int Abc_SopIsComplement( char * pSop )
  475. {
  476. char * pCur;
  477. for ( pCur = pSop; *pCur; pCur++ )
  478. if ( *pCur == '\n' )
  479. return (int)(*(pCur - 1) == '0' || *(pCur - 1) == 'n');
  480. assert( 0 );
  481. return 0;
  482. }
  483. /**Function*************************************************************
  484. Synopsis [Checks if the cover is constant 0.]
  485. Description []
  486. SideEffects []
  487. SeeAlso []
  488. ***********************************************************************/
  489. int Abc_SopIsConst0( char * pSop )
  490. {
  491. return pSop[0] == ' ' && pSop[1] == '0';
  492. }
  493. /**Function*************************************************************
  494. Synopsis [Checks if the cover is constant 1.]
  495. Description []
  496. SideEffects []
  497. SeeAlso []
  498. ***********************************************************************/
  499. int Abc_SopIsConst1( char * pSop )
  500. {
  501. return pSop[0] == ' ' && pSop[1] == '1';
  502. }
  503. /**Function*************************************************************
  504. Synopsis [Checks if the cover is constant 1.]
  505. Description []
  506. SideEffects []
  507. SeeAlso []
  508. ***********************************************************************/
  509. int Abc_SopIsBuf( char * pSop )
  510. {
  511. if ( pSop[4] != 0 )
  512. return 0;
  513. if ( (pSop[0] == '1' && pSop[2] == '1') || (pSop[0] == '0' && pSop[2] == '0') )
  514. return 1;
  515. return 0;
  516. }
  517. /**Function*************************************************************
  518. Synopsis [Checks if the cover is constant 1.]
  519. Description []
  520. SideEffects []
  521. SeeAlso []
  522. ***********************************************************************/
  523. int Abc_SopIsInv( char * pSop )
  524. {
  525. if ( pSop[4] != 0 )
  526. return 0;
  527. if ( (pSop[0] == '0' && pSop[2] == '1') || (pSop[0] == '1' && pSop[2] == '0') )
  528. return 1;
  529. return 0;
  530. }
  531. /**Function*************************************************************
  532. Synopsis [Checks if the cover is AND with possibly complemented inputs.]
  533. Description []
  534. SideEffects []
  535. SeeAlso []
  536. ***********************************************************************/
  537. int Abc_SopIsAndType( char * pSop )
  538. {
  539. char * pCur;
  540. if ( Abc_SopGetCubeNum(pSop) != 1 )
  541. return 0;
  542. for ( pCur = pSop; *pCur != ' '; pCur++ )
  543. if ( *pCur == '-' )
  544. return 0;
  545. if ( pCur[1] != '1' )
  546. return 0;
  547. return 1;
  548. }
  549. /**Function*************************************************************
  550. Synopsis [Checks if the cover is OR with possibly complemented inputs.]
  551. Description []
  552. SideEffects []
  553. SeeAlso []
  554. ***********************************************************************/
  555. int Abc_SopIsOrType( char * pSop )
  556. {
  557. char * pCube, * pCur;
  558. int nVars, nLits;
  559. nVars = Abc_SopGetVarNum( pSop );
  560. if ( nVars != Abc_SopGetCubeNum(pSop) )
  561. return 0;
  562. Abc_SopForEachCube( pSop, nVars, pCube )
  563. {
  564. // count the number of literals in the cube
  565. nLits = 0;
  566. for ( pCur = pCube; *pCur != ' '; pCur++ )
  567. nLits += ( *pCur != '-' );
  568. if ( nLits != 1 )
  569. return 0;
  570. }
  571. return 1;
  572. }
  573. /**Function*************************************************************
  574. Synopsis []
  575. Description []
  576. SideEffects []
  577. SeeAlso []
  578. ***********************************************************************/
  579. int Abc_SopIsExorType( char * pSop )
  580. {
  581. char * pCur;
  582. for ( pCur = pSop; *pCur; pCur++ )
  583. if ( *pCur == '\n' )
  584. return (int)(*(pCur - 1) == 'x' || *(pCur - 1) == 'n');
  585. assert( 0 );
  586. return 0;
  587. }
  588. /**Function*************************************************************
  589. Synopsis []
  590. Description []
  591. SideEffects []
  592. SeeAlso []
  593. ***********************************************************************/
  594. int Abc_SopCheck( char * pSop, int nFanins )
  595. {
  596. char * pCubes, * pCubesOld;
  597. int fFound0 = 0, fFound1 = 0;
  598. // check the logic function of the node
  599. for ( pCubes = pSop; *pCubes; pCubes++ )
  600. {
  601. // get the end of the next cube
  602. for ( pCubesOld = pCubes; *pCubes != ' '; pCubes++ );
  603. // compare the distance
  604. if ( pCubes - pCubesOld != nFanins )
  605. {
  606. fprintf( stdout, "Abc_SopCheck: SOP has a mismatch between its cover size (%d) and its fanin number (%d).\n",
  607. (int)(ABC_PTRDIFF_T)(pCubes - pCubesOld), nFanins );
  608. return 0;
  609. }
  610. // check the output values for this cube
  611. pCubes++;
  612. if ( *pCubes == '0' )
  613. fFound0 = 1;
  614. else if ( *pCubes == '1' )
  615. fFound1 = 1;
  616. else if ( *pCubes != 'x' && *pCubes != 'n' )
  617. {
  618. fprintf( stdout, "Abc_SopCheck: SOP has a strange character (%c) in the output part of its cube.\n", *pCubes );
  619. return 0;
  620. }
  621. // check the last symbol (new line)
  622. pCubes++;
  623. if ( *pCubes != '\n' )
  624. {
  625. fprintf( stdout, "Abc_SopCheck: SOP has a cube without new line in the end.\n" );
  626. return 0;
  627. }
  628. }
  629. if ( fFound0 && fFound1 )
  630. {
  631. fprintf( stdout, "Abc_SopCheck: SOP has cubes in both phases.\n" );
  632. return 0;
  633. }
  634. return 1;
  635. }
  636. /**Function*************************************************************
  637. Synopsis [Derives SOP from the truth table representation.]
  638. Description [Truth table is expected to be in the hexadecimal notation.]
  639. SideEffects []
  640. SeeAlso []
  641. ***********************************************************************/
  642. char * Abc_SopFromTruthBin( char * pTruth )
  643. {
  644. char * pSopCover, * pCube;
  645. int nTruthSize, nVars, Digit, Length, Mint, i, b;
  646. Vec_Int_t * vMints;
  647. // get the number of variables
  648. nTruthSize = strlen(pTruth);
  649. nVars = Abc_Base2Log( nTruthSize );
  650. if ( nTruthSize != (1 << (nVars)) )
  651. {
  652. printf( "String %s does not look like a truth table of a %d-variable function.\n", pTruth, nVars );
  653. return NULL;
  654. }
  655. // collect the on-set minterms
  656. vMints = Vec_IntAlloc( 100 );
  657. for ( i = 0; i < nTruthSize; i++ )
  658. {
  659. if ( pTruth[i] >= '0' && pTruth[i] <= '1' )
  660. Digit = pTruth[i] - '0';
  661. else
  662. {
  663. Vec_IntFree( vMints );
  664. printf( "String %s does not look like a binary representation of the truth table.\n", pTruth );
  665. return NULL;
  666. }
  667. if ( Digit == 1 )
  668. Vec_IntPush( vMints, nTruthSize - 1 - i );
  669. }
  670. if ( Vec_IntSize( vMints ) == 0 || Vec_IntSize( vMints ) == nTruthSize )
  671. {
  672. Vec_IntFree( vMints );
  673. printf( "Cannot create constant function.\n" );
  674. return NULL;
  675. }
  676. // create the SOP representation of the minterms
  677. Length = Vec_IntSize(vMints) * (nVars + 3);
  678. pSopCover = ABC_ALLOC( char, Length + 1 );
  679. pSopCover[Length] = 0;
  680. Vec_IntForEachEntry( vMints, Mint, i )
  681. {
  682. pCube = pSopCover + i * (nVars + 3);
  683. for ( b = 0; b < nVars; b++ )
  684. if ( Mint & (1 << (nVars-1-b)) )
  685. // if ( Mint & (1 << b) )
  686. pCube[b] = '1';
  687. else
  688. pCube[b] = '0';
  689. pCube[nVars + 0] = ' ';
  690. pCube[nVars + 1] = '1';
  691. pCube[nVars + 2] = '\n';
  692. }
  693. Vec_IntFree( vMints );
  694. return pSopCover;
  695. }
  696. /**Function*************************************************************
  697. Synopsis [Derives SOP from the truth table representation.]
  698. Description [Truth table is expected to be in the hexadecimal notation.]
  699. SideEffects []
  700. SeeAlso []
  701. ***********************************************************************/
  702. char * Abc_SopFromTruthHex( char * pTruth )
  703. {
  704. char * pSopCover, * pCube;
  705. int nTruthSize, nVars, Digit, Length, Mint, i, b;
  706. Vec_Int_t * vMints;
  707. // get the number of variables
  708. nTruthSize = strlen(pTruth);
  709. nVars = (nTruthSize < 2) ? 2 : Abc_Base2Log(nTruthSize) + 2;
  710. if ( nTruthSize != (1 << (nVars-2)) )
  711. {
  712. printf( "String %s does not look like a truth table of a %d-variable function.\n", pTruth, nVars );
  713. return NULL;
  714. }
  715. // collect the on-set minterms
  716. vMints = Vec_IntAlloc( 100 );
  717. for ( i = 0; i < nTruthSize; i++ )
  718. {
  719. if ( pTruth[i] >= '0' && pTruth[i] <= '9' )
  720. Digit = pTruth[i] - '0';
  721. else if ( pTruth[i] >= 'a' && pTruth[i] <= 'f' )
  722. Digit = 10 + pTruth[i] - 'a';
  723. else if ( pTruth[i] >= 'A' && pTruth[i] <= 'F' )
  724. Digit = 10 + pTruth[i] - 'A';
  725. else
  726. {
  727. printf( "String %s does not look like a hexadecimal representation of the truth table.\n", pTruth );
  728. return NULL;
  729. }
  730. for ( b = 0; b < 4; b++ )
  731. if ( Digit & (1 << b) )
  732. Vec_IntPush( vMints, 4*(nTruthSize-1-i)+b );
  733. }
  734. // create the SOP representation of the minterms
  735. Length = Vec_IntSize(vMints) * (nVars + 3);
  736. pSopCover = ABC_ALLOC( char, Length + 1 );
  737. pSopCover[Length] = 0;
  738. Vec_IntForEachEntry( vMints, Mint, i )
  739. {
  740. pCube = pSopCover + i * (nVars + 3);
  741. for ( b = 0; b < nVars; b++ )
  742. // if ( Mint & (1 << (nVars-1-b)) )
  743. if ( Mint & (1 << b) )
  744. pCube[b] = '1';
  745. else
  746. pCube[b] = '0';
  747. pCube[nVars + 0] = ' ';
  748. pCube[nVars + 1] = '1';
  749. pCube[nVars + 2] = '\n';
  750. }
  751. /*
  752. // create TT representation
  753. {
  754. extern void Bdc_ManDecomposeTest( unsigned uTruth, int nVars );
  755. unsigned uTruth = 0;
  756. int nVarsAll = 4;
  757. assert( nVarsAll == 4 );
  758. assert( nVars <= nVarsAll );
  759. Vec_IntForEachEntry( vMints, Mint, i )
  760. uTruth |= (1 << Mint);
  761. // uTruth = uTruth | (uTruth << 8) | (uTruth << 16) | (uTruth << 24);
  762. uTruth = uTruth | (uTruth << 16);
  763. Bdc_ManDecomposeTest( uTruth, nVarsAll );
  764. }
  765. */
  766. Vec_IntFree( vMints );
  767. return pSopCover;
  768. }
  769. /**Function*************************************************************
  770. Synopsis [Creates one encoder node.]
  771. Description [Produces MV-SOP for BLIF-MV representation.]
  772. SideEffects []
  773. SeeAlso []
  774. ***********************************************************************/
  775. char * Abc_SopEncoderPos( Mem_Flex_t * pMan, int iValue, int nValues )
  776. {
  777. char Buffer[32];
  778. assert( iValue < nValues );
  779. sprintf( Buffer, "d0\n%d 1\n", iValue );
  780. return Abc_SopRegister( pMan, Buffer );
  781. }
  782. /**Function*************************************************************
  783. Synopsis [Creates one encoder node.]
  784. Description [Produces MV-SOP for BLIF-MV representation.]
  785. SideEffects []
  786. SeeAlso []
  787. ***********************************************************************/
  788. char * Abc_SopEncoderLog( Mem_Flex_t * pMan, int iBit, int nValues )
  789. {
  790. char * pResult;
  791. Vec_Str_t * vSop;
  792. int v, Counter, fFirst = 1, nBits = Abc_Base2Log(nValues);
  793. assert( iBit < nBits );
  794. // count the number of literals
  795. Counter = 0;
  796. for ( v = 0; v < nValues; v++ )
  797. Counter += ( (v & (1 << iBit)) > 0 );
  798. // create the cover
  799. vSop = Vec_StrAlloc( 100 );
  800. Vec_StrPrintStr( vSop, "d0\n" );
  801. if ( Counter > 1 )
  802. Vec_StrPrintStr( vSop, "(" );
  803. for ( v = 0; v < nValues; v++ )
  804. if ( v & (1 << iBit) )
  805. {
  806. if ( fFirst )
  807. fFirst = 0;
  808. else
  809. Vec_StrPush( vSop, ',' );
  810. Vec_StrPrintNum( vSop, v );
  811. }
  812. if ( Counter > 1 )
  813. Vec_StrPrintStr( vSop, ")" );
  814. Vec_StrPrintStr( vSop, " 1\n" );
  815. Vec_StrPush( vSop, 0 );
  816. pResult = Abc_SopRegister( pMan, Vec_StrArray(vSop) );
  817. Vec_StrFree( vSop );
  818. return pResult;
  819. }
  820. /**Function*************************************************************
  821. Synopsis [Creates the decoder node.]
  822. Description [Produces MV-SOP for BLIF-MV representation.]
  823. SideEffects []
  824. SeeAlso []
  825. ***********************************************************************/
  826. char * Abc_SopDecoderPos( Mem_Flex_t * pMan, int nValues )
  827. {
  828. char * pResult;
  829. Vec_Str_t * vSop;
  830. int i, k;
  831. assert( nValues > 1 );
  832. vSop = Vec_StrAlloc( 100 );
  833. for ( i = 0; i < nValues; i++ )
  834. {
  835. for ( k = 0; k < nValues; k++ )
  836. {
  837. if ( k == i )
  838. Vec_StrPrintStr( vSop, "1 " );
  839. else
  840. Vec_StrPrintStr( vSop, "- " );
  841. }
  842. Vec_StrPrintNum( vSop, i );
  843. Vec_StrPush( vSop, '\n' );
  844. }
  845. Vec_StrPush( vSop, 0 );
  846. pResult = Abc_SopRegister( pMan, Vec_StrArray(vSop) );
  847. Vec_StrFree( vSop );
  848. return pResult;
  849. }
  850. /**Function*************************************************************
  851. Synopsis [Creates the decover node.]
  852. Description [Produces MV-SOP for BLIF-MV representation.]
  853. SideEffects []
  854. SeeAlso []
  855. ***********************************************************************/
  856. char * Abc_SopDecoderLog( Mem_Flex_t * pMan, int nValues )
  857. {
  858. char * pResult;
  859. Vec_Str_t * vSop;
  860. int i, b, nBits = Abc_Base2Log(nValues);
  861. assert( nValues > 1 && nValues <= (1<<nBits) );
  862. vSop = Vec_StrAlloc( 100 );
  863. for ( i = 0; i < nValues; i++ )
  864. {
  865. for ( b = 0; b < nBits; b++ )
  866. {
  867. Vec_StrPrintNum( vSop, (int)((i & (1 << b)) > 0) );
  868. Vec_StrPush( vSop, ' ' );
  869. }
  870. Vec_StrPrintNum( vSop, i );
  871. Vec_StrPush( vSop, '\n' );
  872. }
  873. Vec_StrPush( vSop, 0 );
  874. pResult = Abc_SopRegister( pMan, Vec_StrArray(vSop) );
  875. Vec_StrFree( vSop );
  876. return pResult;
  877. }
  878. /**Function*************************************************************
  879. Synopsis [Computes truth table of the node.]
  880. Description []
  881. SideEffects []
  882. SeeAlso []
  883. ***********************************************************************/
  884. word Abc_SopToTruth( char * pSop, int nInputs )
  885. {
  886. static word Truth[8] = {
  887. ABC_CONST(0xAAAAAAAAAAAAAAAA),
  888. ABC_CONST(0xCCCCCCCCCCCCCCCC),
  889. ABC_CONST(0xF0F0F0F0F0F0F0F0),
  890. ABC_CONST(0xFF00FF00FF00FF00),
  891. ABC_CONST(0xFFFF0000FFFF0000),
  892. ABC_CONST(0xFFFFFFFF00000000),
  893. ABC_CONST(0x0000000000000000),
  894. ABC_CONST(0xFFFFFFFFFFFFFFFF)
  895. };
  896. word Cube, Result = 0;
  897. int v, lit = 0;
  898. int nVars = Abc_SopGetVarNum(pSop);
  899. assert( nVars >= 0 && nVars <= 6 );
  900. assert( nVars == nInputs );
  901. do {
  902. Cube = Truth[7];
  903. for ( v = 0; v < nVars; v++, lit++ )
  904. {
  905. if ( pSop[lit] == '1' )
  906. Cube &= Truth[v];
  907. else if ( pSop[lit] == '0' )
  908. Cube &= ~Truth[v];
  909. else if ( pSop[lit] != '-' )
  910. assert( 0 );
  911. }
  912. Result |= Cube;
  913. assert( pSop[lit] == ' ' );
  914. lit++;
  915. lit++;
  916. assert( pSop[lit] == '\n' );
  917. lit++;
  918. } while ( pSop[lit] );
  919. if ( Abc_SopIsComplement(pSop) )
  920. Result = ~Result;
  921. return Result;
  922. }
  923. /**Function*************************************************************
  924. Synopsis [Computes truth table of the node.]
  925. Description []
  926. SideEffects []
  927. SeeAlso []
  928. ***********************************************************************/
  929. void Abc_SopToTruth7( char * pSop, int nInputs, word r[2] )
  930. {
  931. static word Truth[7][2] = {
  932. {ABC_CONST(0xAAAAAAAAAAAAAAAA),ABC_CONST(0xAAAAAAAAAAAAAAAA)},
  933. {ABC_CONST(0xCCCCCCCCCCCCCCCC),ABC_CONST(0xCCCCCCCCCCCCCCCC)},
  934. {ABC_CONST(0xF0F0F0F0F0F0F0F0),ABC_CONST(0xF0F0F0F0F0F0F0F0)},
  935. {ABC_CONST(0xFF00FF00FF00FF00),ABC_CONST(0xFF00FF00FF00FF00)},
  936. {ABC_CONST(0xFFFF0000FFFF0000),ABC_CONST(0xFFFF0000FFFF0000)},
  937. {ABC_CONST(0xFFFFFFFF00000000),ABC_CONST(0xFFFFFFFF00000000)},
  938. {ABC_CONST(0x0000000000000000),ABC_CONST(0xFFFFFFFFFFFFFFFF)},
  939. };
  940. word Cube[2];
  941. int v, lit = 0;
  942. int nVars = Abc_SopGetVarNum(pSop);
  943. assert( nVars >= 0 && nVars <= 7 );
  944. assert( nVars == nInputs );
  945. r[0] = r[1] = 0;
  946. do {
  947. Cube[0] = Cube[1] = ~(word)0;
  948. for ( v = 0; v < nVars; v++, lit++ )
  949. {
  950. if ( pSop[lit] == '1' )
  951. {
  952. Cube[0] &= Truth[v][0];
  953. Cube[1] &= Truth[v][1];
  954. }
  955. else if ( pSop[lit] == '0' )
  956. {
  957. Cube[0] &= ~Truth[v][0];
  958. Cube[1] &= ~Truth[v][1];
  959. }
  960. else if ( pSop[lit] != '-' )
  961. assert( 0 );
  962. }
  963. r[0] |= Cube[0];
  964. r[1] |= Cube[1];
  965. assert( pSop[lit] == ' ' );
  966. lit++;
  967. lit++;
  968. assert( pSop[lit] == '\n' );
  969. lit++;
  970. } while ( pSop[lit] );
  971. if ( Abc_SopIsComplement(pSop) )
  972. {
  973. r[0] = ~r[0];
  974. r[1] = ~r[1];
  975. }
  976. }
  977. /**Function*************************************************************
  978. Synopsis [Computes truth table of the node.]
  979. Description []
  980. SideEffects []
  981. SeeAlso []
  982. ***********************************************************************/
  983. void Abc_SopToTruthBig( char * pSop, int nInputs, word ** pVars, word * pCube, word * pRes )
  984. {
  985. int nVars = Abc_SopGetVarNum(pSop);
  986. int nWords = nVars <= 6 ? 1 : 1 << (nVars-6);
  987. int v, i, lit = 0;
  988. assert( nVars >= 0 && nVars <= 16 );
  989. assert( nVars == nInputs );
  990. for ( i = 0; i < nWords; i++ )
  991. pRes[i] = 0;
  992. do {
  993. for ( i = 0; i < nWords; i++ )
  994. pCube[i] = ~(word)0;
  995. for ( v = 0; v < nVars; v++, lit++ )
  996. {
  997. if ( pSop[lit] == '1' )
  998. {
  999. for ( i = 0; i < nWords; i++ )
  1000. pCube[i] &= pVars[v][i];
  1001. }
  1002. else if ( pSop[lit] == '0' )
  1003. {
  1004. for ( i = 0; i < nWords; i++ )
  1005. pCube[i] &= ~pVars[v][i];
  1006. }
  1007. else if ( pSop[lit] != '-' )
  1008. assert( 0 );
  1009. }
  1010. for ( i = 0; i < nWords; i++ )
  1011. pRes[i] |= pCube[i];
  1012. assert( pSop[lit] == ' ' );
  1013. lit++;
  1014. lit++;
  1015. assert( pSop[lit] == '\n' );
  1016. lit++;
  1017. } while ( pSop[lit] );
  1018. if ( Abc_SopIsComplement(pSop) )
  1019. {
  1020. for ( i = 0; i < nWords; i++ )
  1021. pRes[i] = ~pRes[i];
  1022. }
  1023. }
  1024. ////////////////////////////////////////////////////////////////////////
  1025. /// END OF FILE ///
  1026. ////////////////////////////////////////////////////////////////////////
  1027. ABC_NAMESPACE_IMPL_END