Merge ktlim@zizzer:/bk/newmem
[gem5.git] / src / cpu / trace / reader / ibm_reader.cc
1 /*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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 * Authors: Erik Hallnor
29 */
30
31 /**
32 * @file
33 * Declaration of a IBM memory trace format reader.
34 */
35 #include <sstream>
36
37 #include "cpu/trace/reader/ibm_reader.hh"
38 #include "sim/builder.hh"
39 #include "base/misc.hh" // for fatal
40
41 using namespace std;
42
43 IBMReader::IBMReader(const string &name, const string &filename)
44 : MemTraceReader(name)
45 {
46 if (strcmp((filename.c_str() + filename.length() -3), ".gz") == 0) {
47 // Compressed file, need to use a pipe to gzip.
48 stringstream buf;
49 buf << "gzip -d -c " << filename << endl;
50 trace = popen(buf.str().c_str(), "r");
51 } else {
52 trace = fopen(filename.c_str(), "rb");
53 }
54 if (!trace) {
55 fatal("Can't open file %s", filename);
56 }
57 }
58
59 Tick
60 IBMReader::getNextReq(MemReqPtr &req)
61 {
62 MemReqPtr tmp_req;
63
64 int c = getc(trace);
65 if (c != EOF) {
66 tmp_req = new MemReq();
67 //int cpu_id = (c & 0xf0) >> 4;
68 int type = c & 0x0f;
69 // We have L1 miss traces, so all accesses are 128 bytes
70 tmp_req->size = 128;
71
72 tmp_req->paddr = 0;
73 for (int i = 2; i >= 0; --i) {
74 c = getc(trace);
75 if (c == EOF) {
76 fatal("Unexpected end of file");
77 }
78 tmp_req->paddr |= ((c & 0xff) << (8 * i));
79 }
80 tmp_req->paddr = tmp_req->paddr << 7;
81
82 switch(type) {
83 case IBM_COND_EXCLUSIVE_FETCH:
84 case IBM_READ_ONLY_FETCH:
85 tmp_req->cmd = Read;
86 break;
87 case IBM_EXCLUSIVE_FETCH:
88 case IBM_FETCH_NO_DATA:
89 tmp_req->cmd = Write;
90 break;
91 case IBM_INST_FETCH:
92 tmp_req->cmd = Read;
93 break;
94 default:
95 fatal("Unknown trace entry type.");
96 }
97
98 }
99 req = tmp_req;
100 return 0;
101 }
102
103 BEGIN_DECLARE_SIM_OBJECT_PARAMS(IBMReader)
104
105 Param<string> filename;
106
107 END_DECLARE_SIM_OBJECT_PARAMS(IBMReader)
108
109
110 BEGIN_INIT_SIM_OBJECT_PARAMS(IBMReader)
111
112 INIT_PARAM(filename, "trace file")
113
114 END_INIT_SIM_OBJECT_PARAMS(IBMReader)
115
116
117 CREATE_SIM_OBJECT(IBMReader)
118 {
119 return new IBMReader(getInstanceName(), filename);
120 }
121
122 REGISTER_SIM_OBJECT("IBMReader", IBMReader)