<commit_before>#include <stdio.h>
#include "Ps2Const.h"
#include "SIF.h"
#include "PtrMacro.h"
#include "Profiler.h"
#include "Log.h"
#include "iop/IopBios.h"
#include "StructCollectionStateFile.h"
#include "lexical_cast_ex.h"

#define		CMD_RECVADDR		(CIopBios::CONTROL_BLOCK_END)
#define		RPC_RECVADDR		0xDEADBEEF

#define LOG_NAME					("sif")

#define STATE_REGS_XML				("sif/regs.xml")
#define STATE_CALL_REPLIES_XML		("sif/call_replies.xml")

#ifdef	PROFILE
#define	PROFILE_SIFZONE "SIF"
#endif

#define STATE_PACKET_HEADER_SIZE				("Packet_Header_Size")
#define STATE_PACKET_HEADER_DEST				("Packet_Header_Dest")
#define STATE_PACKET_HEADER_CID					("Packet_Header_CId")
#define STATE_PACKET_HEADER_OPTIONAL			("Packet_Header_Optional")

#define STATE_PACKET_CALL_RECORDID				("Packet_Call_RecordId")
#define STATE_PACKET_CALL_PACKETADDR			("Packet_Call_PacketAddr")
#define STATE_PACKET_CALL_RPCID					("Packet_Call_RpcId")
#define STATE_PACKET_CALL_CLIENTDATAADDR		("Packet_Call_ClientDataAddr")
#define STATE_PACKET_CALL_RPCNUMBER				("Packet_Call_RPCNumber")
#define STATE_PACKET_CALL_SENDSIZE				("Packet_Call_SendSize")
#define STATE_PACKET_CALL_RECV					("Packet_Call_Recv")
#define STATE_PACKET_CALL_RECVSIZE				("Packet_Call_RecvSize")
#define STATE_PACKET_CALL_RECVMODE				("Packet_Call_RecvMode")
#define STATE_PACKET_CALL_SERVERDATAADDR		("Packet_Call_ServerDataAddr")

#define STATE_PACKET_REQUEST_END_RECORDID		("Packet_Request_End_RecordId")
#define STATE_PACKET_REQUEST_END_PACKETADDR		("Packet_Request_End_PacketAddr")
#define STATE_PACKET_REQUEST_END_RPCID			("Packet_Request_End_RpcId")
#define STATE_PACKET_REQUEST_END_CLIENTDATAADDR	("Packet_Request_End_ClientDataAddr")
#define STATE_PACKET_REQUEST_END_CID			("Packet_Request_End_CId")
#define STATE_PACKET_REQUEST_END_SERVERDATAADDR	("Packet_Request_End_ServerDataAddr")
#define STATE_PACKET_REQUEST_END_BUFFER			("Packet_Request_End_Buffer")
#define STATE_PACKET_REQUEST_END_CLIENTBUFFER	("Packet_Request_End_ClientBuffer")

CSIF::CSIF(CDMAC& dmac, uint8* eeRam, uint8* iopRam)
: m_nMAINADDR(0)
, m_nSUBADDR(0)
, m_nMSFLAG(0)
, m_nSMFLAG(0)
, m_nEERecvAddr(0)
, m_nDataAddr(0)
, m_dmac(dmac)
, m_eeRam(eeRam)
, m_iopRam(iopRam)
, m_dmaBuffer(NULL)
, m_dmaBufferSize(0)
{

}

CSIF::~CSIF()
{

}

void CSIF::Reset()
{
	m_nMAINADDR		= 0;
	//This should be the address to which the IOP receives its requests from the EE
	m_nSUBADDR		= CMD_RECVADDR;
	m_nMSFLAG		= 0;
//	m_nSMFLAG		= 0x20000;
	m_nSMFLAG		= 0x60000;

	memset(m_nUserReg, 0, sizeof(uint32) * MAX_USERREG);

	m_packetQueue.clear();
	m_packetProcessed = true;

	m_callReplies.clear();
	m_bindReplies.clear();

	DeleteModules();
}

void CSIF::SetDmaBuffer(uint32 bufferAddress, uint32 size)
{
	m_dmaBuffer = m_iopRam + bufferAddress;
	m_dmaBufferSize = size;
}

void CSIF::RegisterModule(uint32 moduleId, CSifModule* module)
{
	m_modules[moduleId] = module;

	BindReplyMap::iterator replyIterator(m_bindReplies.find(moduleId));
	if(replyIterator != m_bindReplies.end())
	{
		SendPacket(&(replyIterator->second), sizeof(RPCREQUESTEND));
		m_bindReplies.erase(replyIterator);
	}
}

bool CSIF::IsModuleRegistered(uint32 moduleId) const
{
	return m_modules.find(moduleId) != std::end(m_modules);
}

void CSIF::UnregisterModule(uint32 moduleId)
{
	m_modules.erase(moduleId);
}

void CSIF::DeleteModules()
{
	m_modules.clear();
}

uint32 CSIF::ReceiveDMA5(uint32 srcAddress, uint32 size, uint32 unused, bool isTagIncluded)
{
	if(size > m_dmaBufferSize)
	{
		throw std::runtime_error("Packet too big.");
	}
	memcpy(m_eeRam + srcAddress, m_dmaBuffer, size);
	return size;
}

uint32 CSIF::ReceiveDMA6(uint32 nSrcAddr, uint32 nSize, uint32 nDstAddr, bool isTagIncluded)
{
	assert(!isTagIncluded);

	//Humm, this is kinda odd, but it ors the address with 0x20000000
	nSrcAddr &= (PS2::EE_RAM_SIZE - 1);

	if(nDstAddr == RPC_RECVADDR)
	{
		//This should be the arguments for the call command
		//Just save the source address for later use
		m_nDataAddr = nSrcAddr;
		return nSize;
	}
	else if(nDstAddr == CMD_RECVADDR)
	{
		PACKETHDR* pHDR = (PACKETHDR*)(m_eeRam + nSrcAddr);

		//This is kinda odd...
		//plasma_tunnel.elf does this
/*
	    if((pHDR->nCID & 0xFF000000) == 0x08000000)
	    {
		    pHDR->nCID &= 0x00FFFFFF;
		    pHDR->nCID |= 0x80000000;
	    }
*/
		CLog::GetInstance().Print(LOG_NAME, "Received command 0x%0.8X.\r\n", pHDR->nCID);

		switch(pHDR->nCID)
		{
		case 0x80000000:
			Cmd_SetEERecvAddr(pHDR);
			break;
		case SIF_CMD_INIT:
			Cmd_Initialize(pHDR);
			break;
		case SIF_CMD_BIND:
			Cmd_Bind(pHDR);
			break;
		case SIF_CMD_CALL:
			Cmd_Call(pHDR);
			break;
		case SIF_CMD_OTHERDATA:
			Cmd_GetOtherData(pHDR);
			break;
		default:
			{
				PACKETHDR header;
				header.nCID			= pHDR->nCID;
				header.nSize		= sizeof(PACKETHDR);
				header.nDest		= NULL;
				header.nOptional	= 0;

				SendPacket(&header, sizeof(PACKETHDR));
			}
			break;
		}

		return nSize;
	}
	else
	{
		assert(nDstAddr < PS2::IOP_RAM_SIZE);
		if(nDstAddr >= 0 && nDstAddr <= CMD_RECVADDR)
		{
			CLog::GetInstance().Print(LOG_NAME, "Warning: Trying to DMA in Bios Control Area.\r\n");
		}
		else
		{
			memcpy(m_iopRam + nDstAddr, m_eeRam + nSrcAddr, nSize);
		}
		return nSize;
	}
}

void CSIF::SendPacket(void* packet, uint32 size)
{<commit_msg>Log direct EE to IOP transfers.<commit_after>#include <stdio.h>
#include "Ps2Const.h"
#include "SIF.h"
#include "PtrMacro.h"
#include "Profiler.h"
#include "Log.h"
#include "iop/IopBios.h"
#include "StructCollectionStateFile.h"
#include "lexical_cast_ex.h"

#define		CMD_RECVADDR		(CIopBios::CONTROL_BLOCK_END)
#define		RPC_RECVADDR		0xDEADBEEF

#define LOG_NAME					("sif")

#define STATE_REGS_XML				("sif/regs.xml")
#define STATE_CALL_REPLIES_XML		("sif/call_replies.xml")

#ifdef	PROFILE
#define	PROFILE_SIFZONE "SIF"
#endif

#define STATE_PACKET_HEADER_SIZE				("Packet_Header_Size")
#define STATE_PACKET_HEADER_DEST				("Packet_Header_Dest")
#define STATE_PACKET_HEADER_CID					("Packet_Header_CId")
#define STATE_PACKET_HEADER_OPTIONAL			("Packet_Header_Optional")

#define STATE_PACKET_CALL_RECORDID				("Packet_Call_RecordId")
#define STATE_PACKET_CALL_PACKETADDR			("Packet_Call_PacketAddr")
#define STATE_PACKET_CALL_RPCID					("Packet_Call_RpcId")
#define STATE_PACKET_CALL_CLIENTDATAADDR		("Packet_Call_ClientDataAddr")
#define STATE_PACKET_CALL_RPCNUMBER				("Packet_Call_RPCNumber")
#define STATE_PACKET_CALL_SENDSIZE				("Packet_Call_SendSize")
#define STATE_PACKET_CALL_RECV					("Packet_Call_Recv")
#define STATE_PACKET_CALL_RECVSIZE				("Packet_Call_RecvSize")
#define STATE_PACKET_CALL_RECVMODE				("Packet_Call_RecvMode")
#define STATE_PACKET_CALL_SERVERDATAADDR		("Packet_Call_ServerDataAddr")

#define STATE_PACKET_REQUEST_END_RECORDID		("Packet_Request_End_RecordId")
#define STATE_PACKET_REQUEST_END_PACKETADDR		("Packet_Request_End_PacketAddr")
#define STATE_PACKET_REQUEST_END_RPCID			("Packet_Request_End_RpcId")
#define STATE_PACKET_REQUEST_END_CLIENTDATAADDR	("Packet_Request_End_ClientDataAddr")
#define STATE_PACKET_REQUEST_END_CID			("Packet_Request_End_CId")
#define STATE_PACKET_REQUEST_END_SERVERDATAADDR	("Packet_Request_End_ServerDataAddr")
#define STATE_PACKET_REQUEST_END_BUFFER			("Packet_Request_End_Buffer")
#define STATE_PACKET_REQUEST_END_CLIENTBUFFER	("Packet_Request_End_ClientBuffer")

CSIF::CSIF(CDMAC& dmac, uint8* eeRam, uint8* iopRam)
: m_nMAINADDR(0)
, m_nSUBADDR(0)
, m_nMSFLAG(0)
, m_nSMFLAG(0)
, m_nEERecvAddr(0)
, m_nDataAddr(0)
, m_dmac(dmac)
, m_eeRam(eeRam)
, m_iopRam(iopRam)
, m_dmaBuffer(NULL)
, m_dmaBufferSize(0)
{

}

CSIF::~CSIF()
{

}

void CSIF::Reset()
{
	m_nMAINADDR		= 0;
	//This should be the address to which the IOP receives its requests from the EE
	m_nSUBADDR		= CMD_RECVADDR;
	m_nMSFLAG		= 0;
//	m_nSMFLAG		= 0x20000;
	m_nSMFLAG		= 0x60000;

	memset(m_nUserReg, 0, sizeof(uint32) * MAX_USERREG);

	m_packetQueue.clear();
	m_packetProcessed = true;

	m_callReplies.clear();
	m_bindReplies.clear();

	DeleteModules();
}

void CSIF::SetDmaBuffer(uint32 bufferAddress, uint32 size)
{
	m_dmaBuffer = m_iopRam + bufferAddress;
	m_dmaBufferSize = size;
}

void CSIF::RegisterModule(uint32 moduleId, CSifModule* module)
{
	m_modules[moduleId] = module;

	BindReplyMap::iterator replyIterator(m_bindReplies.find(moduleId));
	if(replyIterator != m_bindReplies.end())
	{
		SendPacket(&(replyIterator->second), sizeof(RPCREQUESTEND));
		m_bindReplies.erase(replyIterator);
	}
}

bool CSIF::IsModuleRegistered(uint32 moduleId) const
{
	return m_modules.find(moduleId) != std::end(m_modules);
}

void CSIF::UnregisterModule(uint32 moduleId)
{
	m_modules.erase(moduleId);
}

void CSIF::DeleteModules()
{
	m_modules.clear();
}

uint32 CSIF::ReceiveDMA5(uint32 srcAddress, uint32 size, uint32 unused, bool isTagIncluded)
{
	if(size > m_dmaBufferSize)
	{
		throw std::runtime_error("Packet too big.");
	}
	memcpy(m_eeRam + srcAddress, m_dmaBuffer, size);
	return size;
}

uint32 CSIF::ReceiveDMA6(uint32 nSrcAddr, uint32 nSize, uint32 nDstAddr, bool isTagIncluded)
{
	assert(!isTagIncluded);

	//Humm, this is kinda odd, but it ors the address with 0x20000000
	nSrcAddr &= (PS2::EE_RAM_SIZE - 1);

	if(nDstAddr == RPC_RECVADDR)
	{
		//This should be the arguments for the call command
		//Just save the source address for later use
		m_nDataAddr = nSrcAddr;
		return nSize;
	}
	else if(nDstAddr == CMD_RECVADDR)
	{
		PACKETHDR* pHDR = (PACKETHDR*)(m_eeRam + nSrcAddr);

		//This is kinda odd...
		//plasma_tunnel.elf does this
/*
	    if((pHDR->nCID & 0xFF000000) == 0x08000000)
	    {
		    pHDR->nCID &= 0x00FFFFFF;
		    pHDR->nCID |= 0x80000000;
	    }
*/
		CLog::GetInstance().Print(LOG_NAME, "Received command 0x%0.8X.\r\n", pHDR->nCID);

		switch(pHDR->nCID)
		{
		case 0x80000000:
			Cmd_SetEERecvAddr(pHDR);
			break;
		case SIF_CMD_INIT:
			Cmd_Initialize(pHDR);
			break;
		case SIF_CMD_BIND:
			Cmd_Bind(pHDR);
			break;
		case SIF_CMD_CALL:
			Cmd_Call(pHDR);
			break;
		case SIF_CMD_OTHERDATA:
			Cmd_GetOtherData(pHDR);
			break;
		default:
			{
				PACKETHDR header;
				header.nCID			= pHDR->nCID;
				header.nSize		= sizeof(PACKETHDR);
				header.nDest		= NULL;
				header.nOptional	= 0;

				SendPacket(&header, sizeof(PACKETHDR));
			}
			break;
		}

		return nSize;
	}
	else
	{
		assert(nDstAddr < PS2::IOP_RAM_SIZE);
		CLog::GetInstance().Print(LOG_NAME, "WriteToIop(dstAddr = 0x%0.8X, srcAddr = 0x%0.8X, size = 0x%0.8X);\r\n", 
			nDstAddr, nSrcAddr, nSize);
		if(nDstAddr >= 0 && nDstAddr <= CMD_RECVADDR)
		{
			CLog::GetInstance().Print(LOG_NAME, "Warning: Trying to DMA in Bios Control Area.\r\n");
		}
		else
		{
			memcpy(m_iopRam + nDstAddr, m_eeRam + nSrcAddr, nSize);
		}
		return nSize;
	}
}

void CSIF::SendPacket(void* packet, uint32 size)
{
{'fraction_non_alphanumeric': 0.11714739102025576, 'fraction_numerical': 0.021749276579856248, 'mean_word_length': 5.524969549330085, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5645828', 'n_tokens_mistral': 4841, 'n_tokens_neox': 4334, 'n_words': 733}