home *** CD-ROM | disk | FTP | other *** search
/ messroms.de / 2007-01-13_www.messroms.de.zip / KAYPRO / IMAGES / MKIMAGE.ZIP / tools / mkimage.c next >
C/C++ Source or Header  |  1998-09-21  |  4KB  |  139 lines

  1. /*****************************************************************************
  2.  *
  3.  *     mkimage.c
  4.  *     Make CP/M image file(s) for different formats
  5.  *     This is intended to be used with (and uses) M.E.S.S.
  6.  *     Copyright (c) 1998 Juergen Buchmueller, all rights reserved.
  7.  *
  8.  *     - This source code is released as freeware for non-commercial purposes.
  9.  *     - You are free to use and redistribute this code in modified or
  10.  *       unmodified form, provided you list me in the credits.
  11.  *     - If you modify this source code, you must add a notice to each modified
  12.  *       source file that it has been changed.  If you're a nice person, you
  13.  *       will clearly mark each change too.  :)
  14.  *     - If you wish to use this for commercial purposes, please contact me at
  15.  *       pullmoll@t-online.de
  16.  *     - The author of this copywritten work reserves the right to change the
  17.  *     terms of its usage and license at any time, including retroactively
  18.  *   - This entire notice must remain in the source code.
  19.  *
  20.  *****************************************************************************/
  21.  
  22. #include <stdio.h>
  23.  
  24. typedef unsigned char    byte;
  25. typedef unsigned short    word;
  26.  
  27. typedef enum {
  28.     DEN_FM_LO = 0,
  29.     DEN_FM_HI,
  30.     DEN_MFM_LO,
  31.     DEN_MFM_HI
  32. } DENSITY;
  33.  
  34. typedef enum {
  35.     ORD_SIDES = 0,
  36.     ORD_CYLINDERS,
  37.     ORD_EAGLE
  38. } ORDER;
  39.  
  40. typedef struct {
  41.     word spt;                /* sectors per track        */
  42.     byte bsh;                /* block shift                */
  43.     byte blm;                /* block mask                */
  44.     byte exm;                /* extent mask                */
  45.     word dsm;                /* drive storage max sector */
  46.     word drm;                /* directory max sector     */
  47.     byte al0;                /* allocation bits low        */
  48.     byte al1;                /* allocation bits high     */
  49.     word cks;                /* check sectors (drm+1)/4 if media is removable */
  50.     word off;                /* offset (boot sectors)    */
  51. } cpm_dpb;
  52.  
  53. typedef struct {
  54.     char    *id;            /* short name */
  55.     char    *name;            /* long name */
  56.     char    *ref;            /* id reference */
  57.     DENSITY density;        /* fdd density */
  58.     word    cylinders;        /* number of cylinders */
  59.     byte    sides;            /* number of sides */
  60.     byte    spt;            /* sectors per track */
  61.     word    seclen;         /* sector length */
  62.     byte    skew;            /* sector skew */
  63.     byte    side1[32];        /* side number, sector numbers */
  64.     byte    side2[32];        /* side number, sector numbers */
  65.     ORDER    order;            /* sector ordering */
  66.     char    *label;         /* disk label */
  67.     cpm_dpb dpb;            /* associated dpb */
  68. } dsk_fmt;
  69.  
  70. #include "../src/machine/cpm_disk.c"
  71.  
  72. void usage(void)
  73. {
  74.     fprintf(stderr, "usage:\tmkimage [-option | format image.dsk]\n");
  75.     fprintf(stderr, "\toption can be one of the following\n");
  76.     fprintf(stderr, "-list\tlist available formats to stdout\n");
  77.     fprintf(stderr, "\totherwise you must specify a format and an image name\n");
  78. }
  79.  
  80. int main(int ac, char **av)
  81. {
  82. int i, track, side, sector;
  83. FILE *fd;
  84. char buff[4096];
  85.     if (ac < 2)
  86.     {
  87.         usage();
  88.         exit(1);
  89.     }
  90.     if (!stricmp(av[1], "-list"))
  91.     {
  92.         for (i = 0; formats[i].id; i++)
  93.             printf("%s\t%s\n", formats[i].id, formats[i].name);
  94.         return 0;
  95.     }
  96.     if (ac < 3)
  97.     {
  98.         usage();
  99.         exit(1);
  100.     }
  101.     for (i = 0; formats[i].id; i++)
  102.     {
  103.         if (!stricmp(formats[i].id, av[1]))
  104.             break;
  105.     }
  106.     if (!formats[i].id)
  107.     {
  108.         fprintf(stderr, "format '%s' not supported\n", av[1]);
  109.         exit(1);
  110.     }
  111.     fd = fopen(av[2], "wb");
  112.     if (!fd)
  113.     {
  114.         fprintf(stderr, "can't create image '%s'\n", av[2]);
  115.         exit(1);
  116.     }
  117.     for (track = 0; track < formats[i].cylinders; track++)
  118.     {
  119.         for (side = 0; side < formats[i].sides; side++)
  120.         {
  121.             for (sector = 0; sector < formats[i].spt; sector++)
  122.             {
  123.                 memset(buff, 0xe5, formats[i].seclen);
  124.                 sprintf(buff + 0*32 +1, "%-4.4s            TRACK     %3d", formats[i].id, track);
  125.                 sprintf(buff + 1*32 +1, "SIDE      %3d   HEAD ID   %3d", side, (side) ? formats[i].side2[0] : formats[i].side1[0]);
  126.                 sprintf(buff + 2*32 +1, "SECTOR    %3d   SECTOR ID %3d", sector, (side) ? formats[i].side2[sector+1] : formats[i].side1[sector+1]);
  127.                 if (fwrite(buff, 1, formats[i].seclen, fd) != formats[i].seclen)
  128.                 {
  129.                     fprintf(stderr, "error writing to '%s'\n", av[2]);
  130.                     exit(1);
  131.                 }
  132.             }
  133.         }
  134.     }
  135.     fclose(fd);
  136.     return 0;
  137. }
  138.  
  139.