Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <termio.h>
- #include <strings.h>
- #define _POSIX_SOURCE 1
- #define DATAPORT "/dev/ttyUSB0"
- #define CONTROLPORT "/dev/ttyUSB1"
- #define SLEEP_SHORT 100000
- #define SLEEP_LONG 1000000
- int data_fd, control_fd;
- int open_port()
- {
- // open the data channel
- data_fd = open(DATAPORT, O_RDWR | O_NOCTTY | O_NDELAY);
- if (data_fd == -1) {
- printf("Error opening data channel!\n");
- exit(-1);
- } else {
- printf("Data channel open.\n");
- }
- // open the control channel
- control_fd = open(CONTROLPORT, O_RDWR | O_NOCTTY | O_NDELAY);
- if (control_fd == -1) {
- close(data_fd);
- printf("Error opening control channel!\n");
- exit(-1);
- } else {
- printf("Control channel open.\n");
- }
- return 0;
- }
- int init_port()
- {
- int status = 0;
- // check to make sure the NES is on
- ioctl(control_fd, TIOCMGET, &status);
- while(status & TIOCM_CAR)
- {
- printf("NES is OFF - turn it on, then hit enter\n");
- getchar();
- ioctl(control_fd, TIOCMGET, &status);
- }
- // flush I/O buffers on both serial devices
- tcflush(data_fd, TCIOFLUSH);
- tcflush(control_fd, TCIOFLUSH);
- return 0;
- }
- int close_port()
- {
- close(data_fd);
- close(control_fd);
- return 0;
- }
- #define RESET_COPYMODE 0
- #define RESET_PLAYMODE 1
- #define RESET_ALTPORT 2
- #define RESET_NORESET 4
- int reset_copy_nes(int mode)
- {
- int status = 0;
- if(mode & RESET_PLAYMODE)
- {
- // clr /RTS=1
- ioctl(control_fd, TIOCMGET, &status);
- status &= ~TIOCM_RTS;
- ioctl(control_fd, TIOCMSET, &status);
- }
- else
- {
- // set /RTS=0
- ioctl(control_fd, TIOCMGET, &status);
- status |= TIOCM_RTS;
- ioctl(control_fd, TIOCMSET, &status);
- }
- if(!(mode & RESET_NORESET))
- {
- // pull /RESET low clear D2
- // set /DTR=0
- ioctl(control_fd, TIOCMGET, &status);
- status &= ~TIOCM_DTR;
- ioctl(control_fd, TIOCMSET, &status);
- usleep(SLEEP_SHORT);
- }
- // pull /RESET high set D2
- // clr /DTR=1
- ioctl(control_fd, TIOCMGET, &status);
- status |= TIOCM_DTR;
- ioctl(control_fd, TIOCMSET, &status);
- usleep(SLEEP_SHORT);
- init_port();
- usleep(SLEEP_SHORT);
- return 0;
- }
- int get_version()
- {
- int bytes, i;
- unsigned char a = 0xA1;
- char buf[255];
- bytes = write(data_fd, &a, 1);
- if (bytes != 1) {
- printf("Error sending request: %d (%s)\n", bytes, strerror(errno));
- return -1;
- } else {
- usleep(SLEEP_SHORT);
- // zero out the buffer
- bzero(buf, 255);
- // read the bytes
- for(i = 0; i < 255; i++) {
- bytes = read(data_fd, &buf[i], 1);
- if(bytes <= 0)
- break;
- }
- // output the version string
- printf("Version: %s\n", buf);
- printf("bytes read: %d\n", i);
- }
- return 0;
- }
- int play_cart()
- {
- reset_copy_nes(RESET_PLAYMODE);
- printf("Press enter when done playing.\n");
- getchar();
- reset_copy_nes(RESET_COPYMODE);
- return 0;
- }
- int main(int argc, char** argv)
- {
- if(argc < 2)
- {
- printf("Usage: %s <version|play|status>\n", argv[0]);
- exit(0);
- }
- open_port();
- init_port();
- if(strcasecmp(argv[1], "version") == 0)
- {
- get_version();
- }
- else if(strcasecmp(argv[1], "play") == 0)
- {
- play_cart();
- }
- else if(strcasecmp(argv[1], "status") == 0)
- {
- show_modem_status();
- }
- else
- {
- printf("unknown command\n");
- }
- // must clean up...
- close_port();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement