/[projet1]/public/pc/tools/osdk/main/makedisk/Floppy.cpp
Defence Force logotype

Diff of /public/pc/tools/osdk/main/makedisk/Floppy.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1017 by dbug, Thu Dec 12 20:50:14 2013 UTC revision 1018 by dbug, Sat Dec 14 14:02:00 2013 UTC
# Line 108  Floppy::~Floppy() Line 108  Floppy::~Floppy()
108  }  }
109    
110    
111    unsigned int crctab[256] =
112    {
113      0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
114      0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
115      0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
116      0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
117      0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
118      0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
119      0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
120      0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
121      0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
122      0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
123      0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
124      0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
125      0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
126      0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
127      0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
128      0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
129      0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
130      0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
131      0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
132      0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
133      0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
134      0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
135      0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
136      0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
137      0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
138      0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
139      0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
140      0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
141      0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
142      0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
143      0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
144      0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
145    };
146    
147    void compute_crc(unsigned char *ptr,int count)
148    {
149      int i;
150      unsigned short crc=0xFFFF,byte;
151      for (i=0;i<count;i++) {
152        byte= *ptr++;
153        crc=(crc<<8)^crctab[(crc>>8)^byte];
154      }
155      *ptr++=crc>>8;
156      *ptr++=crc&0xFF;
157    }
158    
159    
160    struct DskHeader
161    {
162      char signature[8];
163      int sides;
164      int tracks;
165      int geometry;
166    };
167    
168    
169    bool Floppy::CreateDisk(int numberOfSides,int numberOfTracks,int numberOfSectors)
170    {
171      // Heavily based on MakeDisk and Tap2DSk
172      int gap1,gap2,gap3;
173    
174      switch (numberOfSectors)
175      {
176      case 15: case 16: case 17:
177        gap1=72; gap2=34; gap3=50;
178        break;
179    
180      case 18:
181        gap1=12; gap2=34; gap3=46;
182        break;
183    
184      default:
185        ShowError("Unrealistic sectors per track number\n");
186      }
187    
188      m_BufferSize=256+numberOfSides*numberOfTracks*6400;
189      m_Buffer=malloc(m_BufferSize);
190      if (m_Buffer)
191      {
192        m_TrackNumber =numberOfTracks;      // 42
193        m_SectorNumber=numberOfSectors;     // 17
194        m_SideNumber  =numberOfSides;       // 2
195    
196        DskHeader* header=(DskHeader*)m_Buffer;
197        memcpy(header->signature,"MFM_DISK",8);
198        header->sides=numberOfSides;
199        header->tracks=numberOfTracks;
200        header->geometry=1;
201    
202        unsigned char* trackbuf=(unsigned char*)m_Buffer+256;
203        for (int s=0;s<numberOfSides;s++)
204        {
205          for (int t=0;t<numberOfTracks;t++)
206          {
207            {
208              int i;
209              int offset=0;
210              for (i=0;i<gap1-12;i++)
211              {
212                trackbuf[offset++]=0x4E;
213              }
214              for (int j=0;j<numberOfSectors;j++)
215              {
216                for (i=0;i<12;i++) trackbuf[offset++]=0;
217                for (i=0;i<3;i++) trackbuf[offset++]=0xA1;
218                trackbuf[offset++]=0xFE;
219                for (i=0;i<6;i++) offset++;
220                for (i=0;i<gap2-12;i++) trackbuf[offset++]=0x22;
221                for (i=0;i<12;i++) trackbuf[offset++]=0;
222                for (i=0;i<3;i++) trackbuf[offset++]=0xA1;
223                trackbuf[offset++]=0xFB;
224                for (i=0;i<258;i++) offset++;
225                for (i=0;i<gap3-12;i++) trackbuf[offset++]=0x4E;
226              }
227    
228              while (offset<6400)
229              {
230                trackbuf[offset++]=0x4E;
231              }
232            }
233            int offset=gap1;
234            for (int i=0;i<numberOfSectors;i++)
235            {
236              trackbuf[offset+4]=t;
237              trackbuf[offset+5]=s;
238              trackbuf[offset+6]=i+1;
239              trackbuf[offset+7]=1;
240              compute_crc(trackbuf+offset,4+4);
241              offset+=4+6;
242              offset+=gap2;
243              memset(trackbuf+offset+4,0,256);
244              compute_crc(trackbuf+offset,4+256);
245              offset+=256+6;
246              offset+=gap3;
247            }
248            trackbuf+=6400;
249          }
250        }
251        return true;
252      }
253    
254      return false;
255    }
256    
257    
258  bool Floppy::LoadDisk(const char* fileName)  bool Floppy::LoadDisk(const char* fileName)
259  {  {
260    if (LoadFile(fileName,m_Buffer,m_BufferSize))    if (LoadFile(fileName,m_Buffer,m_BufferSize))
# Line 127  bool Floppy::LoadDisk(const char* fileNa Line 274  bool Floppy::LoadDisk(const char* fileNa
274    
275  bool Floppy::SaveDisk(const char* fileName) const  bool Floppy::SaveDisk(const char* fileName) const
276  {  {
277    assert(m_Buffer);    if (m_Buffer)
278    return SaveFile(fileName,m_Buffer,m_BufferSize);    {
279        return SaveFile(fileName,m_Buffer,m_BufferSize);
280      }
281      return false;
282  }  }
283    
284  /*  /*
# Line 168  unsigned int Floppy::GetDskImageOffset() Line 318  unsigned int Floppy::GetDskImageOffset()
318    
319    
320  // 0x0319 -> 793  // 0x0319 -> 793
321  void Floppy::WriteSector(const char *fileName)  bool Floppy::WriteSector(const char *fileName)
322  {  {
323      if (!m_Buffer)
324      {
325        return false;
326      }
327    
328    std::string filteredFileName(StringTrim(fileName," \t\f\v\n\r"));    std::string filteredFileName(StringTrim(fileName," \t\f\v\n\r"));
329    
330    void*      buffer;    void*      buffer;
# Line 195  void Floppy::WriteSector(const char *fil Line 350  void Floppy::WriteSector(const char *fil
350    {    {
351      ShowError("Boot Sector file '%s' not found",filteredFileName.c_str());      ShowError("Boot Sector file '%s' not found",filteredFileName.c_str());
352    }    }
353      return true;
354  }  }
355    
356  int Floppy::WriteFile(const char *fileName,int loadAddress)  
357    class TapeInfo
358    {
359    public:
360      TapeInfo()
361        : m_StartAddress(0)
362        , m_EndAddress(0)
363        , m_FileType(0)
364        , m_AutoStarts(false)
365        , m_PtrData(nullptr)
366        , m_DataSize(0)
367      {
368    
369      }
370    
371      bool ParseHeader(void* fileBuffer,size_t fileSize)
372      {
373        m_DataSize=fileSize;
374        m_PtrData =(unsigned char*)fileBuffer;
375        while (m_DataSize && (m_PtrData[0]==0x16))
376        {
377          m_DataSize--;
378          m_PtrData++;
379        }
380        if (m_DataSize>8 && (m_PtrData[0]==0x24) && (m_PtrData[1]==0x00) && (m_PtrData[2]==0x00) )
381        {
382          // At this point at least we have a valid synchro sequence and we know we have a usable header
383          m_FileType    = m_PtrData[3];
384          m_AutoStarts  =(m_PtrData[4]!=0);
385          m_EndAddress  =(m_PtrData[5]<<8)|m_PtrData[6];
386          m_StartAddress=(m_PtrData[7]<<8)|m_PtrData[8];
387    
388          m_DataSize-=9;
389          m_PtrData+=9;
390    
391          if (m_DataSize && (m_PtrData[0]==0x00) )
392          {
393            // Skip the zero
394            m_DataSize--;
395            m_PtrData++;
396    
397            // Now we read the name
398            while (m_DataSize && (m_PtrData[0]!=0x00))
399            {
400              m_FileName+=m_PtrData[0];
401              m_DataSize--;
402              m_PtrData++;
403            }
404            if (m_DataSize && (m_PtrData[0]==0x00) )
405            {
406              // Skip the zero
407              m_DataSize--;
408              m_PtrData++;
409    
410              // Now ptr points on the actual data
411              return true;
412            }
413          }
414        }
415        // Not a valid tape file
416        return false;
417      }
418    
419    public:
420      unsigned char*  m_PtrData;
421      int             m_DataSize;
422      int             m_StartAddress;
423      int             m_EndAddress;
424      int             m_FileType;
425      bool            m_AutoStarts;
426      std::string     m_FileName;
427    };
428    
429    
430    bool Floppy::WriteFile(const char *fileName,int loadAddress,bool removeHeaderIfPresent)
431  {  {
432      if (!m_Buffer)
433      {
434        return false;
435      }
436    
437      void*      fileBuffer;
438      size_t     fileSize;
439      if (!LoadFile(fileName,fileBuffer,fileSize))
440      {
441        ShowError("Error can't open file '%s'\n",fileName);
442      }
443    
444      unsigned char* fileData=(unsigned char*)fileBuffer;
445    
446      if (removeHeaderIfPresent)
447      {
448        TapeInfo tapeInfo;
449        if (!tapeInfo.ParseHeader(fileBuffer,fileSize))
450        {
451          ShowError("File '%s' is not a valid tape file\n",fileName);
452        }
453        // If the file was a valid tape header, then we use these new information
454        fileData=tapeInfo.m_PtrData;
455        fileSize=tapeInfo.m_DataSize;
456        loadAddress=tapeInfo.m_StartAddress;
457      }
458    
459    FileEntry fileEntry;    FileEntry fileEntry;
460    fileEntry.m_FloppyNumber=0;     // 0 for a single floppy program    fileEntry.m_FloppyNumber=0;     // 0 for a single floppy program
461    
# Line 226  int Floppy::WriteFile(const char *fileNa Line 483  int Floppy::WriteFile(const char *fileNa
483    }    }
484    code_sector << m_CurrentSector;    code_sector << m_CurrentSector;
485    
   void*      fileBuffer;  
   size_t     fileSize;  
   if (!LoadFile(fileName,fileBuffer,fileSize))  
   {  
     ShowError("Error can't open file '%s'\n",fileName);  
   }  
   
486    int nb_sectors_by_files=(fileSize+255)/256;    int nb_sectors_by_files=(fileSize+255)/256;
487    
488    fileEntry.m_StartTrack =m_CurrentTrack;           // 0 to 42 (80...)    fileEntry.m_StartTrack =m_CurrentTrack;           // 0 to 42 (80...)
489    fileEntry.m_StartSector=m_CurrentSector;          // 1 to 17 (or 16 or 18...)    fileEntry.m_StartSector=m_CurrentSector;          // 1 to 17 (or 16 or 18...)
490    fileEntry.m_SectorCount=nb_sectors_by_files;      // Should probably be the real length    fileEntry.m_SectorCount=nb_sectors_by_files;      // Should probably be the real length
491    fileEntry.m_LoadAddress=loadAddress;    fileEntry.m_LoadAddress=loadAddress;
492    fileEntry.m_TotalSize=fileSize;    fileEntry.m_TotalSize  =fileSize;
493    fileEntry.m_FilePath   =fileName;    fileEntry.m_FilePath   =fileName;
494    
   unsigned char* fileData=(unsigned char*)fileBuffer;  
495    while (fileSize)    while (fileSize)
496    {    {
497      unsigned int offset=SetPosition(m_CurrentTrack,m_CurrentSector);      unsigned int offset=SetPosition(m_CurrentTrack,m_CurrentSector);
# Line 266  int Floppy::WriteFile(const char *fileNa Line 515  int Floppy::WriteFile(const char *fileNa
515    
516    m_FileEntries.push_back(fileEntry);    m_FileEntries.push_back(fileEntry);
517    
518    return nb_sectors_by_files;    return true;
519  }  }
520    
521    
# Line 315  bool Floppy::SaveDescription(const char* Line 564  bool Floppy::SaveDescription(const char*
564    int counter=0;    int counter=0;
565    for (auto it(m_FileEntries.begin());it!=m_FileEntries.end();++it)    for (auto it(m_FileEntries.begin());it!=m_FileEntries.end();++it)
566    {    {
567      layoutInfo << "// - Entry #" << counter << " '"<< it->m_FilePath << " ' loads at address " << it->m_LoadAddress << " starts on track " << it->m_StartTrack<< " sector "<< it->m_StartSector <<" and is " << it->m_SectorCount << " sectors long (" << it->m_TotalSize << " bytes).\n";      if (it->m_StartTrack<m_TrackNumber)
568        {
569          // First side
570          layoutInfo << "// - Entry #" << counter << " '"<< it->m_FilePath << " ' loads at address " << it->m_LoadAddress << " starts on track " << it->m_StartTrack<< " sector "<< it->m_StartSector <<" and is " << it->m_SectorCount << " sectors long (" << it->m_TotalSize << " bytes).\n";
571        }
572        else
573        {
574          // Second side
575          layoutInfo << "// - Entry #" << counter << " '"<< it->m_FilePath << " ' loads at address " << it->m_LoadAddress << " starts on the second side on track " << (it->m_StartTrack-m_TrackNumber) << " sector "<< it->m_StartSector <<" and is " << it->m_SectorCount << " sectors long (" << it->m_TotalSize << " bytes).\n";
576        }
577      ++counter;      ++counter;
578    }    }
579    layoutInfo << "//\n";    layoutInfo << "//\n";

Legend:
Removed from v.1017  
changed lines
  Added in v.1018

  ViewVC Help
Powered by ViewVC 1.1.26