Code snippets
Contents
Note : Most of the code here is C compiled with GCC.
The code embed a shell script to compile itself so just run :
sh code.c
C / Shell : Standalone single file compilation / execution
(self-executable C file)
A C / Shell trick to pack the code, compilation and execution
of a C program into a single file which is useful for short
programs, prototypes, debugging or to show off C examples, put this
into a test.c file and run it with sh test.c :
#ifdef d
#!/bin/sh
gcc -Wall -O0 test.c -o out
./out
exit
#endif
// your usual C code
C
/ x86 : SIMD debugging / exploration (with intrinsics)
This is a program i did as an exploration / debugging tool
when i started to use SIMD instructions, it use compiler
intrinsics (see the
intrinsics guide) and just print the content of SIMD registers
(here AVX),
quite useful to quickly evaluate the result of some SIMD code. Just
put it into a simd.c file and run it with sh simd.c
:
#ifdef d
#!/bin/sh
gcc -Wall -mavx2 -m32 -O0 simd.c -o out
./out
exit
#endif
#include <stdio.h>
#include <x86intrin.h>
float input2[8] = {
0.75f, 0.5f, 0.25f, 0.0f
};
float output[8] = {
0
};
int main (void) {
__m256i i1i = _mm256_set_epi32(0, 0, 0, 0, 0, 6,
4, 2);
__m256 v1 = _mm256_cvtepi32_ps(i1i);
__m256 i2 = _mm256_loadu_ps(&input2[0]);
__m256 v2 = _mm256_mul_ps(v1, i2);
__m256 v3 = _mm256_hadd_ps(v2, v2);
_mm256_storeu_ps((float *)output, v3);
for (int i = 0; i < 8; i += 1) {
printf("%f ",
output[i]);
}
printf("\n");
return 0;
}
C Raspberry PI Zero 1.3 U-Boot bare metal sample
Quick way for bare metal PI Zero C program with U-Boot, only
require a GCC for the
target platform and Das U-Boot, on
Ubuntu :
sudo apt-get install
gcc-arm-linux-gnueabihf
Might work without U-Boot but untested, this will produce a
minimal binary which can be a starting point though but might
require more initialization code without U-Boot and address will be
likely 0x8000 instead of 0x80000.
This produce a
sample.bin
file which can be
copied to a SD card with all the others proprietary PI blobs along
with U-Boot binary and configuration stuff (config.txt
etc.)In U-Boot prompt you can then load and run the binary (can be
automated as well in U-Boot config.) :
fatload usb 0:1 0x80000
sample.bin
go 0x80000
sample.c :
#ifdef d
#!/bin/sh
set -e
rm -f *.o *.elf *.bin
arm-linux-gnueabihf-as --warn --fatal-warnings start.s -o
start.o
arm-linux-gnueabihf-gcc -Wall -O3 -march=armv6 -mtune=arm1176jzf-s
-mfloat-abi=soft -ffreestanding -nostdlib -c sample.c -o
sample.o
arm-linux-gnueabihf-ld -T rpi0.ld -nostdlib start.o sample.o -o
sample.elf
arm-linux-gnueabihf-objcopy sample.elf -O binary sample.img
#arm-linux-gnueabihf-objdump -D -b binary -m arm sample.img
--adjust-vma=0x80000
rm *.o *.elf
wc -c sample.img
exit
#endif
void main() {
while (1) {
}
}
rpi0.ld :
ENTRY(_start)
MEMORY
{
ram : ORIGIN = 0x80000, LENGTH = 0x1000000
}
SECTIONS
{
.text : { *(.text) } > ram
.data : { *(.data) } > ram
.bss : { *(.bss) } > ram
}
start.s :
.global _start
.section .text
_start:
ldr sp, =0x80000
bl main
b .
Can also see standalone
U-Boot examples.
back to top