// @(#) $Header: /home/abrown/public_html/first/first2002chs/software2002/simulator/RCS/pb_io.c,v 1.17 2006/12/14 21:19:03 abrown Exp $
// Allen Brown  date="20061214 13:17:48"

/***********************************************************************
  * Copyright (C) 2002  Allen Brown
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  * 
  * This program 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 General Public License for more details.
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the
  *   Free Software Foundation, Inc.
  *   59 Temple Place, Suite 330
  *   Boston, MA  02111-1307  USA
  * 
  * To contact the author of this software:
  *   Allen Brown
  *   PO Box J
  *   Corvallis, OR
  * 
  *   http://brown.armoredpenguin.com/~abrown/contact.html
  ***********************************************************************/

#include <stdio.h>
#include "pb.h"

#define MAXSTRING 80
#define pbmasktrue(width)	(1 << width)
#define pbmaskfalse(width)	(~pbmasktrue(width) & 0xff)
#define pbbittopos(value,width)	((0x01 & value) << width)
#define pbbitfrpos(value,width)	(((value & 0xff) >> width) & 0x01)
#define pbextractbit(value,width)	(width<0 ? (value & 0x01) : pbbitfrpos(value,width) )
#define pbwordorbit(value,width)	(width<0 ? (value&0xff) : pbbitfrpos(value,width))

// --------------------------------------------------------------
// pbhigh - Basic Stamp Manual p.135
void pbhigh(int valuea, int widtha){}

// --------------------------------------------------------------
// pbinput - Basic Stamp Manual p.155
void pbinput(int valuea, int widtha){}

// --------------------------------------------------------------
// pblow - Basic Stamp Manual p.187
void pblow(int valuea, int widtha){}

// --------------------------------------------------------------
// pboutput - Basic Stamp Manual p.195
void pboutput(int valuea, int widtha){}

// --------------------------------------------------------------
// pbserin - Basic Stamp Manual p.273
// The scanf variable order below was adjusted for the convenience of
// the CHS team.  It can be any order that is convenient.
int pbserin(
    pbword8_t *oi_swA,  pbword8_t oi_swA_width,
    pbword8_t *oi_swB,  pbword8_t oi_swB_width,
    pbword8_t *rc_swA,  pbword8_t rc_swA_width,
    pbword8_t *rc_swB,  pbword8_t rc_swB_width,
    pbword8_t *p2_x,  pbword8_t p2_x_width,
    pbword8_t *p1_x,  pbword8_t p1_x_width,
    pbword8_t *p4_x,  pbword8_t p4_x_width,
    pbword8_t *p3_x,  pbword8_t p3_x_width,
    pbword8_t *pb_mode,  pbword8_t pb_mode_width,
    pbword8_t *p2_y,  pbword8_t p2_y_width,
    pbword8_t *p1_y,  pbword8_t p1_y_width,
    pbword8_t *p4_y,  pbword8_t p4_y_width,
    pbword8_t *p3_y,  pbword8_t p3_y_width,
    pbword8_t *p1_wheel,  pbword8_t p1_wheel_width,
    pbword8_t *p2_wheel,  pbword8_t p2_wheel_width,
    pbword8_t *p4_wheel,  pbword8_t p4_wheel_width,
    pbword8_t *p3_wheel,  pbword8_t p3_wheel_width
  )
{
  char lineoftext[MAXSTRING+1];

  printf("pbserin: p2_x p2_y= p1_x= oi_swA= oi_swB="); fflush(stdout);
  
  if( fgets( lineoftext, MAXSTRING, stdin) == NULL )
    {
      printf("\n");
      return(-1);
    } else {
      if( BASEFORMAT == 16 )
	{
	  sscanf( lineoftext, "%x%x%x%x%x%x%x", p2_x, p2_y, p1_x, oi_swA, oi_swB, rc_swA, rc_swB);
	  printf("[p2_x=%x,p2_y=%x,p1_x=%x,oi_swA=%x,oi_swB=%x,rc_swA=%x,rc_swB=%x]**************\n",
		 *p2_x, *p2_y, *p1_x, *oi_swA, *oi_swB, *rc_swA, *rc_swB);
	} else {
	  sscanf( lineoftext, "%d%d%d%d%d%d%d", p2_x, p2_y, p1_x, oi_swA, oi_swB, rc_swA, rc_swB);
	  printf("[p2_x=%d,p2_y=%d,p1_x=%d,oi_swA=%d,oi_swB=%d,rc_swA=%d,rc_swB=%d]**************\n",
		 *p2_x, *p2_y, *p1_x, *oi_swA, *oi_swB, *rc_swA, *rc_swB);
	}
    }
  return(0);
} // pbserin

// --------------------------------------------------------------
// pbserout - Basic Stamp Manual p.293
void pbserout(
    pbword8_t dummy1,  pbword8_t dummy1_width,
    pbword8_t dummy2,  pbword8_t dummy2_width,
    pbword8_t field3,  pbword8_t field3_width,
    pbword8_t field4,  pbword8_t field4_width,
    pbword8_t field5,  pbword8_t field5_width,
    pbword8_t field6,  pbword8_t field6_width,
    pbword8_t field7,  pbword8_t field7_width,
    pbword8_t field8,  pbword8_t field8_width,
    pbword8_t field9,  pbword8_t field9_width,
    pbword8_t field10,  pbword8_t field10_width,
    pbword8_t field11,  pbword8_t field11_width,
    pbword8_t field12,  pbword8_t field12_width,
    pbword8_t field13,  pbword8_t field13_width,
    pbword8_t field14,  pbword8_t field14_width,
    pbword8_t field15,  pbword8_t field15_width,
    pbword8_t field16,  pbword8_t field16_width,
    pbword8_t field17,  pbword8_t field17_width,
    pbword8_t field18,  pbword8_t field18_width,
    pbword8_t field19,  pbword8_t field19_width,
    pbword8_t field20,  pbword8_t field20_width,
    pbword8_t dummy3,  pbword8_t dummy3_width,
    pbword8_t dummy4,  pbword8_t dummy4_width,
    pbword8_t dummy5,  pbword8_t dummy5_width,
    pbword8_t dummy6,  pbword8_t dummy6_width
  )
{
  if( BASEFORMAT == 16 )
    {
      printf("pbserout: fld3=%x fld4=%x fld5=%x fld6=%x fld7=%x fld8=%x fld9=%x fld10=%x fld11=%x fld12=%x fld13=%x fld14=%x fld15=%x fld16=%x fld17=%x fld18=%x fld19=%x fld20=%x\n",
	     field3, field4, field5, field6, field7, field8, field9, field10,
	     field11, field12, field13, field14, field15, field16, field17,
	     field18, field19, field20
	     );
    } else {
      printf("pbserout: fld3=%d fld4=%d fld5=%d fld6=%d fld7=%d fld8=%d fld9=%d fld10=%d fld11=%d fld12=%d fld13=%d fld14=%d fld15=%d fld16=%d fld17=%d fld18=%d fld19=%d fld20=%d\n",
	     field3, field4, field5, field6, field7, field8, field9, field10,
	     field11, field12, field13, field14, field15, field16, field17,
	     field18, field19, field20
	     );
    }
  fflush(stdout);
} // pbserout

// --------------------------------------------------------------
// pbshiftout - Basic Stamp Manual p.313
void pbshiftout(){}

// --------------------------------------------------------------
// pbstop - Basic Stamp Manual p.321
void pbstop(){}

// --------------------------------------------------------------
// pbtoggle - Basic Stamp Manual p.327
void pbtoggle(int valuea, int widtha){}

// --------------------------------------------------------------
/* TODO:
 * - Flesh out more IO funcs.
 */

