112855Sgabeblack@google.com/*****************************************************************************
212855Sgabeblack@google.com
312855Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412855Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512855Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612855Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712855Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812855Sgabeblack@google.com  License.  You may obtain a copy of the License at
912855Sgabeblack@google.com
1012855Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112855Sgabeblack@google.com
1212855Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312855Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412855Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512855Sgabeblack@google.com  implied.  See the License for the specific language governing
1612855Sgabeblack@google.com  permissions and limitations under the License.
1712855Sgabeblack@google.com
1812855Sgabeblack@google.com *****************************************************************************/
1912855Sgabeblack@google.com
2012855Sgabeblack@google.com/*****************************************************************************
2112855Sgabeblack@google.com
2212855Sgabeblack@google.com  star111657.cpp --
2312855Sgabeblack@google.com
2412855Sgabeblack@google.com  Original Author: Martin Janssen, Synopsys, Inc., 2002-02-15
2512855Sgabeblack@google.com
2612855Sgabeblack@google.com *****************************************************************************/
2712855Sgabeblack@google.com
2812855Sgabeblack@google.com/*****************************************************************************
2912855Sgabeblack@google.com
3012855Sgabeblack@google.com  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
3112855Sgabeblack@google.com  changes you are making here.
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com      Name, Affiliation, Date:
3412855Sgabeblack@google.com  Description of Modification:
3512855Sgabeblack@google.com
3612855Sgabeblack@google.com *****************************************************************************/
3712855Sgabeblack@google.com
3812855Sgabeblack@google.com/*############################################################################
3912855Sgabeblack@google.com#  Siemens AG                        copyright 2000
4012855Sgabeblack@google.com#                                    All Rights Reserved
4112855Sgabeblack@google.com#
4212855Sgabeblack@google.com#  File name : io_controller.cpp
4312855Sgabeblack@google.com#
4412855Sgabeblack@google.com#  Title     : I/O-Controller
4512855Sgabeblack@google.com#
4612855Sgabeblack@google.com#  Purpose   : functionality for I/O-Controller-module
4712855Sgabeblack@google.com#
4812855Sgabeblack@google.com#  Author    : Hannes Muhr
4912855Sgabeblack@google.com#              PSE EZE MSA
5012855Sgabeblack@google.com#
5112855Sgabeblack@google.com##############################################################################
5212855Sgabeblack@google.com#  Modification History :
5312855Sgabeblack@google.com#
5412855Sgabeblack@google.com#
5512855Sgabeblack@google.com##############################################################################*/
5612855Sgabeblack@google.com
5712855Sgabeblack@google.com#include "systemc.h"
5812855Sgabeblack@google.com#include "io_controller1.h"
5912855Sgabeblack@google.com
6012855Sgabeblack@google.com
6112855Sgabeblack@google.comvoid io_controller_m::mux(){
6212855Sgabeblack@google.com
6312855Sgabeblack@google.com   sc_uint<32> data;
6412855Sgabeblack@google.com
6512855Sgabeblack@google.com   while (true){
6612855Sgabeblack@google.com      while (start_mux.read() == 0) wait();
6712855Sgabeblack@google.com      #ifdef LOGGING
6812855Sgabeblack@google.com         flog << sc_time_stamp()<<": "<<name()<<"::select - enabled" << endl;
6912855Sgabeblack@google.com      #endif
7012855Sgabeblack@google.com      mii_en_o = 1;
7112855Sgabeblack@google.com      for (int i = 0; i < MII_FRAME_SIZE; i++){
7212855Sgabeblack@google.com         data = shared_mem1[i];
7312855Sgabeblack@google.com
7412855Sgabeblack@google.com         mii_data4_o = data.range(3,0);
7512855Sgabeblack@google.com         wait();
7612855Sgabeblack@google.com         mii_data4_o = data.range(7,4);
7712855Sgabeblack@google.com         wait();
7812855Sgabeblack@google.com         mii_data4_o = data.range(11,8);
7912855Sgabeblack@google.com         wait();
8012855Sgabeblack@google.com         mii_data4_o = data.range(15,12);
8112855Sgabeblack@google.com         wait();
8212855Sgabeblack@google.com         mii_data4_o = data.range(19,16);
8312855Sgabeblack@google.com         wait();
8412855Sgabeblack@google.com         mii_data4_o = data.range(23,20);
8512855Sgabeblack@google.com         wait();
8612855Sgabeblack@google.com         mii_data4_o = data.range(27,24);
8712855Sgabeblack@google.com         wait();
8812855Sgabeblack@google.com         mii_data4_o = data.range(31,28);
8912855Sgabeblack@google.com         wait();
9012855Sgabeblack@google.com      }
9112855Sgabeblack@google.com      mii_en_o = 0;
9212855Sgabeblack@google.com      mii_data4_o = 0;
9312855Sgabeblack@google.com      ready_mux = 1;
9412855Sgabeblack@google.com      wait();
9512855Sgabeblack@google.com      ready_mux = 0;
9612855Sgabeblack@google.com      wait();
9712855Sgabeblack@google.com   }
9812855Sgabeblack@google.com}
9912855Sgabeblack@google.com
10012855Sgabeblack@google.comvoid io_controller_m::shift(){
10112855Sgabeblack@google.com
10212855Sgabeblack@google.com    sc_uint<32> data;
10312855Sgabeblack@google.com
10412855Sgabeblack@google.com   while (true){
10512855Sgabeblack@google.com      while (mii_en_i.read() == false) wait();
10612855Sgabeblack@google.com      #ifdef LOGGING
10712855Sgabeblack@google.com         flog << sc_time_stamp()<<": "<<name()<<"::collect - enabled" << endl;
10812855Sgabeblack@google.com      #endif
10912855Sgabeblack@google.com
11012855Sgabeblack@google.com      for (int i = 0; i < MII_FRAME_SIZE; i++){
11112855Sgabeblack@google.com         data.range(3,0) = mii_data4_i;
11212855Sgabeblack@google.com         wait();
11312855Sgabeblack@google.com         data.range(7,4) = mii_data4_i;
11412855Sgabeblack@google.com         wait();
11512855Sgabeblack@google.com         data.range(11,8) = mii_data4_i;
11612855Sgabeblack@google.com         wait();
11712855Sgabeblack@google.com         data.range(15,12) = mii_data4_i;
11812855Sgabeblack@google.com         wait();
11912855Sgabeblack@google.com         data.range(19,16) = mii_data4_i;
12012855Sgabeblack@google.com         wait();
12112855Sgabeblack@google.com         data.range(23,20) = mii_data4_i;
12212855Sgabeblack@google.com         wait();
12312855Sgabeblack@google.com         data.range(27,24) = mii_data4_i;
12412855Sgabeblack@google.com         wait();
12512855Sgabeblack@google.com         data.range(31,28) = mii_data4_i;
12612855Sgabeblack@google.com         shared_mem2[i] = data;
12712855Sgabeblack@google.com         wait();
12812855Sgabeblack@google.com      }
12912855Sgabeblack@google.com      start_read = 1;
13012855Sgabeblack@google.com      wait();
13112855Sgabeblack@google.com      start_read = 0;
13212855Sgabeblack@google.com      wait();
13312855Sgabeblack@google.com   }
13412855Sgabeblack@google.com}
13512855Sgabeblack@google.com
13612855Sgabeblack@google.comsc_uint<32> io_controller_m::read_from_memory0(sc_uint<32> mp){
13712855Sgabeblack@google.com
13812855Sgabeblack@google.com   // read from mbdatm-memory over i486-IF
13912855Sgabeblack@google.com
14012855Sgabeblack@google.com   addr30_o1 = mp >> 2;
14112855Sgabeblack@google.com   ads_n_o1  = 0;
14212855Sgabeblack@google.com   wr_n_o1   = 0;
14312855Sgabeblack@google.com   wait();
14412855Sgabeblack@google.com   ads_n_o1 = 1;
14512855Sgabeblack@google.com   while (rdy_n_i.read() == 1) wait();
14612855Sgabeblack@google.com   sc_uint<32> data = data32_i.read();
14712855Sgabeblack@google.com   wr_n_o1  = 1;
14812855Sgabeblack@google.com   addr30_o1 = 0;
14912855Sgabeblack@google.com   return data;
15012855Sgabeblack@google.com}
15112855Sgabeblack@google.com
15212855Sgabeblack@google.comsc_uint<32> io_controller_m::read_from_memory1(sc_uint<32> mp){
15312855Sgabeblack@google.com
15412855Sgabeblack@google.com   // read from mbdatm-memory over i486-IF
15512855Sgabeblack@google.com
15612855Sgabeblack@google.com   addr30_o2 = mp >> 2;
15712855Sgabeblack@google.com   ads_n_o2  = 0;
15812855Sgabeblack@google.com   wr_n_o2   = 0;
15912855Sgabeblack@google.com   wait();
16012855Sgabeblack@google.com   ads_n_o2 = 1;
16112855Sgabeblack@google.com   while (rdy_n_i.read() == 1) wait();
16212855Sgabeblack@google.com   sc_uint<32> data = data32_i.read();
16312855Sgabeblack@google.com   wr_n_o2  = 1;
16412855Sgabeblack@google.com   addr30_o2 = 0;
16512855Sgabeblack@google.com   return data;
16612855Sgabeblack@google.com}
16712855Sgabeblack@google.com
16812855Sgabeblack@google.comvoid io_controller_m::write_into_memory0(sc_uint<32> mp, sc_uint<32> data){
16912855Sgabeblack@google.com
17012855Sgabeblack@google.com   addr30_o1 = mp >> 2;
17112855Sgabeblack@google.com   ads_n_o1 = 0;
17212855Sgabeblack@google.com   wr_n_o1 = 1;
17312855Sgabeblack@google.com   wait();
17412855Sgabeblack@google.com   ads_n_o1 = 1;
17512855Sgabeblack@google.com   data32_o1 = data;
17612855Sgabeblack@google.com   while (rdy_n_i.read() == 1) wait();
17712855Sgabeblack@google.com   wr_n_o1 = 1;
17812855Sgabeblack@google.com   addr30_o1 = 0;
17912855Sgabeblack@google.com   data32_o1 = 0;
18012855Sgabeblack@google.com}
18112855Sgabeblack@google.com
18212855Sgabeblack@google.comvoid io_controller_m::write_into_memory1(sc_uint<32> mp, sc_uint<32> data){
18312855Sgabeblack@google.com
18412855Sgabeblack@google.com   addr30_o2 = mp >> 2;
18512855Sgabeblack@google.com   ads_n_o2 = 0;
18612855Sgabeblack@google.com   wr_n_o2 = 1;
18712855Sgabeblack@google.com   wait();
18812855Sgabeblack@google.com   ads_n_o2 = 1;
18912855Sgabeblack@google.com   data32_o2 = data;
19012855Sgabeblack@google.com   while (rdy_n_i.read() == 1) wait();
19112855Sgabeblack@google.com   wr_n_o2 = 1;
19212855Sgabeblack@google.com   addr30_o2 = 0;
19312855Sgabeblack@google.com   data32_o2 = 0;
19412855Sgabeblack@google.com}
19512855Sgabeblack@google.com
19612855Sgabeblack@google.comvoid io_controller_m::control_write(){
19712855Sgabeblack@google.com   sc_uint<32> word_cnt;
19812855Sgabeblack@google.com
19912855Sgabeblack@google.com   while (res_n_i.read() == 0) wait();
20012855Sgabeblack@google.com
20112855Sgabeblack@google.com   // initialize
20212855Sgabeblack@google.com
20312855Sgabeblack@google.com   // wait for 1. AR (HWS-Daten)
20412855Sgabeblack@google.com   while (ar_i.read() == 0) wait();
20512855Sgabeblack@google.com   sc_uint<32> hws = data32_i.read();
20612855Sgabeblack@google.com
20712855Sgabeblack@google.com   wait();
20812855Sgabeblack@google.com
20912855Sgabeblack@google.com   // wait for 2. AR (ACB-Pointer)
21012855Sgabeblack@google.com   while (ar_i.read() == 0) wait();
21112855Sgabeblack@google.com   addr_tx_frame_ptr = data32_i.read();
21212855Sgabeblack@google.com   wait();
21312855Sgabeblack@google.com
21412855Sgabeblack@google.com
21512855Sgabeblack@google.com   while(true){
21612855Sgabeblack@google.com      // normally Attention Request - Signal from MBDATM
21712855Sgabeblack@google.com      // would wake up IO-Controller to read data from the memory,
21812855Sgabeblack@google.com      // but the model from Hr. Wahl said: wait for some ms !!!
21912855Sgabeblack@google.com
22012855Sgabeblack@google.com      wait(1000);
22112855Sgabeblack@google.com
22212855Sgabeblack@google.com      #ifdef LOGGING
22312855Sgabeblack@google.com         flog << sc_time_stamp()<<": "<<name()<<"::control_write - Attention Request" << endl;
22412855Sgabeblack@google.com      #endif
22512855Sgabeblack@google.com
22612855Sgabeblack@google.com      while (sem2) wait(); sem2 = true;  // P-operation
22712855Sgabeblack@google.com      sc_uint<32> tx_frame_ptr = read_from_memory0(addr_tx_frame_ptr);
22812855Sgabeblack@google.com      if (tx_frame_ptr != 0)
22912855Sgabeblack@google.com         word_cnt = read_from_memory0(tx_frame_ptr+(MII_FRAME_SIZE+1)*4);
23012855Sgabeblack@google.com      sem2 = false;  // V-operation
23112855Sgabeblack@google.com
23212855Sgabeblack@google.com      // check, if frame available and frame is full (word_cnt == MII_FRAME_SIZE)
23312855Sgabeblack@google.com
23412855Sgabeblack@google.com      while (tx_frame_ptr != 0 && word_cnt == MII_FRAME_SIZE){
23512855Sgabeblack@google.com         #ifdef LOGGING
23612855Sgabeblack@google.com            flog << sc_time_stamp()<<": "<<name()<<"::control_write - writing mii_frame into out_fifo" << endl;
23712855Sgabeblack@google.com         #endif
23812855Sgabeblack@google.com
23912855Sgabeblack@google.com
24012855Sgabeblack@google.com         for (int i = 0; i<MII_FRAME_SIZE; i++){
24112855Sgabeblack@google.com            // reading from i486-IF and writing into
24212855Sgabeblack@google.com            // out_fifo is mixed, so read_from_memory could not be applied
24312855Sgabeblack@google.com
24412855Sgabeblack@google.com            while (sem2) wait(); sem2 = true;  // P-operation
24512855Sgabeblack@google.com            sc_uint<32> data = read_from_memory0(tx_frame_ptr+i*4);
24612855Sgabeblack@google.com            sem2 = false;  // V-operation
24712855Sgabeblack@google.com
24812855Sgabeblack@google.com            if (i == 0){
24912855Sgabeblack@google.com               start_mux = 1;
25012855Sgabeblack@google.com               shared_mem1[i] = data;
25112855Sgabeblack@google.com               wait();
25212855Sgabeblack@google.com               start_mux = 0;
25312855Sgabeblack@google.com            }
25412855Sgabeblack@google.com            else {
25512855Sgabeblack@google.com               shared_mem1[i] = data;
25612855Sgabeblack@google.com               wait();
25712855Sgabeblack@google.com            }
25812855Sgabeblack@google.com            // wait(); ??
25912855Sgabeblack@google.com         }
26012855Sgabeblack@google.com
26112855Sgabeblack@google.com         while (ready_mux.read() == 0) wait();
26212855Sgabeblack@google.com
26312855Sgabeblack@google.com         // write 0xFFFFFFFF (>MII_FRAME_SIZE) into tx_frame_ptr
26412855Sgabeblack@google.com         // to signal software in mbdatm that io-controller has
26512855Sgabeblack@google.com         // read out the frames and sent successfully
26612855Sgabeblack@google.com         while (sem2) wait(); sem2 = true;  // P-operation
26712855Sgabeblack@google.com         write_into_memory0(tx_frame_ptr+(MII_FRAME_SIZE+1)*4, 0xFFFFFFFF);
26812855Sgabeblack@google.com         sem2 = false;  // V-operation
26912855Sgabeblack@google.com
27012855Sgabeblack@google.com         // read next frame_pointer and word_cnt from MBDATM
27112855Sgabeblack@google.com         while (sem2) wait(); sem2 = true;  // P-operation
27212855Sgabeblack@google.com         tx_frame_ptr = read_from_memory0(tx_frame_ptr+MII_FRAME_SIZE*4);
27312855Sgabeblack@google.com         if (tx_frame_ptr != 0)
27412855Sgabeblack@google.com            word_cnt = read_from_memory0(tx_frame_ptr+(MII_FRAME_SIZE+1)*4);
27512855Sgabeblack@google.com         sem2 = false;  // V-operation
27612855Sgabeblack@google.com
27712855Sgabeblack@google.com
27812855Sgabeblack@google.com      }
27912855Sgabeblack@google.com
28012855Sgabeblack@google.com   }
28112855Sgabeblack@google.com}
28212855Sgabeblack@google.com
28312855Sgabeblack@google.comvoid io_controller_m::control_read(){
28412855Sgabeblack@google.com
28512855Sgabeblack@google.com    int arr_ptr = 0;
28612855Sgabeblack@google.com
28712855Sgabeblack@google.com   while (true){
28812855Sgabeblack@google.com      while (start_read.read() == 0) wait();
28912855Sgabeblack@google.com      #ifdef LOGGING
29012855Sgabeblack@google.com         flog << sc_time_stamp()<<": "<<name()<<"::control_read " << endl;
29112855Sgabeblack@google.com      #endif
29212855Sgabeblack@google.com
29312855Sgabeblack@google.com      // read rx_frame_ptr from MBDATM
29412855Sgabeblack@google.com      while (sem1) wait(); sem1 = true;  // P-operation
29512855Sgabeblack@google.com      sc_uint<32> rx_frame_ptr = read_from_memory1(rx_ptr_array+arr_ptr*4);
29612855Sgabeblack@google.com      sem1 = false;  // V-operation
29712855Sgabeblack@google.com      /*if (rx_frame_ptr == 0){
29812855Sgabeblack@google.com         cerr << "\nIO-Controller has read NULL-ptr from rx_array in MBDATM\n";
29912855Sgabeblack@google.com         cerr << "MBDATM did not fill rx_array fast enough\n";
30012855Sgabeblack@google.com         exit(-1);
30112855Sgabeblack@google.com      }*/
30212855Sgabeblack@google.com      if (++arr_ptr == MII_FRAME_SIZE)
30312855Sgabeblack@google.com         arr_ptr = 0;
30412855Sgabeblack@google.com
30512855Sgabeblack@google.com      // write data from in_fifo into MBDATM-memory
30612855Sgabeblack@google.com      for (int i = 0; i < MII_FRAME_SIZE; i++){
30712855Sgabeblack@google.com         sc_uint<32> d = shared_mem2[i];
30812855Sgabeblack@google.com         // grab the semaphore
30912855Sgabeblack@google.com         while (sem1) wait(); sem1 = true;  // P-operation
31012855Sgabeblack@google.com         write_into_memory1(rx_frame_ptr + i*4, d);
31112855Sgabeblack@google.com         // release semaphore
31212855Sgabeblack@google.com         sem1 = false;  // V-operation
31312855Sgabeblack@google.com         wait();
31412855Sgabeblack@google.com
31512855Sgabeblack@google.com      }
31612855Sgabeblack@google.com
31712855Sgabeblack@google.com      // write 0xFFFFFFFF into word_cnt from frame
31812855Sgabeblack@google.com      // to indicate the software (MBDATM) that frame has been filled
31912855Sgabeblack@google.com      while (sem1) wait(); sem1 = true;  // P-operation
32012855Sgabeblack@google.com      write_into_memory1(rx_frame_ptr + (MII_FRAME_SIZE+1)*4, 0xFFFFFFFF);
32112855Sgabeblack@google.com      sem1 = false;  // V-operation
32212855Sgabeblack@google.com   }
32312855Sgabeblack@google.com}
32412855Sgabeblack@google.com
325