Add an example program in C

This commit is contained in:
Konstantin Nazarov 2024-12-06 22:40:52 +00:00
parent b8fb51f5e6
commit c9fc4a420c
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
7 changed files with 53 additions and 8 deletions

1
.gitignore vendored
View file

@ -18,3 +18,4 @@ CTestTestfile.cmake
Testing Testing
result result
build build
example/example

2
example/Makefile Normal file
View file

@ -0,0 +1,2 @@
example: example.c
riscv32-none-elf-gcc -fno-builtin -fvisibility=hidden -nostdlib -nostartfiles -march=rv32im -mabi=ilp32 -o example example.c -T linker.ld

9
example/example.c Normal file
View file

@ -0,0 +1,9 @@
static int mem;
int main() {
int a = 42;
int b = 5;
mem = a*b + 3;
return 0;
}

19
example/linker.ld Normal file
View file

@ -0,0 +1,19 @@
ENTRY(_start)
SECTIONS {
. = 0x1000; /* Start address of the program */
.text : {
*(.text) /* Place all .text sections (code) here */
}
.data : {
*(.data) /* Place all .data sections (initialized data) here */
}
.bss : {
*(.bss) /* Place all .bss sections (uninitialized data) here */
}
/DISCARD/ : { *(.note.GNU-stack) } /* Discard stack section */
}

View file

@ -21,16 +21,22 @@
forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
pkgs = import nixpkgs { inherit system; }; pkgs = import nixpkgs { inherit system; };
}); });
riscv-pkgs = import nixpkgs {
localSystem = "x86_64-linux";
crossSystem = {
config = "riscv32-unknown-linux-gnu";
};
};
in in
{ {
packages = forAllSystems( {pkgs }: packages = forAllSystems( {pkgs}:
{ {
lisp = (pkgs.callPackage ./rve.nix {}); lisp = (pkgs.callPackage ./rve.nix {riscv-pkgs=riscv-pkgs;});
default = (pkgs.callPackage ./rve.nix {}); default = (pkgs.callPackage ./rve.nix {riscv-pkgs=riscv-pkgs;});
} }
); );
overlays.default = final: prev: { overlays.default = final: prev: {
lisp = (prev.callPackage ./rve.nix {}); lisp = (prev.callPackage ./rve.nix {riscv-pkgs=riscv-pkgs;});
}; };
}; };
} }

12
rve.nix
View file

@ -1,4 +1,4 @@
{ pkgs, stdenv }: { pkgs, stdenv, riscv-pkgs }:
pkgs.gcc13Stdenv.mkDerivation rec { pkgs.gcc13Stdenv.mkDerivation rec {
pname = "rve"; pname = "rve";
version = "0.1.0"; version = "0.1.0";
@ -13,7 +13,15 @@ pkgs.gcc13Stdenv.mkDerivation rec {
nativeBuildInputs = with pkgs; [ pkg-config cmake ninja ]; nativeBuildInputs = with pkgs; [ pkg-config cmake ninja ];
buildInputs = with pkgs; [ gdb linuxPackages.perf jq lcov ]; buildInputs = with pkgs; [
gdb
linuxPackages.perf
jq
lcov
#riscv-pkgs.buildPackages.gcc
pkgsCross.riscv32-embedded.buildPackages.gcc
pkgsCross.riscv32-embedded.buildPackages.binutils
];
hardeningDisable = [ "all" ]; hardeningDisable = [ "all" ];
cmakeFlags = [ cmakeFlags = [

View file

@ -5,7 +5,7 @@
#include <cstring> #include <cstring>
int main() { int main() {
const size_t MEMORY_SIZE = 1024; const size_t MEMORY_SIZE = 128*1024;
uint8_t memory[MEMORY_SIZE] = {0}; uint8_t memory[MEMORY_SIZE] = {0};
uint32_t program[] = { uint32_t program[] = {