# List all of the object files that will be combined into your final binary
# in the OBJ variable.
#
# For example, if you have the following source files
#
#   main.c
#   user.c
#   driver.s
#
# then your OBJ list would be
#
#   main.o user.o driver.o
#
# The compiler will automatically choose the correct source file (.c, .s etc)
# according to the object file (.o) to be created.
OBJ=crt0.o main.o tty_16550.o

TTY=/dev/ttyS0

LDSCRIPT=platform.ld

# Specify the CPU type that you are targeting your build towards.
#
# Supported architectures can usually be found with the --target-help argument
# passed to gcc, but a quick summary is:
#
# 68000, 68010, 68020, 68030, 68040, 68060, cpu32 (includes 68332 and 68360),
# 68302
CPU=68000

# Uncomment either of the following depending on how you have installed gcc on
# your system. m68k-linux-gnu for Linux installations, m68k-eabi-elf if gcc was
# built from scratch e.g. on a Mac by running the build script.
PREFIX=m68k-linux-gnu
# PREFIX=m68k-eabi-elf

# Dont modify below this line (unless you know what youre doing).
CC=$(PREFIX)-gcc
LD=$(PREFIX)-ld
AR=$(PREFIX)-ar
OBJCOPY=$(PREFIX)-objcopy
OBJDUMP=$(PREFIX)-objdump

INCDIR=/usr/lib/gcc/m68k-linux-gnu/11/include
CFLAGS=-m$(CPU) -Wall -static -I$(INCDIR) -I../include -I. -msoft-float -MMD -MP -O -Wno-builtin-declaration-mismatch #-mpcrel
LFLAGS=--script=$(LDSCRIPT)

SRC=$(wildcard *.c)
DEP=$(SRC:%.c=%.d)

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

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

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

bmbinary: $(OBJ)
	$(AR) -cvq libtemp.a $(OBJ)
	$(LD) -o $@ $(LFLAGS) -L. -ltemp

-include $(DEP)

all: bmbinary rom
	make dumps >bmbinary.dumps

crt0.o: crt0.S                  # need this so the default rule isnt used; due to the rm below?
	$(CC) $(CFLAGS) -c -o crt0.o crt0.S
	rm -f crt0.d

main.s: main.c
#	$(CC) $(CFLAGS) -S $<
	$(CC) $(CFLAGS) -Wa,-adhln -g $< >main.s

clean:
	rm -f $(OBJ) $(DEP) main.s libtemp.a
	rm -f bmbinary bmbinary.dumps bmbinary.rom

rom: bmbinary
	$(OBJCOPY) -O binary bmbinary bmbinary.rom

dump:
	$(OBJDUMP) -mm68k:$(CPU) -belf32-m68k -st -j.evt bmbinary
	$(OBJDUMP) -mm68k:$(CPU) -belf32-m68k -dt -j.text bmbinary
	$(OBJDUMP) -mm68k:$(CPU) -belf32-m68k -st -j.rodata -j.data -j.bss -j.heap -j.stack bmbinary

dumps:
	$(OBJDUMP) -mm68k:$(CPU) -belf32-m68k -st -j.evt bmbinary
	$(OBJDUMP) -mm68k:$(CPU) -belf32-m68k -St -j.text bmbinary
	$(OBJDUMP) -mm68k:$(CPU) -belf32-m68k -st -j.rodata -j.data -j.bss -j.heap -j.stack bmbinary

hexdump:
	hexdump -C bmbinary.rom

althexdump:
	od  -v -A x -t x1z bmbinary.rom 

