2 # Makefile for cc65 samples
4 # This Makefile requires GNU make
7 # Run 'make SYS=<target>'; or, set a SYS env.
8 # var. to build for another target system.
11 # Just the usual way to define a variable
12 # containing a single space character.
16 # Just the usual way to find out if we're
17 # using cmd.exe to execute make rules.
18 ifneq ($(shell echo),)
33 AS = $(CC65_HOME)/bin/ca65
34 CC = $(CC65_HOME)/bin/cc65
35 CL = $(CC65_HOME)/bin/cl65
36 LD = $(CC65_HOME)/bin/ld65
38 AS := $(if $(wildcard ../bin/ca65*),../bin/ca65,ca65)
39 CC := $(if $(wildcard ../bin/cc65*),../bin/cc65,cc65)
40 CL := $(if $(wildcard ../bin/cl65*),../bin/cl65,cl65)
41 LD := $(if $(wildcard ../bin/ld65*),../bin/ld65,ld65)
44 ifneq ($(filter disk samples.%,$(MAKECMDGOALS)),)
46 TARGET_PATH = $(CC65_HOME)/target
48 TARGET_PATH := $(if $(wildcard ../target),../target,$(shell $(CL) --print-target-path))
51 # If TARGET_PATH contains spaces then it is presumed to contain escaped spaces. GNU make
52 # has very limited support for paths containing spaces. $(wildcard) is the only function
53 # that is aware of escaped spaces. However, $(wildcard) never returns paths with escaped
54 # spaces !!! So if it e.g. finds 4 files in a path with 2 spaces then one ends up with a
55 # return value consisting of 12 plain words :-((
57 # Fortunately we can work around that behaviour here because we know that the files we
58 # are looking for have known extensions. So we can $(filter) the in our example above 12
59 # words for file extensions so we come up with 4 path fragments. Then we remove those
60 # path fragments with $(notdir) from the file names.
62 # So far so good. But here we want to process files from different paths in a single
63 # recipe further down below and therefore want to prepend the paths to the files with
64 # $(addprefix). However, $(foreach) isn't aware of escaped spaces (only $(wildcard) is).
65 # Therefore, we need to replace the spaces with some other character temporarily in order
66 # to have $(foreach) generate one invocation per file. We use the character '?' for that
67 # purpose here, just because it is known to not be part of file names.
69 # Inside the recipe generated per file we then replace the '?' again with a space. As we
70 # want to be compatible with cmd.exe for execution we're not using an escaped space but
71 # rather double-quote the whole path.
73 # Note: The "strange" $(wildcard) further down below just serves the purpose to unescape
74 # spaces for cmd.exe. This could have as well been done with another $(subst).
76 SUBST_TARGET_PATH := $(subst \$(SPACE),?,$(TARGET_PATH))
78 EMD := $(wildcard $(TARGET_PATH)/$(SYS)/drv/emd/*)
79 MOU := $(wildcard $(TARGET_PATH)/$(SYS)/drv/mou/*)
80 TGI := $(wildcard $(TARGET_PATH)/$(SYS)/drv/tgi/*)
82 EMD := $(addprefix $(SUBST_TARGET_PATH)/$(SYS)/drv/emd/,$(notdir $(filter %.emd,$(EMD))))
83 MOU := $(addprefix $(SUBST_TARGET_PATH)/$(SYS)/drv/mou/,$(notdir $(filter %.mou,$(MOU))))
84 TGI := $(addprefix $(SUBST_TARGET_PATH)/$(SYS)/drv/tgi/,$(notdir $(filter %.tgi,$(TGI))))
86 # This one comes with the VICE emulator.
87 # See http://vice-emu.sourceforge.net/
90 # For this one, see https://applecommander.github.io/
93 # For this one, see http://www.horus.com/~hias/atari/
96 DISK_c64 = samples.d64
97 DISK_apple2 = samples.dsk
98 DISK_apple2enh = samples.dsk
99 DISK_atari = samples.atr
100 DISK_atarixl = samples.atr
103 # --------------------------------------------------------------------------
104 # System-dependent settings
105 # For convenience, these groups and lines are sorted alphabetically, first
106 # by target-machine group, then by mission, then by program and sub-target.
108 # The Apple machines need the start address adjusted when using TGI
109 LDFLAGS_mandelbrot_apple2 = --start-addr 0x4000
110 LDFLAGS_mandelbrot_apple2enh = --start-addr 0x4000
111 LDFLAGS_tgidemo_apple2 = --start-addr 0x4000
112 LDFLAGS_tgidemo_apple2enh = --start-addr 0x4000
114 # The Apple ][ needs the start address adjusted for the mousedemo
115 LDFLAGS_mousedemo_apple2 = --start-addr 0x4000
117 # The Apple machines need the end address adjusted for large programs
118 LDFLAGS_gunzip65_apple2 = -D __HIMEM__=0xBF00
119 LDFLAGS_gunzip65_apple2enh = -D __HIMEM__=0xBF00
121 # The atari target needs to reserve some memory when using TGI
122 LDFLAGS_mandelbrot_atari = -D __RESERVED_MEMORY__=0x2000
123 LDFLAGS_tgidemo_atari = -D __RESERVED_MEMORY__=0x2000
125 # The atarixl target needs the start address adjusted when using TGI
126 LDFLAGS_mandelbrot_atarixl = --start-addr 0x4000
127 LDFLAGS_tgidemo_atarixl = --start-addr 0x4000
129 # --------------------------------------------------------------------------
132 .PHONY: all mostlyclean clean install zip samples disk
138 $(CC) $(CFLAGS) -Ors --codesize 500 -T -g -t $(SYS) $<
142 $(AS) $(ASFLAGS) -t $(SYS) $<
148 $(LD) $(LDFLAGS_$(@F)_$(SYS)) $(LDFLAGS) -o $@ -C vic20-32k.cfg -m $@.map $^ $(SYS).lib
150 $(LD) $(LDFLAGS_$(@F)_$(SYS)) $(LDFLAGS) -o $@ -t $(SYS) -m $@.map $^ $(SYS).lib
153 # --------------------------------------------------------------------------
154 # Lists of executables
184 EXELIST_apple2enh = $(EXELIST_apple2)
197 EXELIST_atarixl = $(EXELIST_atari)
199 EXELIST_atari2600 = \
202 # Unlisted targets will try to build everything.
203 # That lets us learn what they cannot build, and what settings
204 # we need to use for programs that can be built and run.
205 ifndef EXELIST_$(SYS)
206 EXELIST_$(SYS) := ${patsubst %.c,%,$(wildcard *.c)}
209 # --------------------------------------------------------------------------
210 # Rules to make the binaries and the disk
212 samples: $(EXELIST_$(SYS))
218 # --------------------------------------------------------------------------
219 # Overlay rules. Overlays need special ld65 configuration files. Also, the
220 # overlay file-names are shortenned to fit the Atari's 8.3-character limit.
222 multdemo: multidemo.o
223 $(LD) $(LDFLAGS) -o $@ -C $(SYS)-overlay.cfg -m $@.map $^ $(SYS).lib
225 ovrldemo: overlaydemo.o
226 $(LD) $(LDFLAGS) -o $@ -C $(SYS)-overlay.cfg -m $@.map $^ $(SYS).lib
228 OVERLAYLIST := $(foreach I,1 2 3,multdemo.$I ovrldemo.$I)
230 # --------------------------------------------------------------------------
231 # Rule to make a CBM disk with all samples. Needs the c1541 program that comes
232 # with the VICE emulator.
234 define D64_WRITE_recipe
236 $(C1541) -attach $@ -write "$(subst ?,$(SPACE),$(file))" $(notdir $(file)) >$(NULLDEV)
238 endef # D64_WRITE_recipe
241 @$(C1541) -format samples,AA d64 $@ >$(NULLDEV)
242 $(foreach file,$(EXELIST_$(SYS)),$(D64_WRITE_recipe))
243 $(foreach file,$(OVERLAYLIST),$(D64_WRITE_recipe))
244 $(foreach file,$(EMD) $(MOU) $(TGI),$(D64_WRITE_recipe))
246 # --------------------------------------------------------------------------
247 # Rule to make an Apple II disk with all samples. Needs the AppleCommander
248 # program, available at https://applecommander.github.io/, and a template disk
249 # named 'prodos.dsk'.
251 define DSK_WRITE_BIN_recipe
253 $(if $(findstring BF00,$(LDFLAGS_$(notdir $(file))_$(SYS))), \
254 java -jar $(AC) -p $@ $(notdir $(file)).system sys <"$(wildcard $(TARGET_PATH)/$(SYS)/util/loader.system)")
255 java -jar $(AC) -as $@ $(notdir $(file)) <"$(file)"
257 endef # DSK_WRITE_BIN_recipe
259 define DSK_WRITE_REL_recipe
261 java -jar $(AC) -p $@ $(notdir $(file)) rel 0 <"$(subst ?,$(SPACE),$(file))"
263 endef # DSK_WRITE_REL_recipe
267 $(foreach file,$(EXELIST_$(SYS)),$(DSK_WRITE_BIN_recipe))
268 $(foreach file,$(OVERLAYLIST),$(DSK_WRITE_REL_recipe))
269 $(foreach file,$(EMD) $(MOU) $(TGI),$(DSK_WRITE_REL_recipe))
271 # --------------------------------------------------------------------------
272 # Rule to make an Atari disk with all samples. Needs the dir2atr program
273 # available at http://www.horus.com/~hias/atari/ and the MyDos4534 variant
274 # of dos.sys and dup.sys.
276 define ATR_WRITE_recipe
278 cp "$(subst ?,$(SPACE),$(file))" atr/$(notdir $(file))
280 endef # ATR_WRITE_recipe
284 cp "dos.sys" atr/dos.sys
285 cp "dup.sys" atr/dup.sys
286 @$(foreach file,$(EXELIST_$(SYS)),$(ATR_WRITE_recipe))
287 @$(foreach file,$(OVERLAYLIST),$(ATR_WRITE_recipe))
288 @$(foreach file,$(EMD) $(MOU) $(TGI),$(ATR_WRITE_recipe))
289 $(DIR2ATR) -d -b MyDos4534 3200 $@ atr
292 # --------------------------------------------------------------------------
296 samplesdir = $(PREFIX)/share/cc65/samples
299 $(if $(PREFIX),,$(error variable "PREFIX" must be set))
300 $(INSTALL) -d $(DESTDIR)$(samplesdir)
301 $(INSTALL) -d $(DESTDIR)$(samplesdir)/geos
302 $(INSTALL) -d $(DESTDIR)$(samplesdir)/tutorial
303 $(INSTALL) -m0644 *.* $(DESTDIR)$(samplesdir)
304 $(INSTALL) -m0644 README $(DESTDIR)$(samplesdir)
305 $(INSTALL) -m0644 Makefile $(DESTDIR)$(samplesdir)
306 $(INSTALL) -m0644 geos/*.* $(DESTDIR)$(samplesdir)/geos
307 $(INSTALL) -m0644 tutorial/*.* $(DESTDIR)$(samplesdir)/tutorial
309 # --------------------------------------------------------------------------
313 @cd .. && zip -r cc65 samples/
315 # --------------------------------------------------------------------------
319 @$(DEL) *.lbl *.map *.o *.s 2>$(NULLDEV)
322 @$(DEL) $(EXELIST_$(SYS)) $(DISK_$(SYS)) 2>$(NULLDEV)
323 @$(DEL) multdemo.? ovrldemo.? 2>$(NULLDEV)