home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_04 / v7n4033b.txt < prev    next >
Text File  |  1989-01-14  |  2KB  |  56 lines

  1. /*                        Listing 3 -- EXAMPLE.C                            */
  2. /*      Sample Program to Demonstrate the Use of the Function Package       */
  3.  
  4. #include "hexio.h"
  5.  
  6. #define CODE_START   0x0080     /*  Start address of code space.            */
  7. #define CODE_END     0x08AF     /*  End address of code space.              */
  8. #define VECT_START   0x1FF5     /*  Start address of interrupt vectors.     */
  9. #define VECT_END     0x1FFF     /*  End address of interrupt vectors.       */
  10.  
  11. #define SWI          0x83       /*  The 6805's SWI opcode byte.             */
  12.  
  13. static void crash(msg)
  14. char *msg;
  15. {
  16.     fprintf(stderr,"Error -- %s.\n",msg);  exit(1);
  17. }
  18.  
  19. int main()
  20. {
  21.     int byte;
  22.     unsigned addr;
  23.     unsigned char *bptr, sum;
  24.     HFILE *hfile;
  25.     static unsigned char buf[VECT_END + 1];
  26.  
  27.     /*  First, fill the active areas of the buffer with SWI instructions.   */
  28.     for (bptr = &buf[CODE_START]; bptr <= &buf[CODE_END]; *bptr++ = SWI);
  29.     for (bptr = &buf[VECT_START]; bptr <= &buf[VECT_END]; *bptr++ = SWI);
  30.  
  31.     /*  Next, read the raw program's S-record file into the buffer.         */
  32.     if (!(hfile = hopen("rawprog.shx","rs"))) crash("rawprog.shx not found");
  33.     while ((byte = hgetc(hfile)) != HEOF) {
  34.         if ((addr = htell(hfile)) > VECT_END) crash("address > 0x1FFF found");
  35.     buf[addr] = byte;
  36.     }
  37.     if (hclose(hfile)) crash("read error on rawprog.shx");
  38.  
  39.     /*  Now, adjust the checksum of the active areas of the buffer to 0x00. */
  40.     sum = 0x00;
  41.     for (bptr = &buf[CODE_START]; bptr <= &buf[CODE_END]; sum += *bptr++);
  42.     for (bptr = &buf[VECT_START]; bptr <= &buf[VECT_END]; sum += *bptr++);
  43.     buf[CODE_END] -= sum;
  44.  
  45.     /*  Finally, write the active areas of the buffer to an Intel hex file. */
  46.     if (!(hfile = hopen("fixprog.hex","wi"))) crash("disk or directory full");
  47.     hseek(hfile,CODE_START);
  48.     for (bptr = &buf[CODE_START]; bptr <= &buf[CODE_END];
  49.         hputc(*bptr++,hfile));
  50.     hseek(hfile,VECT_START);
  51.     for (bptr = &buf[VECT_START]; bptr <= &buf[VECT_END];
  52.         hputc(*bptr++,hfile));
  53.     if (hclose(hfile)) crash("disk or directory full");
  54.  
  55.     return 0;
  56. }