112337Sjason@lowepower.com/* 212337Sjason@lowepower.com * Copyright (c) 2017 Jason Lowe-Power 312337Sjason@lowepower.com * All rights reserved. 412337Sjason@lowepower.com * 512337Sjason@lowepower.com * Redistribution and use in source and binary forms, with or without 612337Sjason@lowepower.com * modification, are permitted provided that the following conditions are 712337Sjason@lowepower.com * met: redistributions of source code must retain the above copyright 812337Sjason@lowepower.com * notice, this list of conditions and the following disclaimer; 912337Sjason@lowepower.com * redistributions in binary form must reproduce the above copyright 1012337Sjason@lowepower.com * notice, this list of conditions and the following disclaimer in the 1112337Sjason@lowepower.com * documentation and/or other materials provided with the distribution; 1212337Sjason@lowepower.com * neither the name of the copyright holders nor the names of its 1312337Sjason@lowepower.com * contributors may be used to endorse or promote products derived from 1412337Sjason@lowepower.com * this software without specific prior written permission. 1512337Sjason@lowepower.com * 1612337Sjason@lowepower.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1712337Sjason@lowepower.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1812337Sjason@lowepower.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1912337Sjason@lowepower.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2012337Sjason@lowepower.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2112337Sjason@lowepower.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2212337Sjason@lowepower.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2312337Sjason@lowepower.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2412337Sjason@lowepower.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2512337Sjason@lowepower.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2612337Sjason@lowepower.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2712337Sjason@lowepower.com * 2812337Sjason@lowepower.com * Authors: Jason Lowe-Power 2912337Sjason@lowepower.com */ 3012337Sjason@lowepower.com 3112337Sjason@lowepower.com#include "learning_gem5/part2/goodbye_object.hh" 3212337Sjason@lowepower.com 3312337Sjason@lowepower.com#include "debug/HelloExample.hh" 3412337Sjason@lowepower.com#include "sim/sim_exit.hh" 3512337Sjason@lowepower.com 3612337Sjason@lowepower.comGoodbyeObject::GoodbyeObject(GoodbyeObjectParams *params) : 3712337Sjason@lowepower.com SimObject(params), event([this]{ processEvent(); }, name() + ".event"), 3812337Sjason@lowepower.com bandwidth(params->write_bandwidth), bufferSize(params->buffer_size), 3912337Sjason@lowepower.com buffer(nullptr), bufferUsed(0) 4012337Sjason@lowepower.com{ 4112337Sjason@lowepower.com buffer = new char[bufferSize]; 4212337Sjason@lowepower.com DPRINTF(HelloExample, "Created the goodbye object\n"); 4312337Sjason@lowepower.com} 4412337Sjason@lowepower.com 4512337Sjason@lowepower.comGoodbyeObject::~GoodbyeObject() 4612337Sjason@lowepower.com{ 4712337Sjason@lowepower.com delete[] buffer; 4812337Sjason@lowepower.com} 4912337Sjason@lowepower.com 5012337Sjason@lowepower.comvoid 5112337Sjason@lowepower.comGoodbyeObject::processEvent() 5212337Sjason@lowepower.com{ 5312337Sjason@lowepower.com DPRINTF(HelloExample, "Processing the event!\n"); 5412337Sjason@lowepower.com 5512337Sjason@lowepower.com // Actually do the "work" of the event 5612337Sjason@lowepower.com fillBuffer(); 5712337Sjason@lowepower.com} 5812337Sjason@lowepower.com 5912337Sjason@lowepower.comvoid 6012337Sjason@lowepower.comGoodbyeObject::sayGoodbye(std::string other_name) 6112337Sjason@lowepower.com{ 6212337Sjason@lowepower.com DPRINTF(HelloExample, "Saying goodbye to %s\n", other_name); 6312337Sjason@lowepower.com 6412337Sjason@lowepower.com message = "Goodbye " + other_name + "!! "; 6512337Sjason@lowepower.com 6612337Sjason@lowepower.com // Kick off the the first buffer fill. If it can't fill the whole buffer 6712337Sjason@lowepower.com // because of a limited bandwidth, then this function will schedule another 6812337Sjason@lowepower.com // event to finish the fill 6912337Sjason@lowepower.com fillBuffer(); 7012337Sjason@lowepower.com} 7112337Sjason@lowepower.com 7212337Sjason@lowepower.comvoid 7312337Sjason@lowepower.comGoodbyeObject::fillBuffer() 7412337Sjason@lowepower.com{ 7512337Sjason@lowepower.com // There better be a message 7612337Sjason@lowepower.com assert(message.length() > 0); 7712337Sjason@lowepower.com 7812337Sjason@lowepower.com // Copy from the message to the buffer per byte. 7912337Sjason@lowepower.com int bytes_copied = 0; 8012337Sjason@lowepower.com for (auto it = message.begin(); 8112337Sjason@lowepower.com it < message.end() && bufferUsed < bufferSize - 1; 8212337Sjason@lowepower.com it++, bufferUsed++, bytes_copied++) { 8312337Sjason@lowepower.com // Copy the character into the buffer 8412337Sjason@lowepower.com buffer[bufferUsed] = *it; 8512337Sjason@lowepower.com } 8612337Sjason@lowepower.com 8712337Sjason@lowepower.com if (bufferUsed < bufferSize - 1) { 8812337Sjason@lowepower.com // Wait for the next copy for as long as it would have taken 8912337Sjason@lowepower.com DPRINTF(HelloExample, "Scheduling another fillBuffer in %d ticks\n", 9012337Sjason@lowepower.com bandwidth * bytes_copied); 9112337Sjason@lowepower.com schedule(event, curTick() + bandwidth * bytes_copied); 9212337Sjason@lowepower.com } else { 9312337Sjason@lowepower.com DPRINTF(HelloExample, "Goodbye done copying!\n"); 9412337Sjason@lowepower.com // Be sure to take into account the time for the last bytes 9512337Sjason@lowepower.com exitSimLoop(buffer, 0, curTick() + bandwidth * bytes_copied); 9612337Sjason@lowepower.com } 9712337Sjason@lowepower.com} 9812337Sjason@lowepower.com 9912337Sjason@lowepower.comGoodbyeObject* 10012337Sjason@lowepower.comGoodbyeObjectParams::create() 10112337Sjason@lowepower.com{ 10212337Sjason@lowepower.com return new GoodbyeObject(this); 10312337Sjason@lowepower.com} 104