diff options
author | Flavian Kaufmann <flavian@flaviankaufmann.ch> | 2024-05-20 16:48:55 +0200 |
---|---|---|
committer | Flavian Kaufmann <flavian@flaviankaufmann.ch> | 2024-05-20 16:48:55 +0200 |
commit | e945c80a6ceaef501350de49bf647ae8539d1cbb (patch) | |
tree | 9e698d839e5f6221af523b49be04b809648efeb9 /prog | |
parent | 142510b8325b9ef89bd3e22463f36c3caa2815de (diff) | |
download | riscv_cpu-e945c80a6ceaef501350de49bf647ae8539d1cbb.tar.gz riscv_cpu-e945c80a6ceaef501350de49bf647ae8539d1cbb.zip |
reset synchronizer
Diffstat (limited to 'prog')
-rw-r--r-- | prog/link.ld | 120 | ||||
-rw-r--r-- | prog/src/main.c | 11 | ||||
-rw-r--r-- | prog/src/prog.s.tst (renamed from prog/src/prog.s) | 0 | ||||
-rw-r--r-- | prog/src/startup.c | 39 | ||||
-rw-r--r-- | prog/src/startup.s | 59 |
5 files changed, 110 insertions, 119 deletions
diff --git a/prog/link.ld b/prog/link.ld index 9644d59..ae0988f 100644 --- a/prog/link.ld +++ b/prog/link.ld @@ -1,67 +1,79 @@ -/* Linker script for RISC-V */ - -/* Define memory regions */ MEMORY { - ROM (rx) : ORIGIN = 0x00010000, LENGTH = 0x1000 /* 1024 bytes for ROM */ - RAM (rwx) : ORIGIN = 0x00100000, LENGTH = 0x1000 /* 1024 bytes for RAM */ + ROM (rx) : ORIGIN = 0x00010000, LENGTH = 4K + RAM (rwx) : ORIGIN = 0x00100000, LENGTH = 4K } ENTRY(_start) -/* Define sections */ SECTIONS { - /* Code section */ - .text : - { - KEEP(*(.init)) - KEEP(*(.text)) - KEEP(*(.fini)) - KEEP(*(.rodata)) - _etext = .; - } > ROM + .text : + { + _stext = .; + . = ORIGIN(ROM); + _start = .; /* Entry point address */ + KEEP(*(.text.startup)) /* Ensure startup code is kept */ + *(.text) /* .text sections (code) */ + *(.text.*) + . = ALIGN(4); + _etext = .; /* End of code section, needed for copying .data */ + } > ROM - /* Initialized data section */ - .data : AT(_etext) - { - _sdata = .; - KEEP(*(.data)) - _edata = .; - } > RAM + .rodata : + { + *(.rodata) /* Read-only data */ + *(.rodata.*) + } > ROM - /* Uninitialized data section */ - .bss : - { - _sbss = .; - KEEP(*(.bss)) - KEEP(*(COMMON)) - _ebss = .; - } > RAM + .data : + { + _sidata = LOADADDR(.data); /* Start address of .data section in ROM */ + . = ALIGN(4); + _sdata = .; /* Start of .data section in RAM */ + *(.data) /* .data sections (initialized data) */ + *(.data.*) + *(.sdata) /* .sdata sections (small initialized data) */ + *(.sdata.*) + . = ALIGN(4); + _edata = .; /* End of .data section in RAM */ + } > RAM AT > ROM - /* Stack section */ - .stack (NOLOAD) : - { - _stack_start = .; - . += 0x100; /* Adjust the size as needed */ - _stack_end = .; - } > RAM + .bss : + { + . = ALIGN(4); + _sbss = .; /* Start of .bss section */ + *(.bss) /* .bss sections (zero-initialized data) */ + *(.bss.*) + *(.sbss) /* .sbss sections (small zero-initialized data) */ + *(.sbss.*) + *(COMMON) + . = ALIGN(4); + _ebss = .; /* End of .bss section */ + } > RAM - /* Heap section */ - .heap (NOLOAD) : - { - _heap_start = .; - . += 0x100; /* Adjust the size as needed */ - _heap_end = .; - } > RAM -} + .heap (NOLOAD) : + { + . = ALIGN(4); + _sheap = .; /* Start of heap */ + . = . + 1K; /* Size of heap (adjust as needed) */ + _eheap = .; /* End of heap */ + . = ALIGN(4); + } > 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 + .stack (NOLOAD) : + { + . = ALIGN(4); + _sstack = .; /* Start of stack */ + . = . + 1K; /* Size of stack (adjust as needed) */ + _estack = .; /* End of stack */ + . = ALIGN(4); + } > RAM + + /DISCARD/ : + { + *(.comment) + *(.note) + *(.note.*) + } +} diff --git a/prog/src/main.c b/prog/src/main.c index 082172d..a31196f 100644 --- a/prog/src/main.c +++ b/prog/src/main.c @@ -1,13 +1,12 @@ #include <stdint.h> -//volatile uint32_t *io_in = (volatile uint32_t *)0x00000000; -//volatile uint32_t *io_out = (volatile uint32_t *)0x00000004; +volatile uint32_t *io_in = (volatile uint32_t *)0x00000000; +volatile uint32_t *io_out = (volatile uint32_t *)0x00000004; + int main(void) { while (1) { - for (int i = 0; i < 32; ++i) { - *((volatile uint32_t *)0x00000004) = i; - for (int j = 0; j < 1024 * 128; ++j); - } + if (*io_in) *io_out = 0b00000; + else *io_out = 0b11111; } } diff --git a/prog/src/prog.s b/prog/src/prog.s.tst index ec227a9..ec227a9 100644 --- a/prog/src/prog.s +++ b/prog/src/prog.s.tst diff --git a/prog/src/startup.c b/prog/src/startup.c new file mode 100644 index 0000000..32aa131 --- /dev/null +++ b/prog/src/startup.c @@ -0,0 +1,39 @@ +extern unsigned int _sidata; /* Start address for the .data section in ROM */ +extern unsigned int _sdata; /* Start address for the .data section in RAM */ +extern unsigned int _edata; /* End address for the .data section in RAM */ +extern unsigned int _sbss; /* Start address for the .bss section */ +extern unsigned int _ebss; /* End address for the .bss section */ +extern unsigned int _estack; /* End address for the stack section */ + +void main(void); /* The main function declaration */ +void _start(void) __attribute__((section(".text.startup"), naked)); /* The entry point */ + +void _start(void) +{ + unsigned int *src, *dest; + + /* Copy .data section from ROM to RAM */ + src = &_sidata; /* ROM address of the .data section */ + for (dest = &_sdata; dest < &_edata;) + { + *dest++ = *src++; + } + + //while(1); + + /* Zero initialize .bss section */ + for (dest = &_sbss; dest < &_ebss;) + { + *dest++ = 0; + } + + + /* Initialize stack pointer */ + asm volatile ("la sp, _estack"); + + /* Call the main function */ + main(); + + /* If main returns, loop forever */ + while (1); +} diff --git a/prog/src/startup.s b/prog/src/startup.s deleted file mode 100644 index 963c288..0000000 --- a/prog/src/startup.s +++ /dev/null @@ -1,59 +0,0 @@ -.section .init -.globl _start -_start: - -prog: -/* - la t0, 4 - la t1, 1 - - sw t1, 0(t0) - - la t0, 0x00100000 - li t1, 1 - sw t1, 0(t0) - lw t2, 0(t0) - - - j halt - -*/ - /* 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 |