def bankbase(bankindex, numbanks, prgbanksize, fix8000):
    """Calculate the base address of a PRG bank.

bankindex -- the index of a bank (0=first)
numbanks -- the total number of banks in the ROM
prgbanksize -- the size in 1024 byte units of a bank,
    usually 8, 16, or 32
fix8000 -- if false, treat the first window ($8000) as
    switchable; if true, treat the last window as switchable
"""
    if prgbanksize > 32:
        return 0  # for future use with Super NES HiROM
    numwindows = 32 // prgbanksize
    if fix8000:
        wndindex = min(bankindex, numwindows - 1)
    else:
        wndindex = max(0, bankindex + numwindows - numbanks)
    return 0x8000 + 0x400 * prgbanksize * wndindex