Debian Codegraph @

ibmpcdos.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9
10 #ifdef IBMPCDOS /* the body is the rest of this file */
11
12 #include "fp.h" /* scr_putchar */
13 #include "chars.h"
14 #include "screen.h"
15 #include "term.h"
16
17 /* here come the actual emulation routines */
18
19 #include <dos.h>
20 #include <conio.h>
21
22 typedef unsigned char BYTE;
23
24 #define VBS_set_cursor_position 0x02
25 #define VBS_get_cursor_position_and_size 0x03
26 #define VBS_select_active_display_page 0x05
27 #define VBS_scroll_window_up 0x06
28 #define VBS_scroll_window_down 0x07
29 #define VBS_read_character_and_attribute 0x08
30 #define VBS_write_character_and_attribute 0x09
31 #define VBS_TTY_character_output 0x0e
32 #define VBS_get_current_video_state 0x0f
33
34 #define VBSF_get_font_information 0x1130
35
36 #define VideoBIOS(r) int86(0x10, (r), (r));
37
38 private BYTE
39 c_attr = 0x07, /* current attribute white on black */
40 c_row = 0, /* current row */
41 c_col = 0; /* current column */
42
43 int
44 Txattr = 0x07, /* VAR: text-attribute (white on black) */
45 Mlattr = 0x70, /* VAR: mode-line-attribute (black on white) */
46 Hlattr = 0x10; /* VAR: highlight-attribute */
47
48 void
49 getTERM(void)
50 {
51 /* Check if 101- or 102-key keyboard is installed.
52 * This test is apparently unreliable, so we allow override.
53 * Courtesy of Ralph Brown's interrupt list.
54 */
55 char _far *kbd_stat_byte3 = (char _far *)0x00400096UL;
56 enhanced_keybrd = (0x10 & *kbd_stat_byte3) != 0;
57 pcSetTerm();
58 }
59
60 private void
61 setcolor(BYTE attr)
62 {
63 c_attr = attr;
64 }
65
66 private void
67 set_cur(void)
68 {
69 union REGS vr;
70
71 vr.h.ah = VBS_set_cursor_position;
72 vr.h.bh = 0; /* video page 0 */
73 vr.h.dl = c_col;
74 vr.h.dh = c_row;
75 VideoBIOS(&vr);
76 }
77
78 private void
79 get_cur(void)
80 {
81 union REGS vr;
82
83 vr.h.ah = VBS_get_cursor_position_and_size;
84 vr.h.bh = 0; /* video page 0 */
85 VideoBIOS(&vr);
86 c_col = vr.h.dl;
87 c_row = vr.h.dh;
88 }
89
90 private BYTE
91 chpl(void)
92 {
93 union REGS vr;
94
95 vr.h.ah = VBS_get_current_video_state;
96 VideoBIOS(&vr);
97 return vr.h.ah;
98 }
99
100 #define cur_mov(r, c) { c_row = (r); c_col = (c); set_cur(); }
101
102 private void
103 scr_win(int op, int no, int ur, int lr)
104 {
105 union REGS vr;
106
107 vr.h.ah = op; /* scroll window up or down */
108 vr.h.al = no; /* number of rows to scroll */
109
110 vr.h.ch = ur; /* upper row */
111 vr.h.cl = 0; /* left column */
112 vr.h.dh = lr; /* lower row */
113 vr.h.dl = CO-1; /* right column */
114
115 vr.h.bh = c_attr;
116 VideoBIOS(&vr);
117 }
118
119 void
120 i_lines(int top, int bottom, int num)
121 {
122 scr_win(VBS_scroll_window_down, num, top, bottom);
123 }
124
125 void
126 d_lines(int top, int bottom, int num)
127 {
128 scr_win(VBS_scroll_window_up, num, top, bottom);
129 }
130
131 void
132 clr_page(void)
133 {
134 SO_off();
135 /* Note: VBS_scroll_window_up with a count of 0 clears the screen! */
136 scr_win(VBS_scroll_window_up, 0, 0, ILI);
137 cur_mov(0, 0);
138 }
139
140 private void
141 ch_out(BYTE c, BYTE n)
142 {
143 union REGS vr;
144
145 vr.h.ah = VBS_write_character_and_attribute;
146 vr.h.al = c;
147 vr.h.bl = c_attr;
148 vr.h.bh = 0; /* video page 0 */
149 vr.x.cx = n;
150 VideoBIOS(&vr);
151 }
152
153 void
154 clr_eoln(void)
155 {
156 ch_out(' ', CO-c_col);
157 }
158
159 /* Video mode setting derived from code posted to comp.os.msdos.programmer
160 * by Joe Huffman 1990 August 15 (found on SIMTEL in msdos/screen/vidmode.zip)
161 */
162
163 private BYTE
164 lpp(void)
165 {
166 union REGS vr;
167 int lines;
168
169 vr.x.ax = VBSF_get_font_information;
170 vr.h.bh = 0; /* we don't care which pointer we get back */
171 vr.h.dl = 0; /* default, if BIOS doesn't know how to this */
172 VideoBIOS(&vr);
173 lines = vr.h.dl; /* number of last line on screen */
174 switch (lines) {
175 default:
176 return lines + 1;
177 case 25:
178 case 28:
179 case 43:
180 case 50:
181 return lines; /* IBM EGA BUG!*/
182 case 0:
183 return 25; /* Who knows? Just a guess. */
184 }
185 }
186
187 /* discover current video attribute */
188
189 private void
190 get_c_attr(void)
191 {
192 union REGS vr;
193
194 vr.h.dl = ' '; /* write out a SPace, using DOS */
195 vr.h.ah = 0x02;
196 int86(0x21, &vr, &vr);
197
198 vr.h.ah = VBS_TTY_character_output; /* backspace over it, using BIOS */
199 vr.h.al = BS;
200 vr.h.bh = 0; /* page number 0 */
201 VideoBIOS(&vr);
202
203 vr.h.ah = VBS_read_character_and_attribute; /* find out attribute */
204 VideoBIOS(&vr);
205 c_attr = vr.h.ah;
206 }
207
208 /* codes for selecting scan lines for alpha mode (service 0x12, function 0x30) */
209
210 #define EGA200 0
211 #define EGA350 1
212 #define EGA400 2
213
214 /* codes for selecting ROM font (function code for service 0x11)
215 * Notes from Ralf Brown's Interrupt List:
216 * The routines called with AL=1xh are designed to be called only
217 * immediately after a mode set and are similar to the routines called
218 * with AL=0xh, except that:
219 * Page 0 must be active.
220 * Bytes/character is recalculated.
221 * Max character rows is recalculated.
222 * CRT buffer length is recalculated.
223 * CRTC registers are reprogrammed as follows:
224 * R09 = bytes/char-1 ; max scan line (mode 7 only)
225 * R0A = bytes/char-2 ; cursor start
226 * R0B = 0 ; cursor end
227 * R12 = ((rows+1)*(bytes/char))-1 ; vertical display end
228 * R14 = bytes/char ; underline loc
229 * (*** BUG: should be 1 less ***)
230 * the current block specifiers may be determined with INT 10/AH=1Bh,
231 * looking at offsets 2Bh and 2Ch of the returned data (VGA only)
232 */
233
234 #define EGA8x8 0x12 /* not 0x02 or 0x23 */
235 #define EGA8x14 0x11 /* not 0x01 or 0x22 */
236 #define EGA8x16 0x14 /* not 0x04 or 0x24 */
237
238 private void
239 EGAsetup(BYTE scanlines, BYTE font)
240 {
241 union REGS vr;
242 vr.h.ah = VBS_select_active_display_page;
243 vr.h.al = 0; /* page 0 */
244 VideoBIOS(&vr);
245
246 vr.h.ah = 0x12; /* request EGA information */
247 vr.h.bl = 0x10; /* function number */
248 vr.x.cx = 0x0000;
249 VideoBIOS(&vr);
250
251 if (vr.x.cx != 0) {
252 /* the display seems to be EGA or better */
253 vr.h.ah = 0x12; /* select scan lines for alpha mode */
254 vr.h.al = scanlines;
255 vr.h.bl = 0x30;
256 VideoBIOS(&vr);
257
258 /* Note: bh is left over from VideoBIOS call *before* last! */
259 if (vr.h.bh == 0)
260 vr.x.ax = 0x0003; /* monochrome */
261 else
262 vr.x.ax = 0x0007; /* 80x25 color text */
263 VideoBIOS(&vr);
264
265 vr.h.ah = 0x11; /* load ROM font */
266 vr.h.al = font;
267 vr.h.bl = 0; /* into block 0 */
268 VideoBIOS(&vr);
269 }
270 get_c_attr();
271 }
272
273 private jbool
274 set_lines(int lines)
275 {
276 switch (lines) {
277 case 25:
278 EGAsetup(EGA400, EGA8x16);
279 break;
280 case 28:
281 EGAsetup(EGA400, EGA8x14);
282 break;
283 case 43:
284 EGAsetup(EGA350, EGA8x8);
285 break;
286 case 50:
287 EGAsetup(EGA400, EGA8x8);
288 break;
289 default:
290 return NO;
291 }
292 return YES;
293 }
294
295 private jbool pc_set = NO;
296 private int unsetLI;
297
298 void
299 pcSetTerm(void)
300 {
301 char *t = getenv("TERM");
302
303 if (!pc_set) {
304 int lines = lpp();
305
306 unsetLI = lines;
307 if (t != NULL) {
308 if (stricmp(t, "ega25") == 0)
309 lines = 25;
310 else if (stricmp(t, "ega28") == 0)
311 lines = 28;
312 else if (stricmp(t, "ega43") == 0)
313 lines = 43;
314 else if (stricmp(t, "ega50") == 0)
315 lines = 50;
316 }
317 if (lines != unsetLI && set_lines(lines))
318 pc_set = YES;
319 }
320 CO = chpl();
321 if (CO > MAXCOLS)
322 CO = MAXCOLS;
323 LI = lpp();
324 ILI = LI - 1;
325 get_cur();
326 }
327
328 void
329 ttsize(void)
330 {
331 }
332
333 void
334 pcUnsetTerm(void)
335 {
336 if (pc_set) {
337 pc_set = NO;
338 (void) set_lines(unsetLI);
339 }
340 }
341
342 private void
343 line_feed(void)
344 {
345 if (++c_row > ILI) {
346 c_row = ILI;
347 scr_win(VBS_scroll_window_up, 1, 0, ILI);
348 }
349 set_cur();
350 }
351
352 #define BELL_P 0x61 /* speaker */
353 #define BELL_D 0x2dc /* 550 hz */
354 #define TIME_P 0x40 /* timer */
355 #define TINI 182 /* 10110110b timer initialization */
356
357 void
358 dobell(int n) /* declared in term.h */
359 {
360 unsigned char spkr_state = inp(BELL_P);
361
362 /* ??? Should accesses to timer be run with interrupts disabled?
363 * It looks as if the timer would be in a bad state if a sequence
364 * of accesses is interrupted. -- DHR 1995 Sept.
365 */
366 outp(TIME_P+3, TINI);
367 outp(TIME_P+2, BELL_D&0xff);
368 outp(TIME_P+2, BELL_D>>8);
369 outp(BELL_P, spkr_state|3); /* turn speaker on */
370 while (n-- > 0) {
371 #ifdef NEVER
372 /* Control duration by counting half cycles of the audio tone.
373 * We detect a half-cycle boundary by noting when the high-order
374 * byte of the counter changes between zero and non-zero.
375 * This should be unaffected by CPU speed.
376 * We can't use the BIOS interval timer facility because
377 * I think it would use the timer channel we're already using.
378 */
379 int half_cycs = 200; /* number of half-cycles to play */
380 jbool hi_is_0 = NO; /* detect zero transitions; initial lie unimportant */
381
382 for (;;) {
383 (void) inp(TIME_P+2); /* low-order counter 2 byte */
384 if ((inp(TIME_P+2) == 0) != hi_is_0) {
385 hi_is_0 = !hi_is_0;
386 if (--half_cycs <= 0)
387 break;
388 }
389 }
390 #else
391 unsigned int i = 0x8888; /* a CPU-speed dependent duration */
392
393 do {} while (--i > 0);
394 #endif
395 }
396 outp(BELL_P, spkr_state);
397 }
398
399 /* scr_putchar: put char on screen. Declared in fp.h */
400
401 /* char is subject to default argument promotions */
402 void
403 scr_putchar(char c)
404 {
405 switch (c) {
406 case LF:
407 line_feed();
408 break;
409 case CR:
410 c_col = 0;
411 set_cur();
412 break;
413 case BS:
414 if (c_col > 0)
415 c_col--;
416 set_cur();
417 break;
418 case CTL('G'): /* ??? is this ever used? */
419 dobell(1);
420 break;
421 default:
422 ch_out((c), 1);
423 if (++c_col > CO-1) {
424 c_col = 0;
425 line_feed();
426 }
427 set_cur();
428 break;
429 }
430 }
431
432 /* No cursor optimization on an IBMPCDOS, this simplifies things a lot.
433 * Think about it: it would be silly!
434 */
435
436 void
437 Placur(int line, int col)
438 {
439 cur_mov(line, col);
440 CapCol = col;
441 CapLine = line;
442 }
443
444 private jbool
445 doing_so = NO,
446 doing_us = NO;
447
448 private void
449 doattr(void)
450 {
451 setcolor((doing_so? Mlattr : Txattr) ^ (doing_us? Hlattr : 0));
452 }
453
454 void
455 SO_effect(jbool f)
456 {
457 doing_so = f;
458 doattr();
459 }
460
461 void
462 US_effect(jbool f)
463 {
464 doing_us = f;
465 doattr();
466 }
467
468 #endif /* IBMPCDOS */