From e945c80a6ceaef501350de49bf647ae8539d1cbb Mon Sep 17 00:00:00 2001 From: Flavian Kaufmann Date: Mon, 20 May 2024 16:48:55 +0200 Subject: reset synchronizer --- prog/link.ld | 120 +++++++++++++++++++++++++++++----------------------- prog/src/main.c | 11 +++-- prog/src/prog.s | 78 ---------------------------------- prog/src/prog.s.tst | 78 ++++++++++++++++++++++++++++++++++ prog/src/startup.c | 39 +++++++++++++++++ prog/src/startup.s | 59 -------------------------- 6 files changed, 188 insertions(+), 197 deletions(-) delete mode 100644 prog/src/prog.s create mode 100644 prog/src/prog.s.tst create mode 100644 prog/src/startup.c delete mode 100644 prog/src/startup.s (limited to 'prog') 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 -//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 deleted file mode 100644 index ec227a9..0000000 --- a/prog/src/prog.s +++ /dev/null @@ -1,78 +0,0 @@ -.section .text -.globl test_prog - - -/* - addi t0, zero, 31 -reset_loop: - addi t6, zero, 0 -loop: - addi t6, t6, 1 - beq t6, t0, reset_loop - j 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 -end: - ret - -branch_eq_ret: - beq t0, t3, branch_ne - j branch_ne_nt - -branch_ne_ret: - addi sp, sp, -16 # Adjust stack pointer to make space for ra and s0 - sw ra, 0(sp) # Save ra to the stack - call func - lw ra, 0(sp) # Load ra from the stack - addi sp, sp, 16 # Restore stack pointer - - li t0, 0x00100000 - sw t1, 0(t0) - lw t2, 0(t0) - - j end - - -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 diff --git a/prog/src/prog.s.tst b/prog/src/prog.s.tst new file mode 100644 index 0000000..ec227a9 --- /dev/null +++ b/prog/src/prog.s.tst @@ -0,0 +1,78 @@ +.section .text +.globl test_prog + + +/* + addi t0, zero, 31 +reset_loop: + addi t6, zero, 0 +loop: + addi t6, t6, 1 + beq t6, t0, reset_loop + j 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 +end: + ret + +branch_eq_ret: + beq t0, t3, branch_ne + j branch_ne_nt + +branch_ne_ret: + addi sp, sp, -16 # Adjust stack pointer to make space for ra and s0 + sw ra, 0(sp) # Save ra to the stack + call func + lw ra, 0(sp) # Load ra from the stack + addi sp, sp, 16 # Restore stack pointer + + li t0, 0x00100000 + sw t1, 0(t0) + lw t2, 0(t0) + + j end + + +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 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 -- cgit v1.2.3