home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / memacs / ue311c.arc / IBMPC.C < prev    next >
C/C++ Source or Header  |  1990-08-16  |  15KB  |  644 lines

  1. /*
  2.  * The routines in this file provide support for the IBM-PC and other
  3.  * compatible terminals. It goes directly to the graphics RAM to do
  4.  * screen output. It compiles into nothing if not an IBM-PC driver
  5.  * Supported monitor cards include CGA, MONO and EGA.
  6.  */
  7.  
  8. #define    termdef    1            /* don't define "term" external */
  9.  
  10. #include        <stdio.h>
  11. #include    "estruct.h"
  12. #include    "eproto.h"
  13. #include        "edef.h"
  14. #include    "elang.h"
  15.  
  16. #if     IBMPC
  17. #define NROW    50            /* Max Screen size.        */
  18. #define NCOL    80                      /* Edit if you want to.         */
  19. #define    MARGIN    8            /* size of minimim margin and    */
  20. #define    SCRSIZ    64            /* scroll size for extended lines */
  21. #define    NPAUSE    200            /* # times thru update to pause */
  22. #define BEL     0x07                    /* BEL character.               */
  23. #define ESC     0x1B                    /* ESC character.               */
  24. #define    SPACE    32            /* space character        */
  25.  
  26. #define    SCADC    0xb8000000L        /* CGA address of screen RAM    */
  27. #define    SCADM    0xb0000000L        /* MONO address of screen RAM    */
  28. #define SCADE    0xb8000000L        /* EGA/VGA address of screen RAM*/
  29.  
  30. #define MONOCRSR 0x0B0D            /* monochrome cursor        */
  31. #define CGACRSR 0x0607            /* CGA cursor            */
  32. #define EGACRSR 0x0709            /* EGA/VGA cursor        */
  33.  
  34. #define    CDCGA    0            /* color graphics card        */
  35. #define    CDMONO    1            /* monochrome text card        */
  36. #define    CDEGA    2            /* EGA color adapter        */
  37. #define    CDVGA    3            /* VGA color adapter        */
  38. #define    CDVGA12    4            /* VGA 12 line mode        */
  39. #define    CDCGA40    5            /* CGA 40 wide mode        */
  40. #define    CDSENSE    9            /* detect the card type        */
  41.  
  42. #define NDRIVE    6            /* number of screen drivers    */
  43.  
  44. int dtype = -1;                /* current display type        */
  45. char drvname[][8] = {            /* screen resolution names    */
  46.     "CGA", "MONO", "EGA", "VGA", "VGA12", "CGA40"
  47. };
  48. long scadd;                /* address of screen ram    */
  49. int *scptr[NROW];            /* pointer to screen lines    */
  50. unsigned int sline[NCOL];        /* screen line image        */
  51. int num_cols;                /* current number of columns    */
  52. int orig_mode;                /* screen mode on entry        */
  53. int egaexist = FALSE;            /* is an EGA card available?    */
  54. int vgaexist = FALSE;            /* is video graphics array available? */
  55. extern union REGS rg;            /* cpu register for use of DOS calls */
  56. int revflag = FALSE;            /* are we currently in rev video? */
  57.  
  58. PASCAL NEAR ibmmove();
  59. PASCAL NEAR ibmeeol();
  60. PASCAL NEAR ibmputc();
  61. PASCAL NEAR ibmeeop();
  62. PASCAL NEAR ibmrev();
  63. PASCAL NEAR ibmcres();
  64. PASCAL NEAR spal();
  65. PASCAL NEAR ibmbeep();
  66. PASCAL NEAR ibmopen();
  67. PASCAL NEAR ibmclose();
  68. PASCAL NEAR ibmkopen();
  69. PASCAL NEAR ibmkclose();
  70. PASCAL NEAR scinit();
  71. int PASCAL NEAR getboard();
  72. PASCAL NEAR egaopen();
  73. PASCAL NEAR egaclose();
  74. PASCAL NEAR cga40_open();
  75. PASCAL NEAR cga40_close();
  76. PASCAL NEAR change_width();
  77. PASCAL NEAR fnclabel();
  78.  
  79. #if    COLOR
  80. PASCAL NEAR ibmfcol();
  81. PASCAL NEAR ibmbcol();
  82. int    cfcolor = -1;        /* current forground color */
  83. int    cbcolor = -1;        /* current background color */
  84. int    ctrans[] =        /* ansi to ibm color translation table */
  85.     {0, 4, 2, 6, 1, 5, 3, 7,
  86.      8, 12, 10, 14, 9, 13, 11, 15};
  87. #endif
  88.  
  89. /*
  90.  * Standard terminal interface dispatch table. Most of the fields point into
  91.  * "termio" code.
  92.  */
  93. TERM    term    = {
  94.     NROW-1,
  95.         NROW-1,
  96.         NCOL,
  97.         NCOL,
  98.     MARGIN,
  99.     SCRSIZ,
  100.     NPAUSE,
  101.         ibmopen,
  102.         ibmclose,
  103.     ibmkopen,
  104.     ibmkclose,
  105.         ttgetc,
  106.     ibmputc,
  107.         ttflush,
  108.         ibmmove,
  109.         ibmeeol,
  110.         ibmeeop,
  111.         ibmbeep,
  112.     ibmrev,
  113.     ibmcres
  114. #if    COLOR
  115.     , ibmfcol,
  116.     ibmbcol
  117. #endif
  118. };
  119.  
  120. #if    COLOR
  121. PASCAL NEAR ibmfcol(color)    /* set the current output color */
  122.  
  123. int color;    /* color to set */
  124.  
  125. {
  126.     cfcolor = ctrans[color];
  127. }
  128.  
  129. PASCAL NEAR ibmbcol(color)    /* set the current background color */
  130.  
  131. int color;    /* color to set */
  132.  
  133. {
  134.         cbcolor = ctrans[color];
  135. }
  136. #endif
  137.  
  138. PASCAL NEAR ibmmove(row, col)
  139. {
  140.     rg.h.ah = 2;        /* set cursor position function code */
  141.     rg.h.dl = col;
  142.     rg.h.dh = row;
  143.     rg.h.bh = 0;        /* set screen page number */
  144.     int86(0x10, &rg, &rg);
  145. }
  146.  
  147. PASCAL NEAR ibmeeol()    /* erase to the end of the line */
  148.  
  149. {
  150.     unsigned int attr;    /* attribute byte mask to place in RAM */
  151.     unsigned int *lnptr;    /* pointer to the destination line */
  152.     int i;
  153.     int ccol;    /* current column cursor lives */
  154.     int crow;    /*       row    */
  155.  
  156.     /* find the current cursor position */
  157.     rg.h.ah = 3;        /* read cursor position function code */
  158.     rg.h.bh = 0;        /* current video page */
  159.     int86(0x10, &rg, &rg);
  160.     ccol = rg.h.dl;        /* record current column */
  161.     crow = rg.h.dh;        /* and row */
  162.  
  163.     /* build the attribute byte and setup the screen pointer */
  164. #if    COLOR
  165.     if (dtype != CDMONO)
  166.         if (revflag)
  167.             attr = (((cfcolor & 15) << 4) | (cbcolor & 15)) << 8;
  168.         else
  169.             attr = (((cbcolor & 15) << 4) | (cfcolor & 15)) << 8;
  170.     else
  171.         if (revflag)
  172.             attr = 0x7000;
  173.         else
  174.             attr = 0x0700;
  175. #else
  176.     attr = 0x0700;
  177. #endif
  178.     lnptr = &sline[0];
  179.     for (i=0; i < term.t_ncol; i++)
  180.         *lnptr++ = SPACE | attr;
  181.  
  182.     if (flickcode && (dtype == CDCGA || dtype == CDCGA40)) {
  183.         /* wait for vertical retrace to be off */
  184.         while ((inp(0x3da) & 8))
  185.             ;
  186.     
  187.         /* and to be back on */
  188.         while ((inp(0x3da) & 8) == 0)
  189.             ;
  190.     }            
  191.  
  192.     /* and send the string out */
  193.     movmem(&sline[0], scptr[crow]+ccol, (term.t_ncol-ccol)*2);
  194.  
  195. }
  196.  
  197. PASCAL NEAR ibmputc(ch) /* put a character at the current position in the
  198.            current colors */
  199.  
  200. int ch;
  201.  
  202. {
  203.     /* if its a newline, we have to move the cursor */
  204.     if (ch == '\n' || ch == '\r') {
  205.         rg.h.ah = 3;
  206.         int86(0x10, &rg, &rg);
  207.         if (rg.h.dh == 24) {
  208.             ibmmove(20, 0);
  209.             /* we must scroll the screen */
  210.             rg.h.ah = 6;    /* scroll up */
  211.             rg.h.al = 1;    /* # of lines to scroll by */
  212.             rg.h.bh = cfcolor; /* attribute for blank line */
  213.             rg.x.cx = 0;    /* upper left corner of scroll */
  214.             rg.x.dx = 0x184f;/* lower right */
  215.             int86(0x10, &rg, &rg);
  216.             rg.h.dh = 23;
  217.         }
  218.         ibmmove(rg.h.dh + 1, 0);
  219.         return;
  220.     }
  221.  
  222.     rg.h.ah = 14;        /* write char to screen with current attrs */
  223.     rg.h.al = ch;
  224. #if    COLOR
  225.     if (dtype != CDMONO)
  226.         rg.h.bl = cfcolor;
  227.     else
  228.         rg.h.bl = 0x07;
  229. #else
  230.     rg.h.bl = 0x07;
  231. #endif
  232.     int86(0x10, &rg, &rg);
  233. }
  234.  
  235. PASCAL NEAR ibmeeop()
  236. {
  237.     int attr;        /* attribute to fill screen with */
  238.  
  239.     rg.h.ah = 6;        /* scroll page up function code */
  240.     rg.h.al = 0;        /* # lines to scroll (clear it) */
  241.     rg.x.cx = 0;        /* upper left corner of scroll */
  242.     rg.x.dx = (term.t_nrow << 8) | (term.t_ncol - 1);
  243.                 /* lower right corner of scroll */
  244. #if    COLOR
  245.     if (dtype != CDMONO)
  246.         if (revflag)
  247.             attr = ((ctrans[gfcolor] & 15) << 4) | (ctrans[gbcolor] & 15);
  248.         else
  249.             attr = ((ctrans[gbcolor] & 15) << 4) | (ctrans[gfcolor] & 15);
  250.     else
  251.         if (revflag)
  252.             attr = 70;
  253.         else
  254.             attr = 07;
  255. #else
  256.     attr = 07;
  257. #endif
  258.     rg.h.bh = attr;
  259.     int86(0x10, &rg, &rg);
  260. }
  261.  
  262. PASCAL NEAR ibmrev(state)    /* change reverse video state */
  263.  
  264. int state;    /* TRUE = reverse, FALSE = normal */
  265.  
  266. {
  267.     revflag = state;
  268. }
  269.  
  270. PASCAL NEAR ibmcres(res) /* change screen resolution */
  271.  
  272. char *res;    /* resolution to change to */
  273.  
  274. {
  275.     int i;        /* index */
  276.  
  277.     for (i = 0; i < NDRIVE; i++)
  278.         if (strcmp(res, drvname[i]) == 0) {
  279.             scinit(i);
  280.             return(TRUE);
  281.         }
  282.     return(FALSE);
  283. }
  284.  
  285. PASCAL NEAR spal(mode)    /* reset the pallette registers */
  286.  
  287. char *mode;
  288.  
  289. {
  290.     /* nothin here now..... */
  291. }
  292.  
  293. PASCAL NEAR ibmbeep()
  294. {
  295. #if    MWC
  296.     ttputc(BEL);
  297. #else
  298.     bdos(6, BEL, 0);
  299. #endif
  300. }
  301.  
  302. PASCAL NEAR ibmopen()
  303. {
  304.     scinit(CDSENSE);
  305.     revexist = TRUE;
  306.     revflag = FALSE;
  307.         ttopen();
  308. }
  309.  
  310. PASCAL NEAR ibmclose()
  311.  
  312. {
  313. #if    COLOR
  314.     ibmfcol(7);
  315.     ibmbcol(0);
  316. #endif
  317. #if    0
  318.     /* if we had the EGA open... close it */
  319.     if (dtype == CDEGA || dtype == CDVGA || dtype == CDVGA12)
  320.         egaclose();
  321. #endif
  322.  
  323.     /* exit in the same mode that we entered */
  324.     if (dtype != orig_mode)
  325.         scinit(orig_mode);
  326.  
  327.     ttclose();
  328. }
  329.  
  330. PASCAL NEAR ibmkopen()    /* open the keyboard */
  331.  
  332. {
  333.     rg.x.ax = 0x3301;
  334.     rg.h.dl = 0x00;
  335.     intdos(&rg, &rg);
  336. }
  337.  
  338. PASCAL NEAR ibmkclose() /* close the keyboard */
  339.  
  340. {
  341. }
  342.  
  343. PASCAL NEAR scinit(type) /* initialize the screen head pointers */
  344.  
  345. int type;    /* type of adapter to init for */
  346.  
  347. {
  348.     /* if asked...find out what display is connected */
  349.     if (type == CDSENSE)
  350.         type = getboard();
  351.  
  352.     /* if we have nothing to do....don't do it */
  353.     if (dtype == type)
  354.         return(TRUE);
  355.  
  356.     /* if we try to switch to EGA and there is none, don't */
  357.     if (type == CDEGA && !egaexist)
  358.         return(FALSE);
  359.  
  360.     /* if we try to switch to VGA and there is none, don't */
  361.     if (type == CDVGA && !vgaexist )
  362.         return(FALSE);
  363.  
  364.     /* if we had the EGA or VGA open... close it */
  365.     if (dtype == CDEGA || dtype == CDVGA)
  366.         egaclose();
  367.  
  368.     /* if we had the CGA40 open... close it */
  369.     if (dtype == CDCGA40)
  370.         cga40_close();
  371.  
  372.     /* and set up the various parameters as needed */
  373.     switch (type) {
  374.         case CDMONO:    /* Monochrome adapter */
  375.                 scadd = SCADM;
  376.                 newsize(TRUE, 25);
  377.                 maxlines(25);
  378.                 change_width(80);
  379.                 break;
  380.  
  381.         case CDCGA:    /* Color graphics adapter */
  382.                 scadd = SCADC;
  383.                 newsize(TRUE, 25);
  384.                 maxlines(25);
  385.                 change_width(80);
  386.                 break;
  387.  
  388.         case CDCGA40:    /* Color graphics adapter */
  389.                 scadd = SCADC;
  390.                 cga40_open();
  391.                 newsize(TRUE, 25);
  392.                 maxlines(25);
  393.                 change_width(40);
  394.                 break;
  395.  
  396.         case CDEGA:    /* Enhanced graphics adapter */
  397.                 scadd = SCADE;
  398.                 egaopen(CDEGA);
  399.                 newsize(TRUE, 43);
  400.                 maxlines(43);
  401.                 change_width(80);
  402.                 break;
  403.  
  404.         case CDVGA:    /* video graphics array - acts as EGA but more lines */
  405.                 scadd = SCADE;
  406.                 egaopen(CDVGA);
  407.                 newsize(TRUE, 50);
  408.                 maxlines(50);
  409.                 change_width(80);
  410.                 break;
  411.  
  412.         case CDVGA12:    /* Video Graphics Array 12 line mode */
  413.                 scadd = SCADE;
  414.                 egaopen(CDVGA12);
  415.                 newsize(TRUE, 12);
  416.                 maxlines(12);
  417.                 change_width(80);
  418.                 break;
  419.  
  420.     }
  421.  
  422.     /* reset the $sres environment variable */
  423.     strcpy(sres, drvname[type]);
  424.     dtype = type;
  425.  
  426.     return(TRUE);
  427. }
  428.  
  429. int PASCAL NEAR change_width(ncols)
  430.  
  431. int ncols;    /* number of columns across */
  432.  
  433. {
  434.     union {
  435.         long laddr;    /* long form of address */
  436.         int *paddr;    /* pointer form of address */
  437.     } addr;
  438.     int i;
  439.  
  440.     /* re-initialize the screen pointer array */
  441.     for (i = 0; i < NROW; i++) {
  442.         addr.laddr = scadd + (long)(ncols * i * 2);
  443.         scptr[i] = addr.paddr;
  444.     }
  445.  
  446.     /* tell the emacs main about the new width */
  447.     newwidth(TRUE, ncols);
  448. }
  449.  
  450. /* getboard:    Determine which type of display board is attached.
  451.         Current known types include:
  452.  
  453.         CDMONO    Monochrome graphics adapter
  454.         CDCGA    Color Graphics Adapter
  455.         CDEGA    Extended graphics Adapter
  456.         CDVGA    Vidio Graphics Array
  457.  
  458.         if MONO    set to MONO
  459.            CGA40 set to CGA40    test as appropriate
  460.            CGA    set to CGA    EGAexist = FALSE VGAexist = FALSE
  461.            EGA    set to CGA    EGAexist = TRUE  VGAexist = FALSE
  462.            VGA    set to CGA    EGAexist = TRUE  VGAexist = TRUE
  463. */
  464.  
  465. int PASCAL NEAR getboard()
  466.  
  467. {
  468.     int type;    /* board type to return */
  469.  
  470.     type = CDCGA;
  471.     int86(0x11, &rg, &rg);
  472.     if ((((rg.x.ax >> 4) & 3) == 3))
  473.         type = CDMONO;
  474.  
  475.     /* test for 40 col mode */
  476.     rg.h.ah = 15;
  477.     int86(0x10, &rg, &rg);
  478.     if (rg.h.al == 1)
  479.         type = CDCGA40;
  480.  
  481.     /* save the original video mode */
  482.     orig_mode = type;
  483.  
  484.     /* test if EGA present */
  485.     rg.x.ax = 0x1200;
  486.     rg.x.bx = 0xff10;
  487.     int86(0x10,&rg, &rg);        /* If EGA, bh=0-1 and bl=0-3 */
  488.     egaexist = !(rg.x.bx & 0xfefc);    /* Yes, it's EGA */
  489.     if (egaexist) {
  490.         /* Adapter says it's an EGA. We'll get the same response
  491.            from a VGA, so try to tell the two apart */
  492.         rg.x.ax = 0x1a00;    /* read display combination */
  493.         int86(0x10,&rg,&rg);
  494.         if (rg.h.al == 0x1a && (rg.h.bl == 7 || rg.h.bl == 8)) {
  495.             /* Function is supported and it's a PS/2 50,60,80 with
  496.                analog display, so it's VGA (I hope!) */
  497.             vgaexist = TRUE;
  498.         } else {
  499.             /* Either BIOS function not supported or something
  500.                other then VGA so set it to be EGA */
  501.             vgaexist = FALSE;
  502.         }
  503.     }
  504.     return(type);
  505. }
  506.  
  507. PASCAL NEAR egaopen(mode) /* init the computer to work with the EGA or VGA */
  508.  
  509. int mode;    /* mode to select [CDEGA/CDVGA] */
  510.  
  511. {
  512.     /* set the proper number of scan lines */
  513.     rg.h.ah = 18;
  514.     switch (mode) {
  515.  
  516.         case CDEGA:    rg.h.al = 1;
  517.                 break;
  518.  
  519.         case CDVGA:    rg.h.al = 2;
  520.                 break;
  521.  
  522.         case CDVGA12:    rg.h.al = 0;
  523.                 break;
  524.  
  525.     }
  526.     rg.h.bl = 48;
  527.     int86(16, &rg, &rg);
  528.  
  529.     /* put the beast into EGA 43/VGA 50 line mode */
  530.     rg.x.ax = 3;
  531.     int86(16, &rg, &rg);
  532.  
  533.     /* set the proper character set */
  534.     if (mode == CDVGA12) {
  535.         rg.h.al = 20;    /*  to 8 by 16 double dot ROM         */
  536.     } else {
  537.         rg.h.al = 18;    /*  to 8 by 8 double dot ROM         */
  538.     }
  539.     rg.h.ah = 17;        /* set char. generator function code */
  540.     rg.h.bl = 0;        /* block 0                           */
  541.     int86(16, &rg, &rg);
  542.  
  543.     /* select the alternative Print Screen function */
  544.     rg.h.ah = 18;        /* alternate select function code    */
  545.     rg.h.al = 0;        /* clear AL for no good reason       */
  546.     rg.h.bl = 32;        /* alt. print screen routine         */
  547.     int86(16, &rg, &rg);
  548.  
  549.     /* resize the cursor */
  550.     rg.h.ah = 1;        /* set cursor size function code */
  551.     rg.x.cx = 0x0607;    /* turn cursor on code */
  552.     int86(0x10, &rg, &rg);
  553.  
  554.     /* video bios bug patch */
  555.     outp(0x3d4, 10);
  556.     outp(0x3d5, 6);
  557. }
  558.  
  559. PASCAL NEAR egaclose()
  560.  
  561. {
  562.     /* put the beast into 80 column mode */
  563.     rg.x.ax = 3;
  564.     int86(16, &rg, &rg);
  565. }
  566.  
  567. PASCAL NEAR cga40_open()
  568.  
  569. {
  570.     /* put the beast into 40 column mode */
  571.     rg.x.ax = 1;
  572.     int86(16, &rg, &rg);
  573. }
  574.  
  575. PASCAL NEAR cga40_close()
  576.  
  577. {
  578.     /* put the beast into 80 column mode */
  579.     rg.x.ax = 3;
  580.     int86(16, &rg, &rg);
  581. }
  582.  
  583. PASCAL NEAR scwrite(row, outstr, forg, bacg)    /* write a line out*/
  584.  
  585. int row;    /* row of screen to place outstr on */
  586. char *outstr;    /* string to write out (must be term.t_ncol long) */
  587. int forg;    /* forground color of string to write */
  588. int bacg;    /* background color */
  589.  
  590. {
  591.     unsigned int attr;    /* attribute byte mask to place in RAM */
  592.     unsigned int *lnptr;    /* pointer to the destination line */
  593.     int i;
  594.  
  595.     /* build the attribute byte and setup the screen pointer */
  596. #if    COLOR
  597.     if (dtype != CDMONO)
  598.         if (revflag)
  599.             attr = (ctrans[forg] << 12) | (ctrans[bacg] << 8);
  600.         else
  601.             attr = (ctrans[bacg] << 12) | (ctrans[forg] << 8);
  602.     else
  603.         if ((bacg || revflag) && !(bacg && revflag))
  604.             attr = 0x7000;
  605.         else
  606.             attr = 0x0700;
  607. #else
  608.     attr = (((bacg & 15) << 4) | (forg & 15)) << 8;
  609. #endif
  610.     lnptr = &sline[0];
  611.     for (i=0; i<term.t_ncol; i++)
  612.         *lnptr++ = (outstr[i] & 255) | attr;
  613.  
  614.     if (flickcode && (dtype == CDCGA)) {
  615.         /* wait for vertical retrace to be off */
  616.         while ((inp(0x3da) & 8))
  617.             ;
  618.     
  619.         /* and to be back on */
  620.         while ((inp(0x3da) & 8) == 0)
  621.             ;
  622.     }
  623.  
  624.     /* and send the string out */
  625.     movmem(&sline[0], scptr[row],term.t_ncol*2);
  626. }
  627.  
  628. #if    FLABEL
  629. PASCAL NEAR fnclabel(f, n)    /* label a function key */
  630.  
  631. int f,n;    /* default flag, numeric argument [unused] */
  632.  
  633. {
  634.     /* on machines with no function keys...don't bother */
  635.     return(TRUE);
  636. }
  637. #endif
  638. #else
  639. ibmhello()
  640. {
  641. }
  642. #endif
  643.  
  644.