# UART0 Qemu Keyboard input

**URL:** <https://ziggit.dev/t/uart0-qemu-keyboard-input/5549>\
**Category:** Help\
**Tags:** language\
**Created:** [August 8, 2024, 4:49am UTC](https://ziggit.dev/t/uart0-qemu-keyboard-input/5549 "2024-08-08T04:49:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![csirak](https://ziggit.dev/user_avatar/ziggit.dev/csirak/32/2542_2.png) [@csirak](https://ziggit.dev/u/csirak)\
**Post date:** [August 8, 2024, 4:49am UTC](https://ziggit.dev/t/uart0-qemu-keyboard-input/5549/1 "2024-08-08T04:49:27Z")

</div>

Writing a xv6 port to zig and ran into an issue with the uart0 not being able to take in keyboard inputs. Ive tested it manually but for some reason the actual keyboard isn’t working. anyone implement it before or know how to debug it?

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [August 8, 2024, 8:38am UTC](https://ziggit.dev/t/uart0-qemu-keyboard-input/5549/2 "2024-08-08T08:38:28Z")

</div>

Don’t forget to mark the pointers [volatile](https://ziglang.org/documentation/master/#volatile) when you reference memory mapped I/O.  
`const io_ptr: *volatile u8 = @ptrFromInt(0x12345678);`

* * *

What machine are you emulating with qemu?

---

<div class="post-metadata">

**Author:** ![csirak](https://ziggit.dev/user_avatar/ziggit.dev/csirak/32/2542_2.png) [@csirak](https://ziggit.dev/u/csirak)\
**Post date:** [August 8, 2024, 4:33pm UTC](https://ziggit.dev/t/uart0-qemu-keyboard-input/5549/3 "2024-08-08T16:33:23Z")

</div>

emulating riscv64 and i use volatile ptrs here is my config

 ![Screenshot 2024-08-08 at 12.32.36 PM](https://ziggit.dev/uploads/default/original/2X/8/84ab472e7bc52491c834cafde320d39e0029c2b4.png)

---

<div class="post-metadata">

**Author:** ![csirak](https://ziggit.dev/user_avatar/ziggit.dev/csirak/32/2542_2.png) [@csirak](https://ziggit.dev/u/csirak)\
**Post date:** [August 8, 2024, 4:33pm UTC](https://ziggit.dev/t/uart0-qemu-keyboard-input/5549/4 "2024-08-08T16:33:52Z")

</div>

I get interrupts but i dont get keyboard input data

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [August 8, 2024, 5:29pm UTC](https://ziggit.dev/t/uart0-qemu-keyboard-input/5549/5 "2024-08-08T17:29:33Z")

</div>

I assume that you use the `virt` board.

The sources are: [qemu/hw/riscv/virt.c at master · qemu/qemu · GitHub](https://github.com/qemu/qemu/blob/master/hw/riscv/virt.c)  
From that file: `UART0` is memory mapped and the base address is `0x10000000`

The uart chip is `ns16550a`, it is the original ibm pc uart. There are 8 registers, the first (in the base address) is for reading/writing the uart bytes. For sure you must initialize some registers (for 8 bits, 1 stop bit, rate, etc.).

The registers are:

| Address | Name | Description |
| --- | --- | --- |
| base+0 | rxd/txd | Receive/Transmitt data |
| base+1 | ier | Interrupt enable |
| base+2 | fcr | FIFO control |
| base+3 | lcr | Line control |
| base+4 | mcr | Modem control |
| base+5 | lsr | Line status |
| base+6 | msr | Modem status |
| base+7 | scr | Scratch Pad |

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [August 8, 2024, 5:55pm UTC](https://ziggit.dev/t/uart0-qemu-keyboard-input/5549/6 "2024-08-08T17:55:17Z")

</div>

There is [GitHub - tzx/nOSering: Attempt on RISC-V Operating System in Zig](https://github.com/tzx/nOSering)

Amazing uart info: [Serial UART, an in depth tutorial - Lammert Bies](https://www.lammertbies.nl/comm/info/serial-uart)  
uart implementation in zig: [nOSering/src/uart.zig at main · tzx/nOSering · GitHub](https://github.com/tzx/nOSering/blob/main/src/uart.zig)  
blog post: [Timmy Xiao](https://timmy.moe/blog/barebones-os-zig/)

---

<div class="post-metadata">

**Author:** ![csirak](https://ziggit.dev/user_avatar/ziggit.dev/csirak/32/2542_2.png) [@csirak](https://ziggit.dev/u/csirak)\
**Post date:** [August 8, 2024, 7:01pm UTC](https://ziggit.dev/t/uart0-qemu-keyboard-input/5549/7 "2024-08-08T19:01:11Z")

</div>

yep ive looked at all of those and emailed lammert, in that implementation he doesn’t actually get uart input but I’ve tried his config

---

<div class="post-metadata">

**Author:** ![csirak](https://ziggit.dev/user_avatar/ziggit.dev/csirak/32/2542_2.png) [@csirak](https://ziggit.dev/u/csirak)\
**Post date:** [August 8, 2024, 8:11pm UTC](https://ziggit.dev/t/uart0-qemu-keyboard-input/5549/8 "2024-08-08T20:11:15Z")

</div>

found the bug was not in qemu, a interrupt enable bit wasn’t being set in my trap handler

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [August 8, 2024, 8:24pm UTC](https://ziggit.dev/t/uart0-qemu-keyboard-input/5549/9 "2024-08-08T20:24:17Z")

</div>

> [@Avoid Screenshots of Text/Code](https://ziggit.dev/t/avoid-screenshots-of-text-code/5369):
>
> Why avoid Screenshots of Text/Code? I dislike screenshots of text, because they are: less accessible (text can be read via screen-readers, or braille displays) less searchable (text recognition tools often aren’t easily available / not well integrated or may contain faults in their output) annoying (you can’t easily/reliably copy the text out of a screenshot) I think these are good reasons to keep the amount of screenshots of text low in the forum and only use them, when it can’t be avoided o…
