OpenCores
URL https://opencores.org/ocsvn/astron_mm/astron_mm/trunk

Subversion Repositories astron_mm

[/] [astron_mm/] [trunk/] [mm_file_pkg.vhd] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 danv
-------------------------------------------------------------------------------
2
--
3
-- Copyright (C) 2017
4
-- ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
5
-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
6
--
7
-- This program is free software: you can redistribute it and/or modify
8
-- it under the terms of the GNU General Public License as published by
9
-- the Free Software Foundation, either version 3 of the License, or
10
-- (at your option) any later version.
11
--
12
-- This program is distributed in the hope that it will be useful,
13
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
14
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
-- GNU General Public License for more details.
16
--
17
-- You should have received a copy of the GNU General Public License
18
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
--
20
-------------------------------------------------------------------------------
21
 
22
-- Author :
23
--   D. van der Schuur  May 2012  Original for Python - file IO - VHDL 
24
--   E. Kooistra        feb 2017  Added purpose and description
25
--                                Added procedures for external control in a
26
--                                pure VHDL test bench.
27
--
28
-- Purpose: Provide DUT access via MM bus through file IO per MM slave
29
-- Description:
30
--   This package provides file IO access to MM slaves and to the status of
31
--   the simulation:
32
--
33
-- 1) MM slave access
34
--   Access to MM slaves is provided by component mm_file.vhd that first calls
35
--   mmf_file_create() and loop forever calling mmf_mm_from_file(). Each MM
36
--   slave has a dedicated pair of request (.ctrl) and response (.stat) IO
37
--   files.
38
--   The mmf_file_create() creates the .ctrl file and mmf_mm_from_file() reads
39
--   it to check whether there is a WR or RD access request. For a WR request
40
--   the wr_data and wr_addr are read from the .ctrl and output on the MM bus
41
--   via mm_mosi. For a RD access request the rd_addr is read from the .ctrl
42
--   and output on the MM bus via mm_mosi. The after the read latency the
43
--   rd_data is written to the .stat file that is then created and closed.
44
--
45
--                    wr             rd  _________               __________
46
--   mmf_mm_bus_wr() ---> ctrl file --->|         |---mm_mosi-->|          |
47
--                                      | mm_file |             | MM slave |
48
--   mmf_mm_bus_rd() <--- stat file <---|___\_____|<--mm_miso---|__________|
49
--                    rd             wr      \
50
--                                            \--> loop: mmf_mm_from_file()
51
--
52
--   The ctrl file is created by mm_file at initialization and recreated by
53
--   every call of mmf_mm_from_file().
54
--   The stat file is recreated by every call of mmf_mm_bus_rd().
55
--
56
-- 2) Simulator access
57
--   External access to the simulation is provided via a .ctrl file that
58
--   supports GET_SIM_TIME and then report the NOW time via the .stat file.
59
--   The simulation access is provided via a procedure mmf_poll_sim_ctrl_file()
60
--   that works similar component mm_file.vhd.
61
--
62
--                      wr             rd
63
--                    |---> ctrl file --->|
64
--   mmf_sim_get_now()|                   |mmf_poll_sim_ctrl_file()
65
--                    |<--- stat file <---|  \
66
--                      rd             wr     \
67
--                                             \--> loop: mmf_sim_ctrl_from_file()
68
--
69
--   The ctrl file is created by mmf_poll_sim_ctrl_file at initialization and
70
--   recreated by every call of mmf_sim_ctrl_from_file().
71
--   The stat file is recreated by every call of mmf_sim_get_now().
72
--
73
-- A) External control by a Python script
74
--   A Python script can issue requests via the .ctrl files to control the
75
--   simulation and read the .stat files. This models the MM access via a
76
--   Monitoring and Control protocol via 1GbE.
77
--
78
--   Internal procedures:
79
--   . mmf_file_create(filename: IN STRING);
80
--   . mmf_mm_from_file(SIGNAL mm_clk  : IN STD_LOGIC; 
81
--   . mmf_sim_ctrl_from_file(rd_filename: IN STRING;
82
--   
83
--   External procedures (used in a VHDL design to provide access to the MM
84
--   slaves and simulation via file IO):
85
--   . mm_file.vhd --> instead of a procedure MM slave file IO uses a component
86
--   . mmf_poll_sim_ctrl_file()
87
--   
88
-- B) External control by a VHDL process --> see tb_mm_file.vhd
89
--   Instead of a Python script the file IO access to the MM slaves can also
90
--   be used in a pure VHDL testbench. This is useful when the MM slave bus
91
--   signals (mm_mosi, mm_miso) are not available on the entity of the DUT
92
--   (device under test), which is typically the case when a complete FPGA
93
--   design needs to be simulated.
94
--
95
--   Internal procedures:
96
--   . mmf_wait_for_file_status()
97
--   . mmf_wait_for_file_empty()
98
--   . mmf_wait_for_file_not_empty()
99
--                                      
100
--   External procedures (used in a VHDL test bench to provide access to the 
101
--   MM slaves in a DUT VHDL design and simulation via file IO):
102
--   . mmf_mm_bus_wr()
103
--   . mmf_mm_bus_rd()
104
--   . mmf_sim_get_now()
105
--
106
--   External function to create unique sim.ctrl/sim.stat filename per test bench in a multi tb
107
--   . mmf_slave_prefix()
108
--
109
-- Remarks:
110
-- . The timing of the MM access in mmf_mm_bus_wr() and mmf_mm_bus_rd() and the
111
--   simulation access in mmf_sim_get_now() is not critical. The timing of the first
112
--   access depends on the tb. Due to falling_edge(mm_clk) in mmf_wait_for_file_*()
113
--   all subsequent accesses will start at falling_edge(mm_clk)
114
 
115
LIBRARY IEEE, common_pkg_lib, common_ram_lib;
116
USE IEEE.STD_LOGIC_1164.ALL;
117
USE IEEE.NUMERIC_STD.ALL;
118
USE common_pkg_lib.common_pkg.ALL;
119
USE common_pkg_lib.tb_common_pkg.ALL;
120
USE common_ram_lib.common_ram_pkg.ALL;
121
USE work.tb_common_mem_pkg.ALL;
122
USE std.textio.ALL;
123
USE IEEE.std_logic_textio.ALL;
124
USE common_pkg_lib.common_str_pkg.ALL;
125
 
126
PACKAGE mm_file_pkg IS
127
 
128
  -- Constants used by mm_file.vhd
129
  CONSTANT c_mmf_mm_clk_period : TIME :=  100 ps;  -- Default mm_clk period in simulation. Set much faster than DP clock to speed up
130
                                                   -- simulation of MM access. Without file IO throttling 100 ps is a good balance
131
                                                   -- between simulation speed and file IO rate.
132
  CONSTANT c_mmf_mm_timeout    : TIME := 1000 ns;  -- Default MM file IO timeout period. Set large enough to account for MM-DP clock
133
                                                   -- domain crossing delays. Use 0 ns to disable file IO throttling, to have file IO
134
                                                   -- at the mm_clk rate.
135
  CONSTANT c_mmf_mm_pause      : TIME :=  100 ns;  -- Default MM file IO pause period after timeout. Balance between file IO rate
136
                                                   -- reduction and responsiveness to new MM access.
137
 
138
  -- Procedure to (re)create empty file
139
  PROCEDURE mmf_file_create(filename: IN STRING);
140
 
141
  -- Procedure to perform an MM access from file
142
  PROCEDURE mmf_mm_from_file(SIGNAL mm_clk  : IN STD_LOGIC;
143
                             SIGNAL mm_rst  : IN STD_LOGIC;
144
                             SIGNAL mm_mosi : OUT t_mem_mosi;
145
                             SIGNAL mm_miso : IN  t_mem_miso;
146
                             rd_filename: IN STRING;
147
                             wr_filename: IN STRING;
148
                             rd_latency: IN NATURAL);
149
 
150
  -- Procedure to process a simulation status request from the .ctrl file and provide response via the .stat file
151
  PROCEDURE mmf_sim_ctrl_from_file(rd_filename: IN STRING;
152
                                   wr_filename: IN STRING);
153
 
154
  -- Procedure to poll the simulation status
155
  PROCEDURE mmf_poll_sim_ctrl_file(rd_file_name: IN STRING;
156
                                   wr_file_name: IN STRING);
157
 
158
  -- Procedure to poll the simulation status
159
  PROCEDURE mmf_poll_sim_ctrl_file(SIGNAL mm_clk  : IN STD_LOGIC;
160
                                   rd_file_name: IN STRING;
161
                                   wr_file_name: IN STRING);
162
 
163
  -- Procedures that keep reading the file until it has been made empty or not empty by some other program,
164
  -- to ensure the file is ready for a new write access
165
  PROCEDURE mmf_wait_for_file_status(rd_filename   : IN STRING;  -- file name with extension
166
                                     exit_on_empty : IN BOOLEAN;
167
                                     SIGNAL mm_clk : IN STD_LOGIC);
168
 
169
  PROCEDURE mmf_wait_for_file_empty(rd_filename   : IN STRING;  -- file name with extension
170
                                    SIGNAL mm_clk : IN STD_LOGIC);
171
  PROCEDURE mmf_wait_for_file_not_empty(rd_filename   : IN STRING;  -- file name with extension
172
                                        SIGNAL mm_clk : IN STD_LOGIC);
173
 
174
  -- Procedure to issue a write access via the MM request .ctrl file  
175
  PROCEDURE mmf_mm_bus_wr(filename      : IN STRING;   -- file name without extension
176
                          wr_addr       : IN INTEGER;  -- use integer to support full 32 bit range
177
                          wr_data       : IN INTEGER;
178
                          SIGNAL mm_clk : IN STD_LOGIC);
179
 
180
  -- Procedure to issue a read access via the MM request .ctrl file and get the read data from the MM response file
181
  PROCEDURE mmf_mm_bus_rd(filename       : IN STRING;   -- file name without extension
182
                          rd_latency     : IN NATURAL;
183
                          rd_addr        : IN INTEGER;  -- use integer to support full 32 bit range
184
                          SIGNAL rd_data : OUT STD_LOGIC_VECTOR(c_word_w-1 DOWNTO 0);
185
                          SIGNAL mm_clk  : IN STD_LOGIC);
186
  -- . rd_latency = 1
187
  PROCEDURE mmf_mm_bus_rd(filename       : IN STRING;
188
                          rd_addr        : IN INTEGER;
189
                          SIGNAL rd_data : OUT STD_LOGIC_VECTOR(c_word_w-1 DOWNTO 0);
190
                          SIGNAL mm_clk  : IN STD_LOGIC);
191
 
192
  -- Procedure that reads the rd_data every rd_interval until has the specified rd_value, the proc arguments can be understood as a sentence
193
  PROCEDURE mmf_mm_wait_until_value(filename         : IN STRING;   -- file name without extension
194
                                    rd_addr          : IN INTEGER;
195
                                    c_representation : IN STRING;  -- treat rd_data as "SIGNED" or "UNSIGNED" 32 bit word
196
                                    SIGNAL rd_data   : INOUT STD_LOGIC_VECTOR(c_word_w-1 DOWNTO 0);
197
                                    c_condition      : IN STRING;  -- ">", ">=", "=", "<=", "<", "/="
198
                                    c_rd_value       : IN INTEGER;
199
                                    c_rd_interval    : IN TIME;
200
                                    SIGNAL mm_clk    : IN STD_LOGIC);
201
 
202
  -- Procedure to get NOW via simulator status
203
  PROCEDURE mmf_sim_get_now(filename       : IN STRING;   -- file name without extension
204
                            SIGNAL rd_now  : OUT STRING;
205
                            SIGNAL mm_clk  : IN STD_LOGIC);
206
 
207
  -- Functions to create prefixes for the mmf file filename
208
  FUNCTION mmf_prefix(name : STRING; index : NATURAL) RETURN STRING;  -- generic prefix name with index to be used for a file IO filename
209
  FUNCTION mmf_tb_prefix(tb : INTEGER) RETURN STRING;                 -- fixed test bench prefix with index tb to allow file IO with multi tb
210
  FUNCTION mmf_subrack_prefix(subrack : INTEGER) RETURN STRING;       -- fixed subrack prefix with index subrack to allow file IO with multi subracks that use same unb numbers
211
 
212
  -- Functions to create mmf file prefix that is unique per slave, for increasing number of hierarchy levels:
213
  -- . return "filepath/s0_i0_"
214
  -- . return "filepath/s0_i0_s1_i1_"
215
  -- . return "filepath/s0_i0_s1_i1_s2_i2_"
216
  -- . return "filepath/s0_i0_s1_i1_s2_i2_s3_i3_"
217
  -- . return "filepath/s0_i0_s1_i1_s2_i2_s3_i3_s4_i4_"
218
  FUNCTION mmf_slave_prefix(dir_path, s0 : STRING; i0 : NATURAL) RETURN STRING;
219
  FUNCTION mmf_slave_prefix(dir_path, s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL) RETURN STRING;
220
  FUNCTION mmf_slave_prefix(dir_path, s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL) RETURN STRING;
221
  FUNCTION mmf_slave_prefix(dir_path, s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL; s3 : STRING; i3 : NATURAL) RETURN STRING;
222
  FUNCTION mmf_slave_prefix(dir_path, s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL; s3 : STRING; i3 : NATURAL; s4 : STRING; i4 : NATURAL) RETURN STRING;
223
 
224
  CONSTANT c_mmf_local_dir_path : STRING := "mmfiles/";   -- local directory in project file build directory
225
  FUNCTION mmf_slave_prefix(s0 : STRING; i0 : NATURAL) RETURN STRING;
226
  FUNCTION mmf_slave_prefix(s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL) RETURN STRING;
227
  FUNCTION mmf_slave_prefix(s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL) RETURN STRING;
228
  FUNCTION mmf_slave_prefix(s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL; s3 : STRING; i3 : NATURAL) RETURN STRING;
229
  FUNCTION mmf_slave_prefix(s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL; s3 : STRING; i3 : NATURAL; s4 : STRING; i4 : NATURAL) RETURN STRING;
230
 
231
  ----------------------------------------------------------------------------
232
  -- Declare mm_file component to support positional generic and port mapping of many instances in a TB
233
  ----------------------------------------------------------------------------
234
  COMPONENT mm_file
235
  GENERIC(
236
    g_file_prefix       : STRING;
237
    g_file_enable       : STD_LOGIC := '1';
238
    g_mm_rd_latency     : NATURAL := 2;
239
    g_mm_timeout        : TIME := c_mmf_mm_timeout;
240
    g_mm_pause          : TIME := c_mmf_mm_pause
241
  );
242
  PORT (
243
    mm_rst        : IN  STD_LOGIC;
244
    mm_clk        : IN  STD_LOGIC;
245
    mm_master_out : OUT t_mem_mosi;
246
    mm_master_in  : IN  t_mem_miso
247
  );
248
  END COMPONENT;
249
 
250
END mm_file_pkg;
251
 
252
PACKAGE BODY mm_file_pkg IS
253
 
254
  PROCEDURE mmf_file_create(filename: IN STRING) IS
255
    FILE created_file : TEXT OPEN write_mode IS filename;
256
  BEGIN
257
    -- Write the file with nothing in it
258
    write(created_file, "");
259
  END;
260
 
261
  PROCEDURE mmf_mm_from_file(SIGNAL mm_clk : IN STD_LOGIC;
262
                             SIGNAL mm_rst : IN STD_LOGIC;
263
                             SIGNAL mm_mosi : OUT t_mem_mosi;
264
                             SIGNAL mm_miso : IN  t_mem_miso;
265
                             rd_filename: IN STRING;
266
                             wr_filename: IN STRING;
267
                             rd_latency: IN NATURAL) IS
268
    FILE rd_file : TEXT;
269
    FILE wr_file : TEXT;
270
 
271
    VARIABLE open_status_rd: file_open_status;
272
    VARIABLE open_status_wr: file_open_status;
273
 
274
    VARIABLE rd_line : LINE;
275
    VARIABLE wr_line : LINE;
276
 
277
    -- Note: Both the address and the data are interpreted as 32-bit data!
278
    -- This means one has to use leading zeros in the file when either is
279
    -- less than 8 hex characters, e.g.:
280
    -- (address) 0000000A
281
    -- (data)    DEADBEEF
282
    -- ...as a hex address 'A' would fit in only 4 bits, causing an error in hread().
283
    VARIABLE v_addr_slv : STD_LOGIC_VECTOR(c_word_w-1 DOWNTO 0);
284
    VARIABLE v_data_slv : STD_LOGIC_VECTOR(c_word_w-1 DOWNTO 0);
285
 
286
    VARIABLE v_rd_wr_str : STRING(1 TO 2); -- Contains 'RD' or 'WR'
287
 
288
  BEGIN
289
 
290
    proc_common_wait_until_low(mm_clk, mm_rst);
291
 
292
    -- We have to open the file explicitely so we can check the status
293
    file_open(open_status_rd, rd_file, rd_filename, read_mode);
294
 
295
    -- open_status may throw an error if the file is being written to by some other program
296
    IF open_status_rd=open_ok THEN
297
 
298
      IF NOT endfile(rd_file) THEN
299
        -- The file is not empty: process its contents
300
 
301
        -- Read a line from it, first line indicates RD or WR
302
        readline(rd_file, rd_line);
303
        read(rd_line, v_rd_wr_str);
304
 
305
        -- The second line represents the address offset:
306
        readline(rd_file, rd_line);
307
        hread(rd_line, v_addr_slv);  -- read the string as HEX and assign to SLV.
308
 
309
        -- Write only: The third line contains the data to write:
310
        IF v_rd_wr_str="WR" THEN
311
          readline(rd_file, rd_line);
312
          hread(rd_line, v_data_slv);  -- read the string as HEX and assign to SLV.
313
        END IF;
314
 
315
        -- We're done reading MM request from the .ctrl file.
316
        -- Clear the .ctrl file by closing and recreating it, because we don't want to do the same
317
        -- MM request again the next time this procedure is called.
318
        file_close(rd_file);
319
        mmf_file_create(rd_filename);
320
 
321
        -- Execute the MM request to the MM slave
322
        IF v_rd_wr_str="WR" THEN
323
          print_str("[" & time_to_str(now) & "] " & rd_filename & ": Writing 0x" & slv_to_hex(v_data_slv) & " to address 0x" & slv_to_hex(v_addr_slv));
324
          -- Treat 32 bit hex data from file as 32 bit VHDL INTEGER, so need to use signed TO_SINT() to avoid out of NATURAL range
325
          -- warning in simulation due to '1' sign bit, because unsigned VHDL NATURAL only fits 31 bits
326
          proc_mem_mm_bus_wr(TO_UINT(v_addr_slv), TO_SINT(v_data_slv), mm_clk, mm_miso, mm_mosi);
327
 
328
        ELSIF v_rd_wr_str="RD" THEN
329
          proc_mem_mm_bus_rd(TO_UINT(v_addr_slv), mm_clk, mm_miso, mm_mosi);
330
          IF rd_latency>0 THEN
331
            proc_mem_mm_bus_rd_latency(rd_latency, mm_clk);
332
          END IF;
333
          v_data_slv := mm_miso.rddata(31 DOWNTO 0);
334
          print_str("[" & time_to_str(now) & "] " & rd_filename & ": Reading from address 0x" & slv_to_hex(v_addr_slv) & ": 0x" & slv_to_hex(v_data_slv));
335
 
336
          -- Write the RD response read data to the .stat file
337
          file_open(open_status_wr, wr_file, wr_filename, write_mode);
338
          hwrite(wr_line, v_data_slv);
339
          writeline(wr_file, wr_line);
340
          file_close(wr_file);
341
        END IF;
342
 
343
      ELSE
344
        -- Nothing to process; wait one MM clock cycle.
345
        proc_common_wait_some_cycles(mm_clk, 1);
346
      END IF;
347
 
348
    ELSE
349
      REPORT "mmf_mm_from_file() could not open " & rd_filename & " at " & time_to_str(now) SEVERITY NOTE;
350
      -- Try again next time; wait one MM clock cycle.
351
      proc_common_wait_some_cycles(mm_clk, 1);
352
    END IF;
353
 
354
    -- The END implicitely close the rd_file, if still necessary.
355
  END;
356
 
357
 
358
  PROCEDURE mmf_sim_ctrl_from_file(rd_filename: IN STRING;
359
                                   wr_filename: IN STRING) IS
360
 
361
    FILE rd_file : TEXT;
362
    FILE wr_file : TEXT;
363
 
364
    VARIABLE open_status_rd: file_open_status;
365
    VARIABLE open_status_wr: file_open_status;
366
 
367
    VARIABLE rd_line : LINE;
368
    VARIABLE wr_line : LINE;
369
 
370
    VARIABLE v_rd_wr_str : STRING(1 TO 12); -- "GET_SIM_TIME"
371
 
372
  BEGIN
373
 
374
    -- We have to open the file explicitely so we can check the status
375
    file_open(open_status_rd, rd_file, rd_filename, read_mode);
376
 
377
    -- open_status may throw an error if the file is being written to by some other program
378
    IF open_status_rd=open_ok THEN
379
 
380
      IF NOT endfile(rd_file) THEN
381
        -- The file is not empty: process its contents
382
 
383
        -- Read a line from it, interpret the simulation request
384
        readline(rd_file, rd_line);
385
        read(rd_line, v_rd_wr_str);
386
 
387
        -- We're done reading this simulation request .ctrl file. Clear the file by closing and recreating it.
388
        file_close(rd_file);
389
        mmf_file_create(rd_filename);
390
 
391
        -- Execute the simulation request
392
        IF v_rd_wr_str="GET_SIM_TIME" THEN
393
          -- Write the GET_SIM_TIME response time NOW to the .stat file
394
          file_open(open_status_wr, wr_file, wr_filename, write_mode);
395
          write(wr_line, time_to_str(now));
396
          writeline(wr_file, wr_line);
397
          file_close(wr_file);
398
        END IF;
399
 
400
      ELSE
401
        -- Nothing to process; wait in procedure mmf_poll_sim_ctrl_file
402
        NULL;
403
      END IF;
404
 
405
    ELSE
406
      REPORT "mmf_mm_from_file() could not open " & rd_filename & " at " & time_to_str(now) SEVERITY NOTE;
407
      -- Try again next time; wait in procedure mmf_poll_sim_ctrl_file
408
    END IF;
409
 
410
    -- The END implicitely close the rd_file, if still necessary.
411
  END;
412
 
413
 
414
  PROCEDURE mmf_poll_sim_ctrl_file(rd_file_name: IN STRING; wr_file_name : IN STRING) IS
415
  BEGIN
416
    -- Create the ctrl file that we're going to read from
417
    print_str("[" & time_to_str(now) & "] " & rd_file_name & ": Created" );
418
    mmf_file_create(rd_file_name);
419
 
420
    WHILE TRUE LOOP
421
      mmf_sim_ctrl_from_file(rd_file_name, wr_file_name);
422
      WAIT FOR 1 ns;
423
    END LOOP;
424
 
425
  END;
426
 
427
 
428
  PROCEDURE mmf_poll_sim_ctrl_file(SIGNAL mm_clk  : IN STD_LOGIC;
429
                                   rd_file_name: IN STRING; wr_file_name : IN STRING) IS
430
  BEGIN
431
    -- Create the ctrl file that we're going to read from
432
    print_str("[" & time_to_str(now) & "] " & rd_file_name & ": Created" );
433
    mmf_file_create(rd_file_name);
434
 
435
    WHILE TRUE LOOP
436
      mmf_sim_ctrl_from_file(rd_file_name, wr_file_name);
437
      proc_common_wait_some_cycles(mm_clk, 1);
438
    END LOOP;
439
 
440
  END;
441
 
442
 
443
  PROCEDURE mmf_wait_for_file_status(rd_filename   : IN STRING;  -- file name with extension
444
                                     exit_on_empty : IN BOOLEAN;
445
                                     SIGNAL mm_clk : IN STD_LOGIC) IS
446
    FILE     rd_file        : TEXT;
447
    VARIABLE open_status_rd : file_open_status;
448
    VARIABLE v_endfile      : BOOLEAN;
449
  BEGIN
450
    -- Check on falling_edge(mm_clk) because mmf_mm_from_file() operates on rising_edge(mm_clk)
451
    -- Note: In fact the file IO also works fine when rising_edge() is used, but then
452
    --       tb_tb_mm_file.vhd takes about 1% more mm_clk cycles
453
    WAIT UNTIL falling_edge(mm_clk);
454
 
455
    -- Keep reading the file until it has become empty by some other program
456
    WHILE TRUE LOOP
457
      -- Open the file in read mode to check whether it is empty
458
      file_open(open_status_rd, rd_file, rd_filename, read_mode);
459
      -- open_status may throw an error if the file is being written to by some other program
460
      IF open_status_rd=open_ok THEN
461
        v_endfile := endfile(rd_file);
462
        file_close(rd_file);
463
        IF exit_on_empty THEN
464
          IF v_endfile THEN
465
            -- The file is empty; continue
466
            EXIT;
467
          ELSE
468
            -- The file is not empty; wait one MM clock cycle.
469
            WAIT UNTIL falling_edge(mm_clk);
470
          END IF;
471
        ELSE
472
          IF v_endfile THEN
473
            -- The file is empty; wait one MM clock cycle.
474
            WAIT UNTIL falling_edge(mm_clk);
475
          ELSE
476
            -- The file is not empty; continue
477
            EXIT;
478
          END IF;
479
        END IF;
480
      ELSE
481
        REPORT "mmf_wait_for_file_status() could not open " & rd_filename & " at " & time_to_str(now) SEVERITY NOTE;
482
        WAIT UNTIL falling_edge(mm_clk);
483
      END IF;
484
    END LOOP;
485
    -- The END implicitely close the file, if still necessary.
486
  END;
487
 
488
  PROCEDURE mmf_wait_for_file_empty(rd_filename   : IN STRING;  -- file name with extension
489
                                    SIGNAL mm_clk : IN STD_LOGIC) IS
490
  BEGIN
491
    mmf_wait_for_file_status(rd_filename, TRUE, mm_clk);
492
  END;
493
 
494
  PROCEDURE mmf_wait_for_file_not_empty(rd_filename   : IN STRING;  -- file name with extension
495
                                        SIGNAL mm_clk : IN STD_LOGIC) IS
496
  BEGIN
497
    mmf_wait_for_file_status(rd_filename, FALSE, mm_clk);
498
  END;
499
 
500
  PROCEDURE mmf_mm_bus_wr(filename      : IN STRING;   -- file name without extension
501
                          wr_addr       : IN INTEGER;  -- use integer to support full 32 bit range
502
                          wr_data       : IN INTEGER;
503
                          SIGNAL mm_clk : IN STD_LOGIC) IS
504
    CONSTANT ctrl_filename  : STRING := filename & ".ctrl";
505
    FILE     ctrl_file      : TEXT;
506
    VARIABLE open_status_wr : file_open_status;
507
    VARIABLE wr_line        : LINE;
508
 
509
  BEGIN
510
    -- Write MM WR access to the .ctrl file.
511
    -- The MM device is ready for a new MM request, because any previous MM request has finished at
512
    -- mmf_mm_bus_wr() or mmf_mm_bus_rd() procedure exit, therefore just overwrite the .ctrl file.
513
    file_open(open_status_wr, ctrl_file, ctrl_filename, write_mode);
514
    -- open_status may throw an error if the file is being written to by some other program
515
    IF open_status_wr=open_ok THEN
516
      write(wr_line, STRING'("WR"));
517
      writeline(ctrl_file, wr_line);
518
      hwrite(wr_line, TO_SVEC(wr_addr, c_word_w));
519
      writeline(ctrl_file, wr_line);
520
      hwrite(wr_line, TO_SVEC(wr_data, c_word_w));
521
      writeline(ctrl_file, wr_line);
522
      file_close(ctrl_file);
523
    ELSE
524
      REPORT "mmf_mm_bus_wr() could not open " & ctrl_filename & " at " & time_to_str(now) SEVERITY NOTE;
525
    END IF;
526
 
527
    -- Prepare for next MM request
528
    -- Keep reading the .ctrl file until it is empty, to ensure that the MM device is ready for a new MM request
529
    mmf_wait_for_file_empty(ctrl_filename, mm_clk);
530
 
531
    -- The END implicitely close the ctrl_file, if still necessary.
532
  END;
533
 
534
  PROCEDURE mmf_mm_bus_rd(filename       : IN STRING;   -- file name without extension
535
                          rd_latency     : IN NATURAL;
536
                          rd_addr        : IN INTEGER;  -- use integer to support full 32 bit range
537
                          SIGNAL rd_data : OUT STD_LOGIC_VECTOR(c_word_w-1 DOWNTO 0);
538
                          SIGNAL mm_clk  : IN STD_LOGIC) IS
539
    CONSTANT ctrl_filename  : STRING := filename & ".ctrl";
540
    CONSTANT stat_filename  : STRING := filename & ".stat";
541
    FILE     ctrl_file      : TEXT;
542
    FILE     stat_file      : TEXT;
543
    VARIABLE open_status_wr : file_open_status;
544
    VARIABLE open_status_rd : file_open_status;
545
    VARIABLE wr_line        : LINE;
546
    VARIABLE rd_line        : LINE;
547
    VARIABLE v_rd_data      : STD_LOGIC_VECTOR(c_word_w-1 DOWNTO 0);
548
 
549
  BEGIN
550
    -- Clear the .stat file by recreating it, because we don't want to do read old file data again
551
    mmf_file_create(stat_filename);
552
 
553
    -- Write MM RD access to the .ctrl file.
554
    -- The MM device is ready for a new MM request, because any previous MM request has finished at
555
    -- mmf_mm_bus_wr() or mmf_mm_bus_rd() procedure exit, therefore just overwrite the .ctrl file.
556
    file_open(open_status_wr, ctrl_file, ctrl_filename, write_mode);
557
    -- open_status may throw an error if the file is being written to by some other program
558
    IF open_status_wr=open_ok THEN
559
      write(wr_line, STRING'("RD"));
560
      writeline(ctrl_file, wr_line);
561
      hwrite(wr_line, TO_SVEC(rd_addr, c_word_w));
562
      writeline(ctrl_file, wr_line);
563
      file_close(ctrl_file);
564
    ELSE
565
      REPORT "mmf_mm_bus_rd() could not open " & ctrl_filename & " at " & time_to_str(now) SEVERITY FAILURE;
566
    END IF;
567
 
568
    -- Wait until the MM RD access has written the read data to the .stat file
569
    mmf_wait_for_file_not_empty(stat_filename, mm_clk);
570
 
571
    -- Read the MM RD access read data from the .stat file
572
    file_open(open_status_rd, stat_file, stat_filename, read_mode);
573
    -- open_status may throw an error if the file is being written to by some other program
574
    IF open_status_rd=open_ok THEN
575
      readline(stat_file, rd_line);
576
      hread(rd_line, v_rd_data);
577
      file_close(stat_file);
578
      rd_data <= v_rd_data;
579
      -- wait to ensure rd_data has got v_rd_data, otherwise rd_data still holds the old data on procedure exit
580
      -- the wait should be < mm_clk period/2 to not affect the read rate
581
      WAIT FOR 1 fs;
582
    ELSE
583
      REPORT "mmf_mm_bus_rd() could not open " & stat_filename & " at " & time_to_str(now) SEVERITY FAILURE;
584
    END IF;
585
 
586
    -- No need to prepare for next MM request, because:
587
    -- . the .ctrl file must already be empty because the .stat file was there
588
    -- . the .stat file will be cleared on this procedure entry
589
 
590
    -- The END implicitely closes the files, if still necessary
591
  END;
592
 
593
  -- rd_latency = 1
594
  PROCEDURE mmf_mm_bus_rd(filename       : IN STRING;
595
                          rd_addr        : IN INTEGER;
596
                          SIGNAL rd_data : OUT STD_LOGIC_VECTOR(c_word_w-1 DOWNTO 0);
597
                          SIGNAL mm_clk  : IN STD_LOGIC) IS
598
  BEGIN
599
    mmf_mm_bus_rd(filename, 1, rd_addr, rd_data, mm_clk);
600
  END;
601
 
602
  PROCEDURE mmf_mm_wait_until_value(filename         : IN STRING;   -- file name without extension
603
                                    rd_addr          : IN INTEGER;
604
                                    c_representation : IN STRING;  -- treat rd_data as "SIGNED" or "UNSIGNED" 32 bit word
605
                                    SIGNAL rd_data   : INOUT STD_LOGIC_VECTOR(c_word_w-1 DOWNTO 0);
606
                                    c_condition      : IN STRING;  -- ">", ">=", "=", "<=", "<", "/="
607
                                    c_rd_value       : IN INTEGER;
608
                                    c_rd_interval    : IN TIME;
609
                                    SIGNAL mm_clk    : IN STD_LOGIC) IS
610
  BEGIN
611
    WHILE TRUE LOOP
612
      -- Read current 
613
      mmf_mm_bus_rd(filename, rd_addr, rd_data, mm_clk);  -- only read low part
614
      IF c_representation="SIGNED" THEN
615
        IF    c_condition=">"  THEN IF TO_SINT(rd_data)> c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;
616
        ELSIF c_condition=">=" THEN IF TO_SINT(rd_data)>=c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;
617
        ELSIF c_condition="/=" THEN IF TO_SINT(rd_data)/=c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;
618
        ELSIF c_condition="<=" THEN IF TO_SINT(rd_data)<=c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;
619
        ELSIF c_condition="<"  THEN IF TO_SINT(rd_data)< c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;
620
        ELSE                        IF TO_SINT(rd_data) =c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;  -- default: "="
621
        END IF;
622
      ELSE  -- default: UNSIGED
623
        IF    c_condition=">"  THEN IF TO_UINT(rd_data)> c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;
624
        ELSIF c_condition=">=" THEN IF TO_UINT(rd_data)>=c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;
625
        ELSIF c_condition="/=" THEN IF TO_UINT(rd_data)/=c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;
626
        ELSIF c_condition="<=" THEN IF TO_UINT(rd_data)<=c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;
627
        ELSIF c_condition="<"  THEN IF TO_UINT(rd_data)< c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;
628
        ELSE                        IF TO_UINT(rd_data) =c_rd_value THEN EXIT; ELSE WAIT FOR c_rd_interval; END IF;  -- default: "="
629
        END IF;
630
      END IF;
631
    END LOOP;
632
  END mmf_mm_wait_until_value;
633
 
634
 
635
  PROCEDURE mmf_sim_get_now(filename       : IN STRING;   -- file name without extension
636
                            SIGNAL rd_now  : OUT STRING;
637
                            SIGNAL mm_clk  : IN STD_LOGIC) IS
638
    CONSTANT ctrl_filename  : STRING := filename & ".ctrl";
639
    CONSTANT stat_filename  : STRING := filename & ".stat";
640
    FILE     ctrl_file      : TEXT;
641
    FILE     stat_file      : TEXT;
642
    VARIABLE open_status_wr : file_open_status;
643
    VARIABLE open_status_rd : file_open_status;
644
    VARIABLE wr_line        : LINE;
645
    VARIABLE rd_line        : LINE;
646
    VARIABLE v_rd_now       : STRING(rd_now'RANGE);
647
 
648
  BEGIN
649
    -- Clear the sim.stat file by recreating it, because we don't want to do read old simulator status again
650
    mmf_file_create(stat_filename);
651
 
652
    -- Write GET_SIM_TIME to the sim.ctrl file
653
    -- The simulation is ready for a new simulation status request, because any previous simulation status request has finished at
654
    -- mmf_sim_get_now() procedure exit, therefore just overwrite the .ctrl file.
655
    file_open(open_status_wr, ctrl_file, ctrl_filename, write_mode);
656
    -- open_status may throw an error if the file is being written to by some other program
657
    IF open_status_wr=open_ok THEN
658
      write(wr_line, STRING'("GET_SIM_TIME"));
659
      writeline(ctrl_file, wr_line);
660
      file_close(ctrl_file);
661
    ELSE
662
      REPORT "mmf_sim_get_now() could not open " & ctrl_filename & " at " & time_to_str(now) SEVERITY FAILURE;
663
    END IF;
664
 
665
    -- Wait until the simulation has written the simulation status to the sim.stat file
666
    mmf_wait_for_file_not_empty(stat_filename, mm_clk);
667
 
668
    -- Read the GET_SIM_TIME simulation status from the .stat file
669
    file_open(open_status_rd, stat_file, stat_filename, read_mode);
670
    -- open_status may throw an error if the file is being written to by some other program
671
    IF open_status_rd=open_ok THEN
672
      readline(stat_file, rd_line);
673
      read(rd_line, v_rd_now);
674
      file_close(stat_file);
675
      rd_now <= v_rd_now;
676
      print_str("GET_SIM_TIME = " & v_rd_now & " at " & time_to_str(now));
677
    ELSE
678
      REPORT "mmf_sim_get_now() could not open " & stat_filename & " at " & time_to_str(now) SEVERITY FAILURE;
679
    END IF;
680
 
681
    -- No need to prepare for next simulation status request, because:
682
    -- . the .ctrl file must already be empty because the .stat file was there
683
    -- . the .stat file will be cleared on this procedure entry
684
 
685
    -- The END implicitely closes the files, if still necessary
686
  END;
687
 
688
  -- Functions to create prefixes for the mmf file filename
689
  FUNCTION mmf_prefix(name : STRING; index : NATURAL) RETURN STRING IS
690
  BEGIN
691
    RETURN name & "_" & int_to_str(index) & "_";
692
  END;
693
 
694
  FUNCTION mmf_tb_prefix(tb : INTEGER) RETURN STRING IS
695
  BEGIN
696
    RETURN mmf_prefix("TB", tb);
697
  END;
698
 
699
  FUNCTION mmf_subrack_prefix(subrack : INTEGER) RETURN STRING IS
700
  BEGIN
701
    RETURN mmf_prefix("SUBRACK", subrack);
702
  END;
703
 
704
  -- Functions to create mmf file prefix that is unique per slave, for increasing number of hierarchy levels:
705
  FUNCTION mmf_slave_prefix(dir_path, s0 : STRING; i0 : NATURAL) RETURN STRING IS
706
  BEGIN
707
    RETURN dir_path & mmf_prefix(s0, i0);
708
  END;
709
 
710
  FUNCTION mmf_slave_prefix(dir_path, s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL) RETURN STRING IS
711
  BEGIN
712
    RETURN dir_path & mmf_prefix(s0, i0) & mmf_prefix(s1, i1);
713
  END;
714
 
715
  FUNCTION mmf_slave_prefix(dir_path, s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL) RETURN STRING IS
716
  BEGIN
717
    RETURN dir_path & mmf_prefix(s0, i0) & mmf_prefix(s1, i1) & mmf_prefix(s2, i2);
718
  END;
719
 
720
  FUNCTION mmf_slave_prefix(dir_path, s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL; s3 : STRING; i3 : NATURAL) RETURN STRING IS
721
  BEGIN
722
    RETURN dir_path & mmf_prefix(s0, i0) & mmf_prefix(s1, i1) & mmf_prefix(s2, i2) & mmf_prefix(s3, i3);
723
  END;
724
 
725
  FUNCTION mmf_slave_prefix(dir_path, s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL; s3 : STRING; i3 : NATURAL; s4 : STRING; i4 : NATURAL) RETURN STRING IS
726
  BEGIN
727
    RETURN dir_path & mmf_prefix(s0, i0) & mmf_prefix(s1, i1) & mmf_prefix(s2, i2) & mmf_prefix(s3, i3) & mmf_prefix(s4, i4);
728
  END;
729
 
730
  -- Use local dir_path  
731
  FUNCTION mmf_slave_prefix(s0 : STRING; i0 : NATURAL) RETURN STRING IS
732
  BEGIN
733
    RETURN c_mmf_local_dir_path & mmf_prefix(s0, i0);
734
  END;
735
 
736
  FUNCTION mmf_slave_prefix(s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL) RETURN STRING IS
737
  BEGIN
738
    RETURN c_mmf_local_dir_path & mmf_prefix(s0, i0) & mmf_prefix(s1, i1);
739
  END;
740
 
741
  FUNCTION mmf_slave_prefix(s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL) RETURN STRING IS
742
  BEGIN
743
    RETURN c_mmf_local_dir_path & mmf_prefix(s0, i0) & mmf_prefix(s1, i1) & mmf_prefix(s2, i2);
744
  END;
745
 
746
  FUNCTION mmf_slave_prefix(s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL; s3 : STRING; i3 : NATURAL) RETURN STRING IS
747
  BEGIN
748
    RETURN c_mmf_local_dir_path & mmf_prefix(s0, i0) & mmf_prefix(s1, i1) & mmf_prefix(s2, i2) & mmf_prefix(s3, i3);
749
  END;
750
 
751
  FUNCTION mmf_slave_prefix(s0 : STRING; i0 : NATURAL; s1 : STRING; i1 : NATURAL; s2 : STRING; i2 : NATURAL; s3 : STRING; i3 : NATURAL; s4 : STRING; i4 : NATURAL) RETURN STRING IS
752
  BEGIN
753
    RETURN c_mmf_local_dir_path & mmf_prefix(s0, i0) & mmf_prefix(s1, i1) & mmf_prefix(s2, i2) & mmf_prefix(s3, i3) & mmf_prefix(s4, i4);
754
  END;
755
 
756
END mm_file_pkg;
757
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.