









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










The keyboard buffer
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
Example
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
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.
On of the solution to the problem can be
#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.
#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); }