Skip to content

Instantly share code, notes, and snippets.

@gasman
Created August 23, 2021 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gasman/3d966f23aa317eacccb8da5e23713333 to your computer and use it in GitHub Desktop.
Save gasman/3d966f23aa317eacccb8da5e23713333 to your computer and use it in GitHub Desktop.
Python script for building the TIC-ROLL TIC-80 cartridge https://tic80.com/play?cart=1754
#!/usr/bin/env python3
import wave
from io import BytesIO
from PIL import Image
wave_buffer = BytesIO()
data_len = 0
with wave.open('nevergonna-1920.wav', 'rb') as wav:
while data_len < 58224:
frames = wav.readframes(2)
b0h = frames[0] // 16
b1h = frames[1] // 16
wave_buffer.write(bytes([b0h | (b1h << 4)]))
data_len += 1
wave_data = wave_buffer.getvalue()
tile_data = wave_data[0x0000:0x2000]
sprite_data = wave_data[0x2000:0x4000]
map_data = wave_data[0x4000:0xbf80]
waveform_data = wave_data[0xbf80:0xc080]
sfx_data = wave_data[0xc080:0xd100]
music_pat_data = wave_data[0xd100:0xfe00]
with open('rick.gif', 'rb') as gif:
cover_data = gif.read()
img = Image.open('rick.gif')
palette_data = bytes(img.getpalette()[:48])
program_data = b'''-- title: TIC-roll
-- author: Gasman / Hooy-Program
-- desc: PCM sample playback at 1.96kHz. I'm really sorry.
-- script: lua
addr=0
function TIC()
poke(0xff9c,0x3c)
poke(0xff9d,0xf0)
if addr < 0xbf80 then
memcpy(0xff9e,addr+0x4000,16)
else
memcpy(0xff9e,addr+0x4064,16)
end
addr=addr+16
if addr>58224 then addr=17664 end
end
'''
def write_chunk(f, typ, data):
tic.write(bytes([typ]))
tic.write(bytes([len(data) & 0xFF]))
tic.write(bytes([len(data) >> 8]))
tic.write(bytes([0]))
tic.write(data)
with open('ticroll.tic', 'wb') as tic:
write_chunk(tic, 0x01, tile_data)
write_chunk(tic, 0x02, sprite_data)
write_chunk(tic, 0x03, cover_data)
write_chunk(tic, 0x04, map_data)
write_chunk(tic, 0x05, program_data)
write_chunk(tic, 0x09, sfx_data)
write_chunk(tic, 0x0a, waveform_data)
write_chunk(tic, 0x0c, palette_data)
write_chunk(tic, 0x0d, music_pat_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment