/*
 *  Copyright (C) 2008 Nuke (wiinuke@gmail.com)
 *
 *  this file is part of GeckoOS for USB Gecko
 *  http://www.usbgecko.com
 *
 *  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; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <gctypes.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <gccore.h>	
#include <malloc.h>
#include "tools.h"
#include "geckomem.h"

#define packetsize 63488

int gecko_readmem()
{
	u8 startaddress[4];
	u8 endaddress[5];
	u8 ack_packet = 0xAA;
	u8 pcresponse;
	static u8 packetbuffer[packetsize] ATTRIBUTE_ALIGN(32);
	u32 memstart;
	u32 memend;
	u32 memrange;
	u32 fullpackets;
	u32 currentpacket;
	u32 lastpacketbytes;

	// ACK
	usb_sendbuffer_safe(EXI_CHANNEL_1,&ack_packet,1);
	usb_recvbuffer_safe(EXI_CHANNEL_1,&startaddress,4);		
	usb_recvbuffer_safe(EXI_CHANNEL_1,&endaddress,4);		
	
	memstart = be32(startaddress);	
	memend = be32(endaddress);	
	memrange = memend - memstart;

	fullpackets = memrange / packetsize;						
	lastpacketbytes = memrange % packetsize;					

	// Full Packet Size
	for (currentpacket = 0;currentpacket < fullpackets;)
	{	
		memcpy(packetbuffer, (char*)memstart, packetsize);
		usb_sendbuffer(EXI_CHANNEL_1,&packetbuffer, packetsize);
		usb_recvbuffer_safe(EXI_CHANNEL_1,&pcresponse,1);	
		if(pcresponse != 0xAA){								
		//	printf("failed to read packet\n");
			return 0;
		}
			memstart += packetsize;
			currentpacket++;	
	}

	// Remainder
	if(lastpacketbytes > 0) {
		memcpy(packetbuffer, (char*)memstart, lastpacketbytes);
		usb_sendbuffer_safe(EXI_CHANNEL_1,&packetbuffer, lastpacketbytes);
		usb_recvbuffer_safe(EXI_CHANNEL_1,&pcresponse,1);	
	}
	
//	printf("memory dump complete\n");
	return 1;
}

int gecko_readrange(u32 memstart, u32 memend)
{

	char pcresponse;
	static char packetbuffer[packetsize] ATTRIBUTE_ALIGN(32);
	u32 memrange;
	u32 fullpackets;
	u32 currentpacket;
	u32 lastpacketbytes;

	memrange = memend - memstart;
	
	fullpackets = memrange / packetsize;						
	lastpacketbytes = memrange % packetsize;		

	// Full Packet Size
	for (currentpacket = 0;currentpacket < fullpackets;)
	{	
		memcpy(packetbuffer, (char*)memstart, packetsize);
		usb_sendbuffer_safe(EXI_CHANNEL_1,&packetbuffer, packetsize);
		usb_recvbuffer_safe(EXI_CHANNEL_1,&pcresponse,1);	
		if(pcresponse != 0xAA){								
			return 0;
		}
			memstart += packetsize;
			currentpacket++;	
	}
	// Remainder
	if(lastpacketbytes > 0) {
		memcpy(packetbuffer, (char*)memstart, lastpacketbytes);
		usb_sendbuffer_safe(EXI_CHANNEL_1,&packetbuffer, lastpacketbytes);
		
	}
	return 1;
}
