feeder.cc revision 13373:962c151f213c
1/* 2 * Copyright 2018 Google, Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer; 8 * redistributions in binary form must reproduce the above copyright 9 * notice, this list of conditions and the following disclaimer in the 10 * documentation and/or other materials provided with the distribution; 11 * neither the name of the copyright holders nor the names of its 12 * contributors may be used to endorse or promote products derived from 13 * this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * Authors: Gabe Black 28 */ 29 30#include "params/Gem5_Feeder.hh" 31#include "sim/sim_exit.hh" 32#include "systemc_simple_object/feeder.hh" 33 34Feeder::Feeder(Gem5_FeederParams *params) : 35 SimObject(params), printer(params->printer), delay(params->delay), 36 strings(params->strings), index(0), event(this) 37{ 38 // Bind the printer objects "input" port to our sc_buffer. This will let 39 // us feed it values. If some other object was responsible for the 40 // sc_buffer, we could interact with it directly and not have built it 41 // ourselves. 42 // 43 // Alternatively Printer could define a normal c++ method which we could 44 // call to feed it words without involving any systemc specific classes. 45 printer->input(buf); 46} 47 48void 49Feeder::startup() 50{ 51 schedule(&event, curTick() + delay); 52} 53 54void 55Feeder::feed() 56{ 57 if (index >= strings.size()) 58 exitSimLoop("Printed all the words."); 59 else 60 buf.write(strings[index++].c_str()); 61 62 schedule(&event, curTick() + delay); 63} 64 65Feeder * 66Gem5_FeederParams::create() 67{ 68 return new Feeder(this); 69} 70