Skip to content

Commit 94d0b0d

Browse files
committed
Command "write_jsonc".
1 parent 362661f commit 94d0b0d

File tree

2 files changed

+191
-1
lines changed

2 files changed

+191
-1
lines changed

src/base/io/io.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static int IoCommandWriteTruths ( Abc_Frame_t * pAbc, int argc, char **argv );
9393
static int IoCommandWriteStatus ( Abc_Frame_t * pAbc, int argc, char **argv );
9494
static int IoCommandWriteSmv ( Abc_Frame_t * pAbc, int argc, char **argv );
9595
static int IoCommandWriteJson ( Abc_Frame_t * pAbc, int argc, char **argv );
96+
static int IoCommandWriteJsonC ( Abc_Frame_t * pAbc, int argc, char **argv );
9697
static int IoCommandWriteResub ( Abc_Frame_t * pAbc, int argc, char **argv );
9798
static int IoCommandWriteMM ( Abc_Frame_t * pAbc, int argc, char **argv );
9899
static int IoCommandWriteMMGia ( Abc_Frame_t * pAbc, int argc, char **argv );
@@ -176,6 +177,7 @@ void Io_Init( Abc_Frame_t * pAbc )
176177
Cmd_CommandAdd( pAbc, "I/O", "write_status", IoCommandWriteStatus, 0 );
177178
Cmd_CommandAdd( pAbc, "I/O", "write_smv", IoCommandWriteSmv, 0 );
178179
Cmd_CommandAdd( pAbc, "I/O", "write_json", IoCommandWriteJson, 0 );
180+
Cmd_CommandAdd( pAbc, "I/O", "write_jsonc", IoCommandWriteJsonC, 0 );
179181
Cmd_CommandAdd( pAbc, "I/O", "&write_resub", IoCommandWriteResub, 0 );
180182
Cmd_CommandAdd( pAbc, "I/O", "write_mm", IoCommandWriteMM, 0 );
181183
Cmd_CommandAdd( pAbc, "I/O", "&write_mm", IoCommandWriteMMGia, 0 );
@@ -4395,6 +4397,54 @@ int IoCommandWriteJson( Abc_Frame_t * pAbc, int argc, char **argv )
43954397
return 1;
43964398
}
43974399

4400+
/**Function*************************************************************
4401+
4402+
Synopsis []
4403+
4404+
Description []
4405+
4406+
SideEffects []
4407+
4408+
SeeAlso []
4409+
4410+
***********************************************************************/
4411+
int IoCommandWriteJsonC( Abc_Frame_t * pAbc, int argc, char **argv )
4412+
{
4413+
extern void Jsonc_WriteTest( Abc_Ntk_t * p, char * pFileName );
4414+
Abc_Ntk_t * pNtk = Abc_FrameReadNtk(pAbc);
4415+
char * pFileName;
4416+
int c;
4417+
4418+
Extra_UtilGetoptReset();
4419+
while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
4420+
{
4421+
switch ( c )
4422+
{
4423+
case 'h':
4424+
goto usage;
4425+
default:
4426+
goto usage;
4427+
}
4428+
}
4429+
if ( pNtk == NULL || !Abc_NtkHasMapping(pNtk) )
4430+
{
4431+
fprintf( pAbc->Out, "No curent network or network is not mapped.\n" );
4432+
return 0;
4433+
}
4434+
if ( argc != globalUtilOptind + 1 )
4435+
goto usage;
4436+
pFileName = argv[globalUtilOptind];
4437+
Jsonc_WriteTest( pNtk, pFileName );
4438+
return 0;
4439+
4440+
usage:
4441+
fprintf( pAbc->Err, "usage: write_jsonc [-ch] <file>\n" );
4442+
fprintf( pAbc->Err, "\t write the network in JSONC format\n" );
4443+
fprintf( pAbc->Err, "\t-h : print the help message\n" );
4444+
fprintf( pAbc->Err, "\tfile : the name of the file to write (extension .jsonc)\n" );
4445+
return 1;
4446+
}
4447+
43984448
/**Function*************************************************************
43994449
44004450
Synopsis []

src/base/io/ioJsonc.c

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#include <string.h>
2424
#include <stdint.h>
2525
#include <stdbool.h>
26+
#include <assert.h>
2627

2728
#include "ioAbc.h"
29+
#include "map/mio/mio.h"
2830

2931
ABC_NAMESPACE_IMPL_START
3032

@@ -605,9 +607,147 @@ int Jsonc_ReadTest( char * pFileName )
605607
return 0;
606608
}
607609

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+
608749
////////////////////////////////////////////////////////////////////////
609750
/// END OF FILE ///
610751
////////////////////////////////////////////////////////////////////////
611752

612753
ABC_NAMESPACE_IMPL_END
613-

0 commit comments

Comments
 (0)