File: display-utils.c

package info (click to toggle)
spim 8.0%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,736 kB
  • sloc: asm: 8,560; ansic: 8,437; yacc: 2,298; makefile: 1,107; lex: 706; sh: 219
file content (288 lines) | stat: -rw-r--r-- 8,032 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* SPIM S20 MIPS simulator.
   Utilities for displaying machine contents.

   Copyright (c) 1990-2010, James R. Larus.
   All rights reserved.

   Redistribution and use in source and binary forms, with or without modification,
   are permitted provided that the following conditions are met:

   Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

   Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation and/or
   other materials provided with the distribution.

   Neither the name of the James R. Larus nor the names of its contributors may be
   used to endorse or promote products derived from this software without specific
   prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "spim.h"
#include "string-stream.h"
#include "spim-utils.h"
#include "inst.h"
#include "data.h"
#include "reg.h"
#include "mem.h"
#include "run.h"
#include "sym-tbl.h"


static mem_addr format_partial_line (str_stream *ss, mem_addr addr);


/* Write to the stream the contents of the machine's registers, in a wide
   variety of formats. */

void
format_registers (str_stream *ss, int print_gpr_hex, int print_fpr_hex)
{
  int i;
  char *grstr, *fpstr;
  char *grfill, *fpfill;
  static char *reg_names[] =
    {"r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
     "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
     "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
     "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"};

  ss_printf (ss, " PC      = %08x   ", PC);
  ss_printf (ss, "EPC     = %08x  ", CP0_EPC);
  ss_printf (ss, " Cause   = %08x  ", CP0_Cause);
  ss_printf (ss, " BadVAddr= %08x\n", CP0_BadVAddr);
  ss_printf (ss, " Status  = %08x   ", CP0_Status);
  ss_printf (ss, "HI      = %08x  ", HI);
  ss_printf (ss, " LO      = %08x\n", LO);

  if (print_gpr_hex)
    grstr = "R%-2d (%2s) = %08x", grfill = "  ";
  else
    grstr = "R%-2d (%2s) = %-10d", grfill = " ";

  ss_printf (ss, "\t\t\t\t General Registers\n");
  for (i = 0; i < 8; i++)
    {
      ss_printf (ss, grstr, i, reg_names[i], R[i]);
      ss_printf (ss, grfill);
      ss_printf (ss, grstr, i+8, reg_names[i+8], R[i+8]);
      ss_printf (ss, grfill);
      ss_printf (ss, grstr, i+16, reg_names[i+16], R[i+16]);
      ss_printf (ss, grfill);
      ss_printf (ss, grstr, i+24, reg_names[i+24], R[i+24]);
      ss_printf (ss, "\n");
    }

  ss_printf (ss, "\n FIR    = %08x   ", FIR);
  ss_printf (ss, " FCSR    = %08x   ", FCSR);
  ss_printf (ss, " FCCR   = %08x  ", FCCR);
  ss_printf (ss, " FEXR    = %08x\n", FEXR);
  ss_printf (ss, " FENR   = %08x\n", FENR);

  ss_printf (ss, "\t\t\t      Double Floating Point Registers\n");

  if (print_fpr_hex)
    fpstr = "FP%-2d=%08x,%08x", fpfill = " ";
  else
    fpstr = "FP%-2d = %#-13.6g", fpfill = " ";

  if (print_fpr_hex)
    for (i = 0; i < 4; i += 1)
      {
	int *r1, *r2;

	/* Use pointers to cast to ints without invoking float->int conversion
	   so we can just print the bits. */
	r1 = (int *)&FPR[i]; r2 = r1 + 1;
	ss_printf (ss, fpstr, 2*i, *r1, *r2);
	ss_printf (ss, fpfill);

	r1 = (int *)&FPR[i+4]; r2 = r1 + 1;
	ss_printf (ss, fpstr, 2*i+8, *r1, *r2);
	ss_printf (ss, fpfill);

	r1 = (int *)&FPR[i+8]; r2 = r1 + 1;
	ss_printf (ss, fpstr, 2*i+16, *r1, *r2);
	ss_printf (ss, fpfill);

	r1 = (int *)&FPR[i+12]; r2 = r1 + 1;
	ss_printf (ss, fpstr, 2*i+24, *r1, *r2);
	ss_printf (ss, "\n");
      }
  else for (i = 0; i < 4; i += 1)
    {
      ss_printf (ss, fpstr, 2*i, FPR[i]);
      ss_printf (ss, fpfill);
      ss_printf (ss, fpstr, 2*i+8, FPR[i+4]);
      ss_printf (ss, fpfill);
      ss_printf (ss, fpstr, 2*i+16, FPR[i+8]);
      ss_printf (ss, fpfill);
      ss_printf (ss, fpstr, 2*i+24, FPR[i+12]);
      ss_printf (ss, "\n");
    }

  if (print_fpr_hex)
    fpstr = "FP%-2d=%08x", fpfill = " ";
  else
    fpstr = "FP%-2d = %#-13.6g", fpfill = " ";

  ss_printf (ss, "\t\t\t      Single Floating Point Registers\n");

  if (print_fpr_hex)
    for (i = 0; i < 8; i += 1)
      {
	/* Use pointers to cast to ints without invoking float->int conversion
	   so we can just print the bits. */
	ss_printf (ss, fpstr, i, *(int *)&FPR_S(i));
	ss_printf (ss, fpfill);

	ss_printf (ss, fpstr, i+8, *(int *)&FPR_S(i+8));
	ss_printf (ss, fpfill);

	ss_printf (ss, fpstr, i+16, *(int *)&FPR_S(i+16));
	ss_printf (ss, fpfill);

	ss_printf (ss, fpstr, i+24, *(int *)&FPR_S(i+24));
	ss_printf (ss, "\n");
      }
  else for (i = 0; i < 8; i += 1)
    {
      ss_printf (ss, fpstr, i, FPR_S(i));
      ss_printf (ss, fpfill);
      ss_printf (ss, fpstr, i+8, FPR_S(i+8));
      ss_printf (ss, fpfill);
      ss_printf (ss, fpstr, i+16, FPR_S(i+16));
      ss_printf (ss, fpfill);
      ss_printf (ss, fpstr, i+24, FPR_S(i+24));
      ss_printf (ss, "\n");
    }
}



/* Write to the stream a printable representation of the instructions in
   memory addresses: FROM...TO. */

void
format_insts (str_stream *ss, mem_addr from, mem_addr to)
{
  instruction *inst;
  mem_addr i;

  for (i = from; i < to; i += 4)
    {
      inst = read_mem_inst (i);
      if (inst != NULL)
	{
	  format_an_inst (ss, inst, i);
	}
    }
}


/* Write to the stream a printable representation of the data and stack
   segments. */

void
format_data_segs (str_stream *ss)
{
  ss_printf (ss, "\tDATA\n");
  format_mem (ss, DATA_BOT, data_top);

  ss_printf (ss, "\n\tSTACK\n");
  format_mem (ss, ROUND_DOWN (R[29], BYTES_PER_WORD), STACK_TOP - 4096);

  ss_printf (ss, "\n\tKERNEL DATA\n");
  format_mem (ss, K_DATA_BOT, k_data_top);
}


#define BYTES_PER_LINE (4*BYTES_PER_WORD)


/* Write to the stream a printable representation of the data in memory
   address: FROM...TO. */

void
format_mem (str_stream *ss, mem_addr from, mem_addr to)
{
  mem_word val;
  mem_addr i = ROUND_UP (from, BYTES_PER_WORD);
  int j;

  i = format_partial_line (ss, i);

  for ( ; i < to; )
    {
      /* Count consecutive zero words */
      for (j = 0; (i + (uint32) j * BYTES_PER_WORD) < to; j += 1)
	{
	  val = read_mem_word (i + (uint32) j * BYTES_PER_WORD);
	  if (val != 0)
	    {
	      break;
	    }
	}

      if (j >= 4)
	{
	  /* Block of 4 or more zero memory words: */
	  ss_printf (ss, "[0x%08x]...[0x%08x]	0x00000000\n",
		     i,
		     i + (uint32) j * BYTES_PER_WORD);

	  i = i + (uint32) j * BYTES_PER_WORD;
	  i = format_partial_line (ss, i);
	}
      else
	{
	  /* Fewer than 4 zero words, print them on a single line: */
	  ss_printf (ss, "[0x%08x]		      ", i);
	  do
	    {
	      val = read_mem_word (i);
	      ss_printf (ss, "  0x%08x", (unsigned int)val);
	      i += BYTES_PER_WORD;
	    }
	  while (i % BYTES_PER_LINE != 0);

	  ss_printf (ss, "\n");
	}
    }
}


/* Write to the stream a text line containing a fraction of a
   quadword. Return the address after the last one written.  */

static mem_addr
format_partial_line (str_stream *ss, mem_addr addr)
{
  if ((addr % BYTES_PER_LINE) != 0)
    {
      ss_printf (ss, "[0x%08x]		      ", addr);

      for (; (addr % BYTES_PER_LINE) != 0; addr += BYTES_PER_WORD)
	{
	  mem_word val = read_mem_word (addr);
	  ss_printf (ss, "  0x%08x", (unsigned int)val);
	}

      ss_printf (ss, "\n");
    }

  return addr;
}