home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume6 / ega-wanderer / patch1 / 386ix_ega.c next >
C/C++ Source or Header  |  1989-02-24  |  11KB  |  429 lines

  1. /*
  2.  *    EGA replacement console driver for VENIX SVR2
  3.  *    Hacked for user mode 386/ix use.
  4.  *
  5.  *    Copyright 1987 PC Research, Inc.  All Rights Reserved.
  6.  *
  7.  *    Modified  June 1988 for use from 386/ix user mode
  8.  *
  9.  *    Hacked again Feb 1989 for use with Wanderer
  10.  *
  11.  *    Permission granted to use this routine in the game
  12.  *    "WANDERER" is hereby granted to everbody.
  13.  *
  14.  *    -Rick Richardson, PC Research, Inc.
  15.  */
  16. #define    ADDR_6845    0x3d4
  17.  
  18. #define ega    EGAaddr
  19.  
  20. #include <sys/inline.h>
  21. splx(){}        /* No way from user mode */
  22. spl7(){}
  23. extern char    *ega;
  24.  
  25. typedef struct
  26. {
  27.     char    cols;        /* Text columns */
  28.     char    rows;        /* Text Rows */
  29.     char    pels;        /* Pel rows per character */
  30.     int    pagelen;    /* Length of text page in bytes */
  31.     char    seq[5];        /* 5 values for sequencer */
  32.     char    misc;        /* misc output register */
  33.     char    crt[25];    /* CRT controller */
  34.     char    atr[20];    /* Attribute controller */
  35.     char    graph[9];    /* Graphics controller */
  36. } CRTC;
  37.  
  38. static    CRTC    ega_parms[19] =
  39. {
  40.     {    /* EGA Mode 0: 25 x 40 sixteen color text */
  41.     /**/        40, 25, 14, 0x0800,
  42.     /*seq*/    { 0x01, 0x0b, 0x03, 0, 0x03 },
  43.     /*misc*/    0xa7,
  44.     /*crt*/    {    0x2d, 0x27, 0x2b, 0x2d, 0x28, 0x6d,
  45.             0x6c, 0x1f, 0, 0x0d, 0x06, 0x07,
  46.             0, 0, 0, 0, 0x5e, 0x2b,
  47.             0x5d, 0x14, 0x0f, 0x5e, 0x0a, 0xa3,
  48.             0xff },
  49.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  50.             0x14, 0x07, 0x38, 0x39, 0x3a, 0x3b,
  51.             0x3c, 0x3d, 0x3e, 0x3f, 0x08, 0,
  52.             0x0f, 0x00 },
  53.     /*graf*/{    0, 0, 0, 0, 0, 0x10,
  54.             0x0e, 0, 0xff}
  55.     },
  56.  
  57.     {    /* EGA Mode 1: 25 x 40 sixteen color text */
  58.     /**/        40, 25, 14, 0x0800,
  59.     /*seq*/    { 0x01, 0x0b, 0x03, 0, 0x03 },
  60.     /*misc*/    0xa7,
  61.     /*crt*/    {    0x2d, 0x27, 0x2b, 0x2d, 0x28, 0x6d,
  62.             0x6c, 0x1f, 0, 0x0d, 0x06, 0x07,
  63.             0, 0, 0, 0, 0x5e, 0x2b,
  64.             0x5d, 0x14, 0x0f, 0x5e, 0x0a, 0xa3,
  65.             0xff },
  66.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  67.             0x14, 0x07, 0x38, 0x39, 0x3a, 0x3b,
  68.             0x3c, 0x3d, 0x3e, 0x3f, 0x08, 0,
  69.             0x0f, 0x00 },
  70.     /*graf*/{    0, 0, 0, 0, 0, 0x10,
  71.             0x0e, 0, 0xff}
  72.     },
  73.  
  74.     {    /* EGA Mode 2: 25 x 80 sixteen color text */
  75.     /**/        80, 25, 14, 0x1000,
  76.     /*seq*/    { 0x01, 0x01, 0x03, 0, 0x03 },
  77.     /*misc*/    0xa7,
  78.     /*crt*/    {    0x5b, 0x4f, 0x53, 0x37, 0x51, 0x5b,
  79.             0x6c, 0x1f, 0, 0x0d, 0x06, 0x07,
  80.             0, 0, 0, 0, 0x5e, 0x2b,
  81.             0x5d, 0x28, 0x0f, 0x5e, 0x0a, 0xa3,
  82.             0xff },
  83.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  84.             0x14, 0x07, 0x38, 0x39, 0x3a, 0x3b,
  85.             0x3c, 0x3d, 0x3e, 0x3f, 0x08, 0,
  86.             0x0f, 0x00 },
  87.     /*graf*/{    0, 0, 0, 0, 0, 0x10,
  88.             0x0e, 0, 0xff}
  89.     },
  90.  
  91.     {    /* EGA Mode 3: 25 x 80 sixteen color text */
  92.     /**/        80, 25, 14, 0x1000,
  93.     /*seq*/    { 0x01, 0x01, 0x03, 0, 0x03 },
  94.     /*misc*/    0xa7,
  95.     /*crt*/    {    0x5b, 0x4f, 0x53, 0x37, 0x51, 0x5b,
  96.             0x6c, 0x1f, 0, 0x0d, 0x0b, 0x0c,
  97.             0, 0, 0, 0, 0x5e, 0x2b,
  98.             0x5d, 0x28, 0x0f, 0x5e, 0x0a, 0xa3,
  99.             0xff },
  100.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  101.             0x14, 0x07, 0x38, 0x39, 0x3a, 0x3b,
  102.             0x3c, 0x3d, 0x3e, 0x3f, 0x08, 0,
  103.             0x0f, 0x00 },
  104.     /*graf*/{    0, 0, 0, 0, 0, 0x10,
  105.             0x0e, 0, 0xff}
  106.     },
  107.  
  108.     {    /* EGA Mode 4: 320x200 four color graphics */
  109.     /**/        40, 25, 8, 0x4000,
  110.     /*seq*/    { 0x01, 0x0b, 0x03, 0, 0x02 },
  111.     /*misc*/    0x23,
  112.     /*crt*/    {    0x37, 0x27, 0x2d, 0x37, 0x30, 0x14,
  113.             0x04, 0x11, 0, 0x01, 0, 0,
  114.             0, 0, 0, 0, 0xe1, 0x24,
  115.             0xc7, 0x14, 0, 0xe0, 0xf0, 0xa2,
  116.             0xff },
  117.     /*atr*/    {    0, 0x13, 0x15, 0x17, 0x02, 0x04,
  118.             0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
  119.             0x14, 0x15, 0x16, 0x17, 0x01, 0,
  120.             0x03, 0x00 },
  121.     /*graf*/{    0, 0, 0, 0, 0, 0x30,
  122.             0x0f, 0, 0xff}
  123.     },
  124.  
  125.     {    /* EGA Mode 5: 320x200 four color graphics */
  126.     /**/        40, 25, 8, 0x4000,
  127.     /*seq*/    { 0x01, 0x0b, 0x03, 0, 0x02 },
  128.     /*misc*/    0x23,
  129.     /*crt*/    {    0x37, 0x27, 0x2d, 0x37, 0x30, 0x14,
  130.             0x04, 0x11, 0, 0x01, 0, 0,
  131.             0, 0, 0, 0, 0xe1, 0x24,
  132.             0xc7, 0x14, 0, 0xe0, 0xf0, 0xa2,
  133.             0xff },
  134.     /*atr*/    {    0, 0x13, 0x15, 0x17, 0x02, 0x04,
  135.             0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
  136.             0x14, 0x15, 0x16, 0x17, 0x01, 0,
  137.             0x03, 0x00 },
  138.     /*graf*/{    0, 0, 0, 0, 0, 0x30,
  139.             0x0f, 0, 0xff}
  140.     },
  141.  
  142.     {    /* EGA Mode 6: 640x200 two color graphics */
  143.     /**/        80, 25, 8, 0x4000,
  144.     /*seq*/    { 0x01, 0x01, 0x01, 0, 0x06 },
  145.     /*misc*/    0x23,
  146.     /*crt*/    {    0x70, 0x4f, 0x59, 0x2d, 0x5e, 0x06,
  147.             0x04, 0x11, 0, 0x01, 0, 0,
  148.             0, 0, 0, 0, 0xe0, 0x23,
  149.             0xc7, 0x28, 0, 0xdf, 0xef, 0xc2,
  150.             0xff },
  151.     /*atr*/    {    0, 0x17, 0x17, 0x17, 0x17, 0x17,
  152.             0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
  153.             0x17, 0x17, 0x17, 0x17, 0x01, 0,
  154.             0x01, 0x00 },
  155.     /*graf*/{    0, 0, 0, 0, 0, 0,
  156.             0x0d, 0, 0xff}
  157.     },
  158.  
  159.     {    /* EGA Mode 7: 80x25 monochrome text */
  160.     /**/        80, 25, 14, 0x1000,
  161.     /*seq*/    { 0x01, 0, 0x03, 0, 0x03 },
  162.     /*misc*/    0xa6,
  163.     /*crt*/    {    0x60, 0x4f, 0x56, 0x3a, 0x51, 0x60,
  164.             0x70, 0x01, 0, 0x0d, 0x0b, 0x0c,
  165.             0, 0, 0, 0, 0x5e, 0x2e,
  166.             0x5d, 0x28, 0x0d, 0x5e, 0x6e, 0xa3,
  167.             0xff },
  168.     /*atr*/    {    0, 0x08, 0x08, 0x08, 0x08, 0x08,
  169.             0x08, 0x08, 0x10, 0x18, 0x18, 0x18,
  170.             0x18, 0x18, 0x18, 0x18, 0x0e, 0,
  171.             0x0f, 0x08 },
  172.     /*graf*/{    0, 0, 0, 0, 0, 0x10,
  173.             0x0a, 0, 0xff}
  174.     },
  175.  
  176.     {    /* EGA Mode 8: reserved */
  177.     /**/        40, 25, 8, 0x4000,
  178.     /*seq*/    { 0x01, 0, 0, 0, 0x03 },
  179.     /*misc*/    0x23,
  180.     /*crt*/    {    0x37, 0x27, 0x2d, 0x37, 0x31, 0x15,
  181.             0x04, 0x11, 0, 0x07, 0x06, 0x07,
  182.             0, 0, 0, 0, 0xe1, 0x24,
  183.             0xc7, 0x14, 0x08, 0xe0, 0xf0, 0xa3,
  184.             0xff },
  185.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  186.             0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
  187.             0x14, 0x15, 0x16, 0x17, 0x08, 0,
  188.             0x0f, 0x00 },
  189.     /*graf*/{    0, 0, 0, 0, 0, 0x10,
  190.             0x0e, 0, 0xff}
  191.     },
  192.  
  193.     {    /* EGA Mode 9: reserved */
  194.     /**/        40, 25, 8, 0x4000,
  195.     /*seq*/    { 0x01, 0, 0, 0, 0x03 },
  196.     /*misc*/    0x23,
  197.     /*crt*/    {    0x37, 0x27, 0x2d, 0x37, 0x31, 0x15,
  198.             0x04, 0x11, 0, 0x07, 0x06, 0x07,
  199.             0, 0, 0, 0, 0xe1, 0x24,
  200.             0xc7, 0x14, 0x08, 0xe0, 0xf0, 0xa3,
  201.             0xff },
  202.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  203.             0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
  204.             0x14, 0x15, 0x16, 0x17, 0x08, 0,
  205.             0x0f, 0x00 },
  206.     /*graf*/{    0, 0, 0, 0, 0, 0x10,
  207.             0x0e, 0, 0xff}
  208.     },
  209.  
  210.     {    /* EGA Mode A: reserved */
  211.     /**/        40, 25, 8, 0x4000,
  212.     /*seq*/    { 0x01, 0, 0, 0, 0x03 },
  213.     /*misc*/    0x23,
  214.     /*crt*/    {    0x37, 0x27, 0x2d, 0x37, 0x31, 0x15,
  215.             0x04, 0x11, 0, 0x07, 0x06, 0x07,
  216.             0, 0, 0, 0, 0xe1, 0x24,
  217.             0xc7, 0x14, 0x08, 0xe0, 0xf0, 0xa3,
  218.             0xff },
  219.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  220.             0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
  221.             0x14, 0x15, 0x16, 0x17, 0x08, 0,
  222.             0x0f, 0x00 },
  223.     /*graf*/{    0, 0, 0, 0, 0, 0x10,
  224.             0x0e, 0, 0xff}
  225.     },
  226.  
  227.     {    /* EGA Mode B: reserved, internal use */
  228.     /**/        80, 25, 8, 0x1000,
  229.     /*seq*/    { 0x01, 0x01, 0x04, 0, 0x07 },
  230.     /*misc*/    0x23,
  231.     /*crt*/    {    0x70, 0x4f, 0x5c, 0x2f, 0x5f, 0x07,
  232.             0x04, 0x11, 0, 0x07, 0x06, 0x07,
  233.             0, 0, 0, 0, 0xe1, 0x24,
  234.             0xc7, 0x28, 0x08, 0xe0, 0xf0, 0xa3,
  235.             0xff },
  236.     /*atr*/    {    0, 0, 0, 0, 0, 0,
  237.             0, 0, 0, 0, 0, 0,
  238.             0, 0, 0, 0, 0, 0,
  239.             0x0f, 0x00 },
  240.     /*graf*/{    0, 0, 0, 0, 0, 0,
  241.             0x04, 0, 0xff}
  242.     },
  243.  
  244.     {    /* EGA Mode C: reserved, internal use */
  245.     /**/        80, 25, 14, 0x1000,
  246.     /*seq*/    { 0x01, 0, 0x04, 0, 0x07 },
  247.     /*misc*/    0xa6,
  248.     /*crt*/    {    0x60, 0x4f, 0x56, 0x3a, 0x51, 0x60,
  249.             0x70, 0x01, 0, 0x0d, 0x0b, 0x0c,
  250.             0, 0, 0, 0, 0x5e, 0x2e,
  251.             0x5d, 0x28, 0x0d, 0x5e, 0x6e, 0xa3,
  252.             0xff },
  253.     /*atr*/    {    0, 0, 0, 0, 0, 0,
  254.             0, 0, 0, 0, 0, 0,
  255.             0, 0, 0, 0, 0x0e, 0,
  256.             0x0f, 0x08 },
  257.     /*graf*/{    0, 0, 0, 0, 0, 0,
  258.             0x04, 0, 0xff}
  259.     },
  260.  
  261.     {    /* EGA Mode D: 320 x 200 sixteen color graphics */
  262.     /**/        40, 25, 8, 0x2000,
  263.     /*seq*/    { 0x01, 0x0b, 0x0f, 0, 0x06 },
  264.     /*misc*/    0x23,
  265.     /*crt*/    {    0x37, 0x27, 0x2d, 0x37, 0x30, 0x14,
  266.             0x04, 0x11, 0, 0, 0, 0,
  267.             0, 0, 0, 0, 0xe1, 0x24,
  268.             0xc7, 0x14, 0, 0xe0, 0xf0, 0xe3,
  269.             0xff },
  270.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  271.             0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
  272.             0x14, 0x15, 0x16, 0x17, 0x01, 0,
  273.             0x0f, 0x00 },
  274.     /*graf*/{    0, 0, 0, 0, 0, 0,
  275.             0x05, 0x0f, 0xff}
  276.     },
  277.  
  278.     {    /* EGA Mode E: 640 x 200 sixteen color graphics */
  279.     /**/        80, 25, 8, 0x4000,
  280.     /*seq*/    { 0x01, 0x01, 0x0f, 0, 0x06 },
  281.     /*misc*/    0x23,
  282.     /*crt*/    {    0x70, 0x4f, 0x59, 0x2d, 0x5e, 0x06,
  283.             0x04, 0x11, 0, 0, 0, 0,
  284.             0, 0, 0, 0, 0xe0, 0x23,
  285.             0xc7, 0x28, 0, 0xdf, 0xef, 0xe3,
  286.             0xff },
  287.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  288.             0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
  289.             0x14, 0x15, 0x16, 0x17, 0x01, 0,
  290.             0x0f, 0x00 },
  291.     /*graf*/{    0, 0, 0, 0, 0, 0,
  292.             0x05, 0x0f, 0xff}
  293.     },
  294.  
  295.     {    /* EGA Mode F: 640 x 350 monochrome graphics */
  296.     /**/        80, 25, 14, 0x8000,
  297.     /*seq*/    { 0x01, 0x05, 0x0f, 0, 0x00 },
  298.     /*misc*/    0xa2,
  299.     /*crt*/    {    0x60, 0x4f, 0x56, 0x1a, 0x50, 0xe0,
  300.             0x70, 0x01, 0, 0, 0, 0,
  301.             0, 0, 0, 0, 0x5e, 0x2e,
  302.             0x5d, 0x14, 0x0d, 0x5e, 0x6e, 0x8b,
  303.             0xff },
  304.     /*atr*/    {    0, 0x08, 0, 0, 0x18, 0x18,
  305.             0, 0, 0, 0x08, 0, 0,
  306.             0, 0x18, 0, 0, 0x0b, 0,
  307.             0x05, 0x00 },
  308.     /*graf*/{    0, 0, 0, 0, 0, 0x10,
  309.             0x07, 0x0f, 0xff}
  310.     },
  311.  
  312.     {    /* EGA Mode 10: 640 x 350 sixteen color graphics */
  313.     /**/        80, 25, 14, 0x8000,
  314.     /*seq*/    { 0x01, 0x01, 0x0f, 0, 0x06 },
  315.     /*misc*/    0xa7,
  316.     /*crt*/    {    0x5b, 0x4f, 0x53, 0x37, 0x52, 0,
  317.             0x6c, 0x1f, 0, 0, 0, 0,
  318.             0, 0, 0, 0, 0x5e, 0x2b,
  319.             0x5d, 0x28, 0x0f, 0x5f, 0x0a, 0xe3,
  320.             0xff },
  321.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  322.             0x14, 0x07, 0x38, 0x39, 0x3a, 0x3b,
  323.             0x3c, 0x3d, 0x3e, 0x3f, 0x01, 0,
  324.             0x0f, 0x00 },
  325.     /*graf*/{    0, 0, 0, 0, 0, 0,
  326.             0x05, 0x0f, 0xff}
  327.     },
  328.  
  329.     {    /* EGA Mode 11: 43 x 80 sixteen color text */
  330.     /**/        80, 42, 8, 0x2000,
  331.     /*seq*/    { 0x01, 0x01, 0x03, 0x05, 0x03 },    /* used cgen2 */
  332.     /*misc*/    0xa7,
  333.     /*crt*/    {    0x5b, 0x4f, 0x53, 0x37, 0x51, 0x5b,
  334.             0x6c, 0x1f, 0, 0x07, 0x06, 0x07,
  335.             0, 0, 0, 0, 0x5e, 0x2b,
  336.             0x58, 0x28, 0x0f, 0x5e, 0x0a, 0xa3,
  337.             0xff },
  338.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  339.             0x14, 0x07, 0x38, 0x39, 0x3a, 0x3b,
  340.             0x3c, 0x3d, 0x3e, 0x3f, 0x08, 0,
  341.             0x0f, 0x00 },
  342.     /*graf*/{    0, 0, 0, 0, 0, 0x10,
  343.             0x0e, 0, 0xff}
  344.     },
  345.  
  346.     {    /* EGA Mode 12: 640 x 480 sixteen color graphics */
  347.         /* PEGA Mode 50: 640 x 480 sixteen color graphics */
  348.     /**/        80, 29, 16, 0xc800,
  349.     /*seq*/    { 0x01, 0x01, 0x0f, 0, 0x06 },
  350.     /*misc*/    0xab,
  351.     /*crt*/    {    0x66, 0x4f, 0x53, 0x3d, 0x55, 0x1f,
  352.             0xfa, 0x1f, 0, 0, 0, 0,
  353.             0, 0, 0, 0, 0xe1, 0x29,
  354.             0xdf, 0x28, 0x0f, 0xe0, 0x1a, 0xe3,
  355.             0xff },
  356.     /*atr*/    {    0, 0x01, 0x02, 0x03, 0x04, 0x05,
  357.             0x14, 0x07, 0x38, 0x39, 0x3a, 0x3b,
  358.             0x3c, 0x3d, 0x3e, 0x3f, 0x01, 0,
  359.             0x0f, 0x00 },
  360.     /*graf*/{    0, 0, 0, 0, 0, 0,
  361.             0x05, 0x0f, 0xff}
  362.     },
  363. };
  364.  
  365. /*
  366.  *    ega_setregs(mode):
  367.  *        program EGA chips for "mode"
  368.  */
  369. ega_setregs(mode)
  370. {
  371.     register CRTC    *cp = &ega_parms[mode];
  372.     register int    j;
  373.     register int    x;
  374.  
  375.     x = spl7();                /* Int's off */
  376.     for (j = 0; j < 5; ++j)            /* Program sequencer */
  377.     {
  378.         outb(0x3c4, j);
  379.         outb(0x3c4 + 1, cp->seq[j]);
  380.     }
  381.     outb(0x3c2, cp->misc );        /* Program misc register */
  382.     outb(0x3c4, 0); outb(0x3c5, 3);    /* Turn off sequencer reset */
  383.     splx(x);                /* Int's back on (maybe) */
  384.     for( j = 0; j < 25; j++ )        /* Program CRT controller */
  385.     {
  386.         outb(ADDR_6845, j);
  387.         outb(ADDR_6845+1, cp->crt[j]);
  388.     }
  389.  
  390.     inb(ADDR_6845+6 );            /* Reset Atr ctrl state */
  391.     for( j = 0; j < 20; j++ )        /* Program Atr controller */
  392.     {
  393.         outb(0x3c0, j);
  394.         outb(0x3c0, cp->atr[j]);
  395.     }
  396.     outb(0x3c0, 0 );
  397.  
  398.     outb(0x3cc, 0 );            /* Program Grafix 1 position */
  399.     outb(0x3ca, 1 );            /* Program Grafix 2 position */
  400.  
  401.     for( j = 0; j < 9; j++ )        /* Program Grafix controller */
  402.     {
  403.         outb(0x3ce, j);
  404.         outb(0x3ce+1, cp->graph[j]);
  405.     }
  406. }
  407.  
  408. ega_palset(pal, color)
  409. {
  410.     register int x;
  411.     register int    areg = 0x3c0;
  412.  
  413.     x = spl7();
  414.     while ((inb(ADDR_6845+6) & 0x08) == 0)
  415.         ;    /* Wait for vert retrace */
  416.     outb(areg, pal);
  417.     outb(areg, color);
  418.     outb(areg, 0x20);
  419.     splx(x);
  420. }
  421.  
  422. /*
  423.  *    Enable the display, must be called after a ega_setregs();
  424.  */
  425. ega_enable()
  426. {
  427.     inb(ADDR_6845+6 ); outb(0x3c0, 0x20 );    /* Enable display */
  428. }
  429.