Skip to content

Instantly share code, notes, and snippets.

@cleverca22
Last active November 24, 2019 11:32
Show Gist options
  • Save cleverca22/c9e89ceaadba96f1969bc8eeab8ba532 to your computer and use it in GitHub Desktop.
Save cleverca22/c9e89ceaadba96f1969bc8eeab8ba532 to your computer and use it in GitHub Desktop.
let
pkgs = import <nixpkgs> {};
helloworld = { linker }: pkgs.pkgsCross.vc4.runCommandCC "helloworld-${linker}" {
src = ./.;
allowSubstitutes = false;
} ''
unpackPhase
cd $sourceRoot
vc4-elf-gcc helloworld.c -O2 -o hello.elf -T ${pkgs.pkgsCross.vc4.stdenv.cc.libc}/vc4-elf/lib/vc4-${linker}.ld
vc4-elf-objcopy -O binary hello.elf hello.bin
vc4-elf-objdump -d hello.elf
mkdir $out
cp -v hello.{elf,bin} $out/
'';
testit = pkgs.runCommandNoCC "testit" {
buildInputs = with pkgs; [ resim strace socat ];
blob = helloworld { linker = "sim"; };
} ''
mkdir $out
cd $out
socat create:uart.txt tcp-listen:12365 &
ulimit -c unlimited
ls -ld $blob
vc4emul -verbose -log $blob/hello.bin 0 0
'';
testit2 = pkgs.runCommandNoCC "testit2" {
buildInputs = with pkgs; [ resim strace socat ];
succeedOnFailure = true;
} ''
mkdir $out
cd $out
socat create:uart.txt tcp-listen:12365 &
ulimit -c unlimited
vc4emul -verbose -log ${/home/clever/apps/rpi/rpi-eeprom/firmware/beta/main-body.bin} 0 0x200
'';
in {
inherit helloworld testit testit2;
}
#include <stdio.h>
#include <stdint.h>
#include "vc4-regs.h"
static inline uint32_t cpuid() {
uint32_t res;
asm("version %0"
: "=r"(res));
return res;
}
int main (int argc, char* argv[])
{
MMIO_WRITE (AUX_MU_BAUD_REG, SYSTEM_CLOCK / (TARGET_BAUD_RATE * 8) - 1);
uint32_t id = cpuid();
printf("Hello world! %lx\n", id);
return 0;
}
#ifndef VC4_REGS_H
#define VC4_REGS_H 1
#define GPFSEL1 0x200004
#define GPSET0 0x20001C
#define GPCLR0 0x200028
#define GPPUD 0x200094
#define GPPUDCLK0 0x200098
#define AUX_ENABLES 0x215004
#define AUX_MU_IO_REG 0x215040
#define AUX_MU_IER_REG 0x215044
#define AUX_MU_IIR_REG 0x215048
#define AUX_MU_LCR_REG 0x21504C
#define AUX_MU_MCR_REG 0x215050
#define AUX_MU_LSR_REG 0x215054
#define AUX_MU_MSR_REG 0x215058
#define AUX_MU_SCRATCH 0x21505C
#define AUX_MU_CNTL_REG 0x215060
#define AUX_MU_STAT_REG 0x215064
#define AUX_MU_BAUD_REG 0x215068
#define MMIO_BASE 0x7e000000
#define MMIO_READ(X) (*(volatile uintptr_t *) (MMIO_BASE + (X)))
#define MMIO_WRITE(X,V) (*(volatile uintptr_t *) (MMIO_BASE + (X))) = (V)
#define SYSTEM_CLOCK 19200000
//#define SYSTEM_CLOCK 250000000
#define TARGET_BAUD_RATE 115200
#define ST_CLO 0x3004
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment