view demo/comm.c @ 1:2ad543b339f1

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