Skip to content

Commit 877e1e9

Browse files
committed
Merge branch 'rejunity-configure-machine-with-directory' into main.
2 parents 772cac2 + e3b7e67 commit 877e1e9

File tree

6 files changed

+59
-5
lines changed

6 files changed

+59
-5
lines changed

README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This is a bare metal 64-bit RISC-V assembly program outputing `Hello.`. It is
44
compiled with the riscv-gnu-toolchain and can be run with the QEMU `sifive_u`
5-
machine.
5+
and `sifive_e` machines.
66

77
I searched for such a program on the Internet but the only examples I found
88
were either bare metal C, or assembly but relying on an OS. Eventually I took
@@ -29,11 +29,12 @@ Assuming the toolchain is in the `$PATH`, running the following produces our
2929
`hello` program.
3030

3131
```
32-
$ riscv64-unknown-elf-gcc -march=rv64g -mabi=lp64 -static -mcmodel=medany \
33-
-fvisibility=hidden -nostdlib -nostartfiles -Thello.ld hello.s -o hello
32+
$ riscv64-unknown-elf-gcc -march=rv64g -mabi=lp64 -static -mcmodel=medany \
33+
-fvisibility=hidden -nostdlib -nostartfiles -Tsifive_u/hello.ld -Isifive_u \
34+
hello.s -o hello
3435
```
3536

36-
The result is a 64-bit RISC-V binary.
37+
The result is a 64-bit RISC-V binary compatible with QEMU `sifive_u` machine.
3738

3839
```
3940
$ file hello
@@ -55,6 +56,33 @@ Note: the program enters an infinite loop after producing the `Hello.` text.
5556
Type `ctrl-a x` to stop QEMU.
5657

5758

59+
## Sifive_e machine
60+
61+
This program can be compiled for more resticted machines like `sifive_e`
62+
that support 32-bit RISC-V, have small amount of RAM and require executable
63+
code to be placed in ROM with different start address.
64+
65+
Assuming the toolchain is in the `$PATH`, running the following produces our
66+
`hello` program, but now ready for `sifive_e`.
67+
68+
```
69+
$ riscv64-unknown-elf-gcc -march=rv32g -mabi=ilp32 -static -mcmodel=medany \
70+
-fvisibility=hidden -nostdlib -nostartfiles -Tsifive_e/hello.ld -Isifive_e \
71+
hello.s -o hello
72+
```
73+
74+
Run it with:
75+
76+
```
77+
$ qemu-system-riscv32 -nographic -machine sifive_e -bios none -kernel hello
78+
Hello.
79+
QEMU: Terminated
80+
```
81+
82+
Note: the program enters an infinite loop after producing the `Hello.` text.
83+
Type `ctrl-a x` to stop QEMU.
84+
85+
5886
## Assembly
5987

6088
To disassemble the program:

hello.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.align 2
2-
.equ UART_BASE, 0x10010000
2+
.include "cfg.inc"
33
.equ UART_REG_TXFIFO, 0
44

55
.section .text

sifive_e/cfg.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.equ UART_BASE, 0x10013000

sifive_e/hello.ld

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
OUTPUT_ARCH( "riscv" )
2+
OUTPUT_FORMAT("elf32-littleriscv")
3+
ENTRY( _start )
4+
SECTIONS
5+
{
6+
/* text: test code section */
7+
. = 0x20400000;
8+
.text : { *(.text) }
9+
/* gnu_build_id: readonly build identifier */
10+
.gnu_build_id : { *(.note.gnu.build-id) }
11+
/* rodata: readonly data segment */
12+
.rodata : { *(.rodata) }
13+
14+
/* data: Initialized data segment */
15+
. = 0x80000000;
16+
.data : { *(.data) }
17+
.sdata : { *(.sdata) }
18+
.debug : { *(.debug) }
19+
. += 0x1000;
20+
stack_top = .;
21+
22+
/* End of uninitalized data segement */
23+
_end = .;
24+
}

sifive_u/cfg.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.equ UART_BASE, 0x10010000
File renamed without changes.

0 commit comments

Comments
 (0)