showmem.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * "showmem.c" A C Norman, January 1996
  3. *
  4. * This program processes a "memory.use" log file created by CSL
  5. * in a variant compiled with MEMORY_TRACE defined. It displays the
  6. * memory use map and allows the user to zoom in using I and out using
  7. * O, move left, right, up or down. V redraws viewing just the box.
  8. * A "." input resets the image. The numbers displayed at the top left
  9. * are as for use with "-m" in a CSL command line to provoke an exception
  10. * when a memory reference in the box is first seen.
  11. *
  12. * This code uses the Watcom graphics library, so is DOS specific. Furthermore
  13. * it is coded assuming you have a monitor suitable for 1024*768 in
  14. * 256 colours.
  15. */
  16. /* Signature: 5c0b0eed 16-Jan-1996 */
  17. #include <conio.h>
  18. #include <graph.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. int main()
  23. {
  24. FILE *f = fopen("memory.use", "rb");
  25. int c, c1, c2, len, col, nrecords;
  26. int x, y, done;
  27. double x1, y1, x2, y2, w;
  28. double ox1, oy1, ox2, oy2;
  29. int sx, sy;
  30. unsigned i;
  31. int zeros = 0, ones=0, onebyte = 0, twobyte = 0, threebyte = 0;
  32. char buffer[100];
  33. double a00, a01, a02, a10, a11, a12;
  34. long int filepos;
  35. if (f == NULL) return 1;
  36. nrecords = getc(f) & 0xff;
  37. nrecords = nrecords + ((getc(f) & 0xff) << 8);
  38. nrecords = nrecords + ((getc(f) & 0xff) << 16);
  39. len = getc(f) & 0xff;
  40. len = len + ((getc(f) & 0xff) << 8);
  41. len = len + ((getc(f) & 0xff) << 16);
  42. a00 = 1020.0/(double)nrecords; a01 = 0.0; a02 = 2.0;
  43. a10 = 0.0; a11 = -760.0/(2.0*(double)len); a12 = 768.0-4.0;
  44. x1 = 0.0; y1 = 0.0; x2 = (double)nrecords; y2 = 2.0*(double)len;
  45. #define X(x,y) ((int)(a00*x+a01*y+a02))
  46. #define Y(x,y) ((int)(a10*x+a11*y+a12))
  47. filepos = ftell(f);
  48. _setvideomode(_XRES256COLOR); /* 1024 * 768 */
  49. repaint:
  50. _clearscreen(_GCLEARSCREEN);
  51. _setcolor(15);
  52. _settextposition(2, 2);
  53. sprintf(buffer, "%d:%d:%d ", 0x400*(int)x1, (int)y1, (int)y2);
  54. _outtext(buffer);
  55. _rectangle(_GBORDER, X(x1,y1), Y(x1,y1), X(x2,y2), Y(x2,y2));
  56. fseek(f, filepos, SEEK_SET);
  57. for (x=0; ; x++)
  58. { col = 0;
  59. c = kbhit();
  60. if (c != 0) break;
  61. for (;;)
  62. { c = getc(f);
  63. if (c == EOF) break;
  64. if ((c & 0x80) == 0)
  65. { if (c == 0) zeros++;
  66. else if (c == 1) ones++;
  67. onebyte++;
  68. col += c;
  69. }
  70. else if ((c & 0x40) == 0)
  71. { c1 = getc(f) & 0xff;
  72. if (c1 == 0)
  73. { if (c == 0x80) break; /* end of line code */
  74. col += 0x400000*(c & 0x3f);
  75. continue;
  76. }
  77. col += (c & 0x3f) + (c1 << 6);
  78. twobyte++;
  79. }
  80. else
  81. { c1 = getc(f) & 0xff;
  82. c2 = getc(f) & 0xff;
  83. c = (c & 0x3f) + (c1 << 6) + (c2 << 14);
  84. if (c < 0x40)
  85. { if (c == 0) c = 30;
  86. else if (c == 16) c = 31;
  87. _setcolor(c);
  88. continue;
  89. }
  90. col += c;
  91. threebyte++;
  92. }
  93. sx = X(x, col);
  94. if (sx >= 0 && sx < 1024)
  95. { sy = Y(x, col);
  96. if (sy >= 0 && sy < 768) _setpixel(X(x,col), Y(x,col));
  97. }
  98. }
  99. if (c == EOF) break;
  100. }
  101. done = 0;
  102. while (!done)
  103. { ox1 = x1, oy1 = y1;
  104. ox2 = x2, oy2 = y2;
  105. switch (tolower(getch()))
  106. {
  107. case 'q':
  108. done = 1;
  109. continue;
  110. case '.':
  111. x1 = 0.0; y1 = 0.0; x2 = (double)nrecords; y2 = 2.0*(double)len;
  112. if (a00 != 1020.0/(double)nrecords ||
  113. a01 != 0.0 ||
  114. a02 != 2.0 ||
  115. a10 != 0.0 ||
  116. a11 != -760.0/(2.0*(double)len) ||
  117. a12 != 768.0-4.0)
  118. { a00 = 1020.0/(double)nrecords; a01 = 0.0; a02 = 2.0;
  119. a10 = 0.0; a11 = -760.0/(2.0*(double)len); a12 = 768.0-4.0;
  120. goto repaint;
  121. }
  122. break;
  123. case 'u':
  124. w = (y2 - y1)/2.0;
  125. y1 += w;
  126. y2 += w;
  127. break;
  128. case 'd':
  129. w = (y2 - y1)/2.0;
  130. y1 -= w;
  131. y2 -= w;
  132. break;
  133. case 'l':
  134. w = (x2 - x1)/2.0;
  135. x1 -= w;
  136. x2 -= w;
  137. break;
  138. case 'r':
  139. w = (x2 - x1)/2.0;
  140. x1 += w;
  141. x2 += w;
  142. break;
  143. case 'i':
  144. w = (y2 - y1)/10.0;
  145. y1 += w;
  146. y2 -= w;
  147. w = (x2 - x1)/10.0;
  148. x1 += w;
  149. x2 -= w;
  150. break;
  151. case 'o':
  152. w = (y2 - y1)/10.0;
  153. y1 -= w;
  154. y2 += w;
  155. w = (x2 - x1)/10.0;
  156. x1 -= w;
  157. x2 += w;
  158. break;
  159. case 'n':
  160. w = (x2 - x1)/10.0;
  161. x1 += w;
  162. x2 -= w;
  163. break;
  164. case 'w':
  165. w = (x2 - x1)/10.0;
  166. x1 -= w;
  167. x2 += w;
  168. break;
  169. case 'v':
  170. a00 = -1018.0/(x1-x2);
  171. a10 = 0.0;
  172. a02 = 2.0*(510.0*x1 - x2)/(x1-x2);
  173. a10 = 0.0;
  174. a11 = 760.0/(y1-y2);
  175. a12 = 4.0*(y1 - 191.0*y2)/(y1-y2);
  176. goto repaint;
  177. }
  178. _setplotaction(_GXOR);
  179. _setcolor(15);
  180. _rectangle(_GBORDER, X(ox1,oy1), Y(ox1,oy1), X(ox2,oy2), Y(ox2,oy2));
  181. _settextposition(2, 2);
  182. sprintf(buffer, "%d:%d:%d ", 0x400*(int)x1, (int)y1, (int)y2);
  183. _outtext(buffer);
  184. _rectangle(_GBORDER, X(x1,y1), Y(x1,y1), X(x2,y2), Y(x2,y2));
  185. _setplotaction(_GPSET);
  186. }
  187. _setvideomode(_DEFAULTMODE);
  188. printf("len = %d bits, Max x was %d, nrecords = %d = %x\n",
  189. 2*len, x, nrecords, nrecords);
  190. printf("zeros=%d ones=%d onebyte=%d twobyte=%d threebyte=%d\n",
  191. zeros, ones, onebyte, twobyte, threebyte);
  192. return 0;
  193. }
  194. /* end of showmem.c */