Skip to content

Instantly share code, notes, and snippets.

@openback
Created August 15, 2011 15:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save openback/1147035 to your computer and use it in GitHub Desktop.
Save openback/1147035 to your computer and use it in GitHub Desktop.
Bash script to compile and assemble an NES ROM using loopy's asm6 and create files appropriate for burning to chips
#!/bin/bash
#===============================================================================
#
# FILE: makeNES
#
# USAGE: ./makeNES [options] [ASM [CHR [NES]]]
#
# DESCRIPTION: Bash script to compile and assemble an NES ROM using loopy's
# asm6 and create files appropriate for burning to chips
#
# OPTIONS: ---
# REQUIREMENTS: asm6 ( http://home.comcast.net/~olimar/NES/ )
# BUGS: ---
# NOTES: ---
# AUTHOR: Timothy Caraballo, openback@gmail.com
# COMPANY: Pixelpod International, Inc.
# VERSION: 1.00
# CREATED: 2010-08-15 11:30:00 AM EST
#===============================================================================
#===============================================================================
# Script variables
#===============================================================================
NOCLOBBER=0
BOLD=`tput bold`
BOLDOFF=`tput sgr0`
PADCHR=0
CREATEDPADDEDCHR=0
PADPRG=0
NESONLY=0
#===============================================================================
# Prints usage info
#===============================================================================
function USAGE () {
echo "\
${BOLD}USAGE${BOLDOFF}:
`basename $0` [options] [ASM [CHR [NES]]]
Bash script to compile and assemble an NES ROM using loopy's asm6 and
create files appropriate for burning to chips.
${BOLD}OPTIONS${BOLDOFF}
${BOLD}-c n${BOLDOFF} Pads the CHR file to the specified number of KB
${BOLD}-e${BOLDOFF} Create NES file only
${BOLD}-n${BOLDOFF} Do not clobber files
${BOLD}-p n${BOLDOFF} Pads the PRG file to the specified number of KB
${BOLD}-v${BOLDOFF} Version information
${BOLD}-h${BOLDOFF} This usage information
${BOLD}NOTES${BOLDOFF}
If the ASM or CHR files are not specified, the first .asm or .chm
found will be used.
${BOLD}REQUIREMENTS${BOLDOFF}
asm6 ( http://home.comcast.net/~olimar/NES/ )
"
[[ $# ]] && exit $1
}
#===============================================================================
# Main routine
#===============================================================================
# Set our option variables
while getopts ":c:ehnp:v?" opt
do
case $opt in
c)
PADCHR=$OPTARG
;;
e)
NESONLY=1
;;
h)
USAGE 0
;;
n)
NOCLOBBER=1
;;
p)
PADPRG=$OPTARG
;;
v)
echo "`basename $0` 1.00 (2011-08-15)"
exit 0
;;
:)
echo "Option -$OPTARG requires an argument." >&2
echo "Try \``basename $0` -h' for more information." >&2
exit 1
;;
?)
echo ""
echo "Invalid option: -$OPTARG" >&2
USAGE 1
;;
esac
done
shift $(($OPTIND - 1))
# Grab the default filenames first
ASM=`ls -1 *.asm 2>/dev/null|head -1`
CHR=`ls -1 *.chr 2>/dev/null|head -1`
X=${ASM%.asm}
NES=${X##*/}.nes
# Grab ones from the command line if given
(( $# )) && ASM=$1
shift
(( $# )) && CHR=$1
shift
(( $# )) && NES=$1
[ -z "$ASM" ] && { echo "Error: No ASM was found" >&2; exit 1; }
[ -z "$CHR" ] && { echo "Error: No CHR was found" >&2; exit 1; }
[ -f "$ASM" ] || { echo "Error: The ASM, $ASM, was not found" >&2; exit 1; }
[ -f "$CHR" ] || { echo "Error: The CHR, $CHR, was not found" >&2; exit 1; }
[ -f "$NES" -a $NOCLOBBER -ne 0 ] && { echo "Error: $NES already exists."; exit 1; }
X=${CHR%.chr}
PADDEDCHR=${X##*/}.padded.chr
echo "Compiling $ASM"
asm6 "$ASM" prg.tmp.bin || exit 1
if (( $PADPRG )); then
echo "Padding the PRG"
# Make a blank file of the right size
dd if=/dev/zero of=prg.padded.bin bs=16 count=$(( ($PADPRG * 64) + 1 )) 2> /dev/null
if [[ `ls -S1 prg.padded.bin prg.tmp.bin|head -1` == "prg.tmp.bin" ]]; then
echo "Error: Padded PRG file is smaller than the original" >&2
rm prg.tmp.bin prg.padded.bin 2>/dev/null
exit 1
fi
# Copy the compiled PRG into that file
dd if=prg.tmp.bin of=prg.padded.bin bs=16 conv=notrunc 2> /dev/null
mv prg.padded.bin prg.tmp.bin || exit 1
fi
if (( $PADCHR )); then
echo "Padding the CHR"
dd if=/dev/zero of="$PADDEDCHR" bs=1024 count=$PADCHR 2> /dev/null
if [[ `ls -S1 "$PADDEDCHR" "$CHR"|head -1` == $CHR ]]; then
echo "Error: The Padded CHR file is smaller than the original" >&2
rm "$PADDEDCHR"
rm prg.tmp.bin prg.padded.bin 2>/dev/null
exit 1
fi
dd if="$CHR" of="$PADDEDCHR" conv=notrunc 2> /dev/null
CHR=$PADDEDCHR
CREATEDPADDEDCHR=1
fi
echo "Creating $NES"
cat prg.tmp.bin "$CHR" >"$NES"
if (( $NESONLY )); then
rm prg.bin 2>/dev/null
(( $CREATEDPADDEDCHR )) && rm $PADDEDCHR 2>/dev/null
else
# Remove the header from the PRG for burning
dd status=noxfer if=prg.tmp.bin of=prg.bin bs=16 skip=1 2>/dev/null || exit 1
fi
rm prg.tmp.bin
(( $? )) || echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment