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

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

unix.superglobalmegacorp.com

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