CPULATOR Functionalities
CPULATOR Functionalities
CPULATOR Functionalities
Systems Programming
Hans-Wolfgang Loidl
Semester 2 — 2020/21
0
No proprietary software has been used in producing these slides
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface 2020/21 1 / 39
Outline
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 1: Prging an LED 4 / 39
Tutorial 2: Programming an LED
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 5 / 39
Tutorial 2: Programming an LED
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 5 / 39
The high-level picture
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 8 / 39
Data explained
Here is a simple program to turn on the first LED (from the right):
#define GPIOBASE 0xFF200000
static unsigned long *gpio = GPIOBASE;
void main () {
*gpio = 0b00000001;
}
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
.data
.equ LEDBASE, 0xFF200000
First we load the bitmask 0b00000001 (first LED on, all other
LEDs 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
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:
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
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 15 / 39
Summary
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 2: Prging an LED 16 / 39
Tutorial 3: Programming a Button 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 the
registers that give control to the device.
For that we will again look into the De1-SoC Manual
We 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
.global _start
_start:
LDR R0, =BUTTONBASE @ physical address of the buttons
LDR R3, [R0] @ read value from BUTTON register
@ this will be a binary
representation of buttons
pressed
@ 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 all
buttons, 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 counter
MOV R2, #0 @ button press counter
MOV R5, #1
LSL R5, #BUTTON_NO @ bitmask for testing BUTTON_NO
loop0: NOP
LDR R3, [R0] @ read value from BUTTON
register
CMP R3, R5
BNE nocount
ADD R2, R2, #1 @ increment press counter
nocount: BL delay
SUBS R1, #1
BNE 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)
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
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 3: Prging a Button 26 / 39
Tutorial 4: Programming a HEX display
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
Note:
1 segment controlled by 1 bit
.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: 2
MOV R2, #0x3f @ bitmask using hex-repres.
ORR R3, R2, LSL #16 @ display: 0
MOV R2, #91 @ bitmask using a decimal no.
ORR R3, R2, LSL #8 @ display: 2
MOV R2, #6 @ again usin a decimal value
ORR R3, R2, LSL #0 @ display: 1
STR R3, [R0]
.data
.equ HEXBASE, 0xFF200020
This code displays the number 2021 on the four rightmost displays of
the 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
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
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, #1
LSL R5, #BUTTON_NO @ (1) bitmask testing BUTTON_NO
loop0: NOP
LDR R3, [R2] @ read value from BUT register
CMP R3, ___ @ (2) check against bitmask
______________________ @ (3) not pressed => OFF
nocount:
______________________ @ (3) pressed => ON
BL delay
B loop0 @ NB: infinite loop
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, #1
LSL R5, #BUTTON_NO @ (1) bitmask for testing
BUTTON_NO
loop0: NOP
LDR R3, [R2] @ read value from BUT register
CMP R3, R5
BLNE showoff @ (3) not pressed => OFF
nocount:
BLEQ showon @ (3) pressed => ON
BL delay
B loop0 @ NB: infinite loop
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 34 / 39
Exercise: Button read and HEX display (show)
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 35 / 39
Exercise: Button read and HEX display (show)
See the picture about the 7-segement display at the beginning of the
slide 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
Hans-Wolfgang Loidl (Heriot-Watt Univ) F28HS Hardware-Software Interface Tutorial 4: Prging a Button 38 / 39
Summary
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