home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1810 < prev    next >
Encoding:
Text File  |  1990-12-28  |  3.7 KB  |  131 lines

  1. Newsgroups: alt.sources
  2. From: Mike.Blackwell@ROVER.RI.CMU.EDU
  3. Subject: [comp.sys.m68k.pc] S-record generator
  4. Message-ID: <1990Sep11.173917.12127@math.lsa.umich.edu>
  5. Date: Tue, 11 Sep 90 17:39:17 GMT
  6.  
  7. Archive-name: srec/10-Sep-90
  8. Original-posting-by: Mike.Blackwell@ROVER.RI.CMU.EDU
  9. Original-subject: S-record generator
  10. Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
  11.  
  12. [Reposted from comp.sys.m68k.pc.
  13. Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti).]
  14.  
  15. Here's a program I whipped up a while ago which let me compile and link
  16. 68030 programs on a Sun-3, then download them to a stand alone 68030
  17. system in S-record format. These days, you should be using something
  18. like VxWorks instead, it will make your life much simpler... Should be
  19. enough to get you started, though.
  20.  
  21.         Mike Blackwell, Robotics Institute, Carnegie Mellon
  22.  
  23. ---------- cut here ----------
  24.  
  25. /************************************************************************/
  26. /*                                    */
  27. /* srec.c  --  Convert a.out files to Motorola S-record format.        */
  28. /*                                    */
  29. /* Link code modules with something like:                */
  30. /* test.run: $(OBJS)                            */
  31. /*           ld -N -e _Start -s -T 8000 $(OBJS) -o test.i        */
  32. /*           srec test.i > test.run                    */
  33. /*                                    */
  34. /* HISTORY                                */
  35. /*                                    */
  36. /* 10-Jun-88  Mike Blackwell (mkb) at Carnegie Mellon University    */
  37. /*    Created.                            */
  38. /*                                    */
  39. /************************************************************************/
  40.  
  41. #include <stdio.h>
  42. #include <a.out.h>
  43.  
  44. #define TEXTPOS(x)    sizeof(x)
  45. #define DATAPOS(x)    TEXTPOS(x) + x.a_text
  46.  
  47. main(argc, argv)
  48. int argc;
  49. char *argv[];
  50. {
  51.     long Taddr, Daddr;            /* Starting addrs of text and data */
  52.     long Tsize, Dsize;            /* Size of text and data segs */
  53.     struct exec filhdr;
  54.     FILE *afp;                /* File descriptor of a.out file */
  55.  
  56.     if (argc == 2) {
  57.     if ((afp = fopen (argv[1], "r")) == NULL) {
  58.         fprintf(stderr, "Can't open input file %s\n", argv[1]);
  59.         exit(1);
  60.     }
  61.     } else {
  62.     fprintf(stderr, "Usage: %s file\n", argv[0]);
  63.     exit(1);
  64.     }
  65.  
  66.     /* Get the header information. */
  67.     if (fread (&filhdr, sizeof(struct exec), 1, afp) != 1) {
  68.     fprintf(stderr, "Bad format in input file %s\n", argv[1]);
  69.     exit(1);
  70.     }
  71.  
  72.     /* Check  the magic number. */
  73.     fprintf(stderr, "Machine type = %d\n", filhdr.a_machtype);
  74.     fprintf(stderr, "Magic number = 0%o\n", filhdr.a_magic);
  75.     if (filhdr.a_magic != OMAGIC) {
  76.     fprintf(stderr, "Bad magic number (0%o) in input file %s\n", filhdr.a_magic, argv[1]);
  77.     exit(1);
  78.     }
  79.  
  80.     /* Record the base loading address. */
  81.     Taddr = filhdr.a_entry;
  82.     Tsize = filhdr.a_text;
  83.     Daddr = Taddr + Tsize;
  84.     Dsize = filhdr.a_data;
  85.  
  86.     /* Output the records. */
  87.     write_srec("S0", 0, 0, 0);
  88.     outbin(afp, Taddr, Tsize);            /* Text segment */
  89.     fseek(afp, DATAPOS(filhdr), 0);        /* Seek to data */
  90.     outbin(afp, Daddr, Dsize);            /* Data segment */
  91.     write_srec("S9", Taddr, 0, 0);
  92.  
  93.     fprintf(stderr, "Text segment = %06x, Size = %x\n", Taddr, Tsize);
  94.     fprintf(stderr, "Data segment = %06x, Size = %x\n", Daddr, Dsize);
  95.     fprintf(stderr, "Start address = %06x\n", Taddr);
  96. }
  97.  
  98.  
  99. /* Procedure that puts out records for a particular segment. */
  100. outbin(afp, addr, size)
  101. FILE *afp;
  102. long addr;
  103. int size;
  104. {
  105.     int i, n;
  106.     char data[16];
  107.  
  108.     for (i = 0; i < size; i += 16) {
  109.     n = ((i + 16) <= size) ? 16 : size - i;
  110.     fread(data, 1, n, afp);
  111.     write_srec("S2", addr + i, n, data);
  112.     }
  113. }
  114.  
  115.  
  116. write_srec(stype, addr, len, data)
  117. char *stype, *data;
  118. int addr, len;
  119. {
  120.     int chk, i;
  121.  
  122.     printf("%2s%02X%06X", stype, len + 4, addr);
  123.     chk = (len + 4);
  124.     chk += ((addr >> 16) & 0xFF) + ((addr >> 8) & 0xFF) + (addr & 0xFF);
  125.     for (i = 0; i < len; i++) {
  126.     printf("%02X", data[i] & 0xFF);
  127.     chk += data[i] & 0xFF;
  128.     }
  129.     printf("%02X\n", ~chk & 0xFF);
  130. }
  131.