Files
VCCPlusDiagnostics/examples/fpga256/build.py
T

80 lines
4.5 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
"""Build the assembly-only FPGA Colour Lab disk with native LWASM/Python."""
import argparse
import hashlib
import itertools
import json
from pathlib import Path
import shutil
import subprocess
PROJECT = Path(__file__).resolve().parent
FONT = dict(zip('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ', [
'25755','65656','34443','65556','74647','74644','34553','55755','72227',
'11152','55655','44447','57755','57775','25552','65644','25573','65655',
'34216','72222','55557','55552','55575','55255','55222','71247',
'75557','26227','61247','61216','55711','74616','34652','71111','25652','25716','00000']))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--lwasm', type=Path, required=True)
parser.add_argument('--output', type=Path, required=True)
a = parser.parse_args()
output = a.output.resolve()
if output.exists(): parser.error('Use a fresh output folder; preserve prior disks.')
output.mkdir(parents=True)
version = subprocess.check_output([str(a.lwasm.resolve()), '--version'], text=True).strip()
image = bytearray(160*32)
for text, y in [('FPGA COLOUR LAB 256',2),('SPACE NEXT A AUTO Q QUIT',12),('1 GRID 2 RAMP 3 STRIPES',22)]:
left = (160 - len(text)*4)//2
for n,c in enumerate(text):
for row, bits in enumerate(FONT[c]):
for bit in range(3):
if int(bits)&(4>>bit): image[(y+row)*160+left+n*4+bit]=255
runs=[]
for value, group in itertools.groupby(image):
count=len(list(group))
while count:
part=min(count,255); runs.extend([part,value]);count-=part
runs.append(0)
words=[]
for i in range(256):
r,g,b=i>>5,(i>>2)&7,i&3
value=((r>>2)<<11)|(((r>>1)&1)<<8)|((r&1)<<5)|((g>>2)<<10)|(((g>>1)&1)<<7)|((g&1)<<4)|((b>>1)<<9)|((b&1)<<6)
words.extend([value>>6,value&63])
with (output/'generated.inc').open('w') as f:
for label,data in [('colours',words),('banner',runs)]:
f.write(label+'\n')
for start in range(0,len(data),16):f.write(' fcb '+','.join('$%02x'%v for v in data[start:start+16])+'\n')
shutil.copy2(PROJECT/'colorlab.asm',output/'colorlab.asm')
subprocess.run([str(a.lwasm.resolve()),'--format=decb','--output=COLORLAB.BIN','--list=COLORLAB.lst','colorlab.asm'],cwd=output,check=True)
binary=(output/'COLORLAB.BIN').read_bytes()
assert binary[0]==0 and int.from_bytes(binary[3:5],'big')==0x2000
assert 0x2000+int.from_bytes(binary[1:3],'big')<0x4000,'Code must stay below remapped framebuffer'
basic=b'10 PCLEAR1:CLEAR200,&H1FFF:CLS\r20 PRINT"FPGA COLOUR LAB - EXTENSION ONLY"\r30 PRINT"ENABLE 256 COLOURS; EJECT SSC."\r40 PRINT"GRID: 16 BY 16 DISTINCT COLOURS"\r50 PRINT"SPACE: NEXT PATTERN A: ANIMATE"\r60 PRINT"Q: RETURN. STANDARD COCO UNSUPPORTED"\r70 PRINT"PRESS ANY KEY TO START"\r80 IF INKEY$="" THEN80\r90 LOADM"COLORLAB":EXEC\r100 PRINT"RETURNED FROM COLOUR LAB"\r'
(output/'COLORLAB.BAS').write_bytes(basic)
disk=bytearray(b'\xff'*(35*18*256)); fat=(17*18+1)*256; directory=(17*18+2)*256; nextgranule=0
for index,(name,data,kind,ascii_flag) in enumerate([('COLORLAB.BAS',basic,0,255),('COLORLAB.BIN',binary,2,0)]):
first=nextgranule; count=(len(data)+2303)//2304
for n in range(count):
g=nextgranule; nextgranule+=1; chunk=data[n*2304:(n+1)*2304]
offset=((g//2+(g>=34))*18+(g%2)*9)*256
disk[offset:offset+len(chunk)]=chunk
disk[fat+g]=(g+1) if n+1<count else 0xc0+(len(chunk)+255)//256
base,ext=name.split('.'); entry=bytearray(32)
entry[:11]=(base.ljust(8)+ext.ljust(3)).encode();entry[11:14]=bytes([kind,ascii_flag,first])
entry[14:16]=(len(data)%256 or 256).to_bytes(2,'big');disk[directory+index*32:directory+(index+1)*32]=entry
(output/'COLORLAB.DSK').write_bytes(disk)
digest=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
manifest={'assembler':version,'cpu':'MC6809','requires':'FPGA 256-color extension; SSC absent',
'hardware_tested':False,'org':8192,'framebuffer':'physical 4000..B7FF; 160x192',
'source_sha256':{n:digest(PROJECT/n) for n in ['colorlab.asm','build.py']},
'artifacts':{n:{'bytes':(output/n).stat().st_size,'sha256':digest(output/n)} for n in ['COLORLAB.BIN','COLORLAB.BAS','COLORLAB.DSK','generated.inc']}}
(output/'manifest.json').write_text(json.dumps(manifest,indent=2)+'\n')
print('Built assembly-only Colour Lab:',output/'COLORLAB.DSK')
if __name__=='__main__':main()