Home > OS >  UART driver for QEMU receiving `delete` byte instead of `backspace`
UART driver for QEMU receiving `delete` byte instead of `backspace`

Time:12-04

I press the backspace button on my keyboard, I get the delete byte, which is 127 instead of 8, which is the representative of backspace using the following simple get/put UART functions.

#define mmio_write_byte(base, offset, value) \
    *((uint8_t *) (base   offset)) = (uint8_t) value

#define mmio_read_byte(base, offset) *((uint8_t *) (base   offset))

void uart0_put(char c) {
    while ((mmio_read_byte(UART0_BASE, UART_LSR) & (1 << 5)) == 0);
    mmio_write_byte(UART0_BASE, UART_THR, c);
}

char uart0_get() {
    while ((mmio_read_byte(UART0_BASE, UART_LSR) & (1 << 0)) == 0);
    return mmio_read_byte(UART0_BASE, UART_RHR);
}

In my driver code, I have the following:

    while (1) {
        char c = read_char();
        if (c == 127) {
            puts("HERE: delete\n");
        } else if (c == 8) {
            puts("HERE: backspace\n");
        } else if (c == 10 || c == 13) {
            putchar('\n');
        } else {
            putchar(c);
        }
    }

When I run QEMU and press the backspace button repeatedly on my keyboard, I receive the following:

$ qemu-system-riscv64 -machine virt -smp 1 -bios none -m 128 -serial mon:stdio -nographic -device virtio-keyboard-device -kernel kernel/kernel.elf
HERE: delete
HERE: delete
HERE: delete

Also, when I press the delete button, I don't get anything printed (so it doesn't map to 8 nor 127)...

CodePudding user response:

It is the correct behaviour. If you press the backspace key on the keyboard it's function is delete the character behind the cursor, not to move the cursor back without deleting.

The delete button does not translate to any ASCII char. It is internal to the terminal but not sent.

  • Related