;;Tests all interrupt vectors to find which interrupt value is used querying the Keyboard in IM2
        ORG     100H        ;Start at address 0x100
;;setup interrupt vector table. Vector itself will be at 0x2020
        DI                  ;disable interrupts
        IM      2           ;set interrupt mode 2
        LD      A,03H       ;Load the most significant byte of
        LD      I,A         ;the vector table address to I
        LD      HL,0300H    ;Start address of the interrupt vector table
        LD      E,L         ;Fills the LSB to E
        LD      D,H         ;and the MSB to D
        INC     DE          ;increase DE
        LD      (HL),02H    ;Load half of the vector to (HL)
        LD      BC,0100H    ;257, because of the case where 8-bit vector is 0xFF
        LDIR                ;walk over the following bytes
        EI                  ;reenable interrupts
        LD      HL,GREET    ;Load the start of a banner
        LD      B,15H       ;The length of the string
        LD      DE,0H       ;start in the topleft corner
        CALL    84CDH       ;Call str_out
MLOOP:  HALT                ;Stop and wait for interrupts
        POP     AF          ;Get the result of the interrupt from the stack
        CP      51H         ;Checks if the pressed key was "ON/Break"
        RET     Z           ;If the key was "ON" end the program.
        LD      HL,BUF      ;Load the buffer addess
        CALL    BTOHEX      ;Convert the byte in A into a HEX-string and store it at BUF
        LD      B,2         ;Set the length of BUF
        LD      DE,15H      ;Position cursor in row 0, col 22
        CALL    84CDH       ;Print the Keycode
        JR      MLOOP       ;Stay in the loop
        RET        
;;Convert the contents of A into a hex-string and stores the result in the buffer pointed in HL
BTOHEX: PUSH    AF          ;Store A onto the stack for later use
        LD      B,4         ;Init a loop for shifting the register
LP1:    SRL     A           ;Shift the MSN down to the least significant Nibble
        DJNZ    LP1         ;Shift again, until all 4 bits where shifted.
        CALL    HEXDGT      ;Converts the LSN to hex
        INC     HL          ;Point HL to the next byte in buffer
        POP     AF          ;Retrieve the original A
        AND     0FH         ;Cut the first digit of the hex no.
        CALL    HEXDGT      ;Call HEXDIGIT again for the second digit.
        LD      HL,BUF      ;Restore the buffer address
        RET
;;Converts the least significant nibble in A into a ASCII hex-digit and stores the result
HEXDGT: CP      0AH         ;Check if the number is smaller than 10
        JR      C,SKIP      ;Skip the next instruction, if it is
        ADD     A,07H       ;If the value is >10, we need to skip the chars between '9' and 'A'
SKIP:   ADD     A,30H       ;Converts the number into its ASCII digit
        LD      (HL),A      ;Store the digit in the correct location.
        RET
GREET:  DB      'Test keyboard input: '
BUF:    DS      2           ;Define a 2 byte buffer to store the keycode in ASCII
        ORG     0202H       ;Start address of the interrupt handler
        DI                  ;turn off the interrupts
        EX      AF,AF'      ;store the AF register
        EXX                 ;store the register states
        IN      A,(10H)     ;read interrupt reasons from I/O port
        SRA     A           ;test for bit 0 set
        JR      NC,ENDI     ;End the routine, of the reason for interrupt was not the keyboard
        CALL    8881H       ;Check the pressed button
        PUSH    AF          ;The keycode is in A, push that to the stack as result.   
ENDI:   EXX                 ;restore the register states
        EX      AF,AF'      ;restore AF register
        EI                  ;enable interrupts
        RETI                ;return
        ORG     0300H       ;start of the vector table
        DS      0100H       ;reserve 256 bytes
        END                 ;End of memory
