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 / IOSIM.C < prev    next >
C/C++ Source or Header  |  2000-06-30  |  3KB  |  137 lines

  1. /*
  2.  * Z80SIM  -  a    Z80-CPU    simulator
  3.  *
  4.  * Copyright (C) 1987-92 by Udo Munk
  5.  *
  6.  * This modul of the simulator contains a simple terminal I/O
  7.  * simulation as an example. It is released to the public domain
  8.  * and may be modified by user.
  9.  *
  10.  * History:
  11.  * 28-SEP-87 Development on TARGON/35 with AT&T Unix System V.3
  12.  * 11-JAN-89 Release 1.1
  13.  * 08-FEB-89 Release 1.2
  14.  * 13-MAR-89 Release 1.3
  15.  * 09-FEB-90 Release 1.4 Ported to TARGON/31 M10/30
  16.  * 20-DEC-90 Release 1.5 Ported to COHERENT 3.0
  17.  * 10-JUN-92 Release 1.6 long casting problem solved with COHERENT 3.2
  18.  *             and some optimization
  19.  * 25-JUN-92 Release 1.7 comments in english
  20.  */
  21.  
  22. /*
  23.  *    Sample I/O-handler
  24.  *
  25.  *    Port 0 input:    reads the next byte from stdin
  26.  *    Port 0 output:    writes the byte to stdout
  27.  *
  28.  *    All the other ports are connected to a I/O-trap handler,
  29.  *    I/O to this ports stops the simulation with an I/O error.
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include "sim.h"
  34. #include "simglb.h"
  35.  
  36. /*
  37.  *    Forward declarations of the I/O functions
  38.  *    for all port addresses.
  39.  */
  40. BYTE io_trap();
  41. BYTE p000_in(),    p000_out();
  42.  
  43. /*
  44.  *    This two dimensional array contains function pointers
  45.  *    for every I/O port (0 - 255), to do the needed I/O.
  46.  *    The first entry is for input, the second for output.
  47.  */
  48. static BYTE (*port[256][2]) () = {
  49.     { p000_in, p000_out }        /* port    0 */
  50. };
  51.  
  52. /*
  53.  *    This function is to initiate the I/O devices.
  54.  *    It will be called from the CPU simulation before
  55.  *    any operation with the Z80 is possible.
  56.  *
  57.  *    In this sample I/O simulation we initialize all
  58.  *    unused port with an error trap handler, so that
  59.  *    simulation stops at I/O on the unused ports.
  60.  *
  61.  *    See the I/O simulation of CP/M for a more complex
  62.  *    example.
  63.  */
  64. void init_io()
  65. {
  66.     register int i;
  67.  
  68.     for (i = 1; i <= 255; i++)
  69.         port[i][0] = port[i][1]    = io_trap;
  70. }
  71.  
  72. /*
  73.  *    This function is to stop the I/O devices. It is
  74.  *    called from the CPU simulation on exit.
  75.  *
  76.  *    Here is just nothing to do, see the I/O simulation
  77.  *    of CP/M for a more complex example.
  78.  */
  79. void exit_io()
  80. {
  81. }
  82.  
  83. /*
  84.  *    This is the main handler for all IN op-codes,
  85.  *    called by the simulator. It calls the input
  86.  *    function for port adr.
  87.  */
  88. BYTE io_in(adr)
  89. BYTE adr;
  90. {
  91.     return((*port[adr][0]) ());
  92. }
  93.  
  94. /*
  95.  *    This is the main handler for all OUT op-codes,
  96.  *    called by the simulator. It calls the output
  97.  *    function for port adr.
  98.  */
  99. BYTE io_out(adr, data)
  100. BYTE adr, data;
  101. {
  102.     (*port[adr][1])    (data);
  103. }
  104.  
  105. /*
  106.  *    I/O trap funtion
  107.  *    This function should be added into all unused
  108.  *    entrys of the port array. It stops the emulation
  109.  *    with an I/O error.
  110.  */
  111. static BYTE io_trap()
  112. {
  113.     cpu_error = IOTRAP;
  114.     cpu_state = STOPPED;
  115.     return((BYTE) 0);
  116. }
  117.  
  118. /*
  119.  *    I/O function port 0 read:
  120.  *    Read next byte from stdin.
  121.  */
  122. static BYTE p000_in()
  123. {
  124.     return((BYTE) getchar());
  125. }
  126.  
  127. /*
  128.  *    I/O function port 0 write:
  129.  *    Write byte to stdout and flush the output.
  130.  */
  131. static BYTE p000_out(data)
  132. register BYTE data;
  133. {
  134.     putchar((int) data);
  135.     fflush(stdout);
  136. }
  137.