AdaCore / startup-gen

A startup code generator for embedded projects
28 stars 9 forks source link

Startup-gen seems to fail with more than two RAM regions #36

Closed Irvise closed 1 year ago

Irvise commented 1 year ago

I have a RISC-V softcore that has the following memory map

        <memoryRegions>
            <memoryRegion>
                <name>ROM</name>
                <baseAddress>0x00000000</baseAddress>
                <size>0x00020000</size>
            </memoryRegion>
            <memoryRegion>
                <name>SRAM</name>
                <baseAddress>0x01000000</baseAddress>
                <size>0x00002000</size>
            </memoryRegion>
            <memoryRegion>
                <name>MAIN_RAM</name>
                <baseAddress>0x40000000</baseAddress>
                <size>0x02000000</size>
            </memoryRegion>
            <memoryRegion>
                <name>CSR</name>
                <baseAddress>0x82000000</baseAddress>
                <size>0x00010000</size>
            </memoryRegion>
        </memoryRegions>

It has one ROM and three RAM regions (SRAM, CSR, MAIN_RAM).

The following GPR project contains such definitions

   package Device_Configuration is
     for CPU_Name use "riscv32";
     for Float_Handling use "soft";

     for Number_Of_Interrupts use "1";

     --  for Memories use ("MAIN_RAM", "SRAM", "CSR", "ROM");

     for Memories use ("MAIN_RAM", "CSR", "ROM");
     --  Specify from which memory bank the program will load
     for Boot_Memory use "ROM";
     --  Main stack memory
     for Main_Stack_Memory use "MAIN_RAM";

     --  Specification of the RAM
     for Mem_Kind ("MAIN_RAM") use "ram";
     for Address ("MAIN_RAM") use "0x40000000";
     for Size ("MAIN_RAM") use "0x02000000";

     --  Specification of the SRAM
     --  for Mem_Kind ("SRAM") use "ram";
     --  for Address ("SRAM") use "0x01000000";
     --  for Size ("SRAM") use "0x00002000";

     --  Specification of the CSR 
     for Mem_Kind ("CSR") use "ram";
     for Address ("CSR") use "0x82000000";
     for Size ("CSR") use "0x00001000";

     --  Specification of the ROM 
     for Mem_Kind ("ROM") use "rom";
     for Address ("ROM") use "0x00000000";
     for Size ("ROM") use "0x00020000";
   end Device_Configuration;

However, I had to take SRAM out as can be seen. If I try to compile the project with all the regions defined I get the following error:

alr build
warn: Generating possibly incomplete environment because of missing dependencies
ⓘ gnat_riscv_elf*: no build needed.
ⓘ Building led_blink/led_blink.gpr...
Compile
   [ASM_CPP]      crt0.S
Bind
   [gprbind]      led_blink.bexch
   [Ada]          led_blink.ali
Link
   [archive]      libled_blink.a
   [index]        libled_blink.a
   [link]         led_blink.adb
/home/fernando/.config/alire/cache/dependencies/gnat_riscv64_elf_12.2.1_fc4a4d08/bin/../lib/gcc/riscv64-elf/12.2.0/../../../../riscv64-elf/bin/ld:/home/fernando/Documents/FOSDEM/2023/ULX3S/NeoRV32-Ada/led_blink//src/link.ld:195: nonconstant expression for fill value
collect2: error: ld returned 1 exit status
gprbuild: link of led_blink.adb failed
gprbuild: failed command was: /home/fernando/.config/alire/cache/dependencies/gnat_riscv64_elf_12.2.1_fc4a4d08/bin/riscv64-elf-gcc led_blink.o b__led_blink.o /home/fernando/Documents/FOSDEM/2023/ULX3S/NeoRV32-Ada/led_blink/obj/development/led_exit.o libled_blink.a /home/fernando/Documents/FOSDEM/2023/ULX3S/NeoRV32-Ada/neorv32_litex_hal/lib/libNeorv32_Litex_Hal.a /home/fernando/Documents/FOSDEM/2023/ULX3S/NeoRV32-Ada/led_blink/alire/cache/dependencies/hal_0.3.0_095ae514/lib/libhal.a -T /home/fernando/Documents/FOSDEM/2023/ULX3S/NeoRV32-Ada/led_blink//src/link.ld -Wl,--print-memory-usage -Wl,--gc-sections -L/home/fernando/Documents/FOSDEM/2023/ULX3S/NeoRV32-Ada/led_blink/obj/development/ -L/home/fernando/Documents/FOSDEM/2023/ULX3S/NeoRV32-Ada/led_blink/obj/development/ -L/home/fernando/Documents/FOSDEM/2023/ULX3S/NeoRV32-Ada/neorv32_litex_hal/lib/ -L/home/fernando/Documents/FOSDEM/2023/ULX3S/NeoRV32-Ada/led_blink/alire/cache/dependencies/hal_0.3.0_095ae514/lib/ -L/home/fernando/.config/alire/cache/dependencies/gnat_riscv64_elf_12.2.1_fc4a4d08/riscv64-elf/lib/gnat/light-rv32imac/adalib/ -static-libgcc /home/fernando/.config/alire/cache/dependencies/gnat_riscv64_elf_12.2.1_fc4a4d08/riscv64-elf/lib/gnat/light-rv32imac/adalib/libgnat.a -Wl,-L/home/fernando/.config/alire/cache/dependencies/gnat_riscv64_elf_12.2.1_fc4a4d08/riscv64-elf/lib/gnat/light-rv32imac//adalib -nostartfiles -nolibc -L/home/fernando/.config/alire/cache/dependencies/gnat_riscv64_elf_12.2.1_fc4a4d08/riscv64-elf/lib/gnat/light-rv32imac//ld_user -march=rv32imac -mabi=ilp32 -o /home/fernando/Documents/FOSDEM/2023/ULX3S/NeoRV32-Ada/led_blink/bin//led_blink
error: Command ["gprbuild", "-s", "-j0", "-p", "-P", "/home/fernando/Documents/FOSDEM/2023/ULX3S/NeoRV32-Ada/led_blink/led_blink.gpr"] exited with code 4
error: Compilation failed.

The thing is, this works if I leave SRAM and take out CSR. The position of the definitions does not seem to matter.

This leads me to believe that startup-gen can only deal with a maximum of 2 RAM regions. Could you look into this?

I am open to testing and feedback. Thank you for your time, Fer

EDIT: I took a look at the linker script. I did not see anything different between the different generated regions, which is strange... However, I am no linker-script expert.

Fabien-Chouteau commented 1 year ago

I narrowed the problem down to this template:

@@MACRO(CREATE_RAM_SECTIONS)@@
@_BOOT_MEM_@
@@END_MACRO@@
@@IF@@ @_SIZE:RAM_REGION_@ > 0
@@TABLE@@
@_CREATE_RAM_SECTIONS()_@ @_RAM_REGION_@
@@END_TABLE@@
@@END_IF@@

which produces:

ROM CSR
 SRAM

Instead of:

ROM CSR
ROM SRAM

@_BOOT_MEM_@ in empty in the second macro expansion. I don't know if it's a bug in startup-gen or template-parser.

In the meantime @Irvise you can just change the line 195 from

  } >  SRAM AT> 

to

  } >  SRAM AT> ROM
Fabien-Chouteau commented 1 year ago

@Irvise This should fix the problem -> https://github.com/AdaCore/startup-gen/pull/37

Irvise commented 1 year ago

Thank you for fixing it!