RubySlicc_Util.hh revision 11208:fa3e56b6e0b6
1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * These are the functions that exported to slicc from ruby.
31 */
32
33#ifndef __MEM_RUBY_SLICC_INTERFACE_RUBYSLICCUTIL_HH__
34#define __MEM_RUBY_SLICC_INTERFACE_RUBYSLICCUTIL_HH__
35
36#include <cassert>
37
38#include "debug/RubySlicc.hh"
39#include "mem/packet.hh"
40#include "mem/ruby/common/Address.hh"
41#include "mem/ruby/common/BoolVec.hh"
42#include "mem/ruby/common/DataBlock.hh"
43#include "mem/ruby/common/TypeDefines.hh"
44
45inline Cycles zero_time() { return Cycles(0); }
46
47inline NodeID
48intToID(int nodenum)
49{
50    NodeID id = nodenum;
51    return id;
52}
53
54inline int
55IDToInt(NodeID id)
56{
57    int nodenum = id;
58    return nodenum;
59}
60
61inline int
62addressToInt(Addr addr)
63{
64    assert(!(addr & 0xffffffff00000000));
65    return addr;
66}
67
68inline int
69mod(int val, int mod)
70{
71    return val % mod;
72}
73
74inline int max_tokens()
75{
76  return 1024;
77}
78
79/**
80 * This function accepts an address, a data block and a packet. If the address
81 * range for the data block contains the address which the packet needs to
82 * read, then the data from the data block is written to the packet. True is
83 * returned if the data block was read, otherwise false is returned.
84 */
85inline bool
86testAndRead(Addr addr, DataBlock& blk, Packet *pkt)
87{
88    Addr pktLineAddr = makeLineAddress(pkt->getAddr());
89    Addr lineAddr = makeLineAddress(addr);
90
91    if (pktLineAddr == lineAddr) {
92        uint8_t *data = pkt->getPtr<uint8_t>();
93        unsigned int size_in_bytes = pkt->getSize();
94        unsigned startByte = pkt->getAddr() - lineAddr;
95
96        for (unsigned i = 0; i < size_in_bytes; ++i) {
97            data[i] = blk.getByte(i + startByte);
98        }
99        return true;
100    }
101    return false;
102}
103
104/**
105 * This function accepts an address, a data block and a packet. If the address
106 * range for the data block contains the address which the packet needs to
107 * write, then the data from the packet is written to the data block. True is
108 * returned if the data block was written, otherwise false is returned.
109 */
110inline bool
111testAndWrite(Addr addr, DataBlock& blk, Packet *pkt)
112{
113    Addr pktLineAddr = makeLineAddress(pkt->getAddr());
114    Addr lineAddr = makeLineAddress(addr);
115
116    if (pktLineAddr == lineAddr) {
117        const uint8_t *data = pkt->getConstPtr<uint8_t>();
118        unsigned int size_in_bytes = pkt->getSize();
119        unsigned startByte = pkt->getAddr() - lineAddr;
120
121        for (unsigned i = 0; i < size_in_bytes; ++i) {
122            blk.setByte(i + startByte, data[i]);
123        }
124        return true;
125    }
126    return false;
127}
128
129inline int
130countBoolVec(BoolVec bVec)
131{
132    int count = 0;
133    for (const auto &it: bVec) {
134        if (it) {
135            count++;
136        }
137    }
138    return count;
139}
140
141#endif // __MEM_RUBY_SLICC_INTERFACE_RUBYSLICCUTIL_HH__
142