mccpm.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* $Header$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /*
  7. * dit programma leest een a.out file
  8. * voor een kleine uP (adres space = 64K)
  9. * en levert aan de standaard output een
  10. * download formaat voor de MCCPM computer.
  11. *
  12. */
  13. #define MAXBYTE 24
  14. #include <stdio.h>
  15. #include <out.h>
  16. char hex[] = "0123456789ABCDEF";
  17. char **s;
  18. int bytes, bytcnt, checksum;
  19. unsigned pc;
  20. unsigned offset;
  21. unsigned htou();
  22. main (argc,argv)
  23. int argc;
  24. char *argv[];
  25. {
  26. if (argc > 3)
  27. fatal ("usage: %s filename [start-adres]\n",argv[0]);
  28. offset = 0;
  29. if (argc == 3)
  30. if (!(offset = htou(argv[2])))
  31. fatal ("adres error %s\n", argv[2]);
  32. if (! rd_open(*++argv)) fatal ("can't open %s\n",*argv);
  33. else {
  34. s = argv;
  35. convert ();
  36. }
  37. }
  38. convert ()
  39. {
  40. struct outhead head;
  41. struct outsect sect[MAXSECT];
  42. int i;
  43. rd_ohead(&head);
  44. if (head.oh_flags & HF_LINK) {
  45. fatal("%s contains unresolved references\n",s);
  46. }
  47. rd_sect(sect, head.oh_nsect);
  48. for (i = 0; i < head.oh_nsect; i++) {
  49. rd_outsect(i);
  50. pc = sect[i].os_base - offset;
  51. while (sect[i].os_size) {
  52. unsigned int sz = 8096, fl;
  53. extern char *calloc();
  54. register char *buf;
  55. char *pbuf;
  56. if (sz > sect[i].os_size) sz = sect[i].os_size;
  57. sect[i].os_size -= sz;
  58. pbuf = buf = calloc(sz, 1);
  59. if (fl = sect[i].os_flen) {
  60. if (fl > sz) fl = sz;
  61. sect[i].os_flen -= fl;
  62. rd_emit(buf, (long) fl);
  63. }
  64. while (sz >= MAXBYTE) {
  65. data(MAXBYTE, (int) pc, buf);
  66. checksum = 0;
  67. sz -= MAXBYTE;
  68. buf += MAXBYTE;
  69. pc += MAXBYTE;
  70. }
  71. if (sz > 0) {
  72. data(sz, (int) pc, buf);
  73. checksum = 0;
  74. }
  75. free(pbuf);
  76. }
  77. }
  78. printf (":00000001FF\n");
  79. }
  80. data (sz, pc, buf)
  81. register char *buf;
  82. {
  83. printf (":");
  84. outbyte (sz);
  85. bytcnt += 4;
  86. outbyte (pc >> 8);
  87. outbyte (pc);
  88. outbyte (0);
  89. while (sz != 0)
  90. {
  91. outbyte (*buf++);
  92. sz--;
  93. }
  94. outbyte (-checksum);
  95. putchar ('\n');
  96. putchar (0);
  97. putchar (0);
  98. }
  99. outbyte (b)
  100. int b;
  101. {
  102. checksum = (checksum + b) & 0xFF;
  103. putchar (hex[(b>>4) & 0xF]);
  104. putchar (hex[b & 0xF]);
  105. }
  106. fatal (s,a)
  107. char *s, *a;
  108. {
  109. fprintf (stderr,s,a);
  110. exit (-1);
  111. }
  112. rd_fatal()
  113. {
  114. fatal("Read error\n");
  115. }
  116. /* convert a string of hex digits to an unsigned 16 bit number */
  117. unsigned htou(t)
  118. char *t;
  119. {
  120. unsigned n = 0;
  121. char c;
  122. while(c = *t++){
  123. if(c >= '0' && c <= '9')
  124. c -= '0';
  125. else if(c >= 'a' && c <= 'f')
  126. c -= 'a' - 10;
  127. else if(c >= 'A' && c <= 'F')
  128. c -= 'A' - 10;
  129. else
  130. return(0);
  131. n = n * 16 + c;
  132. }
  133. return(n);
  134. }