From c9fc4a420ccbda8fa01787da751685802556ee85 Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Fri, 6 Dec 2024 22:40:52 +0000 Subject: [PATCH] Add an example program in C --- .gitignore | 3 ++- example/Makefile | 2 ++ example/example.c | 9 +++++++++ example/linker.ld | 19 +++++++++++++++++++ flake.nix | 14 ++++++++++---- rve.nix | 12 ++++++++++-- src/rve.cpp | 2 +- 7 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 example/Makefile create mode 100644 example/example.c create mode 100644 example/linker.ld diff --git a/.gitignore b/.gitignore index 2b050c3..6b7083d 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ CMakeCache.txt CTestTestfile.cmake Testing result -build \ No newline at end of file +build +example/example \ No newline at end of file diff --git a/example/Makefile b/example/Makefile new file mode 100644 index 0000000..57666ed --- /dev/null +++ b/example/Makefile @@ -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 diff --git a/example/example.c b/example/example.c new file mode 100644 index 0000000..460520c --- /dev/null +++ b/example/example.c @@ -0,0 +1,9 @@ +static int mem; + +int main() { + int a = 42; + int b = 5; + mem = a*b + 3; + + return 0; +} diff --git a/example/linker.ld b/example/linker.ld new file mode 100644 index 0000000..a5ea337 --- /dev/null +++ b/example/linker.ld @@ -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 */ +} diff --git a/flake.nix b/flake.nix index 9e06636..6e2312b 100644 --- a/flake.nix +++ b/flake.nix @@ -21,16 +21,22 @@ forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { pkgs = import nixpkgs { inherit system; }; }); + riscv-pkgs = import nixpkgs { + localSystem = "x86_64-linux"; + crossSystem = { + config = "riscv32-unknown-linux-gnu"; + }; + }; in { - packages = forAllSystems( {pkgs }: + packages = forAllSystems( {pkgs}: { - lisp = (pkgs.callPackage ./rve.nix {}); - default = (pkgs.callPackage ./rve.nix {}); + lisp = (pkgs.callPackage ./rve.nix {riscv-pkgs=riscv-pkgs;}); + default = (pkgs.callPackage ./rve.nix {riscv-pkgs=riscv-pkgs;}); } ); overlays.default = final: prev: { - lisp = (prev.callPackage ./rve.nix {}); + lisp = (prev.callPackage ./rve.nix {riscv-pkgs=riscv-pkgs;}); }; }; } diff --git a/rve.nix b/rve.nix index 0608629..6be2130 100644 --- a/rve.nix +++ b/rve.nix @@ -1,4 +1,4 @@ -{ pkgs, stdenv }: +{ pkgs, stdenv, riscv-pkgs }: pkgs.gcc13Stdenv.mkDerivation rec { pname = "rve"; version = "0.1.0"; @@ -13,7 +13,15 @@ pkgs.gcc13Stdenv.mkDerivation rec { 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" ]; cmakeFlags = [ diff --git a/src/rve.cpp b/src/rve.cpp index 92be719..d179de5 100644 --- a/src/rve.cpp +++ b/src/rve.cpp @@ -5,7 +5,7 @@ #include int main() { - const size_t MEMORY_SIZE = 1024; + const size_t MEMORY_SIZE = 128*1024; uint8_t memory[MEMORY_SIZE] = {0}; uint32_t program[] = {