|
23 | 23 | #include <string.h> |
24 | 24 | #include <stdint.h> |
25 | 25 | #include <stdbool.h> |
| 26 | +#include <assert.h> |
26 | 27 |
|
27 | 28 | #include "ioAbc.h" |
| 29 | +#include "map/mio/mio.h" |
28 | 30 |
|
29 | 31 | ABC_NAMESPACE_IMPL_START |
30 | 32 |
|
@@ -605,9 +607,147 @@ int Jsonc_ReadTest( char * pFileName ) |
605 | 607 | return 0; |
606 | 608 | } |
607 | 609 |
|
| 610 | +static int Jsonc_ParseNameBit( const char * pName, char * pBase, int nBase ) |
| 611 | +{ |
| 612 | + const char * pOpen; |
| 613 | + const char * pClose; |
| 614 | + int nCopy; |
| 615 | + if ( pBase && nBase > 0 ) |
| 616 | + pBase[0] = '\0'; |
| 617 | + if ( pName == NULL || pBase == NULL || nBase <= 1 ) |
| 618 | + return 0; |
| 619 | + pOpen = strrchr( pName, '[' ); |
| 620 | + pClose = pOpen ? strchr( pOpen, ']' ) : NULL; |
| 621 | + if ( pOpen && pClose && pClose > pOpen + 1 ) |
| 622 | + { |
| 623 | + nCopy = (int)(pOpen - pName); |
| 624 | + if ( nCopy > nBase - 1 ) |
| 625 | + nCopy = nBase - 1; |
| 626 | + memcpy( pBase, pName, nCopy ); |
| 627 | + pBase[nCopy] = '\0'; |
| 628 | + return atoi( pOpen + 1 ); |
| 629 | + } |
| 630 | + strncpy( pBase, pName, nBase - 1 ); |
| 631 | + pBase[nBase - 1] = '\0'; |
| 632 | + return 0; |
| 633 | +} |
| 634 | +static const char * Jsonc_GetPortName( Abc_Obj_t * pObj ) |
| 635 | +{ |
| 636 | + if ( Abc_ObjIsCi(pObj) && Abc_NtkIsNetlist(pObj->pNtk) && Abc_ObjFanoutNum(pObj) ) |
| 637 | + return Abc_ObjName( Abc_ObjFanout0(pObj) ); |
| 638 | + if ( Abc_ObjIsCo(pObj) && Abc_NtkIsNetlist(pObj->pNtk) && Abc_ObjFaninNum(pObj) ) |
| 639 | + return Abc_ObjName( Abc_ObjFanin0(pObj) ); |
| 640 | + return Abc_ObjName(pObj); |
| 641 | +} |
| 642 | +static const char * Jsonc_GetNodeOutName( Abc_Obj_t * pObj ) |
| 643 | +{ |
| 644 | + static char Buffer[1024]; |
| 645 | + if ( Abc_ObjFanoutNum(pObj) ) |
| 646 | + { |
| 647 | + Abc_Obj_t * pFan0 = Abc_ObjFanout0(pObj); |
| 648 | + if ( Abc_ObjIsNet(pFan0) || Abc_ObjIsCo(pFan0) ) |
| 649 | + return Abc_ObjName(pFan0); |
| 650 | + } |
| 651 | + if ( Abc_ObjName(pObj) ) |
| 652 | + return Abc_ObjName(pObj); |
| 653 | + snprintf( Buffer, sizeof(Buffer), "n%d", Abc_ObjId(pObj) ); |
| 654 | + return Buffer; |
| 655 | +} |
| 656 | + |
| 657 | +/**Function************************************************************* |
| 658 | +
|
| 659 | + Synopsis [] |
| 660 | +
|
| 661 | + Description [] |
| 662 | + |
| 663 | + SideEffects [] |
| 664 | +
|
| 665 | + SeeAlso [] |
| 666 | +
|
| 667 | +***********************************************************************/ |
| 668 | +void Jsonc_WriteTest( Abc_Ntk_t * p, char * pFileName ) |
| 669 | +{ |
| 670 | + Vec_Ptr_t * vNodes; |
| 671 | + Vec_Int_t * vObj2Num; |
| 672 | + Abc_Obj_t * pObj; |
| 673 | + FILE * pFile; |
| 674 | + int i, Counter, Total; |
| 675 | + assert( Abc_NtkHasMapping(p) ); |
| 676 | + vNodes = Abc_NtkDfs2( p ); |
| 677 | + vObj2Num = Vec_IntStartFull( Abc_NtkObjNumMax(p) ); |
| 678 | + Total = Abc_NtkPiNum(p) + Vec_PtrSize(vNodes) + Abc_NtkPoNum(p); |
| 679 | + pFile = fopen( pFileName, "wb" ); |
| 680 | + if ( pFile == NULL ) |
| 681 | + { |
| 682 | + printf( "Jsonc_WriteTest(): Cannot open output file \"%s\" for writing.\n", pFileName ); |
| 683 | + Vec_IntFree( vObj2Num ); |
| 684 | + Vec_PtrFree( vNodes ); |
| 685 | + return; |
| 686 | + } |
| 687 | + fprintf( pFile, "{\n" ); |
| 688 | + fprintf( pFile, " \"name\": \"%s\",\n", Abc_NtkName(p) ? Abc_NtkName(p) : "" ); |
| 689 | + fprintf( pFile, " \"nodes\": [\n" ); |
| 690 | + Counter = 0; |
| 691 | + Abc_NtkForEachPi( p, pObj, i ) |
| 692 | + { |
| 693 | + char Name[1024]; |
| 694 | + int Bit = Jsonc_ParseNameBit( Jsonc_GetPortName(pObj), Name, sizeof(Name) ); |
| 695 | + Vec_IntWriteEntry( vObj2Num, Abc_ObjId(pObj), Counter ); |
| 696 | + fprintf( pFile, " {\n" ); |
| 697 | + fprintf( pFile, " \"type\": \"pi\",\n" ); |
| 698 | + fprintf( pFile, " \"name\": \"%s\",\n", Name ); |
| 699 | + fprintf( pFile, " \"bit\": %d\n", Bit ); |
| 700 | + fprintf( pFile, " }%s\n", ++Counter == Total ? "" : "," ); |
| 701 | + } |
| 702 | + Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i ) |
| 703 | + { |
| 704 | + Mio_Gate_t * pGate = (Mio_Gate_t *)pObj->pData; |
| 705 | + Mio_Pin_t * pPin; |
| 706 | + int k; |
| 707 | + assert( pGate != NULL ); |
| 708 | + Vec_IntWriteEntry( vObj2Num, Abc_ObjId(pObj), Counter ); |
| 709 | + fprintf( pFile, " {\n" ); |
| 710 | + fprintf( pFile, " \"type\": \"%s\",\n", Mio_GateReadName(pGate) ); |
| 711 | + fprintf( pFile, " \"name\": \"%s\",\n", Jsonc_GetNodeOutName(pObj) ); |
| 712 | + fprintf( pFile, " \"fanins\": [\n" ); |
| 713 | + fprintf( pFile, " {\n" ); |
| 714 | + for ( pPin = Mio_GateReadPins(pGate), k = 0; pPin; pPin = Mio_PinReadNext(pPin), k++ ) |
| 715 | + { |
| 716 | + Abc_Obj_t * pFanin = Abc_ObjFanin0Ntk( Abc_ObjFanin(pObj, k) ); |
| 717 | + int FanId = Vec_IntEntry( vObj2Num, Abc_ObjId(pFanin) ); |
| 718 | + assert( FanId >= 0 ); |
| 719 | + fprintf( pFile, " \"%s\": { \"node\": %d }%s\n", Mio_PinReadName(pPin), FanId, Mio_PinReadNext(pPin) ? "," : "" ); |
| 720 | + } |
| 721 | + fprintf( pFile, " }\n" ); |
| 722 | + fprintf( pFile, " ]\n" ); |
| 723 | + fprintf( pFile, " }%s\n", ++Counter == Total ? "" : "," ); |
| 724 | + } |
| 725 | + Abc_NtkForEachPo( p, pObj, i ) |
| 726 | + { |
| 727 | + Abc_Obj_t * pDriver = Abc_ObjFanin0Ntk( Abc_ObjFanin0(pObj) ); |
| 728 | + char Name[1024]; |
| 729 | + int Bit = Jsonc_ParseNameBit( Jsonc_GetPortName(pObj), Name, sizeof(Name) ); |
| 730 | + int FanId = Vec_IntEntry( vObj2Num, Abc_ObjId(pDriver) ); |
| 731 | + assert( FanId >= 0 ); |
| 732 | + fprintf( pFile, " {\n" ); |
| 733 | + fprintf( pFile, " \"type\": \"PO\",\n" ); |
| 734 | + fprintf( pFile, " \"name\": \"%s\",\n", Name ); |
| 735 | + fprintf( pFile, " \"bit\": %d,\n", Bit ); |
| 736 | + fprintf( pFile, " \"fanin\": { \"node\": %d", FanId ); |
| 737 | + //if ( Abc_ObjIsNode(pDriver) && pDriver->pData != NULL ) |
| 738 | + // fprintf( pFile, ", \"pin\": \"%s\"", Mio_GateReadOutName((Mio_Gate_t *)pDriver->pData) ); |
| 739 | + fprintf( pFile, " }\n" ); |
| 740 | + fprintf( pFile, " }%s\n", ++Counter == Total ? "" : "," ); |
| 741 | + } |
| 742 | + fprintf( pFile, " ]\n" ); |
| 743 | + fprintf( pFile, "}\n" ); |
| 744 | + fclose( pFile ); |
| 745 | + Vec_IntFree( vObj2Num ); |
| 746 | + Vec_PtrFree( vNodes ); |
| 747 | +} |
| 748 | + |
608 | 749 | //////////////////////////////////////////////////////////////////////// |
609 | 750 | /// END OF FILE /// |
610 | 751 | //////////////////////////////////////////////////////////////////////// |
611 | 752 |
|
612 | 753 | ABC_NAMESPACE_IMPL_END |
613 | | - |
|
0 commit comments