rdbsend.c 3.21 KB
#include "os.h"
#include "os_internal.h"
#include "rdb.h"
#include "R4300.h"
#ifdef BBPLAYER
#include "bbint.h"
#include "bbdebug.h"
#endif

#ifndef _FINALROM
extern rdbPacket * __osRdb_IP6_Data;
extern u32    __osRdb_IP6_Size;
extern u32    __osRdb_IP6_Ct;
extern u32    __osRdb_IP6_CurWrite;
u32           __osRdb_Usb_Active = 0;
extern u32    __osRdb_Usb_GetAvailSendBytes();
void   (*__osRdb_Usb_StartSend)(void *, int);

#ifdef BBPLAYER
extern u32    __osRdb_IP6_Empty;
#else
u32    __osRdb_IP6_Empty = 1;
#endif

#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif

#ifdef BBPLAYER
/* Check if the IDE connection is present */
#define PROBE_IDE_RDB() \
	(IDE_WRITE(IDE_RDB_RDATA_REG+0,0xbabe), \
	 IDE_WRITE(IDE_RDB_RDATA_REG+2,0xcafe), \
	 ((IDE_READ(IDE_RDB_RDATA_REG+0) == 0xbabe) & \
	  (IDE_READ(IDE_RDB_RDATA_REG+2) == 0xcafe)))
#endif

u32 __osRdbSend(u8 *buf, u32 size, u32 type)
{
    rdbPacket   *pPtr, pkt;
    u32         len, c, inCt = 0;
    u32         needFirst = 0;
    u32         mask;
    u32         sent = 0;

#ifdef BBPLAYER
    /* call usb initialization automatically */
    if (__osRdb_IP6_Data == NULL) {
	osBbUsbInit();
	/* if IDE is present, it overrides USB */
	if (PROBE_IDE_RDB())
	    __osRdb_Usb_Active = 0;
    }
#endif

    mask = __osDisableInt();
    
    if(__osRdb_IP6_Empty)    /* if there are no transmissions in progress */
    {
#ifdef BBPLAYER
	    if (__osRdb_Usb_Active) {
            if (__osRdb_Usb_GetAvailSendBytes() < sizeof pkt) {
                /* Not enough space in RDBS - wait until next time */
                __osRestoreInt(mask);
                return 0;
            }
        }
#endif

	__osRdb_IP6_Empty = 0;      /* mark __osRdb_IP6_Empty as FALSE */
	pkt.type = (unsigned int)type;
	len = MIN(size,3);
	pkt.length = (unsigned int)len;
	c = 0;
	while(c < len)
	    pkt.buf[c++] = buf[inCt++];
    /* Fill the rest with 0 (better debugging) */
    while(c < 3)
        pkt.buf[c++] = 0;
	
	size -= len;
	sent = len;
	needFirst = 1;
    }
    
    /*
     * If there isn't room in the buffer, don't try and send in this block.
     * return with the number sent, and let application reenter call.
     */
    while((size > 0) && (__osRdb_IP6_Ct < __osRdb_IP6_Size))
    {
	len = MIN(size,3);
	pPtr = &__osRdb_IP6_Data[__osRdb_IP6_CurWrite];
	pPtr->type = (unsigned int)type;
	pPtr->length = (unsigned int)len;
	c = 0;
	while(c < len)
	    pPtr->buf[c++] = buf[inCt++];
    /* Fill the rest with 0 (better debugging) */
    while(c < 3)
        pPtr->buf[c++] = 0;
	
	__osRdb_IP6_CurWrite++;
	if(__osRdb_IP6_CurWrite >= __osRdb_IP6_Size)
	    __osRdb_IP6_CurWrite = 0;
	__osRdb_IP6_Ct++;
	size -= len;
	sent += len;
    }

    if(needFirst)
    {
#ifdef BBPLAYER
	if (__osBbIsBb) {
	    if (__osRdb_Usb_Active) {
		(*__osRdb_Usb_StartSend)(&pkt, sizeof pkt);
	    } else {
	        IDE_WRITE(IDE_RDB_RDATA_REG, *((u32 *) &pkt)>>16);
	        IDE_WRITE(IDE_RDB_RDATA_REG+2, *((u32 *) &pkt));
	        IDE_WRITE(IDE_RDB_CTRL_REG, IDE_RDB_CTRL_SET_BB_REQ);
	    }
	} else
#endif
	*((u32 *) RDB_BASE_REG) = *((u32 *) &pkt); 
    }
#ifdef BBPLAYER
    else if (__osBbIsBb && __osRdb_Usb_Active)
	(*__osRdb_Usb_StartSend)(NULL, 0);
#endif
   
    __osRestoreInt(mask);

    return(sent);
}

#endif /* ifndef _FINALROM */