[comp.lang.pascal] BIOS DATA AREA $40:0 - $40:$84

zhou@brazil.psych.purdue.edu (Albert Zhou) (11/11/90)

 It is said that BIOS is very poorly documented. Recently I have been able
to find some information about BIOS data area $40:0 - $40:$84. But I still
have some problem with some of them. Especially about keyboard buffer info-
mation. 
     There are 32 bytes starting at $40:10 for keyboard buffer. Where is
information on how many keys are still in the buffer and where do they
start (As I know they do not always start from the first byte)? 
     This question is very important since I'm designing a software in 
which I need to send a string to the keyboard buffer to mimic actual    
keystrokes. Is anybody expert at BIOS?

bb16@prism.gatech.EDU (Scott Bostater) (11/12/90)

In article <11461@j.cc.purdue.edu> zhou@brazil.psych.purdue.edu (Albert Zhou) writes:
>
> It is said that BIOS is very poorly documented. Recently I have been able
>to find some information about BIOS data area $40:0 - $40:$84. But I still
>have some problem with some of them. Especially about keyboard buffer info-
>mation. 
>     There are 32 bytes starting at $40:10 for keyboard buffer. Where is
>information on how many keys are still in the buffer and where do they
>start (As I know they do not always start from the first byte)? 

There is a pointer to the head and tail position of the buffer.
Buffer Head:   $40:$1a
Buffer Tail:   $40:$1c

>     This question is very important since I'm designing a software in 
>which I need to send a string to the keyboard buffer to mimic actual    
>keystrokes. Is anybody expert at BIOS?

I wouldn't call myself an expert, but I have messed around with the keyboard 
buffer before.  There are two ways of doing what you are trying.  The first 
is to use the Buffer head and buffer tail pointers to put data into the 
buffer.  This has a distinct disadvantage that if you run the program on a
computer that has a keyboard buffer extender (i.e. buf128, kbfix2, etc.) it
will probably not work right.  

The second method is to use a BIOS call to load the keyboard buffer.  This 
normally works with most keyboard extenders, but requires newer versions of
BIOS.  This will not work with the original PC.  Below is the code from my
misc unit that plays with the keyboard buffer.

--------------------------  cut  Here  --------------------------------------

const
  UseBios: Boolean = True;

Procedure PutKeyBoardBuffer( c, scan: Char);
{ if c = #0, then scan = keyboard scan code, otherwise scan is ignored }

var
  Reg: Registers;
  KeyBufTail: Word absolute $40:$1c;
Begin
  If UseBios Then
   Begin
    Reg.AH := $05;
    Reg.CH := ord( scan);
    Reg.CL := ord( c);
    intr( $16, Reg);
   End
  Else
   Begin
    Mem[$40:KeyBufTail] := ord( c);
    Mem[$40:KeyBufTail+1] := ord( Scan);
    If KeyBufTail = $3c Then
      KeyBufTail := $1e
    Else
      Inc(KeyBufTail, 2);
   End; { If }
End;

Procedure TestKeyboardBios;
var
  ch: Char;

Begin
  While KeyPressed do ch := ReadKey;      { Flush keyboard buffer }
  PutKeyboardBuffer( #251, #0);

  If KeyPressed Then
   Begin
    ch := readkey;
    UseBios := ch = #251;
   End
  Else
    UseBios := False;
End;

---------------------------  Cut  Here  --------------------------------------

I don't know how robust the code is, but it works on all the computers I have 
access to.  One obvious problem with my code is that it does not check for
a buffer overrun problem.  This has never been a problem for me since I seldom
want to place more than 5 characters in the buffer.  You may want to add the
code to check it if you plan on putting lots of data in the buffer.

-- 
Scott Bostater      Georgia Tech Research Institute - Radar Systems Analysis
"My soul finds rest in God alone; my salvation comes from Him"  -Ps 62.1
uucp:     ...!{allegra,amd,hplabs,ut-ngp}!gatech!prism!bb16
Internet: bb16@prism.gatech.edu