# Path: tests/packer_coef_calc.py
# class JesdPackerAlgo:
#     def __init__(self, dtype = None, M=1, N=8, S=1, CS=0, CF=0, L=1, F=1, HD=0):
#         
#         self.N = N
#         self.S = S
#         self.M = M
#         self.CS = CS
#         self.CF = CF
#         self.F = F
#         self.L = L
#         self.HD = HD
#         self.dtype = dtype
#     
#     def form_words(self, samples):
#         words = []
#         if self.CF == 0:
#             for s in samples:
#                 words.append(s[0] % s[1])
#         else:
#             control_word = self.dtype(0)()
#             for s in samples:
#                 words.append(s[0])
#                 control_word %= s[1]
#             
#             words.append(control_word)
#         
#         return words
#     
#     def form_nibble_groups(self, words):
#         nibble_groups = []
#         for w in words:
#             ng = []
#             upper = len(w)
#             while upper >= 4:
#                 ng.append(w[upper-4:upper-1])
#                 upper -= 4;
#                 
#             if upper != 0:
#                 ng.append(w[0:upper-1] % self.dtype(4-upper)(0))
#                 
#             nibble_groups.append(ng)
#                 
#         return nibble_groups
#     
#     def form_lane_groups(self, ng):
#         lg_nibbles = []
#         start = 0
#         if self.HD == 0:
#             start = 0
#             for i in range(self.L):
#                 lane_ng = []
#                 while (len(lane_ng) < self.F*2) and (start < len(ng)):
#                     if len(lane_ng) + len(ng[start]) <= self.F*2:
#                         lane_ng.extend(ng[start])
#                         start += 1
#                     else:
#                         break
#                     
#                 for i in range(len(lane_ng), self.F*2):
#                     lane_ng.append(self.dtype(4)(0))
#                             
#                 lg_nibbles.append(lane_ng)
#         else:
#             start = 0
#             nibbles = []
#             for n in ng:
#                 nibbles.extend(n)
#             
#             for i in range(self.L):
#                 if (start + self.F*2) <= len(nibbles):
#                     lane_ng = nibbles[start:start+self.F*2]
#                 else:
#                     lane_ng = nibbles[start:]
#                     for i in range(len(lane_ng), self.F*2):
#                         lane_ng.append(self.dtype(4)(0))
#                 
#                 start += self.F*2       
#                 lg_nibbles.append(lane_ng)
#         
#         lg = []
#         for l in lg_nibbles:
#             lane = []
#             for i in range(0,len(l),2):
#                 lane.append(l[i] % l[i+1])
#         
#             lg.append(lane)
# 
#         return lg
#     
#     def pack(self, samples):
#         words = self.form_words(samples)
#         print('Words: ', words)
#         ng = self.form_nibble_groups(words)
#         print('NG: ', ng)
#         frame = self.form_lane_groups(ng)
#         
#         print()
#         print('Output Frame:')
#         print()
#         for l in frame:
#             print(l)
#             
#         return frame


import sydpy
from tests.packer_coef_calc import JesdPackerAlgo
        return str(self.val)

    def __repr__(self):
        return repr(self.val)

    def __getitem__(self, key):
        if isinstance( key, slice ) :
            high = max(key.start, key.stop)
            low = min(key.start, key.stop)
        elif isinstance( key, int ) :
            high = low = int(key)
        else:
            raise TypeError("Invalid argument type.")
        
        return SymbolicBit(high-low+1)(val = self.val[low:(high+1)])

class PackerTlMatrix(sydpy.Component, JesdPackerAlgo):
    @sydpy.compinit
    def __init__(self, name, parent, ch_samples, tSample = None, N=8, S=1, CS=0, CF=0, L=1, F=1, HD=0, **kwargs):
        sydpy.Component.__init__(self, name, parent)
        dtype = SymbolicBit
        M = len(ch_samples)
        JesdPackerAlgo.__init__(self, dtype=dtype, M=M, N=N, S=S, CS=CS, CF=CF, L=L, F=F, HD=HD)
    
        sym_samples = []
        for i in range(M):
            sym_samples.append((dtype(N)([(i, 0, j) for j in range(N)]), 
                            dtype(CS)([(i, 1, j) for j in range(CS)])))    
    
        print('Samples: ', sym_samples)

# Path: tests/packer_coef_calc.py
# class JesdPackerAlgo:
#     def __init__(self, dtype = None, M=1, N=8, S=1, CS=0, CF=0, L=1, F=1, HD=0):
#         
#         self.N = N
#         self.S = S
#         self.M = M
#         self.CS = CS
#         self.CF = CF
#         self.F = F
#         self.L = L
#         self.HD = HD
#         self.dtype = dtype
#     
#     def form_words(self, samples):
#         words = []
#         if self.CF == 0:
#             for s in samples:
#                 words.append(s[0] % s[1])
#         else:
#             control_word = self.dtype(0)()
#             for s in samples:
#                 words.append(s[0])
#                 control_word %= s[1]
#             
#             words.append(control_word)
#         
#         return words
#     
#     def form_nibble_groups(self, words):
#         nibble_groups = []
#         for w in words:
#             ng = []
#             upper = len(w)
#             while upper >= 4:
#                 ng.append(w[upper-4:upper-1])
#                 upper -= 4;
#                 
#             if upper != 0:
#                 ng.append(w[0:upper-1] % self.dtype(4-upper)(0))
#                 
#             nibble_groups.append(ng)
#                 
#         return nibble_groups
#     
#     def form_lane_groups(self, ng):
#         lg_nibbles = []
#         start = 0
#         if self.HD == 0:
#             start = 0
#             for i in range(self.L):
#                 lane_ng = []
#                 while (len(lane_ng) < self.F*2) and (start < len(ng)):
#                     if len(lane_ng) + len(ng[start]) <= self.F*2:
#                         lane_ng.extend(ng[start])
#                         start += 1
#                     else:
#                         break
#                     
#                 for i in range(len(lane_ng), self.F*2):
#                     lane_ng.append(self.dtype(4)(0))
#                             
#                 lg_nibbles.append(lane_ng)
#         else:
#             start = 0
#             nibbles = []
#             for n in ng:
#                 nibbles.extend(n)
#             
#             for i in range(self.L):
#                 if (start + self.F*2) <= len(nibbles):
#                     lane_ng = nibbles[start:start+self.F*2]
#                 else:
#                     lane_ng = nibbles[start:]
#                     for i in range(len(lane_ng), self.F*2):
#                         lane_ng.append(self.dtype(4)(0))
#                 
#                 start += self.F*2       
#                 lg_nibbles.append(lane_ng)
#         
#         lg = []
#         for l in lg_nibbles:
#             lane = []
#             for i in range(0,len(l),2):
#                 lane.append(l[i] % l[i+1])
#         
#             lg.append(lane)
# 
#         return lg
#     
#     def pack(self, samples):
#         words = self.form_words(samples)
#         print('Words: ', words)
#         ng = self.form_nibble_groups(words)
#         print('NG: ', ng)
#         frame = self.form_lane_groups(ng)
#         
#         print()
#         print('Output Frame:')
#         print()
#         for l in frame:
#             print(l)
#             
#         return frame


# Path: tests/packer_tl_matrix.py
import sydpy
from tests.packer_coef_calc import JesdPackerAlgo

        return str(self.val)

    def __repr__(self):
        return repr(self.val)

    def __getitem__(self, key):
        if isinstance( key, slice ) :
            high = max(key.start, key.stop)
            low = min(key.start, key.stop)
        elif isinstance( key, int ) :
            high = low = int(key)
        else:
            raise TypeError("Invalid argument type.")
        
        return SymbolicBit(high-low+1)(val = self.val[low:(high+1)])

class PackerTlMatrix(sydpy.Component, JesdPackerAlgo):
    @sydpy.compinit
    def __init__(self, name, parent, ch_samples, tSample = None, N=8, S=1, CS=0, CF=0, L=1, F=1, HD=0, **kwargs):
        sydpy.Component.__init__(self, name, parent)
        dtype = SymbolicBit
        M = len(ch_samples)
        JesdPackerAlgo.__init__(self, dtype=dtype, M=M, N=N, S=S, CS=CS, CF=CF, L=L, F=F, HD=HD)
    
        sym_samples = []
        for i in range(M):
            sym_samples.append((dtype(N)([(i, 0, j) for j in range(N)]), 
                            dtype(CS)([(i, 1, j) for j in range(CS)])))    
    
        print('Samples: ', sym_samples)

        self.pack_m = JesdPackerAlgo.pack(self, sym_samples)