##############################
#    File: Makefile
#  Author: Henrik Gingsjo - Frontgrade Gaisler AB
# Created: 2023-11-22
#
# Makefile for the GR740-MINI examples in this directory. BCC2 and
# MKPROM2 need to be installed to run it.
#
# https://gaisler.com/gr740-mini
# https://gaisler.com/index.php/downloads/compilers
#
#############################

##############################
### Compiler configuration ###
##############################
GCC?=sparc-gaisler-elf-gcc
CFLAGS=-O2 -Wall -Wextra
LDFLAGS=-qbsp=gr740 -mcpu=leon3 -ldrv

##############################
### MKPROM2 parameters #######
##############################

# The GR740 runs at 250 MHz
FREQMHZ ?= 250
UARTBAUD ?= 38400

# There is 256MiB of RAM (SDRAM) on the board
RAMSIZE = 0x10000000
STACK   = 0x0FFFFFF0

# Boot flash memory configuration
MCFG1 ?= 0x00000022
MCFG3 ?= 0x00000000
# 8-bit PROM with 32 waitstates (for both read and write)

# NOTE: The only substantial difference compared to the reset value
# of MCFG1 and MCFG3 is that there are 256 waitstates at reset. So
# this configuration speeds up the boot by up to a factor 8.

# NOTE: The flash has access time 100 ns for random accesses. For
# sequential accesses the access time is much faster at 15 ns. But we
# cannot take advantage of this with the GR740.

# NOTE: 32 waitstates at 250 MHz => 128 ns.

##############################
### Phony targets ############
##############################

all: helloblink.prom

clean:
	rm -f helloblink.prom bdinit.o helloblink.ram

##############################
### Real targets #############
##############################

# helloblink.ram is an ELF file that can be loaded directly to RAM and
# executed with GRMON. But it cannot be loaded to flash since the GR740
# does not contain any built-in bootloader.
helloblink.ram: helloblink.c
	$(GCC) $(CFLAGS) -o $@ $< $(LDFLAGS)

# helloblink.prom is an ELF file whose sections can be loaded to flash
# (the file should not be loaded verbatim). It contains bootloader code
# that executes directly from flash when the GR740 is reset. It also
# contains helloblink.ram in compressed which the bootloader will
# uncompress and load to RAM.
helloblink.prom: helloblink.ram bdinit.o
	mkprom2 \
    -rstaddr 0xc0000000 -sparcleon0 \
    -stack $(STACK) -ramsize $(RAMSIZE) \
    -memcfg1 $(MCFG1) -memcfg3 $(MCFG3) \
    -freq $(FREQMHZ) \
    -uart 0xFF900000 -baud $(UARTBAUD) \
    -bdinit $< -o $@

# bdinit.o contains low-level initialization routines called by MKPROM2
# at various stages of the boot. The output file _MUST_ be named
# "bdinit.o" because the MKPROM2 utility is hard-coded to look for a
# file with this exact name.
bdinit.o: bdinit.c
	$(GCC) $(CFLAGS) -c $<
