2016-07-21 03:39:11 +00:00
#!/usr/bin/python
2017-06-20 22:59:56 +00:00
# system packages
import sys
import os
2017-05-30 21:06:33 +00:00
import argparse
2017-06-20 22:59:56 +00:00
import re
# external packages
import png # package name is "pypng" on pypi.python.org
2016-07-21 03:39:11 +00:00
class Colors :
2017-06-20 13:37:57 +00:00
black , magenta , green , orange , blue , white , key = range ( 7 )
2016-09-10 18:04:57 +00:00
2017-06-20 22:59:56 +00:00
def slugify ( s ) :
""" Simplifies ugly strings into something that can be used as an assembler
label .
2016-09-10 18:04:57 +00:00
2017-06-20 22:59:56 +00:00
>> > print slugify ( " [Some] _ Article ' s Title-- " )
SOME_ARTICLES_TITLE
2017-05-30 21:06:33 +00:00
2017-06-20 22:59:56 +00:00
From https : / / gist . github . com / dolph / 3622892 #file-slugify-py
"""
2016-07-21 03:39:11 +00:00
2017-06-20 22:59:56 +00:00
# "[Some] _ Article's Title--"
# "[SOME] _ ARTICLE'S TITLE--"
s = s . upper ( )
2016-07-21 03:39:11 +00:00
2017-06-20 22:59:56 +00:00
# "[SOME] _ ARTICLE'S_TITLE--"
# "[SOME]___ARTICLE'S_TITLE__"
for c in [ ' ' , ' - ' , ' . ' , ' / ' ] :
s = s . replace ( c , ' _ ' )
2017-05-30 21:06:33 +00:00
2017-06-20 22:59:56 +00:00
# "[SOME]___ARTICLE'S_TITLE__"
# "SOME___ARTICLES_TITLE__"
s = re . sub ( ' \ W ' , ' ' , s )
2017-06-20 13:37:57 +00:00
2017-06-20 22:59:56 +00:00
# "SOME___ARTICLES_TITLE__"
# "SOME ARTICLES TITLE "
s = s . replace ( ' _ ' , ' ' )
# "SOME ARTICLES TITLE "
# "SOME ARTICLES TITLE "
s = re . sub ( ' \ s+ ' , ' ' , s )
# "SOME ARTICLES TITLE "
# "SOME ARTICLES TITLE"
s = s . strip ( )
# "SOME ARTICLES TITLE"
# "SOME_ARTICLES_TITLE"
s = s . replace ( ' ' , ' _ ' )
return s
2017-06-21 02:55:47 +00:00
class Syntax ( object ) :
def asm ( self , text ) :
return " \t %s " % text
def comment ( self , text ) :
return " \t ; %s " % text
def label ( self , text ) :
return text
def byte ( self , text ) :
return self . asm ( " .byte %s " % text )
def word ( self , text ) :
return self . asm ( " .word %s " % text )
def address ( self , text ) :
return self . asm ( " .addr %s " % text )
def origin ( self , text ) :
return self . asm ( " *= %s " % text )
2017-06-21 05:09:27 +00:00
def binary_constant ( self , value ) :
try :
# already a string
_ = len ( value )
return " # %% %s " % value
except TypeError :
return " # %s " % format ( value , " 08b " )
2017-06-21 02:55:47 +00:00
class Mac65 ( Syntax ) :
def address ( self , text ) :
return self . asm ( " .word %s " % text )
2017-06-21 05:09:27 +00:00
def binary_constant ( self , value ) :
# MAC/65 doesn't do binary constants
try :
# a string
value = int ( value , 2 )
except TypeError :
pass
return " #$ %02x ; %s " % ( value , format ( value , " 08b " ) )
2017-06-21 02:55:47 +00:00
class CC65 ( Syntax ) :
def label ( self , text ) :
return " %s : " % text
2017-06-20 22:59:56 +00:00
class Listing ( object ) :
disclaimer = '''
; This file was generated by HiSprite . py , a sprite compiler by Quinn Dunki .
; If you feel the need to modify this file , you are probably doing it wrong .
'''
2017-06-21 02:55:47 +00:00
def __init__ ( self , assembler ) :
self . assembler = assembler
2017-06-20 22:59:56 +00:00
self . lines = [ self . disclaimer ]
2017-06-21 04:46:16 +00:00
self . current = None
self . desired_count = 1
self . stash_list = [ ]
2017-06-20 22:59:56 +00:00
def __str__ ( self ) :
2017-06-21 04:46:16 +00:00
self . flush_stash ( )
2017-06-20 22:59:56 +00:00
return " \n " . join ( self . lines ) + " \n "
def out ( self , line ) :
2017-06-21 04:46:16 +00:00
self . flush_stash ( )
2017-06-20 22:59:56 +00:00
self . lines . append ( line )
2017-06-21 02:55:47 +00:00
def out_append_last ( self , line ) :
self . lines [ - 1 ] + = line
def label ( self , text ) :
self . out ( self . assembler . label ( text ) )
def comment ( self , text ) :
self . out_append_last ( self . assembler . comment ( text ) )
def asm ( self , text ) :
self . out ( self . assembler . asm ( text ) )
def addr ( self , text ) :
self . out ( self . assembler . address ( text ) )
2017-06-21 04:46:16 +00:00
def flush_stash ( self ) :
if self . current is not None and len ( self . stash_list ) > 0 :
self . lines . append ( self . current ( " , " . join ( self . stash_list ) ) )
self . current = None
self . stash_list = [ ]
self . desired_count = 1
def stash ( self , desired , text , per_line ) :
if self . current is not None and ( self . current != desired or per_line == 1 ) :
self . flush_stash ( )
if per_line > 1 :
if self . current is None :
self . current = desired
self . desired_count = per_line
self . stash_list . append ( text )
if len ( self . stash_list ) > = self . desired_count :
self . flush_stash ( )
else :
self . out ( desired ( text ) )
2017-06-21 02:55:47 +00:00
2017-06-21 05:09:27 +00:00
def binary_constant ( self , value ) :
return self . assembler . binary_constant ( value )
2017-06-21 04:46:16 +00:00
def byte ( self , text , per_line = 1 ) :
self . stash ( self . assembler . byte , text , per_line )
def word ( self , text , per_line = 1 ) :
self . stash ( self . assembler . word , text , per_line )
2017-06-21 02:55:47 +00:00
2017-06-20 22:59:56 +00:00
class Sprite ( Listing ) :
2017-06-21 02:55:47 +00:00
def __init__ ( self , pngfile , assembler , xdraw = False , processor = " any " ) :
Listing . __init__ ( self , assembler )
2017-06-20 22:59:56 +00:00
reader = png . Reader ( pngfile )
try :
pngdata = reader . asRGB8 ( )
except :
raise RuntimeError
self . xdraw = xdraw
2017-06-21 02:55:47 +00:00
self . processor = processor
2017-06-20 22:59:56 +00:00
self . niceName = slugify ( os . path . splitext ( pngfile ) [ 0 ] )
self . width = pngdata [ 0 ]
self . height = pngdata [ 1 ]
self . pixelData = list ( pngdata [ 2 ] )
self . calcStorage ( )
self . jumpTable ( )
for i in range ( self . numShifts ) :
self . blitShift ( i )
def calcStorage ( self ) :
self . byteWidth = self . width / 2 + 1 + 1 # TODO: Calculate a power of two for this
self . numShifts = 7
def jumpTable ( self ) :
# Prologue
2017-06-21 02:55:47 +00:00
self . label ( " %s " % self . niceName )
self . comment ( " %d bytes per row " % self . byteWidth )
self . asm ( " SAVE_AXY " )
self . asm ( " ldy PARAM0 " )
self . asm ( " ldx MOD7_2,y " )
if self . processor == " any " :
self . out ( " .ifpC02 " )
self . jump65C02 ( )
self . out ( " .else " )
self . jump6502 ( )
self . out ( " .endif " )
elif self . processor == " 65C02 " :
self . jump65C02 ( )
elif self . processor == " 6502 " :
self . jump6502 ( )
else :
raise RuntimeError ( " Processor %s not supported " % self . processor )
def jump65C02 ( self ) :
self . asm ( " jmp ( %s _JMP,x) \n " % ( self . niceName ) )
2017-06-20 22:59:56 +00:00
offset_suffix = " "
# Bit-shift jump table for 65C02
2017-06-21 02:55:47 +00:00
self . label ( " %s _JMP " % ( self . niceName ) )
2017-06-20 22:59:56 +00:00
for shift in range ( self . numShifts ) :
2017-06-21 02:55:47 +00:00
self . addr ( " %s _SHIFT %d " % ( self . niceName , shift ) )
2017-06-20 22:59:56 +00:00
2017-06-21 02:55:47 +00:00
def jump6502 ( self ) :
2017-06-20 22:59:56 +00:00
# Fast jump table routine; faster and smaller than self-modifying code
2017-06-21 02:55:47 +00:00
self . asm ( " lda %s _JMP+1,x " % ( self . niceName ) )
self . asm ( " pha " )
self . asm ( " lda %s _JMP,x " % ( self . niceName ) )
self . asm ( " pha " )
self . asm ( " rts \n " )
2017-06-20 22:59:56 +00:00
# Bit-shift jump table for generic 6502
2017-06-21 02:55:47 +00:00
self . label ( " %s _JMP " % ( self . niceName ) )
2017-06-20 22:59:56 +00:00
for shift in range ( self . numShifts ) :
2017-06-21 02:55:47 +00:00
self . addr ( " %s _SHIFT %d -1 " % ( self . niceName , shift ) )
2017-06-20 22:59:56 +00:00
def blitShift ( self , shift ) :
# Blitting functions
self . out ( " \n " )
2017-06-20 13:37:57 +00:00
# Track cycle count of the blitter. We start with fixed overhead:
# SAVE_AXY + RESTORE_AXY + rts + sprite jump table
cycleCount = 9 + 12 + 6 + 3 + 4 + 6
2017-06-21 02:55:47 +00:00
self . label ( " %s _SHIFT %d " % ( self . niceName , shift ) )
self . asm ( " ldx PARAM1 " )
2017-06-20 13:37:57 +00:00
cycleCount + = 3
2017-06-20 22:59:56 +00:00
rowStartCode , extraCycles = self . rowStartCalculatorCode ( ) ;
self . out ( rowStartCode )
2017-06-20 13:37:57 +00:00
cycleCount + = extraCycles
2017-06-20 22:59:56 +00:00
spriteChunks = self . layoutSpriteChunk ( shift , cycleCount )
2017-06-20 13:37:57 +00:00
2017-06-20 22:59:56 +00:00
for row in range ( self . height ) :
2017-06-20 13:37:57 +00:00
for chunkIndex in range ( len ( spriteChunks ) ) :
2017-06-20 22:59:56 +00:00
self . out ( spriteChunks [ chunkIndex ] [ row ] )
2016-12-25 20:43:14 +00:00
2017-06-20 22:59:56 +00:00
def layoutSpriteChunk ( self , shift , cycleCount ) :
2016-12-25 20:43:14 +00:00
2017-06-20 22:59:56 +00:00
colorStreams = self . byteStreamsFromPixels ( shift , bitsForColor , highBitForColor )
# print colorStreams
maskStreams = self . byteStreamsFromPixels ( shift , bitsForMask , highBitForMask )
# print maskStreams
code = self . generateBlitter ( colorStreams , maskStreams , cycleCount )
2016-12-25 20:43:14 +00:00
2017-06-20 22:59:56 +00:00
return code
2016-12-25 20:43:14 +00:00
2017-06-20 22:59:56 +00:00
def byteStreamsFromPixels ( self , shift , bitDelegate , highBitDelegate ) :
byteStreams = [ " " for x in range ( self . height ) ]
byteWidth = self . width / 2 + 1 + 1
2016-12-25 20:43:14 +00:00
2017-06-20 22:59:56 +00:00
for row in range ( self . height ) :
bitStream = " "
2017-06-20 13:37:57 +00:00
2017-06-20 22:59:56 +00:00
# Compute raw bitstream for row from PNG pixels
for pixelIndex in range ( self . width ) :
pixel = pixelColor ( self . pixelData , row , pixelIndex )
bitStream + = bitDelegate ( pixel )
2017-06-20 13:37:57 +00:00
2017-06-20 22:59:56 +00:00
# Shift bit stream as needed
bitStream = shiftStringRight ( bitStream , shift )
bitStream = bitStream [ : byteWidth * 8 ]
2017-06-20 13:37:57 +00:00
2017-06-20 22:59:56 +00:00
# Split bitstream into bytes
bitPos = 0
byteSplits = [ 0 for x in range ( byteWidth ) ]
2017-06-20 13:37:57 +00:00
2017-06-20 22:59:56 +00:00
for byteIndex in range ( byteWidth ) :
remainingBits = len ( bitStream ) - bitPos
bitChunk = " "
if remainingBits < 0 :
bitChunk = " 0000000 "
else :
if remainingBits < 7 :
bitChunk = bitStream [ bitPos : ]
bitChunk + = fillOutByte ( 7 - remainingBits )
else :
bitChunk = bitStream [ bitPos : bitPos + 7 ]
bitChunk = bitChunk [ : : - 1 ]
# Determine palette bit from first pixel on each row
highBit = highBitDelegate ( self . pixelData [ row ] [ 0 ] )
byteSplits [ byteIndex ] = highBit + bitChunk
bitPos + = 7
byteStreams [ row ] = byteSplits ;
2016-12-25 20:43:14 +00:00
2017-06-20 22:59:56 +00:00
return byteStreams
2017-06-20 13:37:57 +00:00
2017-06-20 22:59:56 +00:00
def generateBlitter ( self , colorStreams , maskStreams , baseCycleCount ) :
byteWidth = len ( colorStreams [ 0 ] )
spriteChunks = [ [ " " for y in range ( self . height ) ] for x in range ( byteWidth ) ]
2017-06-20 13:37:57 +00:00
2017-06-20 22:59:56 +00:00
cycleCount = baseCycleCount
optimizationCount = 0
for row in range ( self . height ) :
2017-06-20 13:37:57 +00:00
2017-06-20 22:59:56 +00:00
byteSplits = colorStreams [ row ]
2017-06-20 13:37:57 +00:00
2017-06-20 22:59:56 +00:00
# Generate blitting code
for chunkIndex in range ( len ( byteSplits ) ) :
# Optimization
if byteSplits [ chunkIndex ] != " 00000000 " and \
byteSplits [ chunkIndex ] != " 10000000 " :
2017-06-21 05:09:27 +00:00
value = self . binary_constant ( byteSplits [ chunkIndex ] )
2017-06-20 22:59:56 +00:00
# Store byte into video memory
if self . xdraw :
spriteChunks [ chunkIndex ] [ row ] = \
" \t lda (SCRATCH0),y \n " + \
2017-06-21 05:09:27 +00:00
" \t eor %s \n " % value + \
2017-06-20 22:59:56 +00:00
" \t sta (SCRATCH0),y \n " ;
cycleCount + = 5 + 2 + 6
else :
spriteChunks [ chunkIndex ] [ row ] = \
2017-06-21 05:09:27 +00:00
" \t lda %s \n " % value + \
2017-06-20 22:59:56 +00:00
" \t sta (SCRATCH0),y \n " ;
cycleCount + = 2 + 6
2017-06-20 13:37:57 +00:00
else :
2017-06-20 22:59:56 +00:00
optimizationCount + = 1
# Increment indices
if chunkIndex == len ( byteSplits ) - 1 :
spriteChunks [ chunkIndex ] [ row ] + = " \n "
else :
spriteChunks [ chunkIndex ] [ row ] + = " \t iny "
cycleCount + = 2
# Finish the row
if row < self . height - 1 :
rowStartCode , extraCycles = self . rowStartCalculatorCode ( )
spriteChunks [ chunkIndex ] [ row ] + = " \t inx \n " + rowStartCode ;
cycleCount + = 2 + extraCycles
2017-06-20 13:37:57 +00:00
else :
2017-06-20 22:59:56 +00:00
spriteChunks [ chunkIndex ] [ row ] + = " \t RESTORE_AXY \n "
spriteChunks [ chunkIndex ] [ row ] + = " \t rts \t ;Cycle count: %d , Optimized %d rows. " % ( cycleCount , optimizationCount ) + " \n "
2017-06-20 13:37:57 +00:00
2017-06-20 22:59:56 +00:00
return spriteChunks
2016-08-18 19:12:11 +00:00
2017-06-20 22:59:56 +00:00
def rowStartCalculatorCode ( self ) :
return \
" \t lda HGRROWS_H1,x \n " + \
" \t sta SCRATCH1 \n " + \
" \t lda HGRROWS_L,x \n " + \
" \t sta SCRATCH0 \n " + \
" \t ldy PARAM0 \n " + \
" \t lda DIV7_2,y \n " + \
" \t tay \n " , 4 + 3 + 4 + 3 + 3 + 4 + 2 ;
2016-12-25 20:43:14 +00:00
2016-09-10 18:04:57 +00:00
def fillOutByte ( numBits ) :
2017-06-20 13:37:57 +00:00
filler = " "
for bit in range ( numBits ) :
filler + = " 0 "
return filler
2016-08-18 19:12:11 +00:00
2016-09-10 18:04:57 +00:00
def shiftStringRight ( string , shift ) :
2017-06-20 13:37:57 +00:00
if shift == 0 :
return string
shift * = 2
result = " "
for i in range ( shift ) :
result + = " 0 "
result + = string
return result
2016-12-25 20:43:14 +00:00
def bitsForColor ( pixel ) :
2017-06-20 13:37:57 +00:00
if pixel == Colors . black :
return " 00 "
else :
if pixel == Colors . white :
return " 11 "
else :
if pixel == Colors . green or pixel == Colors . orange :
return " 01 "
2016-12-25 20:43:14 +00:00
2017-06-20 13:37:57 +00:00
# blue or magenta
return " 10 "
2016-12-25 20:43:14 +00:00
def bitsForMask ( pixel ) :
2017-06-20 13:37:57 +00:00
if pixel == Colors . black :
return " 00 "
2016-12-25 20:43:14 +00:00
2017-06-20 13:37:57 +00:00
return " 11 "
2016-12-25 20:43:14 +00:00
def highBitForColor ( pixel ) :
2017-06-20 13:37:57 +00:00
# Note that we prefer high-bit white because blue fringe is less noticeable than magenta.
highBit = " 0 "
if pixel == Colors . orange or pixel == Colors . blue or pixel == Colors . white :
highBit = " 1 "
2016-12-25 20:43:14 +00:00
2017-06-20 13:37:57 +00:00
return highBit
2016-12-25 20:43:14 +00:00
def highBitForMask ( pixel ) :
2017-06-20 13:37:57 +00:00
return " 1 "
2016-12-25 20:43:14 +00:00
def pixelColor ( pixelData , row , col ) :
2017-06-20 13:37:57 +00:00
r = pixelData [ row ] [ col * 3 ]
g = pixelData [ row ] [ col * 3 + 1 ]
b = pixelData [ row ] [ col * 3 + 2 ]
color = Colors . black
if r == 255 and g == 0 and b == 255 :
color = Colors . magenta
else :
if r == 0 and g == 255 and b == 0 :
color = Colors . green
else :
if r == 0 and g == 0 and b == 255 :
color = Colors . blue
else :
if r == 255 and g > 0 and b == 0 :
color = Colors . orange
else :
if r == 255 and g == 255 and b == 255 :
color = Colors . white
else :
if r == g and r == b and r != 0 and r != 255 : # Any gray is chroma key
color = Colors . key
return color
2016-07-21 03:39:11 +00:00
2016-08-18 19:12:11 +00:00
2017-06-20 22:59:56 +00:00
class HorizontalLookup ( Listing ) :
2017-06-21 02:55:47 +00:00
def __init__ ( self , assembler ) :
Listing . __init__ ( self , assembler )
self . generate_hgr ( )
2017-06-20 22:59:56 +00:00
self . generate_tables ( )
2017-05-30 21:06:33 +00:00
2017-06-21 02:55:47 +00:00
def generate_hgr ( self ) :
offsets = [ ]
for y in range ( 192 ) :
# From Apple Graphics and Arcade Game Design
a = y / / 64
d = y - ( 64 * a )
b = d / / 8
c = d - 8 * b
offsets . append ( ( 1024 * c ) + ( 128 * b ) + ( 40 * a ) )
self . label ( " HGRROWS_H1 " )
for y in range ( 192 ) :
addr = 0x2000 + offsets [ y ]
2017-06-21 04:46:16 +00:00
self . byte ( " $ %02x " % ( addr / / 256 ) , 8 )
2017-06-21 02:55:47 +00:00
2017-06-21 04:46:16 +00:00
self . out ( " \n " )
2017-06-21 02:55:47 +00:00
self . label ( " HGRROWS_H2 " )
for y in range ( 192 ) :
addr = 0x4000 + offsets [ y ]
2017-06-21 04:46:16 +00:00
self . byte ( " $ %02x " % ( addr / / 256 ) , 8 )
2017-06-21 02:55:47 +00:00
2017-06-21 04:46:16 +00:00
self . out ( " \n " )
2017-06-21 02:55:47 +00:00
self . label ( " HGRROWS_L " )
for y in range ( 192 ) :
addr = offsets [ y ]
2017-06-21 04:46:16 +00:00
self . byte ( " $ %02x " % ( addr & 0xff ) , 8 )
2017-06-21 02:55:47 +00:00
2017-06-20 22:59:56 +00:00
def generate_tables ( self ) :
2017-06-21 02:55:47 +00:00
self . label ( " DIV7_2 " )
2017-06-20 22:59:56 +00:00
for pixel in range ( 140 ) :
2017-06-21 04:46:16 +00:00
self . byte ( " $ %02x " % ( ( pixel / 7 ) * 2 ) , 7 )
2016-07-21 03:39:11 +00:00
2017-06-21 02:55:47 +00:00
self . out ( " \n " )
self . label ( " MOD7_2 " )
2017-06-20 22:59:56 +00:00
for pixel in range ( 140 ) :
2017-06-21 04:46:16 +00:00
self . byte ( " $ %02x " % ( ( pixel % 7 ) * 2 ) , 7 )
2016-07-21 03:39:11 +00:00
if __name__ == " __main__ " :
2017-06-20 22:59:56 +00:00
parser = argparse . ArgumentParser ( description = " Sprite compiler for 65C02/6502 to generate assembly code to render all shifts of the given sprite, optionally with exclusive-or drawing (if background will be non-black). Generated code has conditional compilation directives for the CC65 assembler to allow the same file to be compiled for either architecture. " )
parser . add_argument ( " -v " , " --verbose " , default = 0 , action = " count " )
parser . add_argument ( " -t " , " --tables " , action = " store_true " , default = False , help = " output only lookup tables for horizontal sprite shifts (division and modulus 7) " )
parser . add_argument ( " -x " , " --xdraw " , action = " store_true " , default = False , help = " use XOR for sprite drawing " )
2017-06-21 02:55:47 +00:00
parser . add_argument ( " -s " , " --syntax " , default = " cc65 " , nargs = 1 , choices = [ " cc65 " , " mac65 " ] , help = " Assembler syntax (default: %(default)s ) " )
parser . add_argument ( " -p " , " --processor " , default = " any " , nargs = 1 , choices = [ " any " , " 6502 " , " 65C02 " ] , help = " Processor type (default: %(default)s ) " )
2017-06-20 22:59:56 +00:00
parser . add_argument ( " files " , metavar = " IMAGE " , nargs = " * " , help = " a PNG image [or a list of them]. PNG files must not have an alpha channel! " )
options , extra_args = parser . parse_known_args ( )
2017-06-21 02:55:47 +00:00
if options . syntax [ 0 ] . lower ( ) == " cc65 " :
syntax = CC65 ( )
elif options . syntax [ 0 ] . lower ( ) == " mac65 " :
syntax = Mac65 ( )
else :
print ( " Unknown assembler %s " % options . syntax )
parser . print_help ( )
2017-06-20 22:59:56 +00:00
if options . tables :
2017-06-21 02:55:47 +00:00
print HorizontalLookup ( syntax )
2017-06-20 22:59:56 +00:00
exit ( 0 )
for pngfile in options . files :
try :
2017-06-21 02:55:47 +00:00
print Sprite ( pngfile , syntax , options . xdraw , options . processor [ 0 ] )
except RuntimeError , e :
print e
parser . print_help ( )