This repository contains assembly code and scripts used in the lab classes of the course "Architettura degli Elaboratori" of the Department of Computer Science of the University of Catania.
In order to emulate Raspberry Pi OS on Ubuntu you need:
- qemu-system
- Latest qemu kernel
- Raspberry Pi OS image
sudo apt-get install qemu-system
This git repository contains the latest qemu-rpi kernel. I added a copy of the repository here in the folder qemu-rpi-kernel
You can find the image of the last Rspberry Pi OS in this link. Follow the istructions and save the .img file in the main folder of this repository
First change the name of the image inside the start.sh script with the name of the image you downloaded (-hda argument):
#!/bin/bash
qemu-system-arm \
-M versatilepb \
-cpu arm1176 \
-m 256 \
-hda YOUR_IMAGE_NAME.img \
-net nic -net user,hostfwd=tcp::5022-:22 \
-dtb qemu-rpi-kernel/versatile-pb-buster.dtb \
-kernel qemu-rpi-kernel/kernel-qemu-4.19.50-buster \
-append 'root=/dev/sda2 panic=1' \
-no-reboot
Now you can simply run the script and qemu will start:
./start.sh
The default username and password for the Raspberry Pi OS are:
- User: pi
- Password: raspberry
In order to connect via ssh to the Raspberry Pi OS running on qemu, open a terminal in Raspberry Pi OS and start the ssh service:
sudo service ssh start
You can now connect to the Raspberry Pi OS from your host system:
ssh [email protected] -p 5022 -X
And send files to the Raspberry Pi OS using scp:
scp -P 5022 -r PATH_OF_THE_FOLDER_IN_THE_HOST_SYSTEM [email protected]:PATH_OF_THE_DESTINATION_FOLDER_IN_RASPBERRY_PI_OS
If SSH doesn’t start in your emulator at startup by default, you can change that inside your Pi terminal with:
sudo update-rc.d ssh enable
Inside the assembly directory there are 2 programs written in assembly:
- prodottoMain.s: Simple program that calculates the product of 2 integers. The input numbers are read using the clib functions printf and scanf. The function that actually calculates the product is stored in a different source file (prodottoFunc.s)
- ledon.s: Simple program that uses some GPIO pins to control 3 leds pressing a button. This code will only run on a real raspberry hardware since the GPIO pins are not emulated in the kernel used here
In order to assemble prodottoMain in the raspberry Pi OS we can use gcc:
gcc -g -o prodotto prodottoFunc.s prodottoMain.s
After running gcc the executable is saved in the file prodotto. To run the program we can simply execute the following command:
./prodotto
If we want to run it in debug mode we can use gdb:
gdb ./prodotto
Please refer to this tutorial to learn how to use the basic functions of gdb debugger
In order to run this code you need a raspberry Pi 2/3 board. The program is expecting to have 3 leds connected to the pins GPIO 5, 6 and 26, and a button connected to the pin GPIO 22. The following schematic shows a possible way to connect the components to the board.
The led connected to GPIO26 will turn on everytime the button is pressed, the led on GPIO5 will switch its state whenever the button transits from not-pressed to pressed, and the led on GPIO6 will switch its state whenever the button transits from pressed to not-pressed.
In order to assemble ledon.s in the raspberry Pi OS use gcc:
gcc -g -o ledon ledon.s
To run the program execute the following command:
./ledon
If we want to run it in debug mode use this command:
gdb ./ledon