/*
 * Copyright (c) 2019 Clementine Computing LLC.
 *
 * This file is part of PopuFare.
 *
 * PopuFare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PopuFare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with PopuFare.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

#include "byte_access.h"

// LSB first!
//
unsigned short int _usi(void *p)
{
  unsigned short int usi = 0;
  unsigned char c;

  c = (unsigned char)(*((unsigned char *)p));
  usi |= c;

  c = (unsigned char)(*((unsigned char *)(p+1)));
  usi |= (c<<8);

  return usi;
}

unsigned long int _uli(void *p)
{
  unsigned long int uli = 0;
  unsigned char c;

  c = (unsigned char)(*((unsigned char *)p));
  uli |= c;

  c = (unsigned char)(*((unsigned char *)(p+1)));
  uli |= (c<<8);

  c = (unsigned char)(*((unsigned char *)(p+2)));
  uli |= (c<<16);

  c = (unsigned char)(*((unsigned char *)(p+3)));
  uli |= (c<<24);

  return uli;
}

unsigned long long int _ulli(void *p)
{
  unsigned long long int ulli = 0;
  unsigned long long int t;
  unsigned char c;

  c = (unsigned char)(*((unsigned char *)p));
  ulli |= c;

  c = (unsigned char)(*((unsigned char *)(p+1)));
  t = c;
  ulli |= (t<<8);

  c = (unsigned char)(*((unsigned char *)(p+2)));
  t = c;
  ulli |= (t<<16);

  c = (unsigned char)(*((unsigned char *)(p+3)));
  t = c;
  ulli |= (t<<24);

  c = (unsigned char)(*((unsigned char *)(p+4)));
  t = c;
  ulli |= (t<<32);

  c = (unsigned char)(*((unsigned char *)(p+5)));
  t = c;
  ulli |= (t<<40);

  c = (unsigned char)(*((unsigned char *)(p+6)));
  t = c;
  ulli |= (t<<48);

  c = (unsigned char)(*((unsigned char *)(p+7)));
  t = c;
  ulli |= (t<<56);

  return ulli;
}


void _usiw(void *p, unsigned short int usi)
{
  unsigned char c;
  unsigned char *chp;

  chp = p;

  c = usi & 0xff;
  *chp++ = c;

  c = (usi >> 8) & 0xff;
  *chp++ = c;

}

void _uliw(void *p, unsigned long int uli)
{
  unsigned char c;
  unsigned char *chp;

  chp = p;

  c = uli & 0xff;
  *chp++ = c;

  c = (uli >> 8) & 0xff;
  *chp++ = c;

  c = (uli >> 16) & 0xff;
  *chp++ = c;

  c = (uli >> 24) & 0xff;
  *chp++ = c;

}

void _ulliw(void *p, unsigned long long int ulli)
{
  unsigned char c;
  unsigned char *chp;

  chp = p;

  c = ulli & 0xff;
  *chp++ = c;

  c = (ulli >> 8) & 0xff;
  *chp++ = c;

  c = (ulli >> 16) & 0xff;
  *chp++ = c;

  c = (ulli >> 24) & 0xff;
  *chp++ = c;

  c = (ulli >> 32) & 0xff;
  *chp++ = c;

  c = (ulli >> 40) & 0xff;
  *chp++ = c;

  c = (ulli >> 48) & 0xff;
  *chp++ = c;

  c = (ulli >> 56) & 0xff;
  *chp++ = c;

}



