Docsity

The Keyboard Buffer-System Programming-Lecture Notes, Study notes of System Programming

Toggle sidebar
bg1
Lecture # 8
The keyboard buffer
Keyboard Buffer
•Keyboard Buffer is located in BIOS Data Area.
•Starts at 40: IEH
•Ends at 40 : 3DH
•Has 32 byes of memory 2 bytes for each
character.
•Head pointer is located at address 40 : 1A to
40:IBH
•Tail pointer located at address 40 : IC to 40:IDH
The keyboard buffer is a memory area reserved in the BIOS data area. This area stores
the ASCII or special key codes pressed from the keyboard. It works as a circular buffer
and two bytes are reserved for each character, moreover 2 bytes are used to store a single
character. The first character stores the ASCII code and the second byte stores 0 in case
an ASCII key is pressed. In case a extended key like F1- F12 or arrow key is pressed the
first byte stores a 0 indicating a extended key and the second byte stores its extended key
code.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download The Keyboard Buffer-System Programming-Lecture Notes and more Study notes System Programming in PDF only on Docsity!

Lecture # 8

The keyboard buffer

Keyboard Buffer

  • Keyboard Buffer is located in BIOS Data Area.
  • Starts at 40: IEH
  • Ends at 40 : 3DH
  • Has 32 byes of memory 2 bytes for each character.
  • Head pointer is located at address 40 : 1A to 40:IBH
  • Tail pointer located at address 40 : IC to 40:IDH

The keyboard buffer is a memory area reserved in the BIOS data area. This area stores the ASCII or special key codes pressed from the keyboard. It works as a circular buffer and two bytes are reserved for each character, moreover 2 bytes are used to store a single character. The first character stores the ASCII code and the second byte stores 0 in case an ASCII key is pressed. In case a extended key like F1- F12 or arrow key is pressed the first byte stores a 0 indicating a extended key and the second byte stores its extended key code.

Circular buffer

40:1AH 40:1CH Head Tail 40:1EH

40:3DH

The circular keyboard buffer starts at the address 40:1EH and contains 32 bytes. The address 40:1AH stores the head of this circular buffer while the address 40:1CH stores the tail of this buffer. If the buffer is empty the head and tail points at the same location as shown in the diagram above.

Storing characters in the keyboard buffer

83

0

0

ā€žBā€Ÿ

0ā€Ÿ

ā€žAā€Ÿ 0x 0x 0x 0x Head = 0x

Tail 0x1E

  • So KBD buffer acts as a circular buffer.
  • The tail value should be examined to get to the start of the buffer.

Example

#include <dos.h>

void interrupt (*old)();

void interrupt new1()!

unsigned char far *scr = (unsigned char far

*) 0x0040001C

void main()

old=getvect(0x09);

setvect(0x09,new1);

keep(0,100);

void interrupt new1 () { if(inportb(0x60)==83) { ((unsigned char far)0x00400000+scr)=25; if((scr)==60) *scr=30; else *scr+=2; outportb(0x20,0x20); return; }}

The program listed in the slides above intercepts interrupt 9. Whenever the interrupt 9 occurs it reads the keyboard port 0x60. If the port contains 83 then it means DEL was pressed, if so it places the code 25 in the buffer and then updates the head in circular manner. The code 25 placed instead of 83 represents the combinations CTRL+Y. The program when resident will cause the program to receive CTRL+Y combination whenever DEL is pressed by the user. i.e in Borland C environment CTRL+Y combination is used to delete a line, if this program is resident then in Borland C environment a line will be deleted whenever DEL is pressed by the user. But the thing worth noticing is that the interrupt function returns and does not call the real interrupt 9 after placing 25 in the buffer, rather it returns directly. But before returning as it has intercepted a hardware interrupt it needs to notify the PIC, this is done by outport(0x20,0x20); statement. 0x20 is the address of the OCW that receives the EOI code which incidentally is also 0x20.

Reading OCW

OCW2& OCW

X X X

7 6 5 4 3 2 1 0

001 FOR Non Specific EOI 01 If other Registers are to be accessed

00 If EOI is to be sent

The same port i.e 0x20 is used to access the OCWs. 00 is placed in bits number 4 and 3 to indicate an EOI is being received and 01 is placed to indicate that a internal register is to be accessed.

0

7 6 5 4 3 2 1 0

O1 To read IRR or ISR 10 = IRR 11= In-Service Register

The value in bits number 1 and 0 indicate which Register is to accessed. 10 is for IRR and 11 is for ISR.

Accessing the ISR and IRR.

0 0 0 0 1 0 0 0

7 6 5 4 3 2 1 0

No EOI relevant

Other Register to be Accessed

Donā€Ÿt Care

A value is placed in the port 0x20 as shown in the above slide to indicate that a register is to be accessed.

0 0 0 0 1 0 1 0

7 6 5 4 3 2 1 0

01 PIC Notified about reading operation

IRR Accessed

Then again a value in that same port is placed to indicate which register is to be accessed, as in the above slide IRR is to be accessed.

indicate that IRR is to accessed. Now this port 0x20 can be read which will contain the value in IRR. Similarly it is done again by placing the 0x0B in port 0x20 to access the ISR.

More about TSR Programs

  • A TSR need to be loaded once in memory
  • Multiple loading will leave redundant copies in memory
  • So we need to have some check which will load the program only once

On of the solution to the problem can be

Using a Global Variable as a

flag

int 65H is empty, we can use

its vector as a flag.

Address of vector

seg = 0

offset = 65H * 4

Example:

#include<stdio.h> #include<BIOS.H> #include<DOS.H> unsigned int far * int65vec = (unsigned far ) MK_FP(0,0x654) void interrupt (oldint) ( ); void interrupt newfunc ( ); void main() { if((int65vec) != 0xF00F) //corrected { oldint =getvect (0x08);

setvect(0x08, newint); (int65vec) = 0xF00F; keep (0,1000); }else { puts (ā€œProgram Already Residentā€); }} void interrupt newfunc () { ::::::: ::::::: (oldint) ( ); }

The above template shows how the vector of int 0x65 can be used as a flag. This template shows that a far pointer is maintained which is assigned the address of the int 0x vector. Before calling the keep() function i.e making the program resident a value of 0xf00f is placed at this vector( this vector can be tempered as it is not being used by the OS or device drivers). Now if another instance of the program attempts to run the if statement at the start of the program will check the presence of 0x0f00f at the int vector of 0x65, if found the program will simply exit otherwise it will make itself resident. Or in other word we can say that 0xf00f at the int 0x65 vector in this case indicate that the program is already resident.

But what if another program is

resident or using this vector.

Another Method

Service # 0xFF usually does not

exist for ISRā€Ÿs.

Key is to create another service

0xFF for the ISR interrupt besides

other processing.

Example:

#include<stdio.h> #include<BIOS.H> #include<DOS.H> void interrupt (*oldint) ( ); void interrupt newfunc ( unsigned int BP,..…,flags); void main() { _DI = 0; _AH = 0xFF; geninterrupt (0x13); if (_DI = = 0xF00F) { puts (ā€œProgram Already Residentā€); exit (0); }