home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 001-099 / ff093.lzh / MicroEmacs / source / src.arc / ibmpc.c < prev    next >
C/C++ Source or Header  |  1987-08-16  |  10KB  |  465 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        "edef.h"
  13.  
  14. #if     IBMPC
  15. #define NROW    43            /* Max Screen size.        */
  16. #define NCOL    80                      /* Edit if you want to.         */
  17. #define    MARGIN    8            /* size of minimim margin and    */
  18. #define    SCRSIZ    64            /* scroll size for extended lines */
  19. #define    NPAUSE    200            /* # times thru update to pause */
  20. #define BEL     0x07                    /* BEL character.               */
  21. #define ESC     0x1B                    /* ESC character.               */
  22. #define    SPACE    32            /* space character        */
  23.  
  24. #define    SCADC    0xb8000000L        /* CGA address of screen RAM    */
  25. #define    SCADM    0xb0000000L        /* MONO address of screen RAM    */
  26. #define SCADE    0xb8000000L        /* EGA address of screen RAM    */
  27.  
  28. #define MONOCRSR 0x0B0D            /* monochrome cursor        */
  29. #define CGACRSR 0x0607            /* CGA cursor            */
  30. #define EGACRSR 0x0709            /* EGA cursor            */
  31.  
  32. #define    CDCGA    0            /* color graphics card        */
  33. #define    CDMONO    1            /* monochrome text card        */
  34. #define    CDEGA    2            /* EGA color adapter        */
  35. #define    CDSENSE    9            /* detect the card type        */
  36.  
  37. #define NDRIVE    3            /* number of screen drivers    */
  38.  
  39. int dtype = -1;                /* current display type        */
  40. char drvname[][8] = {            /* screen resolution names    */
  41.     "CGA", "MONO", "EGA"
  42. };
  43. long scadd;                /* address of screen ram    */
  44. int *scptr[NROW];            /* pointer to screen lines    */
  45. int sline[NCOL];            /* screen line image        */
  46. int egaexist = FALSE;            /* is an EGA card available?    */
  47. extern union REGS rg;            /* cpu register for use of DOS calls */
  48.  
  49. extern  int     ttopen();               /* Forward references.          */
  50. extern  int     ttgetc();
  51. extern  int     ttputc();
  52. extern  int     ttflush();
  53. extern  int     ttclose();
  54. extern  int     ibmmove();
  55. extern  int     ibmeeol();
  56. extern  int     ibmeeop();
  57. extern  int     ibmbeep();
  58. extern  int     ibmopen();
  59. extern    int    ibmrev();
  60. extern    int    ibmcres();
  61. extern    int    ibmclose();
  62. extern    int    ibmputc();
  63. extern    int    ibmkopen();
  64. extern    int    ibmkclose();
  65.  
  66. #if    COLOR
  67. extern    int    ibmfcol();
  68. extern    int    ibmbcol();
  69.  
  70. int    cfcolor = -1;        /* current forground color */
  71. int    cbcolor = -1;        /* current background color */
  72. int    ctrans[] =        /* ansi to ibm color translation table */
  73.     {0, 4, 2, 6, 1, 5, 3, 7};
  74. #endif
  75.  
  76. /*
  77.  * Standard terminal interface dispatch table. Most of the fields point into
  78.  * "termio" code.
  79.  */
  80. TERM    term    = {
  81.     NROW-1,
  82.         NROW-1,
  83.         NCOL,
  84.         NCOL,
  85.     MARGIN,
  86.     SCRSIZ,
  87.     NPAUSE,
  88.         ibmopen,
  89.         ibmclose,
  90.     ibmkopen,
  91.     ibmkclose,
  92.         ttgetc,
  93.     ibmputc,
  94.         ttflush,
  95.         ibmmove,
  96.         ibmeeol,
  97.         ibmeeop,
  98.         ibmbeep,
  99.     ibmrev,
  100.     ibmcres
  101. #if    COLOR
  102.     , ibmfcol,
  103.     ibmbcol
  104. #endif
  105. };
  106.  
  107. extern union REGS rg;
  108.  
  109. #if    COLOR
  110. ibmfcol(color)        /* set the current output color */
  111.  
  112. int color;    /* color to set */
  113.  
  114. {
  115.     cfcolor = ctrans[color];
  116. }
  117.  
  118. ibmbcol(color)        /* set the current background color */
  119.  
  120. int color;    /* color to set */
  121.  
  122. {
  123.         cbcolor = ctrans[color];
  124. }
  125. #endif
  126.  
  127. ibmmove(row, col)
  128. {
  129.     rg.h.ah = 2;        /* set cursor position function code */
  130.     rg.h.dl = col;
  131.     rg.h.dh = row;
  132.     rg.h.bh = 0;        /* set screen page number */
  133.     int86(0x10, &rg, &rg);
  134. }
  135.  
  136. ibmeeol()    /* erase to the end of the line */
  137.  
  138. {
  139.     int attr;    /* attribute byte mask to place in RAM */
  140.     int *lnptr;    /* pointer to the destination line */
  141.     int i;
  142.     int ccol;    /* current column cursor lives */
  143.     int crow;    /*       row    */
  144.  
  145.     /* find the current cursor position */
  146.     rg.h.ah = 3;        /* read cursor position function code */
  147.     rg.h.bh = 0;        /* current video page */
  148.     int86(0x10, &rg, &rg);
  149.     ccol = rg.h.dl;        /* record current column */
  150.     crow = rg.h.dh;        /* and row */
  151.  
  152.     /* build the attribute byte and setup the screen pointer */
  153. #if    COLOR
  154.     if (dtype != CDMONO)
  155.         attr = (((cbcolor & 15) << 4) | (cfcolor & 15)) << 8;
  156.     else
  157.         attr = 0x0700;
  158. #else
  159.     attr = 0x0700;
  160. #endif
  161.     lnptr = &sline[0];
  162.     for (i=0; i < term.t_ncol; i++)
  163.         *lnptr++ = SPACE | attr;
  164.  
  165.     if (flickcode && (dtype == CDCGA)) {
  166.         /* wait for vertical retrace to be off */
  167.         while ((inp(0x3da) & 8))
  168.             ;
  169.     
  170.         /* and to be back on */
  171.         while ((inp(0x3da) & 8) == 0)
  172.             ;
  173.     }            
  174.  
  175.     /* and send the string out */
  176.     movmem(&sline[0], scptr[crow]+ccol, (term.t_ncol-ccol)*2);
  177.  
  178. }
  179.  
  180. ibmputc(ch)    /* put a character at the current position in the
  181.            current colors */
  182.  
  183. int ch;
  184.  
  185. {
  186.     rg.h.ah = 14;        /* write char to screen with current attrs */
  187.     rg.h.al = ch;
  188. #if    COLOR
  189.     if (dtype != CDMONO)
  190.         rg.h.bl = cfcolor;
  191.     else
  192.         rg.h.bl = 0x07;
  193. #else
  194.     rg.h.bl = 0x07;
  195. #endif
  196.     int86(0x10, &rg, &rg);
  197. }
  198.  
  199. ibmeeop()
  200. {
  201.     int attr;        /* attribute to fill screen with */
  202.  
  203.     rg.h.ah = 6;        /* scroll page up function code */
  204.     rg.h.al = 0;        /* # lines to scroll (clear it) */
  205.     rg.x.cx = 0;        /* upper left corner of scroll */
  206.     rg.x.dx = (term.t_nrow << 8) | (term.t_ncol - 1);
  207.                 /* lower right corner of scroll */
  208. #if    COLOR
  209.     if (dtype != CDMONO)
  210.         attr = ((ctrans[gbcolor] & 15) << 4) | (ctrans[gfcolor] & 15);
  211.     else
  212.         attr = 0;
  213. #else
  214.     attr = 0;
  215. #endif
  216.     rg.h.bh = attr;
  217.     int86(0x10, &rg, &rg);
  218. }
  219.  
  220. ibmrev(state)        /* change reverse video state */
  221.  
  222. int state;    /* TRUE = reverse, FALSE = normal */
  223.  
  224. {
  225.     /* This never gets used under the IBM-PC driver */
  226. }
  227.  
  228. ibmcres(res)    /* change screen resolution */
  229.  
  230. char *res;    /* resolution to change to */
  231.  
  232. {
  233.     int i;        /* index */
  234.  
  235.     for (i = 0; i < NDRIVE; i++)
  236.         if (strcmp(res, drvname[i]) == 0) {
  237.             scinit(i);
  238.             return(TRUE);
  239.         }
  240.     return(FALSE);
  241. }
  242.  
  243. spal()    /* reset the pallette registers */
  244.  
  245. {
  246.     /* nothin here now..... */
  247. }
  248.  
  249. ibmbeep()
  250. {
  251. #if    MWC86
  252.     putcnb(BEL);
  253. #else
  254.     bdos(6, BEL, 0);
  255. #endif
  256. }
  257.  
  258. ibmopen()
  259. {
  260.     scinit(CDSENSE);
  261.     revexist = TRUE;
  262.         ttopen();
  263. }
  264.  
  265. ibmclose()
  266.  
  267. {
  268. #if    COLOR
  269.     ibmfcol(7);
  270.     ibmbcol(0);
  271. #endif
  272.     /* if we had the EGA open... close it */
  273.     if (dtype == CDEGA)
  274.         egaclose();
  275.  
  276.     ttclose();
  277. }
  278.  
  279. ibmkopen()    /* open the keyboard */
  280.  
  281. {
  282. }
  283.  
  284. ibmkclose()    /* close the keyboard */
  285.  
  286. {
  287. }
  288.  
  289. scinit(type)    /* initialize the screen head pointers */
  290.  
  291. int type;    /* type of adapter to init for */
  292.  
  293. {
  294.     union {
  295.         long laddr;    /* long form of address */
  296.         int *paddr;    /* pointer form of address */
  297.     } addr;
  298.     int i;
  299.  
  300.     /* if asked...find out what display is connected */
  301.     if (type == CDSENSE)
  302.         type = getboard();
  303.  
  304.     /* if we have nothing to do....don't do it */
  305.     if (dtype == type)
  306.         return(TRUE);
  307.  
  308.     /* if we try to switch to EGA and there is none, don't */
  309.     if (type == CDEGA && egaexist != TRUE)
  310.         return(FALSE);
  311.  
  312.     /* if we had the EGA open... close it */
  313.     if (dtype == CDEGA)
  314.         egaclose();
  315.  
  316.     /* and set up the various parameters as needed */
  317.     switch (type) {
  318.         case CDMONO:    /* Monochrome adapter */
  319.                 scadd = SCADM;
  320.                 newsize(TRUE, 25);
  321.                 break;
  322.  
  323.         case CDCGA:    /* Color graphics adapter */
  324.                 scadd = SCADC;
  325.                 newsize(TRUE, 25);
  326.                 break;
  327.  
  328.         case CDEGA:    /* Enhanced graphics adapter */
  329.                 scadd = SCADE;
  330.                 egaopen();
  331.                 newsize(TRUE, 43);
  332.                 break;
  333.     }
  334.  
  335.     /* reset the $sres environment variable */
  336.     strcpy(sres, drvname[type]);
  337.     dtype = type;
  338.  
  339.     /* initialize the screen pointer array */
  340.     for (i = 0; i < NROW; i++) {
  341.         addr.laddr = scadd + (long)(NCOL * i * 2);
  342.         scptr[i] = addr.paddr;
  343.     }
  344. }
  345.  
  346. /* getboard:    Determine which type of display board is attached.
  347.         Current known types include:
  348.  
  349.         CDMONO    Monochrome graphics adapter
  350.         CDCGA    Color Graphics Adapter
  351.         CDEGA    Extended graphics Adapter
  352. */
  353.  
  354. /* getbaord:    Detect the current display adapter
  355.         if MONO        set to MONO
  356.            CGA        set to CGA    EGAexist = FALSE
  357.            EGA        set to CGA    EGAexist = TRUE
  358. */
  359.  
  360. int getboard()
  361.  
  362. {
  363.     int type;    /* board type to return */
  364.  
  365.     type = CDCGA;
  366.     int86(0x11, &rg, &rg);
  367.     if ((((rg.x.ax >> 4) & 3) == 3))
  368.         type = CDMONO;
  369.  
  370.     /* test if EGA present */
  371.     rg.x.ax = 0x1200;
  372.     rg.x.bx = 0xff10;
  373.     int86(0x10,&rg, &rg);        /* If EGA, bh=0-1 and bl=0-3 */
  374.     egaexist = !(rg.x.bx & 0xfefc);    /* Yes, it's EGA */
  375.     return(type);
  376. }
  377.  
  378. egaopen()    /* init the computer to work with the EGA */
  379.  
  380. {
  381.     /* put the beast into EGA 43 row mode */
  382.     rg.x.ax = 3;
  383.     int86(16, &rg, &rg);
  384.  
  385.     rg.h.ah = 17;        /* set char. generator function code */
  386.     rg.h.al = 18;        /*  to 8 by 8 double dot ROM         */
  387.     rg.h.bl = 0;        /* block 0                           */
  388.     int86(16, &rg, &rg);
  389.  
  390.     rg.h.ah = 18;        /* alternate select function code    */
  391.     rg.h.al = 0;        /* clear AL for no good reason       */
  392.     rg.h.bl = 32;        /* alt. print screen routine         */
  393.     int86(16, &rg, &rg);
  394.  
  395.     rg.h.ah = 1;        /* set cursor size function code */
  396.     rg.x.cx = 0x0607;    /* turn cursor on code */
  397.     int86(0x10, &rg, &rg);
  398.  
  399.     outp(0x3d4, 10);    /* video bios bug patch */
  400.     outp(0x3d5, 6);
  401. }
  402.  
  403. egaclose()
  404.  
  405. {
  406.     /* put the beast into 80 column mode */
  407.     rg.x.ax = 3;
  408.     int86(16, &rg, &rg);
  409. }
  410.  
  411. scwrite(row, outstr, forg, bacg)    /* write a line out*/
  412.  
  413. int row;    /* row of screen to place outstr on */
  414. char *outstr;    /* string to write out (must be term.t_ncol long) */
  415. int forg;    /* forground color of string to write */
  416. int bacg;    /* background color */
  417.  
  418. {
  419.     int attr;    /* attribute byte mask to place in RAM */
  420.     int *lnptr;    /* pointer to the destination line */
  421.     int i;
  422.  
  423.     /* build the attribute byte and setup the screen pointer */
  424. #if    COLOR
  425.     if (dtype != CDMONO)
  426.         attr = (((ctrans[bacg] & 15) << 4) | (ctrans[forg] & 15)) << 8;
  427.     else
  428.         attr = (((bacg & 15) << 4) | (forg & 15)) << 8;
  429. #else
  430.     attr = (((bacg & 15) << 4) | (forg & 15)) << 8;
  431. #endif
  432.     lnptr = &sline[0];
  433.     for (i=0; i<term.t_ncol; i++)
  434.         *lnptr++ = (outstr[i] & 255) | attr;
  435.  
  436.     if (flickcode && (dtype == CDCGA)) {
  437.         /* wait for vertical retrace to be off */
  438.         while ((inp(0x3da) & 8))
  439.             ;
  440.     
  441.         /* and to be back on */
  442.         while ((inp(0x3da) & 8) == 0)
  443.             ;
  444.     }
  445.  
  446.     /* and send the string out */
  447.     movmem(&sline[0], scptr[row],term.t_ncol*2);
  448. }
  449.  
  450. #if    FLABEL
  451. fnclabel(f, n)        /* label a function key */
  452.  
  453. int f,n;    /* default flag, numeric argument [unused] */
  454.  
  455. {
  456.     /* on machines with no function keys...don't bother */
  457.     return(TRUE);
  458. }
  459. #endif
  460. #else
  461. ibmhello()
  462. {
  463. }
  464. #endif
  465.