diff demo/comm.c @ 0:e38dacceb958

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 05:53:11 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/comm.c	Fri Oct 21 05:53:11 2011 -0700
     1.3 @@ -0,0 +1,70 @@
     1.4 +/* Communicates with SPC the way the SNES would.
     1.5 +
     1.6 +Note: You'll need an "spc_rom.h" file that contains the 64-byte IPL ROM contents */
     1.7 +
     1.8 +#include "snes_spc/spc.h"
     1.9 +
    1.10 +#include "demo_util.h"
    1.11 +#include <string.h>
    1.12 +#include <stdio.h>
    1.13 +
    1.14 +static SNES_SPC* snes_spc;
    1.15 +
    1.16 +/* Make port access more convenient. Fakes time by simply incrementing it each call. */
    1.17 +static spc_time_t stime;
    1.18 +static int  pread ( int port )           { return spc_read_port( snes_spc, stime++, port ); }
    1.19 +static void pwrite( int port, int data ) { spc_write_port( snes_spc, stime++, port, data ); }
    1.20 +
    1.21 +static unsigned char const spc_rom [spc_rom_size] = {
    1.22 +	/* ROM data not provided with emulator */
    1.23 +	#include "spc_rom.h"
    1.24 +};
    1.25 +
    1.26 +int main()
    1.27 +{
    1.28 +	int i;
    1.29 +	
    1.30 +	/* Data to upload */
    1.31 +	static unsigned char const data [4] = "\xFA\xDE\xD1";
    1.32 +	unsigned const data_addr = 0xF5; /* second I/O port */
    1.33 +	
    1.34 +	snes_spc = spc_new();
    1.35 +	if ( !snes_spc ) error( "Out of memory" );
    1.36 +	spc_init_rom( snes_spc, spc_rom );
    1.37 +	spc_reset( snes_spc );
    1.38 +	
    1.39 +	/* Simulate reads and writes that SNES code would do */
    1.40 +	
    1.41 +	/* Wait for SPC to be ready */
    1.42 +	while ( pread( 0 ) != 0xAA || pread( 1 ) != 0xBB ) { }
    1.43 +	
    1.44 +	/* Start address */
    1.45 +	pwrite( 2, data_addr & 0xFF );
    1.46 +	pwrite( 3, data_addr >> 8 );
    1.47 +	
    1.48 +	/* Tell SPC to start transfer and wait for acknowledgement */
    1.49 +	pwrite( 0, 0xCC );
    1.50 +	pwrite( 1, 0x01 );
    1.51 +	while ( pread( 0 ) != 0xCC ) { }
    1.52 +	
    1.53 +	/* Send each byte and wait for acknowledgement */
    1.54 +	for ( i = 0; i < 4; i++ )
    1.55 +	{
    1.56 +		printf( "%02X ", data [i] );
    1.57 +		pwrite( 1, data [i] );
    1.58 +		pwrite( 0, i );
    1.59 +		while ( pread( 0 ) != i ) { }
    1.60 +	}
    1.61 +	printf( "\n" );
    1.62 +	
    1.63 +	/* Verify that data was transferred properly */
    1.64 +	for ( i = 0; i < 3; i++ )
    1.65 +		printf( "%02X ", pread( i + 1 ) );
    1.66 +	printf( "\n" );
    1.67 +	
    1.68 +	printf( "Cycles: %ld\n", (long) stime );
    1.69 +	
    1.70 +	spc_delete( snes_spc );
    1.71 +	
    1.72 +	return 0;
    1.73 +}