testbench.vhd 25.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
--==============================================================================
-- CERN (BE-CO-HT)
-- Testbench for CONV-TTL-BLO design
--==============================================================================
--
-- author: Theodor Stana (t.stana@cern.ch)
--
-- date of creation: 2014-02-18
--
-- version: 1.0
--
-- description:
--    Design-wide simulation testbench for the CONV-TTL-BLO gateware. Currently
--    simulated features include:
--      - pulse triggering
16
--      - I2C master for reading register contents
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
--
-- dependencies:
--    None.
--
--==============================================================================
-- GNU LESSER GENERAL PUBLIC LICENSE
--==============================================================================
-- This source file is free software; you can redistribute it and/or modify it
-- under the terms of the GNU Lesser General Public License as published by the
-- Free Software Foundation; either version 2.1 of the License, or (at your
-- option) any later version. This source is distributed in the hope that it
-- will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
-- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-- See the GNU Lesser General Public License for more details. You should have
-- received a copy of the GNU Lesser General Public License along with this
-- source; if not, download it from http://www.gnu.org/licenses/lgpl-2.1.html
--==============================================================================
-- last changes:
--    2014-02-18   Theodor Stana     File created
--==============================================================================
-- TODO: -
--==============================================================================

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;


entity testbench is
end entity testbench;


architecture behav of testbench is

  --============================================================================
  -- Type declarations
  --============================================================================
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  type t_state_i2c_mst is
    (
      IDLE,
      I2C_ADDR, I2C_ADDR_ACK,

      WB_ADDR_B0, WB_ADDR_B0_ACK,
      WB_ADDR_B1, WB_ADDR_B1_ACK,

      ST_OP,

      RD_RESTART, RD_RESTART_ACK,
      RD, RD_ACK,

      WR, WR_ACK,

      STO,

      SUCCESS,
      ERR
    );

75 76 77 78

  --============================================================================
  -- Constant declarations
  --============================================================================
79
  -- Clock periods
80 81 82
  constant c_clk_20_per   : time := 50 ns;
  constant c_clk_125_per  : time :=  8 ns;

83 84 85 86
  -- Number of I2C masters and slaves for the I2C bus model
  constant c_nr_masters : positive := 1;
  constant c_nr_slaves  : positive := 1;

87 88 89 90 91 92 93
  --============================================================================
  -- Component declarations
  --============================================================================
  component conv_ttl_blo is
    generic
    (
      g_nr_ttl_chan : natural := 6;
94 95
      g_nr_inv_chan : natural := 4;
      g_sim         : boolean := false
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    );
    port
    (
      -- Clocks
      -- 20 MHz from VCXO
      clk_20_vcxo_i           : in  std_logic;
      -- 125 MHz from clock generator
      fpga_clk_p_i           : in  std_logic;
      fpga_clk_n_i           : in  std_logic;
  
      -- LEDs
      led_ctrl0_o            : out std_logic;
      led_ctrl0_oen_o        : out std_logic;
      led_ctrl1_o            : out std_logic;
      led_ctrl1_oen_o        : out std_logic;
      led_multicast_2_0_o    : out std_logic;
      led_multicast_3_1_o    : out std_logic;
      led_wr_gmt_ttl_ttln_o  : out std_logic;
      led_wr_link_syserror_o : out std_logic;
      led_wr_ok_syspw_o      : out std_logic;
      led_wr_ownaddr_i2c_o   : out std_logic;
  
      -- I/Os for pulses
      pulse_front_led_n_o    : out std_logic_vector(g_nr_ttl_chan downto 1);
      pulse_rear_led_n_o     : out std_logic_vector(g_nr_ttl_chan downto 1);
      fpga_input_ttl_n_i     : in  std_logic_vector(g_nr_ttl_chan downto 1);
      fpga_out_ttl_o         : out std_logic_vector(g_nr_ttl_chan downto 1);
      fpga_blo_in_i          : in  std_logic_vector(g_nr_ttl_chan downto 1);
      fpga_trig_blo_o        : out std_logic_vector(g_nr_ttl_chan downto 1);
      inv_in_n_i             : in  std_logic_vector(g_nr_inv_chan downto 1);
      inv_out_o              : out std_logic_vector(g_nr_inv_chan downto 1);
  
      -- Output enable lines
      fpga_oe_o              : out std_logic;
      fpga_blo_oe_o          : out std_logic;
      fpga_trig_ttl_oe_o     : out std_logic;
      fpga_inv_oe_o          : out std_logic;
  
      --TTL/INV_TTL_N
      ttl_switch_n_i         : in  std_logic;
  
      extra_switch_n_i       : in  std_logic_vector(7 downto 1);
  
      -- Lines for the i2c_slave
      scl_i                  : in  std_logic;
      scl_o                  : out std_logic;
      scl_oe_o               : out std_logic;
      sda_i                  : in  std_logic;
      sda_o                  : out std_logic;
      sda_oe_o               : out std_logic;
      fpga_ga_i              : in  std_logic_vector(4 downto 0);
      fpga_gap_i             : in  std_logic;
  
      -- Flash memory lines
      fpga_prom_cclk_o       : out std_logic;
      fpga_prom_cso_b_n_o    : out std_logic;
      fpga_prom_mosi_o       : out std_logic;
      fpga_prom_miso_i       : in  std_logic;
  
      -- Blocking power supply reset line
      mr_n_o                 : out std_logic;
  
      -- Thermometer line
      thermometer_b          : inout std_logic;
  
      -- PLL DACs
      -- DAC1: 20 MHz VCXO control
      fpga_plldac1_din_o     : out std_logic;
      fpga_plldac1_sclk_o    : out std_logic;
      fpga_plldac1_sync_n_o  : out std_logic;
      -- DAC2: 125 MHz clock generator control
      fpga_plldac2_din_o     : out std_logic;
      fpga_plldac2_sclk_o    : out std_logic;
      fpga_plldac2_sync_n_o  : out std_logic;
  
      -- SFP lines
      fpga_sfp_los_i         : in    std_logic;
      fpga_sfp_mod_def0_i    : in    std_logic;
      fpga_sfp_rate_select_o : out   std_logic;
      fpga_sfp_mod_def1_b    : inout std_logic;
      fpga_sfp_mod_def2_b    : inout std_logic;
      fpga_sfp_tx_disable_o  : out   std_logic;
      fpga_sfp_tx_fault_i    : in    std_logic;
  
      -- RTM identifiers, should match with the expected values
      fpga_rtmm_n_i          : in std_logic_vector(2 downto 0);
      fpga_rtmp_n_i          : in std_logic_vector(2 downto 0)
    );
  end component conv_ttl_blo;

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  -- I2C bus model
  component i2c_bus_model is
    generic
    (
      g_nr_masters : positive := 1;
      g_nr_slaves  : positive := 1
    );
    port
    (
      -- Input ports from master lines
      mscl_i : in  std_logic_vector(g_nr_masters-1 downto 0);
      msda_i : in  std_logic_vector(g_nr_masters-1 downto 0);

      -- Input ports from slave lines
      sscl_i : in  std_logic_vector(g_nr_slaves-1 downto 0);
      ssda_i : in  std_logic_vector(g_nr_slaves-1 downto 0);

      -- SCL and SDA line outputs
      scl_o  : out std_logic;
      sda_o  : out std_logic
    );
  end component i2c_bus_model;

  -- I2C master
  component i2c_master_byte_ctrl is
    port
    (
      clk    : in std_logic;
      rst    : in std_logic; -- synchronous active high reset (WISHBONE compatible)
      nReset : in std_logic;	-- asynchornous active low reset (FPGA compatible)
      ena    : in std_logic; -- core enable signal

      clk_cnt : in unsigned(15 downto 0);	-- 4x SCL

      -- input signals
      start,
      stop,
      read,
      write,
      ack_in : std_logic;
      din    : in std_logic_vector(7 downto 0);

      -- output signals
      cmd_ack  : out std_logic; -- command done
      ack_out  : out std_logic;
      i2c_busy : out std_logic; -- arbitration lost
      i2c_al   : out std_logic; -- i2c bus busy
      dout     : out std_logic_vector(7 downto 0);

      -- i2c lines
      scl_i   : in std_logic;  -- i2c clock line input
      scl_o   : out std_logic; -- i2c clock line output
      scl_oen : out std_logic; -- i2c clock line output enable, active low
      sda_i   : in std_logic;  -- i2c data line input
      sda_o   : out std_logic; -- i2c data line output
      sda_oen : out std_logic  -- i2c data line output enable, active low
    );
  end component i2c_master_byte_ctrl;

245 246 247 248 249
  --============================================================================
  -- Signal declarations
  --============================================================================
  signal clk_20, clk_125              : std_logic;
  signal clk_125_p, clk_125_n         : std_logic;
250
  signal rst_n, rst                   : std_logic;
251 252 253 254 255 256 257 258 259 260 261 262 263 264

  signal pulse_led_front_n            : std_logic_vector(6 downto 1);
  signal pulse_led_front              : std_logic_vector(6 downto 1);
  signal pulse_led_rear_n             : std_logic_vector(6 downto 1);
  signal pulse_led_rear               : std_logic_vector(6 downto 1);
  signal ttl_inp_n, ttl_outp          : std_logic_vector(6 downto 1);
  signal ttl_pulse                    : std_logic_vector(6 downto 1);
  signal inv_pulse                    : std_logic_vector(6 downto 1);
  signal blo_inp, blo_outp            : std_logic_vector(6 downto 1);
  signal blo_pulse                    : std_logic_vector(6 downto 1);
  signal oe, blo_oe, ttl_oe, inv_oe   : std_logic;
  signal ttl_switch_n                 : std_logic;
  signal switches_n                   : std_logic_vector(7 downto 1);

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  -- I2C signals
  signal state_i2c_mst                : t_state_i2c_mst;

  signal mst_fsm_op                   : std_logic;
  signal mst_fsm_start                : std_logic;
  signal stim_cnt                     : unsigned(31 downto 0);

  signal cnt                          : unsigned(2 downto 0);
  signal buf_byte_cnt                 : integer;
  signal once                         : boolean;

  signal byte_cnt                     : unsigned(1 downto 0);
  signal rcvd                         : std_logic_vector(31 downto 0);
  signal send                         : std_logic_vector(31 downto 0);
  signal send_val                     : std_logic_vector(31 downto 0);
  signal wrote                        : std_logic;
  signal slv_addr                     : std_logic_vector(6 downto 0);
  signal adr                          : std_logic_vector(31 downto 0);

  signal mst_sta                      : std_logic;
  signal mst_sto                      : std_logic;
  signal mst_rd                       : std_logic;
  signal mst_wr                       : std_logic;
  signal mst_ack                      : std_logic;
  signal mst_dat_in                   : std_logic_vector(7 downto 0);
  signal mst_dat_out                  : std_logic_vector(7 downto 0);
  signal mst_cmd_ack                  : std_logic;
  signal ack_fr_slv                   : std_logic;

  signal mscl, msda                   : std_logic_vector(c_nr_masters-1 downto 0);
  signal sscl, ssda                   : std_logic_vector(c_nr_slaves-1 downto 0);
  signal scl, sda                     : std_logic;

  signal scl_fr_mst                   : std_logic;
  signal scl_en_mst                   : std_logic;
  signal sda_fr_mst                   : std_logic;
  signal sda_en_mst                   : std_logic;

  signal scl_fr_slv                   : std_logic;
  signal scl_en_slv                   : std_logic;
  signal sda_fr_slv                   : std_logic;
  signal sda_en_slv                   : std_logic;

  signal t                            : boolean;

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
--==============================================================================
--  architecture begin
--==============================================================================
begin

  --============================================================================
  -- Generate clock signals
  --============================================================================
  p_clk_20 : process
  begin
    clk_20 <= '0';
    wait for c_clk_20_per/2;
    clk_20 <= '1';
    wait for c_clk_20_per/2;
  end process;

  p_clk_125 : process
  begin
    clk_125 <= '0';
    wait for c_clk_125_per/2;
    clk_125 <= '1';
    wait for c_clk_125_per/2;
  end process;

  clk_125_p <= clk_125;
  clk_125_n <= not clk_125;

  --============================================================================
  -- Instantiate the DUT
  --============================================================================
  cmp_dut : conv_ttl_blo
    generic map
    (
      g_nr_ttl_chan => 6,
344 345
      g_nr_inv_chan => 4,
      g_sim         => true
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    )
    port map
    (
      -- Clocks
      -- 20 MHz from VCXO
      clk_20_vcxo_i          => clk_20,
      -- 125 MHz from clock generator
      fpga_clk_p_i           => clk_125_p,
      fpga_clk_n_i           => clk_125_n,

      -- LEDs
      led_ctrl0_o            => open,
      led_ctrl0_oen_o        => open,
      led_ctrl1_o            => open,
      led_ctrl1_oen_o        => open,
      led_multicast_2_0_o    => open,
      led_multicast_3_1_o    => open,
      led_wr_gmt_ttl_ttln_o  => open,
      led_wr_link_syserror_o => open,
      led_wr_ok_syspw_o      => open,
      led_wr_ownaddr_i2c_o   => open,

      -- I/Os for pulses
      pulse_front_led_n_o    => pulse_led_front_n,
      pulse_rear_led_n_o     => pulse_led_rear_n,
      fpga_input_ttl_n_i     => ttl_inp_n,
      fpga_out_ttl_o         => ttl_outp,
      fpga_blo_in_i          => blo_inp,
      fpga_trig_blo_o        => blo_outp,
      inv_in_n_i             => (others => '1'),
      inv_out_o              => open,

      -- Output enable lines
      fpga_oe_o              => oe,
      fpga_blo_oe_o          => blo_oe,
      fpga_trig_ttl_oe_o     => ttl_oe,
      fpga_inv_oe_o          => inv_oe,

      --TTL/INV_TTL_N
      ttl_switch_n_i         => ttl_switch_n,

      extra_switch_n_i       => switches_n,

      -- Lines for the i2c_slave
390 391 392 393 394 395 396
      scl_i                  => scl,
      scl_o                  => scl_fr_slv,
      scl_oe_o               => scl_en_slv,
      sda_i                  => sda,
      sda_o                  => sda_fr_slv,
      sda_oe_o               => sda_en_slv,
      fpga_ga_i              => "11110",
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
      fpga_gap_i             => '0',

      -- Flash memory lines
      fpga_prom_cclk_o       => open,
      fpga_prom_cso_b_n_o    => open,
      fpga_prom_mosi_o       => open,
      fpga_prom_miso_i       => 'Z',

      -- Blocking power supply reset line
      mr_n_o                 => rst_n,

      -- Thermometer line
      thermometer_b          => open,

      -- PLL DACs
      -- DAC1: 20 MHz VCXO control
      fpga_plldac1_din_o     => open,
      fpga_plldac1_sclk_o    => open,
      fpga_plldac1_sync_n_o  => open,
      -- DAC2: 125 MHz clock generator control
      fpga_plldac2_din_o     => open,
      fpga_plldac2_sclk_o    => open,
      fpga_plldac2_sync_n_o  => open,

      -- SFP lines
      fpga_sfp_los_i         => '1',
      fpga_sfp_mod_def0_i    => '1',
      fpga_sfp_rate_select_o => open,
      fpga_sfp_mod_def1_b    => open,
      fpga_sfp_mod_def2_b    => open,
      fpga_sfp_tx_disable_o  => open,
      fpga_sfp_tx_fault_i    => '1',

      -- RTM identifiers, should match with the expected values
      fpga_rtmm_n_i          => (others => '0'),
      fpga_rtmp_n_i          => (others => '0')
    );

435 436 437 438 439 440 441 442 443
  -- Tri-state buffers on the I2C lines
  sscl(0) <= scl_fr_slv when (scl_en_slv = '1') else
             '1';
  ssda(0) <= sda_fr_slv when (sda_en_slv = '1') else
             '1';

  -- Active-high reset
  rst <= not rst_n;

444 445 446
  --============================================================================
  -- Pulse outputs assignment based on OE signals
  --============================================================================
447 448 449
  ttl_pulse <= ttl_outp when (oe = '1') and (ttl_oe = '1') else (others => '0');
  blo_pulse <= blo_outp when (oe = '1') and (blo_oe = '1') else (others => '0');
  inv_pulse <= blo_outp when (oe = '1') and (inv_oe = '1') else (others => '0');
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

  --============================================================================
  -- Switches
  --============================================================================
  -- TTL
  ttl_switch_n <= '0';
  -- GF
  switches_n(1) <= '0';
  -- other
  switches_n(7 downto 2) <= (others => '1');

  --============================================================================
  -- Pulse LEDs
  --============================================================================
  pulse_led_front <= not pulse_led_front_n;
  pulse_led_rear  <= not pulse_led_rear_n;

  --============================================================================
  -- Pulse stimuli
  --============================================================================
470
  blo_inp(6) <= '0';
471
  ttl_inp_n(5 downto 1) <= (others => '1');
472 473 474
  gen_pulse_chain : for i in 6 downto 2 generate
    blo_inp(i-1) <= blo_outp(i);
  end generate gen_pulse_chain;
475

476
  p_stim_pulse : process
477 478
  begin
    ttl_inp_n(6) <= '1';
479 480
    wait until t = true;
    while (t = true) loop
481
      wait for 240 us;
482 483 484
      ttl_inp_n(6) <= '0';
      wait for 500 ns;
      ttl_inp_n(6) <= '1';
485 486 487 488 489 490
      if ttl_outp(6) /= '1' then
        assert false report "ttl_outp not '1'" severity warning;
      end if;
      if blo_outp(6) /= '1' then
        assert false report "blo_outp not '1'" severity warning;
      end if;
491
    end loop;
492 493 494 495 496 497 498 499 500 501 502
  end process p_stim_pulse;

  process
  begin
    t <= true;
    wait for 2 ms;
    t <= false;
    wait for 500 ms;
    t <= true;
    wait for 10 ms;
    t <= false;
503
    wait;
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 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 642 643 644 645 646 647 648 649 650 651 652 653 654 655 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 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
  end process;

  --============================================================================
  -- I2C master
  --============================================================================
  ------------------------------------------------------------------------------
  -- First, the component instantiation
  ------------------------------------------------------------------------------
  cmp_master : i2c_master_byte_ctrl
    port map
    (
      clk      => clk_20,
      rst      => rst,
      nReset   => rst_n,
      ena      => '1',

      clk_cnt  => x"0027",

      -- input signals
      start    => mst_sta,
      stop     => mst_sto,
      read     => mst_rd,
      write    => mst_wr,
      ack_in   => mst_ack,
      din      => mst_dat_in,

      -- output signals
      cmd_ack  => mst_cmd_ack,
      ack_out  => ack_fr_slv,
      i2c_busy => open,
      i2c_al   => open,
      dout     => mst_dat_out,

      -- i2c lines
      scl_i    => scl,
      scl_o    => scl_fr_mst,
      scl_oen  => scl_en_mst,
      sda_i    => sda,
      sda_o    => sda_fr_mst,
      sda_oen  => sda_en_mst
    );

  -- Then, the tri-state_i2c_mst buffers on the line
  mscl(0) <= scl_fr_mst when (scl_en_mst = '0') else
             '1';
  msda(0) <= sda_fr_mst when (sda_en_mst = '0') else
             '1';

  ------------------------------------------------------------------------------
  -- Bus model instantiation and connection to master and slaves
  ------------------------------------------------------------------------------
  cmp_i2c_bus : i2c_bus_model
    generic map
    (
      g_nr_masters => c_nr_masters,
      g_nr_slaves  => c_nr_slaves
    )
    port map
    (
      mscl_i => mscl,
      msda_i => msda,
      sscl_i => sscl,
      ssda_i => ssda,
      scl_o  => scl,
      sda_o  => sda
    );

  ------------------------------------------------------------------------------
  -- This FSM controls the signals to the master component to implement the I2C
  -- protocol defined together with ELMA. The FSM is controlled by the
  -- stimuli process below
  ------------------------------------------------------------------------------
  p_mst_fsm : process (clk_20) is
  begin
    if rising_edge(clk_20) then
      if (rst_n = '0') then
        state_i2c_mst      <= IDLE;
        mst_sta    <= '0';
        mst_wr     <= '0';
        mst_sto    <= '0';
        mst_rd     <= '0';
        mst_dat_in <= (others => '0');
        mst_ack    <= '0';
        cnt        <= (others => '0');
        once       <= true;
        byte_cnt   <= (others => '0');
        rcvd       <= (others => '0');
        send       <= (others => '0');
      else
        case state_i2c_mst is

          when IDLE =>
            if (mst_fsm_start = '1') then
              state_i2c_mst <= I2C_ADDR;
              send  <= std_logic_vector(send_val);
            end if;

          when I2C_ADDR =>
            mst_sta   <= '1';
            mst_wr    <= '1';
            mst_dat_in <= slv_addr & '0';
            if (mst_cmd_ack = '1') then
              mst_sta <= '0';
              mst_wr  <= '0';
              state_i2c_mst   <= I2C_ADDR_ACK;
            end if;

          when I2C_ADDR_ACK =>
            cnt <= cnt + 1;
            if (cnt = 7) then
              if (ack_fr_slv = '0') then
                state_i2c_mst <= WB_ADDR_B0;
              else
                state_i2c_mst <= ERR;
              end if;
            end if;

          when WB_ADDR_B0 =>
            mst_wr <= '1';
            mst_dat_in <= adr(15 downto 8);
            if (mst_cmd_ack = '1') then
              mst_wr <= '0';
              state_i2c_mst  <= WB_ADDR_B0_ACK;
            end if;

          when WB_ADDR_B0_ACK =>
            cnt <= cnt + 1;
            if (cnt = 7) then
              if (ack_fr_slv = '0') then
                state_i2c_mst <= WB_ADDR_B1;
              else
                state_i2c_mst <= ERR;
              end if;
            end if;

          when WB_ADDR_B1 =>
            mst_wr <= '1';
            mst_dat_in <= adr(7 downto 0);
            if (mst_cmd_ack = '1') then
              mst_wr <= '0';
              state_i2c_mst  <= WB_ADDR_B1_ACK;
            end if;

          when WB_ADDR_B1_ACK =>
            cnt <= cnt + 1;
            if (cnt = 7) then
              if (ack_fr_slv = '0') then
                state_i2c_mst <= ST_OP;
              else
                state_i2c_mst <= ERR;
              end if;
            end if;

          when ST_OP =>
            if (mst_fsm_op = '1') then
              state_i2c_mst <= RD_RESTART;
            else
              state_i2c_mst <= WR;
            end if;

          when RD_RESTART =>
            mst_wr <= '1';
            mst_dat_in <= slv_addr & '1';
            mst_sta <= '1';
            if (mst_cmd_ack = '1') then
              mst_sta <= '0';
              mst_wr  <= '0';
              state_i2c_mst <= RD_RESTART_ACK;
            end if;

          when RD_RESTART_ACK =>
            cnt <= cnt + 1;
            if (cnt = 7) then
              if (ack_fr_slv = '0') then
                state_i2c_mst <= RD;
              else
                state_i2c_mst <= ERR;
              end if;
            end if;

          when RD =>
            mst_rd <= '1';
            mst_ack <= '0';
            if (byte_cnt = 3) then
              mst_ack <= '1';
            end if;
            if (mst_cmd_ack = '1') then
              mst_rd <= '0';
              byte_cnt <= byte_cnt + 1;
              rcvd     <= mst_dat_out & rcvd(31 downto 8);
              mst_ack  <= '0';
              state_i2c_mst    <= RD;
              if (byte_cnt = 3) then
                state_i2c_mst  <= STO;
              end if;
            end if;

          when RD_ACK =>
            cnt <= cnt + 1;
            if (cnt = 7) then
              byte_cnt <= byte_cnt + 1;
              rcvd     <= mst_dat_out & rcvd(31 downto 8);
              mst_ack  <= '0';
              state_i2c_mst <= RD;
              if (byte_cnt = 3) then
                state_i2c_mst  <= STO;
              end if;
            end if;

          when WR =>
            mst_wr <= '1';
            mst_dat_in <= send(7 downto 0);
            if (mst_cmd_ack = '1') then
              mst_wr <= '0';
              state_i2c_mst  <= WR_ACK;
            end if;

          when WR_ACK =>
            cnt <= cnt + 1;
            if (cnt = 7) then
              if (ack_fr_slv = '0') then
                byte_cnt <= byte_cnt + 1;
                send     <= x"00" & send(31 downto 8);
                state_i2c_mst    <= WR;
                if (byte_cnt = 3) then
                  state_i2c_mst  <= STO;
                end if;
              else
                state_i2c_mst <= ERR;
              end if;
            end if;

          when STO =>
            mst_sto <= '1';
            if (mst_cmd_ack = '1') then
              mst_sto <= '0';
              state_i2c_mst   <= IDLE;
            end if;

          when ERR =>
            if (once) then
              report("Error!");
              once <= false;
            end if;

          when others =>
            state_i2c_mst <= ERR;

        end case;
      end if;
    end if;
  end process p_mst_fsm;

  ------------------------------------------------------------------------------
  -- Process to "stimulate" the master FSM above
  ------------------------------------------------------------------------------
  p_stim_mst_fsm : process (rst_n, t, state_i2c_mst)
  begin
    if (rst_n = '0') then
      mst_fsm_start <= '0';
      mst_fsm_op    <= '0';
      slv_addr      <= "1011110";
      adr           <= (others => '0');
      buf_byte_cnt  <= 0;
    elsif (not t) and (state_i2c_mst = IDLE) then
      mst_fsm_start <= '1';
      mst_fsm_op    <= '1';
      buf_byte_cnt  <= buf_byte_cnt + 1;
      case buf_byte_cnt is
        when 0 =>
          adr(11 downto 0) <= x"030";
        when 1 =>
          adr(11 downto 0) <= x"034";
        when 2 =>
          adr(11 downto 0) <= x"038";
        when 3 =>
          adr(11 downto 0) <= x"02c";
          buf_byte_cnt     <= 0;
        when others =>
          buf_byte_cnt <= 0;
      end case;
    else
      mst_fsm_start <= '0';
    end if;
  end process p_stim_mst_fsm;
789 790 791 792 793

end architecture behav;
--==============================================================================
--  architecture end
--==============================================================================