annotate demo/comm.c @ 0:e38dacceb958

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 05:53:11 -0700
parents
children
rev   line source
rlm@0 1 /* Communicates with SPC the way the SNES would.
rlm@0 2
rlm@0 3 Note: You'll need an "spc_rom.h" file that contains the 64-byte IPL ROM contents */
rlm@0 4
rlm@0 5 #include "snes_spc/spc.h"
rlm@0 6
rlm@0 7 #include "demo_util.h"
rlm@0 8 #include <string.h>
rlm@0 9 #include <stdio.h>
rlm@0 10
rlm@0 11 static SNES_SPC* snes_spc;
rlm@0 12
rlm@0 13 /* Make port access more convenient. Fakes time by simply incrementing it each call. */
rlm@0 14 static spc_time_t stime;
rlm@0 15 static int pread ( int port ) { return spc_read_port( snes_spc, stime++, port ); }
rlm@0 16 static void pwrite( int port, int data ) { spc_write_port( snes_spc, stime++, port, data ); }
rlm@0 17
rlm@0 18 static unsigned char const spc_rom [spc_rom_size] = {
rlm@0 19 /* ROM data not provided with emulator */
rlm@0 20 #include "spc_rom.h"
rlm@0 21 };
rlm@0 22
rlm@0 23 int main()
rlm@0 24 {
rlm@0 25 int i;
rlm@0 26
rlm@0 27 /* Data to upload */
rlm@0 28 static unsigned char const data [4] = "\xFA\xDE\xD1";
rlm@0 29 unsigned const data_addr = 0xF5; /* second I/O port */
rlm@0 30
rlm@0 31 snes_spc = spc_new();
rlm@0 32 if ( !snes_spc ) error( "Out of memory" );
rlm@0 33 spc_init_rom( snes_spc, spc_rom );
rlm@0 34 spc_reset( snes_spc );
rlm@0 35
rlm@0 36 /* Simulate reads and writes that SNES code would do */
rlm@0 37
rlm@0 38 /* Wait for SPC to be ready */
rlm@0 39 while ( pread( 0 ) != 0xAA || pread( 1 ) != 0xBB ) { }
rlm@0 40
rlm@0 41 /* Start address */
rlm@0 42 pwrite( 2, data_addr & 0xFF );
rlm@0 43 pwrite( 3, data_addr >> 8 );
rlm@0 44
rlm@0 45 /* Tell SPC to start transfer and wait for acknowledgement */
rlm@0 46 pwrite( 0, 0xCC );
rlm@0 47 pwrite( 1, 0x01 );
rlm@0 48 while ( pread( 0 ) != 0xCC ) { }
rlm@0 49
rlm@0 50 /* Send each byte and wait for acknowledgement */
rlm@0 51 for ( i = 0; i < 4; i++ )
rlm@0 52 {
rlm@0 53 printf( "%02X ", data [i] );
rlm@0 54 pwrite( 1, data [i] );
rlm@0 55 pwrite( 0, i );
rlm@0 56 while ( pread( 0 ) != i ) { }
rlm@0 57 }
rlm@0 58 printf( "\n" );
rlm@0 59
rlm@0 60 /* Verify that data was transferred properly */
rlm@0 61 for ( i = 0; i < 3; i++ )
rlm@0 62 printf( "%02X ", pread( i + 1 ) );
rlm@0 63 printf( "\n" );
rlm@0 64
rlm@0 65 printf( "Cycles: %ld\n", (long) stime );
rlm@0 66
rlm@0 67 spc_delete( snes_spc );
rlm@0 68
rlm@0 69 return 0;
rlm@0 70 }