From 9e76b9001c37ab2da2e99c922406b991bd0e53af Mon Sep 17 00:00:00 2001 From: Flavian Kaufmann Date: Wed, 15 May 2024 08:27:12 +0200 Subject: running c program --- prog/link.ld | 67 +++++++++++++++++++++++++++++++----- prog/src/main.c | 6 ++++ prog/src/prog.s | 99 ----------------------------------------------------- prog/src/prog.s.bak | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++ prog/src/startup.s | 42 +++++++++++++++++++++++ 5 files changed, 206 insertions(+), 107 deletions(-) create mode 100644 prog/src/main.c delete mode 100644 prog/src/prog.s create mode 100644 prog/src/prog.s.bak create mode 100644 prog/src/startup.s (limited to 'prog') diff --git a/prog/link.ld b/prog/link.ld index 98658c3..4cc02ec 100644 --- a/prog/link.ld +++ b/prog/link.ld @@ -1,16 +1,67 @@ -OUTPUT_ARCH( "riscv" ) -ENTRY(_start) +/* Linker script for RISC-V */ +/* Define memory regions */ MEMORY { - ROM (rx) : ORIGIN = 0x00010000, LENGTH = 0xF0000 # 0x0001_0000 - 0x000F_FFFF - RAM (rwx) : ORIGIN = 0x00100000, LENGTH = 0xFEFFFFF # 0x0010_0000 - 0xFF0F_FFFF + ROM (rx) : ORIGIN = 0x00010000, LENGTH = 0x400 /* 1024 bytes for ROM */ + RAM (rwx) : ORIGIN = 0x00100000, LENGTH = 0x400 /* 1024 bytes for RAM */ } +ENTRY(_start) + +/* Define sections */ SECTIONS { - .text : { *(.text) } > ROM - .data : { *(.data) } > RAM - .bss : { *(.bss) } > RAM - .stack : { *(.stack) } > RAM + /* Code section */ + .text : + { + KEEP(*(.init)) + KEEP(*(.text)) + KEEP(*(.fini)) + KEEP(*(.rodata)) + _etext = .; + } > ROM + + /* Initialized data section */ + .data : AT(_etext) + { + _sdata = .; + KEEP(*(.data)) + _edata = .; + } > RAM + + /* Uninitialized data section */ + .bss : + { + _sbss = .; + KEEP(*(.bss)) + KEEP(*(COMMON)) + _ebss = .; + } > RAM + + /* Stack section */ + .stack (NOLOAD) : + { + _stack_start = .; + . += 0x10; /* Adjust the size as needed */ + _stack_end = .; + } > RAM + + /* Heap section */ + .heap (NOLOAD) : + { + _heap_start = .; + . += 0x10; /* Adjust the size as needed */ + _heap_end = .; + } > RAM } + +/* Define symbols for memory initialization */ +PROVIDE(_start = 0x00010000); +PROVIDE(_etext = _etext); +PROVIDE(_sdata = _sdata); +PROVIDE(_edata = _edata); +PROVIDE(_sbss = _sbss); +PROVIDE(_ebss = _ebss); +PROVIDE(_sstack = _sstack); +PROVIDE(_estack = _estack); \ No newline at end of file diff --git a/prog/src/main.c b/prog/src/main.c new file mode 100644 index 0000000..1ce067c --- /dev/null +++ b/prog/src/main.c @@ -0,0 +1,6 @@ + + +int main(void) { + while (1) { + } +} diff --git a/prog/src/prog.s b/prog/src/prog.s deleted file mode 100644 index 63026c9..0000000 --- a/prog/src/prog.s +++ /dev/null @@ -1,99 +0,0 @@ -.section .text -.globl _start - -_start: - - - j test_prog - - - -/* - addi t0, zero, 31 -reset_loop: - addi t6, zero, 0 -loop: - addi t6, t6, 1 - beq t6, t0, reset_loop - j loop -*/ - -halt_loop: - j halt_loop - -test_prog: - li t0, 0xFFFFFFFF - li t1, 0x33333333 - li t2, 0x88888888 - li t3, 0x11111111 - - add t4, t1, t2 - sub t4, t1, t2 - slt t4, t3, t0 - slt t4, t0, t3 - sltu t4, t3, t0 - sltu t4, t0, t3 - and t4, zero, zero - and t4, zero, t0 - and t4, t0, t0 - or t4, zero, zero - or t4, zero, t0 - or t4, t0, t0 - xor t4, zero, zero - xor t4, zero, t0 - xor t4, t0, t0 - - beq t0, t0, branch_eq - j branch_eq_nt - -branch_eq_ret: - beq t0, t3, branch_ne - j branch_ne_nt - -branch_ne_ret: - call func - - li t0, 0x00100000 - sw t1, 0(t0) - lw t2, 0(t0) - - j halt_loop - - -branch_eq: - addi t5, zero, 1 - j branch_eq_ret - -branch_eq_nt: - addi t5, zero, 2 - j branch_eq_ret - -branch_ne: - addi t5, zero, 3 - j branch_ne_ret - -branch_ne_nt: - addi t5, zero, 4 - j branch_ne_ret - -func: - addi t5, zero, 5 - ret - - - - - - - - - - -.section .data - - -.section .bss - -.section .stack - .space 0x1000 # Allocate stack space -stack_top: diff --git a/prog/src/prog.s.bak b/prog/src/prog.s.bak new file mode 100644 index 0000000..63026c9 --- /dev/null +++ b/prog/src/prog.s.bak @@ -0,0 +1,99 @@ +.section .text +.globl _start + +_start: + + + j test_prog + + + +/* + addi t0, zero, 31 +reset_loop: + addi t6, zero, 0 +loop: + addi t6, t6, 1 + beq t6, t0, reset_loop + j loop +*/ + +halt_loop: + j halt_loop + +test_prog: + li t0, 0xFFFFFFFF + li t1, 0x33333333 + li t2, 0x88888888 + li t3, 0x11111111 + + add t4, t1, t2 + sub t4, t1, t2 + slt t4, t3, t0 + slt t4, t0, t3 + sltu t4, t3, t0 + sltu t4, t0, t3 + and t4, zero, zero + and t4, zero, t0 + and t4, t0, t0 + or t4, zero, zero + or t4, zero, t0 + or t4, t0, t0 + xor t4, zero, zero + xor t4, zero, t0 + xor t4, t0, t0 + + beq t0, t0, branch_eq + j branch_eq_nt + +branch_eq_ret: + beq t0, t3, branch_ne + j branch_ne_nt + +branch_ne_ret: + call func + + li t0, 0x00100000 + sw t1, 0(t0) + lw t2, 0(t0) + + j halt_loop + + +branch_eq: + addi t5, zero, 1 + j branch_eq_ret + +branch_eq_nt: + addi t5, zero, 2 + j branch_eq_ret + +branch_ne: + addi t5, zero, 3 + j branch_ne_ret + +branch_ne_nt: + addi t5, zero, 4 + j branch_ne_ret + +func: + addi t5, zero, 5 + ret + + + + + + + + + + +.section .data + + +.section .bss + +.section .stack + .space 0x1000 # Allocate stack space +stack_top: diff --git a/prog/src/startup.s b/prog/src/startup.s new file mode 100644 index 0000000..7adfb5e --- /dev/null +++ b/prog/src/startup.s @@ -0,0 +1,42 @@ +.section .init +.globl _start +_start: + /* Set up stack pointer */ + la sp, _stack_end + + /* Initialize .data section (copy from LMA to VMA) */ + la t0, _sdata /* VMA start of .data */ + la t1, _edata /* VMA end of .data */ + la t2, _etext /* LMA start of .data in flash */ +copy_data: + beq t0, t1, clear_bss + lw t3, 0(t2) + sw t3, 0(t0) + addi t0, t0, 4 + addi t2, t2, 4 + j copy_data + +clear_bss: + /* Clear .bss section */ + la t0, _sbss /* VMA start of .bss */ + la t1, _ebss /* VMA end of .bss */ +clear_loop: + beq t0, t1, init_libc + sw x0, 0(t0) + addi t0, t0, 4 + j clear_loop + +init_libc: + /* Call libc initialization functions */ + /* call _init */ + + /* Call main function */ + call main + +finalize_libc: + /* Call libc finalization functions */ + /* call _fini */ + +halt: + /* Infinite loop after main returns */ + j halt -- cgit v1.2.3