nascom.c 3.3 KB

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