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