CANtestLib.vhd 166 KB
Newer Older
1
--------------------------------------------------------------------------------
2
-- 
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
3
-- CTU CAN FD IP Core
4 5 6 7
-- Copyright (C) 2015-2018
-- 
-- Authors:
--     Ondrej Ille <ondrej.ille@gmail.com>
8
--     Martin Jerabek <martin.jerabek01@gmail.com>
9 10
-- 
-- Project advisors: 
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
11 12
-- 	Jiri Novak <jnovak@fel.cvut.cz>
-- 	Pavel Pisa <pisa@cmp.felk.cvut.cz>
13
-- 
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
14 15 16
-- Department of Measurement         (http://meas.fel.cvut.cz/)
-- Faculty of Electrical Engineering (http://www.fel.cvut.cz)
-- Czech Technical University        (http://www.cvut.cz/)
17
-- 
18 19 20 21 22
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this VHDL component and associated documentation files (the "Component"),
-- to deal in the Component without restriction, including without limitation
-- the rights to use, copy, modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Component, and to permit persons to whom the
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
23
-- Component is furnished to do so, subject to the following conditions:
24
-- 
25
-- The above copyright notice and this permission notice shall be included in
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
26
-- all copies or substantial portions of the Component.
27
-- 
28 29 30 31
-- THE COMPONENT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHTHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33
-- FROM, OUT OF OR IN CONNECTION WITH THE COMPONENT OR THE USE OR OTHER DEALINGS
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
34
-- IN THE COMPONENT.
35
-- 
36 37
-- The CAN protocol is developed by Robert Bosch GmbH and protected by patents.
-- Anybody who wants to implement this IP core on silicon has to obtain a CAN
38
-- protocol license from Bosch.
39
-- 
40 41
--------------------------------------------------------------------------------

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
42
--------------------------------------------------------------------------------
43
--  Purpose:
Martin Jeřábek's avatar
Martin Jeřábek committed
44
--    Main test library for CTU CAN FD controller. Contains all test resources
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
45 46 47 48 49
--    for running CANTest framework and low-level access functions.
--
--    Note that in this library several types are (nearly) the same as types
--    in synthesizable code! This is done on purpose, to avoid using wrongly
--    defined values. These types are defined from documentation manually.
50
--
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
51
--------------------------------------------------------------------------------
52 53
-- Revision History:
--    27.5.2016   Created file
Martin Jeřábek's avatar
Martin Jeřábek committed
54 55
--    13.1.2017   Added formatting of identifier in CAN_send_frame,
--                CAN_read_frame to fit the native decimal interpretation
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
56 57 58 59
--                (the same way as in C driver)
--    27.11.2017  Added "reset_test" function fix. Implemented reset synchroniser
--                to avoid async reset in the core. As consequnce after the core
--                reset is released, the core has to wait at least TWO clock
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
60
--                cycles till the reset is synchronised and deasserted.
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
61 62
--    06.02.2018  Modified the library to work with generated constants from the
--                8 bit register map generated from IP-XACT.
63
--    09.02.2018  Added support fow RWCNT field in the SW_CAN_Frame.
Martin Jeřábek's avatar
Martin Jeřábek committed
64
--    15.02.2018  Added support for TXT Buffer commands in CAN Send frame
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
65 66 67
--                procedure.
--    23.02.2018  Corrected "CAN_generate_frame" function for proper placement
--                of BASE identifier to unsigned value.
68 69 70
--     28.4.2018  Converted TXT Buffer access functions to use generated macros.
--      1.5.2018  1. Added HAL layer types and functions.
--                2. Added Byte enable support to memory access functions.
71
--      7.6.2018  Added "CAN_insert_TX_frame" procedure.
Martin Jeřábek's avatar
Martin Jeřábek committed
72
--     18.6.2018  Added optimized clock_gen_proc, timestamp_gen_proc procedures.
73
--     15.9.2018  Added support for message filter manipulation!
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
74 75
--     27.9.2018  Added burst support for avalon access. Added option to read
--                frame from RX Buffer via burst partially!
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
76
--------------------------------------------------------------------------------
77

78 79 80
Library ieee;
USE IEEE.std_logic_1164.all;
USE IEEE.numeric_std.ALL;
81 82
use STD.textio.all;
use IEEE.std_logic_textio.all;
83 84
USE ieee.math_real.ALL;
USE work.randomLib.All;
85
use work.can_constants.all;
86

87
use work.CAN_FD_register_map.all;
88
use work.CAN_FD_frame_format.all;
89

90 91
library vunit_lib;
context vunit_lib.vunit_context;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
92

93 94
package CANtestLib is

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    ----------------------------------------------------------------------------
    ----------------------------------------------------------------------------
    -- Types
    ----------------------------------------------------------------------------
    ----------------------------------------------------------------------------

    ----------------------------------------------------------------------------
    --Common test types
    ----------------------------------------------------------------------------

    -- Logger severity type (severities in increasing order)
    type log_lvl_type is (
        info_l,
        warning_l,
        error_l
    );
111

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
112 113 114 115 116
    -- Behaviour of the test when error occurs
    type err_beh_type is (
        quit,
        go_on
    );
117

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
118 119 120 121 122 123
    -- Status of test
    type test_status_type is (
        waiting,
        running,
        passed,
        failed
124
    );
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
125 126


127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    ----------------------------------------------------------------------------
    -- Memory access types
    ----------------------------------------------------------------------------

    -- Avalon bus access size (to support "byte enable" functionality)
    type aval_access_size is (
        BIT_8,
        BIT_16,
        BIT_32
    );

    ----------------------------------------------------------------------------
    -- Core common types for register map (generated in future)
    -- Implemented to create HAL like abstraction and allow easier modifications
    -- of register map without touching the test code!
    ----------------------------------------------------------------------------
Martin Jeřábek's avatar
Martin Jeřábek committed
143

144 145 146 147 148 149 150 151 152 153
    -- Controller modes
    type SW_mode is record
        reset                   :   boolean;
        listen_only             :   boolean;
        self_test               :   boolean;
        acceptance_filter       :   boolean;
        flexible_data_rate      :   boolean;
        rtr_pref                :   boolean;
        tripple_sampling        :   boolean;
        acknowledge_forbidden   :   boolean;
154 155
        internal_loopback       :   boolean;
        iso_fd_support          :   boolean;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    end record;


    -- Controller commands
    type SW_command is record
        abort_transmission      :   boolean;
        release_rec_buffer      :   boolean;
        clear_data_overrun      :   boolean;
    end record;


    -- Controller status
    type SW_status is record
        receive_buffer          :   boolean;
        data_overrun            :   boolean;
171
        tx_buffer_empty         :   boolean;
172 173 174 175 176 177 178 179
        error_transmission      :   boolean;
        receiver                :   boolean;
        transmitter             :   boolean;
        error_warning           :   boolean;
        bus_status              :   boolean;
    end record;


180
    -- Interrupt sources
181 182 183 184 185 186 187 188 189 190 191 192 193
    type SW_interrupts is record
        receive_int             :   boolean;
        transmitt_int           :   boolean;
        error_warning_int       :   boolean;
        data_overrun_int        :   boolean;
        error_passive_int       :   boolean;
        arb_lost_int            :   boolean;
        bus_error_int           :   boolean;
        logger_finished_int     :   boolean;
        rx_buffer_full_int      :   boolean;
        bit_rate_shift_int      :   boolean;
        rx_buffer_not_empty_int :   boolean;
        tx_buffer_hw_cmd        :   boolean;
Martin Jeřábek's avatar
Martin Jeřábek committed
194
    end record;
195 196 197 198 199 200 201 202

    -- Fault confinement states
    type SW_fault_state is (
        fc_error_active,
        fc_error_passive,
        fc_bus_off
    );

203 204 205 206 207
    -- Fault confinement state thresholds
    type SW_fault_thresholds is record
        ewl                     :   natural range 0 to 255;
        erp                     :   natural range 0 to 255;
    end record;
208

209
    -- Error counters (Normal and Special)
210 211 212 213 214 215 216 217
    type SW_error_counters is record
        rx_counter              :   natural range 0 to 2 ** 16 - 1;
        tx_counter              :   natural range 0 to 2 ** 16 - 1;
        err_norm                :   natural range 0 to 2 ** 16 - 1;
        err_fd                  :   natural range 0 to 2 ** 16 - 1;
    end record;


218 219 220 221 222 223 224
    -- Traffic counters
    type SW_traffic_counters is record
        rx_frames               :   natural;
        tx_frames               :   natural;
    end record;


225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    -- RX Buffer info and status
    type SW_RX_Buffer_info is record
        rx_buff_size            :   natural range 0 to 2 ** 13 - 1;
        rx_mem_free             :   natural range 0 to 2 ** 13 - 1;
        rx_write_pointer        :   natural range 0 to 2 ** 13 - 1;
        rx_read_pointer         :   natural range 0 to 2 ** 13 - 1;
        rx_full                 :   boolean;
        rx_empty                :   boolean;
        rx_frame_count          :   natural range 0 to 2 ** 11 - 1;
    end record;


    -- RX Buffer options
    type SW_RX_Buffer_options is record
        rx_time_stamp_options   :   boolean;
    end record;


    -- TXT Buffer priorities
    type SW_TXT_priority is record
        txt_buffer_1_priority   :   natural range 0 to 7;
        txt_buffer_2_priority   :   natural range 0 to 7;
        txt_buffer_3_priority   :   natural range 0 to 7;
        txt_buffer_4_priority   :   natural range 0 to 7;
    end record;




Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
254 255 256 257
    ----------------------------------------------------------------------------
    -- Message filter types
    ----------------------------------------------------------------------------

Martin Jeřábek's avatar
Martin Jeřábek committed
258
    -- Frame information on input of message filter
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
259 260 261 262
    type mess_filter_input_type is record
        rec_ident_in            :   std_logic_vector(28 downto 0);
        ident_type              :   std_logic;
        frame_type              :   std_logic;
Martin Jeřábek's avatar
Martin Jeřábek committed
263
        rec_ident_valid         :   std_logic;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
264 265 266 267 268 269 270 271 272
    end record;

    -- Driving bus values on the input off message filter
    type mess_filter_drv_type is record

        -- Filter A bit mask, control bits and bit value
        drv_filter_A_mask       :   std_logic_vector(28 downto 0);
        drv_filter_A_ctrl       :   std_logic_vector(3 downto 0);
        drv_filter_A_bits       :   std_logic_vector(28 downto 0);
Martin Jeřábek's avatar
Martin Jeřábek committed
273

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
274 275 276 277
        -- Filter B bit mask, control bits and bit value
        drv_filter_B_mask       :   std_logic_vector(28 downto 0);
        drv_filter_B_ctrl       :   std_logic_vector(3 downto 0);
        drv_filter_B_bits       :   std_logic_vector(28 downto 0);
Martin Jeřábek's avatar
Martin Jeřábek committed
278

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
279 280 281 282
        -- Filter C bit mask, control bits and bit value
        drv_filter_C_mask       :   std_logic_vector(28 downto 0);
        drv_filter_C_ctrl       :   std_logic_vector(3 downto 0);
        drv_filter_C_bits       :   std_logic_vector(28 downto 0);
Martin Jeřábek's avatar
Martin Jeřábek committed
283

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
284 285 286 287 288 289 290 291 292 293 294 295 296
        -- Range filter control bits, lower and upper thresholds
        drv_filter_ran_ctrl     :   std_logic_vector(3 downto 0);
        drv_filter_ran_lo_th    :   std_logic_vector(28 downto 0);
        drv_filter_ran_hi_th    :   std_logic_vector(28 downto 0);

        -- Filters are enabled
        drv_filters_ena         :   std_logic;

    end record;

    ----------------------------------------------------------------------------
    -- Prescaler types
    ----------------------------------------------------------------------------
Martin Jeřábek's avatar
Martin Jeřábek committed
297

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
298 299
    -- Driving bus settings for prescaler
    type presc_drv_type is record
Martin Jeřábek's avatar
Martin Jeřábek committed
300

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
301 302 303 304 305 306 307 308 309 310 311 312 313
        -- Time quanta (Nominal and Data)
        drv_tq_nbt              :   std_logic_vector (7 downto 0);
        drv_tq_dbt              :   std_logic_vector (7 downto 0);

        -- Propagation segment (Nominal and Data)
        drv_prs_nbt             :   std_logic_vector (6 downto 0);
        drv_prs_dbt             :   std_logic_vector (5 downto 0);

        -- Phase 1 segment (Nominal and Data)
        drv_ph1_nbt             :   std_logic_vector (5 downto 0);
        drv_ph1_dbt             :   std_logic_vector (4 downto 0);

        -- Phase 2 segment (Nominal and Data)
Martin Jeřábek's avatar
Martin Jeřábek committed
314 315
        drv_ph2_nbt             :   std_logic_vector (5 downto 0);
        drv_ph2_dbt             :   std_logic_vector (4 downto 0);
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
316 317 318 319 320 321 322 323 324

        -- Synchronisation jump width (Nominal and Data)
        drv_sjw_nbt             :   std_logic_vector(4 downto 0);
        drv_sjw_dbt             :   std_logic_vector(4 downto 0);

    end record;

    -- Triggering signals joined to single record
    type presc_triggers_type is record
Martin Jeřábek's avatar
Martin Jeřábek committed
325

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        -- Sample signal (Nominal and Data)
        -- (Used for bus sampling)
        sample_nbt              :   std_logic;
        sample_dbt              :   std_logic;

        -- Sample signal delayed by one clock cycle (Nominal and Data)
        -- (Used for Bit destuffing)
        sample_nbt_del_1        :   std_logic;
        sample_dbt_del_1        :   std_logic;

        -- Sample signal delayed by two clock cycle (Nominal and Data)
        -- (Used for Processing the data by Protocol control)
        sample_nbt_del_2        :   std_logic;
        sample_dbt_del_2        :   std_logic;

        -- Synchronisation signal
        -- (Used to transmitt data)
        sync_nbt                :   std_logic;
        sync_dbt                :   std_logic;

        -- Synchronisation signal by one clock cycle (Nominal and Data)
        -- (Used for Bit stuffing)
        sync_nbt_del_1          :   std_logic;
        sync_dbt_del_1          :   std_logic;
    end record;


    ----------------------------------------------------------------------------
    -- TXT Buffer types
    ----------------------------------------------------------------------------
Martin Jeřábek's avatar
Martin Jeřábek committed
356

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
357 358 359 360 361 362 363 364 365 366
    -- TXT Buffer state (used in test access, not in synthesizable code)
    type SW_TXT_Buffer_state_type is (
        buf_empty,
        buf_ready,
        buf_tx_progress,
        buf_ab_progress,
        buf_aborted,
        buf_failed,
        buf_done
    );
367

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    -- TXT Buffer commands (used in test access, not synthesizable code)
    type SW_TXT_Buffer_command_type is (
        buf_set_empty,
        buf_set_ready,
        buf_set_abort
    );



    ----------------------------------------------------------------------------
    -- Avalon memory interface type
    ----------------------------------------------------------------------------
    type Avalon_mem_type is record
        clk_sys                 :   std_logic;
        data_in                 :   std_logic_vector(31 downto 0);
        data_out                :   std_logic_vector(31 downto 0);
        address                 :   std_logic_vector(23 downto 0);
        scs                     :   std_logic;
        swr                     :   std_logic;
        srd                     :   std_logic;
388
        sbe                     :   std_logic_vector(3 downto 0);
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    end record;


    ----------------------------------------------------------------------------
    -- Main Bus timing configuration type used in feature and sanity tests
    -- (using "naturals" instead of std_logic_vector)
    ----------------------------------------------------------------------------
    type bit_time_config_type is record
         tq_nbt                 :   natural;
         tq_dbt                 :   natural;
         prop_nbt               :   natural;
         ph1_nbt                :   natural;
         ph2_nbt                :   natural;
         sjw_nbt                :   natural;
         prop_dbt               :   natural;
         ph1_dbt                :   natural;
         ph2_dbt                :   natural;
         sjw_dbt                :   natural;
    end record;


410 411
    type SW_CAN_data_type is array (0 to 63) of std_logic_vector(7 downto 0);

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
412 413 414 415 416 417 418 419 420 421 422
    ----------------------------------------------------------------------------
    -- Software CAN Frame type. Used for generation, transmission, reception,
    -- comparison of CAN Frames.
    ----------------------------------------------------------------------------
    type SW_CAN_frame_type is record

        -- CAN Identifier. Decimal value. Note that the value differs for
        -- BASE and EXTENDED Identifiers!
        identifier              :   natural;

        -- Data payload
423
        data                    :   SW_CAN_data_type;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442

        -- Data length code as defined in CAN Standard
        dlc                     :   std_logic_vector(3 downto 0);

        -- Data length in bytes
        data_length             :   natural range 0 to 64;

        -- Identifier type (0 - BASE Format, 1 - Extended Format);
        ident_type              :   std_logic;

        -- Frame type (0 - Normal CAN, 1 - CAN FD)
        frame_format            :   std_logic;

        -- RTR Flag (0 - No RTR Frame, 1 - RTR Frame)
        rtr                     :   std_logic;

        -- Bit rate shift flag
        brs                     :   std_logic;

443 444 445
        -- ESI Flag (Error state indicator)
        esi                     :   std_logic;

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
446 447 448 449 450 451 452 453 454 455 456
        -- Timestamp (as defined in TIMESTAMP_U_W and TIMESTAMP_L_W)
        timestamp               :   std_logic_vector(63 downto 0);

        -- Receive word count field as stored in RX Buffer FRAME_FORM_W.
        -- Indicates number of 32 bit words which Frame ocupies in RX Buffer
        -- without FRAME_FORM_W.
        -- Note that this value is valid only for received frames and has
        -- no meaning in TXT Buffer.
        rwcnt                   :   natural;
    end record;

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

    type SW_CAN_mask_filter_type is (
        filter_A,
        filter_B,
        filter_C  
    );


    type SW_CAN_mask_filter_config is record
        ID_value                :   natural;
        ID_mask                 :   natural;
        ident_type              :   std_logic;
        acc_CAN_2_0             :   boolean;
        acc_CAN_FD              :   boolean;
    end record;


    type SW_CAN_range_filter_config is record
        ID_th_low               :   natural;
        ID_th_high              :   natural;
        ident_type              :   std_logic;        
        acc_CAN_2_0             :   boolean;
        acc_CAN_FD              :   boolean;
    end record;


Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
483 484 485 486 487 488 489 490 491 492 493
    ----------------------------------------------------------------------------
    -- Transceiver delay type
    ----------------------------------------------------------------------------
    type tran_delay_type is record
        tx_delay_sr             :   std_logic_vector(255 downto 0);
        rx_delay_sr             :   std_logic_vector(255 downto 0);
        tx_point                :   std_logic;
        rx_point                :   std_logic;
    end record;


494 495 496 497 498
    ----------------------------------------------------------------------------
    -- Test storage memory
    ----------------------------------------------------------------------------
    type test_mem_type is array (0 to 255) of std_logic_vector(31 downto 0);

Martin Jeřábek's avatar
Martin Jeřábek committed
499 500 501 502 503 504 505 506
    ----------------------------------------------------------------------------
    -- Clock params
    ----------------------------------------------------------------------------
    type generate_clock_precomputed_t is record
        low_time : time;
        high_time : time;
    end record;

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
    ----------------------------------------------------------------------------
    -- Bit sequence generator
    ----------------------------------------------------------------------------
    
    -- Longest possible CAN FD Frame is aroud 700 bits. If each bit has opposite
    -- polarity than previous one, this could use up to 700 entries. Have some
    -- reserve...
    constant BS_MAX_LENGTH      :   natural := 1024;

    type bs_durations_type is array (1 to BS_MAX_LENGTH) of natural;
    type bs_values_type is array (1 to BS_MAX_LENGTH) of std_logic;

    type bit_seq_type is record
        bit_durations           :   bs_durations_type;
        bit_values              :   bs_values_type;
        length                  :   natural;
    end record;

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
525 526 527 528 529 530 531 532 533

    ----------------------------------------------------------------------------
    ----------------------------------------------------------------------------
    -- Constants
    ----------------------------------------------------------------------------
    ----------------------------------------------------------------------------
    constant f100_MHZ           :   natural := 10000;


534 535 536 537 538 539 540 541 542 543 544 545 546 547
    ----------------------------------------------------------------------------
    ----------------------------------------------------------------------------
    -- sanity test Stuff; must be in a package
    ----------------------------------------------------------------------------
    ----------------------------------------------------------------------------
    constant NODE_COUNT : natural := 4;
    type bus_matrix_type is array(1 to NODE_COUNT, 1 to NODE_COUNT) of real;
    type anat_t is array (integer range <>) of natural;
    subtype anat_nc_t is anat_t (1 to NODE_COUNT);

    subtype epsilon_type is anat_nc_t;
    subtype trv_del_type is anat_nc_t;
    subtype timing_config_t is anat_t(1 to 10);

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
548 549 550 551 552 553 554

    ----------------------------------------------------------------------------
    ----------------------------------------------------------------------------
    -- Functions definitions
    ----------------------------------------------------------------------------
    ----------------------------------------------------------------------------

Martin Jeřábek's avatar
Martin Jeřábek committed
555

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
556 557 558 559 560 561 562 563 564
    ----------------------------------------------------------------------------
    -- Function called at the end of the test which evaluates results of test.
    -- If Error threshold is bigger than number of errors which ocurred in the
    -- test, test status will be updated to "passed", if not, test status will
    -- be updated to "failed". After the evaluation test simulation is finished.
    --
    -- Arguments:
    --  error_th        Error threshold.
    --  errors          Number of errors which occurred in the test
Martin Jeřábek's avatar
Martin Jeřábek committed
565
    --  status          Status to be updated
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
566 567
    ----------------------------------------------------------------------------
    procedure evaluate_test(
Martin Jeřábek's avatar
Martin Jeřábek committed
568 569 570
        constant error_th         : in    natural;
        constant errors           : in    natural;
        signal   status           : out   test_status_type
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
571 572 573 574 575 576 577 578
    );


    ----------------------------------------------------------------------------
    -- Generates clock signal for the test with custom period, duty cycle and
    -- clock jitter.
    --
    -- Arguments:
Martin Jeřábek's avatar
Martin Jeřábek committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
    --  par             Precomputed parameters.
    --  out_clk         Generated clock.
    ----------------------------------------------------------------------------
    procedure generate_clock(
        constant par            : in    generate_clock_precomputed_t;
        signal   out_clk        : out   std_logic
    );

    -- deprecated compatibility wrapper (lower performance)
    procedure generate_clock(
        constant period         : in    natural;
        constant duty           : in    natural;
        constant epsilon_ppm    : in    natural;
        signal   out_clk        : out   std_logic
    );

    ----------------------------------------------------------------------------
    -- Combinatorial procedure, which infinitely generates clock signal.
    --
    -- Arguments:
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
599 600 601 602 603
    --  period          Period of generated clock in picoseconds.
    --  duty            Duty cycle of generated clock in percents.
    --  epsilon_ppm     Clock uncertainty (jitter) which is always added to the
    --                  default clock period.
    ----------------------------------------------------------------------------
Martin Jeřábek's avatar
Martin Jeřábek committed
604
    procedure clock_gen_proc(
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
605 606 607 608 609 610 611
        constant period         : in    natural;
        constant duty           : in    natural;
        constant epsilon_ppm    : in    natural;
        signal   out_clk        : out   std_logic
    );


Martin Jeřábek's avatar
Martin Jeřábek committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    ----------------------------------------------------------------------------
    -- Combinatorial procedure, which infinitely generates timestamp.
    --
    -- Arguments:
    --  clk             Input clock.
    --  timestamp       Output timestamp counter.
    ----------------------------------------------------------------------------
    procedure timestamp_gen_proc(
        signal     clk           : in    std_logic;
        signal     timestamp     : out   std_logic_vector(63 downto 0)
    );


    ----------------------------------------------------------------------------
    -- Precompute constants for clock signal generation for the test with
    -- custom period, duty cycle and clock jitter.
    --
    -- Arguments:
    --  period          Period of generated clock in picoseconds.
    --  duty            Duty cycle of generated clock in percents.
    --  epsilon_ppm     Clock uncertainty (jitter) which is always added to the
    --                  default clock period.
    -- Returns: precomputed params
    ----------------------------------------------------------------------------
    function precompute_clock(
        constant period         : in    natural;
        constant duty           : in    natural;
        constant epsilon_ppm    : in    natural
    ) return generate_clock_precomputed_t;

642

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
643 644 645 646 647 648 649 650 651 652 653 654
    ----------------------------------------------------------------------------
    -- Reports message when severity level is lower or equal than severity
    -- of message.
    --
    -- Arguments:
    --  message         String to be reported.
    --  log_severity    Severity level which is set in current test.
    --  log_level       Severity of message
    ----------------------------------------------------------------------------
    procedure log(
        constant message        : in    String;
        constant log_severity   : in    log_lvl_type;
Martin Jeřábek's avatar
Martin Jeřábek committed
655
        constant log_level      : in    log_lvl_type
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
    );


    ----------------------------------------------------------------------------
    -- Asserts reset signal to restart the operated circuit. Then waits until
    -- run condition signal becomes "true" and sets test status to "running" and
    -- sets error counter to 0. Finally, deasserts reset signal.
    --
    -- Arguments:
    --  res_n           Reset signal (active low).
    --  status          Test status to assert "running".
    --  run             Run condition to wait until comes true.
    --  error_ctr       Error counter to erase.
    ----------------------------------------------------------------------------
    procedure reset_test(
        signal res_n            : out   std_logic;
        signal status           : out   test_status_type;
        signal run              : in    boolean;
        signal error_ctr        : out   natural
    );


    ----------------------------------------------------------------------------
    -- Process error ocurrence in the test. This consists of following steps:
    --  1. Increment error counter
    --  2. If Error behaviour is "quit", assert Immediate exit flag to "true".
    --     Otherwise assert Immediate exit flag to "false".
Martin Jeřábek's avatar
Martin Jeřábek committed
683
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
684 685 686 687 688 689
    -- Arguments:
    --  error_ctr       Error counter to increment.
    --  error_beh       Error behaviour to evaluate.
    --  exit_imm        Immediate exit flag to assert.
    ----------------------------------------------------------------------------
    procedure process_error(
Martin Jeřábek's avatar
Martin Jeřábek committed
690 691 692
        signal   error_ctr      : inout natural;
        constant error_beh      : in    err_beh_type;
        signal   exit_imm       : out   boolean
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
693 694 695 696 697
    );


    ----------------------------------------------------------------------------
    -- Print basic testbench information.
Martin Jeřábek's avatar
Martin Jeřábek committed
698
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
699 700 701 702 703 704 705
    -- Arguments:
    --  iterations      Number of iterations to execute in test.
    --  log_level       Severity level which is set in actual frame.
    --  error_beh       Error behaviour of test.
    --  error_tol       Error tolerance of test.
    ----------------------------------------------------------------------------
    procedure print_test_info(
Martin Jeřábek's avatar
Martin Jeřábek committed
706 707 708 709
        constant iterations     : in    natural;
        constant log_level      : in    log_lvl_type;
        constant error_beh      : in    err_beh_type;
        constant error_tol      : in    natural
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
710 711 712
    );


713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
    ----------------------------------------------------------------------------
    -- Sets random counter based on the seed that was given. To differentiate
    -- individual counters, fixed offset can be added.
    --
    -- Arguments:
    --  seed            Random seed input.
    --  offset          Offset of counter
    --  rand_ctr        Random counter to set.
    ----------------------------------------------------------------------------
    procedure apply_rand_seed(
        constant seed            : in   natural;
        constant offset          : in   natural;
        signal   rand_ctr        : out  natural range 0 to RAND_POOL_SIZE
    );


729 730 731 732 733 734
    -- variation of above, only returns the seed value
    function apply_rand_seed(
        constant seed            : in   natural;
        constant offset          : in   natural
    ) return natural;

735

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
736 737 738
    ----------------------------------------------------------------------------
    -- Decode data length code from value as defined in CAN FD Standard to
    -- length of frame in bytes.
Martin Jeřábek's avatar
Martin Jeřábek committed
739
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
740 741
    -- Arguments:
    --  dlc             Data length code as received or transmitted.
Martin Jeřábek's avatar
Martin Jeřábek committed
742
    --  length          Length of CAN Frame in bytes
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
743 744 745 746 747 748 749 750 751 752 753
    ----------------------------------------------------------------------------
    procedure decode_dlc(
        constant dlc            : in    std_logic_vector(3 downto 0);
        variable length         : out   natural
    );


    ----------------------------------------------------------------------------
    -- Decode Read word count value as present in FRAME_FORMAT_W of RX Buffer
    -- from DLC. Read word count value indicates how many 32-bit words will the
    -- buffer occupy in RX Buffer without Frame Format word.
Martin Jeřábek's avatar
Martin Jeřábek committed
754
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
755 756 757 758 759 760 761 762 763 764
    -- Arguments:
    --  dlc             Data length code to decode as transmitted or received.
    --  rwcnt           Read word count value.
    ----------------------------------------------------------------------------
    procedure decode_dlc_rx_buff(
        constant dlc            : in    std_logic_vector(3 downto 0);
        variable rwcnt          : out   natural
    );


765 766 767 768 769 770 771 772 773 774 775 776 777
	----------------------------------------------------------------------------
    -- Decode length do DLC code.
    --
    -- Arguments:
    --  length          Data lenght in bytes.
    --  dlc				Variable where output Data lenght code  will be stored.
    ----------------------------------------------------------------------------
	procedure decode_length(
		constant length			: in	natural;
		variable dlc			: out	std_logic_vector(3 downto 0)
	);


Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
778 779 780
    ----------------------------------------------------------------------------
    -- Decode number of 32-bit words CAN Frame will occupy in RX Buffer
    -- (together with Frame format word).
Martin Jeřábek's avatar
Martin Jeřábek committed
781
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
782 783 784 785 786
    -- Arguments:
    --  dlc             Data length code to decode as transmitted or received.
    --  buff_space      Number of 32-bit words.
    ----------------------------------------------------------------------------
    procedure decode_dlc_buff(
Martin Jeřábek's avatar
Martin Jeřábek committed
787
        constant dlc            : in    std_logic_vector(3 downto 0);
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
788 789
        variable buff_space     : out   natural
    );
Martin Jeřábek's avatar
Martin Jeřábek committed
790

791

792 793 794
    ----------------------------------------------------------------------------
    -- Convert identifier from register format (as stored in IDENTIFIER_W of
    --  TXT Buffers and RX Buffer) to integer value as used by SW.
Martin Jeřábek's avatar
Martin Jeřábek committed
795
    --
796 797 798 799 800 801 802 803 804 805 806 807 808
    -- Arguments:
    --  identifier      Input identifier as stored in IDENTIFIER_W
    --  id_type         Type of identifier (BASE or EXTENDED)
    --  out             Identifier in integer format
    ----------------------------------------------------------------------------
    procedure id_hw_to_sw(
        constant id_in          : in    std_logic_vector(28 downto 0);
        constant id_type        : in    std_logic;
        variable id_out         : out   natural
    );


    ----------------------------------------------------------------------------
Martin Jeřábek's avatar
Martin Jeřábek committed
809
    -- Convert identifier from SW format to register format (as stored in
810
    --  IDENTIFIER_W of TXT Buffers and RX Buffer).
Martin Jeřábek's avatar
Martin Jeřábek committed
811
    --
812 813 814 815 816 817 818 819 820 821 822 823
    -- Arguments:
    --  identifier      Input identifier in integer format (as used by SW).
    --  id_type         Type of identifier (BASE or EXTENDED)
    --  out             Identifier in register format as stored in IDENTIFIER_W.
    ----------------------------------------------------------------------------
    procedure id_sw_to_hw(
        constant id_in          : in    natural;
        constant id_type        : in    std_logic;
        variable id_out         : out   std_logic_vector(28 downto 0)
    );


Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
824 825 826
    ----------------------------------------------------------------------------
    -- Generate simple triggering signals.
    -- Two signals are generated, "sync" and "sample". No bit rate switch is
Martin Jeřábek's avatar
Martin Jeřábek committed
827 828 829
    -- implemented in this procedure. There is random amount of clock cycles
    -- between sync and sample, but not less than "min_diff" and not more than
    -- 10 clock cycles! "min_diff" is intended for determining how many clock
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
830
    -- cycles each circuit needs between bit transmittion and reception!
Martin Jeřábek's avatar
Martin Jeřábek committed
831
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
    -- Arguments:
    --  rand_ctr        Pointer to random pool.
    --  sync            Synchronisation trigger (for transmission)
    --  sample          Sampling trigger (for reception)
    --  clk_sys         Clock signal
    --  min_diff        Minimal gap (in clock cycles) between sync and sample
    --                  signals.
    ----------------------------------------------------------------------------
    procedure generate_simple_trig(
        signal   rand_ctr       : inout natural range 0 to RAND_POOL_SIZE;
        signal   sync           : out   std_logic;
        signal   sample         : out   std_logic;
        signal   clk_sys        : in    std_logic;
        variable min_diff       : in    natural
    );


    procedure generate_trig(
        signal    sync          : out   std_logic;
        signal    sample        : out   std_logic;
        signal    clk_sys       : in    std_logic;
        signal    seg1          : in    natural;
        signal    seg2          : in    natural
    );
Martin Jeřábek's avatar
Martin Jeřábek committed
856

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
857 858 859 860 861 862

    ----------------------------------------------------------------------------
    -- Memory access routines
    ----------------------------------------------------------------------------

    ----------------------------------------------------------------------------
863 864
    -- Execute write access on Avalon memory bus. Does not support unaligned
    -- accesses.
Martin Jeřábek's avatar
Martin Jeřábek committed
865
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
866 867 868
    -- Arguments:
    --  w_data          Data to write.
    --  w_address       Address where to write the data
869
    --  w_size          Size of the access (8 Bit, 16 bit or 32 bit)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
870 871 872 873 874
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure aval_write(
        constant  w_data        : in    std_logic_vector(31 downto 0);
        constant  w_address     : in    std_logic_vector(23 downto 0);
875
        constant  w_size        : in    aval_access_size;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
876 877 878 879 880
        signal    mem_bus       : inout Avalon_mem_type
    );


    ----------------------------------------------------------------------------
881 882
    -- Execute read access on Avalon memory bus. Does not supports unaligned
    -- accesses.
Martin Jeřábek's avatar
Martin Jeřábek committed
883
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
884 885 886
    -- Arguments:
    --  r_data          Variable in which Read data will be returned.
    --  r_address       Address to read the data from.
887
    --  r_size          Size of the access (8 Bit, 16 bit or 32 bit)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
888 889 890 891 892
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure aval_read(
        variable  r_data        : out   std_logic_vector(31 downto 0);
        constant  r_address     : in    std_logic_vector(23 downto 0);
893
        constant  r_size        : in    aval_access_size;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
894 895 896 897
        signal    mem_bus       : inout Avalon_mem_type
    );


Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
    ----------------------------------------------------------------------------
    -- Execute write access on Avalon memory bus via Avalon burst. 
    -- Does not support unaligned accesses. Size of the burst is given by
    -- length of "w_data".
    --
    -- Arguments:
    --  w_data          Data to write (Little endian)
    --  w_address       Address where to start write burst.
    --  stat_burst      True for "stationary" burst where address should not
    --                  be incremented during the burst!
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure aval_write_burst(
        constant  w_data        : in    std_logic_vector;
        constant  w_address     : in    std_logic_vector(23 downto 0);
        constant  stat_burst    : in    boolean := false;
        signal    mem_bus       : inout Avalon_mem_type
    );


    ----------------------------------------------------------------------------
    -- Execute read access on Avalon memory bus via Avalon burst. 
    -- Does not support unaligned accesses. Size of the burst is given by
    -- length of "r_data".
    --
    -- Arguments:
    --  r_data          Data to be read(Little endian)
    --  w_address       Address where to start write burst.
    --  stat_burst      True for "stationary" burst where address should not
    --                  be incremented during the burst!
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure aval_read_burst(
        variable  r_data        : out   std_logic_vector;
        constant  r_address     : in    std_logic_vector(23 downto 0);
        constant  stat_burst    : in    boolean := false;
        signal    mem_bus       : inout Avalon_mem_type
    );


Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
938
    ----------------------------------------------------------------------------
939
    -- Execute write access to CTU CAN FD Core over Avalon Bus. If size is not
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
940 941
    -- specified, 32 bit access is executed. If input data size is other than
    -- 32 bits, burst access is executed.
942 943
    --
    -- Address bits meaning is following:
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
944 945 946 947
    --  [19:16]     Component type (always CAN_COMPONENT_TYPE)
    --  [15:12]     Identifier (Index) of core. Allows to distinguish between
    --              up to 16 instances of CTU CAN FD Core.
    --  [11:0]      Register or Buffer offset within a the core.
Martin Jeřábek's avatar
Martin Jeřábek committed
948
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
949 950 951 952 953
    -- Arguments:
    --  w_data          Data to write to CTU CAN FD Core.
    --  w_offset        Register or buffer offset (bits 11:0).
    --  ID              Index of CTU CAN FD Core instance (bits 15:12)
    --  mem_bus         Avalon memory bus to execute the access on.
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
954 955
    --  stat_burst      If Burst access is executed, address should not be
    --                  incremented during the burst.
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
956 957
    ----------------------------------------------------------------------------
    procedure CAN_write(
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
958
        constant  w_data        : in    std_logic_vector;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
959 960
        constant  w_offset      : in    std_logic_vector(11 downto 0);
        constant  ID            : in    natural range 0 to 15;
961
        signal    mem_bus       : inout Avalon_mem_type;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
962 963
        constant  w_size        : in    aval_access_size := BIT_32;
        constant  stat_burst    : in    boolean := false
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
964 965 966 967
    );


    ----------------------------------------------------------------------------
968
    -- Execute read access from CTU CAN FD Core over Avalon Bus. If size is not
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
969 970
    -- specified, 32 bit access is executed. If input data size is other than
    -- 32 bits, burst access is executed.
971 972
    --
    -- Address bits meaning is following:
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
973 974 975 976
    --  [19:16]     Component type (always CAN_COMPONENT_TYPE)
    --  [15:12]     Identifier (Index) of core. Allows to distinguish between
    --              up to 16 instances of CTU CAN FD Core.
    --  [11:0]      Register or Buffer offset within a the core.
Martin Jeřábek's avatar
Martin Jeřábek committed
977
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
978 979 980 981 982
    -- Arguments:
    --  r_data          Variable in which Read data will be returned.
    --  r_offset        Register or buffer offset (bits 11:0).
    --  ID              Index of CTU CAN FD Core instance (bits 15:12)
    --  mem_bus         Avalon memory bus to execute the access on.
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
983 984
    --  stat_burst      If Burst access is executed, address should not be
    --                  incremented during the burst.
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
985 986
    ----------------------------------------------------------------------------
    procedure CAN_read(
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
987
        variable  r_data        : out   std_logic_vector;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
988 989
        constant  r_offset      : in    std_logic_vector(11 downto 0);
        constant  ID            : in    natural range 0 to 15;
990
        signal    mem_bus       : inout Avalon_mem_type;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
991 992
        constant  r_size        : in    aval_access_size := BIT_32;
        constant  stat_burst    : in    boolean := false
Martin Jeřábek's avatar
Martin Jeřábek committed
993
    );
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
994 995


996 997


Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
998 999
    ----------------------------------------------------------------------------
    -- CAN configuration routines
1000
    ----------------------------------------------------------------------------
Martin Jeřábek's avatar
Martin Jeřábek committed
1001

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1002 1003 1004
    ----------------------------------------------------------------------------
    -- Configure Bus timing on CTU CAN FD Core.
    -- (duration of bit phases, synchronisation jump width, baud-rate prescaler)
Martin Jeřábek's avatar
Martin Jeřábek committed
1005
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1006 1007 1008 1009 1010 1011 1012 1013
    -- Arguments:
    --  bus_timing      Bus timing structure that contains timing configuration
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_configure_timing(
        constant bus_timing     : in    bit_time_config_type;
        constant ID             : in    natural range 0 to 15;
Martin Jeřábek's avatar
Martin Jeřábek committed
1014
        signal   mem_bus        : inout Avalon_mem_type
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1015 1016 1017 1018 1019 1020
    );


    ----------------------------------------------------------------------------
    -- Read Bus timing configuration from CTU CAN FD Core.
    -- (duration of bit phases, synchronisation jump width, baud-rate prescaler)
Martin Jeřábek's avatar
Martin Jeřábek committed
1021
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1022 1023 1024 1025 1026 1027 1028 1029 1030
    -- Arguments:
    --  bus_timing      Bus timing structure that will be filled by timing
    --                  configuration.
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_read_timing(
        signal   bus_timing     : out   bit_time_config_type;
        constant ID             : in    natural range 0 to 15;
Martin Jeřábek's avatar
Martin Jeřábek committed
1031
        signal   mem_bus        : inout Avalon_mem_type
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1032 1033
    );

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    ----------------------------------------------------------------------------
    -- Read Bus timing configuration from CTU CAN FD Core.
    -- (duration of bit phases, synchronisation jump width, baud-rate prescaler)
    --
    -- Arguments:
    --  bus_timing      Bus timing structure that will be filled by timing
    --                  configuration.
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_read_timing_v(
        variable bus_timing     : out   bit_time_config_type;
        constant ID             : in    natural range 0 to 15;
        signal   mem_bus        : inout Avalon_mem_type
    );

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1050

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
    ----------------------------------------------------------------------------
    -- Print Bus timing configuration of CTU CAN FD Core.
    -- (duration of bit phases, synchronisation jump width, baud-rate prescaler)
    --
    -- Arguments:
    --  bus_timing      Bus timing structure that will be printed in simulator.
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_print_timing(
        constant   bus_timing     : in   bit_time_config_type
    );


    ----------------------------------------------------------------------------
    -- Print Bus matrix from sanity test configuration.
    --
    -- Arguments:
    --  bus_matrix      Bus Matrix to be printed.
    ----------------------------------------------------------------------------
    procedure CAN_print_bus_matrix(
        constant   bus_matrix     : in   bus_matrix_type
    );


Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1076 1077
    ----------------------------------------------------------------------------
    -- Turn on/off CTU_CAN_FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
1078
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1079 1080 1081 1082 1083 1084 1085 1086
    -- Arguments:
    --  turn_on         Turns on the Core when "true". Turns off otherwise.
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_turn_controller(
        constant turn_on        : in    boolean;
        constant ID             : in    natural range 0 to 15;
Martin Jeřábek's avatar
Martin Jeřábek committed
1087
        signal   mem_bus        : inout Avalon_mem_type
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1088 1089 1090 1091 1092
    );


    ----------------------------------------------------------------------------
    -- Enables/Disabled Retransmitt limiting in CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
1093
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
    -- Arguments:
    --  enable          Enables retransmitt limiting when "true".
    --                  Disables rettransmitt limiting otherwise.
    --  limit           Limit for number of retransmissions.
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_enable_retr_limit(
        constant enable         : in    boolean;
        constant limit          : in    natural range 0 to 15;
        constant ID             : in    natural range 0 to 15;
Martin Jeřábek's avatar
Martin Jeřábek committed
1105
        signal   mem_bus        : inout Avalon_mem_type
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1106 1107 1108
    );


1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
    ----------------------------------------------------------------------------
    -- Configures mask filter of CTU CAN FD Core.
    --
    -- Arguments:
    --  filter          Identifier of Mask filter which should be configured.
    --  config          Configuration structure of CTU CAN FD mask filter.
    --  ID              Index of CTU CAN FD Core instance.
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_set_mask_filter(
        constant filter         : in    SW_CAN_mask_filter_type;
        constant config         : in    SW_CAN_mask_filter_config;
        constant ID             : in    natural range 0 to 15;
        signal   mem_bus        : inout Avalon_mem_type
    );


    ----------------------------------------------------------------------------
    -- Configures mask filter of CTU CAN FD Core.
    --
    -- Arguments:
    --  config          Configuration structure of CTU CAN FD mask filter.
    --  ID              Index of CTU CAN FD Core instance.
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_set_range_filter(
        constant config         : in    SW_CAN_range_filter_config;
        constant ID             : in    natural range 0 to 15;
        signal   mem_bus        : inout Avalon_mem_type
    );


Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1141 1142
    ----------------------------------------------------------------------------
    -- Generate random CAN FD Frame.
Martin Jeřábek's avatar
Martin Jeřábek committed
1143
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1144 1145 1146
    -- Arguments:
    --  rand_ctr        Pointer to pool of random values.
    --  frame           Output variable in which CAN FD Frame sill be generated.
Martin Jeřábek's avatar
Martin Jeřábek committed
1147
    ----------------------------------------------------------------------------
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1148 1149 1150 1151 1152 1153
    procedure CAN_generate_frame(
        signal   rand_ctr       : inout natural range 0 to RAND_POOL_SIZE;
        variable frame          : inout SW_CAN_frame_type
    );


1154 1155
    ----------------------------------------------------------------------------
    -- Prints CAN Frame to simulator output
Martin Jeřábek's avatar
Martin Jeřábek committed
1156
    --
1157 1158 1159
    -- Arguments:
    --  frame           Frame to print
    --  severity        Severity level that should be used to print the frame.
Martin Jeřábek's avatar
Martin Jeřábek committed
1160
    ----------------------------------------------------------------------------
1161 1162 1163 1164 1165 1166
    procedure CAN_print_frame(
        constant frame          : in    SW_CAN_frame_type;
        constant log_level      : in    log_lvl_type
    );


Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1167 1168
    ----------------------------------------------------------------------------
    -- Compare two CAN FD frames and decide if they are equal.
Martin Jeřábek's avatar
Martin Jeřábek committed
1169
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1170 1171 1172 1173 1174 1175 1176 1177
    -- Arguments:
    --  frame_A         First frame in comparison
    --  frame_B         Second frame in comparison
    --  comp_ts         When "true" timestamps should be considered in
    --                  comparison. When "false" timestamps should not be
    --                  considered.
    --  outcome         Variable to store result of comparison into. When frames
    --                  are equal "true" is stored, "false" otherwise.
Martin Jeřábek's avatar
Martin Jeřábek committed
1178
    ----------------------------------------------------------------------------
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1179 1180 1181 1182 1183 1184 1185 1186
    procedure CAN_compare_frames(
        constant frame_A        : in    SW_CAN_frame_type;
        constant frame_B        : in    SW_CAN_frame_type;
        constant comp_ts        : in    boolean;
        variable outcome        : inout boolean
    );


1187 1188 1189 1190 1191 1192 1193 1194
 	----------------------------------------------------------------------------
    -- Stores CAN Frame to auxiliarly test memory. Used for creating SW models
	-- of Buffers. Frame is stored in the same order as in TXT Buffer and RX
	-- Buffer:
	--		1. FRAME_FORMAT
	--		2. IDENTIFIER
	--		3. TIMESTAMP_L
	--		4. TIMESTAMP_U
Martin Jeřábek's avatar
Martin Jeřábek committed
1195 1196
	--		5-20. DATA
    --
1197 1198 1199 1200 1201
    -- Arguments:
    --  frame         	Frame to store.
    --  memory			Memory to store the frame into
	--	pointer			Pointer to the memory index where frame shouldbe stored
    ----------------------------------------------------------------------------
1202
    procedure store_frame_to_test_mem(
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
        constant frame          :  in       SW_CAN_frame_type;
        signal   memory         :  out      test_mem_type;
        signal   pointer        :  inout    natural
    );


	----------------------------------------------------------------------------
    -- Read frame from auxiliarly test memory. Used for creating SW models of
	-- Buffers. Frame is expected to be in the same format as IN TXT Buffer and
	-- RX Buffer:
	--		1. FRAME_FORMAT
	--		2. IDENTIFIER
	--		3. TIMESTAMP_L
	--		4. TIMESTAMP_U
Martin Jeřábek's avatar
Martin Jeřábek committed
1217
	--		5-20. DATA
1218 1219 1220 1221 1222
	--
    -- Arguments:
    --  frame           Output variable in which CAN FD Frame sill be generated.
    --  memory          Memory from which the CAN frame should be read.
    --  pointer         Pointer to memory where CAN Frame is starting.
Martin Jeřábek's avatar
Martin Jeřábek committed
1223
    ----------------------------------------------------------------------------
1224
    procedure read_frame_from_test_mem(
1225 1226 1227 1228 1229 1230
        variable frame          :  inout    SW_CAN_frame_type;
        constant memory         :  in       test_mem_type;
        variable pointer        :  inout    natural
    );


1231 1232 1233
    ----------------------------------------------------------------------------
    -- Inserts frame to TXT Buffer. Function does NOT check state of the
    -- buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
1234
    --
1235 1236 1237 1238 1239 1240
    -- Arguments:
    --  frame           CAN FD Frame to send
    --  buf_nr          Number of TXT Buffer from which the frame should be
    --                  sent (1:4)
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
Martin Jeřábek's avatar
Martin Jeřábek committed
1241
    --  outcome         Returns "true" if the frame was inserted properly,
1242 1243
    --                  "false" if TXT Buffer was in states : Ready,
    --                  TX in progress, Abort in progress
Martin Jeřábek's avatar
Martin Jeřábek committed
1244
    ----------------------------------------------------------------------------
1245 1246 1247 1248 1249 1250 1251 1252
    procedure CAN_insert_TX_frame(
        constant frame          : in    SW_CAN_frame_type;
        constant buf_nr         : in    natural range 1 to 4;
        constant ID             : in    natural range 0 to 15;
        signal   mem_bus        : inout Avalon_mem_type
    );


Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1253 1254 1255 1256
    ----------------------------------------------------------------------------
    -- Check whether TXT Buffer is accessible (Empty, Aborted, TX Failed or Done)
    -- If yes, insert the frame to TXT Buffer and give "set_ready" command.
    -- The function does not wait until the frame is transmitted.
Martin Jeřábek's avatar
Martin Jeřábek committed
1257
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1258 1259 1260 1261 1262 1263
    -- Arguments:
    --  frame           CAN FD Frame to send
    --  buf_nr          Number of TXT Buffer from which the frame should be
    --                  sent (1:4)
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
Martin Jeřábek's avatar
Martin Jeřábek committed
1264
    --  outcome         Returns "true" if the frame was inserted properly,
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1265 1266
    --                  "false" if TXT Buffer was in states : Ready,
    --                  TX in progress, Abort in progress
Martin Jeřábek's avatar
Martin Jeřábek committed
1267
    ----------------------------------------------------------------------------
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    procedure CAN_send_frame(
        constant frame          : in    SW_CAN_frame_type;
        constant buf_nr         : in    natural range 1 to 4;
        constant ID             : in    natural range 0 to 15;
        signal   mem_bus        : inout Avalon_mem_type;
        variable outcome        : out   boolean
    );


    ----------------------------------------------------------------------------
    -- Reads CAN Frame from RX Buffer FIFO.
Martin Jeřábek's avatar
Martin Jeřábek committed
1279
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
    -- Arguments:
    --  frame           Output variable where CAN FD Frame will be stored
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_read_frame(
        variable frame          : inout SW_CAN_frame_type;
        constant ID             : in    natural range 0 to 15;
        signal   mem_bus        : inout Avalon_mem_type
    );


    ----------------------------------------------------------------------------
    -- Waits until frame starts (unit turns transceiver or receiver),
    -- and then waits until frame finishes (bus becomes idle).
    --
    -- Procedure is polling on status of CTU CAN FD Core over Avalon bus!
Martin Jeřábek's avatar
Martin Jeřábek committed
1297
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
    -- Arguments:
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_wait_frame_sent(
        constant ID             : in    natural range 0 to 15;
        signal   mem_bus        : inout Avalon_mem_type
    );


    ----------------------------------------------------------------------------
    -- Waits until CAN bus becomes idle (no frame in progress).
Martin Jeřábek's avatar
Martin Jeřábek committed
1310
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
    -- Procedure is polling on status of CTU CAN FD Core over Avalon bus!
    --
    -- Arguments:
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_wait_bus_idle(
        constant ID             : in    natural range 0 to 15;
        signal   mem_bus        : inout Avalon_mem_type
    );


    ----------------------------------------------------------------------------
    -- Waits until CAN bus becomes idle (no frame in progress).
    --
    -- Procedure is polling on status of CTU CAN FD Core over Avalon bus!
Martin Jeřábek's avatar
Martin Jeřábek committed
1327
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1328 1329 1330 1331 1332 1333 1334 1335
    -- Arguments:
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_wait_error_transmitted(
        constant ID             : in    natural range 0 to 15;
        signal   mem_bus        : inout Avalon_mem_type
    );
Martin Jeřábek's avatar
Martin Jeřábek committed
1336

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1337

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
    ----------------------------------------------------------------------------
    -- Reads bit timing parameters and waits for length of several bit times.
    --
    -- Arguments:
    --  bits            Number of Bit times to wait for
    --  nominal         "true" if Nominal Bit time should be used, "false" if
    --                  Data Bit Time should be used.
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_wait_n_bits(
        constant bits           : in    natural;
        constant nominal        : in    boolean;
        constant ID             : in    natural range 0 to 15;
        signal   mem_bus        : inout Avalon_mem_type
    );


1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
    ----------------------------------------------------------------------------
    -- Waits until transmission or reception is started by a Node.
    --
    -- Arguments:
    --  bits            Number of Bit times to wait for
    --  exit_trans      Exit when unit turns transceiver.         
    --  exit_rec        Exit when unit turns receiver.
    --  ID              Index of CTU CAN FD Core instance
    --  mem_bus         Avalon memory bus to execute the access on.
    ----------------------------------------------------------------------------
    procedure CAN_wait_tx_rx_start(
        constant exit_trans     : in    boolean;
        constant exit_rec       : in    boolean;
        constant ID             : in    natural range 0 to 15;
        signal   mem_bus        : inout Avalon_mem_type
    );


Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1374 1375
    ----------------------------------------------------------------------------
    -- Calculate length of CAN Frame in bits (stuff bits not included).
Martin Jeřábek's avatar
Martin Jeřábek committed
1376
    --
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1377 1378 1379 1380 1381 1382 1383 1384 1385
    -- Arguments:
    --  frame           CAN Frame whose length should be calculated.
    --  bit_length      Variable in which the length of CAN Frame in bits is
    --                  stored.
    ----------------------------------------------------------------------------
    procedure CAN_calc_frame_length(
        constant frame          : in    SW_CAN_frame_type;
        variable bit_length     : inout natural
    );