|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "QEMU 4.0 adds micro:bit emulation support" |
| 4 | +date: 2019-05-22 12:45:00 +0200 |
| 5 | +categories: ['qemu 4', microbit, gsoc, outreachy, internships] |
| 6 | +--- |
| 7 | +[micro:bit](http://microbit.org/) emulation support is available from QEMU 4.0 |
| 8 | +onwards and can be used for low-level software testing and development. Unlike |
| 9 | +existing micro:bit simulators, QEMU performs full-system emulation and actually |
| 10 | +runs the same ARM code as the real hardware. This blog post explains what |
| 11 | +full-system emulation means and why QEMU is now a useful tool for developing |
| 12 | +micro:bit software. |
| 13 | + |
| 14 | +The [micro:bit is a tiny ARM board](https://tech.microbit.org/hardware/) |
| 15 | +designed for teaching. It is increasingly being used around the world to |
| 16 | +expose children to computers, programming, and electronics in a low-cost way |
| 17 | +with an active online community that shares project ideas, lesson plans, and |
| 18 | +programming tips. |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +## Simulators and emulators |
| 23 | +*Simulators* are used for many tasks from mobile app development to |
| 24 | +performance analysis of computer hardware. It is possible to develop code |
| 25 | +using a simulator without having access to real hardware. Oftentimes using a |
| 26 | +simulator is more convenient than flashing and debugging programs on real |
| 27 | +hardware. |
| 28 | + |
| 29 | +*Emulators* allow programs written for one computer system to run on a |
| 30 | +different computer system. They use techniques like [machine code |
| 31 | +interpreters](https://en.wikipedia.org/wiki/Interpreter_%28computing%29) and |
| 32 | +[just-in-time |
| 33 | +compilers](https://en.wikipedia.org/wiki/Just-in-time_compilation) to execute |
| 34 | +guest programs that do not run natively on the host computer. Each CPU |
| 35 | +instruction must be correctly implemented by the emulator so it can run guest |
| 36 | +software. |
| 37 | + |
| 38 | +## How existing micro:bit simulators work |
| 39 | +Simulators can be implemented at various layers in the software stack. The |
| 40 | +[MakeCode editor](https://makecode.microbit.org/#editor) for JavaScript |
| 41 | +development includes a micro:bit simulator: |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +This simulator does not execute any ARM code and is therefore not running |
| 46 | +the same CPU instructions as a real micro:bit. Instead it reuses the JavaScript |
| 47 | +engine already available in your web browser to execute micro:bit JavaScript |
| 48 | +programs. This is achieved by providing the micro:bit JavaScript APIs that |
| 49 | +micro:bit programs expect. The programs don't need to know whether those APIs |
| 50 | +are implemented by the real micro:bit software stack or whether they are |
| 51 | +actually calling into the MakeCode simulator. |
| 52 | + |
| 53 | +In the screenshot above the micro:bit program calls `showString("Hello |
| 54 | +world!")` and this becomes a call into the MakeCode simulator code to |
| 55 | +render images of LEDs in the web browser. On real hardware the code path is |
| 56 | +different and eventually leads to an LED matrix driver that lights |
| 57 | +up the LEDs by driving output pins on the micro:bit board. |
| 58 | + |
| 59 | +## Full-system emulation |
| 60 | +Unlike the MakeCode simulator, QEMU emulates the micro:bit CPU and boots |
| 61 | +from the same ARM code as the real micro:bit board. The simulation happens at |
| 62 | +the CPU instruction and hardware interface level instead of at the JavaScript |
| 63 | +API level. This is called *full-system emulation* because the entire |
| 64 | +guest software environment is present. |
| 65 | + |
| 66 | +What are the advantages of full-system emulation? |
| 67 | +* Programs written in any language can run (MicroPython, mbed C/C++, etc) |
| 68 | +* Boot, device driver, and language run-time code can be tested |
| 69 | +* Bugs in lower layers of the software stack can be reproduced |
| 70 | +* CPU architecture-specific bugs can be reproduced (stack and memory corruption bugs) |
| 71 | +* A debugger can be connected to inspect the entire software stack |
| 72 | + |
| 73 | +The main disadvantage of full-system emulation is that the performance |
| 74 | +overhead is higher since simulation happens at the CPU instruction level. |
| 75 | +Programs consist of many CPU instructions so the task of emulation is |
| 76 | +performance-sensitive. Luckily the micro:bit's CPU is much less powerful than |
| 77 | +CPUs available in our laptops and desktops, so programs execute at a reasonable |
| 78 | +speed. |
| 79 | + |
| 80 | +## Running micro:bit programs on QEMU |
| 81 | +QEMU emulates the core devices on the micro:bit, including the serial port |
| 82 | +(UART) and timers. This is enough for developing and testing low-level |
| 83 | +software but does not offer the LEDs, radio, and other devices that most |
| 84 | +micro:bit programs rely on. These devices might be emulated by QEMU in the |
| 85 | +future, but for now the main use of QEMU is for developing and testing |
| 86 | +low-level micro:bit code. |
| 87 | + |
| 88 | +To run `test.hex`: |
| 89 | +```shell |
| 90 | +$ qemu-system-arm -M microbit -device loader,file=test.hex -serial stdio |
| 91 | +``` |
| 92 | + |
| 93 | +Any output written to the serial port is printed to the terminal by QEMU. |
| 94 | + |
| 95 | +## Debugging micro:bit programs with QEMU and GDB |
| 96 | +QEMU has GDB guest debugging support. This means GDB can connect to QEMU in |
| 97 | +order to debug the guest software. This is similar to debugging a real system |
| 98 | +over JTAG, except no hardware is necessary! |
| 99 | + |
| 100 | +Connect with GDB to debug the guest: |
| 101 | +``` |
| 102 | +$ qemu-system-arm -M microbit -device loader,file=test.hex -s |
| 103 | +$ gdb |
| 104 | +(gdb) target remote tcp:127.0.0.1:1234 |
| 105 | +(gdb) x/10i $pc |
| 106 | +=> 0x161c4: ldr r3, [r4, #0] |
| 107 | + 0x161c6: cmp r3, #0 |
| 108 | + 0x161c8: beq.n 0x161d2 |
| 109 | + 0x161ca: ldr r3, [pc, #48] ; (0x161fc) |
| 110 | + 0x161cc: ldr r3, [r3, #0] |
| 111 | + 0x161ce: cmp r3, #0 |
| 112 | + 0x161d0: bne.n 0x161d8 |
| 113 | + 0x161d2: movs r0, #6 |
| 114 | + 0x161d4: bl 0x16160 |
| 115 | + 0x161d8: ldr r0, [r4, #0] |
| 116 | +``` |
| 117 | + |
| 118 | +Having a debugger is very powerful. QEMU can also load ELF files in |
| 119 | +addition to the popular .hex files used for micro:bit programs. ELF files can |
| 120 | +contain debugging information that enables source-level debugging so GDB can |
| 121 | +display function and variable names as well as listing the source code instead |
| 122 | +of showing assembly instructions. |
| 123 | + |
| 124 | +## Conclusion |
| 125 | +QEMU now offers a platform for developing and testing micro:bit programs. |
| 126 | +It is open to future extension, hopefully to emulate more devices and offer |
| 127 | +a graphical user interface. |
| 128 | + |
| 129 | +micro:bit emulation was contributed by Julia Suvorova and Steffen Görtz as |
| 130 | +part of their Outreachy and Google Summer of Code internships with QEMU. Jim |
| 131 | +Mussared, Joel Stanley, and Stefan Hajnoczi acted as mentors and contributed |
| 132 | +patches as well. |
0 commit comments