Skip to content

Instantly share code, notes, and snippets.

@desaster
Created August 6, 2023 23:21
Show Gist options
  • Save desaster/5f2fd3575d60f1ad1b3a068dfafa6d83 to your computer and use it in GitHub Desktop.
Save desaster/5f2fd3575d60f1ad1b3a068dfafa6d83 to your computer and use it in GitHub Desktop.
Makefile for building a bunch of .asm files into bootable DOS floppies
#
# Builds all .asm files in src/ into .com files in build/
#
# For each built .com file in build/, a floppy image is generated in build/
# containing the program, as well as an autoexec.bat to run the program and
# shut down the computer on exit
#
# Requires a bootable DOS floppy image as a base (BASEFLOPPY)
#
# To have the autoexec.bat run the program under debug, use:
#
# make DEBUG=1
#
# To run the generated floppy images, qemu may be used like this:
#
# qemu-system-i386 -fda build/foo.img -boot order=a -nographic
#
ASM=nasm
MCOPY=mcopy
ZCAT=zcat
# should autoexec run the program with debug?
DEBUG?=0
# floppy to use as a base for creating the floppy - may contain useful
# stuff like a bootable dos and debug.com
BASEFLOPPY=../wread/dos622base.img.gz
BUILD_DIR := build
SRC_DIR := src
SRC_FILES := $(wildcard $(SRC_DIR)/*.asm)
COM_FILES := $(patsubst $(SRC_DIR)/%.asm,$(BUILD_DIR)/%.com,$(SRC_FILES))
IMG_FILES := $(patsubst $(SRC_DIR)/%.asm,$(BUILD_DIR)/%.img,$(SRC_FILES))
PROG_PREFIX=
ifeq '$(DEBUG)' '1'
PROG_PREFIX=debug
endif
.PHONY: all clean
.PRECIOUS: $(BUILD_DIR)/%.com
all: $(IMG_FILES)
# bootable DOS floppy image
# requires the base image and mtools
$(BUILD_DIR)/%.img: $(BUILD_DIR)/%.com $(BUILD_DIR)/%.bat $(BUILD_DIR)/shut.com
$(ZCAT) $(BASEFLOPPY) > $@
$(MCOPY) -D o -i $@ $(word 2,$^) ::autoexec.bat
$(MCOPY) -i $@ $(word 3,$^) ::shut.com
$(MCOPY) -i $@ $< ::$(<F)
# autoexec.bat tailored to run our program
$(BUILD_DIR)/%.bat: $(BUILD_DIR)/%.com
printf "@echo off\r\nprompt $$p$$g\r\n$(PROG_PREFIX)$(<F)\r\nshut" > $@
# build the program itself into a com file
$(BUILD_DIR)/%.com: $(SRC_DIR)/%.asm
nasm -w+error -f bin -l $(BUILD_DIR)/$*.lst -o $@ $<
# small com program to shut down the computer by int 15h
# http://www.delorie.com/djgpp/doc/rbinter/id/08/14.html
$(BUILD_DIR)/shut.com:
printf '\270\7\123\273\1\0\271\3\0\315\25' > $@
clean:
rm -f $(BUILD_DIR)/*.com $(BUILD_DIR)/*.lst $(BUILD_DIR)/*.img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment