#!/usr/bin/python
# Copyright (c) 2013 Quanta Research Cambridge, Inc.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import print_function
import sys
import json

boardfile = sys.argv[1]
pinoutfile = sys.argv[2]

pinstr = open(pinoutfile).read()
pinout = json.loads(pinstr)

boardInfo = json.loads(open(boardfile).read())

template='''\
set_property LOC "%(LOC)s" [get_ports "%(name)s"]
set_property IOSTANDARD "%(IOSTANDARD)s" [get_ports "%(name)s"]
set_property PIO_DIRECTION "%(PIO_DIRECTION)s" [get_ports "%(name)s"]
'''
for pin in pinout:
    pinInfo = pinout[pin]
    loc = 'TBD'
    iostandard = 'TBD'
    if pinInfo.has_key('fmc'):
        fmcPin = pinInfo['fmc']
        if boardInfo.has_key(fmcPin):
            loc = boardInfo[fmcPin]['LOC']
            iostandard = boardInfo[fmcPin]['IOSTANDARD']
        else:
            print('Missing pin description for', fmcPin, file=sys.stderr)
            loc = 'fmc.%s' % (fmcPin)
    print(template % {
        'name': pin,
        'LOC': loc,
        'IOSTANDARD': iostandard,
        'PIO_DIRECTION': pinInfo['PIO_DIRECTION']
        })


@@ -1550,16 +1550,101 @@
 s%22%5D%0A'''%0A
+setPropertyTemplate='''%5C%0Aset_property %25(prop)s %22%25(val)s%22 %5Bget_ports %22%25(name)s%22%5D%0A'''%0A%0A
 for pin 
@@ -2046,16 +2046,105 @@
 fmcPin)%0A
+        if pinInfo.has_key('IOSTANDARD'):%0A            iostandard = pinInfo%5B'IOSTANDARD'%5D%0A
     prin
@@ -2293,10 +2293,246 @@
       %7D)
+%0A    for k in pinInfo:%0A        if k in %5B'fmc', 'IOSTANDARD', 'PIO_DIRECTION'%5D: continue%0A        print (setPropertyTemplate %25 %7B%0A                'name': pin,%0A                'prop': k,%0A                'val': pinInfo%5Bk%5D,%0A                %7D)
 %0A%0A

Update generate-constraints.py to allow other properties