# HPMCU mailbox-echo firmware: bare-metal RV32IMC for the RV1106 SCR1 core.
# Same xPack riscv-none-embed-gcc 10.2.0 + flags as the watchdog firmware.

# Point XPACK at the xpack riscv toolchain bin/ inside a flare-edge SDK checkout.
XPACK ?= $(error set XPACK to the xpack-riscv-none-embed-gcc bin/ directory)
CROSS ?= $(XPACK)/riscv-none-embed-

CC      = $(CROSS)gcc
OBJCOPY = $(CROSS)objcopy
SIZE    = $(CROSS)size

CFLAGS  = -march=rv32imc -mabi=ilp32 -mcmodel=medany -Os -g \
          -ffreestanding -nostdlib -fno-builtin \
          -ffunction-sections -fdata-sections -Wall -Wextra
LDFLAGS = -T link.lds -nostdlib -Wl,--gc-sections -Wl,-Map=mailbox-echo.map

OBJS = start.o main.o

all: hpmcu-mailbox-echo.bin

hpmcu-mailbox-echo.elf: $(OBJS) link.lds
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS)
	$(SIZE) $@

hpmcu-mailbox-echo.bin: hpmcu-mailbox-echo.elf
	$(OBJCOPY) -O binary $< $@
	@ls -la $@

%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

%.o: %.S
	$(CC) $(CFLAGS) -c -o $@ $<

clean:
	rm -f *.o *.elf *.bin *.map

.PHONY: all clean
