top_builddir=../../../..
include $(top_builddir)/Makefile.conf

GENSRC = bios_symbols.c
SFILES = bios.S
bios_S_START = 0xe000
ALL_CPPFLAGS += -DBIOS_START=$(bios_S_START)

all: lib

include $(REALTOPDIR)/src/Makefile.common

# to support parallel build, we need a separate .o and .elf for every gen target
bios1.o: $(srcdir)/bios.S $(srcdir)/Makefile $(top_builddir)/src/include/version.hh
	$(CPP) $(ALL_CPPFLAGS) -x assembler-with-cpp $< | $(XAS) $(XASFLAGS) -o $@

.INTERMEDIATE: bios1.o

bios_symbols.c: bios1.o
	nm -g -n $< | gawk '\
		BEGIN {\
		  COUNT=0;\
		  print "// Warning: autogenerated";\
	          print "";\
	          print "#include \"bios_sym.h\"";\
	          print "";\
		  print "struct bios_symbol_entry bios_symbol[] = {";\
		}\
		{\
		  HEXSTR = sprintf("0x%s", $$1);\
		  ADDR = strtonum(HEXSTR) + $(bios_S_START);\
		  if (ADDR <= 0xffff) {\
		    COUNT++;\
		    printf "  { 0x%04x, \"%s\" },\n", ADDR, $$3;\
		  }\
		}\
		END {\
		  print "};";\
		  print "";\
	          print "int bios_symbol_num = " COUNT ";";\
		}\
		' > $@ || (rm -f $@ ; false)

clean::
	rm -f *.o *.d bios_symbols.c *.map
