nascom.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * Download Z80 load module into the NASCOM
  8. *
  9. * Johan Stevenson, Vrije Universiteit, Amsterdam
  10. * Adapted (untested) to new ack.out format by
  11. * Ceriel Jacobs, Vrije Universiteit, Amsterdam
  12. */
  13. #include <stdio.h>
  14. #include <assert.h>
  15. #include <sgtty.h>
  16. #include <signal.h>
  17. #include <out.h>
  18. int check;
  19. int nascom = 1;
  20. int nl = '\037';
  21. int zero = 0;
  22. int disp = 0;
  23. char hex[] = "0123456789ABCDEF";
  24. char *progname;
  25. struct sgttyb ttynormal;
  26. struct sgttyb ttyraw;
  27. int rawmode = 0;
  28. struct outhead ohead;
  29. struct outsect sect[MAXSECT];
  30. stop(code) {
  31. if (rawmode)
  32. stty(1, &ttynormal);
  33. exit(code);
  34. }
  35. main(argc,argv) char **argv; {
  36. register unsigned nd;
  37. long pc;
  38. register char *s;
  39. int i;
  40. progname = argv[0];
  41. while (argc > 1 && argv[1][0] == '-') {
  42. switch (argv[1][1]) {
  43. case 'u':
  44. /* unix output */
  45. nascom = 0;
  46. nl = '\n';
  47. break;
  48. case 'e':
  49. /* fill EPROM. make minimal change */
  50. zero = 0xFF;
  51. break;
  52. case 'd':
  53. /* displacement at load time */
  54. disp = atoi(&argv[1][2]);
  55. break;
  56. }
  57. argc--;
  58. argv++;
  59. }
  60. s = "a.out";
  61. if (argc == 2)
  62. s = argv[1];
  63. else if (argc != 1) {
  64. fprintf(stderr,"usage: %s [flags] [object file]\n",progname);
  65. stop(-1);
  66. }
  67. if (! rd_open(s)) {
  68. fprintf(stderr,"%s: can't open %s\n",progname,s);
  69. stop(-1);
  70. }
  71. if (nascom) {
  72. signal(SIGHUP, SIG_IGN);
  73. signal(SIGINT, SIG_IGN);
  74. signal(SIGQUIT, stop);
  75. signal(SIGTERM, stop);
  76. if (gtty(1, &ttynormal) < 0) {
  77. fprintf(stderr, "no tty\n");
  78. stop(-1);
  79. }
  80. rawmode++;
  81. ttyraw = ttynormal;
  82. ttyraw.sg_flags |= RAW;
  83. ttyraw.sg_ispeed = B1200;
  84. ttyraw.sg_ospeed = B1200;
  85. stty(1, &ttyraw);
  86. sleep(5);
  87. }
  88. rd_ohead(&ohead);
  89. if (ohead.oh_flags & HF_LINK) {
  90. fprintf(stderr,"%s: %s contains unresolved references\n",progname,s);
  91. stop(-1);
  92. }
  93. rd_sect(sect, ohead.oh_nsect);
  94. for (i = 0; i < ohead.oh_nsect; i++) {
  95. rd_outsect(i);
  96. pc = sect[i].os_base;
  97. while (sect[i].os_size) {
  98. unsigned int sz = 8096, fl;
  99. extern char *calloc();
  100. register char *buf;
  101. char *pbuf;
  102. if (sz > sect[i].os_size) sz = sect[i].os_size;
  103. sect[i].os_size -= sz;
  104. pbuf = buf = calloc(sz, 1);
  105. if (fl = sect[i].os_flen) {
  106. if (fl > sz) fl = sz;
  107. sect[i].os_flen -= fl;
  108. rd_emit(buf, (long) fl);
  109. }
  110. while (sz >= 8) {
  111. data(8, (int) pc, buf);
  112. sz -= 8;
  113. buf += 8;
  114. pc += 8;
  115. }
  116. if (sz > 0) {
  117. data(sz, (int) pc, buf);
  118. }
  119. free(pbuf);
  120. }
  121. }
  122. putchar('.');
  123. putchar(nl);
  124. if (nascom)
  125. sleep(5);
  126. stop(0);
  127. }
  128. data(nd,pc,buf)
  129. register char *buf;
  130. int pc;
  131. {
  132. register i;
  133. check = 0;
  134. pc += disp;
  135. byte(pc>>8);
  136. byte(pc);
  137. for (i = 0; i < nd; i++) {
  138. putchar(' ');
  139. byte(*buf++);
  140. }
  141. while (i < 8) {
  142. putchar(' ');
  143. byte(zero);
  144. i++;
  145. }
  146. putchar(' ');
  147. byte(check);
  148. putchar(nl);
  149. }
  150. byte(b) {
  151. check += b;
  152. putchar(hex[(b>>4) & 017]);
  153. putchar(hex[b & 017]);
  154. }
  155. rd_fatal()
  156. {
  157. fprintf(stderr, "%s: Read error\n", progname);
  158. stop(-1);
  159. }