У меня есть файл SREC, который представляет собой простой текстовый файл, и я хочу прочитать его по строкам в verilog. Как я могу это сделать?Как читать текстовый файл по строкам в verilog?
0
A
ответ
14
Следующее читает файл, 1 строка за такт: ожидаемый формат данных - одно десятичное число в строке.
integer data_file ; // file handler
integer scan_file ; // file handler
logic signed [21:0] captured_data;
`define NULL 0
initial begin
data_file = $fopen("data_file.dat", "r");
if (data_file == `NULL) begin
$display("data_file handle was NULL");
$finish;
end
end
always @(posedge clk) begin
scan_file = $fscanf(data_file, "%d\n", captured_data);
if (!$feof(data_file)) begin
//use captured_data as you would any other wire or reg value;
end
end
3
Благодарим за решение. Я немного изменил его, чтобы использовать 2 .txt-файл, содержащий 32 номера HEX в каждой строке, и нашел некоторые трудности на пути, так как я не понял, что делает каждая строка кода. Мои выводы были следующими.
Только вары и регс декларация
////////I'm using inputs.txt and outputs.txt to read both lines at the same time
module Decryption_Top_Testbench;
////////TEXT DOC variables
integer file_outputs ; // var to see if file exists
integer scan_outputs ; // captured text handler
integer file_inputs ; // var to see if file exists
integer scan_inputs ; // captured text handler
//TXT
reg [127:0] captured_outputs; ///Actual text obtained from outputs.txt lines
reg [127:0] captured_inputs; ///Actual text obtained from inputs.txt lines
Открытие обоих файлов
initial
begin
// TEXT FILE outputs///////////////////////
file_outputs = $fopen("C:/outputs.txt", "r"); //Opening text file
//you should use the full path if you don't want to get in the trouble
//of using environment vars
if (file_outputs == 0) begin // If outputs file is not found
$display("data_file handle was NULL"); //simulation monitor command
$finish;
end
// TEXT FILE inputs///////////////////////
file_inputs = $fopen("C:/inputs.txt", "r"); //Opening text file (inputs)
if (file_inputs == 0) begin //If inputs file is not found
$display("data_file handle was NULL");
$finish;
end
end
В этой части я линия читать строки в шестнадцатеричном формате и сохранить его в «captured_outputs» зарегистрировать и «captured_inputs " регистр.
///Since I'm using it just to simulate I'm not interested on a clock pulse,
/// I want it to happen all at the same time with whatever comes first
always @(*)
begin
if (!$feof(file_outputs))
begin
///!$feof means if not reaching the end of file
///file_outputs is always returning a different number other than "0" if the doc
///has not ended. When reaching "0" it means the doc is over.
///Since both of my docs are the same length I'm only validating one of them
///but if you have different lenghts you should verify each doc you're reading
///
scan_inputs = $fscanf(file_inputs, "%h\n", captured_inputs); //Inputs Line text
scan_outputs = $fscanf(file_outputs, "%h\n", captured_outputs); //Outputs line text
$display ("Line :[inputs: %h _ outputs: %h ]" captured_inputs, captured_outputs);
// Displaying each line at the simulation monitor
///$fscanf means formatted text, $scanf would read text ignoring the format
/// %h\n means it should expect HEX numbers and the end of line character, that means
/// the line is over, but if you want to use a diff criteria
/// you can replace \n to whatever you may need
end
else
begin
$finish;
$fclose(file_outputs); //Closing files just in case to prevent wasting memory
$fclose(file_inputs);
end
end
Я просто хотел внести свой вклад в чем-то кто-кто начинает код в Verilog мог понять и приложить эту замечательную особенность его/ее проекта.
Наслаждайтесь!
Смежные вопросы
- 1. Как читать текстовый файл по строкам?
- 2. Как читать текстовый файл по строкам с FTP-сервера?
- 3. Perl - читать текстовый файл по строкам в массив
- 4. Прочитать текстовый файл по строкам в строки
- 5. Как читать файл по строкам в файле?
- 6. Как получить текстовый файл, читаемый по строкам?
- 7. Запись в текстовый файл по строкам
- 8. Текстовый файл по строкам в строковый массив
- 9. Запись в текстовый файл по строкам
- 10. Прочитать текстовый файл по строкам в Swift?
- 11. Как читать файл gzip по строкам?
- 12. Java 8 - Как читать файл по строкам в java?
- 13. Прочитать текстовый файл по строкам из массива
- 14. Как читать данные из NSFileHandle по строкам?
- 15. Как читать локальный (res/raw) файл по строкам?
- 16. Как читать текстовый файл
- 17. Bash Shell Scripting читать файл по строкам
- 18. C++ читать большой текстовый файл
- 19. Как читать переменную по строкам?
- 20. Как читать строки по строкам
- 21. Как читать в текстовый файл
- 22. Как читать текстовый файл continuosuly
- 23. Как читать текстовый файл по строкам, затем анализировать символы в строке?
- 24. Как я могу читать текстовый файл по строкам и добавлять эти строки в массив?
- 25. Как читать удаленный текстовый файл
- 26. Как читать и писать .txt-файл по строкам в python?
- 27. Как читать файл по строкам в VB Script?
- 28. Как читать файл по строкам и закрывать его в Scala?
- 29. Как читать большой текстовый файл по очереди и добавлять этот поток в файл по строкам в Ruby?
- 30. Как читать строку по строкам в j2me