6

I have a project like space impact and I try to handle keyboard interrupt.My problem is I don't want to use global variable(ship) in my_keyboard_interrupt_handler .But i send ship as paremeter to this function , i don't know how to arrange setvect(0x09,my_keyboard_interrupt_handler);.İf there is any way for using setvect function like that please give me any advise.

int main()
{
    void interrupt (*old_keyboard_interrupt_handler)();
    ship = (space_ship*)malloc(sizeof(space_ship));
    old_keyboard_interrupt_handler = getvect(0x09);
        ...
    setvect(0x09,my_keyboard_interrupt_handler);
    return 0;
}
int handle_key()
{
    int key;

    asm{   
        sti   
        in al,60H   
        xor ah,ah   
        mov key,ax   
        in al,61h   
        or al,82h   
        out 61h,al   
        and al,7fh   
        out 61h,al   
        mov al,20h   
        out 20h,al   
       } 

     return key;
}

my keyboard interupt handler :

void interrupt my_keyboard_interrupt_handler()
{
    int key = handle_key();
    if(key == SPACE){

    }else if(key == RIGHT){
        ship->column++; 
    }else if(key == LEFT){
        ship->column--;
    }else if(key == UP){
        ship->row_start--;
        ship->row_end--;
    } else if(key == DOWN){
        ship->row_start++;
        ship->row_end++;
    }else if(key == ESC){

    }
    clrscr();
    print_space_ship(ship);
}

In brief I want to do void interrupt my_keyboard_interrupt_handler(space_ship* ship){..}.But i don't know how to handle setvect function in this situation

Melih Altıntaş
  • 2,495
  • 1
  • 22
  • 35

1 Answers1

4

Well, if you're using ship only in the ISR, then you might as well declare it static inside this function:

void interrupt my_keyboard_interrupt_handler()
{
    static space_ship ship = {0};
    ...
    print_space_ship(ship);
}

But if you're using it in other threads or ISRs, then you have to declare it as a shared (global) variable, and protect it with a standard OS resource (such as Semaphore, or more likely - Mutex) where needed.

If that is indeed the case, then passing it as an argument to the ISR is not going to make any difference.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • thanks for your answer.I am using it in other fuctions.So I have to declare global or send as paremeter.But if I send ship as parameter , setvect function doesn't accept my_keyboard_interrupt_handler with param :( – Melih Altıntaş Mar 13 '14 at 13:04
  • 1
    You can't send the ship as a parameter to the interrupt handler; you are forced to use a global variable (or, more precisely, a variable with file scope, which might or might not be visible outside the source file containing the interrupt handler). – Jonathan Leffler Mar 13 '14 at 14:15
  • 1
    @Melih Altıntaş: The prototype (declaration) of the ISR does not allow you to pass an argument to the function. But what I meant in my answer, is that even if it **did** allow you to pass an argument, it would not help you, because you would still have to protect that argument by ensuring *mutual exclusion*. In fact, that is probably the reason why this function prototype takes no arguments to begin with. It prevents the user from mistakenly assuming that the input arguments are mutually exclusive inside the function. – barak manos Mar 13 '14 at 14:16
  • @JonathanLeffler and barakmanos Thanks for your detailed explanation. – Melih Altıntaş Mar 13 '14 at 16:52