3 #include "DebugInterface.h"
10 for (traceIndex
= 0; traceIndex
< traceBufferSize
; traceIndex
++)
11 traceBuffer
[traceIndex
] = NOTHING
;
21 // type() - return the trace type at 'index'
22 unsigned int Trace::type(unsigned int index
) const {
23 unsigned int traceType
= get(index
) & TYPE_MASK
;
24 unsigned int cycleType
= traceType
& (CYCLE_COUNTER_LO
26 return cycleType
? cycleType
: traceType
;
29 // When logging is enabled, the entire trace buffer will be copied to a file.
30 void Trace::enableLogging(char *fname
) {
31 fout
.open( fname
, std::ios::out
| std::ios::binary
);
34 void Trace::disableLogging() {
38 int Trace::isCycleTrace(unsigned int index
, lword
*cycle
) const {
39 if ( (get(index
) & (CYCLE_COUNTER_LO
| CYCLE_COUNTER_HI
)) == 0)
44 // A cycle counter occupies two consecutive trace buffer entries.
45 // We have to determine if the current entry (pointed to by index) is
46 // the high or low integer of the cycle counter.
48 // The upper two bits of the trace are used to decode the two 32-bit
49 // integers that comprise the cycle counter. The encoding algorithm is
50 // optimized for speed:
51 // CYCLE_COUNTER_LO is defined as 1<<31
52 // CYCLE_COUNTER_HI is defined as 1<<30
54 // trace[i] = low 32 bits of cycle counter | CYCLE_COUNTER_LO
55 // trace[i+1] = upper 32 bits of " " | CYCLE_COUNTER_HI | bit 31 of cycle counter
57 // The low 32-bits are always saved in the trace buffer with the msb (CYCLE_COUNTER_LO)
58 // set. However, notice that this bit may've already been set prior to calling trace().
59 // So we need to make sure that we don't lose it. This is done by copying it along
60 // with the high 32-bits of the cycle counter into the next trace buffer location. The
61 // upper 2 bits of the cycle counter are assumed to always be zero (if they're not, gpsim
62 // has been running for a loooonnnggg time!). Bit 30 (CYCLE_COUNTER_HIGH) is always
63 // set in the high 32 bit trace. While bit 31 gets the copy of bit 31 that was over
64 // written in the low 32 bit trace.
66 // Here are some examples:
68 // cycle counter | trace[i] trace[i+1] [i] [i+1]
69 //---------------------+----------------------------------------
70 // 0x12345678 | 0x92345678 0x40000000 10 01
71 // 0x44445555 | 0xc4445555 0x40000000 11 01
72 // 0x1111222233334444 | 0xb3334444 0x51112222 10 01
73 // 0x9999aaaa | 0x9999aaaa 0xc0000000 10 11
74 // 0xccccdddd | 0xccccdddd 0xc0000000 11 11
75 // 0xccccddde | 0xccccddde 0xc0000000 11 11
77 // Looking at the upper two bits of trace buffer, we can make these
80 // 00 - not a cycle counter trace
81 // 10 - current index points at the low int of a cycle counter
82 // 01 - current index points at the high int of a cycle counter
83 // 11 - if traces on either side of the current index are the same
84 // then the current index points to a low int else it points to a high int
86 int j
= index
; // Assume that the index is pointing to the low int.
87 int k
= (j
+ 1) & traceBufferMask
; // and that the next entry is the high int.
89 if ( ((get(j
) & CYCLE_COUNTER_LO
) != 0)&& ((get(k
) & CYCLE_COUNTER_HI
))
91 if ( (get(j
) & CYCLE_COUNTER_HI
) != 0) {
92 // The upper two bits of the current trace are set. This means that
93 // the trace is either the high 32 bits or the low 32 bits of the cycle
94 // counter. This ambiguity is resolved by examining the trace buffer on
95 // either side of the current index. If the entry immediately proceeding
96 // this one is not a cycle counter trace, then we know that we're pointing
97 // at the low 32 bits. If the proceeding entry IS a cycle counter trace then
98 // we have two consecutive cycle traces (we already know that the entry
99 // immediately following the current trace index is a cycle counter trace).
100 // Now we know that if have consecutive cycle traces, then they differ by one
101 // count. We only need to look at the low 32 bits of these consecutive
102 // traces to ascertain this.
103 int i
= (index
- 1) & traceBufferMask
; // previous index
104 if ( (get(i
) & (CYCLE_COUNTER_HI
| CYCLE_COUNTER_LO
) != 0)
105 &&( ((get(k
) - get(i
)) & 0x7fffffff) == 1))
109 // The current index points to the low int and the next entry is
111 // extract the ~64bit cycle counter from the trace buffer.
113 *cycle
= get(k
) & 0x3fffffff;
114 *cycle
= (*cycle
<< 32) | ((get(j
) & 0x7fffffff) | (get(k
)
125 #define hexchar(a) std::hex << int(a) << std::dec
126 void Trace::print(unsigned index
, std::ostream
& ostr
,
127 DebugInterface
& dbgi
) const {
129 if (isCycleTrace(index
, &cycle
) == 2)
132 switch( type(index
) ) {
134 ostr
<< "empty trace cycle"<< std::endl
;
138 ostr
<< "Break: "<< (get(index
) & 0xffffff)<< std::endl
;
142 switch (get(index
) & 0xff) {
144 ostr
<< "Power-on reset"<< std::endl
;
148 ostr
<< "WDT reset"<< std::endl
;
152 ostr
<< "JTAG reset"<< std::endl
;
156 ostr
<< "External reset"<< std::endl
;
160 ostr
<< "Software initiated reset"<< std::endl
;
164 ostr
<< "Brown out detection reset"<< std::endl
;
168 ostr
<< "Simulation Reset"<< std::endl
;
172 ostr
<< "unknown reset"<< std::endl
;
178 if (type(index
-1) == OPCODE_WRITE
)
179 ostr
<< "wrote opcode: " << std::hex
<< (get(index
)&0xffff)
180 << "to pgm memory: "<< (get(index
- 1) & 0xffffff)
181 << std::dec
<< std::endl
;
186 byte reg
= ((get(index
)&0xffff00) >> 8);
187 ostr
<< "Registers write: address "
188 << dbgi
.registerName( reg
)
189 << " value = " << hexchar(get(index
)&0xff)
195 byte reg
= ((get(index
)&0xffff00) >> 8);
196 ostr
<< "Registers read: address "
197 << dbgi
.registerName( reg
)
198 << " value = " << hexchar(get(index
)&0xff)
204 dword addr
= (get(index
)&0xffffff);
205 ostr
<< "PC trace: address "
206 << std::hex
<< addr
<< std::dec
<< ": ";
207 dbgi
.trace( ostr
, addr
);
217 void Trace::printFrom(unsigned index
, std::ostream
& ostr
, Device
& dev
) const {
218 DebugInterface
*dbgi
= dev
.debugInterface();
219 while( index
< traceIndex
) {
220 print( index
, ostr
, *dbgi
);
227 void Trace::print(std::ostream
& ostr
, Device
& dev
) const {
228 DebugInterface
*dbgi
= dev
.debugInterface();
229 unsigned int i
= tbi(traceIndex
-2);
230 unsigned int k
= tbi(traceIndex
-1);
232 if( isCycleTrace(i
, 0) != 2 )
235 unsigned int frame_start
= tbi(traceIndex
-2);
236 unsigned int frame_end
= traceIndex
;
238 while( inRange(k
,frame_end
,frame_start
) ) {
239 print(k
, ostr
, *dbgi
);
246 void Trace::list(const char *filename
, std::ostream
& ostr
, Device
& dev
) {
247 std::ifstream
f( filename
, std::ios::in
| std::ios::binary
);
251 DebugInterface
*dbgi
= dev
.debugInterface();
252 traceIndex
= traceBufferSize
;
255 f
.read( (char*)traceBuffer
, traceBufferSize
* sizeof(unsigned int) );
257 traceIndex
= f
.gcount() / sizeof(unsigned int);
261 for(unsigned int index
= 0; index
< traceIndex
; ++index
)
262 print( index
, ostr
, *dbgi
);