Skip to content

Commit cd42774

Browse files
committed
Docs: correct C startup to set gp register and mention A, M and Zicsr extensions
The extension list includes even options for GNU toolchain to enable them. The gp (global pointer) register setting is mandatory to access C global variables consistent way from C code if it is not position independent. And position independent can complicate compilation even more. Signed-off-by: Pavel Pisa <[email protected]>
1 parent 2b21b3a commit cd42774

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

docs/user/reference/external_toolchains.md

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,62 @@ Or for RV64:
3737
riscv64-unknown-elf-gcc -march=rv64i -mabi=lp64 -nostdlib -nostartfiles -o program.elf program.s
3838
```
3939

40+
The above choice is for basic RISC-V integer ISA with 32 registers.
41+
42+
QtRvSim supports even following extensions:
43+
44+
- M (`-march=rv32im`/`-march=rv64im`) - hardware multiply and divide instructions
45+
- A (with M `-march=rv32ima`/`-march=rv64ima`) - atomic operations
46+
- Zicsr (with A and M `-march=rv32ima_zicsr`/`-march=rv64ima_zicsr`) support for control registers
47+
48+
The A, M and XLEN should match setting in the `Core ISA and Hazards` setup dialog tab.
49+
4050
## Compiling C Programs
4151

4252
For C programs, you need a minimal startup file and appropriate compiler flags.
4353

4454
**Startup code (`crt0.s`):**
4555
```asm
56+
/* minimal replacement of crt0.o which is else provided by C library */
57+
58+
.globl main
4659
.globl _start
60+
.globl _heap_stack_start
61+
.globl _heap_stack_end
62+
4763
.text
4864
4965
_start:
50-
la sp, _stack_top # Initialize stack pointer
51-
call main # Call main function
52-
66+
.option push
67+
.option norelax
68+
/* set a global pointer to allow access to C global variables */
69+
la gp, __global_pointer$
70+
/* it has to be done without "relax", because else it is
71+
* optimized to a gp register relative operation by linker
72+
*/
73+
.option pop
74+
la sp, _heap_stack_end
75+
addi a0, zero, 0
76+
addi a1, zero, 0
77+
jal main
5378
_exit:
54-
ebreak # Stop execution
55-
j _exit
56-
57-
.section .bss
58-
.align 4
59-
_stack_bottom:
60-
.space 4096 # 4KB stack
61-
_stack_top:
79+
addi a0, zero, 0
80+
addi a7, zero, 93 /* SYS_exit */
81+
ecall
82+
/* catch case when syscalls are disabled */
83+
ebreak
84+
j _exit
85+
86+
.bss
87+
88+
/* the area which can be used for a heap from the bootom
89+
* and stack from the top
90+
*/
91+
_heap_stack_start:
92+
.skip 16384
93+
_heap_stack_end:
94+
95+
.end
6296
```
6397

6498
**Example C program (`hello.c`):**

0 commit comments

Comments
 (0)