external.c 1.88 KB
/*************************************************************************
 *
 *  File: external.c
 * 
 *  (1) This file, to write data into ddr via backdoor at beginning
 *	(2) Stimulate from external test bench.
 *      
 */
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>

#include "iomap.h"
#include "iotest.h"
#include "sim.h"
#include "simipc.h"
#include "bcp_util.h"
#include "drv_test.h"

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	Load rom file into DDR at the beginning
    Please be awared that it can be done before DDR is initialized
	N64 rom file have 4K header at the beginning.
	(please refer to tools/romdump.c)

	return uploading length
 */
int load_rom_into_ddr(char *romfile) 
{
	FILE *f;
	int  len = 0, i;
	unsigned int data, ddr_offset;
	V_INT   vdata[8];

	if (romfile == NULL || strlen(romfile)==0) 
		return 0;

	if ((f = fopen(romfile, "rb")) == NULL) 
		return 0;

	do_keep_alive_socket(1);
	fseek(f, BOOT_ADDR, SEEK_SET);
	for (i=ddr_offset=0; i<4; i++)
		ddr_offset = (ddr_offset << 8) | (fgetc(f) & 0xff);
	ddr_offset = N64_TO_PHYS(ddr_offset); 

	fseek(f, N64_HEADER, SEEK_SET);
	memset(vdata, 0, sizeof(V_INT)*8);
	for (i=0; i<MEM_SIZE; i+=8*sizeof(int)) 
		BD_V_IO_BWRITE(i, BLK_8WORD, BD_REQ_NO_IPC_MSG, vdata);

	while (!feof(f)) {
		/* reading 4 bytes */
		for (i=data=0; i<4; i++) 
			data = (data << 8) | (fgetc(f) & 0xff);
		if (feof(f)) break;

		BD_IO_WRITE(ddr_offset+len, data);
		len += 4;
	}

	do_keep_alive_socket(0);
	fclose(f);

	data = BD_IO_READ(0x450);

	return len;
}
			

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	stimulate external USB bus via backdoor
 */
int stimulate_usb(int opts)
{
	/* So for, just let it keep running */
	do_keep_alive_socket(1);
	for (; ;) BCP_STALL(4);
	do_keep_alive_socket(0);
}