/* ALink85 by Shawn D'Alimonte (aa600@torfree.net)                      */
/* Based on:                                                            */
/* XLink85  -  TI85<->PC  link  program,  Copyright (c) 1996 Jani Halme	*/
/* My work is mostly in tiport.c, which is the low level port access    */
/* routines.  The rest is by Jani Halme, but has been reorganized.      */
/*                                                                      */
/* !!!!!!!!!!!!!!!!!!!!!!!!!WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!       */
/*                                                                      */
/* This program uses the CIA registers directly without telling the OS  */
/* Therefore it will interfere with and be interefered by any serial and*/
/* possiblly parallel port activity.  Also it will not function on any  */
/* possible future Amiga's unless the serial port handshaking bits stay */
/* put.  It will not work with any add-on serial cards                  */
/*									*/
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License or (at */
/* your option) any later version.					*/
/*									*/
/* 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;  it  now,  write  to  the  Free Software	*/
/* Foundation, Inc., Mass Ave, Cambridge, MA02139.			*/
/*									*/  
/* The author can be contacted by email at:aa600@torfree.net*/
									
#include "packet.h"
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "tiport.h"



unsigned char *buffer=NULL;
unsigned short last_packet_size;


			
/* send a TI-85 packet */
void put_packet(UBYTE packet_type, UWORD dataword)
{
	UWORD checksum, i;

	send(ID_COMPUTER);
	send(packet_type);
#ifdef DBG
	printf("dataword: %x\n",dataword);
#endif
	send(dataword >> 8);
	send(dataword & 0xff);
	if (dataword!=0 && (packet_type==PAK_VARHEADER || packet_type==PAK_DATAPART || packet_type==PAK_SKIPVAR))
	{
		checksum=0;
		for(i=0;i<WORDSWAP(dataword);i++)
		{
			send(buffer[i]);
			checksum+=buffer[i];
		}
		checksum=checksum & 65535;
		send(checksum & 0xff);
		send(checksum >> 8);
#ifdef DBG
		printf("Packet sent.  Waiting for acknowledgement.\n");
#endif
		if (get_packet()!=PAK_ACKNOWLEDGE) 
		{
			printf("Packet not acknowledged, exiting...\n");
			exit(1);
		} 
	}
} 


/* receive a TI-85 packet */
unsigned char get_packet(void)
{
	unsigned char calc_type, packet_type;
	unsigned short checksum, dataword, i;

	calc_type=recv();
	packet_type=recv();
#ifdef DEBUG
	printf("In get_packet: calctype=%x\nPacket type is %x\n",calc_type,packet_type);
#endif
	dataword=recv();
	dataword=(recv()<<8)|dataword;
	if (dataword!=0 && (packet_type==PAK_VARHEADER || packet_type==PAK_DATAPART || packet_type==PAK_SKIPVAR))
	{
		checksum=0;
		for(i=0;i<dataword;i++)
		{
			buffer[i]=recv();
			checksum+=buffer[i];
		}
		checksum=checksum & 65535;
		checksum-=recv();
		checksum-=recv()<<8;

		if (checksum!=0)
		{
			/* wont work for some reason.. */
			printf("\nChecksum failure!\n");
			put_packet(PAK_REQRESEND, 0);
			get_packet();
		}
		put_packet(PAK_ACKNOWLEDGE, 0); 
	}
	last_packet_size=dataword;
	
	return packet_type;
}		











