#!/usr/bin/python
#===============================================================================
# CERN (BE-CO-HT)
# Concatenate two bitstreams
#===============================================================================
# author: Theodor Stana (t.stana@cern.ch)
#
# date of creation:
#
# version: 1.0
#
# description:
#   This script takes two bitstreams as input and generates a combined bitstream
#   for MultiBoot purposes that can be directly downloaded into the flash chip.
#
#===============================================================================
# GNU LESSER GENERAL PUBLIC LICENSE
#===============================================================================
# This source file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 2.1 of the License, or (at your
# option) any later version. This source is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details. You should have
# received a copy of the GNU Lesser General Public License along with this
# source; if not, download it from http://www.gnu.org/licenses/lgpl-2.1.html
#===============================================================================
# last changes:
#===============================================================================
#  TODO: - 
#===============================================================================

import sys
import subprocess

if __name__ == "__main__":
    # Ask for type of output
    print("How should the program run?")
    print("(1) .bin + .bin -> .bin")
    print("(2) .bit + .bit -> .mcs")

    o = raw_input("Select (1)/(2): ")
    if (o != '1') and (o != '2'):
        print ("ERROR -- Please select a valid input!")
        sys.exit(1)

    if (o == '1'):
        # open files
        fn1 = raw_input("file 1  (.bin): ")
        fn2 = raw_input("file 2  (.bin): ")
        fn3 = raw_input("outfile (.bin): ")
        fn1f = fn1.split('.')[-1]
        fn2f = fn2.split('.')[-1]
        fn3f = fn3.split('.')[-1]
        if (fn1f != "bin") or (fn2f != "bin") or (fn3f != "bin"):
            print("ERROR -- Wrong binary file format!")
            print("expected : .bin/.bin/.bin")
            print("got      : .%s/.%s/.%s" % (fn1f, fn2f, fn3f))
            sys.exit(1)
        f1 = open(fn1,'rb')
        f2 = open(fn2,'rb')
        f3 = open(fn3,'wb')
        a1 = []
        a2 = []

        # read contents
        a1 = f1.read()
        a2 = f2.read()

        # get sector boundary and fill with zeroes up to next sector
        lt = 0x10000 - (len(a1) % 0x10000)
        for i in range(0, lt):
          a1 += '\0'

        # write output files
        f3.write(a1+a2)

        print("SUCCESS -- Output written to " + fn3)
        f1.close()
        f2.close()
        f3.close()

    elif (o == '2'):
        fn1 = raw_input("file 1  (.bit): ")
        fn2 = raw_input("file 2  (.bit): ")
        fn3 = raw_input("outfile (.mcs): ")
        fn1f = fn1.split('.')[-1]
        fn2f = fn2.split('.')[-1]
        fn3f = fn3.split('.')[-1]
        if (fn1f != "bit") or (fn2f != "bit") or (fn3f != "mcs"):
            print("ERROR -- Wrong binary file format!")
            print("expected : .bit/.bit/.mcs")
            print("got      : .%s/.%s/.%s" % (fn1f, fn2f, fn3f))
            sys.exit(1)
        s = raw_input("flash size (KB): ")
        a1 = raw_input("bistream 1 start address: 0x")
        a2 = raw_input("bistream 2 start address: 0x")
        err = subprocess.call("promgen -w -p mcs -spi -s " + s + " -u " + a1 + " " + fn1 + " -u " + a2 + " " + fn2 + " -o " + fn3, shell=True)

        if (not err):
            print("SUCCESS -- Output written to " + fn3)

