XilinxProcessorIPLib/drivers/aiengine/examples/aie_sim_test/ext/top/xaiesim_elfload.c
void XAieSim_WriteSection(XAieSim_Tile *TileInstPtr, uint8 *SectName,
						Elf32_Shdr *SectPtr, FILE *Fd)
{
	uint32 *DataPtr;
	uint32 *CurrPtr;
	uint32 NumBytesRead;
	uint32 NumBytesRem;
	uint32 PrevPos;
	uint32 CurrPos;
	uint32 Idx;
        uint32 DmbOff;
        uint32 SectAddr;
        uint32 RemSize;
        uint32 DoneSize;
	uint64_t DmbAddr;
	uint64_t TgtTileAddr;

	/* Point to the section in the ELF file */
	fseek(Fd, SectPtr->sh_offset, SEEK_SET);

	/* Allocate a memory of the size of this section */
	DataPtr = (uint32 *)malloc(SectPtr->sh_size);
	memset((uint8 *)DataPtr, 0U, SectPtr->sh_size);

	NumBytesRem = SectPtr->sh_size;
	NumBytesRead = 0U;
	CurrPtr = DataPtr;
	PrevPos = ftell(Fd);

	/*
	 * Read complete data of this section until all
	 * bytes of the section are read
	 */
	while(NumBytesRem > 0U) {
		if(NumBytesRem < 4U) {
			*(uint8 *)CurrPtr = fgetc(Fd);
		} else {
			fgets((uint8 *)CurrPtr, NumBytesRem, Fd);
		}
		CurrPos = ftell(Fd);
		NumBytesRead = (CurrPos - PrevPos);
		NumBytesRem -= NumBytesRead;
		PrevPos = CurrPos;
		CurrPtr = (uint8 *)CurrPtr + NumBytesRead;

		XAieSim_print("NumBytesRead : %d, NumBytesRem : %d\n",
				NumBytesRead, NumBytesRem);
	}

	CurrPtr = DataPtr;

	if(SectPtr->sh_flags == SHF_ALLOC ||
			SectPtr->sh_flags == (SHF_ALLOC | SHF_WRITE)) {

                XAieSim_print("Loading %s program section with flag value of 0x%x in data memory at 0x%x address\n",
				SectName, SectPtr->sh_flags, SectPtr->sh_addr);

		TgtTileAddr = XAieSim_GetTargetTileAddr(TileInstPtr,
							SectPtr->sh_addr);

                SectAddr = SectPtr->sh_addr;
                RemSize = SectPtr->sh_size;
                DoneSize = 0U;

		/* Use 32 bit loads to match sim output */
		for(Idx = 0U; Idx < RemSize; Idx += 4U){
			/* Mask address as per the Data memory offset*/
			DmbOff = (SectAddr &
                                       XAIESIM_ELF_TILEADDR_DMB_MASK) + Idx;

                        if(DmbOff & 0x8000U) {
                                /*
                                  * 32 KB boundary cross. Data section 
                                  * moving to the memory bank of next
                                  * cardinal direction. Change the target
                                  * tile address
                                  */
                                SectPtr->sh_addr += 0x8000U;
                                TgtTileAddr =
                                        XAieSim_GetTargetTileAddr(TileInstPtr,
						(SectPtr->sh_addr &
                                                0x1FFFFU));
                                SectAddr = 0x0U;
                                Idx = 0U;
                                DmbOff = 0U;
                                RemSize -= DoneSize;
                        }

                        DmbAddr = TgtTileAddr +
                                        XAIESIM_ELF_TILECORE_DATMEM + DmbOff;
			XAieGbl_Write32(DmbAddr, *CurrPtr++);
                        DoneSize += 4U;
		}
	} else if(SectPtr->sh_flags == (SHF_ALLOC | SHF_EXECINSTR)) {

		XAieSim_print("Loading %s program section with flag value of 0x%x in program memory at 0x%x address\n",
				SectName, SectPtr->sh_flags, SectPtr->sh_addr);

		TgtTileAddr = TileInstPtr->TileAddr +
				XAIESIM_ELF_TILECORE_PRGMEM + SectPtr->sh_addr;

		for(Idx = 0U; Idx < SectPtr->sh_size; Idx += 4U) {
			XAieGbl_Write32((TgtTileAddr + Idx), *CurrPtr++);
		}
	} else {
		XAieSim_print("ERROR: Invalid %s program section with flag value of 0x%x at %p ELF offset\n",
				SectName, SectPtr->sh_flags, SectPtr->sh_offset);
	}

	/* Free the allocated memory */
	free(DataPtr);
}
/*****************************************************************************/
/**
*
* This routine is used to write to the specified section by reading the
* corresponding data from the ELF file.
*
* @param	TileInstPtr: Pointer to the Tile instance structure.
* @param        SectName: Name of the section.
* @param        SectPtr: Poiner to the section entry in the ELF file.
* @param	Fd: Pointer to the ELF file.
*
* @return	None.
*
* @note		None.
*
*******************************************************************************/
This routine is used to write to the specified section by reading the
corresponding data from the ELF file.
This routine is used to write to the specified section by reading the corresponding data from the ELF file .
void XAieSim_WriteSection(XAieSim_Tile *TileInstPtr, uint8 *SectName,
						Elf32_Shdr *SectPtr, FILE *Fd)
{
	uint32 *DataPtr;
	uint32 *CurrPtr;
	uint32 NumBytesRead;
	uint32 NumBytesRem;
	uint32 PrevPos;
	uint32 CurrPos;
	uint32 Idx;
        uint32 DmbOff;
        uint32 SectAddr;
        uint32 RemSize;
        uint32 DoneSize;
	uint64_t DmbAddr;
	uint64_t TgtTileAddr;
	fseek(Fd, SectPtr->sh_offset, SEEK_SET);
	DataPtr = (uint32 *)malloc(SectPtr->sh_size);
	memset((uint8 *)DataPtr, 0U, SectPtr->sh_size);
	NumBytesRem = SectPtr->sh_size;
	NumBytesRead = 0U;
	CurrPtr = DataPtr;
	PrevPos = ftell(Fd);
	while(NumBytesRem > 0U) {
		if(NumBytesRem < 4U) {
			*(uint8 *)CurrPtr = fgetc(Fd);
		} else {
			fgets((uint8 *)CurrPtr, NumBytesRem, Fd);
		}
		CurrPos = ftell(Fd);
		NumBytesRead = (CurrPos - PrevPos);
		NumBytesRem -= NumBytesRead;
		PrevPos = CurrPos;
		CurrPtr = (uint8 *)CurrPtr + NumBytesRead;
		XAieSim_print("NumBytesRead : %d, NumBytesRem : %d\n",
				NumBytesRead, NumBytesRem);
	}
	CurrPtr = DataPtr;
	if(SectPtr->sh_flags == SHF_ALLOC ||
			SectPtr->sh_flags == (SHF_ALLOC | SHF_WRITE)) {
                XAieSim_print("Loading %s program section with flag value of 0x%x in data memory at 0x%x address\n",
				SectName, SectPtr->sh_flags, SectPtr->sh_addr);
		TgtTileAddr = XAieSim_GetTargetTileAddr(TileInstPtr,
							SectPtr->sh_addr);
                SectAddr = SectPtr->sh_addr;
                RemSize = SectPtr->sh_size;
                DoneSize = 0U;
		for(Idx = 0U; Idx < RemSize; Idx += 4U){
			DmbOff = (SectAddr &
                                       XAIESIM_ELF_TILEADDR_DMB_MASK) + Idx;
                        if(DmbOff & 0x8000U) {
                                SectPtr->sh_addr += 0x8000U;
                                TgtTileAddr =
                                        XAieSim_GetTargetTileAddr(TileInstPtr,
						(SectPtr->sh_addr &
                                                0x1FFFFU));
                                SectAddr = 0x0U;
                                Idx = 0U;
                                DmbOff = 0U;
                                RemSize -= DoneSize;
                        }
                        DmbAddr = TgtTileAddr +
                                        XAIESIM_ELF_TILECORE_DATMEM + DmbOff;
			XAieGbl_Write32(DmbAddr, *CurrPtr++);
                        DoneSize += 4U;
		}
	} else if(SectPtr->sh_flags == (SHF_ALLOC | SHF_EXECINSTR)) {
		XAieSim_print("Loading %s program section with flag value of 0x%x in program memory at 0x%x address\n",
				SectName, SectPtr->sh_flags, SectPtr->sh_addr);
		TgtTileAddr = TileInstPtr->TileAddr +
				XAIESIM_ELF_TILECORE_PRGMEM + SectPtr->sh_addr;
		for(Idx = 0U; Idx < SectPtr->sh_size; Idx += 4U) {
			XAieGbl_Write32((TgtTileAddr + Idx), *CurrPtr++);
		}
	} else {
		XAieSim_print("ERROR: Invalid %s program section with flag value of 0x%x at %p ELF offset\n",
				SectName, SectPtr->sh_flags, SectPtr->sh_offset);
	}
	free(DataPtr);
}
void XAieSim_WriteSection ( XAieSim_Tile * TileInstPtr , uint8 * SectName , Elf32_Shdr * SectPtr , FILE * Fd ) { uint32 * DataPtr ; uint32 * CurrPtr ; uint32 NumBytesRead ; uint32 NumBytesRem ; uint32 PrevPos ; uint32 CurrPos ; uint32 Idx ; uint32 DmbOff ; uint32 SectAddr ; uint32 RemSize ; uint32 DoneSize ; uint64_t DmbAddr ; uint64_t TgtTileAddr ; fseek ( Fd , SectPtr -> sh_offset , SEEK_SET ) ; DataPtr = ( uint32 * ) malloc ( SectPtr -> sh_size ) ; memset ( ( uint8 * ) DataPtr , 0U , SectPtr -> sh_size ) ; NumBytesRem = SectPtr -> sh_size ; NumBytesRead = 0U ; CurrPtr = DataPtr ; PrevPos = ftell ( Fd ) ; while ( NumBytesRem > 0U ) { if ( NumBytesRem < 4U ) { * ( uint8 * ) CurrPtr = fgetc ( Fd ) ; } else { fgets ( ( uint8 * ) CurrPtr , NumBytesRem , Fd ) ; } CurrPos = ftell ( Fd ) ; NumBytesRead = ( CurrPos - PrevPos ) ; NumBytesRem -= NumBytesRead ; PrevPos = CurrPos ; CurrPtr = ( uint8 * ) CurrPtr + NumBytesRead ; XAieSim_print ( " \n " , NumBytesRead , NumBytesRem ) ; } CurrPtr = DataPtr ; if ( SectPtr -> sh_flags == SHF_ALLOC || SectPtr -> sh_flags == ( SHF_ALLOC | SHF_WRITE ) ) { XAieSim_print ( " \n " , SectName , SectPtr -> sh_flags , SectPtr -> sh_addr ) ; TgtTileAddr = XAieSim_GetTargetTileAddr ( TileInstPtr , SectPtr -> sh_addr ) ; SectAddr = SectPtr -> sh_addr ; RemSize = SectPtr -> sh_size ; DoneSize = 0U ; for ( Idx = 0U ; Idx < RemSize ; Idx += 4U ) { DmbOff = ( SectAddr & XAIESIM_ELF_TILEADDR_DMB_MASK ) + Idx ; if ( DmbOff & 0x8000U ) { SectPtr -> sh_addr += 0x8000U ; TgtTileAddr = XAieSim_GetTargetTileAddr ( TileInstPtr , ( SectPtr -> sh_addr & 0x1FFFFU ) ) ; SectAddr = 0x0U ; Idx = 0U ; DmbOff = 0U ; RemSize -= DoneSize ; } DmbAddr = TgtTileAddr + XAIESIM_ELF_TILECORE_DATMEM + DmbOff ; XAieGbl_Write32 ( DmbAddr , * CurrPtr ++ ) ; DoneSize += 4U ; } } else if ( SectPtr -> sh_flags == ( SHF_ALLOC | SHF_EXECINSTR ) ) { XAieSim_print ( " \n " , SectName , SectPtr -> sh_flags , SectPtr -> sh_addr ) ; TgtTileAddr = TileInstPtr -> TileAddr + XAIESIM_ELF_TILECORE_PRGMEM + SectPtr -> sh_addr ; for ( Idx = 0U ; Idx < SectPtr -> sh_size ; Idx += 4U ) { XAieGbl_Write32 ( ( TgtTileAddr + Idx ) , * CurrPtr ++ ) ; } } else { XAieSim_print ( " \n " , SectName , SectPtr -> sh_flags , SectPtr -> sh_offset ) ; } free ( DataPtr ) ; }
This routine is used to write to the specified section by reading the
corresponding data from the ELF file.
This routine is used to write to the specified section by reading the corresponding data from the ELF file .
/* Point to the section in the ELF file */ /* Allocate a memory of the size of this section */ /*
	 * Read complete data of this section until all
	 * bytes of the section are read
	 */ /* Use 32 bit loads to match sim output */ /* Mask address as per the Data memory offset*/ /*
                                  * 32 KB boundary cross. Data section 
                                  * moving to the memory bank of next
                                  * cardinal direction. Change the target
                                  * tile address
                                  */ /* Free the allocated memory */