Port example program to C

This commit is contained in:
Graham Edgecombe 2017-12-15 20:08:55 +00:00
parent d97f8c5e8e
commit e4e2e65293
6 changed files with 74 additions and 20 deletions

1
.gitignore vendored
View file

@ -8,5 +8,6 @@
*.rpt *.rpt
*.stat *.stat
/pll.sv /pll.sv
/progmem
!.git* !.git*
!.mailmap !.mailmap

View file

@ -20,6 +20,10 @@ FREQ_PLL = 36
TARGET = riscv64-unknown-elf TARGET = riscv64-unknown-elf
AS = $(TARGET)-as AS = $(TARGET)-as
ASFLAGS = -march=rv32i -mabi=ilp32 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 OBJCOPY = $(TARGET)-objcopy
.PHONY: all clean syntax time stat flash .PHONY: all clean syntax time stat flash
@ -27,13 +31,16 @@ OBJCOPY = $(TARGET)-objcopy
all: $(BIN) all: $(BIN)
clean: 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 \ $(OBJCOPY) -O srec $< /dev/stdout \
| srec_cat - -byte-swap 4 -output - -binary \ | srec_cat - -byte-swap 4 -output - -binary \
| xxd -p -c 4 > $@ | xxd -p -c 4 > $@
progmem: progmem.o start.o progmem.lds
$(LD) $(LDFLAGS) -o $@ progmem.o start.o
progmem_syn.hex: progmem_syn.hex:
icebram -g 32 256 > $@ icebram -g 32 256 > $@

20
progmem.c Normal file
View file

@ -0,0 +1,20 @@
#include <stdint.h>
#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;
}
}

21
progmem.lds Normal file
View file

@ -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
}

View file

@ -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

23
start.s Normal file
View file

@ -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: