dcpu-16

How to best get a character from the keyboard on the DCPU16


So, my goal is the write a subroutine that when called hangs until the user has entered a string terminated by <return>, which it then returns(probably by writing it to an address specified by the user).

My problem lies in how i best get individual characters from the keyboard. As I see it there are 3 ways:

  1. Listen for interrupts from the keyboard and get the character in response to those. This would mean that the logic performed by getline would be in the interrupt handler, which seem to cause some problems. i.e. how do you return from getline in response to a press on the <return> key? You don't have the return address handy when in the interrupt handler. Also the pattern of putting too much specific logic in the interrupt handler seems to me... wrong... even though I'm very inexperienced in low level coding.

  2. Just keep pulling the keyboard for key presses.

  3. Implementing the old 1.1 behavior with the interrupt handler, by loading all characters pressed into a circular buffer(possibly of length 1).

Some more perspective on these options would be nice.


Solution

  • when you call your getline it should setup the interrupt handler so it adds the typed keys to buffer and updates a index

    then start a busy loop until the end of the buffer has a new line and disable the interrupts from the keyboard

    getline: 
    set push B
    set push X
    ;--coming from interrupt dispatch
    SET B, buffer ;--address from argument
    SET C, bufferlength ;-- from argument
    SET PUSH, 0
    SET X, SP ;--address of counter
    SET A, startKeyInput
    INT softwareInterrupt
    
    IAQ 1;--restart interrupts
    startloop:
    SET A,buffer
    ADD A,SP
    IFN [A],'\n'
        set PC, startloop ;--busy loop
    IFL PEEK, X
        set PC, startloop ;-- stopping buffer overflow
    IAQ 0;--stop interrupts
    set A, stopKeyInput
    INT softwareInterrupt
    
    SET C,POP;-- C is return value
    SET X,POP
    SET B,POP
    FRI ;-- from interrupt
    

    and the interrupt handler adds the typed key to the buffer until it is full and adds 1 to the counter, this can be put in a interrupt handler itself but you'll need to reactivate interrupts while in the busy loop