litex/lib/sata/link/test/crc_tb.py

61 lines
1.3 KiB
Python
Raw Normal View History

import subprocess
2014-11-03 12:54:41 -05:00
from migen.fhdl.std import *
from lib.sata.std import *
from lib.sata.link.crc import *
from lib.sata.link.test.common import *
2014-11-03 12:54:41 -05:00
class TB(Module):
def __init__(self, length, random):
2014-11-03 12:54:41 -05:00
self.submodules.crc = SATACRC()
self.length = length
self.random = random
def get_c_crc(self, datas):
stdin = ""
for data in datas:
stdin += "0x%08x " %data
stdin += "exit"
with subprocess.Popen("./crc", stdin=subprocess.PIPE, stdout=subprocess.PIPE) as process:
process.stdin.write(stdin.encode("ASCII"))
out, err = process.communicate()
return int(out.decode("ASCII"), 16)
2014-11-03 12:54:41 -05:00
def gen_simulation(self, selfp):
# init CRC
selfp.crc.d = 0
2014-11-03 12:54:41 -05:00
selfp.crc.ce = 1
selfp.crc.reset = 1
yield
selfp.crc.reset = 0
# feed CRC with datas
datas = []
for i in range(self.length):
data = seed_to_data(i, self.random)
datas.append(data)
selfp.crc.d = data
yield
# log results
yield
sim_crc = selfp.crc.value
2014-11-03 12:54:41 -05:00
# stop
selfp.crc.ce = 0
for i in range(32):
2014-11-03 12:54:41 -05:00
yield
# get C core reference
c_crc = self.get_c_crc(datas)
# check results
s, l, e = check(c_crc, sim_crc)
2014-11-03 12:54:41 -05:00
print("shift "+ str(s) + " / length " + str(l) + " / errors " + str(e))
if __name__ == "__main__":
from migen.sim.generic import run_simulation
length = 8192
run_simulation(TB(length, True), ncycles=length+100, vcd_name="my.vcd")