F28HS Hardware-Software Interface:Systems Programming
Hans-Wolfgang Loidl
School of Mathematical and Computer Sciences,Heriot-Watt University, Edinburgh
Semester 2 — 2020/21
0No proprietary software has been used in producing these slidesHans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface 2020/21 1 / 39
Outline
1 Tutorial 1: ARM Assembler Exercise: Caesar Cipher (ROT13)
2 Tutorial 2: Programming an LED
3 Tutorial 3: Programming a Button
4 Tutorial 4: Programming a HEX display
5 Tutorial 5: Cache-friendly Programming
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface 2020/21 2 / 39
Overview of the Caesar Cipher (ROT13) Exercises
This sequence of screencasts is intended to give you a deeperunderstanding of ARM Assembler programmingWe will develop one non-trivial program step-by-step in ARMAssemblerFollow the online screencasts, and do the matching lab exercisesA Caesar cipher is a very simple cipher, that encodes a string byrotating each char by a certain amountFor example, with rotation value of 1: a → b, b → c, ..., z → aA Caesar cipher with rotation value of 13 (ROT13) is sometimesused for low-security encryption.Background on Caesar cipher:
I see slides (15–20) in Introduction to CryptographyI exercise of breaking (optional, search for “Exercise 1: Caesar
cipher”)I see also this wikipedia page on ROT13
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 1: Prging an LED 3 / 39
Structure of the Sequence of Screencasts
TASK: Implement a Caesar cipher (rotation cipher) in ARMAssemblerBackground: you should have watched the ARM Assemblerprogramming leactures (F28HS lectures Assembler 1-5) and donethe matching tutorials and lab exercisesStructure:
I Version 0: C versionI Version 1: plain (lower case letters only)I Version 2: printing the result (using printf libc function)I Version 3: using subroutinesI Version 4: covering upper case letters as wellI Version 5: using word load and store (for fewer memory operations)I Version 6: printing the result (in Assembler as well)
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 1: Prging an LED 4 / 39
Tutorial 2: Programming an LED
This tutorial will deal with programming an LED output device.This is the “hello world” program for external devices.It will deal with programming techniques common to other outputdevices.The learning objective of this exercise is to learn how to directlycontrol an external device through C and Assembler programs.
Note: This year we will be using the online CPUlator (an ARMsimulator) for programming (virtual) external devices. This uses adifferent chipset (De1-SoC by Altera) from the chip-set that’s on a RPi(BCM2836 SoC by Broadcom). The RPi code that you might findonline, will NOT work on the CPUlator.Documentation for the De1-SoC (Altera) chip is available here.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 5 / 39
Tutorial 2: Programming an LED
This tutorial will deal with programming an LED output device.This is the “hello world” program for external devices.It will deal with programming techniques common to other outputdevices.The learning objective of this exercise is to learn how to directlycontrol an external device through C and Assembler programs.
Note: This year we will be using the online CPUlator (an ARMsimulator) for programming (virtual) external devices. This uses adifferent chipset (De1-SoC by Altera) from the chip-set that’s on a RPi(BCM2836 SoC by Broadcom). The RPi code that you might findonline, will NOT work on the CPUlator.Documentation for the De1-SoC (Altera) chip is available here.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 5 / 39
The high-level picture
From the main chip we want to control an (external) device, herean LED.We use one of the LEDs that is connected to the chip on theCPUlator.Logically we want to send 1 bit to this device to turn it on/off.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 6 / 39
The low-level picture
Programmatically we need to go through these steps:Identify the peripheral registers, in charge of LEDs, from the AlteraDe1-SoC manualIdentify the bits in the registers that control a specific LEDThen, directly access the device via memory read/writes intothese registersNote: In the program, turning on an LED is typically just one writeoperation
Note: if running an OS on the device, a step of memory-mapping theaddress space of the GPIO registers into user-space is needed (e.g.on a Raspberry Pi); since we are running code directly on the device(“bare metal”) no suxh step is needed.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 7 / 39
Finding the registers in the manual
The relevant information can be found the hardware manual forthe relevant chip (for us, the DE1-SoC chip by Altera)Such manuals are very detailed, since they need to cover allissues about programming and usage (for CS as well as EEapplications)To find the info in these documents, look for sections oncomponents / peripherals (for us, “2. DE1-SoC ComputerContents”)Specifically, we need the information on the LEDs, in Section“2.5.7. Red LED Parallel Port”.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 8 / 39
Data explained
The picture (and discussion in the manual) tells usThe lowest 10 bits in the register at address 0xFF200020 are incharge of 10 LEDs hard-wired to the chipThe remaining bits in the register are unusedNote that the processor has a word-size of 32-bitsEach of the lower bits controls one LED, and turns it on (bit: 1) oroff (bit: 0)
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 9 / 39
Controlling an LED in C
Here is a simple program to turn on the first LED (from the right):
#define GPIOBASE 0xFF200000static unsigned long *gpio = GPIOBASE;void main () {
*gpio = 0b00000001;}
In essence, this program performs one write operation: it writes thevalue 1 into the memory address 0xFF200000, which we know is incharge of the LEDs.The value 1 represents the bitmask 0b00000001, meaning turn on thefirst (rightmost) LED.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 10 / 39
CPUlator after running the program
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 11 / 39
Controlling an LED in ARM Assembler
We can achieve the same behaviour in ARM Assembler:
.textLDR R0, =LEDBASE @ physical address of LEDsMOV R2, #0b00000001 @ bitmask to turn on first LEDSTR R2, [R0, #0] @ turn it on
.data
.equ LEDBASE, 0xFF200000
First we load the bitmask 0b00000001 (first LED on, all otherLEDs off) into register R2.Then we write this value into the LED register.The behaviour is the same as in the C program before.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 12 / 39
Next Task: Blinking LED in ARM Assembler
We now want to implement a blinking LEDThis should be coded in ARM AssemblerWe use the same principles as before, but need an (infinite) loopWe also need some kind of delay / timer functionality
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 13 / 39
Blinking LED in ARM Assembler
.global _start_start:
start: LDR R2, =LEDBASE @ physical address of the LEDMOV R1, #10 @ counter
loop: NOPEOR R3, #0b1 @ flip current value: 1->0, 0->1STR R3, [R2] @ store value into LED registerBL delaySUBS R1, #1 @ decrement counter & set flagsBNE loop @ R1 != 0 => continue loop
.data
.equ LEDBASE, 0xFF200000
One loop (with counter in R1), flipping a value (R3) in each iteration.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 14 / 39
The delay function in ARM Assembler
delay: PUSH {R4-R7,LR}MOV R4, #0x0f
loop1: LDR R5, =#0xffffbody: NOP
SUBS R5, #1BNE bodySUBS R4, #1BNE loop1POP {R4-R7,LR}BX LR
In essence, two nested loops that do nothing.The choice of the counters depends on the chip / simulator speed.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 15 / 39
Summary
Controlling a simple external device means logically sending 1 bitof information (on/off)Realising this control means physically writing into specialregisters which have special meaningThe information on the special meaning is usually in bulkyhardware-description documentationOnce uncovered, the code for direct device control is fairly short
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 16 / 39
Tutorial 3: Programming a Button input device
In this tutorial we want to use a button, as simulated on theCPUlatorUsually, these devices would be connected through a breadboard.A button is the simplest input device that we will cover.The CPUlator simulates 4 push-buttonsThe code needed to control is typical for such devices.This tutorial deals with programming a button as input device.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 17 / 39
Accessing Push-Buttons on the CPUlator
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 18 / 39
Core Techniques
In the LED tutorial, we have seen that we first need to identify theregisters that give control to the device.For that we will again look into the De1-SoC ManualWe need to identify the documentation for peripheral devices:“2.5.10 Pushbutton Key Parallel Port” on page 10.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 19 / 39
The low-level picture
We need to read from address 0xFF200050 to determine thestate of the buttonsAgain there is a one-to-one relationship between the lowest 4 bitsin the register and the buttons:if button 0 is pressed, bit 0 will be set to 1if button 1 is pressed, bit 1 will be set to 1 etcThus, reading from the register and masking the right bit is all weneed to do.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 20 / 39
Reading from a Button in ARM Assembler
.global _start_start:
LDR R0, =BUTTONBASE @ physical address of the buttonsLDR R3, [R0] @ read value from BUTTON register
@ this will be a binaryrepresentation of buttonspressed
@ i.e. 1st and 2nd button pressed=> 0b11 == 3
.data
.equ BUTTONBASE, 0xFF200050
We read the contents of the button register, containing the status of allbuttons, into register R3.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 21 / 39
A button Press Counter in ARM Assembler
.global _start_start:start: LDR R0, =BUTTONBASE @ physical address of buttons
MOV R1, #10 @ loop counterMOV R2, #0 @ button press counterMOV R5, #1LSL R5, #BUTTON_NO @ bitmask for testing BUTTON_NO
loop0: NOPLDR R3, [R0] @ read value from BUTTON
registerCMP R3, R5BNE nocountADD R2, R2, #1 @ increment press counter
nocount: BL delaySUBS R1, #1BNE loop0
.data
.equ BUTTONBASE, 0xFF200050
.equ BUTTON_NO, 0
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 22 / 39
A button Press Counter in ARM Assembler(Discussion)
This code continuously reads the button registerWe prepare a bit-mask in R5 to identify the bit in charge of buttonnumber BUTTON_NO.We first read the contents of the button register into register R3.We use the bit-mask in R5 to extract the bit in charge of buttonnumber BUTTON_NO.If this bit is on, we increment the counter in R2.We finish the program after a fixed number of iterations (couldalso use an infinite loop here)When running this in the CPUlator do check that R2 acts as acounter of how often a press has been registeredNote: In practice, you want to count distinct button presses, i.e.don’t count a continuous button press several times
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 23 / 39
A button Press Counter in ARM Assembler.global _start_start:start: LDR R0, =BUTTONBASE @ physical address of buttons
MOV R1, #10 @ loop counterMOV R2, #0 @ button press counterMOV R5, #1LSL R5, #BUTTON_NO @ bitmask for testing BUTTON_NO
loop0: NOPLDR R3, [R0] @ read value from registerAND R3, R5 @ extract the relevant bitCMP R3, #0 @ check whether it is onBEQ nocountADD R2, R2, #1 @ increment press counter
nocount: BL delaySUBS R1, #1BNE loop0
.data
.equ BUTTONBASE, 0xFF200050
.equ BUTTON_NO, 0
This version checks only for the specific button BUTTON_NO.Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 24 / 39
The delay function is the same as before
delay: PUSH {R4-R7,LR}MOV R4, #0x0f
loop1: LDR R5, =#0xffffbody: NOP
SUBS R5, #1BNE bodySUBS R4, #1BNE loop1POP {R4-R7,LR}BX LR
These are in essence two nested loops, doing nothing in the body.Needed to provide a delay between button reads.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 25 / 39
Summary
Reading input from a button works in the same way as writing tothe LED:
I We need to read from a certain bit in a register, in charge of buttons.
With the button you have a basic input device to communicatewith the systemOther devices use the same principle, but communicate moredata, e.g. an 8-bit wide parallel port for I/O communicationWith the CPUlator’s restrictions, we don’t discuss these devices inany more detail this year
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 26 / 39
Tutorial 4: Programming a HEX display
The De1-SoC chip (by Altera) on the CPUlator, provides several7-segment displays, or HEX displaysThese displays can be used to display numbers, and some lettersTherefore, the output is more meaningful than just one LEDThe principle to program the display is the same as for the LEDBut first we need to find the relevant information in the manual:“2.5.8 7-Segment Displays Parallel Port” (page 10)
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 27 / 39
Program the 7-Segment (HEX) Displays in Assembler
The address for the data register of the HEX displays is0xFF200020The 7 segments in the HEX display are each controlled by 1 bit inthe HEX data registerThe mapping of the bit positions to the segments is shown in thepicture on the right, e.g. bit 0 controls the upper bar in display 1We need 7 bits per HEX display in the register (8th bit unused).The next 7 bits are in charge of the next HEX display (HEX1) etc.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 28 / 39
Note:
1 segment controlled by 1 bit
Program the 7-Segment (HEX) Displays in Assembler
The address for the data register of the HEX displays is0xFF200020The 7 segments in the HEX display are each controlled by 1 bit inthe HEX data registerThe mapping of the bit positions to the segments is shown in thepicture on the right, e.g. bit 0 controls the upper bar in display 1We need 7 bits per HEX display in the register (8th bit unused).The next 7 bits are in charge of the next HEX display (HEX1) etc.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 28 / 39
Note:
1 segment controlled by 1 bit
Controlling a HEX display in ARM Assembler
.global _start_start: LDR R0, =HEXBASE @ physical address of the HEX
MOV R2, #0b1011011 @ bitmask for pattern of a ’2’MOV R3, R2, LSL #24 @ display: 2MOV R2, #0x3f @ bitmask using hex-repres.ORR R3, R2, LSL #16 @ display: 0MOV R2, #91 @ bitmask using a decimal no.ORR R3, R2, LSL #8 @ display: 2MOV R2, #6 @ again usin a decimal valueORR R3, R2, LSL #0 @ display: 1STR R3, [R0]
.data
.equ HEXBASE, 0xFF200020
This code displays the number 2021 on the four rightmost displays ofthe CPUlator
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 29 / 39
HEX Display Example
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 30 / 39
Exercise: Button read and HEX display
As next exercise we want to continuously read from a buttondeviceand display on the HEX display whether we detected a press(“on”) or not (“off”)This combines the previous 2 exercisesThe core structure is a loop (either infinite or with a fixed counter)A delay is used in the loop to control the frequency of checks
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 31 / 39
Exercise: Button read and HEX display (structure)
_start:start: LDR R2, =BUTTONBASE @ physical address of buttons
________________________ @ (1) construct bitmask________________________ @ (1) construct bitmask
loop0: NOPLDR R3, [R2] @ read value from BUT registerCMP R3, ___ @ (2) check against bitmask______________________ @ (3) not pressed => OFF
nocount:______________________ @ (3) pressed => ONBL delayB loop0 @ NB: infinite loop
We need to construct a bitmask for selecting the bit matching buttonBUTTON_NO in (1).
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 32 / 39
Exercise: Button read and HEX display (structure)
_start:start: LDR R2, =BUTTONBASE @ physical address of buttons
MOV R5, #1LSL R5, #BUTTON_NO @ (1) bitmask testing BUTTON_NO
loop0: NOPLDR R3, [R2] @ read value from BUT registerCMP R3, ___ @ (2) check against bitmask______________________ @ (3) not pressed => OFF
nocount:______________________ @ (3) pressed => ONBL delayB loop0 @ NB: infinite loop
In (2) we need to check R3 against the bitmask constructed above.In (3) we need to call sub-routines to show either “on” or “off”.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 33 / 39
Exercise: Button read and HEX display
_start:start: LDR R2, =BUTTONBASE @ physical address of buttons
MOV R5, #1LSL R5, #BUTTON_NO @ (1) bitmask for testing
BUTTON_NOloop0: NOP
LDR R3, [R2] @ read value from BUT registerCMP R3, R5BLNE showoff @ (3) not pressed => OFF
nocount:BLEQ showon @ (3) pressed => ONBL delayB loop0 @ NB: infinite loop
Now we still need to define the sub-routines showon and showff.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 34 / 39
Exercise: Button read and HEX display (show)
showon: PUSH {R4-R7, LR}LDR R4, =HEXBASELDR R5, =char_oLDRB R6, [R5]LSL R6, #8LDR R5, =char_nLDRB R7, [R5]ORR R6, R7MOV R0, R6STR R0, [R4]POP {R4-R7, LR}BX LR
This displays “on” on the HEX displays, without disrupting registers.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 35 / 39
Exercise: Button read and HEX display (show)
showoff: PUSH {R4-R7, LR}LDR R4, =HEXBASELDR R5, =char_oLDRB R6, [R5]LSL R6, #8LDR R5, =char_fLDRB R7, [R5]ORR R6, R7LSL R6, #8LDR R5, =char_fLDRB R7, [R5]ORR R6, R7MOV R0, R6STR R0, [R4]POP {R4-R7, LR}BX LR
This displays “off” on the HEX displays, without disrupting registers.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 36 / 39
Constants for the letters to display
Below we define the bit-patterns for the letters that we need.
.align 4char_o: .byte 0b1011100 @ ochar_n: .byte 0b1010100 @ nchar_f: .byte 0b1110001 @ f
See the picture about the 7-segement display at the beginning of theslide set to match bit-patterns to segments on the display.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 37 / 39
Summary
Reading from a button, amounts to reading from a register andselecting the right bit (1 bit)Writing to a display, amounts to writing to a register and definingthe right bit pattern (7 bits)All other input/output operations for external devices can be builton top of this basic functionalityDetails of registers and how to access them vary from chip to chipFor example, the Raspberry Pi 2 also uses an ARM core, but aBCM2835 chipThe handling of LEDs and buttons is therefore different, and morecomplex on the Raspberry Pi 2.
Optional: For those interested, complete tutorials for programming theRaspberry Pi 2 are available on the course’s web page for 2019/20.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 38 / 39
Summary
Reading from a button, amounts to reading from a register andselecting the right bit (1 bit)Writing to a display, amounts to writing to a register and definingthe right bit pattern (7 bits)All other input/output operations for external devices can be builton top of this basic functionalityDetails of registers and how to access them vary from chip to chipFor example, the Raspberry Pi 2 also uses an ARM core, but aBCM2835 chipThe handling of LEDs and buttons is therefore different, and morecomplex on the Raspberry Pi 2.
Optional: For those interested, complete tutorials for programming theRaspberry Pi 2 are available on the course’s web page for 2019/20.
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 38 / 39
Tutorial 5: Cache-friendly Programming
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 5: Cache-friendly Programming 39 / 39