RubySlicc_Util.hh revision 9466
16145Snate@binkert.org/* 26145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 36145Snate@binkert.org * All rights reserved. 46145Snate@binkert.org * 56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without 66145Snate@binkert.org * modification, are permitted provided that the following conditions are 76145Snate@binkert.org * met: redistributions of source code must retain the above copyright 86145Snate@binkert.org * notice, this list of conditions and the following disclaimer; 96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright 106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the 116145Snate@binkert.org * documentation and/or other materials provided with the distribution; 126145Snate@binkert.org * neither the name of the copyright holders nor the names of its 136145Snate@binkert.org * contributors may be used to endorse or promote products derived from 146145Snate@binkert.org * this software without specific prior written permission. 156145Snate@binkert.org * 166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 276145Snate@binkert.org */ 286145Snate@binkert.org 296145Snate@binkert.org/* 307039Snate@binkert.org * These are the functions that exported to slicc from ruby. 316145Snate@binkert.org */ 326145Snate@binkert.org 337039Snate@binkert.org#ifndef __MEM_RUBY_SLICC_INTERFACE_RUBYSLICCUTIL_HH__ 347039Snate@binkert.org#define __MEM_RUBY_SLICC_INTERFACE_RUBYSLICCUTIL_HH__ 356145Snate@binkert.org 367832Snate@binkert.org#include <cassert> 377832Snate@binkert.org 389302Snilay@cs.wisc.edu#include "debug/RubySlicc.hh" 397039Snate@binkert.org#include "mem/ruby/common/Address.hh" 406154Snate@binkert.org#include "mem/ruby/common/Global.hh" 417039Snate@binkert.org#include "mem/ruby/slicc_interface/RubySlicc_ComponentMapping.hh" 426285Snate@binkert.org#include "mem/ruby/system/System.hh" 436145Snate@binkert.org 447039Snate@binkert.orginline int 457039Snate@binkert.orgrandom(int n) 466145Snate@binkert.org{ 476145Snate@binkert.org return random() % n; 486145Snate@binkert.org} 496145Snate@binkert.org 507039Snate@binkert.orginline Time 517039Snate@binkert.orgget_time() 526145Snate@binkert.org{ 539171Snilay@cs.wisc.edu return g_system_ptr->getTime(); 546145Snate@binkert.org} 556145Snate@binkert.org 567039Snate@binkert.orginline Time 577039Snate@binkert.orgzero_time() 586145Snate@binkert.org{ 597039Snate@binkert.org return 0; 606145Snate@binkert.org} 616145Snate@binkert.org 627039Snate@binkert.orginline NodeID 637039Snate@binkert.orgintToID(int nodenum) 646145Snate@binkert.org{ 657039Snate@binkert.org NodeID id = nodenum; 667039Snate@binkert.org return id; 676145Snate@binkert.org} 686145Snate@binkert.org 697039Snate@binkert.orginline int 707039Snate@binkert.orgIDToInt(NodeID id) 716145Snate@binkert.org{ 727039Snate@binkert.org int nodenum = id; 737039Snate@binkert.org return nodenum; 746145Snate@binkert.org} 756145Snate@binkert.org 767039Snate@binkert.orginline Time 777039Snate@binkert.orggetTimeModInt(Time time, int modulus) 786145Snate@binkert.org{ 797039Snate@binkert.org return time % modulus; 806145Snate@binkert.org} 816145Snate@binkert.org 827039Snate@binkert.orginline Time 837039Snate@binkert.orggetTimePlusInt(Time addend1, int addend2) 846145Snate@binkert.org{ 857039Snate@binkert.org return (Time) addend1 + addend2; 866145Snate@binkert.org} 876145Snate@binkert.org 887039Snate@binkert.orginline Time 897039Snate@binkert.orggetTimeMinusTime(Time t1, Time t2) 906145Snate@binkert.org{ 917832Snate@binkert.org assert(t1 >= t2); 927039Snate@binkert.org return t1 - t2; 936145Snate@binkert.org} 946145Snate@binkert.org 957039Snate@binkert.org// Return type for time_to_int is "Time" and not "int" so we get a 967039Snate@binkert.org// 64-bit integer 977039Snate@binkert.orginline Time 987039Snate@binkert.orgtime_to_int(Time time) 996145Snate@binkert.org{ 1007039Snate@binkert.org return time; 1016145Snate@binkert.org} 1026145Snate@binkert.org 1036145Snate@binkert.org// Appends an offset to an address 1047039Snate@binkert.orginline Address 1057039Snate@binkert.orgsetOffset(Address addr, int offset) 1066145Snate@binkert.org{ 1077039Snate@binkert.org Address result = addr; 1087039Snate@binkert.org result.setOffset(offset); 1097039Snate@binkert.org return result; 1106145Snate@binkert.org} 1116145Snate@binkert.org 1126145Snate@binkert.org// Makes an address into a line address 1137039Snate@binkert.orginline Address 1147039Snate@binkert.orgmakeLineAddress(Address addr) 1156145Snate@binkert.org{ 1167039Snate@binkert.org Address result = addr; 1177039Snate@binkert.org result.makeLineAddress(); 1187039Snate@binkert.org return result; 1196145Snate@binkert.org} 1206145Snate@binkert.org 1217039Snate@binkert.orginline int 1227039Snate@binkert.orgaddressOffset(Address addr) 1236467Sdrh5@cs.wisc.edu{ 1247039Snate@binkert.org return addr.getOffset(); 1256467Sdrh5@cs.wisc.edu} 1266467Sdrh5@cs.wisc.edu 1278131SLisa.Hsu@amd.cominline int 1288131SLisa.Hsu@amd.commod(int val, int mod) 1298131SLisa.Hsu@amd.com{ 1308131SLisa.Hsu@amd.com return val % mod; 1318131SLisa.Hsu@amd.com} 1328131SLisa.Hsu@amd.com 1339466Snilay@cs.wisc.eduinline int max_tokens() 1349466Snilay@cs.wisc.edu{ 1359466Snilay@cs.wisc.edu return 1024; 1369466Snilay@cs.wisc.edu} 1379466Snilay@cs.wisc.edu 1389302Snilay@cs.wisc.edu/** 1399302Snilay@cs.wisc.edu * This function accepts an address, a data block and a packet. If the address 1409302Snilay@cs.wisc.edu * range for the data block contains the address which the packet needs to 1419302Snilay@cs.wisc.edu * read, then the data from the data block is written to the packet. True is 1429302Snilay@cs.wisc.edu * returned if the data block was read, otherwise false is returned. 1439302Snilay@cs.wisc.edu */ 1449302Snilay@cs.wisc.eduinline bool 1459302Snilay@cs.wisc.edutestAndRead(Address addr, DataBlock& blk, Packet *pkt) 1469302Snilay@cs.wisc.edu{ 1479302Snilay@cs.wisc.edu Address pktLineAddr(pkt->getAddr()); 1489302Snilay@cs.wisc.edu pktLineAddr.makeLineAddress(); 1499302Snilay@cs.wisc.edu 1509302Snilay@cs.wisc.edu Address lineAddr = addr; 1519302Snilay@cs.wisc.edu lineAddr.makeLineAddress(); 1529302Snilay@cs.wisc.edu 1539302Snilay@cs.wisc.edu if (pktLineAddr == lineAddr) { 1549302Snilay@cs.wisc.edu uint8_t *data = pkt->getPtr<uint8_t>(true); 1559302Snilay@cs.wisc.edu unsigned int size_in_bytes = pkt->getSize(); 1569302Snilay@cs.wisc.edu unsigned startByte = pkt->getAddr() - lineAddr.getAddress(); 1579302Snilay@cs.wisc.edu 1589302Snilay@cs.wisc.edu for (unsigned i = 0; i < size_in_bytes; ++i) { 1599302Snilay@cs.wisc.edu data[i] = blk.getByte(i + startByte); 1609302Snilay@cs.wisc.edu } 1619302Snilay@cs.wisc.edu return true; 1629302Snilay@cs.wisc.edu } 1639302Snilay@cs.wisc.edu return false; 1649302Snilay@cs.wisc.edu} 1659302Snilay@cs.wisc.edu 1669302Snilay@cs.wisc.edu/** 1679302Snilay@cs.wisc.edu * This function accepts an address, a data block and a packet. If the address 1689302Snilay@cs.wisc.edu * range for the data block contains the address which the packet needs to 1699302Snilay@cs.wisc.edu * write, then the data from the packet is written to the data block. True is 1709302Snilay@cs.wisc.edu * returned if the data block was written, otherwise false is returned. 1719302Snilay@cs.wisc.edu */ 1729302Snilay@cs.wisc.eduinline bool 1739302Snilay@cs.wisc.edutestAndWrite(Address addr, DataBlock& blk, Packet *pkt) 1749302Snilay@cs.wisc.edu{ 1759302Snilay@cs.wisc.edu Address pktLineAddr(pkt->getAddr()); 1769302Snilay@cs.wisc.edu pktLineAddr.makeLineAddress(); 1779302Snilay@cs.wisc.edu 1789302Snilay@cs.wisc.edu Address lineAddr = addr; 1799302Snilay@cs.wisc.edu lineAddr.makeLineAddress(); 1809302Snilay@cs.wisc.edu 1819302Snilay@cs.wisc.edu if (pktLineAddr == lineAddr) { 1829302Snilay@cs.wisc.edu uint8_t *data = pkt->getPtr<uint8_t>(true); 1839302Snilay@cs.wisc.edu unsigned int size_in_bytes = pkt->getSize(); 1849302Snilay@cs.wisc.edu unsigned startByte = pkt->getAddr() - lineAddr.getAddress(); 1859302Snilay@cs.wisc.edu 1869302Snilay@cs.wisc.edu for (unsigned i = 0; i < size_in_bytes; ++i) { 1879302Snilay@cs.wisc.edu blk.setByte(i + startByte, data[i]); 1889302Snilay@cs.wisc.edu } 1899302Snilay@cs.wisc.edu return true; 1909302Snilay@cs.wisc.edu } 1919302Snilay@cs.wisc.edu return false; 1929302Snilay@cs.wisc.edu} 1939302Snilay@cs.wisc.edu 1947039Snate@binkert.org#endif // __MEM_RUBY_SLICC_INTERFACE_RUBYSLICCUTIL_HH__ 195