diff --git a/arm/bootloader.ld b/arm/bootloader.ld new file mode 100644 index 0000000..e84d6fc --- /dev/null +++ b/arm/bootloader.ld @@ -0,0 +1,32 @@ +MEMORY +{ + ICTM (rwx) : ORIGIN = 0x00000000, LENGTH = 16384 +} + +SECTIONS +{ + .text : + { + KEEP(vector_table.o(.vector_table)) + + *(.text*) + *(.rodata*) + } > ICTM + + .bss (NOLOAD) : + { + _bss_begin = .; + *(.bss*) + *(COMMON) + _bss_end = .; + } > ICTM + + .data : + { + *(.data*) + + __exidx_start = .; + *(.exidx*) + __exidx_end = .; + } > ICTM +} \ No newline at end of file diff --git a/arm/main.cc b/arm/main.cc new file mode 100644 index 0000000..75320d3 --- /dev/null +++ b/arm/main.cc @@ -0,0 +1,13 @@ +#include + +struct Gpio { + volatile uint32_t data; +}; + +#define gpio0 ((Gpio*) 0x40000000) + +int main() { + gpio0->data = 42; + + while (true) {} +} diff --git a/arm/makefile b/arm/makefile new file mode 100644 index 0000000..eb944cd --- /dev/null +++ b/arm/makefile @@ -0,0 +1,25 @@ +CC = arm-none-eabi-gcc +LD = arm-none-eabi-gcc +CXX = arm-none-eabi-g++ +OBJCOPY = arm-none-eabi-objcopy + +linker_script = bootloader.ld + +CFLAGS = -march=armv6-m -g -ffunction-sections -fdata-sections -O2 +CXXFLAGS = $(CFLAGS) -std=c++20 -fno-exceptions +LDFLAGS = -march=armv6-m -g --specs=nano.specs --specs=nosys.specs -Wl,--gc-sections -Wl,-T$(linker_script) -O2 + +all: bootloader.bin + +%.bin: %.elf + $(OBJCOPY) -O binary $< $@ + +bootloader.elf: main.o vector_table.o + +%.elf: + $(LD) $(LDFLAGS) -o $@ $^ + +.PHONY: clean + +clean: + rm -rf *.elf *.bin *.o \ No newline at end of file diff --git a/arm/vector_table.cc b/arm/vector_table.cc new file mode 100644 index 0000000..c2f54ad --- /dev/null +++ b/arm/vector_table.cc @@ -0,0 +1,30 @@ +#include + +extern "C" uint32_t _bss_begin, _bss_end; +extern "C" int main(); + +namespace { + +enum VectorTableEntry { + StackPointer = 0, + Reset = 1, +}; + +void ResetHandler() { + // clear .bss + for (uint32_t* ptr = &_bss_begin; ptr < &_bss_end; ptr++) { + *ptr = 0; + } + + main(); + + while(true) {} +} + +} // namespace + +__attribute__((section(".vector_table"))) +uint32_t vector_table[16] = { + [StackPointer] = 0x00010000, + [Reset] = reinterpret_cast(ResetHandler), +}; \ No newline at end of file