Advertisement
Guest User

Wookie

a guest
Mar 3rd, 2009
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 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.     int status = 0;
  45.    
  46.     // check to make sure the NES is on
  47.     ioctl(control_fd, TIOCMGET, &status);
  48.    
  49.     while(status & TIOCM_CAR)
  50.     {
  51.         printf("NES is OFF - turn it on, then hit enter\n");
  52.         getchar();
  53.         ioctl(control_fd, TIOCMGET, &status);
  54.     }
  55.  
  56.     // flush I/O buffers on both serial devices
  57.     tcflush(data_fd, TCIOFLUSH);
  58.     tcflush(control_fd, TCIOFLUSH);
  59.     return 0;
  60. }
  61.  
  62. int close_port()
  63. {
  64.     close(data_fd);
  65.     close(control_fd);
  66.     return 0;
  67. }
  68.  
  69. #define RESET_COPYMODE 0
  70. #define RESET_PLAYMODE 1
  71. #define RESET_ALTPORT  2
  72. #define RESET_NORESET  4
  73.  
  74. int reset_copy_nes(int mode)
  75. {
  76.     int status = 0;
  77.  
  78.     if(mode & RESET_PLAYMODE)
  79.     {
  80.         // clr /RTS=1
  81.         ioctl(control_fd, TIOCMGET, &status);
  82.         status &= ~TIOCM_RTS;
  83.         ioctl(control_fd, TIOCMSET, &status);
  84.     }
  85.     else
  86.     {
  87.         // set /RTS=0
  88.         ioctl(control_fd, TIOCMGET, &status);
  89.         status |= TIOCM_RTS;
  90.         ioctl(control_fd, TIOCMSET, &status);
  91.  
  92.     }
  93.    
  94.  
  95.     if(!(mode & RESET_NORESET))
  96.     {
  97.         // pull /RESET low    clear D2
  98.         // set /DTR=0
  99.         ioctl(control_fd, TIOCMGET, &status);
  100.         status &= ~TIOCM_DTR;
  101.         ioctl(control_fd, TIOCMSET, &status);
  102.         usleep(SLEEP_SHORT);
  103.     }
  104.    
  105.     // pull /RESET high       set D2
  106.     // clr /DTR=1
  107.     ioctl(control_fd, TIOCMGET, &status);
  108.     status |= TIOCM_DTR;
  109.     ioctl(control_fd, TIOCMSET, &status);
  110.    
  111.     usleep(SLEEP_SHORT);
  112.     init_port();
  113.     usleep(SLEEP_SHORT);
  114.    
  115.     return 0;
  116. }
  117.  
  118. int get_version()
  119. {
  120.     int bytes, i;
  121.     unsigned char a = 0xA1;
  122.     char buf[255];
  123.    
  124.     bytes = write(data_fd, &a, 1);
  125.  
  126.     if (bytes != 1) {
  127.         printf("Error sending request: %d (%s)\n", bytes, strerror(errno));
  128.         return -1;
  129.     } else {
  130.         usleep(SLEEP_SHORT);
  131.        
  132.         // zero out the buffer
  133.         bzero(buf, 255);
  134.        
  135.         // read the bytes
  136.         for(i = 0; i < 255; i++) {
  137.             bytes = read(data_fd, &buf[i], 1);
  138.        
  139.             if(bytes <= 0)
  140.                 break;
  141.         }
  142.        
  143.         // output the version string
  144.         printf("Version: %s\n", buf);
  145.         printf("bytes read: %d\n", i);
  146.     }
  147.    
  148.     return 0;
  149. }
  150.  
  151. int play_cart()
  152. {
  153.     reset_copy_nes(RESET_PLAYMODE);
  154.     printf("Press enter when done playing.\n");
  155.     getchar();
  156.     reset_copy_nes(RESET_COPYMODE);
  157.     return 0;
  158. }
  159.  
  160. int main(int argc, char** argv)
  161. {
  162.     if(argc < 2)
  163.     {
  164.         printf("Usage: %s <version|play|status>\n", argv[0]);
  165.         exit(0);
  166.     }
  167.    
  168.     open_port();
  169.  
  170.     init_port();
  171.    
  172.     if(strcasecmp(argv[1], "version") == 0)
  173.     {
  174.         get_version();
  175.     }
  176.     else if(strcasecmp(argv[1], "play") == 0)
  177.     {
  178.         play_cart();
  179.     }
  180.     else if(strcasecmp(argv[1], "status") == 0)
  181.     {
  182.         show_modem_status();
  183.     }
  184.     else
  185.     {
  186.         printf("unknown command\n");
  187.     }
  188.    
  189.     // must clean up...
  190.     close_port();
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement