1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
//
// Copyright 2015 Ettus Research LLC
//
`ifndef WORKING_DIR
`define WORKING_DIR "."
`endif
`define ABSPATH(name) {`WORKING_DIR, "/", name}
typedef enum {
READ, WRITE, APPEND
} fopen_mode_t;
typedef enum {
HEX, DEC, OCT, BIN, FLOAT
} fformat_t;
// Create a handle to a data_file with
// - FILENAME: Name of the file
// - FORMAT: Data format (HEX, DEC, OCT, BIN, FLOAT)
// - DWIDTH: Width of each element stored in the file (one line per word)
//
//TODO: We would ideally use a class but that is not
// supported by most simulators.
interface data_file_t #(
parameter FILENAME = "test.hex",
parameter FORMAT = HEX,
parameter DWIDTH = 64
) (input clk);
bit is_open;
integer handle;
// Open the data file for reading or writing.
//
// Usage: open(mode)
// where
// - mode: RW mode (Choose from: READ, WRITE, APPEND)
//
function open(fopen_mode_t mode = READ);
if (mode == APPEND)
handle = $fopen(`ABSPATH(FILENAME), "a");
else if (mode == WRITE)
handle = $fopen(`ABSPATH(FILENAME), "w");
else
handle = $fopen(`ABSPATH(FILENAME), "r");
if (handle == 0) begin
$error("Could not open file: %s", `ABSPATH(FILENAME));
$finish();
end
is_open = 1;
endfunction
// Close an open data file. No-op if file isn't already open
//
// Usage: close()
//
function close();
$fclose(handle);
handle = 0;
is_open = 0;
endfunction
// Is end-of-file reached.
//
// Usage: is_eof() Returns eof
// where
// - eof: A boolean
//
function logic is_eof();
return ($feof(handle));
endfunction
// Read a line from the datafile
//
// Usage: readline() Returns data
// where
// - data: A logic array of width DWIDTH containing the read word
//
function logic [DWIDTH-1:0] readline();
automatic logic [DWIDTH-1:0] word = 64'h0;
automatic integer status;
if (FORMAT == HEX)
status = $fscanf(handle, "%x\n", word);
else if (FORMAT == DEC)
status = $fscanf(handle, "%d\n", word);
else if (FORMAT == OCT)
status = $fscanf(handle, "%o\n", word);
else if (FORMAT == BIN)
status = $fscanf(handle, "%b\n", word);
else if (FORMAT == DEC)
status = $fscanf(handle, "%g\n", word);
else
$error("Invalid format");
return word;
endfunction
// Write a line to the datafile
//
// Usage: writeline(data)
// where
// - data: A logic array of width DWIDTH to write to the file
//
function void writeline(logic [DWIDTH-1:0] word);
if (FORMAT == HEX)
$fdisplay(handle, "%x", word);
else if (FORMAT == DEC)
$fdisplay(handle, "%d", word);
else if (FORMAT == OCT)
$fdisplay(handle, "%o", word);
else if (FORMAT == BIN)
$fdisplay(handle, "%b", word);
else if (FORMAT == DEC)
$fdisplay(handle, "%g", word);
else
$error("Invalid format");
endfunction
endinterface
|