#!/bin/python

# Data format:
#
#   7 6 5 4 3 2 1 0
#   E V H R G B x x

bit_e = 1<<7 # end, reset counters
bit_v = 1<<6 # vsync
bit_h = 1<<5 # hsync
bit_r = 1<<4 # red
bit_g = 1<<3 # green
bit_b = 1<<2 # blue


# ROM size to fill
romsize = 32*1024

# Vertical timing measured in lines
num_v_visible = 480
num_v_frontporch = 10
num_v_sync = 2
num_v_backporch = 33

# Horizontal timing measured in 16-pixel blocks
num_h_visible = 40
num_h_frontporch = 1
num_h_sync = 6
num_h_backporch = 3

# sync polarity
vsync_polarity_pos = False
hsync_polarity_pos = False


# base value - no image, no sync, not end
basevalue = bit_e
if not vsync_polarity_pos:
    basevalue += bit_v
if not hsync_polarity_pos:
    basevalue += bit_h


def gen_line(vsync, imgsource):
    porchvalue = basevalue ^ (bit_v if vsync else 0)
    if imgsource:
        line = []
        for x in range(num_h_visible):
            line.append(porchvalue + next(imgsource))
    else:
        line = ([porchvalue] * num_h_visible)
    line.extend([porchvalue] * num_h_frontporch)
    line.extend([porchvalue ^ bit_h] * num_h_sync)
    line.extend([porchvalue] * num_h_backporch)
    return line


def imgsource_generator(filename):
    with open(filename,"rb") as f:
        for byte in f.read():
            yield byte & 31


imgsource = imgsource_generator("finch.img")

data = []
for line in range(num_v_visible):
    data.extend(gen_line(False, imgsource))
data.extend(num_v_frontporch * gen_line(False, None))
data.extend(num_v_sync * gen_line(True, None))
data.extend(num_v_backporch * gen_line(False, None))
data[-1] ^= bit_e

assert len(data) <= romsize

data.extend([basevalue ^ bit_e] * (romsize - len(data)))


with open("vgarom.rom", "wb") as fp:
    fp.write(bytearray(data))

