diff --git a/arm/app.ld b/arm/app.ld new file mode 100644 index 0000000..ed1ff7d --- /dev/null +++ b/arm/app.ld @@ -0,0 +1,34 @@ +MEMORY +{ + ICTM (rwx) : ORIGIN = 0x00000800, LENGTH = 12228 +} + +SECTIONS +{ + .text : + { + KEEP(*(.app_init)) + + *(.text*) + *(.rodata*) + } > ICTM + + .bss (NOLOAD) : + { + _bss_begin = .; + *(.bss*) + *(COMMON) + _bss_end = .; + } > ICTM + + .data : + { + *(.data*) + + __exidx_start = .; + *(.exidx*) + __exidx_end = .; + } > ICTM + + _initial_stack_pointer = 16384; +} diff --git a/arm/app_init.cc b/arm/app_init.cc new file mode 100644 index 0000000..9afc20b --- /dev/null +++ b/arm/app_init.cc @@ -0,0 +1,23 @@ +#include + +extern "C" int main(); +extern uint32_t _bss_begin, _bss_end, _initial_stack_pointer; + +__attribute__((section(".app_init"))) +void AppInit() { + *(uint32_t*)(0x40000000) = 0; + asm ("mov sp, %0" + : + : "r" (&_initial_stack_pointer) + : + ); + + // clear .bss + for (uint32_t* ptr = &_bss_begin; ptr < &_bss_end; ptr++) { + *ptr = 0; + } + + main(); + + while(true) {} +} diff --git a/arm/bootloader.cc b/arm/bootloader.cc new file mode 100644 index 0000000..10348a2 --- /dev/null +++ b/arm/bootloader.cc @@ -0,0 +1,63 @@ +#include + +#include "gpio.h" +#include "uart.h" + +namespace { + +uint8_t UartRead() { + uint8_t c; + while (XUartLite_Recv(uart0, &c, 1) < 1) {} + return c; +} + +void UartWrite(uint8_t c) { + XUartLite_Send(uart0, &c, 1); +} + +uint32_t UartRead32() { + uint32_t val = 0; + + // little endian + val |= (UartRead() << 0); + val |= (UartRead() << 8); + val |= (UartRead() << 16); + val |= (UartRead() << 24); + + return val; +} + +} // namespace + +int main() { + gpio0->data = 1; + + InitUarts(); + + while(1) { + uint8_t c = UartRead(); + if (c == 'c') { + uint32_t addr = UartRead32(); + uint32_t bytes = UartRead32(); + + uint8_t* start = reinterpret_cast(addr); + uint8_t* end = reinterpret_cast(addr + bytes); + for (uint8_t* ptr = start; ptr < end; ptr++) { + *ptr = UartRead(); + } + + UartWrite('a'); + UartWrite(bytes); + gpio0->data = 0xf0; + } else if (c == 'j') { + uint32_t addr = UartRead32(); + + gpio0->data = 0x55; + + addr |= 0x0001; + + auto jump = reinterpret_cast(addr); + jump(); + } + } +} diff --git a/arm/bootloader.ld b/arm/bootloader.ld index d13401e..a0fab94 100644 --- a/arm/bootloader.ld +++ b/arm/bootloader.ld @@ -1,6 +1,6 @@ MEMORY { - ICTM (rwx) : ORIGIN = 0x00000000, LENGTH = 16384 + ICTM (rwx) : ORIGIN = 0x00000000, LENGTH = 2048 } SECTIONS @@ -30,5 +30,5 @@ SECTIONS __exidx_end = .; } > ICTM - _initial_stack_pointer = 16384; + _initial_stack_pointer = 2048; } diff --git a/arm/gpio.h b/arm/gpio.h new file mode 100644 index 0000000..d825f25 --- /dev/null +++ b/arm/gpio.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +struct Gpio { + volatile uint32_t data; +}; + +#define gpio0 ((Gpio*) 0x40000000) diff --git a/arm/main.cc b/arm/main.cc index 0bf5354..55d01f5 100644 --- a/arm/main.cc +++ b/arm/main.cc @@ -1,30 +1,12 @@ -#include - -#include "xuartlite.h" - -struct Gpio { - volatile uint32_t data; -}; - -#define gpio0 ((Gpio*) 0x40000000) +#include "gpio.h" namespace { -constexpr uintptr_t kUart0BaseAddress = 0x40001000; -XUartLite uart0; -XUartLite_Config uart0_config = { - .DeviceId = 0, - .RegBaseAddr = kUart0BaseAddress, - .BaudRate = 115200, - .UseParity = false, - .DataBits = 8, -}; - [[maybe_unused]] void sleep(int ms) { for (int i = 0; i < ms; i++) { // sleep for 1 ms - for (int j = 0; j < 50000; j++) { + for (int j = 0; j < 22000; j++) { asm(""); } } @@ -32,24 +14,19 @@ void sleep(int ms) { } // namespace -int main() { - int recv = 0; - gpio0->data = 37; +extern "C" int main() { + uint8_t cnt = 0; - XUartLite_CfgInitialize(&uart0, &uart0_config, uart0_config.RegBaseAddr); + for (int i = 0; i < 256; i++) { + gpio0->data = cnt; + cnt++; - while (true) { - uint8_t c; - int received_bytes = XUartLite_Recv(&uart0, &c, 1); - if (received_bytes < 1) { - continue; - } - recv++; - gpio0->data = recv & 0xff; - XUartLite_Send(&uart0, &c, 1); - if (c == '\r') { - c = '\n'; - XUartLite_Send(&uart0, &c, 1); - } + sleep(125); } -} \ No newline at end of file + + auto* airc = reinterpret_cast(0xe000ed0c); + uint32_t a = *airc; + *airc = a | 0x04; + + while (1) {} +} diff --git a/arm/makefile b/arm/makefile index 072b80e..f7dc69f 100644 --- a/arm/makefile +++ b/arm/makefile @@ -11,7 +11,7 @@ CFLAGS = -march=armv6-m -g -ffunction-sections -fdata-sections -O2 -Werror -Wall 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 \ + -Wl,--gc-sections -O2 \ -Wl,--print-memory-usage sources += hal/lib/common/xil_assert.c @@ -20,21 +20,25 @@ includes += -Ihal/lib/common sources += hal/uart/xuartlite.c hal/uart/xuartlite_stats.c includes += -Ihal/uart -objects = main.o vector_table.o $(sources:.c=.o) +bootloader_objects = uart.o bootloader.o vector_table.o $(sources:.c=.o) +app_objects = app_init.o main.o CFLAGS += $(includes) -all: bootloader.elf +all: bootloader.elf app.bin %.bin: %.elf $(OBJCOPY) -O binary $< $@ -bootloader.elf: $(objects) +bootloader.elf: $(bootloader_objects) + $(LD) -Wl,-Tbootloader.ld $(LDFLAGS) -o $@ $^ + +app.elf: $(app_objects) %.elf: - $(LD) $(LDFLAGS) -o $@ $^ + $(LD) -Wl,-Tapp.ld $(LDFLAGS) -o $@ $^ .PHONY: clean clean: - rm -rf *.elf *.bin $(objects) \ No newline at end of file + rm -rf *.elf *.bin $(app_objects) $(bootloader_objects) diff --git a/arm/prog.py b/arm/prog.py new file mode 100644 index 0000000..b86d6d5 --- /dev/null +++ b/arm/prog.py @@ -0,0 +1,47 @@ +import serial +import struct +import sys + +offset = 0x800 +tty = 'tty' +baud = 115200 +chunksize = 128 + + +def write(s, offset, dat): + for i in range(0, len(dat), chunksize): + chunk = dat[i: i + chunksize] + cmd = struct.pack('