home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / EMULATOR / UNIX / Z80PACK / Z80SIM / SIM0.C < prev    next >
C/C++ Source or Header  |  2000-06-30  |  6KB  |  206 lines

  1. /*
  2.  * Z80SIM  -  a    Z80-CPU    simulator
  3.  *
  4.  * Copyright (C) 1987-92 by Udo Munk
  5.  *
  6.  * This module of the Z80-CPU simulator must not be modified by a user,
  7.  * see license agreement!
  8.  *
  9.  * History:
  10.  * 28-SEP-87 Development on TARGON/35 with AT&T Unix System V.3
  11.  * 11-JAN-89 Release 1.1
  12.  * 08-FEB-89 Release 1.2
  13.  * 13-MAR-89 Release 1.3
  14.  * 09-FEB-90 Release 1.4 Ported to TARGON/31 M10/30
  15.  * 20-DEC-90 Release 1.5 Ported to COHERENT 3.0
  16.  * 10-JUN-92 Release 1.6 long casting problem solved with COHERENT 3.2
  17.  *             and some optimization
  18.  * 25-JUN-92 Release 1.7 comments in english and ported to COHERENT 4.0
  19.  */
  20.  
  21. /*
  22.  *    This modul contain the 'main()' function of the simulator,
  23.  *    where the options are checked and variables are initialized.
  24.  *    After initialization of the UNIX interrupts ( int_on() )
  25.  *    and initialization of the I/O simulation ( init_io() )
  26.  *    the user interface ( mon() ) is called.
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #if defined(COHERENT) && !defined(_I386)
  32. #include <sys/fcntl.h>
  33. #else
  34. #include <fcntl.h>
  35. #endif
  36. #ifndef COHERENT
  37. #include <memory.h>
  38. #endif
  39. #include "sim.h"
  40. #include "simglb.h"
  41.  
  42. main(argc, argv)
  43. int argc;
  44. char *argv[];
  45. {
  46.     void exit(), init_io(),    exit_io();
  47.     register char *s, *p;
  48.     register char *pn = argv[0];
  49.  
  50.     while (--argc >    0 && (*++argv)[0] == '-')
  51.         for (s = argv[0] + 1; *s != '\0'; s++)
  52.             switch (*s) {
  53.             case 's':    /* save core and CPU on exit */
  54.                 s_flag = 1;
  55.                 break;
  56.             case 'l':    /* load core and CPU from file */
  57.                 l_flag = 1;
  58.                 break;
  59.             case 'h':    /* execute HALT opcode */
  60.                 break_flag = 0;
  61.                 break;
  62.             case 'm':    /* initialize Z80 memory */
  63.                 m_flag = exatoi(s+1);
  64.                 s += strlen(s+1);
  65.                 break;
  66.             case 'x':    /* get filename with Z80 executable */
  67.                 x_flag = 1;
  68.                 s++;
  69.                 p = xfn;
  70.                 while (*s)
  71.                     *p++ = *s++;
  72.                 *p = '\0';
  73.                 s--;
  74.                 break;
  75.             default:
  76.                 printf("illegal option %c\n", *s);
  77.                 printf("usage:\t%s [-s -l -h -mn -xfilename]\n", pn);
  78.                 puts("\ts = save core and cpu");
  79.                 puts("\tl = load core and cpu");
  80.                 puts("\th = execute HALT op-code");
  81.                 puts("\tm = init memory with n");
  82.                 puts("\tx = load and execute filename");
  83.                 exit(1);
  84.             }
  85.  
  86.     puts("#######  #####    ###            #####    ###   #     #");
  87.     puts("     #  #     #  #   #          #     #    #    ##   ##");
  88.     puts("    #   #     # #     #         #          #    # # # #");
  89.     puts("   #     #####  #     #  #####   #####     #    #  #  #");
  90.     puts("  #     #     # #     #               #    #    #     #");
  91.     puts(" #      #     #  #   #          #     #    #    #     #");
  92.     puts("#######  #####    ###            #####    ###   #     #");
  93.     printf("\nRelease %s, %s\n", RELEASE, COPYR);
  94. #ifdef    USR_COM
  95.     printf("%s Release %s, %s\n", USR_COM, USR_REL,    USR_CPR);
  96. #endif
  97.     putchar('\n');
  98.     fflush(stdout);
  99.  
  100.     wrk_ram    = PC = STACK = ram;
  101. #ifdef COHERENT
  102.     memset((char *)    ram, m_flag, 32767);
  103. #else
  104.     memset((char *)    ram, m_flag, 32768);
  105.     memset((char *)    ram + 32768, m_flag, 32768);
  106. #endif
  107.     if (l_flag)
  108.         load_core();
  109.     int_on();
  110.     init_io();
  111.     mon();
  112.     if (s_flag)
  113.         save_core();
  114.     exit_io();
  115.     int_off();
  116.     exit(0);
  117. }
  118.  
  119. /*
  120.  *    This function saves the CPU and the memory into the file core.z80
  121.  *
  122.  */
  123. static save_core()
  124. {
  125.     int fd;
  126.  
  127.     if ((fd    = open("core.z80", O_WRONLY | O_CREAT, 0600)) == -1) {
  128.         puts("can't open file core.z80");
  129.         return;
  130.     }
  131.     write(fd, (char    *) &A, sizeof(A));
  132.     write(fd, (char    *) &F, sizeof(F));
  133.     write(fd, (char    *) &B, sizeof(B));
  134.     write(fd, (char    *) &C, sizeof(C));
  135.     write(fd, (char    *) &D, sizeof(D));
  136.     write(fd, (char    *) &E, sizeof(E));
  137.     write(fd, (char    *) &H, sizeof(H));
  138.     write(fd, (char    *) &L, sizeof(L));
  139.     write(fd, (char    *) &A_,    sizeof(A_));
  140.     write(fd, (char    *) &F_,    sizeof(F_));
  141.     write(fd, (char    *) &B_,    sizeof(B_));
  142.     write(fd, (char    *) &C_,    sizeof(C_));
  143.     write(fd, (char    *) &D_,    sizeof(D_));
  144.     write(fd, (char    *) &E_,    sizeof(E_));
  145.     write(fd, (char    *) &H_,    sizeof(H_));
  146.     write(fd, (char    *) &L_,    sizeof(L_));
  147.     write(fd, (char    *) &I, sizeof(I));
  148.     write(fd, (char    *) &IFF, sizeof(IFF));
  149.     write(fd, (char    *) &R, sizeof(R));
  150.     write(fd, (char    *) &PC,    sizeof(PC));
  151.     write(fd, (char    *) &STACK, sizeof(STACK));
  152.     write(fd, (char    *) &IX,    sizeof(IX));
  153.     write(fd, (char    *) &IY,    sizeof(IY));
  154. #ifdef COHERENT
  155.     write(fd, (char *) ram, 32767);
  156. #else
  157.     write(fd, (char    *) ram,    32768);
  158.     write(fd, (char    *) ram + 32768,    32768);
  159. #endif
  160.     close(fd);
  161. }
  162.  
  163. /*
  164.  *    This function loads the CPU and memory from the file core.z80
  165.  *
  166.  */
  167. static load_core()
  168. {
  169.     int fd;
  170.  
  171.     if ((fd    = open("core.z80", O_RDONLY)) == -1) {
  172.         puts("can't open file core.z80");
  173.         return;
  174.     }
  175.     read(fd, (char *) &A, sizeof(A));
  176.     read(fd, (char *) &F, sizeof(F));
  177.     read(fd, (char *) &B, sizeof(B));
  178.     read(fd, (char *) &C, sizeof(C));
  179.     read(fd, (char *) &D, sizeof(D));
  180.     read(fd, (char *) &E, sizeof(E));
  181.     read(fd, (char *) &H, sizeof(H));
  182.     read(fd, (char *) &L, sizeof(L));
  183.     read(fd, (char *) &A_, sizeof(A_));
  184.     read(fd, (char *) &F_, sizeof(F_));
  185.     read(fd, (char *) &B_, sizeof(B_));
  186.     read(fd, (char *) &C_, sizeof(C_));
  187.     read(fd, (char *) &D_, sizeof(D_));
  188.     read(fd, (char *) &E_, sizeof(E_));
  189.     read(fd, (char *) &H_, sizeof(H_));
  190.     read(fd, (char *) &L_, sizeof(L_));
  191.     read(fd, (char *) &I, sizeof(I));
  192.     read(fd, (char *) &IFF,    sizeof(IFF));
  193.     read(fd, (char *) &R, sizeof(R));
  194.     read(fd, (char *) &PC, sizeof(PC));
  195.     read(fd, (char *) &STACK, sizeof(STACK));
  196.     read(fd, (char *) &IX, sizeof(IX));
  197.     read(fd, (char *) &IY, sizeof(IY));
  198. #ifdef COHERENT
  199.     read(fd, (char *) ram, 32767);
  200. #else
  201.     read(fd, (char *) ram, 32768);
  202.     read(fd, (char *) ram +    32768, 32768);
  203. #endif
  204.     close(fd);
  205. }
  206.