问题
I have a module in written in System Verilog that dumps the contents of the SRAM into a file. I would like to read from this file and use the data in a separate program written in python, but in real time. I don't have much control over the writing from the verilog code. Is it possible to somehow manage the two read and writes? Currently when it reads from the file, there is a (seemingly) random number inserted at the start of every line and that throws off the parsing. I assume these prefixes only appear when they are reading and writing at the same time because if I run them both very slowly it works fine.
window = Tk()
canvas = Canvas(window, width=WIDTH, height=HEIGHT, bg="#000000")
canvas.pack()
img = PhotoImage(width=WIDTH, height=HEIGHT)
canvas.create_image((WIDTH/2, HEIGHT/2), image=img, state="normal")
def redraw():
fp = open('test_data.txt','r')
lines=fp.readlines()
for i in range(len(lines)):
#do stuff
fp.close()
window.after(35,redraw)
window.after(35,redraw)
mainloop()
This is what is reading.
Any suggestions are appreciated.
回答1:
Reading and writing a file from multiple processes is likely to be unpredictable.
If you are running on a Unix-like system, you could use mkfifo
to make a file-like object which you can write to and read from simultaneously and the data will stay in the correct order.
On Windows you need a NamedPipe - which you could create from Python and then connect to by opening as a normal file in SystemVerilog (I believe!)
http://docs.activestate.com/activepython/2.4/pywin32/win32pipe.html
回答2:
I would suggest using VPI to directly access the contents of the SRAM straight from the simulation. This also opens the possibility of dynamically adjusting your stimulus (e.g. sending data until a FIFO is full) rather than relying on files for input/output.
Since you're using Python you could look into Cocotb, an open-source Python cosimulation framework. Basically you can use the python 'dot' notation to traverse the design hierarchy and pull out values:
# Pull out the values from the simulation
for index in range(len(dut.path.through.hierarchy.ram)):
val = dut.path.through.hierarchy.ram[index].value.integer
# do stuff
I created a quick example on EDA Playground with a simplistic example: http://www.edaplayground.com/s/57/565
Disclaimer: I'm one of the Cocotb developers.
回答3:
You can use pipe, in this example a cmd (windows) command line writes in the pipe, then the program shows its output from the same pipe:
import subprocess,sys
p = subprocess.Popen("netsatat",shell=False ,stdout=subprocess.PIPE)
while True:
out = p.stdout.readline()
if out == '' and p.poll() != None:
break
if out != b'':
print(out.decode('ascii','backslashreplace'))
else :
break;
来源:https://stackoverflow.com/questions/15992375/reading-and-writing-to-a-file-simultaneously