home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / TURBO.ZIP / DMA.INC < prev    next >
Encoding:
Text File  |  1985-09-16  |  15.8 KB  |  447 lines

  1. {-----------------------------------------------------------------------------}
  2. { Name:    DMA.INC                                                            }
  3. { Author:  Peter Thomas                                                       }
  4. { Purpose: This set of procedures and functions can be used to replace the    }
  5. {          standard TURBO Pascal output routines with direct "pokes" into     }
  6. {          the hardware buffer on an IBM-PC (or truely compatible machine).   }
  7. {                                                                             }
  8. {          If used on an IBM Color adaptor board, these routines are          }
  9. {          (unfortunately) slower than standard DOS calls if a flicker        }
  10. {          free screen is wanted. If however screen flicker can be tolerated  }
  11. {          the routines can be considerably quicker. Programs may also make   }
  12. {          use of special calls to "turn off" the screen during periods       }
  13. {          of heavy screen I/O. This will enable faster running, albeit       }
  14. {          with a blank screen at certain times.                              }
  15. {                                                                             }
  16. {          All data structures, variables, constants, functions and           }
  17. {          procedures which are intended to be used soley within this         }
  18. {          file begin with the character "_". Any other variables etc.        }
  19. {          may be freely used by the main program.                            }
  20. {                                                                             }
  21. { Usage:   (*$I DMA.INC *)       Include the code                             }
  22. {          DMA_Out  (BW_Screen); Activate the DMA procedures for monochrome   }
  23. {       or DMA_Out  (Color_Screen);                          for color        }
  24. {          DMA_In ;              Optionally activate read back from screen    }
  25. {          DMA_Color_Type ( Color_IBM ) using an IBM color adaptor            }
  26. {          DMA_Color_Type ( Color_Other) using a compatible adaptor           }
  27. {                                        which is flicker free                }
  28. {          DMA_Screen_Disable    Turn off the screen (usable on IBM adaptor   }
  29. {                                to speed up screen handling, with no         }
  30. {                                flicker)                                     }
  31. {          DMA_Screen_Enable     Turn on the screen                           }
  32. {          DMA_Out_Reset         Reset's output to use non DMA functions      }
  33. {          DMA_In_Reset          Reset's input to use standard keyboard       }
  34. {                                functions                                    }
  35. {                                                                             }
  36. {          From here on:                                                      }
  37. {                    1 - any WRITE or WRITELN's that would go to              }
  38. {                        the TRM, or CON  will go to via DMA                  }
  39. {                        to the screen.                                       }
  40. {                    2 - To utilize the standard files, WRITEs                }
  41. {                        will have to utilize the device AUX                  }
  42. {                        (i.e. WRITELN ( Aux , Junk ) ).                      }
  43. {                    3 - Any calls to ClrEol, ClrScr, DelLine, InsLine,       }
  44. {                        GotoXY, LowVideo, NormVideo, WhereX, WhereY will be  }
  45. {                        TextColor, TextBackGround,                           }
  46. {                        replaced by their DMA equivalent.                    }
  47. {                    4 - The TURBO calls will be replaced by performed by     }
  48. {                        prefixing the function or procedure with T_          }
  49. {                        (i.e. T_ClrEol ).                                    }
  50. {                    5 - The system may be reset to standard usage of WRITE   }
  51. {                        by calling DMA_Out_Reset. However, all calls         }
  52. {                        to TURBO screen handling will have to be performed   }
  53. {                        by the T_ prefixed functions.                        }
  54. {                                                                             }
  55. {  Optional Input Handling: ONLY IF DMA_In has been called                    }
  56. {                    1 - Any READ or READLN's that would go to                }
  57. {                        TRM, KBD, CON will read the data from                }
  58. {                        the present cursor postion.                          }
  59. {                    2 - To perform a READ ( KBD, Char) the function Get_Kbd  }
  60. {                        will have to be used.                                }
  61. {                    3 - On a READ ( Aux ) there is no echoing performed      }
  62. {                        to the screen.                                       }
  63. {                    4 - The system may be reset to standard usage of READ by }
  64. {                        calling DMA_In_Reset.                                }
  65. {                                                                             }
  66. { How It Works: DMA.INC makes use of the ability of a user to define his      }
  67. {               own Input/Output handles. The standard TURBO screen drivers   }
  68. {               are referenced thru the variables ConInPtr and ConOutPtr.     }
  69. {               These are modified to point to two TURBO routines to return   }
  70. {               one character from the screen and place one on the screen.    }
  71. {               All standard screen handling functions are made available     }
  72. {               by defining the replacemnts AFTER the prefixed versions       }
  73. {               have been created, thus in the scope of T_ClrEol the meaning  }
  74. {               of ClrEol is the TURBO standard, whereas in the user code     }
  75. {               the meaning of ClrEol is the modified ClrEol.                 }
  76. {-----------------------------------------------------------------------------}
  77.  
  78.  
  79. {-----------------------------------------------------------------------------}
  80. { Functions and Procedures to provide access to standard TURBO screen         }
  81. { handling, while the DMA.INC file has been included.                         }
  82. {-----------------------------------------------------------------------------}
  83.  
  84. PROCEDURE T_ClrEol ;
  85. BEGIN
  86.    ClrEol ;
  87. END;
  88.  
  89. PROCEDURE T_ClrScr ;
  90. BEGIN
  91.    ClrScr ;
  92. END;
  93.  
  94. PROCEDURE T_DelLine ;
  95. BEGIN
  96.    DelLine ;
  97. END ;
  98.  
  99. PROCEDURE T_InsLine ;
  100. BEGIN
  101.    InsLine ;
  102. END ;
  103.  
  104. PROCEDURE T_GotoXY ( X , Y : INTEGER ) ;
  105. BEGIN
  106.    GotoXY ( X , Y ) ;
  107. END ;
  108.  
  109. PROCEDURE T_LowVideo ;
  110. BEGIN
  111.    LowVideo ;
  112. END ;
  113.  
  114. PROCEDURE T_NormVideo ;
  115. BEGIN
  116.    NormVideo ;
  117. END ;
  118.  
  119. PROCEDURE T_TextBackground ( Color : Byte ) ;
  120. BEGIN
  121.    TextBackground ( Color ) ;
  122. END ;
  123.  
  124. PROCEDURE T_TextColor ( Color : Byte ) ;
  125. BEGIN
  126.    TextColor ( Color ) ;
  127. END ;
  128.  
  129. FUNCTION T_WhereX : INTEGER ;
  130. BEGIN
  131.    T_WhereX := WhereX ;
  132. END ;
  133.  
  134. FUNCTION T_WhereY : INTEGER ;
  135. BEGIN
  136.    T_WhereY := WhereY ;
  137. END ;
  138.  
  139. {-----------------------------------------------------------------------------}
  140. { End of TURBO Replacement Functions                                          }
  141. {-----------------------------------------------------------------------------}
  142.  
  143. {-----------------------------------------------------------------------------}
  144. { Internal Data Types                                                         }
  145. {-----------------------------------------------------------------------------}
  146.  
  147. TYPE
  148.    _Screen_Position = ( _Char_Byte, _Attr_Byte) ;
  149.    _Rows            = 1..25;
  150.    _Cols            = 1..80;
  151.    _Screen          = ARRAY [1..25, 1..80, _Char_Byte.._Attr_Byte] OF CHAR;
  152.    _Screen_Ptr      = ^ _Screen;
  153.    _Location        = RECORD
  154.                          Y : _Rows;
  155.                          X : _Cols;
  156.                       END;
  157.  
  158.  
  159. {-----------------------------------------------------------------------------}
  160. { Public Constants                                                            }
  161. {-----------------------------------------------------------------------------}
  162.  
  163. CONST
  164.    BW_Screen       = $B000 ;         { Offset of B+W Screen }
  165.    Color_Screen    = $B800 ;         { Offset of color screen }
  166.  
  167. TYPE
  168.    _DMA_Screen_Make = ( BW , Color_IBM , Color_Other ) ;
  169.  
  170. {-----------------------------------------------------------------------------}
  171. { Internal Variables                                                          }
  172. {-----------------------------------------------------------------------------}
  173.  
  174. VAR
  175.    _DMA_Screen      : _Screen_Ptr;  { Offset to start of screen memory }
  176.    _Global_Loc      : _Location;    { Present location on screen       }
  177.    _Default_Attr    : Byte ;        { Display data with this attribute }
  178.    _Old_Aux_InPtr,                  { Save the old Auxilary Device     }
  179.    _Old_Aux_OutPtr  : Integer ;     { Input and Output handlers        }
  180.    _DMA_Action      : BOOLEAN ;     { Used to "force" changes to output ports }
  181.    _DMA_Color       : _DMA_Screen_Make ; { What type screen : BW, IBM color or other color }
  182.    _DMA_Enable      : BOOLEAN ;     { Allow total turn-off of screen   }
  183.  
  184. {-----------------------------------------------------------------------------}
  185. { Internal Functions and Procedures                                           }
  186. {-----------------------------------------------------------------------------}
  187.  
  188. PROCEDURE _DMA_New_Pos (VAR Where : _Location; D_Row : INTEGER; D_Col : INTEGER);
  189. { Given a Location and a "Delta" location return the next postion.            }
  190. BEGIN
  191.    WITH Where DO
  192.       BEGIN
  193.          X := X + D_Col;
  194.          Y := Y + D_Row;
  195.          IF X < 1 THEN
  196.             X := 1
  197.          ELSE IF X > 80 THEN
  198.             BEGIN
  199.                Y := Y + 1;
  200.                X := 1;
  201.             END;
  202.          IF Y < 1 THEN
  203.             Y := 1
  204.          ELSE IF Y > 25 THEN
  205.             Y := 25;
  206.       END;
  207. END;
  208.  
  209. PROCEDURE _DMA_Color_Off ;
  210. { Disables the screen , or waits for vertical retrace on an IBM color card }
  211. BEGIN
  212.    IF _DMA_Action OR
  213.       ( _DMA_Enable AND ( _DMA_Color = Color_IBM ) ) THEN
  214.       BEGIN
  215.          WHILE ( PORT[$3DA] AND $08) = 0  DO ;
  216.          PORT[$3D8] := $21 ;
  217.          _DMA_Action := FALSE ;
  218.       END ;
  219. END ;
  220.  
  221. PROCEDURE _DMA_Color_On ;
  222. { Re-enables the screen disabled by _DMA_Color_Off }
  223. BEGIN
  224.    IF _DMA_Action OR
  225.       ( _DMA_Enable AND ( _DMA_Color = Color_IBM ) ) THEN
  226.       BEGIN
  227.          PORT[$3D8] := $29 ;
  228.          _DMA_Action := FALSE ;
  229.       END ;
  230. END ;
  231.  
  232. PROCEDURE _DMA_Display ( What : CHAR ;  X , Y : INTEGER ) ;
  233. { Displays the character What at a given location on the screen. }
  234. { No attempt to provide "flicker free" display }
  235. BEGIN
  236.    _DMA_Screen^[Y,X,_Attr_Byte] := CHR(_Default_Attr);
  237.    _DMA_Screen^[Y,X,_Char_Byte] := What;
  238. END ;
  239.  
  240. PROCEDURE _DMA_CopyLine ( FromLine, ToLine : _Rows ) ;
  241. { Copies one line of the screen to a second }
  242. BEGIN
  243.    MOVE ( _DMA_Screen^[FromLine] , _DMA_Screen^[ToLine], SIZEOF (_DMA_Screen^[1] ) ) ;
  244. END ;
  245.  
  246. PROCEDURE _DMA_WriteCh (What : CHAR);
  247. { Places a character on the screen with the presently active display attribute}
  248. { Called by TURBO as Console device Output Handler                            }
  249. BEGIN
  250.    _DMA_Color_Off ;
  251.    _DMA_Display ( What , _Global_Loc.X, _Global_Loc.Y ) ;
  252.    _DMA_Color_On ;
  253.    _DMA_New_Pos (_Global_Loc, 0, 1);
  254. END;
  255.  
  256. FUNCTION _DMA_GetCh : CHAR;
  257. { Returns a character from the screen                                         }
  258. { Called by TURBO as Console Device Input Handler                             }
  259. BEGIN
  260.    WITH _Global_Loc DO
  261.       _DMA_GetCh := _DMA_Screen^[Y, X, _Char_Byte];
  262.    _DMA_New_Pos (_Global_Loc, 0, 1);
  263. END;
  264.  
  265. { TURBO Replacement Functions and Procedures                                  }
  266.  
  267. PROCEDURE ClrEol ;
  268. VAR
  269.    Temp_X  : _Cols ;
  270. BEGIN
  271.    _DMA_Color_Off ;
  272.    WITH _Global_Loc DO
  273.       FOR Temp_X := X TO 80 DO
  274.          _DMA_Display ( ' ' , Temp_X , Y ) ;
  275.    _DMA_Color_On ;
  276. END ;
  277.  
  278. PROCEDURE ClrScr;
  279. VAR
  280.   Temp_X : _Cols;
  281.   Temp_Y : _Rows;
  282. BEGIN
  283.    _DMA_Color_Off ;
  284.    FOR Temp_X := 1 TO 80 DO
  285.       _DMA_Display ( ' ' , Temp_X , 1 ) ;
  286.    FOR Temp_Y := 2 TO 25 DO
  287.       _DMA_CopyLine ( 1, Temp_Y ) ;
  288.    _DMA_Color_On ;
  289. END;
  290.  
  291. PROCEDURE DelLine ;
  292. VAR
  293.    Temp_Y : _Rows ;
  294.    Temp_X : _Cols ;
  295. BEGIN
  296.    _DMA_Color_Off ;
  297.    WITH _Global_Loc DO
  298.       FOR Temp_Y := Y TO 24 DO
  299.          _DMA_CopyLine ( Temp_Y+1, Temp_Y ) ;
  300.    FOR Temp_X := 1 TO 80 DO
  301.       _DMA_Display ( ' ' , Temp_X , 25 ) ;
  302.    _DMA_Color_On ;
  303. END ;
  304.  
  305. PROCEDURE InsLine ;
  306. VAR
  307.    Temp_X : _Rows ;
  308.    Temp_Y : _COls ;
  309. BEGIN
  310.    _DMA_Color_Off ;
  311.    WITH _Global_Loc DO
  312.       BEGIN
  313.          FOR Temp_Y := 25 DOWNTO Y+1 DO
  314.             _DMA_CopyLine ( Temp_Y - 1, Temp_Y ) ;
  315.          FOR Temp_X := 1 TO 80 DO
  316.             _DMA_Display ( ' ' , Temp_X , Y ) ;
  317.       END ;
  318.    _DMA_Color_On ;
  319. END ;
  320.  
  321. PROCEDURE GotoXY (X_Pos : _Cols; Y_Pos : _Rows) ;
  322. BEGIN
  323.    WITH _Global_Loc DO
  324.       BEGIN
  325.          X := X_Pos;
  326.          Y := Y_Pos;
  327.       END;
  328. END;
  329.  
  330. PROCEDURE LowVideo ;
  331. BEGIN
  332.    _Default_Attr := $07 ;
  333. END ;
  334.  
  335. PROCEDURE NormVideo ;
  336. BEGIN
  337.    _Default_Attr := $0F ;
  338. END ;
  339.  
  340. PROCEDURE HighVideo ;
  341. BEGIN
  342.    NormVideo ;
  343. END;
  344.  
  345. PROCEDURE TextBackground ( Color : Byte ) ;
  346. BEGIN
  347.    _Default_Attr := ( _Default_Attr AND $0F ) OR ( ( Color AND $0F ) shl 4 ) ;
  348. END ;
  349.  
  350. PROCEDURE TextColor ( Color : Byte ) ;
  351. BEGIN
  352.    _Default_Attr := ( _Default_Attr AND $F0 ) OR ( Color AND $0F ) ;
  353. END ;
  354.  
  355.  
  356. FUNCTION WhereX : INTEGER;
  357. BEGIN
  358.    WhereX := _Global_Loc.X;
  359. END;
  360.  
  361. FUNCTION WhereY : INTEGER;
  362. BEGIN
  363.    WhereY := _Global_Loc.Y;
  364. END;
  365.  
  366. FUNCTION GetKbd : Char ;
  367. VAR
  368.    Temp_Char : Char ;
  369. BEGIN
  370.    ConInPtr := AuxInPtr ;
  371.    READ ( Kbd , Temp_Char ) ;
  372.    ConInPtr := OFS ( _DMA_GetCh ) ;
  373.    GetKbd := Temp_Char ;
  374. END ;
  375.  
  376. {-----------------------------------------------------------------------------}
  377. { User Initialization Routines                                                }
  378. {-----------------------------------------------------------------------------}
  379.  
  380. PROCEDURE DMA_Out (Which : Integer ) ;
  381. { Called to initialize the interface. Which is the segment address of the     }
  382. { screen buffer }
  383. BEGIN
  384.    _DMA_Enable := TRUE ;
  385.    _DMA_Action :=  FALSE ;
  386.    IF Which = Color_Screen THEN
  387.       _DMA_Color := Color_IBM
  388.    ELSE
  389.       _DMA_Color := BW ;
  390.    _DMA_Screen := PTR(Which,0);
  391.    WITH _Global_Loc DO
  392.       BEGIN
  393.          X := T_WhereX;
  394.          Y := T_WhereY;
  395.       END;
  396.   _Old_Aux_OutPtr := AuxOutPtr ;
  397.   AuxOutPtr := ConOutPtr;
  398.   ConOutPtr := OFS(_DMA_WriteCh);
  399.   _Default_Attr := $0F ;
  400. END;
  401.  
  402. PROCEDURE DMA_Color_Type ( Screen_Type : _DMA_Screen_Make ) ;
  403. { Allows specification of a "flicker free" color screen }
  404. BEGIN
  405.    IF SEG(_DMA_Screen^) = Color_Screen THEN
  406.       _DMA_Color := Screen_Type ;
  407. END ;
  408.  
  409. PROCEDURE DMA_Screen_Disable ;
  410. { Disable all screen updates }
  411. BEGIN
  412.    _DMA_Action := TRUE ;
  413.    _DMA_Color_Off ;
  414.    _DMA_Enable := FALSE ;
  415. END ;
  416.  
  417. PROCEDURE DMA_Screen_Enable ;
  418. { Re-enables screen updates }
  419. BEGIN
  420.    _DMA_Action := TRUE ;
  421.    _DMA_Color_On ;
  422.    _DMA_Enable := TRUE ;
  423. END ;
  424.  
  425. PROCEDURE DMA_In ;
  426. { Activates input reading from the screen (bypasses KBD) }
  427. BEGIN
  428.   _Old_Aux_InPtr := AuxInPtr ;
  429.   ConOutPtr := OFS(_DMA_WriteCh);
  430.   AuxInPtr  := ConInPtr ;
  431. END;
  432.  
  433. PROCEDURE DMA_Out_Reset ;
  434. { Rsstores "standard" output handling }
  435. BEGIN
  436.    ConOutPtr := AuxOutPtr ;
  437.    AuxOutPtr := _Old_Aux_OutPtr ;
  438. END;
  439.  
  440. PROCEDURE DMA_In_Reset ;
  441. { Restores "standard" input handling }
  442. BEGIN
  443.    ConInPtr  := AuxInPtr ;
  444.    AuxInPtr  := _Old_Aux_InPtr ;
  445. END;
  446. {on
  447. OWsC rhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss