/*
    CaPriCe Forever - Amstrad CPC 464/664/6128 emulator
    Copyright (C) 2015-2019 by Frédéric Coste

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "..\Native_CPC.h"



/*
** Make sure we can call this stuff from C++.
*/
#ifdef __cplusplus
extern "C" {
#endif


//===================
// PATCH begin
#ifdef _PATCH_ENABLE

#endif /* _PATCH_ENABLE */
// PATCH end
//===================


/* ============================================================================
Source: http://www.cpcwiki.eu/index.php/ParaDOS (Parados.pdf)
type (single sided or double sided)
FORMAT          CAPACITY  DIRECTORIES TYPE SECTORS/TRACK  SECTORS

PARADOS 80      396k      128         SS  10              &91-&9a
PARADOS 41      203k      64          SS  10              &81-&8a
PARADOS 40D     396k      128         DS  10              &a1-&aa
ROMDOS D1       716k      128         DS  9               &01-&09
ROMDOS D2       712k      256         DS  9               &21-&29
ROMDOS D10      796k      128         DS  10              &11-&1a
ROMDOS D20      792k      256         DS  10              &31-&3a
ROMDOS D40      396k      128         SS  10              &51-&5a
S-DOS           396k      128         SS  10              &71-&7a
DATA (SS 40)    178k      64          SS  9               &c1-&c9
DATA (DS 40)    356k      64          DS  9               &c1-&c9 E
DATA (SS 80)    356k      64          SS  9               &c1-&c9 E
DATA (DS 80)    716k      64          DS  9               &c1-&c9 E
SYSTEM (SS 40)  169k      64          SS  9               &41-&49
SYSTEM (DS 40)  346k      64          DS  9               &41-&49 E
SYSTEM (SS 80)  346k      64          SS  9               &41-&49 E
SYSTEM (DS 80)  704k      64          DS  9               &41-&49 E
IBM (SS 40)     169k      64          SS  8               &01-&08
IBM (DS 40)     346k      64          DS  8               &01-&08 E
IBM (SS 80)     346k      64          SS  8               &01-&08 E
IBM (DS 80)     704k      64          DS  8               &01-&08 E
ULTRAFORM       203k      64          SS  9               &10-&19
============================================================================ */


typedef struct
{
  tULong NbSectors;
  tULong MaxTracks;
  tULong MaxSides;
  tUChar SectorSize;
  tUChar SectorIDMask;
  tUChar ExpectedCatalogueTrack;
  tULong FormatType;
} tDiskFormatInfo;

static const tDiskFormatInfo DiskFormats[] =
{
  {  9, 42, 1, 2, 0xC0, 0, DISK_FORMAT_AMSDOS_DATA },
  {  9, 42, 1, 2, 0x40, 2, DISK_FORMAT_AMSDOS_SYSTEM },
  { 10, 42, 1, 2, 0x00, 1, DISK_FORMAT_AMSDOS_IBM },
  { 10, 80, 2, 2, 0x10, 0, DISK_FORMAT_ROMDOS_D10 },
  {  9, 80, 2, 2, 0x00, 0, DISK_FORMAT_ROMDOS_D1 },
  // Last one !!
  {  0,  0, 0, 0, 0x00, 0, DISK_FORMAT_UNKNOWN }
};


tVoid Native_DiskFormatType(tDiskOperation* DiskOperation)
/***********************************************************************
 *
 *  Native_DiskFormatType
 *
 ***********************************************************************/
{
tDrive* driveP;
tTrack* trackP;
tSector* sectorP;
const tDiskFormatInfo* DiskFormatP;

  driveP = DiskOperation->Drive;
  trackP = &driveP->track[0][0]; // Track 0
  sectorP = &trackP->sector[0]; // First sector

  DiskOperation->FormatType = DISK_FORMAT_UNKNOWN;

  for (DiskFormatP = DiskFormats;
       DiskFormatP->NbSectors;
       DiskFormatP++)
  {
    // Matching format ?
    if (     (trackP->sectors <= DiskFormatP->NbSectors)
          && ((driveP->sides+1) <= DiskFormatP->MaxSides)
          && (driveP->tracks <= DiskFormatP->MaxTracks)
          && (sectorP->CHRN[SECTOR_CHRN_N] == DiskFormatP->SectorSize)
          && ((sectorP->CHRN[SECTOR_CHRN_R] & 0xF0) == DiskFormatP->SectorIDMask) )
    {
      DiskOperation->FormatType = DiskFormatP->FormatType;
      DiskOperation->CatalogueTrack = DiskFormatP->ExpectedCatalogueTrack;
      break;
    }
  }
}
/*----------------------------------------------------------------------------*/

#ifdef __cplusplus
}  /* End of the 'extern "C"' block */
#endif
