Advertisement
Guest User

Wookie

a guest
Mar 3rd, 2009
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <termio.h>
  7. #include <strings.h>
  8. #define _POSIX_SOURCE 1
  9.  
  10. #define DATAPORT "/dev/ttyUSB0"
  11. #define CONTROLPORT "/dev/ttyUSB1"
  12. #define SLEEP_SHORT 100000
  13. #define SLEEP_LONG 1000000
  14.  
  15. int data_fd, control_fd;
  16.  
  17. int open_port()
  18. {
  19.     // open the data channel
  20.     data_fd = open(DATAPORT, O_RDWR  | O_NOCTTY | O_NDELAY);
  21.  
  22.     if (data_fd == -1) {
  23.         printf("Error opening data channel!\n");
  24.         exit(-1);
  25.     } else {
  26.         printf("Data channel open.\n");
  27.     }
  28.  
  29.     // open the control channel
  30.     control_fd = open(CONTROLPORT, O_RDWR | O_NOCTTY | O_NDELAY);
  31.  
  32.     if (control_fd == -1) {
  33.         close(data_fd);
  34.         printf("Error opening control channel!\n");
  35.         exit(-1);
  36.     } else {
  37.         printf("Control channel open.\n");
  38.     }
  39.     return 0;
  40. }
  41.  
  42. int init_port()
  43. {
  44.     // flush I/O buffers on both serial devices
  45.     tcflush(data_fd, TCIOFLUSH);
  46.     tcflush(control_fd, TCIOFLUSH);
  47.     return 0;
  48. }
  49.  
  50. int close_port()
  51. {
  52.     close(data_fd);
  53.     close(control_fd);
  54.     return 0;
  55. }
  56.  
  57. #define RESET_COPYMODE 0
  58. #define RESET_PLAYMODE 1
  59. #define RESET_ALTPORT  2
  60. #define RESET_NORESET  4
  61.  
  62. int reset_copy_nes(int mode)
  63. {
  64.     int status = 0;
  65.  
  66.     if(mode & RESET_PLAYMODE)
  67.     {
  68.         // clr /RTS=1
  69.         ioctl(control_fd, TIOCMGET, &status);
  70.         status &= ~TIOCM_RTS;
  71.         ioctl(control_fd, TIOCMSET, &status);
  72.     }
  73.     else
  74.     {
  75.         // set /RTS=0
  76.         ioctl(control_fd, TIOCMGET, &status);
  77.         status |= TIOCM_RTS;
  78.         ioctl(control_fd, TIOCMSET, &status);
  79.  
  80.     }
  81.    
  82.  
  83.     if(!(mode & RESET_NORESET))
  84.     {
  85.         // pull /RESET low    clear D2
  86.         // set /DTR=0
  87.         ioctl(control_fd, TIOCMGET, &status);
  88.         status &= ~TIOCM_DTR;
  89.         ioctl(control_fd, TIOCMSET, &status);
  90.         usleep(SLEEP_SHORT);
  91.     }
  92.    
  93.     // pull /RESET high       set D2
  94.     // clr /DTR=1
  95.     ioctl(control_fd, TIOCMGET, &status);
  96.     status |= TIOCM_DTR;
  97.     ioctl(control_fd, TIOCMSET, &status);
  98.    
  99.     usleep(SLEEP_SHORT);
  100.     init_port();
  101.     usleep(SLEEP_SHORT);
  102.    
  103.     return 0;
  104. }
  105.  
  106. int get_version()
  107. {
  108.     int bytes, i;
  109.     unsigned char a = 0xA1;
  110.     char buf[255];
  111.    
  112.     bytes = write(data_fd, &a, 1);
  113.  
  114.     if (bytes != 1) {
  115.         printf("Error sending request: %d (%s)\n", bytes, strerror(errno));
  116.         return -1;
  117.     } else {
  118.         usleep(SLEEP_SHORT);
  119.        
  120.         // zero out the buffer
  121.         bzero(buf, 255);
  122.        
  123.         // read the bytes
  124.         for(i = 0; i < 255; i++) {
  125.             bytes = read(data_fd, &buf[i], 1);
  126.        
  127.             if(bytes <= 0)
  128.                 break;
  129.         }
  130.        
  131.         // output the version string
  132.         printf("Version: %s\n", buf);
  133.         printf("bytes read: %d\n", i);
  134.     }
  135.    
  136.     return 0;
  137. }
  138.  
  139. int play_cart()
  140. {
  141.     reset_copy_nes(RESET_PLAYMODE);
  142.     printf("Press enter when done playing.\n");
  143.     getchar();
  144.     reset_copy_nes(RESET_COPYMODE);
  145.     return 0;
  146. }
  147.  
  148. int main(int argc, char** argv)
  149. {
  150.     if(argc < 2)
  151.     {
  152.         printf("Usage: %s <version|play>\n", argv[0]);
  153.         exit(0);
  154.     }
  155.    
  156.     open_port();
  157.  
  158.     init_port();
  159.    
  160.     if(strcasecmp(argv[1], "version") == 0)
  161.     {
  162.         get_version();
  163.     }
  164.     else if(strcasecmp(argv[1], "play") == 0)
  165.     {
  166.         play_cart();
  167.     }
  168.     else
  169.     {
  170.         printf("unknown command\n");
  171.     }
  172.    
  173.     // must clean up...
  174.     close_port();
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement