#include "plugin_api.h"

//-----------------------------------------------------------
// Some Useful Functions
//-----------------------------------------------------------
//

static __forceinline void
helper_plugin_cntr_zero( int counter_id ) {
    get_pcntr( pluginId, counter_id );
    WU_loadGlobalRegister( stats_regnum, 0, stats_cerr );
}

static __forceinline void
helper_ultohex_lmem(
	__declspec(local_mem) unsigned int val,
	__declspec(local_mem) char *val_str )
{
    __declspec(local_mem) char zero_str[2] = "0";
    if( val > 0 )	onl_api_int2str( val, val_str );
    else		strcpy_lmem( val_str, zero_str );
}

//						<<< helper_ultodec_lmem >>>
//
// convert the unsigned long 'val' to a decimal string and copy to 'p'
//
//XXX static __forceinline void
void
helper_ultodec_lmem(
		__declspec(local_mem) unsigned long val,
		__declspec(local_mem) char *p )
{
    __declspec(local_mem)	char tmp[12];	// work area
    __declspec(local_mem)	char *tptr;	// point into tmp[]
    __declspec(local_mem)	char *pp;

    tptr = &(tmp[11]);
    if( val == 0 ) {		// handle 0 value
        *tptr = '0';
	--tptr;
    } else {
	do {			// produce digits
	    if( val > 0 ) {
		*tptr = '0' + val%10;
		val = val/10;
		--tptr;
	    } else {
		break;
	    }
	} while( tptr > tmp );
    }
    // characters (without '\0') are now right justified in tmp[]

    pp = p;
    while( tptr < tmp+11 ) {	// copy digits out
	++tptr;
    	*pp = *tptr;
	++pp;
    }
    *pp = '\0';
}

/*
// output debug msg with 2 args (string, unsigned long)
//	the c-string cstr can not be more than 28 bytes long
//
void
helper_sram_dbgmsg_str_1ul( char *cstr, unsigned long xval ) {
    __declspec(sram) char sram_dbgmsg_buf[28];
    __declspec(local_mem) char lmem_dbgmsg_buf[28];
    __declspec(gp_reg)	int n;

    n = strlen_sram( cstr );
    strcpy_sram( sram_dbgmsg_buf, cstr );
    sram_dbgmsg_buf[n] = ' ';
    sram_dbgmsg_buf[n+1] = '\0';
    ++n;
    helper_ultoa_sram( xval, sram_dbgmsg_buf+n, 28-n );

    memcpy_lmem_sram( lmem_dbgmsg_buf, (void *)sram_dbgmsg_buf, 28 );
    onl_api_debug_message( msgNextBlock, lmem_dbgmsg_buf );
}*/
