RAM_LIMIT=16384
NET_TYPE=bonobo

CFILES=../nekot1/alloc64.c ../nekot1/bonobo.c ../nekot1/breakkey.c ../nekot1/config.c ../nekot1/console.c ../nekot1/irq.c ../nekot1/kern.c ../nekot1/keyboard.c ../nekot1/main.c ../nekot1/network.c ../nekot1/prelude.c ../nekot1/real.c ../nekot1/score.c ../nekot1/vdg.c ../nekot1/wall.c ../nekot1/wiznet.c

GAMES=blue.game clock.game forth.game green.game lib8.game life.game red.game spacewar.game

DFLAGS=-DRAM_LIMIT=16384 -DRAM_LIMIT_16384=1 -DNET_TYPE=bonobo -DNET_TYPE_bonobo=1

KERNEL_PRODUCTS = _nekot1.decb _nekot1_sym.o _game.script

all: ${KERNEL_PRODUCTS} ${GAMES}
	echo ALL OKAY

.SUFFIXES:

GCC = ../../bin/gcc6809 -std=gnu99 
GCFLAGS = -Os -I.. -f'omit-frame-pointer' -Wall $(DFLAGS)

LWASM = ../../bin/lwasm.orig --obj  \
      --pragma=undefextern --pragma=cescapes --pragma=importundefexport

LWLINK = ../../bin/lwlink --format=decb  \
      -L'../../lib/gcc/m6809-unknown/4.6.4/'

_nekot1.c: $(CFILES)
	for x in $(CFILES); do \
            echo "#line 1 \"nekot1/$$(basename $$x)\""; \
            cat $$x; \
        done > _nekot1.c

_nekot1.s: _nekot1.c
	$(GCC) -f'whole-program' -S $(GCFLAGS) $<

_nekot1.o: _nekot1.s
	$(LWASM) $< -o'$@' --list='$@.list' --map='$@.map' --symbol-dump='$@.sym'

_nekot1.decb: _nekot1.o
	$(LWLINK) $< -o'$@' --map='$@.map' --script=../nekot1/kernel.script --entry=entry -lgcc
	grep '^Section:' $@.map
	sha256sum < $@ | dd bs=1 count=16 | ( cat ; echo '' ) > $@.hash
	cat $@.hash
	python3 -c "import sys; sys.stdout.buffer.write(bytes([0, 0, 8, 0x01, 0x18]))" > _hash.decb
	python3 -c "import sys; x=0x$$(cat $@.hash); sys.stdout.buffer.write(bytes([(255 & (x>>(8*(7-i)))) for i in range(8)]))" >> _hash.decb
	cat _hash.decb $@  > $@.new
	mv -fv $@.new $@
	cp -fv $@ nekot1.$$(cat $@.hash).decb
	echo OKAY $@

_nekot1_sym.s : _nekot1.decb
	awk ' \
            BEGIN {print "  .area .absolute"} \
            /^Symbol: _([gx][A-Z]|mem|str)/ { \
                printf "  .globl  %s\n%-20s EQU  $$%s\n", $$2, $$2, $$5} \
        ' < _nekot1.decb.map > '$@'

_nekot1_sym.o : _nekot1_sym.s
	$(LWASM) -o'$@' $< --list='$@.list' --map='$@.map' --symbol-dump='$@.sym'

_game.script : _nekot1.decb
	sh ../nekot1/n1-create-game-script.sh < '$<.map' > '_game.script'

install-test98: _nekot1.decb
	cp -vf  _nekot1.decb  ../../build-frobio/pizga-base/Internal/LEMMINGS/test98.lem

################
blue.game: ../games/blue/game.c
	python3 ../nekot1/n1preprocess.py $< blue.c
	$(GCC) -fwhole-program -S $(GCFLAGS) -I../games/blue -I.. blue.c
	cat _nekot1_sym.s >> blue.s
	$(LWASM) -oblue.o blue.s --list=blue.o.list --map=blue.o.map --symbol-dump=blue.o.sym
	$(LWLINK) --decb --output=blue.game blue.o -lgcc --entry=__n1pre_entry --script=_game.script --map=blue.map
	grep "^Section:" blue.map
	cp -vf blue.game /tmp/blue.game
	cp -vf blue.game /tmp/blue.$$(cat _nekot1.decb.hash).game
	echo OKAY $@

clock.game: ../games/clock/game.c
	python3 ../nekot1/n1preprocess.py $< clock.c
	$(GCC) -fwhole-program -S $(GCFLAGS) -I../games/clock -I.. clock.c
	cat _nekot1_sym.s >> clock.s
	$(LWASM) -oclock.o clock.s --list=clock.o.list --map=clock.o.map --symbol-dump=clock.o.sym
	$(LWLINK) --decb --output=clock.game clock.o -lgcc --entry=__n1pre_entry --script=_game.script --map=clock.map
	grep "^Section:" clock.map
	cp -vf clock.game /tmp/clock.game
	cp -vf clock.game /tmp/clock.$$(cat _nekot1.decb.hash).game
	echo OKAY $@

forth.game: ../games/forth/game.c
	python3 ../nekot1/n1preprocess.py $< forth.c
	$(GCC) -fwhole-program -S $(GCFLAGS) -I../games/forth -I.. forth.c
	cat _nekot1_sym.s >> forth.s
	$(LWASM) -oforth.o forth.s --list=forth.o.list --map=forth.o.map --symbol-dump=forth.o.sym
	$(LWLINK) --decb --output=forth.game forth.o -lgcc --entry=__n1pre_entry --script=_game.script --map=forth.map
	grep "^Section:" forth.map
	cp -vf forth.game /tmp/forth.game
	cp -vf forth.game /tmp/forth.$$(cat _nekot1.decb.hash).game
	echo OKAY $@

green.game: ../games/green/game.c
	python3 ../nekot1/n1preprocess.py $< green.c
	$(GCC) -fwhole-program -S $(GCFLAGS) -I../games/green -I.. green.c
	cat _nekot1_sym.s >> green.s
	$(LWASM) -ogreen.o green.s --list=green.o.list --map=green.o.map --symbol-dump=green.o.sym
	$(LWLINK) --decb --output=green.game green.o -lgcc --entry=__n1pre_entry --script=_game.script --map=green.map
	grep "^Section:" green.map
	cp -vf green.game /tmp/green.game
	cp -vf green.game /tmp/green.$$(cat _nekot1.decb.hash).game
	echo OKAY $@

lib8.game: ../games/lib8/game.c
	python3 ../nekot1/n1preprocess.py $< lib8.c
	$(GCC) -fwhole-program -S $(GCFLAGS) -I../games/lib8 -I.. lib8.c
	cat _nekot1_sym.s >> lib8.s
	$(LWASM) -olib8.o lib8.s --list=lib8.o.list --map=lib8.o.map --symbol-dump=lib8.o.sym
	$(LWLINK) --decb --output=lib8.game lib8.o -lgcc --entry=__n1pre_entry --script=_game.script --map=lib8.map
	grep "^Section:" lib8.map
	cp -vf lib8.game /tmp/lib8.game
	cp -vf lib8.game /tmp/lib8.$$(cat _nekot1.decb.hash).game
	echo OKAY $@

life.game: ../games/life/game.c
	python3 ../nekot1/n1preprocess.py $< life.c
	$(GCC) -fwhole-program -S $(GCFLAGS) -I../games/life -I.. life.c
	cat _nekot1_sym.s >> life.s
	$(LWASM) -olife.o life.s --list=life.o.list --map=life.o.map --symbol-dump=life.o.sym
	$(LWLINK) --decb --output=life.game life.o -lgcc --entry=__n1pre_entry --script=_game.script --map=life.map
	grep "^Section:" life.map
	cp -vf life.game /tmp/life.game
	cp -vf life.game /tmp/life.$$(cat _nekot1.decb.hash).game
	echo OKAY $@

red.game: ../games/red/game.c
	python3 ../nekot1/n1preprocess.py $< red.c
	$(GCC) -fwhole-program -S $(GCFLAGS) -I../games/red -I.. red.c
	cat _nekot1_sym.s >> red.s
	$(LWASM) -ored.o red.s --list=red.o.list --map=red.o.map --symbol-dump=red.o.sym
	$(LWLINK) --decb --output=red.game red.o -lgcc --entry=__n1pre_entry --script=_game.script --map=red.map
	grep "^Section:" red.map
	cp -vf red.game /tmp/red.game
	cp -vf red.game /tmp/red.$$(cat _nekot1.decb.hash).game
	echo OKAY $@

spacewar.game: ../games/spacewar/game.c
	python3 ../nekot1/n1preprocess.py $< spacewar.c
	$(GCC) -fwhole-program -S $(GCFLAGS) -I../games/spacewar -I.. spacewar.c
	cat _nekot1_sym.s >> spacewar.s
	$(LWASM) -ospacewar.o spacewar.s --list=spacewar.o.list --map=spacewar.o.map --symbol-dump=spacewar.o.sym
	$(LWLINK) --decb --output=spacewar.game spacewar.o -lgcc --entry=__n1pre_entry --script=_game.script --map=spacewar.map
	grep "^Section:" spacewar.map
	cp -vf spacewar.game /tmp/spacewar.game
	cp -vf spacewar.game /tmp/spacewar.$$(cat _nekot1.decb.hash).game
	echo OKAY $@

