/*
 *  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 "font.h"
#include "geckomem.h"
#include "geckolowlevel.h"

#define packetsize 63488;
#define bufferstart 0x80100000
#define bufferend 0x80300000
#define sramsize 64
#define bios_size 0x200000

dvdcmdblk cmdblk;

extern u32 __sram_read(void *buffer);

/*---------------------------------------------------------------------------*
    Name:               commands_dumpwiibiosfonts
    Description:        Dump Bios Fonts
*---------------------------------------------------------------------------*/
u32 commands_dumpwiibiosfonts()
{
	u8 ack_packet = 0xAA;
	u8 dumped_packet = 0xCC;
	u32 i;

	memset((void*)bufferstart, 0, bios_size);
	
	usb_sendbuffer_safe(EXI_CHANNEL_1,&ack_packet,1);

    for (i=0; i < 0x200000; i+=0x400) {
        exi_chan0sr = 0x150;   
        exi_chan0data = i<<6;
        exi_chan0cr	 = 0x35;
        while((exi_chan0cr)&1);
		exi_chan0dmasta = (int)bufferstart+i;
        exi_chan0dmalen = 0x400;
        exi_chan0cr	 = 3; 
        while((exi_chan0cr)&1);
        exi_chan0sr = 0x0;
	}

	DCFlushRange((void*)bufferstart, bios_size);

	usb_sendbuffer_safe(EXI_CHANNEL_1,&dumped_packet,1);
	gecko_readrange(bufferstart, bufferend);
	write_font(185, 154, "Dump Complete");
	return 1;
}


/*---------------------------------------------------------------------------*
    Name:               commands_dumpsram()
    Description:        Dump the SRAM
*---------------------------------------------------------------------------*/

int commands_dumpsram()
{
	u8 ack_packet = 0xAA;
	u8 fail_packet = 0xFF;
	static char srambuffer[128] ATTRIBUTE_ALIGN(32);
	u32 ret;

	memset(srambuffer, 0, 64);

	ret = __sram_read(srambuffer);
	if(!ret){
		usb_sendbuffer_safe(EXI_CHANNEL_1,&fail_packet,1);
		write_font(185, 154, "SRAM Failed To Read");
		return 0;
	}

	usb_sendbuffer_safe(EXI_CHANNEL_1,&ack_packet,1);
	usb_sendbuffer_safe(EXI_CHANNEL_1,&srambuffer, 64);
	write_font(185, 154, "Dump Comples");
	return 1;
}
