crt.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. {
  2. $Id$
  3. }
  4. unit crt;
  5. interface
  6. const
  7. { CRT modes }
  8. BW40 = 0; { 40x25 B/W on Color Adapter }
  9. CO40 = 1; { 40x25 Color on Color Adapter }
  10. BW80 = 2; { 80x25 B/W on Color Adapter }
  11. CO80 = 3; { 80x25 Color on Color Adapter }
  12. Mono = 7; { 80x25 on Monochrome Adapter }
  13. Font8x8 = 256; { Add-in for ROM font }
  14. { Mode constants for 3.0 compatibility }
  15. C40 = CO40;
  16. C80 = CO80;
  17. { Foreground and background color constants }
  18. Black = 0;
  19. Blue = 1;
  20. Green = 2;
  21. Cyan = 3;
  22. Red = 4;
  23. Magenta = 5;
  24. Brown = 6;
  25. LightGray = 7;
  26. { Foreground color constants }
  27. DarkGray = 8;
  28. LightBlue = 9;
  29. LightGreen = 10;
  30. LightCyan = 11;
  31. LightRed = 12;
  32. LightMagenta = 13;
  33. Yellow = 14;
  34. White = 15;
  35. { Add-in for blinking }
  36. Blink = 128;
  37. var
  38. { Interface variables }
  39. CheckBreak: Boolean; { Enable Ctrl-Break }
  40. CheckEOF: Boolean; { Enable Ctrl-Z }
  41. DirectVideo: Boolean; { Enable direct video addressing }
  42. CheckSnow: Boolean; { Enable snow filtering }
  43. LastMode: Word; { Current text mode }
  44. TextAttr: Byte; { Current text attribute }
  45. WindMin: Word; { Window upper left coordinates }
  46. WindMax: Word; { Window lower right coordinates }
  47. { Interface procedures }
  48. procedure AssignCrt(var F: Text);
  49. function KeyPressed: Boolean;
  50. function ReadKey: Char;
  51. procedure TextMode(Mode: Integer);
  52. procedure Window(X1,Y1,X2,Y2: Byte);
  53. procedure GotoXY(X,Y: Byte);
  54. function WhereX: Byte;
  55. function WhereY: Byte;
  56. procedure ClrScr;
  57. procedure ClrEol;
  58. procedure InsLine;
  59. procedure DelLine;
  60. procedure TextColor(Color: Byte);
  61. procedure TextBackground(Color: Byte);
  62. procedure LowVideo;
  63. procedure HighVideo;
  64. procedure NormVideo;
  65. procedure Delay(MS: Word);
  66. procedure Sound(Hz: Word);
  67. procedure NoSound;
  68. {Extra Functions}
  69. procedure cursoron;
  70. procedure cursoroff;
  71. procedure cursorbig;
  72. implementation
  73. uses
  74. watcom;
  75. {$ASMMODE ATT}
  76. var
  77. DelayCnt,
  78. ScreenWidth,
  79. ScreenHeight : longint;
  80. VidSeg : Word;
  81. {
  82. definition of textrec is in textrec.inc
  83. }
  84. {$i textrec.inc}
  85. {****************************************************************************
  86. Low level Routines
  87. ****************************************************************************}
  88. procedure setscreenmode(mode : byte);
  89. var
  90. regs : trealregs;
  91. begin
  92. regs.realeax:=mode;
  93. realintr($10,regs);
  94. end;
  95. function GetScreenHeight : longint;
  96. begin
  97. getscreenheight:=mem[$40:$84]+1;
  98. If mem[$40:$84]=0 then
  99. getscreenheight := 25;
  100. end;
  101. function GetScreenWidth : longint;
  102. begin
  103. getscreenwidth:=memw[$40:$4a];
  104. end;
  105. procedure SetScreenCursor(x,y : longint);
  106. var
  107. regs : trealregs;
  108. begin
  109. regs.realeax:=$0200;
  110. regs.realebx:=0;
  111. regs.realedx:=(y-1) shl 8+(x-1);
  112. realintr($10,regs);
  113. end;
  114. procedure GetScreenCursor(var x,y : longint);
  115. begin
  116. x:=mem[$40:$50]+1;
  117. y:=mem[$40:$51]+1;
  118. end;
  119. {****************************************************************************
  120. Helper Routines
  121. ****************************************************************************}
  122. Function WinMinX: Byte;
  123. {
  124. Current Minimum X coordinate
  125. }
  126. Begin
  127. WinMinX:=(WindMin and $ff)+1;
  128. End;
  129. Function WinMinY: Byte;
  130. {
  131. Current Minimum Y Coordinate
  132. }
  133. Begin
  134. WinMinY:=(WindMin shr 8)+1;
  135. End;
  136. Function WinMaxX: Byte;
  137. {
  138. Current Maximum X coordinate
  139. }
  140. Begin
  141. WinMaxX:=(WindMax and $ff)+1;
  142. End;
  143. Function WinMaxY: Byte;
  144. {
  145. Current Maximum Y coordinate;
  146. }
  147. Begin
  148. WinMaxY:=(WindMax shr 8) + 1;
  149. End;
  150. Function FullWin:boolean;
  151. {
  152. Full Screen 80x25? Window(1,1,80,25) is used, allows faster routines
  153. }
  154. begin
  155. FullWin:=(WinMinX=1) and (WinMinY=1) and
  156. (WinMaxX=ScreenWidth) and (WinMaxY=ScreenHeight);
  157. end;
  158. {****************************************************************************
  159. Public Crt Functions
  160. ****************************************************************************}
  161. procedure textmode(mode : integer);
  162. var
  163. regs : trealregs;
  164. begin
  165. lastmode:=mode;
  166. mode:=mode and $ff;
  167. setscreenmode(mode);
  168. { set 8x8 font }
  169. if (lastmode and $100)<>0 then
  170. begin
  171. regs.realeax:=$1112;
  172. regs.realebx:=$0;
  173. realintr($10,regs);
  174. end;
  175. screenwidth:=getscreenwidth;
  176. screenheight:=getscreenheight;
  177. windmin:=0;
  178. windmax:=(screenwidth-1) or ((screenheight-1) shl 8);
  179. end;
  180. Procedure TextColor(Color: Byte);
  181. {
  182. Switch foregroundcolor
  183. }
  184. Begin
  185. TextAttr:=(Color and $f) or (TextAttr and $70);
  186. If (Color>15) Then TextAttr:=TextAttr Or Blink;
  187. End;
  188. Procedure TextBackground(Color: Byte);
  189. {
  190. Switch backgroundcolor
  191. }
  192. Begin
  193. TextAttr:=((Color shl 4) and ($f0 and not Blink)) or (TextAttr and ($0f OR Blink) );
  194. End;
  195. Procedure HighVideo;
  196. {
  197. Set highlighted output.
  198. }
  199. Begin
  200. TextColor(TextAttr Or $08);
  201. End;
  202. Procedure LowVideo;
  203. {
  204. Set normal output
  205. }
  206. Begin
  207. TextColor(TextAttr And $77);
  208. End;
  209. Procedure NormVideo;
  210. {
  211. Set normal back and foregroundcolors.
  212. }
  213. Begin
  214. TextColor(7);
  215. TextBackGround(0);
  216. End;
  217. Procedure GotoXy(X: Byte; Y: Byte);
  218. {
  219. Go to coordinates X,Y in the current window.
  220. }
  221. Begin
  222. If (X>0) and (X<=WinMaxX- WinMinX+1) and
  223. (Y>0) and (Y<=WinMaxY-WinMinY+1) Then
  224. Begin
  225. Inc(X,WinMinX-1);
  226. Inc(Y,WinMinY-1);
  227. SetScreenCursor(x,y);
  228. End;
  229. End;
  230. Procedure Window(X1, Y1, X2, Y2: Byte);
  231. {
  232. Set screen window to the specified coordinates.
  233. }
  234. Begin
  235. if (X1>X2) or (X2>ScreenWidth) or
  236. (Y1>Y2) or (Y2>ScreenHeight) then
  237. exit;
  238. WindMin:=((Y1-1) Shl 8)+(X1-1);
  239. WindMax:=((Y2-1) Shl 8)+(X2-1);
  240. GoToXY(1,1);
  241. End;
  242. Procedure ClrScr;
  243. {
  244. Clear the current window, and set the cursor on 1,1
  245. }
  246. var
  247. fil : word;
  248. y : longint;
  249. begin
  250. fil:=32 or (textattr shl 8);
  251. if FullWin then
  252. DosmemFillWord(VidSeg,0,ScreenHeight*ScreenWidth,fil)
  253. else
  254. begin
  255. for y:=WinMinY to WinMaxY do
  256. DosmemFillWord(VidSeg,((y-1)*ScreenWidth+(WinMinX-1))*2,WinMaxX-WinMinX+1,fil);
  257. end;
  258. Gotoxy(1,1);
  259. end;
  260. Procedure ClrEol;
  261. {
  262. Clear from current position to end of line.
  263. }
  264. var
  265. x,y : longint;
  266. fil : word;
  267. Begin
  268. GetScreenCursor(x,y);
  269. fil:=32 or (textattr shl 8);
  270. if x<=WinMaxX then
  271. DosmemFillword(VidSeg,((y-1)*ScreenWidth+(x-1))*2,WinMaxX-x+1,fil);
  272. End;
  273. Function WhereX: Byte;
  274. {
  275. Return current X-position of cursor.
  276. }
  277. var
  278. x,y : longint;
  279. Begin
  280. GetScreenCursor(x,y);
  281. WhereX:=x-WinMinX+1;
  282. End;
  283. Function WhereY: Byte;
  284. {
  285. Return current Y-position of cursor.
  286. }
  287. var
  288. x,y : longint;
  289. Begin
  290. GetScreenCursor(x,y);
  291. WhereY:=y-WinMinY+1;
  292. End;
  293. {*************************************************************************
  294. KeyBoard
  295. *************************************************************************}
  296. var
  297. is_last : boolean;
  298. last : char;
  299. function readkey : char;
  300. var
  301. char2 : char;
  302. char1 : char;
  303. regs : trealregs;
  304. begin
  305. if is_last then
  306. begin
  307. is_last:=false;
  308. readkey:=last;
  309. end
  310. else
  311. begin
  312. regs.ah:=$10;
  313. realintr($16,regs);
  314. if (regs.al=$e0) and (regs.ah<>0) then
  315. regs.al:=0;
  316. char1:=chr(regs.al);
  317. char2:=chr(regs.ah);
  318. if char1=#0 then
  319. begin
  320. is_last:=true;
  321. last:=char2;
  322. end;
  323. readkey:=char1;
  324. end;
  325. end;
  326. function keypressed : boolean;
  327. var
  328. regs : trealregs;
  329. begin
  330. if is_last then
  331. begin
  332. keypressed:=true;
  333. exit;
  334. end
  335. else
  336. begin
  337. regs.ah:=$11;
  338. realintr($16,regs);
  339. keypressed:=((regs.realflags and zeroflag) = 0);
  340. end;
  341. end;
  342. {*************************************************************************
  343. Delay
  344. *************************************************************************}
  345. procedure Delayloop;assembler;
  346. asm
  347. .LDelayLoop1:
  348. subl $1,%eax
  349. jc .LDelayLoop2
  350. cmpl %fs:(%edi),%ebx
  351. je .LDelayLoop1
  352. .LDelayLoop2:
  353. end;
  354. procedure initdelay;assembler;
  355. asm
  356. pushl %ebx
  357. pushl %edi
  358. { for some reason, using int $31/ax=$901 doesn't work here }
  359. { and interrupts are always disabled at this point when }
  360. { running a program inside gdb(pas). Web bug 1345 (JM) }
  361. sti
  362. movl $0x46c,%edi
  363. movl $-28,%edx
  364. movl %fs:(%edi),%ebx
  365. .LInitDel1:
  366. cmpl %fs:(%edi),%ebx
  367. je .LInitDel1
  368. movl %fs:(%edi),%ebx
  369. movl %edx,%eax
  370. call DelayLoop
  371. notl %eax
  372. xorl %edx,%edx
  373. movl $55,%ecx
  374. divl %ecx
  375. movl %eax,DelayCnt
  376. popl %edi
  377. popl %ebx
  378. end;
  379. procedure Delay(MS: Word);assembler;
  380. asm
  381. pushl %ebx
  382. pushl %edi
  383. movzwl MS,%ecx
  384. jecxz .LDelay2
  385. movl $0x400,%edi
  386. movl DelayCnt,%edx
  387. movl %fs:(%edi),%ebx
  388. .LDelay1:
  389. movl %edx,%eax
  390. call DelayLoop
  391. loop .LDelay1
  392. .LDelay2:
  393. popl %edi
  394. popl %ebx
  395. end;
  396. procedure sound(hz : word);
  397. begin
  398. if hz=0 then
  399. begin
  400. nosound;
  401. exit;
  402. end;
  403. asm
  404. movzwl hz,%ecx
  405. movl $1193046,%eax
  406. cltd
  407. divl %ecx
  408. movl %eax,%ecx
  409. inb $0x61,%al
  410. testb $0x3,%al
  411. jnz .Lsound_next
  412. orb $0x3,%al
  413. outb %al,$0x61
  414. movb $0xb6,%al
  415. outb %al,$0x43
  416. .Lsound_next:
  417. movb %cl,%al
  418. outb %al,$0x42
  419. movb %ch,%al
  420. outb %al,$0x42
  421. end ['EAX','ECX','EDX'];
  422. end;
  423. procedure nosound;
  424. begin
  425. asm
  426. inb $0x61,%al
  427. andb $0xfc,%al
  428. outb %al,$0x61
  429. end ['EAX'];
  430. end;
  431. {****************************************************************************
  432. HighLevel Crt Functions
  433. ****************************************************************************}
  434. procedure removeline(y : longint);
  435. var
  436. fil : word;
  437. begin
  438. fil:=32 or (textattr shl 8);
  439. y:=WinMinY+y-1;
  440. While (y<WinMaxY) do
  441. begin
  442. dosmemmove(VidSeg,(y*ScreenWidth+(WinMinX-1))*2,
  443. VidSeg,((y-1)*ScreenWidth+(WinMinX-1))*2,(WinMaxX-WinMinX+1)*2);
  444. inc(y);
  445. end;
  446. dosmemfillword(VidSeg,((WinMaxY-1)*ScreenWidth+(WinMinX-1))*2,(WinMaxX-WinMinX+1),fil);
  447. end;
  448. procedure delline;
  449. begin
  450. removeline(wherey);
  451. end;
  452. procedure insline;
  453. var
  454. my,y : longint;
  455. fil : word;
  456. begin
  457. fil:=32 or (textattr shl 8);
  458. y:=WhereY;
  459. my:=WinMaxY-WinMinY;
  460. while (my>=y) do
  461. begin
  462. dosmemmove(VidSeg,(((WinMinY+my-1)-1)*ScreenWidth+(WinMinX-1))*2,
  463. VidSeg,(((WinMinY+my)-1)*ScreenWidth+(WinMinX-1))*2,(WinMaxX-WinMinX+1)*2);
  464. dec(my);
  465. end;
  466. dosmemfillword(VidSeg,(((WinMinY+y-1)-1)*ScreenWidth+(WinMinX-1))*2,(WinMaxX-WinMinX+1),fil);
  467. end;
  468. {****************************************************************************
  469. Extra Crt Functions
  470. ****************************************************************************}
  471. procedure cursoron;
  472. var
  473. regs : trealregs;
  474. begin
  475. regs.realeax:=$0100;
  476. regs.realecx:=$90A;
  477. If VidSeg=$b800 then
  478. regs.realecx:=$90A
  479. else
  480. regs.realecx:=$b0d;
  481. realintr($10,regs);
  482. end;
  483. procedure cursoroff;
  484. var
  485. regs : trealregs;
  486. begin
  487. regs.realeax:=$0100;
  488. regs.realecx:=$ffff;
  489. realintr($10,regs);
  490. end;
  491. procedure cursorbig;
  492. var
  493. regs : trealregs;
  494. begin
  495. regs.realeax:=$0100;
  496. regs.realecx:=$10A;
  497. realintr($10,regs);
  498. end;
  499. {*****************************************************************************
  500. Read and Write routines
  501. *****************************************************************************}
  502. var
  503. CurrX,CurrY : longint;
  504. Procedure WriteChar(c:char);
  505. var
  506. regs : trealregs;
  507. begin
  508. case c of
  509. #10 : inc(CurrY);
  510. #13 : CurrX:=WinMinX;
  511. #8 : begin
  512. if CurrX>WinMinX then
  513. dec(CurrX);
  514. end;
  515. #7 : begin { beep }
  516. regs.dl:=7;
  517. regs.ah:=2;
  518. realintr($21,regs);
  519. end;
  520. else
  521. begin
  522. memw[VidSeg:((CurrY-1)*ScreenWidth+(CurrX-1))*2]:=(textattr shl 8) or byte(c);
  523. inc(CurrX);
  524. end;
  525. end;
  526. if CurrX>WinMaxX then
  527. begin
  528. CurrX:=WinMinX;
  529. inc(CurrY);
  530. end;
  531. while CurrY>WinMaxY do
  532. begin
  533. removeline(1);
  534. dec(CurrY);
  535. end;
  536. end;
  537. Function CrtWrite(var f : textrec):integer;
  538. var
  539. i : longint;
  540. begin
  541. GetScreenCursor(CurrX,CurrY);
  542. for i:=0 to f.bufpos-1 do
  543. WriteChar(f.buffer[i]);
  544. SetScreenCursor(CurrX,CurrY);
  545. f.bufpos:=0;
  546. CrtWrite:=0;
  547. end;
  548. Function CrtRead(Var F: TextRec): Integer;
  549. procedure BackSpace;
  550. begin
  551. if (f.bufpos>0) and (f.bufpos=f.bufend) then
  552. begin
  553. WriteChar(#8);
  554. WriteChar(' ');
  555. WriteChar(#8);
  556. dec(f.bufpos);
  557. dec(f.bufend);
  558. end;
  559. end;
  560. var
  561. ch : Char;
  562. Begin
  563. GetScreenCursor(CurrX,CurrY);
  564. f.bufpos:=0;
  565. f.bufend:=0;
  566. repeat
  567. if f.bufpos>f.bufend then
  568. f.bufend:=f.bufpos;
  569. SetScreenCursor(CurrX,CurrY);
  570. ch:=readkey;
  571. case ch of
  572. #0 : case readkey of
  573. #71 : while f.bufpos>0 do
  574. begin
  575. dec(f.bufpos);
  576. WriteChar(#8);
  577. end;
  578. #75 : if f.bufpos>0 then
  579. begin
  580. dec(f.bufpos);
  581. WriteChar(#8);
  582. end;
  583. #77 : if f.bufpos<f.bufend then
  584. begin
  585. WriteChar(f.bufptr^[f.bufpos]);
  586. inc(f.bufpos);
  587. end;
  588. #79 : while f.bufpos<f.bufend do
  589. begin
  590. WriteChar(f.bufptr^[f.bufpos]);
  591. inc(f.bufpos);
  592. end;
  593. end;
  594. ^S,
  595. #8 : BackSpace;
  596. ^Y,
  597. #27 : begin
  598. f.bufpos:=f.bufend;
  599. while f.bufend>0 do
  600. BackSpace;
  601. end;
  602. #13 : begin
  603. WriteChar(#13);
  604. WriteChar(#10);
  605. f.bufptr^[f.bufend]:=#13;
  606. f.bufptr^[f.bufend+1]:=#10;
  607. inc(f.bufend,2);
  608. break;
  609. end;
  610. #26 : if CheckEOF then
  611. begin
  612. f.bufptr^[f.bufend]:=#26;
  613. inc(f.bufend);
  614. break;
  615. end;
  616. else
  617. begin
  618. if f.bufpos<f.bufsize-2 then
  619. begin
  620. f.buffer[f.bufpos]:=ch;
  621. inc(f.bufpos);
  622. WriteChar(ch);
  623. end;
  624. end;
  625. end;
  626. until false;
  627. f.bufpos:=0;
  628. SetScreenCursor(CurrX,CurrY);
  629. CrtRead:=0;
  630. End;
  631. Function CrtReturn(Var F: TextRec): Integer;
  632. Begin
  633. CrtReturn:=0;
  634. end;
  635. Function CrtClose(Var F: TextRec): Integer;
  636. Begin
  637. F.Mode:=fmClosed;
  638. CrtClose:=0;
  639. End;
  640. Function CrtOpen(Var F: TextRec): Integer;
  641. Begin
  642. If F.Mode=fmOutput Then
  643. begin
  644. TextRec(F).InOutFunc:=@CrtWrite;
  645. TextRec(F).FlushFunc:=@CrtWrite;
  646. end
  647. Else
  648. begin
  649. F.Mode:=fmInput;
  650. TextRec(F).InOutFunc:=@CrtRead;
  651. TextRec(F).FlushFunc:=@CrtReturn;
  652. end;
  653. TextRec(F).CloseFunc:=@CrtClose;
  654. CrtOpen:=0;
  655. End;
  656. procedure AssignCrt(var F: Text);
  657. begin
  658. Assign(F,'');
  659. TextRec(F).OpenFunc:=@CrtOpen;
  660. end;
  661. { use the C version to avoid using dpmiexcp unit
  662. which makes sysutils and exceptions working incorrectly PM }
  663. //function __djgpp_set_ctrl_c(enable : longint) : boolean;cdecl;external;
  664. var
  665. x,y : longint;
  666. begin
  667. { Load startup values }
  668. ScreenWidth:=GetScreenWidth;
  669. ScreenHeight:=GetScreenHeight;
  670. WindMax:=(ScreenWidth-1) or ((ScreenHeight-1) shl 8);
  671. { Load TextAttr }
  672. GetScreenCursor(x,y);
  673. lastmode := mem[$40:$49];
  674. if screenheight>25 then
  675. lastmode:=lastmode or $100;
  676. If not(lastmode=Mono) then
  677. VidSeg := $b800
  678. else
  679. VidSeg := $b000;
  680. TextAttr:=mem[VidSeg:((y-1)*ScreenWidth+(x-1))*2+1];
  681. { Redirect the standard output }
  682. assigncrt(Output);
  683. Rewrite(Output);
  684. TextRec(Output).Handle:=StdOutputHandle;
  685. assigncrt(Input);
  686. Reset(Input);
  687. TextRec(Input).Handle:=StdInputHandle;
  688. { Calculates delay calibration }
  689. initdelay;
  690. { Enable ctrl-c input (JM) }
  691. // __djgpp_set_ctrl_c(0);
  692. end.
  693. {
  694. $Log$
  695. Revision 1.3 2003-10-03 21:59:28 peter
  696. * stdcall fixes
  697. Revision 1.2 2003/09/07 22:29:26 hajny
  698. * syswat renamed to system, CVS log added
  699. }