From e4e2e65293250496cbcb08ade0c3d9ae23b4c427 Mon Sep 17 00:00:00 2001 From: Graham Edgecombe Date: Fri, 15 Dec 2017 20:08:55 +0000 Subject: [PATCH] Port example program to C --- .gitignore | 1 + Makefile | 11 +++++++++-- progmem.c | 20 ++++++++++++++++++++ progmem.lds | 21 +++++++++++++++++++++ progmem.s | 18 ------------------ start.s | 23 +++++++++++++++++++++++ 6 files changed, 74 insertions(+), 20 deletions(-) create mode 100644 progmem.c create mode 100644 progmem.lds delete mode 100644 progmem.s create mode 100644 start.s diff --git a/.gitignore b/.gitignore index a4085aa..ead9110 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,6 @@ *.rpt *.stat /pll.sv +/progmem !.git* !.mailmap diff --git a/Makefile b/Makefile index a9fd34c..87845da 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,10 @@ FREQ_PLL = 36 TARGET = riscv64-unknown-elf AS = $(TARGET)-as ASFLAGS = -march=rv32i -mabi=ilp32 +LD = $(TARGET)-ld +LDFLAGS = -Tprogmem.lds -melf32lriscv +CC = $(TARGET)-gcc +CFLAGS = -march=rv32i -mabi=ilp32 -Wall -Wextra -pedantic OBJCOPY = $(TARGET)-objcopy .PHONY: all clean syntax time stat flash @@ -27,13 +31,16 @@ OBJCOPY = $(TARGET)-objcopy all: $(BIN) clean: - $(RM) $(BLIF) $(ASC_SYN) $(ASC) $(BIN) $(PLL) progmem_syn.hex progmem.hex progmem.o + $(RM) $(BLIF) $(ASC_SYN) $(ASC) $(BIN) $(PLL) progmem_syn.hex progmem.hex progmem.o start.o progmem -progmem.hex: progmem.o +progmem.hex: progmem $(OBJCOPY) -O srec $< /dev/stdout \ | srec_cat - -byte-swap 4 -output - -binary \ | xxd -p -c 4 > $@ +progmem: progmem.o start.o progmem.lds + $(LD) $(LDFLAGS) -o $@ progmem.o start.o + progmem_syn.hex: icebram -g 32 256 > $@ diff --git a/progmem.c b/progmem.c new file mode 100644 index 0000000..d280bab --- /dev/null +++ b/progmem.c @@ -0,0 +1,20 @@ +#include + +#define LEDS *((volatile uint32_t *) 0x00010000) +#define UART_BAUD *((volatile uint32_t *) 0x00020000) +#define UART_STATUS *((volatile uint32_t *) 0x00020004) +#define UART_DATA *((volatile int32_t *) 0x00020008) + +int main() { + UART_BAUD = 36000000 / 9600; + + for (;;) { + int32_t c; + do { + c = UART_DATA; + } while (c < 0); + + UART_DATA = c; + LEDS = c; + } +} diff --git a/progmem.lds b/progmem.lds new file mode 100644 index 0000000..89dcf29 --- /dev/null +++ b/progmem.lds @@ -0,0 +1,21 @@ +ENTRY(start) + +MEMORY { + bram (rwx) : ORIGIN = 0x00000000, LENGTH = 0x00002000 +} + +SECTIONS { + .bram : { + start.o(.text); + *(.text); + *(.data); + + . = ALIGN(4); + bss_start = .; + + *(.bss); + + . = ALIGN(4); + bss_end = .; + } > bram +} diff --git a/progmem.s b/progmem.s deleted file mode 100644 index fce1015..0000000 --- a/progmem.s +++ /dev/null @@ -1,18 +0,0 @@ -# set baud rate to 9600 - li t0, 0x00020000 - li t1, 3750 - sw t1, 0(t0) - -# read char from the UART - li t0, 0x00020008 - li t1, 0x00010000 -loop: - lw t2, 0(t0) - bltz t2, loop - -# write to the LEDs - sw t2, 0(t0) - -# echo back to the UART - sw t2, 0(t1) - j loop diff --git a/start.s b/start.s new file mode 100644 index 0000000..3195518 --- /dev/null +++ b/start.s @@ -0,0 +1,23 @@ +.extern bss_start +.extern bss_end + +.global start +start: + la t0, bss_start + la t1, bss_end + + beq t0, t1, clear_bss_done +clear_bss: + sw x0, 0(t0) + addi t0, t0, 4 + bne t0, t1, clear_bss +clear_bss_done: + + la sp, stack_top + call main + j . + +.section bss +.local stack_bottom +.comm stack_bottom, 256, 16 +stack_top: