fix bugs in the trng tester helper script

This commit is contained in:
bunnie 2021-07-04 01:50:31 +08:00
parent 9647559d25
commit f63b29cadd

View file

@ -48,7 +48,7 @@ class PrecursorUsb:
read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False) read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False)
if display == True: if display == True:
print("0x{:08x}".format(read_data)) sys.stderr.write("0x{:08x}\n".format(read_data))
return read_data return read_data
def poke(self, addr, wdata, check=False, display=False): def poke(self, addr, wdata, check=False, display=False):
@ -56,24 +56,25 @@ class PrecursorUsb:
_dummy_s = '\x00'.encode('utf-8') _dummy_s = '\x00'.encode('utf-8')
data = array.array('B', _dummy_s * 4) data = array.array('B', _dummy_s * 4)
for attempt in range(10): numread = self.dev.ctrl_transfer(bmRequestType=(0x80 | 0x43), bRequest=0,
try: wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff),
numread = self.dev.ctrl_transfer(bmRequestType=(0x80 | 0x43), bRequest=0, data_or_wLength=data, timeout=500)
wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff),
data_or_wLength=data, timeout=500)
except Exception as e:
self.dev.reset()
time.sleep(2)
else:
break
read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False) read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False)
print("before poke: 0x{:08x}".format(read_data)) sys.stderr.write("before poke: 0x{:08x}\n".format(read_data))
data = array.array('B', wdata.to_bytes(4, 'little')) data = array.array('B', wdata.to_bytes(4, 'little'))
numwritten = self.dev.ctrl_transfer(bmRequestType=(0x00 | 0x43), bRequest=0, for attempt in range(10):
wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff), try:
data_or_wLength=data, timeout=500) numwritten = self.dev.ctrl_transfer(bmRequestType=(0x00 | 0x43), bRequest=0,
wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff),
data_or_wLength=data, timeout=500)
except Exception as e:
sys.stderr.write("error; resetting device\n")
self.dev.reset()
time.sleep(2)
else:
break
if check == True: if check == True:
_dummy_s = '\x00'.encode('utf-8') _dummy_s = '\x00'.encode('utf-8')
@ -84,9 +85,9 @@ class PrecursorUsb:
data_or_wLength=data, timeout=500) data_or_wLength=data, timeout=500)
read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False) read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False)
print("after poke: 0x{:08x}".format(read_data)) sys.stderr.write("after poke: 0x{:08x}\n".format(read_data))
if display == True: if display == True:
print("wrote 0x{:08x} to 0x{:08x}".format(wdata, addr)) sys.stderr.write("wrote 0x{:08x} to 0x{:08x}\n".format(wdata, addr))
def burst_read(self, addr, len): def burst_read(self, addr, len):
_dummy_s = '\x00'.encode('utf-8') _dummy_s = '\x00'.encode('utf-8')
@ -99,7 +100,7 @@ class PrecursorUsb:
time.sleep(0.2) # this improves system stability, somehow time.sleep(0.2) # this improves system stability, somehow
for pkt_num in range(packet_count): for pkt_num in range(packet_count):
# print('.', end='') # sys.stderr.write('.', end='')
cur_addr = addr + pkt_num * maxlen cur_addr = addr + pkt_num * maxlen
if pkt_num == packet_count - 1: if pkt_num == packet_count - 1:
if len % maxlen != 0: if len % maxlen != 0:
@ -120,16 +121,17 @@ class PrecursorUsb:
if self.vexdbg_addr != None: if self.vexdbg_addr != None:
self.poke(self.vexdbg_addr, 0x02000000) self.poke(self.vexdbg_addr, 0x02000000)
except Exception as e: except Exception as e:
sys.stderr.write("error; resetting device\n")
self.dev.reset() self.dev.reset()
time.sleep(2) time.sleep(2)
else: else:
break break
else: else:
sys.stderr.write("Burst read failed") sys.stderr.write("Burst read failed\n")
exit(1) exit(1)
if numread != bufsize: if numread != bufsize:
sys.stderr.write("Burst read error: {} bytes requested, {} bytes read at 0x{:08x}".format(bufsize, numread, cur_addr)) sys.stderr.write("Burst read error: {} bytes requested, {} bytes read at 0x{:08x}\n".format(bufsize, numread, cur_addr))
else: else:
ret = ret + data ret = ret + data
@ -160,7 +162,7 @@ class PrecursorUsb:
data_or_wLength=wdata, timeout=500) data_or_wLength=wdata, timeout=500)
if numwritten != bufsize: if numwritten != bufsize:
print("Burst write error: {} bytes requested, {} bytes written at 0x{:08x}".format(bufsize, numwritten, cur_addr)) sys.stderr.write("Burst write error: {} bytes requested, {} bytes written at 0x{:08x}".format(bufsize, numwritten, cur_addr))
exit(1) exit(1)
def ping_wdt(self): def ping_wdt(self):
@ -175,7 +177,7 @@ class PrecursorUsb:
hasher.update(csr_data[:0x7FC0]) hasher.update(csr_data[:0x7FC0])
digest = hasher.digest() digest = hasher.digest()
if digest != csr_data[0x7fc0:]: if digest != csr_data[0x7fc0:]:
print("Could not find a valid csr.csv descriptor on the device, aborting!") sys.stderr.write("Could not find a valid csr.csv descriptor on the device, aborting!\n")
exit(1) exit(1)
csr_len = int.from_bytes(csr_data[:4], 'little') csr_len = int.from_bytes(csr_data[:4], 'little')
@ -196,7 +198,7 @@ class PrecursorUsb:
self.regions[row[1]] = [row[2], row[3]] self.regions[row[1]] = [row[2], row[3]]
if 'git_rev' in row[0]: if 'git_rev' in row[0]:
self.gitrev = row[1] self.gitrev = row[1]
print("Using SoC {} registers".format(self.gitrev)) sys.stderr.write("Using SoC {} registers\n".format(self.gitrev))
def auto_int(x): def auto_int(x):
return int(x, 0) return int(x, 0)
@ -225,13 +227,14 @@ def main():
dev.set_configuration() dev.set_configuration()
if args.config: if args.config:
cfg = dev.get_active_configuration() cfg = dev.get_active_configuration()
print(cfg) sys.stderr.write(str(cfg))
sys.stderr.write("\n")
pc_usb = PrecursorUsb(dev) pc_usb = PrecursorUsb(dev)
if args.peek: if args.peek:
pc_usb.peek(args.peek, display=True) pc_usb.peek(args.peek, display=True)
# print(burst_read(dev, args.peek, 256).hex()) # sys.stderr.write(burst_read(dev, args.peek, 256).hex())
exit(0) exit(0)
if args.poke: if args.poke:
@ -241,11 +244,11 @@ def main():
# d = bytearray(os.urandom(8000)) # d = bytearray(os.urandom(8000))
# burst_write(dev, addr, d) # burst_write(dev, addr, d)
# r = burst_read(dev, addr, 8000) # r = burst_read(dev, addr, 8000)
# print(r.hex()) # sys.stderr.write(r.hex())
# if d != r: # if d != r:
# print("mismatch") # sys.stderr.write("mismatch")
# else: # else:
# print("match") # sys.stderr.write("match")
exit(0) exit(0)
pc_usb.load_csrs() # prime the CSR values pc_usb.load_csrs() # prime the CSR values
@ -263,13 +266,13 @@ def main():
LOC_WF200 = 0x07F80000 LOC_WF200 = 0x07F80000
LOC_EC = 0x07FCE000 LOC_EC = 0x07FCE000
else: else:
print("SoC is from an unknow rev '{}', use --force to continue anyways with v0.8 firmware offsets".format(pc_usb.load_csrs())) sys.stderr.write("SoC is from an unknow rev '{}', use --force to continue anyways with v0.8 firmware offsets".format(pc_usb.load_csrs()))
exit(1) exit(1)
vexdbg_addr = int(pc_usb.regions['vexriscv_debug'][0], 0) vexdbg_addr = int(pc_usb.regions['vexriscv_debug'][0], 0)
pc_usb.vexdbg_addr = vexdbg_addr pc_usb.vexdbg_addr = vexdbg_addr
#pc_usb.ping_wdt() #pc_usb.ping_wdt()
#print("Halting CPU.") #sys.stderr.write("Halting CPU.")
#pc_usb.poke(vexdbg_addr, 0x00020000) #pc_usb.poke(vexdbg_addr, 0x00020000)
messible2_in = pc_usb.register('messible2_in') messible2_in = pc_usb.register('messible2_in')
@ -280,46 +283,64 @@ def main():
TIMEOUT = 30.0 TIMEOUT = 30.0
phase = 0 phase = 0
last_phase = 0
blocks = 0 blocks = 0
while True: while True:
start_time = time.time() start_time = time.time()
#sys.stderr.write("at phase, {} waiting for next buffer\n".format(phase)) sys.stderr.write("at phase {}, waiting for next buffer\n".format(phase))
while True: while True:
remote_phase = pc_usb.peek(messible_out) remote_phase = pc_usb.peek(messible_out)
if remote_phase > phase: if remote_phase > phase:
break break
time.sleep(0.5) time.sleep(0.5)
if time.time() > (start_time + TIMEOUT): if time.time() > (start_time + TIMEOUT):
sys.stderr.write("timeout\n") try:
pc_usb.poke(pc_usb.register('reboot_soc_reset'), 0xac, display=False)
except usb.core.USBError:
pass # we expect an error because we reset the SOC and that includes the USB core
time.sleep(2.0)
dev = usb.core.find(idProduct=0x5bf0, idVendor=0x1209)
dev.set_configuration()
pc_usb = PrecursorUsb(dev)
pc_usb.load_csrs() # prime the CSR values
#pc_usb.poke(vexdbg_addr, 0x02000000) # maybe the CPU is still halted, try resuming it
sys.stderr.write("timeout & reset\n")
phase = 0
last_phase = 0
remote_phase = pc_usb.peek(messible_out)
break break
phase = remote_phase phase = remote_phase
pc_usb.poke(messible2_in, phase) pc_usb.poke(messible2_in, phase)
if (phase % 2) == 1: if last_phase != phase:
#sys.stderr.write("phase {} fetching RAM_A\n".format(phase)) if (phase % 2) == 1:
page = pc_usb.burst_read(RAM_A, BURST_LEN) sys.stderr.write("phase {} fetching RAM_A\n".format(phase))
sys.stdout.buffer.write(page) page = pc_usb.burst_read(RAM_A, BURST_LEN)
#sys.stderr.write("got page A {}\n".format(len(page))) sys.stdout.buffer.write(page)
sys.stderr.write("got page A {}\n".format(len(page)))
else:
sys.stderr.write("phase {} fetching RAM_B\n".format(phase))
page = pc_usb.burst_read(RAM_B, BURST_LEN)
sys.stdout.buffer.write(page)
sys.stderr.write("got page B {}\n".format(len(page)))
blocks += 1
else: else:
#sys.stderr.write("phase {} fetching RAM_B\n".format(phase)) sys.stderr.write("phase didn't increment, not transferring identical block")
page = pc_usb.burst_read(RAM_B, BURST_LEN)
sys.stdout.buffer.write(page)
#sys.stderr.write("got page B {}\n".format(len(page)))
blocks += 1 sys.stderr.write("at block {}".format(blocks))
#sys.stderr.write("Resuming CPU.")
#print("Resuming CPU.")
#pc_usb.poke(vexdbg_addr, 0x02000000) #pc_usb.poke(vexdbg_addr, 0x02000000)
#print("Resetting SOC...") #sys.stderr.write("Resetting SOC...")
#try: #try:
# pc_usb.poke(pc_usb.register('reboot_soc_reset'), 0xac, display=False) # pc_usb.poke(pc_usb.register('reboot_soc_reset'), 0xac, display=False)
#except usb.core.USBError: #except usb.core.USBError:
# pass # we expect an error because we reset the SOC and that includes the USB core # pass # we expect an error because we reset the SOC and that includes the USB core
# print("If you need to run more commands, please unplug and re-plug your device in, as the Precursor USB core was just reset") # sys.stderr.write("If you need to run more commands, please unplug and re-plug your device in, as the Precursor USB core was just reset")
if __name__ == "__main__": if __name__ == "__main__":
main() main()