I'm stuck, help me... this Keyboard/INT has halted my progre

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
VoidLogic

I'm stuck, help me... this Keyboard/INT has halted my progre

Post by VoidLogic »

Please help! I know this is a repost but i really need help :( tear

1. the following code if for real mode
2. it compiles
3. it is has correct syntax
4. it crashes

The main loop in constently looking for a new char, the int. function is called at a key press and fills the vector. The first time a key is pressed everything works, the second time everything works and after about 25-75 runs through the main loop the program freezes. :(

here she is:

#include <BIOS.h>
#include <Ctype.h>
#include <StdIO.h>
#include <DOS.h>
#include <Apqueue.h>

#define INTR 0011 // The Keyboard INT

#ifdef __cplusplus
    #define __CPPARGS ...
#else
    #define __CPPARGS
#endif

apqueue <int> KeyQue;

void interrupt ( *oldhandler)(__CPPARGS);

void interrupt handler(__CPPARGS)
{
disable();
oldhandler();
KeyQue.enqueue(bioskey(0));
enable();
}

void main(void)
{
bool Loop_Flag = true;
int key = 0;
oldhandler = getvect(INTR);
setvect(INTR, handler);
while(Loop_Flag)
{
  cout<<".";
  if (KeyQue.length() > 0)
  {
   KeyQue.dequeue(key);
   if (isalnum(key & 0xFF))
    printf("%c", key);
   else
    printf("%#02x", key);
   if(char(key) == 'e') Loop_Flag = false;
  }
}
setvect(INTR, oldhandler);
}

//Thanx Guys :)
jamethiel

RE:I'm stuck, help me... this Keyboard/INT has halted my pro

Post by jamethiel »

A few questions:

What happens on a key up transition? (what does bioskey(0) return?)
What happens if someone hits Alt or something?

Is KeyQue.dequeue() threadsafe? What happens if a key is pressed while it is being executed?

What do disable() and enable() do? Are they really necessary for a hardware interrupt (which, in real mode, disables interrupts on entry and restores them on exit -anyway-)?
VoidLogic

RE:I'm stuck, help me... this Keyboard/INT has halted my pro

Post by VoidLogic »

-BIOS key returns only when the key is down
-Alt any non keys other than like "ABC" return there hexidemical #
-This is DOS real mode, so there are no threads
-Enable & Disable, turn off all but non-maskible INTS, during the handling of the keyboard INT

other info: the crash is somewhat perdicible, the first keypress functions normaly, then 25-90 time through the main loop the program locks up for no appearent reason

:(
Thanx in adv.
VoidLogic
jamethiel

RE:I'm stuck, help me... this Keyboard/INT has halted my pro

Post by jamethiel »

So, let me get this straight...

User presses key, Int 9 triggers, you disable the already-disabled interrupts, you call the BIOS interrupt handler, the BIOS handler posts the key event to the BIOS queue, you move the event from the BIOS queue to your queue, enable the interrupts, and return from the interrupt (small race condition as another interrupt comes in while you're restoring the register state).

User -releases- key, Int 9 triggers, you call the BIOS interrupt handler, the BIOS handler -doesn't- post a key event to the BIOS queue, you -lock up- trying to read the BIOS queue (because it's empty), and interrupts are disabled so no new key events can come in.

Is this approximately what's happening?
Post Reply