| Author |
Message |
davyzhu
Joined: 23 May 2004 Posts: 521 Helped: 3 Location: oriental
|
07 Jul 2005 13:45 [Verilog] How to read data from a file? |
|
|
|
|
Hi all,
I want to read one data per clock from a flie, and send them to a pipelined circuit.
After that, save one data per clock to a file.
I am new to testbench. Now I decide to read a data (in) from file @(negedge clk).
And pass this data to the module that I want to test @(posedge clk).
After that, save data (out) to a file.
Is my test method right? Any suggestions will be appreciated!
// Code list below seems not work
//------code-----------
`define clk_cycle 10
module testbench_tb;
reg gclk;
integer os1,os2;
integer i,j;
wire [5:0] in;
reg [5:0] out;
initial begin
gclk = 0;
os1 = $fopen("tb_1.txt","r");
os2 = $fopen("tb_2.txt","w");
end
always # (`clk_cycle / 2) gclk = ~gclk;
always @ (negedge gclk)
begin
$fscanf(os1, "%d", in);
$fwrite(os2, "%d", out);
end
// pipelined circuit under test
always @ (posedge gclk)
begin
if(greset == 0)
begin
out <= 0;
end
else
begin
out <=in;
end
end
endmodule
Best regards,
Davy
|
|
| Back to top |
|
 |
eeeraghu
Joined: 03 Jun 2005 Posts: 215 Helped: 17
|
07 Jul 2005 14:15 Re: [Verilog] How to read data from a file? |
|
|
|
|
Use $readmemb or $readmemh to load a reg array (memory) with file data.
$readmemb — The file contains binary ASCII data
$readmemb ("filename", memory_name, [start_addr, [finish_addr]]);
$readmemh — The file contains hexadecimal ASCII data
$readmemh ("filename", memory_name, [start_addr, [finish_addr]]);
You can default the addresses, or provide them in the system task or in the data
file (or both). The system task by default writes from the left memory address (as
declared) toward the right memory address. Addresses (if any) in the data file
must reside with the range (if any) specified in the system task.
this might help u,
|
|
| Back to top |
|
 |
Google AdSense

|
07 Jul 2005 14:15 Ads |
|
|
|
|
|
|
| Back to top |
|
 |
visualart
Joined: 21 Dec 2001 Posts: 588 Helped: 26
|
08 Jul 2005 4:00 [Verilog] How to read data from a file? |
|
|
|
|
| It is obligatory that be couple the task $fopen and the task $fclose, Only $fopen, you will occupy the deal with memory.
|
|
| Back to top |
|
 |
jarodz
Joined: 12 Mar 2005 Posts: 100 Helped: 14
|
10 Jul 2005 17:05 [Verilog] How to read data from a file? |
|
|
|
|
hi, davyzhu
You can declare two large reg arrarys as input/output buffer.
If your input data is in ASCII format, use $readmem* as eeeraghu's describption. Otherwise use $fgetc in initial block.
Then save the data in output buffer to file before $finish.
Regards,
Jarod
|
|
| Back to top |
|
 |