showmem.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * "showmem.c" A C Norman, January 1996-2002
  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. /*
  17. * This code may be used and modified, and redistributed in binary
  18. * or source form, subject to the "CCL Public License", which should
  19. * accompany it. This license is a variant on the BSD license, and thus
  20. * permits use of code derived from this in either open and commercial
  21. * projects: but it does require that updates to this code be made
  22. * available back to the originators of the package.
  23. * Before merging other code in with this or linking this code
  24. * with other packages or libraries please check that the license terms
  25. * of the other material are compatible with those of this.
  26. */
  27. /* Signature: 40623905 08-Apr-2002 */
  28. #include <conio.h>
  29. #include <graph.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <ctype.h>
  33. int main()
  34. {
  35. FILE *f = fopen("memory.use", "rb");
  36. int c, c1, c2, len, col, nrecords;
  37. int x, y, done;
  38. double x1, y1, x2, y2, w;
  39. double ox1, oy1, ox2, oy2;
  40. int sx, sy;
  41. unsigned i;
  42. int zeros = 0, ones=0, onebyte = 0, twobyte = 0, threebyte = 0;
  43. char buffer[100];
  44. double a00, a01, a02, a10, a11, a12;
  45. long int filepos;
  46. if (f == NULL) return 1;
  47. nrecords = getc(f) & 0xff;
  48. nrecords = nrecords + ((getc(f) & 0xff) << 8);
  49. nrecords = nrecords + ((getc(f) & 0xff) << 16);
  50. len = getc(f) & 0xff;
  51. len = len + ((getc(f) & 0xff) << 8);
  52. len = len + ((getc(f) & 0xff) << 16);
  53. a00 = 1020.0/(double)nrecords; a01 = 0.0; a02 = 2.0;
  54. a10 = 0.0; a11 = -760.0/(2.0*(double)len); a12 = 768.0-4.0;
  55. x1 = 0.0; y1 = 0.0; x2 = (double)nrecords; y2 = 2.0*(double)len;
  56. #define X(x,y) ((int)(a00*x+a01*y+a02))
  57. #define Y(x,y) ((int)(a10*x+a11*y+a12))
  58. filepos = ftell(f);
  59. _setvideomode(_XRES256COLOR); /* 1024 * 768 */
  60. repaint:
  61. _clearscreen(_GCLEARSCREEN);
  62. _setcolor(15);
  63. _settextposition(2, 2);
  64. sprintf(buffer, "%d:%d:%d ", 0x400*(int)x1, (int)y1, (int)y2);
  65. _outtext(buffer);
  66. _rectangle(_GBORDER, X(x1,y1), Y(x1,y1), X(x2,y2), Y(x2,y2));
  67. fseek(f, filepos, SEEK_SET);
  68. for (x=0; ; x++)
  69. { col = 0;
  70. c = kbhit();
  71. if (c != 0) break;
  72. for (;;)
  73. { c = getc(f);
  74. if (c == EOF) break;
  75. if ((c & 0x80) == 0)
  76. { if (c == 0) zeros++;
  77. else if (c == 1) ones++;
  78. onebyte++;
  79. col += c;
  80. }
  81. else if ((c & 0x40) == 0)
  82. { c1 = getc(f) & 0xff;
  83. if (c1 == 0)
  84. { if (c == 0x80) break; /* end of line code */
  85. col += 0x400000*(c & 0x3f);
  86. continue;
  87. }
  88. col += (c & 0x3f) + (c1 << 6);
  89. twobyte++;
  90. }
  91. else
  92. { c1 = getc(f) & 0xff;
  93. c2 = getc(f) & 0xff;
  94. c = (c & 0x3f) + (c1 << 6) + (c2 << 14);
  95. if (c < 0x40)
  96. { if (c == 0) c = 30;
  97. else if (c == 16) c = 31;
  98. _setcolor(c);
  99. continue;
  100. }
  101. col += c;
  102. threebyte++;
  103. }
  104. sx = X(x, col);
  105. if (sx >= 0 && sx < 1024)
  106. { sy = Y(x, col);
  107. if (sy >= 0 && sy < 768) _setpixel(X(x,col), Y(x,col));
  108. }
  109. }
  110. if (c == EOF) break;
  111. }
  112. done = 0;
  113. while (!done)
  114. { ox1 = x1, oy1 = y1;
  115. ox2 = x2, oy2 = y2;
  116. switch (tolower(getch()))
  117. {
  118. case 'q':
  119. done = 1;
  120. continue;
  121. case '.':
  122. x1 = 0.0; y1 = 0.0; x2 = (double)nrecords; y2 = 2.0*(double)len;
  123. if (a00 != 1020.0/(double)nrecords ||
  124. a01 != 0.0 ||
  125. a02 != 2.0 ||
  126. a10 != 0.0 ||
  127. a11 != -760.0/(2.0*(double)len) ||
  128. a12 != 768.0-4.0)
  129. { a00 = 1020.0/(double)nrecords; a01 = 0.0; a02 = 2.0;
  130. a10 = 0.0; a11 = -760.0/(2.0*(double)len); a12 = 768.0-4.0;
  131. goto repaint;
  132. }
  133. break;
  134. case 'u':
  135. w = (y2 - y1)/2.0;
  136. y1 += w;
  137. y2 += w;
  138. break;
  139. case 'd':
  140. w = (y2 - y1)/2.0;
  141. y1 -= w;
  142. y2 -= w;
  143. break;
  144. case 'l':
  145. w = (x2 - x1)/2.0;
  146. x1 -= w;
  147. x2 -= w;
  148. break;
  149. case 'r':
  150. w = (x2 - x1)/2.0;
  151. x1 += w;
  152. x2 += w;
  153. break;
  154. case 'i':
  155. w = (y2 - y1)/10.0;
  156. y1 += w;
  157. y2 -= w;
  158. w = (x2 - x1)/10.0;
  159. x1 += w;
  160. x2 -= w;
  161. break;
  162. case 'o':
  163. w = (y2 - y1)/10.0;
  164. y1 -= w;
  165. y2 += w;
  166. w = (x2 - x1)/10.0;
  167. x1 -= w;
  168. x2 += w;
  169. break;
  170. case 'n':
  171. w = (x2 - x1)/10.0;
  172. x1 += w;
  173. x2 -= w;
  174. break;
  175. case 'w':
  176. w = (x2 - x1)/10.0;
  177. x1 -= w;
  178. x2 += w;
  179. break;
  180. case 'v':
  181. a00 = -1018.0/(x1-x2);
  182. a10 = 0.0;
  183. a02 = 2.0*(510.0*x1 - x2)/(x1-x2);
  184. a10 = 0.0;
  185. a11 = 760.0/(y1-y2);
  186. a12 = 4.0*(y1 - 191.0*y2)/(y1-y2);
  187. goto repaint;
  188. }
  189. _setplotaction(_GXOR);
  190. _setcolor(15);
  191. _rectangle(_GBORDER, X(ox1,oy1), Y(ox1,oy1), X(ox2,oy2), Y(ox2,oy2));
  192. _settextposition(2, 2);
  193. sprintf(buffer, "%d:%d:%d ", 0x400*(int)x1, (int)y1, (int)y2);
  194. _outtext(buffer);
  195. _rectangle(_GBORDER, X(x1,y1), Y(x1,y1), X(x2,y2), Y(x2,y2));
  196. _setplotaction(_GPSET);
  197. }
  198. _setvideomode(_DEFAULTMODE);
  199. printf("len = %d bits, Max x was %d, nrecords = %d = %x\n",
  200. 2*len, x, nrecords, nrecords);
  201. printf("zeros=%d ones=%d onebyte=%d twobyte=%d threebyte=%d\n",
  202. zeros, ones, onebyte, twobyte, threebyte);
  203. return 0;
  204. }
  205. /* end of showmem.c */