Annotation of hatari/src/createBlankImage.c, revision 1.1.1.2

1.1       root        1: /*
                      2:   Hatari
                      3: 
                      4:   Create Blank .ST/.MSA Disc Images
                      5: */
                      6: 
                      7: #include "main.h"
                      8: #include "dialog.h"
                      9: #include "file.h"
                     10: #include "floppy.h"
                     11: #include "memAlloc.h"
                     12: #include "misc.h"
                     13: #include "msa.h"
                     14: #include "st.h"
                     15: 
                     16: 
                     17: /*-----------------------------------------------------------------------*/
                     18: /*
                     19:            40 track SS   40 track DS   80 track SS   80 track DS
                     20:  0- 1   Branch instruction to boot program if executable
                     21:  2- 7   'Loader'
                     22:  8-10   24-bit serial number
                     23: 11-12   BPS    512           512           512           512
                     24: 13      SPC     1             2             2             2
                     25: 14-15   RES     1             1             1             1
                     26: 16      FAT     2             2             2             2
                     27: 17-18   DIR     64           112           112           112
                     28: 19-20   SEC    360           720           720          1440
                     29: 21      MEDIA  252           253           248           249 (isn't used by ST-BIOS)
                     30: 22-23   SPF     2             2             5             5
                     31: 24-25   SPT     9             9             9             9
                     32: 26-27   SIDE    1             2             1             2
                     33: 28-29   HID     0             0             0             0
                     34: 510-511 CHECKSUM
                     35: */
                     36: 
                     37: typedef struct {
                     38:   int nTracks,nSectors,nSides;
                     39: } STANDARDDISCSIZES;
                     40: 
                     41: #define DEFAULT_STANDARDDISC  1
                     42: #define MAX_STANDARDDISCSIZES  2
                     43: static STANDARDDISCSIZES StandardDiscSizes[MAX_STANDARDDISCSIZES] = {
                     44:   80,9,1,  /* 80 tracks, single sided (360k) */
                     45:   80,9,2   /* 80 tracks, double sided (720k) */
                     46: };
                     47: static char *pszStandardDiscNames[] = {
                     48:   "80 tracks, single sided (360k)",
                     49:   "80 tracks, double sided (720k)",
1.1.1.2 ! root       50:   NULL  /* term */
1.1       root       51: };
                     52: 
                     53: #define NUM_TRACKSTEXTS    3
                     54: #define NUM_SECTORSTEXTS  3
                     55: #define NUM_SIDESTEXTS    2
                     56: static char *pszTracksText[] = {
                     57:   "80","81","82",NULL
                     58: };
                     59: static char *pszSectorsText[] = {
                     60:   "9","10","11",NULL
                     61: };
                     62: static char *pszSidesText[] = {
                     63:   "1","2",NULL
                     64: };
                     65: 
                     66: /* FIXME
                     67: static int Custom_DialogItems[] = {
                     68:   IDC_STATICTRACKS,
                     69:   IDC_STATICSECTORS,
                     70:   IDC_STATICSIDES,
                     71:   IDC_EDITTRACKS,
                     72:   IDC_EDITSECTORS,
                     73:   IDC_EDITSIDES,
                     74:   IDC_SPINTRACKS,
                     75:   IDC_SPINSECTORS,
                     76:   IDC_SPINSIDES,
                     77:   IDC_STATICSIZE,
                     78:   0,  //term
                     79: };
                     80: */
                     81: 
                     82: static int CustomTracks=0,CustomSectors=0,CustomSides=1;    /* Default settings, 80 tracks, 9 sectors, 2 sides */
                     83: static BOOL bInsertIntoDrive=FALSE;                         /* Insert disc image into drive when complete? */
                     84: static BOOL bCustomDiscImage=FALSE;                         /* Is custom or standard disc size? */
                     85: static int ChosenDiscType;                                  /* Index into StandardDiscSizes[] of chosen image type */
                     86: static int ChosenDiscDrive;                                 /* Drive we are making disc image in, eg 0=A:, 1=B: */
                     87: static char CreateBlankImageFileName[MAX_FILENAME_LENGTH];
                     88: 
                     89: 
                     90: /*-----------------------------------------------------------------------*/
                     91: /*
                     92:   Create .ST/.MSA disc image according to 'Tracks,Sector,Sides' and save as filename
                     93: */
                     94: void CreateBlankImage_CreateFile(/*HWND hDlg,*/char *pszFileName,int nTracks, int nSectors, int nSides, BOOL bInsertIntoDrive)
                     95: {
                     96:   unsigned char *pDiscFile;
                     97:   unsigned long DiscSize;
                     98:   unsigned short int SPC,DIR,MEDIA,SPF;
                     99:   char szString[MAX_FILENAME_LENGTH];
                    100:   BOOL bRet=FALSE;
                    101: 
                    102:   /* Calculate size of disc image */
                    103:   DiscSize = nTracks * (nSectors*NUMBYTESPERSECTOR) * nSides;
                    104:   /* Allocate space for our 'file', and blank */
                    105:   pDiscFile = (unsigned char *)Memory_Alloc(DiscSize);
                    106:   Memory_Clear(pDiscFile,DiscSize);
                    107: 
                    108:   /* Fill in boot-sector, this would better as a structure but 'C' pads the variables out */
                    109:   Memory_Set(pDiscFile+2,0x4e,6);                           /* 2-7 'Loader' */
                    110:   *(unsigned char *)(pDiscFile+8) = rand();                 /* 8-10 24-bit serial number */
                    111:   *(unsigned char *)(pDiscFile+9) = rand();
                    112:   *(unsigned char *)(pDiscFile+10) = rand();
                    113:   *(unsigned short int *)(pDiscFile+11) = NUMBYTESPERSECTOR;  /* 11-12 BPS */
                    114:   if ( (nTracks==40) && (nSides==1) ) SPC = 1;
                    115:   else SPC = 2;
                    116:   *(unsigned char *)(pDiscFile+13) = SPC;                   /* 13 SPC */
                    117:   *(unsigned short int *)(pDiscFile+14) = 1;                /* 14-15 RES */
                    118:   *(unsigned char *)(pDiscFile+16) = 2;                     /* 16 FAT */
                    119:   if (SPC==1) DIR = 64;
                    120:   else DIR = 112;
                    121:   *(unsigned short int *)(pDiscFile+17) = DIR;              /* 17-18 DIR */
                    122:   *(unsigned short int *)(pDiscFile+19) = nTracks*nSectors*nSides;  /* 19-20 SEC */
                    123:   if (nTracks==40) MEDIA = 252;
                    124:   else MEDIA = 248;
                    125:   if (nSides==2) MEDIA++;
                    126:   *(unsigned char *)(pDiscFile+21) = MEDIA;                 /* 21 MEDIA */
                    127:   if (nTracks>=80) SPF = 5;
                    128:   else SPF = 2;
                    129:   *(unsigned short int *)(pDiscFile+22) = SPF;              /* 22-23 SPF */
                    130:   *(unsigned short int *)(pDiscFile+24) = nSectors;         /* 24-25 SPT */
                    131:   *(unsigned short int *)(pDiscFile+26) = nSides;           /* 26-27 SIDE */
                    132:   *(unsigned short int *)(pDiscFile+28) = 0;                /* 28-29 HID */
                    133: 
                    134:   /* Ask if OK to overwrite, if exists? */
                    135:   if (File_QueryOverwrite(/*hDlg,*/pszFileName)) {
                    136:     /* Save image to file, as .ST or compressed .MSA */
                    137:     if (File_FileNameIsMSA(pszFileName))
                    138:       bRet = MSA_WriteDisc(pszFileName,pDiscFile,DiscSize);
                    139:     else if (File_FileNameIsST(pszFileName))
                    140:       bRet = ST_WriteDisc(pszFileName,pDiscFile,DiscSize);
                    141: 
                    142:     /* Did create successfully? */
                    143:     if (bRet) {
                    144:       /* Say OK, */
                    145: //      MessageBox(hDlg,"Disc image created successfully.",PROG_NAME,MB_OK | MB_ICONINFORMATION);
                    146:       /* Insert into drive A: or B: ? */
                    147:       if (bInsertIntoDrive)
                    148:         Floppy_InsertDiscIntoDrive(ChosenDiscDrive,pszFileName);
                    149:     }
                    150:     else {
                    151:       /* Warn user we were unable to create image */
                    152:       sprintf(szString,"Unable to create disc image '%s'.",pszFileName);
                    153: //      MessageBox(hDlg,szString,PROG_NAME,MB_OK | MB_ICONSTOP);
                    154:     }
                    155:   }
                    156: 
                    157:   /* Free image */
                    158:   Memory_Free(pDiscFile);
                    159: }
                    160: 
1.1.1.2 ! root      161: 
        !           162: /*-----------------------------------------------------------------------*/
1.1       root      163: /*
                    164:   Enable dialog items according to choice of standard or custom size
                    165: */
                    166: void CreateBlankImage_EnableDialog(/*HWND hDlg,*/BOOL bState)
                    167: {
1.1.1.2 ! root      168:   /* Standard disc size */
1.1       root      169: //  Dialog_EnableItem(hDlg,IDC_IMAGECOMBO,bState);
                    170: 
1.1.1.2 ! root      171:   /* Custom disc size */
1.1       root      172: //  Dialog_EnableItems(hDlg,Custom_DialogItems,!bState);
                    173: }
                    174: 
1.1.1.2 ! root      175: 
        !           176: /*-----------------------------------------------------------------------*/
1.1       root      177: /*
                    178:   Find number of Tracks,Sectors and Sides from chosen dialog settings
                    179: */
                    180: void CreateBlankImage_FindTracksSectorsSides(int *nTracks, int *nSectors, int *nSides, BOOL bCustom, int ChosenDiscType)
                    181: {
                    182:   if (bCustom) {
                    183:     *nTracks = atoi(pszTracksText[CustomTracks]);
                    184:     *nSectors = atoi(pszSectorsText[CustomSectors]);
                    185:     *nSides = atoi(pszSidesText[CustomSides]);
                    186:   }
                    187:   else {
                    188:     *nTracks = StandardDiscSizes[ChosenDiscType].nTracks;
                    189:     *nSectors = StandardDiscSizes[ChosenDiscType].nSectors;
                    190:     *nSides = StandardDiscSizes[ChosenDiscType].nSides;
                    191:   }
                    192: }
                    193: 
1.1.1.2 ! root      194: 
        !           195: /*-----------------------------------------------------------------------*/
1.1       root      196: /*
                    197:   Show size of custom disc in dialog
                    198: */
                    199: void CreateBlankImage_ShowDiscImageCapacity(/*HWND hDlg*/)
                    200: {
                    201:   char szString[256];
                    202:   int DiscSize,nTracks,nSectors,nSides;
                    203: 
1.1.1.2 ! root      204:   /* Find size of disc image */
1.1       root      205:   CreateBlankImage_FindTracksSectorsSides(&nTracks,&nSectors,&nSides,TRUE,ChosenDiscType);
                    206:   DiscSize = nTracks*nSectors*nSides*NUMBYTESPERSECTOR;
                    207: 
                    208:   sprintf(szString,"Capactity: %dk (%d bytes)", DiscSize/1024,DiscSize);
                    209: //  Dialog_SetText(hDlg,IDC_STATICSIZE,szString);
                    210: }
                    211: 
1.1.1.2 ! root      212: 
        !           213: /*-----------------------------------------------------------------------*/
1.1       root      214: /*
                    215:   Set dialog edit box details for Tracks,Sectors and Sides
                    216: */
                    217: 
                    218: void CreateBlankImage_SetDiscImageTracksDetail(/*HWND hDlg,*/int NewCustomTracks)
                    219: {
                    220: //  Dialog_SetText(hDlg,IDC_EDITTRACKS,pszTracksText[CustomTracks=NewCustomTracks]);
                    221: //  CreateBlankImage_ShowDiscImageCapacity(hDlg);
                    222: }
                    223: 
                    224: void CreateBlankImage_SetDiscImageSectorsDetail(/*HWND hDlg,*/int NewCustomSectors)
                    225: {
                    226: //  Dialog_SetText(hDlg,IDC_EDITSECTORS,pszSectorsText[CustomSectors=NewCustomSectors]);
                    227: //  CreateBlankImage_ShowDiscImageCapacity(hDlg);
                    228: }
                    229: 
                    230: void CreateBlankImage_SetDiscImageSidesDetail(/*HWND hDlg,*/int NewCustomSides)
                    231: {
                    232: //  Dialog_SetText(hDlg,IDC_EDITSIDES,pszSidesText[CustomSides=NewCustomSides]);
                    233: //  CreateBlankImage_ShowDiscImageCapacity(hDlg);
                    234: }
                    235: 
1.1.1.2 ! root      236: 
        !           237: /*-----------------------------------------------------------------------*/
1.1       root      238: /*
                    239:   Handle create disc dialog messages
                    240: */
                    241: BOOL CreateBlankImage_DiscImageDialog(/*HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam*/)
                    242: {
                    243: #if 0
                    244:   NMUPDOWN *pUpDown;
                    245:   char szString[MAX_FILENAME_LENGTH];
                    246:   int nTracks,nSectors,nSides;
                    247:   int Offset;
                    248: 
                    249:   switch(Message) {
                    250:     case WM_INITDIALOG:
                    251:       // Set Insert into drive checkbox
                    252:       Dialog_SetButton(hDlg,IDC_CHECK_INSERTINTODRIVE,bInsertIntoDrive);
                    253:       // Fill in combobox
                    254:       Dialog_SetComboBoxItems(hDlg,IDC_IMAGECOMBO,pszStandardDiscNames,DEFAULT_STANDARDDISC);
                    255:       // And select, defaults to 720k disc image size
                    256:       ChosenDiscType = DEFAULT_STANDARDDISC;  // 80 tracks, 9 sectors, 2 sides 720k
                    257:       // Set default choice
                    258:       Dialog_SetButton(hDlg,IDC_STDDISCSIZERADIO,!bCustomDiscImage);
                    259:       Dialog_SetButton(hDlg,IDC_CUSTOMDISCSIZERADIO,bCustomDiscImage);
                    260:       // Set custom spin items
                    261:       Dialog_SetSpinList(hDlg,IDC_EDITTRACKS,IDC_SPINTRACKS,pszTracksText,NUM_TRACKSTEXTS,CustomTracks);
                    262:       Dialog_SetSpinList(hDlg,IDC_EDITSECTORS,IDC_SPINSECTORS,pszSectorsText,NUM_SECTORSTEXTS,CustomSectors);
                    263:       Dialog_SetSpinList(hDlg,IDC_EDITSIDES,IDC_SPINSIDES,pszSidesText,NUM_SIDESTEXTS,CustomSides);
                    264: 
                    265:       CreateBlankImage_ShowDiscImageCapacity(hDlg);
                    266:       CreateBlankImage_EnableDialog(hDlg,!bCustomDiscImage);
                    267:       return(TRUE);
                    268:     
                    269:     case WM_NOTIFY:
                    270:       switch(wParam) {
                    271:         // Handle spin controls
                    272:         case IDC_SPINTRACKS:
                    273:           pUpDown = (NMUPDOWN *)lParam;
                    274:           Offset = Misc_LimitInt(pUpDown->iPos + pUpDown->iDelta, 0,NUM_TRACKSTEXTS-1);
                    275:           Dialog_UpdateSpinList(hDlg,IDC_EDITTRACKS,pszTracksText,NUM_TRACKSTEXTS,Offset);
                    276:           CreateBlankImage_SetDiscImageTracksDetail(hDlg,Offset);
                    277:           return(TRUE);
                    278:         case IDC_SPINSECTORS:
                    279:           pUpDown = (NMUPDOWN *)lParam;
                    280:           Offset = Misc_LimitInt(pUpDown->iPos + pUpDown->iDelta, 0,NUM_SECTORSTEXTS-1);
                    281:           Dialog_UpdateSpinList(hDlg,IDC_EDITSECTORS,pszSectorsText,NUM_SECTORSTEXTS,Offset);
                    282:           CreateBlankImage_SetDiscImageSectorsDetail(hDlg,Offset);
                    283:           return(TRUE);
                    284:         case IDC_SPINSIDES:
                    285:           pUpDown = (NMUPDOWN *)lParam;
                    286:           Offset = Misc_LimitInt(pUpDown->iPos + pUpDown->iDelta, 0,NUM_SIDESTEXTS-1);
                    287:           Dialog_UpdateSpinList(hDlg,IDC_EDITSIDES,pszSidesText,NUM_SIDESTEXTS,Offset);
                    288:           CreateBlankImage_SetDiscImageSidesDetail(hDlg,Offset);
                    289:           return(TRUE);
                    290:       }
                    291: 
                    292:       break;
                    293: 
                    294:     case WM_COMMAND:
                    295:       switch(wParam) {
                    296:         case IDC_STDDISCSIZERADIO:
                    297:         case IDC_CUSTOMDISCSIZERADIO:
                    298:           bCustomDiscImage ^= TRUE;
                    299:           CreateBlankImage_EnableDialog(/*hDlg,*/!bCustomDiscImage);
                    300:           return(TRUE);
                    301:         case IDC_IMAGECOMBO:
                    302:           return(TRUE);
                    303: 
                    304:         case IDOK:
                    305:           // Read back dialog choices
                    306:           bInsertIntoDrive = Dialog_ReadButton(hDlg,IDC_CHECK_INSERTINTODRIVE);
                    307:           bCustomDiscImage = Dialog_ReadButton(hDlg,IDC_CUSTOMDISCSIZERADIO);
                    308:           // Get type of disc(for standard size)
                    309:           ChosenDiscType = Dialog_GetSelectedComboBoxItem(hDlg,IDC_IMAGECOMBO);
                    310:           // Find disc details,(if chosen or custom type)
                    311:           CreateBlankImage_FindTracksSectorsSides(&nTracks,&nSectors,&nSides,bCustomDiscImage,ChosenDiscType);
                    312:           // Confirm with user and create image
                    313:           sprintf(szString,"Create disc image '%s'\n(%d Track, %d Sectors, %d Sides) ?",CreateBlankImageFileName,nTracks,nSectors,nSides);
                    314:           if (MessageBox(hDlg,szString,PROG_NAME,MB_YESNO | MB_ICONQUESTION)==IDYES)
                    315:             CreateBlankImage_CreateFile(hDlg,CreateBlankImageFileName,nTracks,nSectors,nSides,bInsertIntoDrive);
                    316:         case IDCANCEL:
                    317:           EndDialog(hDlg,0);
                    318:           return(TRUE);
                    319:         }
                    320:       break;
                    321:   }
                    322: 
                    323:   return(FALSE);
                    324: #endif
                    325: }
                    326: 
1.1.1.2 ! root      327: 
        !           328: /*-----------------------------------------------------------------------*/
1.1       root      329: /*
                    330:   Create output disassembly dialog
                    331: */
                    332: BOOL CreateBlankImage_DoDialog(/*HWND hDlg,*/int Drive,char *pszFileName)
                    333: {
                    334: //  PROC lpfnDlgProc;
                    335:  
                    336:   /* Is filename valid, ie .ST or .MSA? */
                    337:   if (File_FileNameIsST(pszFileName) || File_FileNameIsMSA(pszFileName)) {
                    338:     /* Store drive for later(when create image) */
                    339:     ChosenDiscDrive = Drive;
                    340:     /* Store filename for later */
                    341:     strcpy(CreateBlankImageFileName,pszFileName);
                    342:     /* Open dialog */
                    343: //    lpfnDlgProc = MakeProcInstance((PROC)CreateBlankImage_DiscImageDialog,hInst);
                    344: //    DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_DIALOG_BLANKIMAGE),hDlg,(DLGPROC)lpfnDlgProc,NULL);
                    345:     return(TRUE);
                    346:   }
                    347:   else
                    348: //    MessageBox(hWnd,"Invalid disc filename. Please re-enter.",PROG_NAME,MB_OK | MB_ICONSTOP);
                    349: 
                    350:   return(FALSE);
                    351: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.