home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / src / baseline / jove-4.14.6.lha / jove-4.14.6 / pcscr.c < prev    next >
C/C++ Source or Header  |  1992-01-10  |  7KB  |  468 lines

  1. /***************************************************************************
  2.  * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
  3.  * is provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is    *
  5.  * included in all the files.                                              *
  6.  ***************************************************************************/
  7.  
  8. #include "jove.h"
  9.  
  10. #ifdef    IBMPC
  11.  
  12. /* here come the actual emulation routines    */
  13.  
  14. #include <dos.h>
  15. #include <conio.h>
  16.  
  17. #define BYTE    unsigned char
  18. #define WORD    unsigned int
  19.  
  20. #ifdef    MAC
  21. # undef private
  22. # define private
  23. #endif
  24.  
  25. private BYTE near get_mode proto((void));
  26.  
  27. private WORD
  28.     near cur_page proto((void)),
  29.     near get_cur proto((void));
  30.  
  31. private void
  32.     near ch_out proto((BYTE, BYTE)),
  33.     near clr_eop proto((void)),
  34.     near cur_advance proto((void)),
  35.     near cur_down proto((void)),
  36.     near cur_left proto((void)),
  37.     near cur_right proto((void)),
  38.     near cur_up proto((void)),
  39.     near line_feed proto((void)),
  40.     near set_cur proto((WORD)),
  41.     near set_mode proto((BYTE)),
  42.     near wherexy proto((BYTE *, BYTE *));
  43.  
  44. void    near normfun proto((char)),
  45.     near scr_win proto((int, BYTE, BYTE, BYTE, BYTE)),
  46.     near clr_page(),
  47.     near clr_eoln();
  48.  
  49. #ifdef    MAC
  50. # undef private
  51. # define private static
  52. #endif
  53.  
  54. #define VIDEO   0x10
  55.  
  56. #define intr(n, r)    int86((n), (r), (r));
  57.  
  58. BYTE CHPL=80,
  59.      LPP=25,
  60.      CUR_PAGE=0,
  61.      C_ATTR = 0x07,
  62.      C_X=0,
  63.      C_Y=0;
  64.  
  65. int Fgcolor = 7,
  66.     Bgcolor = 0,
  67.     Mdcolor = 0;
  68.  
  69. void setcolor(fg, bg)
  70. BYTE fg, bg;
  71. {
  72.    C_ATTR = ((bg&0xf)<<4)|(fg&0xf);
  73. }
  74.  
  75. private
  76. WORD near cur_page()
  77. {
  78.    union REGS vr;
  79.  
  80.    vr.h.ah = 0x0f;
  81.    intr(VIDEO, &vr);
  82.    return vr.h.bh;
  83. }
  84.  
  85. private
  86. void near set_cur(xy)
  87. WORD xy;
  88. {
  89.    union REGS vr;
  90.  
  91.    vr.h.bh = CUR_PAGE;
  92.    vr.h.ah = 0x02;
  93.    vr.x.dx = xy;
  94.    intr(VIDEO, &vr);
  95. }
  96.  
  97. private
  98. WORD near get_cur()
  99. {
  100.    union REGS vr;
  101.  
  102.    vr.h.bh = CUR_PAGE;
  103.    vr.h.ah = 0x03;
  104.    intr(VIDEO, &vr);
  105.    return (vr.x.dx);
  106. }
  107.  
  108. private
  109. BYTE near get_mode()
  110. {
  111.   union REGS vr;
  112.  
  113.   vr.h.ah = 0x0f;
  114.   intr(VIDEO, &vr);
  115.   return vr.h.al;
  116. }
  117.  
  118. BYTE lpp()
  119. {
  120.    int far *regen = (int far *) 0x44C;
  121.    int what;
  122.    BYTE chpl();
  123.  
  124.    what = (*regen&0xff00)/2/chpl();
  125.    return (what > 43 ? 25 : what);
  126. }
  127.  
  128. private
  129. void near set_mode(n)
  130. BYTE n;
  131. {
  132.   union REGS vr;
  133.  
  134.   vr.h.ah = 0x00;
  135.   vr.h.al = n;
  136.   intr(VIDEO, &vr);
  137. }
  138.  
  139. #define gotoxy(x,y)    set_cur((x)<<8|((y)&0xff))
  140. #define cur_mov(x,y)    set_cur((C_X=(x))<<8|((C_Y=(y))&0xff))
  141.  
  142. private
  143. void near wherexy( x, y)
  144. BYTE *x, *y;
  145. {
  146.   register WORD xy;
  147.  
  148.   xy = get_cur();
  149.   *x = xy>>8;
  150.   *y = xy&0xff;
  151. }
  152.  
  153. #define wherex()    C_X
  154. #define wherey()    C_Y
  155.  
  156. void near scr_win(no, ulr, ulc, lrr, lrc)
  157. int no;
  158. BYTE ulr, ulc, lrr, lrc;
  159. {
  160.   union REGS vr;
  161.  
  162.   if (no >= 0)
  163.      vr.h.ah = 0x06;
  164.   else {
  165.      vr.h.ah = 0x07;
  166.      no = - no;
  167.   }
  168.   vr.h.al = no;
  169.   vr.x.cx = ulr<<8 | ulc;
  170.   vr.x.dx = lrr<<8 | lrc;
  171.   vr.h.bh = C_ATTR;
  172.   intr(VIDEO, &vr);
  173. }
  174.  
  175. BYTE chpl()
  176. {
  177.   union REGS vr;
  178.  
  179.   vr.h.ah = 0x0f;
  180.   intr(VIDEO, &vr);
  181.   return vr.h.ah;
  182. }
  183.  
  184. void near
  185. clr_page()
  186. {
  187.     scr_win(0, 0, 0, LPP-1, CHPL-1);
  188.     gotoxy(C_X = 0, C_Y = 0);
  189. }
  190.  
  191. private
  192. void near cur_right()
  193. {
  194.    if (C_Y < CHPL-1)
  195.       C_Y++;
  196.    gotoxy(C_X, C_Y);
  197. }
  198.  
  199. private
  200. void near cur_up()
  201. {
  202.    if (C_X)
  203.       C_X--;
  204.    gotoxy(C_X, C_Y);
  205. }
  206.  
  207. private
  208. void near cur_left()
  209. {
  210.    if (C_Y)
  211.       C_Y--;
  212.    gotoxy(C_X, C_Y);
  213. }
  214.  
  215. private
  216. void near cur_down()
  217. {
  218.    if (C_X < LPP-1)
  219.       C_X++;
  220.    gotoxy(C_X, C_Y);
  221. }
  222.  
  223. private
  224. void near ch_out(c, n)
  225. BYTE c, n;
  226. {
  227.   union REGS vr;
  228.  
  229.   vr.h.ah = 0x09;
  230.   vr.h.al = c;
  231.   vr.h.bl = C_ATTR;
  232.   vr.h.bh = CUR_PAGE;
  233.   vr.x.cx = n;
  234.   intr(VIDEO, &vr);
  235. }
  236.  
  237. #define wrch(c)        ch_out((c), 1), cur_advance()
  238.  
  239. #define home_cur()    gotoxy(C_X = 0, C_Y = 0)
  240.  
  241. void near
  242. clr_eoln()
  243. {
  244.     ch_out(' ', CHPL-wherey());
  245. }
  246.  
  247. private
  248. void near clr_eop()
  249. {
  250.   clr_eoln();
  251.   scr_win(LPP-1-wherex(), wherex()+1, 0, LPP-1, CHPL-1);
  252. }
  253.  
  254. void init_43()
  255. {
  256.    BYTE far *info = (BYTE far *) 0x487;
  257.    WORD far *CRTC = (WORD far *) 0x463;
  258.    union REGS vr;
  259.    WORD cur;
  260.  
  261.    CUR_PAGE = cur_page();
  262.    CHPL = chpl();
  263.    LPP = lpp();
  264.  
  265.    if (get_mode()!=3)
  266.       set_mode(3);
  267.    cur = get_cur();
  268.  
  269.    vr.x.ax = 0x1112;
  270.    vr.h.bl = 0;
  271.    intr(VIDEO, &vr);
  272.  
  273.    *info |= 1;
  274.    vr.x.ax = 0x0100;
  275.    vr.h.bh = 0;
  276.    vr.x.cx = 0x0600;
  277.    intr(VIDEO, &vr);
  278.  
  279.    outp(*CRTC, 0x14);
  280.    outp(*CRTC+1, 0x07);
  281.  
  282.    vr.x.ax = 0x1200;
  283.    vr.h.bl = 0x20;
  284.    intr(VIDEO, &vr);
  285.  
  286.    LPP = lpp();
  287.  
  288.    set_cur(cur);
  289.    wherexy(&C_X, &C_Y);
  290. }
  291.  
  292. void reset_43()
  293. {
  294.    BYTE far *info = (BYTE far *) 0x487;
  295.    WORD far *CRTC = (WORD far *) 0x463;
  296.    union REGS vr;
  297.  
  298.    set_mode(3);
  299.  
  300.    *info &= 128;
  301.    vr.x.ax = 0x0100;
  302.    vr.h.bh = 0x0607;
  303.    vr.x.cx = 0x0607;
  304.    intr(VIDEO, &vr);
  305.  
  306.    outp(*CRTC, 0x14);
  307.    outp(*CRTC+1, 13);
  308.  
  309. }
  310.  
  311. #define scr_up()        scr_win(1, 0, 0, LPP-1, CHPL-1)
  312. #define back_space()    cur_left()
  313.  
  314. private
  315. void near line_feed()
  316. {
  317.    if (++C_X > LPP-1) {
  318.       C_X = LPP-1;
  319.       scr_up();
  320.    }
  321.    gotoxy(C_X, C_Y);
  322. }
  323.  
  324. #define BELL_P 0x61            /* speaker */
  325. #define BELL_D 0x2dc            /* 550 hz  */
  326. #define TIME_P 0x40            /* timer   */
  327. #define TINI   182            /* 10110110b timer initialization */
  328.  
  329. void dobell(x)
  330. {
  331.    unsigned int n = 0x8888;
  332.    int orgval;
  333.  
  334.    outp(TIME_P+3, TINI);
  335.    outp(TIME_P+2, BELL_D&0xff);
  336.    outp(TIME_P+2, BELL_D>>8);
  337.    orgval = inp(BELL_P);
  338.    outp(BELL_P, orgval|3);        /* turn speaker on  */
  339.    while (--n > 0)
  340.     outp(BELL_P, orgval);
  341. }
  342.  
  343. #define carriage_return()    gotoxy(wherex(), C_Y = 0)
  344.  
  345. private
  346. void near cur_advance()
  347. {
  348.    if (++C_Y > CHPL-1) {
  349.       C_Y = 0;
  350.       if (++C_X > LPP-1) {
  351.      scr_up();
  352.      C_X = LPP-1;
  353.       }
  354.    }
  355.    gotoxy(C_X, C_Y);
  356. }
  357.  
  358. void init_term()
  359. {
  360.    if (lpp() == 43)
  361.       reset_43();
  362.    CUR_PAGE = cur_page();
  363.    CHPL = chpl();
  364.    LPP = lpp();
  365.    wherexy(&C_X, &C_Y);
  366. }
  367.  
  368. void near normfun();
  369.  
  370. void write_em(s)
  371. char *s;
  372. {
  373.   while (*s)
  374.     normfun(*s++);
  375. }
  376.  
  377. void write_emif(s)
  378. char *s;
  379. {
  380.   if (s)
  381.      write_em(s);
  382. }
  383.  
  384. void write_emc(s, n)
  385. char *s;
  386. int n;
  387. {
  388.    while (n--)
  389.      normfun(*s++);
  390. }
  391.  
  392. void near normfun(c)
  393. char c;
  394. {
  395.     switch (c) {
  396.     case 10: line_feed(); break;
  397.     case 13: carriage_return(); break;
  398.     case  8: back_space(); break;
  399.     case  7: dobell(0); break;
  400.     case  0: break;
  401.     default: wrch(c);
  402.     }
  403. }
  404.  
  405. #endif    /* IBMPC */
  406.  
  407. #ifdef    IBMPC
  408.  
  409. /* No cursor optimization on an IBMPC, this simplifies things a lot.
  410.    Think about it: it would be silly!
  411.  */
  412.  
  413. int    phystab = 8;
  414.  
  415. void
  416. Placur(line, col)
  417. {
  418.     cur_mov(line, col);
  419.     CapCol = col;
  420.     CapLine = line;
  421. }
  422.  
  423. void
  424. SO_on()
  425. {
  426.     if (Mdcolor)
  427.         setcolor(Mdcolor&0xf, Mdcolor>>4);
  428.     else
  429.         setcolor(Bgcolor, Fgcolor);
  430. }
  431.  
  432. void
  433. SO_off()
  434. {
  435.    setcolor(Fgcolor, Bgcolor);
  436. }
  437.  
  438. extern bool EGA;
  439.  
  440. void
  441.  
  442. UnsetTerm(foo)
  443. char *foo;
  444. {
  445.   extern int ILI;
  446.  
  447.   Placur(ILI, 0);
  448.   clr_eoln();
  449.   if (EGA)
  450.      reset_43();
  451. }
  452.  
  453.  
  454. void
  455. ResetTerm()
  456. {
  457.     if (EGA)
  458.        init_43();
  459.     else
  460.        init_term();
  461.  
  462.     do_sgtty();        /* this is so if you change baudrate or stuff
  463.                    like that, JOVE will notice. */
  464.     ttyset(ON);
  465. }
  466.  
  467. #endif
  468.