# Produce MSDOS executable using Watcom C
# Build with pdmake -f makefile.wcd
# or wmake -h -e -a -ms -f makefile.wcd

# Choose t (tiny), s (small), m (medium), c (compact), l (large), h (huge)
MODELC=l
# Choose long form of above name, required by wasm instead of wcl
MODELA=large

# If you want to produce a COM file, uncommment this, and follow the
# instructions further down
#MAKECOM=-DMAKECOM

# If you want to use a 32-bit size_t in huge memory model, uncomment this
#SZ4=-D__SZ4__

# If you are using pdld as the linker, you can define SEGHACK
# to stop a rogue sole segment reference from being generated.

# Note that -zt0 or a larger number can be used to put data into
# "far" so that you can have more than 64k.
# Also, -zdf is useful if this code may be called from some
# other executable, so that this executable can reference its
# own data correctly

CC=wcl
COPTS=$(SZ4) -oneatx -ecc -zp1 -q -w -c -I. \
    -m$(MODELC) -zl -D__MSDOS__ -fpi87 -s -zdp -zu -DXSEGHACK
# Adding -d2 allows you to get source code in the disassembly
# (wdis -l -s), but you need to switch off optimization (-oneatx)

pdptest.exe: dosstart.obj pdptest.obj stdio.obj string.obj stdlib.obj \
       start.obj time.obj errno.obj assert.obj signal.obj locale.obj \
       ctype.obj setjmp.obj math.obj dossupc.obj dossupa.obj
  if exist watcom.lib del watcom.lib
  wlib -b -q watcom +dosstart.obj
  wlib -b -q watcom +stdio.obj
  wlib -b -q watcom +string.obj
  wlib -b -q watcom +stdlib.obj
  wlib -b -q watcom +start.obj
  wlib -b -q watcom +time.obj
  wlib -b -q watcom +errno.obj
  wlib -b -q watcom +assert.obj
  wlib -b -q watcom +signal.obj
  wlib -b -q watcom +locale.obj
  wlib -b -q watcom +ctype.obj
  wlib -b -q watcom +setjmp.obj
  wlib -b -q watcom +math.obj
  wlib -b -q watcom +dossupc.obj
  wlib -b -q watcom +dossupa.obj
# To create a COM file, make it "Form dos com" and change the name to .com
# Stack size has no meaning for .com (the SP will be set by the OS, and the
# SS will be set to the CS/DS/ES). Also dosstart.obj needs to come first
# for a COM file, as the IP is always set to 0100H. Also it needs to be
# built with -mt and memmodel=tiny. If you build a .exe instead of a .com
# with all of that in place, the utility exe2bin, if available, can be
# used to convert a .exe into a .com if you are using a linker that
# can't directly produce a .com file.
# Because we are linking dosstart first, we are required to use the
# dosseg option, for some reason, because we need the stack to come last,
# so that the calculations in dosstart.asm are accurate with regard to
# releasing memory.
  wlink File dosstart.obj,pdptest.obj Name pdptest.exe Form dos Library watcom.lib Option quiet,nod,stack=8192,start=___asmstart,map,verbose,dosseg
# Use this instead for an NE executable
#  wasm -zcm -q -DPDOS86 ..\src\needpdos.asm
#  wlink File needpdos.obj Name needpdos.exe Form dos Option quiet,dosseg
#  rm -f kernel.lib
#  echo ++__AHINCR.KERNEL.__AHINCR >temp.wat
#  echo ++__AHSHIFT.KERNEL.__AHSHIFT >>temp.wat
#  wlib -q kernel @temp.wat
#  wlink File dosstart.obj,pdptest.obj Name pdptest.exe Form windows Library watcom.lib,kernel.lib Option quiet,nod,stack=8192,start=___asmstart,map,verbose,dosseg,stub=needpdos.exe

.c.obj:
  $(CC) $(COPTS) $<

dosstart.obj: dosstart.asm
#  as86 -DWATCOM $(SZ4) -Dmemodel=$(MODELA) $(MAKECOM) dosstart.asm
#  ml -c -DWATCOM $(SZ4) -Dmemodel=$(MODELA) $(MAKECOM) dosstart.asm
  wasm -zcm -q -DWATCOM -DWATCOMC $(SZ4) -Dmemodel=$(MODELA) $(MAKECOM) dosstart.asm

dossupa.obj: dossupa.asm
#  as86 -DWATCOMC $(SZ4) -Dmemodel=$(MODELA) -DWATCOM dossupa.asm
#  ml -c -DWATCOMC $(SZ4) -Dmemodel=$(MODELA) -DWATCOM dossupa.asm
  wasm -zcm -q -DWATCOM -DWATCOMC $(SZ4) -Dmemodel=$(MODELA) dossupa.asm
