comparison demo/comm.c @ 0:e38dacceb958

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 05:53:11 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e38dacceb958
1 /* Communicates with SPC the way the SNES would.
2
3 Note: You'll need an "spc_rom.h" file that contains the 64-byte IPL ROM contents */
4
5 #include "snes_spc/spc.h"
6
7 #include "demo_util.h"
8 #include <string.h>
9 #include <stdio.h>
10
11 static SNES_SPC* snes_spc;
12
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 ); }
17
18 static unsigned char const spc_rom [spc_rom_size] = {
19 /* ROM data not provided with emulator */
20 #include "spc_rom.h"
21 };
22
23 int main()
24 {
25 int i;
26
27 /* Data to upload */
28 static unsigned char const data [4] = "\xFA\xDE\xD1";
29 unsigned const data_addr = 0xF5; /* second I/O port */
30
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 );
35
36 /* Simulate reads and writes that SNES code would do */
37
38 /* Wait for SPC to be ready */
39 while ( pread( 0 ) != 0xAA || pread( 1 ) != 0xBB ) { }
40
41 /* Start address */
42 pwrite( 2, data_addr & 0xFF );
43 pwrite( 3, data_addr >> 8 );
44
45 /* Tell SPC to start transfer and wait for acknowledgement */
46 pwrite( 0, 0xCC );
47 pwrite( 1, 0x01 );
48 while ( pread( 0 ) != 0xCC ) { }
49
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" );
59
60 /* Verify that data was transferred properly */
61 for ( i = 0; i < 3; i++ )
62 printf( "%02X ", pread( i + 1 ) );
63 printf( "\n" );
64
65 printf( "Cycles: %ld\n", (long) stime );
66
67 spc_delete( snes_spc );
68
69 return 0;
70 }