Files
eb001/tb/shinTest/gen_pkt.py

157 lines
4.6 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python
#encoding=utf-8
import random
class GenPkt(object):
def __init__(self):
super(GenPkt, self).__init__()
self.pkt_buf = bytearray(b'\0' * 2048)
self.pkt_type = 0
self.pkt_sub_pkt = 0
self.sub_pkt = list()
def clean(self):
self.pkt_buf = bytearray(b'\0' * 2048)
self.pkt_type = 0
self.pkt_sub_pkt = 0
self.sub_pkt = list()
def setPktType(self, type):
if (type != 0) and (type != 1):
print('Error...set type failed:', type)
else:
self.pkt_type = type
def addSubPkt(self, dev_idx, ptype, addr, cmd, len, data):
if (len > 2032):
print('Error, bad subpkt len:%d', len)
return
if not (ptype in [0, 2]):
print('Error, bad subpkt type:', ptype)
return
if not (cmd in [0, 1, 2, 0xb, 0xc, 0xd, 0xe, 0xf]):
print('Error, bad cmd:', cmd)
return
self.pkt_sub_pkt = self.pkt_sub_pkt + 1
self.sub_pkt.append([dev_idx, ptype, addr, cmd, len, data])
def gen(self, filename="pkt_verilog_test.v", filetype=0):
pkt_len = 4
for sp in self.sub_pkt:
pkt_len = pkt_len + sp[4] + 8
if pkt_len > 2048:
print('Error, bad pkt_len:', pkt_len)
return
print("pkt_len:%d, data_len:%d"%(pkt_len, pkt_len - 4))
i = 4
if self.pkt_type == 1:
pkt_len = 0
self.pkt_buf[0] = 0
self.pkt_buf[1] = (self.pkt_type & 0x3)
self.pkt_buf[2] = 0x3f
self.pkt_buf[3] = 0xff
else:
#for i in range(pkt_len):
self.pkt_buf[0] = ((pkt_len - 4) >> 4) & 0xff
self.pkt_buf[1] = (((pkt_len - 4) << 4) & 0xf0) | (self.pkt_type & 0x3)
self.pkt_buf[2] = 0
self.pkt_buf[3] = 0
for sp in self.sub_pkt:
#设备索引(14bit)+类型(2bit)
temp = ((sp[0] & 0x3fff) << 2) | (sp[1] & 0x3)
self.pkt_buf[i] = (temp >> 8) & 0xff
i = i + 1
self.pkt_buf[i] = temp & 0xff
i = i + 1
#地址
self.pkt_buf[i] = (sp[2] >> 8) & 0xff
i = i + 1
self.pkt_buf[i] = sp[2] & 0xff
i = i + 1
#命令类型(4bit)+长度(12bit)
temp = (sp[3] << 12) | (sp[4] & 0xfff)
self.pkt_buf[i] = (temp >> 8) & 0xff
i = i + 1
self.pkt_buf[i] = temp & 0xff
i = i + 1
#工作计数
self.pkt_buf[i] = 0
i = i + 1
self.pkt_buf[i] = 0
i = i + 1
#数据
if sp[5] == None:
for _k in range(sp[4]):
self.pkt_buf[_k + i] = random.randint(0, 255)
else:
for _k in range(sp[4]):
self.pkt_buf[_k + i] = sp[5][_k]
i = i + sp[4]
f = open(filename, "wt+")
for _k in range(i):
f.write("txBuf[%d]=8'h%02X;\n"%(3 + _k, self.pkt_buf[_k]))
f.close()
if __name__ == '__main__':
genPkt = GenPkt()
genPkt.setPktType(1)
genPkt.gen("./generated/shin/detect_pkt.vh")
genPkt.clean()
genPkt.setPktType(0)
#subPkt参数索引、索引类型、地址、命令、长度、数据
genPkt.addSubPkt(0x02, 0, 0x0830, 0x2, 56, None)
genPkt.addSubPkt(0x01, 0, 0x0840, 0x1, 56, None)
genPkt.addSubPkt(0x03, 0, 0x1040, 0x0, 56, None)
for i in range(5):
genPkt.addSubPkt(random.randint(100, 4000), 0, random.randint(0, 65536), 0x0, 56, None)
#genPkt.addSubPkt(10, 2, 0x300, 0x2, 4, None)
#genPkt.addSubPkt(11, 1, 0x300, 0x2, 4, None)
genPkt.gen("./generated/shin/seq_pkt.vh")
genPkt.clean()
genPkt.setPktType(0)
#subPkt参数索引、索引类型、地址、命令、长度、数据
genPkt.addSubPkt(0x0, 0, random.randint(0, 65536), 0xe, 12, b'\0'*12)
genPkt.gen("./generated/shin/sync_pkt.vh")
genPkt.clean()
genPkt.setPktType(0)
#subPkt参数索引、索引类型、地址、命令、长度、数据
data = bytearray(b"\0" * 0x28)
data[0] = 0xff
data[16] = 0x90
data[17] = 0x88
data[24] = 0xff
data[25] = 0x0f
data[28] = 0x0a
data[29] = 0x0
data[36] = 0x0
data[37] = 0x01
genPkt.addSubPkt(0x2, 0, 0x120, 0x1, 0x28, data)
genPkt.gen("./generated/shin/sync_sig_pkt.vh")