X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=z80comp2;a=blobdiff_plain;f=emu%2Fsrc%2Fmain.c;fp=emu%2Fsrc%2Fmain.c;h=54a3d3caa096e52efa389ffa0d9960c6032ca789;hp=0000000000000000000000000000000000000000;hb=a0a85eb847ff3bb5da13e78618efdcd1b9f588cf;hpb=b55f31d1ac4a1991f2ad7b2d17be06969ea25e47 diff --git a/emu/src/main.c b/emu/src/main.c new file mode 100644 index 0000000..54a3d3c --- /dev/null +++ b/emu/src/main.c @@ -0,0 +1,149 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "emu.h" + +static void sighandler(int s); +static int parse_args(int argc, char **argv); + +static const char *rom_fname = "rom"; +static const char *termdev = "/dev/tty"; + +static int ttyfd; +static struct termios saved_term; +static volatile int quit; + +int main(int argc, char **argv) +{ + void *rom; + int res, fd, maxfd; + struct stat st; + struct termios term; + fd_set rdset; + struct timeval tv = {0, 0}; + + if(parse_args(argc, argv) == -1) { + return 1; + } + + if((fd = open(rom_fname, O_RDONLY)) == -1) { + fprintf(stderr, "failed to open ROM image: %s: %s\n", rom_fname, strerror(errno)); + return -1; + } + fstat(fd, &st); + + if((rom = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void*)-1) { + fprintf(stderr, "failed to map ROM image\n"); + return -1; + } + + if((ttyfd = open(termdev, O_RDWR)) == -1) { + fprintf(stderr, "failed to open terminal device: %s: %s\n", termdev, strerror(errno)); + return -1; + } + if(tcgetattr(ttyfd, &term) == -1) { + perror("failed to get terminal attributes"); + return -1; + } + saved_term = term; + term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); + term.c_oflag &= ~OPOST; + term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + term.c_cflag = (term.c_cflag & ~(CSIZE | PARENB)) | CS8; + term.c_cc[VMIN] = 0; + term.c_cc[VTIME] = 1; + tcsetattr(ttyfd, TCSAFLUSH, &term); + + if(emu_init(rom, st.st_size) == -1) { + return 1; + } + + signal(SIGINT, sighandler); + signal(SIGQUIT, sighandler); + signal(SIGTERM, sighandler); + signal(SIGSEGV, sighandler); + signal(SIGILL, sighandler); + + while(!quit) { + FD_ZERO(&rdset); + FD_SET(ttyfd, &rdset); + + maxfd = ttyfd; + + res = select(maxfd + 1, &rdset, 0, 0, &tv); + if(quit) break; + + if(res > 0) { + if(FD_ISSET(ttyfd, &rdset)) { + int i, rd; + char buf[256]; + + while((rd = read(ttyfd, buf, sizeof buf)) > 0) { + for(i=0; i