
# Look for rising and falling edges in 8-bit vector.
# Print out in form suitable for initializing VHDL array...

# from bitstring import BitArray


firstRisingEdge = []
lastFallingEdge = []
multipleEdgesPresent = []
risingEdgePresent = []
fallingEdgePresent = []

for value in range(0,512):
    print hex(value)

    # Initialize array and get zero'th element
    bitSetArray = []
    bitSet = ( 1 & value)
    bitSetArray.append(bitSet)

    risingEdges = []
    fallingEdges = []
    
    for bit in range (1,9):
        bitSet = (( 1 << bit ) & value) >> bit
        # print bitSet
        bitSetArray.append(bitSet)
        if (bitSetArray[bit-1] ==0) and (bitSetArray[bit] == 1):
            print "Rising edge found"
            risingEdges.append(bit)
        if (bitSetArray[bit-1] ==1) and (bitSetArray[bit] == 0):
            print "Falling edge found"
            fallingEdges.append(bit)

    if len(risingEdges) != 0:
        firstRisingEdge.append(risingEdges[0] - 1)
        risingEdgePresent.append(1)
    else:
        firstRisingEdge.append(0)
        risingEdgePresent.append(0)

    if len(fallingEdges) != 0:
        lastFallingEdge.append(fallingEdges[len(fallingEdges)-1] -1 )
        fallingEdgePresent.append(1)
    else:
        lastFallingEdge.append(0)
        fallingEdgePresent.append(0)

    if (len(risingEdges) >1 ) or (len(risingEdges) >1 ):
        multipleEdgesPresent.append(1)
    else:
        multipleEdgesPresent.append(0)


    print bitSetArray , risingEdges , fallingEdges
        
print firstRisingEdge
print risingEdgePresent

print

print lastFallingEdge
print fallingEdgePresent

print

print multipleEdgesPresent

# Rising and falling edge times encoded as a LUT. Contents:
# MRFrrrfff
# M = multiple edges present ( more then one rising or falling edge)
# R = at least one rising edge present
# F = at least one falling edge present.

for value in range(0,512):

    if not (value % 8):
        # print newline every eight values.
        print

    word = lastFallingEdge[value] + (firstRisingEdge[value]<<3) + (fallingEdgePresent[value]<<6) + (risingEdgePresent[value]<<7) + (multipleEdgesPresent[value]<<8)

#    print "last falling edge" , hex(lastFallingEdge[value])
#    print "first rising edge" , hex(firstRisingEdge[value])
#    print "multi edges?" , multipleEdgesPresent[value]

    bitString = ""

    for bit in range (0,9):
        bitSet = (( 1 << bit ) & word) >> bit
        if bitSet:
            bitString = "1" + bitString
        else:
            bitString = "0" + bitString

#    print hex(value)
#    print hex(word)
    print ("\"" + bitString + "\","),


#    print

    
#    valBits = BitArray(int = value, length = 8)
#    print BitArray

# Find the first rising edge in time and the last falling edge...

