/[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 1021 by dbug, Sat Dec 14 14:02:00 2013 UTC revision 1022 by dbug, Sat Dec 14 17:31:43 2013 UTC
# Line 15  Line 15 
15    
16    
17    
 // BORN.DSK=537856 bytes  
 // 17*358=6086  
 // *2=12172  
 // *44=537856  
   
 // Boot sector at offset 793 (Confirmed)  
 // Loader at offset 0x734 (1844) - Confirmed  
   
 // 793-256-156=381  
 // 381-256=125  
   
 // offset=256+156; // on ajoute le header  
 // offset+=track*6400; // On avance à la bonne piste  
 // offset+=(taille_secteur+nb_oct_after_sector+nb_oct_before_sector)*(sector-1);  
 //  
 // So: Offset = 256+156 + (track*6400) + (taille_secteur+nb_oct_after_sector+nb_oct_before_sector)*(sector-1)  
 //            = 256+156 + (track*6400) + (256+43+59)*(sector-1)  
 //            = 412     + (track*6400) + (358)*(sector-1)  
   
18    
19  FloppyHeader::FloppyHeader()  FloppyHeader::FloppyHeader()
20  {  {
# Line 58  bool FloppyHeader::IsValidHeader() const Line 39  bool FloppyHeader::IsValidHeader() const
39    return true;    return true;
40  }  }
41    
42    
43    void FloppyHeader::Clear()
44    {
45      memset(this,0,sizeof(FloppyHeader));
46    }
47    
48    void FloppyHeader::SetSignature(char signature[8])
49    {
50      memcpy(m_Signature,signature,8);
51    }
52    
53    
54    void FloppyHeader::SetSideNumber(int sideNumber)
55    {
56      m_Sides[0]=(sideNumber>>0)&255;
57      m_Sides[1]=(sideNumber>>8)&255;
58      m_Sides[2]=(sideNumber>>16)&255;
59      m_Sides[3]=(sideNumber>>24)&255;
60    }
61    
62  int FloppyHeader::GetSideNumber() const  int FloppyHeader::GetSideNumber() const
63  {  {
64    int sideNumber= ( ( ( ( (m_Sides[3]<<8) | m_Sides[2]) << 8 ) | m_Sides[1]) << 8 ) | m_Sides[0];    int sideNumber= ( ( ( ( (m_Sides[3]<<8) | m_Sides[2]) << 8 ) | m_Sides[1]) << 8 ) | m_Sides[0];
65    return sideNumber;    return sideNumber;
66  }  }
67    
68    
69    void FloppyHeader::SetTrackNumber(int trackNumber)
70    {
71      m_Tracks[0]=(trackNumber>>0)&255;
72      m_Tracks[1]=(trackNumber>>8)&255;
73      m_Tracks[2]=(trackNumber>>16)&255;
74      m_Tracks[3]=(trackNumber>>24)&255;
75    }
76    
77  int FloppyHeader::GetTrackNumber() const  int FloppyHeader::GetTrackNumber() const
78  {  {
79    int trackNumber= ( ( ( ( (m_Tracks[3]<<8) | m_Tracks[2]) << 8 ) | m_Tracks[1]) << 8 ) | m_Tracks[0];    int trackNumber= ( ( ( ( (m_Tracks[3]<<8) | m_Tracks[2]) << 8 ) | m_Tracks[1]) << 8 ) | m_Tracks[0];
80    return trackNumber;    return trackNumber;
81  }  }
82    
83    void FloppyHeader::SetGeometry(int geometry)
84    {
85      m_Geometry[0]=(geometry>>0)&255;
86      m_Geometry[1]=(geometry>>8)&255;
87      m_Geometry[2]=(geometry>>16)&255;
88      m_Geometry[3]=(geometry>>24)&255;
89    }
90    
91    int FloppyHeader::GetGeometry() const
92    {
93      int geometry= ( ( ( ( (m_Geometry[3]<<8) | m_Geometry[2]) << 8 ) | m_Geometry[1]) << 8 ) | m_Geometry[0];
94      return geometry;
95    }
96    
97    int FloppyHeader::FindNumberOfSectors(int& firstSectorOffset,int& sectorInterleave) const
98    {
99      firstSectorOffset=0;
100      sectorInterleave=0;
101    
102      /*
103      Format of a track:
104      6400 bytes in total
105      - gap1: 72/12 bytes at the start of the track (with zeroes)
106      Then for each sector:
107      - ?:             4 bytes (A1 A1 A1 FE)
108      - track number:  1 byte (0-40-80...)
109      - side number:   1 byte (0-1)
110      - sector number: 1 byte (1-18-19)
111      - one:           1 byte (1)
112      - crc:           2 bytes (crc of the 8 previous bytes)
113      - gap2:          34 bytes (22xAE , 12x00)
114      - ?:             4 bytes (A1 A1 A1 FB)
115      - data:          256 bytes
116      - crc:           2 bytes (crc of the 256+4 previous bytes)
117      - gap3:          50/46 bytes
118      */
119      unsigned char* trackDataStart=(unsigned char*)(this+1);
120      unsigned char* trackData   =trackDataStart;
121      unsigned char* trackDataEnd=trackDataStart+6400;
122    
123      int lastSectorFound=0;
124      while (trackData<(trackDataEnd-16))
125      {
126        if ( (trackData[0]==0xa1) && (trackData[1]==0xa1) && (trackData[2]==0xa1) && (trackData[3]==0xfe) )
127        {
128          // Found a marker for a synchronization sequence for a sector [#A1 #A1 #A1], [#FE Track Side Sector tt CRC CRC]
129          int sectorNumber=trackData[6];
130          if (sectorNumber==(lastSectorFound+1))
131          {
132            lastSectorFound=sectorNumber;
133          }
134          else
135          {
136            ShowError("There's something wrong in the track structure of the floppy, the sector id does not make sense.");
137          }
138          trackData+=10;  // Skip synchronization sequence
139          trackData+=34;  // - gap2:          34 bytes (22xAE , 12x00)
140          trackData+=4;   // - ?:             4 bytes (A1 A1 A1 FB)
141    
142          if (sectorNumber==1)
143          {
144            firstSectorOffset=trackData-trackDataStart;
145          }
146          else
147          if (sectorNumber==2)
148          {
149            sectorInterleave=trackData-trackDataStart-firstSectorOffset;
150          }
151          trackData+=256; // Sector data
152        }
153        else
154        {
155          trackData++;
156        }
157      }
158    
159      return lastSectorFound;
160    }
161    
162    
163    
# Line 97  Floppy::Floppy() : Line 185  Floppy::Floppy() :
185    m_TrackNumber(0),    m_TrackNumber(0),
186    m_SectorNumber(0),    m_SectorNumber(0),
187    m_CurrentTrack(0),    m_CurrentTrack(0),
188    m_CurrentSector(1)    m_CurrentSector(1),
189      m_OffsetFirstSector(156),   // 156 (Location of the first byte of data of the first sector)
190      m_InterSectorSpacing(358)   // 358 (Number of bytes to skip to go to the next sector: 256+59+43)
191  {  {
192  }  }
193    
# Line 157  void compute_crc(unsigned char *ptr,int Line 247  void compute_crc(unsigned char *ptr,int
247  }  }
248    
249    
 struct DskHeader  
 {  
   char signature[8];  
   int sides;  
   int tracks;  
   int geometry;  
 };  
   
   
250  bool Floppy::CreateDisk(int numberOfSides,int numberOfTracks,int numberOfSectors)  bool Floppy::CreateDisk(int numberOfSides,int numberOfTracks,int numberOfSectors)
251  {  {
252    // Heavily based on MakeDisk and Tap2DSk    // Heavily based on MakeDisk and Tap2DSk
# Line 193  bool Floppy::CreateDisk(int numberOfSide Line 274  bool Floppy::CreateDisk(int numberOfSide
274      m_SectorNumber=numberOfSectors;     // 17      m_SectorNumber=numberOfSectors;     // 17
275      m_SideNumber  =numberOfSides;       // 2      m_SideNumber  =numberOfSides;       // 2
276    
277      DskHeader* header=(DskHeader*)m_Buffer;      FloppyHeader& header(*((FloppyHeader*)m_Buffer));
278      memcpy(header->signature,"MFM_DISK",8);      header.Clear();
279      header->sides=numberOfSides;      header.SetSignature("MFM_DISK");
280      header->tracks=numberOfTracks;      header.SetSideNumber(numberOfSides);
281      header->geometry=1;      header.SetTrackNumber(numberOfTracks);
282        header.SetGeometry(1);
283    
284      unsigned char* trackbuf=(unsigned char*)m_Buffer+256;      unsigned char* trackbuf=(unsigned char*)m_Buffer+256;
285      for (int s=0;s<numberOfSides;s++)      for (int s=0;s<numberOfSides;s++)
# Line 248  bool Floppy::CreateDisk(int numberOfSide Line 330  bool Floppy::CreateDisk(int numberOfSide
330          trackbuf+=6400;          trackbuf+=6400;
331        }        }
332      }      }
333      return true;      if (header.IsValidHeader())
334        {
335          m_SectorNumber=header.FindNumberOfSectors(m_OffsetFirstSector,m_InterSectorSpacing);   //17;    // Can't figure out that from the header Oo
336          return true;
337        }
338    }    }
339    
340    return false;    return false;
# Line 264  bool Floppy::LoadDisk(const char* fileNa Line 350  bool Floppy::LoadDisk(const char* fileNa
350      {      {
351        m_TrackNumber =header.GetTrackNumber();        m_TrackNumber =header.GetTrackNumber();
352        m_SideNumber  =header.GetSideNumber();        m_SideNumber  =header.GetSideNumber();
353        m_SectorNumber=17;    // Can't figure out that from the header Oo        m_SectorNumber=header.FindNumberOfSectors(m_OffsetFirstSector,m_InterSectorSpacing);   //17;    // Can't figure out that from the header Oo
354        return true;        return true;
355      }      }
356    }    }
# Line 308  remplacée par un octet #F7. Comme on le Line 394  remplacée par un octet #F7. Comme on le
394  15, 16 or 17 sectors: gap1=72; gap2=34; gap3=50;  15, 16 or 17 sectors: gap1=72; gap2=34; gap3=50;
395            18 sectors: gap1=12; gap2=34; gap3=46;            18 sectors: gap1=12; gap2=34; gap3=46;
396  */  */
397    // Header secteur
398    #define nb_oct_before_sector  59      // Cas de 17 secteurs/pistes !
399    #define nb_oct_after_sector   43      //#define nb_oct_after_sector 31
400    
401  unsigned int Floppy::GetDskImageOffset()  unsigned int Floppy::GetDskImageOffset()
402  {  {
403    unsigned int offset=256+156;     // Add the header    unsigned int offset=256;         // Add the DSK file header size
404    offset+=m_CurrentTrack*6400;     // And move to the correct track    offset+=m_CurrentTrack*6400;     // And move to the correct track
405    offset+=(taille_secteur+nb_oct_after_sector+nb_oct_before_sector)*(m_CurrentSector-1);    offset+=m_OffsetFirstSector;     // Add the offset from the start of track to the data of the first sector
406      offset+=m_InterSectorSpacing*(m_CurrentSector-1);
407    return offset;    return offset;
408  }  }
409    

Legend:
Removed from v.1021  
changed lines
  Added in v.1022

  ViewVC Help
Powered by ViewVC 1.1.26