# VCC Plus Hello Disk recipe adapted for Quadrapassel; DOS helper retained below.
CMOC ?= cmoc
PERL ?= perl
WRITECOCOFILE ?= writecocofile
INSTALL_LOADER ?= install-coco-boot-loader
VCC_CONFIGURATION ?= Debug
ifeq ($(VCC_CONFIGURATION),Release)
OPTIM ?= -O2 --no-relocate --switch=jump
else
OPTIM ?= -O0
DEBUG_FRAME ?= -fno-omit-frame-pointer
endif
CPPFLAGS += -Isrc/game -Isrc/platform -Isrc/APIProbe/include
CFLAGS += --coco --intermediate --deps --stack-space=1024 $(OPTIM) $(DEBUG_FRAME)
VPATH := src/game:src/platform
LDFLAGS += --coco --no-relocate --intermediate --org=1400 --data=0E00 --limit=7B00 --stack-space=1024
ifeq ($(VCC_PROBE),1)
ENTRY := src/APIProbe/mapped.c
SOURCES := src/game/rules.c src/platform/coco3_video.c src/APIProbe/src/disk.c generated/word-count.c
LDLIBS += -ldecbfile
else
ENTRY := main.c
SOURCES := src/game/rules.c src/game/input.c src/game/store.c src/platform/coco3_video.c src/platform/video.c src/platform/disk_store.c
LDLIBS += -lcardgame -lbsound -ldecbfile
endif
OBJECTS := obj/entry.o $(SOURCES:%.c=obj/%.o)
.PHONY: all build disk clean
all: disk
build: QUAD.bin
QUAD.bin: $(OBJECTS) Makefile
	"$(CMOC)" $(LDFLAGS) -o $@ $(OBJECTS) $(LDLIBS)
	$(PERL) tools/check_memory.pl QUAD.map
obj/entry.o: $(ENTRY) Makefile
	@mkdir -p $(dir $@)
	"$(CMOC)" $(CPPFLAGS) $(CFLAGS) --intdir=$(dir $@) -c -o $@ $<
obj/%.o: %.c Makefile
	@mkdir -p $(dir $@)
	"$(CMOC)" $(CPPFLAGS) $(CFLAGS) --intdir=$(dir $@) -c -o $@ $<
generated/word-count.c generated/WORDS.DAT &: src/APIProbe/tools/build-data.pl src/APIProbe/assets/words.txt Makefile
	@mkdir -p generated
	$(PERL) src/APIProbe/tools/build-data.pl src/APIProbe/assets/words.txt generated/word-count.c generated/WORDS.DAT

# DOS helper: Pierre Sarrazin, autostart.c 0.1.2, public domain.
obj/vcc-dos-boot/loader.bin: Makefile
	@mkdir -p $(dir $@)
	@printf '%s\n' '// autostart.c - Program to be run by the Disk Basic DOS command.' '// By Pierre Sarrazin <http://sarrazip.com/>' '// This file is in the public domain.' '//' '// Reference: Roger Schrag, "A Special Use For The DOS Command",' '// The Rainbow, November 1984, p.140.' '//' '// To compile: cmoc --dos autostart.c' '// To install on CoCo disk image: install-coco-boot-loader some-image.dsk autostart.bin' '// To run on CoCo: type DOS and <Enter> to automatically run *.BAS.' '//' '// Version history:' '// 0.1.0    2016-01-16      Use of legacy.h discontinued.' '// 0.1.1    2018-04-03      Adapted to CMOC 0.1.51 (const).' '// 0.1.2    2018-08-04      Basic line now forced to be uppercase.' '' '#include <coco.h>' '' '#define ENDFLG  0x00            // STOP/END FLAG: POSITIVE=STOP, NEG=END' '#define CINBFL  0x70            // CONSOLE IN BUFFER FLAG: 00=NOT EMPTY, $$FF=EMPTY' '#define RVEC4   0x016a          // RAM hook: CONSOLE IN' '' '' 'byte oldHookInstr;' 'word oldHookAddr;' 'const char *basicLineReader;' '' '' 'void feedKeyboard()' '{' '    word preservedX;' '    asm("stx", preservedX);' '' '    * (byte *) CINBFL = 0;  // console in buffer now not empty' '' '    char nextBasicByte = (char) toupper(*basicLineReader);' '    ++basicLineReader;' '    if (nextBasicByte == 0)  // if finished returning Basic line' '    {' '        * (byte *) RVEC4 = oldHookInstr;' '        * (word *) (RVEC4 + 1) = oldHookAddr;' '        nextBasicByte = '"'"'\r'"'"';  // return "Enter" to cause execution of Basic line' '    }' '' '    asm {' '        lda     nextBasicByte' '        ldx     preservedX' '        tfr     u,s             // discard stack frame' '        leas    4,s             // discard previous stack frame ptr and return addr' '        rts                     // return to caller of CONSOLE IN' '    }' '}' '' '' 'int main()' '{' '    * (byte *) ENDFLG = - * (byte *) ENDFLG;  // why?' '' '    basicLineReader =' '        #ifndef COMMAND' '        "RUN\"*.BAS\""' '        #else' '        COMMAND' '        #endif' '        ;' '' '    // Install function feedKeyboard() as CONSOLE IN routine.' '    //' '    oldHookInstr = * (byte *) RVEC4;' '    oldHookAddr  = * (word *) (RVEC4 + 1);' '' '    * (byte *) RVEC4 = 0x7e;  // JMP instruction' '    * (void **) (RVEC4 + 1) = (void *) feedKeyboard;' '' '    * (word *) 0x2600 = 0;  // overwrite "OS" marker with two null bytes' '' '    asm("jmp", "$$ac7c");  // go interpret a Basic line (which is read from console)' '' '    return 0;  // avoids compiler error' '}' > obj/vcc-dos-boot/loader.c
	"$(CMOC)" --dos -O0 --limit=2800 -o $@ '-DCOMMAND="PCLEAR1:LOADM\"QUAD.BIN\":EXEC"' obj/vcc-dos-boot/loader.c


generated/QPTEXT.DAT: assets/ui-text.tsv tools/build_ui.pl
	@mkdir -p generated
	$(PERL) tools/build_ui.pl $< $@

disk: Quadrapassel.DSK
Quadrapassel.DSK: QUAD.bin generated/QPTEXT.DAT generated/WORDS.DAT obj/vcc-dos-boot/loader.bin Makefile
	$(PERL) -e 'print chr(255) x (35*18*256)' > $@
	$(INSTALL_LOADER) $@ obj/vcc-dos-boot/loader.bin
	$(WRITECOCOFILE) $@ QUAD.bin
	$(WRITECOCOFILE) $@ generated/WORDS.DAT
	$(WRITECOCOFILE) $@ generated/QPTEXT.DAT
clean:
	rm -rf obj generated
	rm -f QUAD.bin QUAD.map QUAD.link Quadrapassel.DSK
-include $(OBJECTS:.o=.d)
