test05.cpp revision 12871:ca8b5215b10e
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  test05.cpp -- Test sc_vector (and its binding functions)
23
24  Original Author: Philipp A. Hartmann, OFFIS, 2010-03-05
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38#include <systemc.h>
39
40using sc_core::sc_vector;
41
42typedef sc_vector< sc_fifo_out<int> > port_vec;
43
44static void
45dump_port_array( const char* from, const port_vec& in )
46{
47    std::cout << "\n" << from << "\n";
48    for( unsigned i=0; i<in.size(); ++i )
49    {
50      std::cout
51        << "  "
52        << in[i].name()
53        << ".size() == " << in[i].size();
54
55      for( int j=0; j<in[i].size(); ++j)
56      {
57        std::cout
58          << " - "
59          << dynamic_cast<const sc_core::sc_object*>(in[i][j])->name()
60          << " @ " << j;
61      }
62      std::cout << std::endl;
63    }
64}
65
66SC_MODULE(sub_module)
67{
68  port_vec in;
69
70  sub_module( sc_core::sc_module_name, unsigned n_sub )
71    : in("in", n_sub ) {}
72
73  void before_end_of_elaboration()
74    { dump_port_array( "sub_module::before_end_of_elaboration", in ); }
75  void end_of_elaboration()
76    { dump_port_array( "sub_module::end_of_elaboration", in ); }
77};
78
79
80SC_MODULE(module)
81{
82  sub_module sub;
83  port_vec   in;
84
85  SC_CTOR(module)
86    : sub("sub", 4), in("in",4)
87  {
88    // vector to vector binding
89    sub.in( in );
90  }
91
92  void before_end_of_elaboration()
93    { dump_port_array( "module::before_end_of_elaboration", in ); }
94  void end_of_elaboration()
95    { dump_port_array( "module::end_of_elaboration", in ); }
96};
97
98int sc_main( int, char*[] )
99{
100
101  module top("top");
102
103  const unsigned size = 4;
104  const unsigned half = 2;
105
106  sc_assert( top.in.size() == size );
107
108  sc_vector< sc_fifo<int> >
109    fifo_1( "src_1", half ),
110    fifo_2( "src_2", size );
111
112  // bind full vector (smaller than target)
113  port_vec::iterator mid = top.in( fifo_1 );
114  sc_assert( ( mid - top.in.begin() ) - half == 0 );
115
116  // bind range, starting from last position
117  mid = top.in.bind( fifo_2.begin(), fifo_2.end(), mid );
118  sc_assert( mid == top.in.end() );
119
120  // bind a plain C array of channels
121  sc_fifo<int> fifo_a[ half ];
122  top.sub.in( fifo_a, fifo_a + half );
123
124  sc_start();
125  return 0;
126}
127