hdlc_tx_frm.py

download

#!/usr/bin/env python2.3
# -*- coding: Latin-1 -*-
#xpysource:/user/freepool/libgrafen/python/grafen/hdlc_tx_frm.py

"""Hilfe zu %(progname)s:
Aufgabe:
...

Sonstiges:
...

Aufruf:
(1) %(progname)s <fname1> [<fname2> ...]
(2) cat <fname> | %(progname)s
"""

__revision__ = "0.1"

S_ABORT_OUT = "S_ABORT_OUT"
S_EOF_OUT = "S_EOF_OUT"
S_TRANSFER = "S_TRANSFER"
S_INJECT_ZERO = "S_INJECT_ZERO"

INTERFILL_7e = "INTERFILL_7e"
INTERFILL_fe = "INTERFILL_fe"
INTERFILL_ff = "INTERFILL_ff"

class Counter(object):
def __init__(self):
self.val = 0
self.next_val = 0

def __cmp__(self, other):
return cmp(self.val, other)

def incr(self):
self.next_val = self.val + 1

def set(self, number):
self.next_val = number

def update(self):
self.val = self.next_val

def __repr__(self):
return 'Counter => val: %s next_val: %s' % (self.val, self.next_val)

class Data:
def __init__(self, ivec = ""):
if not ivec:
self.is_firstbit = 0
self.is_lastbit = 0
self.is_data_rdy = 0
self.Do_ser = "U"

else:
self.ivec = ivec = map(None, ivec)
self.is_firstbit = 1
self.is_data_rdy = 1
self.Do_ser = ivec[0]
del ivec[0]

if not ivec:
self.is_lastbit = 1

else:
self.is_lastbit = 0

def is_empty(self):
return self.Do_ser == "U"

def update(self):
ivec = self.ivec

self.is_firstbit = 0

if not ivec:
self.is_data_rdy = 0
self.Do_ser = "U"

else:
self.Do_ser = ivec[0]
del ivec[0]

if not ivec:
self.is_lastbit = 1

else:
self.is_lastbit = 0

class State_tx_frm:
def __init__(self):
self.state = S_ABORT_OUT

self.dout = '1'
self.request_di = 0

class Dummy:
pass

self.next = Dummy()
self.next.dout = '1'
self.next.request_di = '1'

def __call__(self):
state = next_state = self.state
dout = self.dout

Di_ser = data.Do_ser
is_firstbit = data.is_firstbit
is_data_rdy = data.is_data_rdy
is_lastbit = data.is_lastbit

if state == S_ABORT_OUT:
if cnt < 7:
dout = '1'
request_di = 0
cnt.incr()

elif cnt >= 7:
if interfill == INTERFILL_7e:
dout = '0'
request_di = 0
cnt.set(0)

next_state = S_EOF_OUT

else:
if is_data_rdy and \
is_firstbit:

dout = '0'
request_di = 0
cnt.set(0)

next_state = S_EOF_OUT

else:
if interfill == INTERFILL_fe:
dout = '0'
request_di = 0
cnt.set(1)

elif interfill == INTERFILL_ff:
dout = '1'
request_di = 0
cnt.set(7)

elif state == S_EOF_OUT:
if cnt < 6:
dout = '1'
request_di = 0
cnt.incr()

elif cnt == 6:
dout = '0'
request_di = 1
cnt.incr()

else:
if interfill == INTERFILL_7e:
if not is_data_rdy or \
not is_firstbit:

dout = '1'
request_di = 0
cnt.set(1)

else:
dout = Di_ser
request_di = 1
cnt.set(int(Di_ser))

next_state = S_TRANSFER

else: # INTERFILL_fe | INTERFILL_ff
if not is_data_rdy or \
not is_firstbit:

dout = '1'
request_di = 0
cnt.set(1)

else:
dout = Di_ser
request_di = 1
cnt.set(int(Di_ser))

next_state = S_TRANSFER

elif state == S_TRANSFER:
if not is_data_rdy or \
do_abort_frame:

dout = '1'
request_di = 0
cnt.set(1)

next_state = S_ABORT_OUT

else:
if not enable_zero_insertion:
dout = Di_ser
request_di = 0
cnt.set(1)

if cnt < 5:
if Di_ser == '1':
dout = Di_ser

if cnt == 4:
request_di = 0

else:
request_di = 1

cnt.incr()

else:
dout = Di_ser
request_di = 1
cnt.set(0)

if is_lastbit:
request_di = 0
cnt.set(0)

next_state = S_INJECT_ZERO

elif cnt == 5:
dout = '0'
request_di = 1

if Di_ser == '0':
cnt.set(6)

else:
assert Di_ser == '1'
cnt.set(7)

elif cnt == 6:
dout = Di_ser
request_di = 1
cnt.set(0)

if is_lastbit:
request_di = 0
cnt.set(0)

next_state = S_INJECT_ZERO

elif cnt == 7:
dout = Di_ser
request_di = 1
cnt.set(1)

if is_lastbit:
request_di = 0
cnt.set(0)

next_state = S_INJECT_ZERO

elif state == S_INJECT_ZERO:
raise notimplemented

self.next.request_di = request_di
self.next.dout = dout
self.next.state = next_state

def update(self):
self.request_di = self.next.request_di
self.dout = self.next.dout
self.state = self.next.state

def main(vector):

global interfill
global data, cnt, stm
global do_abort_frame, enable_zero_insertion

do_abort_frame = 0
enable_zero_insertion = 1

interfill = INTERFILL_7e

data = Data(vector)
cnt = Counter()
stm = State_tx_frm()

class NotFinished:
def __init__(self, n):
self.n = n
self.v = 0

def __call__(self):
self.v += 1
return self.v < (self.n+1)

not_finished = NotFinished(20)
while not data.is_empty() or not_finished():
stm()
print "%s %s%s%s : %s %s" % (data.Do_ser, data.is_firstbit,
data.is_lastbit, data.is_data_rdy,
stm.dout, stm.request_di),
print " ",
print "%-12s %s %s" % (stm.state, cnt.val, stm.next.state)

cnt.update ()
if stm.request_di:
data.update ()
stm.update ()

if __name__ == "__main__":
import sys, os

argv = sys.argv
arglen = len(argv)

progname = os.path.basename(argv[0])

def proghelp():
"Gives help information for this program"
print __doc__ % globals()
sys.exit(1)

if arglen == 1:
vector = sys.stdin.read()

main(vector)

elif "-h" in argv or "--help" in argv:
proghelp()

else:
for vector in argv[1:]:
main(vector)