Example program for the Cidco MailStation Z80 computer
1#
2# MailStation example program
3# Copyright (c) 2019-2021 joshua stein <jcs@jcs.org>
4#
5# Permission to use, copy, modify, and distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16#
17
18# the name of your program
19PROG= hello
20
21# your c source files
22SRCS= main.c
23
24# your asm source files
25ASM_SRCS=
26
27# default to build running from ram
28LOC?= ram
29
30
31ASZ80?= sdasz80 -l -ff
32SDCC?= sdcc -mz80 --opt-code-size
33
34SRCDIR?= ${.CURDIR}
35
36OBJ?= ${SRCDIR}/obj
37
38.if ${LOC} == "flash"
39BASE_ADDR= 0x4000
40DEFS+= -DLOC_FLASH
41.elif ${LOC} == "ram"
42BASE_ADDR= 0x8000
43DEFS+= -DLOC_RAM
44.else
45.BEGIN:
46 @echo 'LOC must be "flash" or "ram"'
47 @exit 1
48.endif
49
50all: checkobj $(PROG).bin
51
52obj: .PHONY
53 mkdir $(OBJ)
54
55# it would be nice to just make this ourselves but if it doesn't exist before
56# make is invoked, it screws things up
57checkobj: .PHONY
58 @if [ ! -d $(OBJ) ]; then \
59 echo "\"${MAKE} obj\" first"; \
60 exit 1; \
61 fi
62
63clean:
64 rm -rf $(OBJ)/*
65
66# assembly
67
68ADDRS_INC= ${SRCDIR}/lib/addrs-${LOC}.inc
69
70crt0.rel: lib/crt0.s
71 $(ASZ80) -o ${.TARGET} ${ADDRS_INC} $>
72
73getchar.rel: lib/getchar.s
74 $(ASZ80) -o ${.TARGET} ${ADDRS_INC} $>
75
76putchar.rel: lib/putchar.s lib/font/spleen-5x8.inc
77 $(ASZ80) -o ${.TARGET} ${ADDRS_INC} $(SRCDIR)/lib/putchar.s
78
79wifi.rel: lib/wifi.s
80 $(ASZ80) -o ${.TARGET} ${ADDRS_INC} $>
81
82# generated code
83
84$(SRCDIR)/lib/font/spleen-5x8.inc: $(SRCDIR)/lib/font/spleen-5x8.hex
85 ruby $(SRCDIR)/lib/tools/hexfont2inc.rb $> > ${.TARGET}
86
87# all relocation files for final ihx
88REL_FILES= crt0.rel putchar.rel getchar.rel wifi.rel
89
90.for S in $(SRCS)
91REL_FILES+= ${S:R}.rel
92${S:R}.rel: $(S)
93 $(SDCC) $(DEFS) -c ${.TARGET} $(SRCDIR)/$(S)
94.endfor
95
96.for S in $(ASM_SRCS)
97REL_FILES+= ${S:R}.rel
98${S:R}.rel: $(S)
99 $(ASZ80) $(DEFS) -o ${.TARGET} ${ADDRS_INC} $>
100.endfor
101
102# link
103
104$(PROG).ihx: $(REL_FILES)
105 @SDCC="$(SDCC) --no-std-crt0" TARGET="$(.TARGET)" \
106 BASE_ADDR="$(BASE_ADDR)" CODE_OFF="$(CODE_OFF)" \
107 PROG_MAP="$(PROG).map" \
108 ruby $(SRCDIR)/lib/tools/relink_packed.rb $>
109
110# convert to binary
111
112$(PROG).bin: $(PROG).ihx
113 objcopy -Iihex -Obinary $> $@
114 @if [ `stat -f '%z' ${.TARGET}` -gt 16384 ]; then \
115 ls -l ${.TARGET}; \
116 echo "${.TARGET} overflows a ${LOC} page, must be <= 16384; increase DATA_SIZE"; \
117 exit 1; \
118 fi
119
120# helpers
121
122disasm: $(PROG).bin
123 z80dasm -al -g ${BASE_ADDR} $> > $(PROG).dasm