Search code examples
cinterruptdosturbo-c

Keyboard interrupt handle in c global variable usage


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


Solution

  • 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.