#include "common.h"
#include <string.h>

#ifdef ENDIAN_CORRECT_BY_FUNC
DWORD htonl(DWORD d) {
	DWORD rtn=0;
	rtn |= ((d&0xFF000000UL)>>24);
	rtn |= ((d&0x00FF0000UL)>> 8);
  rtn |= ((d&0x0000FF00UL)<< 8);
	rtn |= ((d&0x000000FFUL)<<24);
	return rtn;
}

unsigned htons(unsigned w) {
	unsigned rtn=0;
	rtn |= ((w&0xFF00u) >> 8);
	rtn |= ((w&0x00FFu) << 8);
	return rtn;
}
#endif

void wait_10n4clk(unsigned char i){
  while(i--);
/*_asm
start_wait_10n4:
  mov r2,dpl            ; 2clk
  mov ar3,r2            ; 2clk
  dec r2                ; 1clk
  mov a,r3              ; 1clk
  jnz start_wait_10n4   ; 2/4clk
_endasm;*/
  //ret                 ; 6clk
}

/**
 * Delay function with declared wait time in microseconds
 * 
 * @param count time in ns
 */
void wait_us(unsigned int count){
  while(count--) wait_10n4clk(2); // (2+2+1+1+4)*2 - (4-2) + 6 = 24clk
}

/**
 * Delay function with declared wait time in milliseconds
 * 
 * @param count - time in ms
 */
void wait_ms(unsigned int count){
  int i;
  for(i = 0; i < count; i++) wait_us(1000);
}
