from magma import *

from mantle.xilinx.zynq import PS7
from ..spartan.spartan import Spartan
from ..gpio import GPIO, Pin
from .clock import Clock

class Zynq(Spartan):
    family = 'zynq'

    def __init__(self, name, board, speed):
        Spartan.__init__(self, name, board, speed)

        
    def CLG484(self):
        self.package = 'CLG484'
        Pin(self, "Pin")
        Pin(self, "T12")
        Pin(self, "N11")
        Pin(self, "K12")
        Pin(self, "K11")
        Pin(self, "M11")
        Pin(self, "M12")
        Pin(self, "G9")
        Pin(self, "G11")
        Pin(self, "N12")
        Pin(self, "L12")
        Pin(self, "L11")
        Pin(self, "G10")
        Pin(self, "T10")
        Pin(self, "T8")
        Pin(self, "T14")
        Pin(self, "H13")
        Pin(self, "G14")
        Pin(self, "T7")
        Pin(self, "T13")
        Pin(self, "T11")
        Pin(self, "G12")
        GPIO(self, "R7")
        GPIO(self, "V10")
        GPIO(self, "V9")
        GPIO(self, "V8")
        GPIO(self, "W8")
        GPIO(self, "W11")
        GPIO(self, "W10")
        GPIO(self, "V12")
        GPIO(self, "W12")
        GPIO(self, "U12")
        GPIO(self, "U11")
        GPIO(self, "U10")
        GPIO(self, "U9")
        GPIO(self, "AA12")
        GPIO(self, "AB12")
        GPIO(self, "AA11")
        GPIO(self, "AB11")
        GPIO(self, "AB10")
        GPIO(self, "AB9")
        GPIO(self, "Y11")
        GPIO(self, "Y10")
        GPIO(self, "AA9")
        GPIO(self, "AA8")
        GPIO(self, "Y9")
        GPIO(self, "Y8")
        GPIO(self, "Y6")
        GPIO(self, "Y5")
        GPIO(self, "AA7")
        GPIO(self, "AA6")
        GPIO(self, "AB2")
        GPIO(self, "AB1")
        GPIO(self, "AB5")
        GPIO(self, "AB4")
        GPIO(self, "AB7")
        GPIO(self, "AB6")
        GPIO(self, "Y4")
        GPIO(self, "AA4")
        GPIO(self, "R6")
        GPIO(self, "T6")
        GPIO(self, "T4")
        GPIO(self, "U4")
        GPIO(self, "V5")
        GPIO(self, "V4")
        GPIO(self, "U6")
        GPIO(self, "U5")
        GPIO(self, "V7")
        GPIO(self, "W7")
        GPIO(self, "W6")
        GPIO(self, "W5")
        GPIO(self, "U7")
        GPIO(self, "U19")
        GPIO(self, "T21")
        GPIO(self, "U21")
        GPIO(self, "T22")
        GPIO(self, "U22")
        GPIO(self, "V22")
        GPIO(self, "W22")
        GPIO(self, "W20")
        GPIO(self, "W21")
        GPIO(self, "U20")
        GPIO(self, "V20")
        GPIO(self, "V18")
        GPIO(self, "V19")
        GPIO(self, "AA22")
        GPIO(self, "AB22")
        GPIO(self, "AA21")
        GPIO(self, "AB21")
        GPIO(self, "Y20")
        GPIO(self, "Y21")
        GPIO(self, "AB19")
        GPIO(self, "AB20")
        GPIO(self, "Y19")
        GPIO(self, "AA19")
        GPIO(self, "Y18")
        GPIO(self, "AA18")
        GPIO(self, "W17")
        GPIO(self, "W18")
        GPIO(self, "W16")
        GPIO(self, "Y16")
        GPIO(self, "U15")
        GPIO(self, "U16")
        GPIO(self, "U17")
        GPIO(self, "V17")
        GPIO(self, "AA17")
        GPIO(self, "AB17")
        GPIO(self, "AA16")
        GPIO(self, "AB16")
        GPIO(self, "V14")
        GPIO(self, "V15")
        GPIO(self, "V13")
        GPIO(self, "W13")
        GPIO(self, "W15")
        GPIO(self, "Y15")
        GPIO(self, "Y14")
        GPIO(self, "AA14")
        GPIO(self, "Y13")
        GPIO(self, "AA13")
        GPIO(self, "AB14")
        GPIO(self, "AB15")
        GPIO(self, "U14")
        GPIO(self, "H15")
        GPIO(self, "J15")
        GPIO(self, "K15")
        GPIO(self, "J16")
        GPIO(self, "J17")
        GPIO(self, "K16")
        GPIO(self, "L16")
        GPIO(self, "L17")
        GPIO(self, "M17")
        GPIO(self, "N17")
        GPIO(self, "N18")
        GPIO(self, "M15")
        GPIO(self, "M16")
        GPIO(self, "J18")
        GPIO(self, "K18")
        GPIO(self, "J21")
        GPIO(self, "J22")
        GPIO(self, "J20")
        GPIO(self, "K21")
        GPIO(self, "L21")
        GPIO(self, "L22")
        GPIO(self, "K19")
        GPIO(self, "K20")
        GPIO(self, "L18")
        GPIO(self, "L19")
        GPIO(self, "M19")
        GPIO(self, "M20")
        GPIO(self, "N19")
        GPIO(self, "N20")
        GPIO(self, "M21")
        GPIO(self, "M22")
        GPIO(self, "N22")
        GPIO(self, "P22")
        GPIO(self, "R20")
        GPIO(self, "R21")
        GPIO(self, "P20")
        GPIO(self, "P21")
        GPIO(self, "N15")
        GPIO(self, "P15")
        GPIO(self, "P17")
        GPIO(self, "P18")
        GPIO(self, "T16")
        GPIO(self, "T17")
        GPIO(self, "R19")
        GPIO(self, "T19")
        GPIO(self, "R18")
        GPIO(self, "T18")
        GPIO(self, "P16")
        GPIO(self, "R16")
        GPIO(self, "R15")
        GPIO(self, "H17")
        GPIO(self, "F16")
        GPIO(self, "E16")
        GPIO(self, "D16")
        GPIO(self, "D17")
        GPIO(self, "E15")
        GPIO(self, "D15")
        GPIO(self, "G15")
        GPIO(self, "G16")
        GPIO(self, "F18")
        GPIO(self, "E18")
        GPIO(self, "G17")
        GPIO(self, "F17")
        GPIO(self, "C15")
        GPIO(self, "B15")
        GPIO(self, "B16")
        GPIO(self, "B17")
        GPIO(self, "A16")
        GPIO(self, "A17")
        GPIO(self, "A18")
        GPIO(self, "A19")
        GPIO(self, "C17")
        GPIO(self, "C18")
        GPIO(self, "D18")
        GPIO(self, "C19")
        GPIO(self, "B19")
        GPIO(self, "B20")
        GPIO(self, "D20")
        GPIO(self, "C20")
        GPIO(self, "A21")
        GPIO(self, "A22")
        GPIO(self, "D22")
        GPIO(self, "C22")
        GPIO(self, "E21")
        GPIO(self, "D21")
        GPIO(self, "B21")
        GPIO(self, "B22")
        GPIO(self, "H19")
        GPIO(self, "H20")
        GPIO(self, "G19")
        GPIO(self, "F19")
        GPIO(self, "E19")
        GPIO(self, "E20")
        GPIO(self, "G20")
        GPIO(self, "G21")
        GPIO(self, "F21")
        GPIO(self, "F22")
        GPIO(self, "H22")
        GPIO(self, "G22")
        GPIO(self, "H18")
        GPIO(self, "F7")
        GPIO(self, "F8")
        GPIO(self, "B5")
        GPIO(self, "E6")
        GPIO(self, "E9")
        GPIO(self, "E10")
        GPIO(self, "F11")
        GPIO(self, "E11")
        GPIO(self, "F12")
        GPIO(self, "D7")
        GPIO(self, "E8")
        GPIO(self, "F9")
        GPIO(self, "G13")
        GPIO(self, "F14")
        GPIO(self, "F13")
        GPIO(self, "E14")
        GPIO(self, "D8")
        GPIO(self, "E13")
        GPIO(self, "D12")
        GPIO(self, "D11")
        GPIO(self, "D13")
        GPIO(self, "D10")
        GPIO(self, "C9")
        GPIO(self, "B6")
        GPIO(self, "D6")
        GPIO(self, "A7")
        GPIO(self, "A8")
        GPIO(self, "A14")
        GPIO(self, "B7")
        GPIO(self, "A13")
        GPIO(self, "A12")
        GPIO(self, "A11")
        GPIO(self, "C7")
        GPIO(self, "B12")
        GPIO(self, "A9")
        GPIO(self, "B14")
        GPIO(self, "C13")
        GPIO(self, "C8")
        GPIO(self, "B11")
        GPIO(self, "B9")
        GPIO(self, "B10")
        GPIO(self, "C14")
        GPIO(self, "C10")
        GPIO(self, "C12")
        GPIO(self, "A6")
        GPIO(self, "C5")
        GPIO(self, "B4")
        GPIO(self, "G7")
        GPIO(self, "C4")
        GPIO(self, "E5")
        GPIO(self, "D5")
        GPIO(self, "A4")
        GPIO(self, "A3")
        GPIO(self, "E4")
        GPIO(self, "F6")
        GPIO(self, "A2")
        GPIO(self, "A1")
        GPIO(self, "G6")
        GPIO(self, "F3")
        GPIO(self, "D1")
        GPIO(self, "C3")
        GPIO(self, "B2")
        GPIO(self, "D3")
        GPIO(self, "B1")
        GPIO(self, "C2")
        GPIO(self, "D2")
        GPIO(self, "E3")
        GPIO(self, "E1")
        GPIO(self, "F2")
        GPIO(self, "F1")
        GPIO(self, "G2")
        GPIO(self, "G1")
        GPIO(self, "L1")
        GPIO(self, "L2")
        GPIO(self, "H3")
        GPIO(self, "H2")
        GPIO(self, "J2")
        GPIO(self, "L3")
        GPIO(self, "K1")
        GPIO(self, "J1")
        GPIO(self, "K3")
        GPIO(self, "G4")
        GPIO(self, "F4")
        GPIO(self, "H4")
        GPIO(self, "G5")
        GPIO(self, "J3")
        GPIO(self, "H5")
        GPIO(self, "J5")
        GPIO(self, "J6")
        GPIO(self, "J7")
        GPIO(self, "K5")
        GPIO(self, "K6")
        GPIO(self, "L4")
        GPIO(self, "M7")
        GPIO(self, "N7")
        GPIO(self, "N4")
        GPIO(self, "N5")
        GPIO(self, "K4")
        GPIO(self, "M5")
        GPIO(self, "M4")
        GPIO(self, "M6")
        GPIO(self, "L6")
        GPIO(self, "L7")
        GPIO(self, "P5")
        GPIO(self, "P6")
        GPIO(self, "V3")
        GPIO(self, "R4")
        GPIO(self, "P3")
        GPIO(self, "R5")
        GPIO(self, "M1")
        GPIO(self, "T3")
        GPIO(self, "N3")
        GPIO(self, "T1")
        GPIO(self, "P1")
        GPIO(self, "N2")
        GPIO(self, "P2")
        GPIO(self, "R3")
        GPIO(self, "T2")
        GPIO(self, "M2")
        GPIO(self, "R1")
        GPIO(self, "AA3")
        GPIO(self, "U1")
        GPIO(self, "AA1")
        GPIO(self, "U2")
        GPIO(self, "AA2")
        GPIO(self, "V2")
        GPIO(self, "W2")
        GPIO(self, "W1")
        GPIO(self, "Y3")
        GPIO(self, "W3")
        GPIO(self, "Y1")
        Pin(self, "A5")
        Pin(self, "A15")
        Pin(self, "AA5")
        Pin(self, "AA15")
        Pin(self, "AB8")
        Pin(self, "AB18")
        Pin(self, "B8")
        Pin(self, "B18")
        Pin(self, "C1")
        Pin(self, "C11")
        Pin(self, "C21")
        Pin(self, "D4")
        Pin(self, "D14")
        Pin(self, "E7")
        Pin(self, "E17")
        Pin(self, "F10")
        Pin(self, "F20")
        Pin(self, "G3")
        Pin(self, "H6")
        Pin(self, "H8")
        Pin(self, "H12")
        Pin(self, "H14")
        Pin(self, "H16")
        Pin(self, "J9")
        Pin(self, "J11")
        Pin(self, "J13")
        Pin(self, "J19")
        Pin(self, "K2")
        Pin(self, "K8")
        Pin(self, "K10")
        Pin(self, "K14")
        Pin(self, "K22")
        Pin(self, "L5")
        Pin(self, "L9")
        Pin(self, "L13")
        Pin(self, "L15")
        Pin(self, "M8")
        Pin(self, "M10")
        Pin(self, "M14")
        Pin(self, "M18")
        Pin(self, "N1")
        Pin(self, "N9")
        Pin(self, "N13")
        Pin(self, "N21")
        Pin(self, "P4")
        Pin(self, "P8")
        Pin(self, "P10")
        Pin(self, "P12")
        Pin(self, "P14")
        Pin(self, "R9")
        Pin(self, "R11")
        Pin(self, "R13")
        Pin(self, "R17")
        Pin(self, "T20")
        Pin(self, "U3")
        Pin(self, "U13")
        Pin(self, "V6")
        Pin(self, "V16")
        Pin(self, "W9")
        Pin(self, "W19")
        Pin(self, "Y2")
        Pin(self, "Y12")
        Pin(self, "Y22")
        Pin(self, "J12")
        Pin(self, "J14")
        Pin(self, "K13")
        Pin(self, "L14")
        Pin(self, "M13")
        Pin(self, "N14")
        Pin(self, "P13")
        Pin(self, "R14")
        Pin(self, "L10")
        Pin(self, "N10")
        Pin(self, "P11")
        Pin(self, "R10")
        Pin(self, "R12")
        Pin(self, "AA10")
        Pin(self, "AB3")
        Pin(self, "T5")
        Pin(self, "U8")
        Pin(self, "V11")
        Pin(self, "W4")
        Pin(self, "Y7")
        Pin(self, "AA20")
        Pin(self, "AB13")
        Pin(self, "U18")
        Pin(self, "V21")
        Pin(self, "W14")
        Pin(self, "Y17")
        Pin(self, "K17")
        Pin(self, "L20")
        Pin(self, "N16")
        Pin(self, "P19")
        Pin(self, "R22")
        Pin(self, "T15")
        Pin(self, "A20")
        Pin(self, "C16")
        Pin(self, "D19")
        Pin(self, "E22")
        Pin(self, "F15")
        Pin(self, "G18")
        Pin(self, "H21")
        Pin(self, "H11")
        Pin(self, "J10")
        Pin(self, "E2")
        Pin(self, "F5")
        Pin(self, "H1")
        Pin(self, "J4")
        Pin(self, "K7")
        Pin(self, "M3")
        Pin(self, "N6")
        Pin(self, "R2")
        Pin(self, "V1")
        Pin(self, "H10")
        Pin(self, "K9")
        Pin(self, "M9")
        Pin(self, "P9")
        Pin(self, "T9")
        Pin(self, "G8")
        Pin(self, "H9")
        Pin(self, "J8")
        Pin(self, "L8")
        Pin(self, "N8")
        Pin(self, "R8")
        Pin(self, "B3")
        Pin(self, "C6")
        Pin(self, "A10")
        Pin(self, "B13")
        Pin(self, "D9")
        Pin(self, "E12")
        GPIO(self, "H7")
        GPIO(self, "P7")

class XC7Z020(Zynq):
    part = 'xc7z020'

    def __init__(self, name='', board=None, package='clg484', speed='-1'):
        assert package in ['clg484']
        Zynq.__init__(self, name, board, speed)
        if package == 'clg484':
            self.CLG484()
        
        #TODO Import PS7 peripheral nicely!
        self.ps7 = PS7(self)
        self.ps7.on()

        self.clock = Clock(self)

if __name__ == "__main__":
    fpga = XC7Z020()





#        s = "SLOW"
#        f = "FAST"
#        pinmap = [
#                {'NET':'MIO','DRIVE':"8",'IOSTANDARD':'LVCMOS18','LOC':["C12", "D10", "C10", "D13", "C14", "D11", "B10", "D12", "B9", "E13", "B11", "D8", "C8", "E14", "C13", "F13", "B14", "A9", "F14", "B12", "G13", "C7", "F9", "A11", "E8", "A12", "D7", "A13", "F12", "B7", "E11", "A14", "F11", "A8", "E10", "A7", "E9", "D6", "E6", "B6", "A6", "C5", "B4", "G7", "C4", "E5", "D5", "A4", "A3", "E4", "F6", "A2", "A1", "G6"], 'SLEW':[s,s,s,s,s,s,s,s,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,s,s,s,s,s,s,s,f,s,f,f,f,f,f,f,s]},
#                {'NET':"DDR_WEB", 'SLEW':s,  'IOSTANDARD':'SSTL15', 'LOC':"R4"},
#                {'NET':"DDR_VRP", 'SLEW':f,  'IOSTANDARD':'SSTL15_T_DCI', 'LOC':"N7"},
#                {'NET':"DDR_VRN", 'SLEW':f 'IOSTANDARD':'SSTL15_T_DCI', 'LOC':"M7"},
#                {'NET':"DDR_RAS_n", 'SLEW':s 'IOSTANDARD':'SSTL15', 'LOC':"R5"},
#                {'NET':"DDR_ODT", 'SLEW':s 'IOSTANDARD':'SSTL15', 'LOC':"P5"},
#                {'NET':"DDR_DRSTB", 'SLEW':f 'IOSTANDARD':'SSTL15', 'LOC':"F3"},
#                {'NET':'DDR_DQS', 'LOC':["V2", "N2","H2", "C2"],
#                {'NET':'DDR_DQS_n', 'LOC':["W2", "P2", "J2", "D2"],
#                {'NET':'DDR_DQ', 'LOC':["Y1", "W3", "Y3", "W1", "U2", "AA1", "U1", "AA3", "R1", "M2", "T2", "R3", "T1", "N3", "T3", "M1", "K3", "J1", "K1", "L3", "L2", "L1", "G1", "G2", "F1", "F2", "E1", "E3", "D3", "B2", "C3", "D1"],
#                {'NET':'DDR_DM', 'LOC':["AA2", "P1", "H3", "B1"],
#                {'NET':'DDR_CS_n', 'LOC':"P6",
#                {'NET':'DDR_CKE', 'LOC':"V3",
#                {'NET':'DDR_Clk', 'LOC':"N4",
#                {'NET':'DDR_Clk_n, 'LOC':"N5",
#                {'NET':'DDR_CAS_n', 'LOC':"P3",
#                {'NET':'DDR_ 'LOC':["M6", "L6", "L7"],
#                {'NET':'DDR_ 'LOC':["G4", "F4", "H4", "G5", "J3", "H5", "J5", "J6", "J7", "K5", "K6", "L4", "K4", "M5", "M4"],
#                {'NET': 'LOC':"B5", 
#                {'NET': 'LOC':"C9",
#                {'NET': 'LOC':"F7"
#            ]
 
