Skip to content

Instantly share code, notes, and snippets.

@gasman
Created August 23, 2021 11:44
Show Gist options
  • Select an option

  • Save gasman/3d966f23aa317eacccb8da5e23713333 to your computer and use it in GitHub Desktop.

Select an option

Save gasman/3d966f23aa317eacccb8da5e23713333 to your computer and use it in GitHub Desktop.

Revisions

  1. gasman created this gist Aug 23, 2021.
    67 changes: 67 additions & 0 deletions ticroll.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    #!/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)