Renamed

Dependencies:   mbed

send_pc1403.cpp

Committer:
ffxx68
Date:
2022-02-21
Revision:
2:dff96be9617e
Parent:
1:9289febf4ae9
Child:
5:062962db7a48

File content as of revision 2:dff96be9617e:

#include "send_pc1403.h"
#include "bit_send.h"

///////////////////////////////////////////////////////
// variables from PocketTools code
uint  SHCc = 0 ,      /* Read not from bin, but from Transfile PC plus SHC-format (header included) */
      SHCe = 0 ;      /* End marks are the SHC Basic image included only*/
uint  QTc  = 0 ;      /* Write to Quick-Tape wave format */
uint  STc  = 0 ;      /* Header of SuperTape or file header of PC-1600 format is in the image included */
uint  TAPc = 0 ;      /* Write not to wave format but to emulator tap format (raw bytes)*/
uint  Qcnt = 0 ;      /* Quiet, minimal output */
uint  Scnt = 0 ;      /* Number of sync parameters or sync and sync spaces was defined */
uint  Acnt = 0 ;      /* Number of address parameters, non standard load or entry addresses */
uint  Mcnt = 0 ;      /* Read header parameter from a file */

double  speed = 1.00 ;  /* Used it for modified Pocket Computer with a CPU Speed Up switched on */

ulong  pcId  = 1500 ;  /* ID number of the pocket computer */
ushort pcgrpId = IDENT_UNKNOWN ;
// ulong  pcAddr = 0 ;         /* PC-specific address, used for the end of variable data of PCs with DB-tables */
bool  cnvstr_upr = false ; /* Convert strings to upper, for Pokecon with SML-key not */
bool  type_asm = false ;   /* Image contains assembler source code (subtype), true for CASL and CAP-X */
// TODO (mr#2#): Count all warnings and errors
ulong  err_cnt = 0 ;    /* counts minor errors */
ulong  wrn_cnt = 0 ;    /* counts warnings */

////////////////////////////////////////////////////////////////////////////////
// WriteBitToWav replaced from PocketTools version (writing to a WAV file)
// Here we send directly signals to OUT lines
int WriteBitToWav (int   value,
              FileInfo*  ptrFile)
{
    // Calling the buffered bit-sending routine
    switch ( value ) {
        case 1:
            if  ( bitWaitSend ( value, 0 ) > MAX_BIT_TIME_ERR )
                return ( ERR_NOK ) ;
            break;
        case 0:
            if  ( bitWaitSend ( value, 0 ) > MAX_BIT_TIME_ERR )
                return ( ERR_NOK ) ;
            break;
        default:
            break;
    }
        
    return ( ERR_OK ) ;
}

////////////////////////////////////////////////////////////////////////////////
// Write* methods taken from Pocket-Tools source code 
// https://www.peil-partner.de/ifhe.de/sharp/
int WriteQuaterToWav (uint   value,
                      uint   stopBits,
                 FileInfo*   ptrFile)
{
      uint  tmp ;
      uint  ii ;
       int  error ;

    // if (TAPc > 0) return (WriteQuaterToTap (value, ptrFile)); // no  
    
    do {
        
        error = WriteBitToWav (0, ptrFile) ;
        if (error != ERR_OK) break ;

        for ( ii = 0 ; ii < 4 ; ++ii ) {
            tmp = 1 << ii ;
            if ( (value & tmp) == 0 )
                error = WriteBitToWav (0, ptrFile) ;
            else
                error = WriteBitToWav (1, ptrFile) ;

            if (error != ERR_OK) break ;
        }
        if (error != ERR_OK) break ;

        for ( ii = 0 ; ii < stopBits ; ++ii ) {
            error = WriteBitToWav (1, ptrFile) ;
            if (error != ERR_OK) break ;
        }
        if (error != ERR_OK) break ;

    } while (0) ;
    return (error);
}

int WriteByteToWav (ulong  value,
                    uchar  order,
                    uchar  mode,
                FileInfo*  ptrFile)
{
      uint  lsq ;
      uint  msq ;
      int  error ;

    // if (TAPc > 0) return (WriteByteToTap (value, order, ptrFile)) ; // no  

    // if (order == ORDER_E) return (WriteByteToEWav (value, ptrFile)) ; // no  1403

    do {
        lsq = value & 0x0F ;
        msq = (value >> 4) & 0x0F ;
        if (order == ORDER_INV) {
            error = WriteQuaterToWav (lsq, Mode[mode].stopb1, ptrFile) ;
            if (error != ERR_OK) break ;
            error = WriteQuaterToWav (msq, Mode[mode].stopb2, ptrFile) ;
            if (error != ERR_OK) break ;
        }
        else {
            error = WriteQuaterToWav (msq, Mode[mode].stopb1, ptrFile) ;
            if (error != ERR_OK) break ;
            error = WriteQuaterToWav (lsq, Mode[mode].stopb2, ptrFile) ;
            if (error != ERR_OK) break ;
        }

    } while (0) ;
    return (error);
}

int CheckSumB1 (  ulong  Byte,
              FileInfo*  ptrFile)
{
    ushort sum ;

    /* Update the checksum */
        sum = ptrFile->sum + ((Byte & 0xF0) >> 4) ;
        if (sum > 0xFF) {
            ++sum ;
            sum &= 0xFF ;
            }
        ptrFile->sum = (sum + (Byte & 0x0F)) & 0xFF ;

    return (0);
}


int CheckSumE (  ulong  Byte,
//               ulong*  ptrSum )
               FileInfo*  ptrFile)
{
    uint tmp, ii ;

    /* Update the checksum */
    tmp = 0x80 ;
    for ( ii = 0 ; ii < 8 ; ++ii, tmp >>= 1 )
        if ( (Byte & tmp) != 0 )  
            // ++ *ptrSum ;
             ++ ptrFile->sum ;

    return (0);
}


int WriteSyncToWav (ulong  nbSync,  // 
                FileInfo*  ptrFile)
{
    ulong  ii ;
    int    error = ERR_OK ;

    if (TAPc > 0) return (ERR_OK); // no

    do {
        /* Write the Synchro patern */
        for ( ii = 0 ; ii < nbSync ; ++ii ) {
            error = WriteBitToWav (1, ptrFile) ;
            if (error != ERR_OK) break ;
        }
        if (error != ERR_OK) break ;

    } while (0) ;
    return (error);
}

int WriteUsedatLenToQTWav ( uchar  order, /* Quick-Tape incomplete blocks with fill data */
                            uchar  mode,
                        FileInfo*  ptrFile)
{   long tmpL ;
    int  error ;

    tmpL = ptrFile->nbByte - ptrFile->total_diff - ptrFile->total ; //not for IDENT_QT_DAT: variable block in RAM based

    if (tmpL > 0) {
            if (tmpL > BLK_OLD) tmpL = BLK_OLD ;
            --tmpL ;
            if (tmpL < BLK_OLD -1) ptrFile->usedat_len = tmpL + 1 ;
            else ptrFile->usedat_len = 0 ; /* L:0x4F ignored, no effect */

            error = WriteByteToWav (tmpL, order, mode, ptrFile) ;
            if (error != ERR_OK) return (error) ;
            if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG (L:%02X)", (uint) tmpL);

            ptrFile->sum = tmpL ;
    }
    else if (tmpL == 0 ) error = ERR_OK ;
    else {
            printf(" WUsedatLQT: End of file was expected");
            error = ERR_FMT ;
    }
    ptrFile->count = 0 ;

    return (error);
}

int WriteByteSumToWav (ulong  value,
                       uchar  order,
                       uchar  mode,
                       FileInfo*  ptrFile)
{
    int   error ;
    bool  writ_full_block = false ;
    
    do {

        if ( (ptrFile->debug & 0x0040) > 0) {
            debug_printf(" %02X", (uchar) value);
            if ( ptrFile->total %0x100 == 0xFF ) printf("\n");
        }
        error = WriteByteToWav (value, order, mode, ptrFile) ;
        if (error != ERR_OK) break ;

        if (mode == MODE_B22 || mode == MODE_B11) ptrFile->sum += value ;
        else if (mode == MODE_B9 || mode == MODE_B10) CheckSumE (value, ptrFile) ; //ptrFile
        else CheckSumB1 (value, ptrFile) ;

        ++ptrFile->count ;
        if (!writ_full_block) ++ptrFile->total ;

        if ( ptrFile->usedat_len > 0) { /* QTape incomplete block */
            if (--ptrFile->usedat_len == 0) {
                if ( ( (ptrFile->debug & 0x0040) > 0 ) && (Qcnt == 0) ) debug_printf("DEBUG Fill data:");
                value = 0x00 ;
                writ_full_block = true ;
            }
        }

        switch (mode) {
        case MODE_B22 :
            if ( ptrFile->count >= BLK_OLD ) {

                if ( (ptrFile->debug & 0x0040) > 0 )
                    debug_printf(" (%04X)", (uint) ptrFile->sum);

                /* Write the checksum */
                error = WriteByteToWav (ptrFile->sum >> 8 & 0xFF, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;
                error = WriteByteToWav (ptrFile->sum & 0xFF, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;

                ptrFile->count = 0 ;
                ptrFile->sum   = 0 ;
            }
            break ;

        case MODE_B21 :
        case MODE_B20 :
        case MODE_B19 :
            if ( (ptrFile->count % BLK_OLD_SUM) == 0) {

                if ( (ptrFile->debug & 0x0040) > 0 )
                    debug_printf(" (%02X)", (uchar) ptrFile->sum);

                /* Write the checksum */
                error = WriteByteToWav (ptrFile->sum, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;

                if ( ptrFile->count >= BLK_OLD ) {
                    ptrFile->count = 0 ;
                    ptrFile->sum   = 0 ;
                    // if (pcgrpId==IDENT_PC1211) error = WriteSyncToWav (1803, ptrFile) ; //DATA not
                    if (ptrFile->ident == IDENT_PC1211) /* default 1803 bits, data not */
                        error = WriteSyncToWav (ptrFile->nbSync, ptrFile) ;
                }
            }
            break ;

        case MODE_B17 :
        case MODE_B16 :
        case MODE_B15 :
            if ( ptrFile->count >= BLK_OLD_SUM) {

                if ( (ptrFile->debug & 0x0040) > 0 )
                    debug_printf(" (%02X)", (uchar) ptrFile->sum);

                /* Write the checksum */
                error = WriteByteToWav (ptrFile->sum, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;

                ptrFile->count = 0 ;
                ptrFile->sum   = 0 ;
            }
            break ;

        case MODE_B14 :
        case MODE_B13 :
            if ( ptrFile->count >= BLK_NEW) {

                if ( (ptrFile->debug & 0x0040) > 0 )
                    debug_printf(" (%02X)", (uchar) ptrFile->sum);

                /* Write the checksum */
                error = WriteByteToWav (ptrFile->sum, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;

                ptrFile->count = 0 ;
                ptrFile->sum   = 0 ;
            }
            break ;

        case MODE_B9 : /* PC-E/G/1600 */
            if ( ptrFile->count >= ptrFile->block_len ) {

                if ( (ptrFile->debug & 0x0040) > 0 )
                    debug_printf(" (%04X)", (uint) ptrFile->sum & 0xFFFF);

                /* Write the checksum */
                error = WriteByteToWav (ptrFile->sum >> 8 & 0xFF, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;
                error = WriteByteToWav (ptrFile->sum & 0xFF, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;

                ptrFile->count = 0 ;
                ptrFile->sum   = 0 ;
            }
            break ;

        case MODE_B10 : /* SuperTape */
            if ( ptrFile->count >= ptrFile->block_len ) {

                if ( (ptrFile->debug & 0x0040) > 0 )
                    debug_printf(" (%04X)", (uint) ptrFile->sum & 0xFFFF);

                /* Write the checksum */
                error = WriteByteToWav (ptrFile->sum & 0xFF, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;
                error = WriteByteToWav (ptrFile->sum >> 8 & 0xFF, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;

                if ( (ptrFile->debug & 0x0040) > 0) debug_printf(" %02X", (uchar) SUPT_END);
                error = WriteByteToWav (SUPT_END, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;

                ptrFile->count = 0 ;
                ptrFile->sum   = 0 ;
            }
            break ;

        case MODE_B11 :
            if ( ptrFile->count >= BLK_OLD ) {

                if ( (ptrFile->debug & 0x0040) > 0 )
                    debug_printf(" (%04X)", (uint) ptrFile->sum);
                /* Write the checksum */
                error = WriteByteToWav (ptrFile->sum >> 8 & 0xFF, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;
                error = WriteByteToWav (ptrFile->sum & 0xFF, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;

                error = WriteByteToWav (EOF_15, order, mode, ptrFile) ;
                if (error != ERR_OK) break ;
                if ( (ptrFile->debug & 0x0040) > 0) debug_printf(" (E:%02X)", (uint) EOF_15);
                writ_full_block = false ;

                error = WriteSyncToWav (50, ptrFile) ; /* 0.02 s */
                if (error != ERR_OK) break ;
                error = WriteUsedatLenToQTWav (order, mode, ptrFile) ;
            }
            break ;

        default :
            debug_printf ("%s: Unknown Mode\n", argP) ;
            break ;
        }

    } while (writ_full_block) ;

    return (error);
}

ulong SwapByte (ulong byte)
{
    return ( (byte >> 4) + (byte << 4 & 0xF0) );
}

/* File name for New and Old BASIC */
int WriteSaveNameToWav (char*  ptrName,
                        uchar  mode,
                    FileInfo*  ptrFile)
{
     uint  ii ;
     uint  tmpL ;
     char  byte ;
     char  tmpS[20] ;
      int  error ;

   if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG ptrName %s\n\r", ptrName);

    do {
        /* Uppercase the name is done in main if needed */
        // tmpL = strlen (ptrName) ;
        for (ii = 0; ii < cLPF-1 && ptrName[ii] != '\0'; ii++) ;
        tmpL = ii;
        
        if (tmpL > 7)
            tmpL = 7 ;
            
        for ( ii = 0 ; ii < tmpL ; ++ii )
            tmpS[ii] = ptrName[ii] ;
    
        tmpS[tmpL] = 0 ;
        //if (Qcnt == 0) debug_printf ("Save name    : %s\n", tmpS) ;
        // strncpy( ptrName, tmpS, cLPF-1) ;
        for (ii = 0; ii < cLPF-1 && ptrName[ii] != '\0'; ii++) 
           tmpS[ii] = ptrName[ii]; 
       /* crash?
       for ( ; ii < cLPF-1; ii++)
           tmpS[ii] = '\0';
       */
       tmpL = 7 - tmpL ;
       for ( ii = 0 ; ii < tmpL ; ++ii )
            tmpS[ii] = 0 ;

        if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG tmpS %s\n\r", tmpS);
        for ( ii = tmpL ; ii < 7 ; ++ii ) {
            byte = (ulong) ptrName[6 - ii] ;

            switch (mode) {
            case MODE_B19 :
            case MODE_B20 :

                if (byte < 0x80)
                    byte = CodeOld[byte] ;
                else
                    byte = CodeOld[0] ;
                break ;

            default :

                if (byte >= 0x80)
                    byte = 0x20 ;
                break ;
            }
            tmpS[ii] = (char) SwapByte(byte) ;
        }
        tmpS[7] = 0x5F ;

        /* Write the Name */
        ptrFile->count = 0 ;
        ptrFile->sum   = 0 ;
        for ( ii = 0 ; ii < 8 ; ++ii ) {
            error = WriteByteSumToWav (tmpS[ii], ORDER_STD, mode, ptrFile) ;
            if (error != ERR_OK) break ;
        }

        if (ptrFile->ident == IDENT_PC1211)
            error = WriteSyncToWav (151, ptrFile) ;
        else if (ptrFile->ident == IDENT_PC121_DAT)
            error = WriteSyncToWav (111, ptrFile) ;

        ptrFile->count = 0 ;
        ptrFile->sum   = 0 ;

    } while (0) ;
    return (error);
}

int DEBUGSaveNameToWav (char*  ptrName,
                        uchar  mode,
                    FileInfo*  ptrFile)
{
     uint  ii ;
     uint  i ;
    ulong  byte ;
    ulong  tmpL ;
     char  tmpS[10] ;
      int  error ;

    do {
        /* Uppercase the name is done in main if needed */
        tmpL = strlen (ptrName) ;
        debug_printf("DEBUG tmpL %u\n\r", tmpL);

        if (tmpL > 7)
            tmpL = 7 ;

        for ( ii = 0 ; ii < tmpL ; ++ii ) {
            debug_printf("DEBUG ptrName[ii] %c\n\r", ptrName[ii]);
            tmpS[ii] = ptrName[ii] ;
        }
        tmpS[tmpL] = 0 ;
        //if (Qcnt == 0) debug_printf ("Save name    : %s\n", tmpS) ;
        debug_printf("DEBUG ptrName %s\n\r", ptrName);
        debug_printf("DEBUG i 1 ");

        // strncpy( ptrName, tmpS, cLPF-1) ;
        for (i = 0; i < cLPF-1 && ptrName[i] != '\0'; i++) {
           debug_printf("%u ", i);
           tmpS[i] = ptrName[i]; 
        }
        debug_printf("\n\rDEBUG i 2 ");
        /* crash?
        for ( ; i < cLPF-1; i++) {
            debug_printf("%u ", i);
            tmpS[i] = '\0';
        }
        */
        debug_printf("\n\rDEBUG tmpS %s\n\r", tmpS);
        tmpL = 7 - tmpL ;
        for ( ii = 0 ; ii < tmpL ; ++ii )
            tmpS[ii] = 0 ;
        debug_printf("DEBUG tmpS %s\n\r", tmpS);

        /* reverse-order bytes */
        for ( ii = tmpL ; ii < 7 ; ++ii ) {
            byte = (ulong) ptrName[6 - ii] ;
            debug_printf("DEBUG byte %u\n\r", byte);

            switch (mode) {
            case MODE_B19 :
            case MODE_B20 :

                if (byte < 0x80)
                    byte = CodeOld[byte] ;
                else
                    byte = CodeOld[0] ;
                break ;

            default :

                if (byte >= 0x80)
                    byte = 0x20 ;
                break ;
            }
            tmpS[ii] = (char) SwapByte(byte) ;
        }
        tmpS[7] = 0x5F ;
        debug_printf("DEBUG ii %u\n\r", ii);

        /* Write the Name */
        ptrFile->count = 0 ;
        ptrFile->sum   = 0 ;
        for ( ii = 0 ; ii < 8 ; ++ii ) {
            error = WriteByteSumToWav (tmpS[ii], ORDER_STD, mode, ptrFile) ;
            if (error != ERR_OK) break ;
        }
        if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG Name - Bytes was printed swapped.\n\r");

        debug_printf("DEBUG WriteSyncToWav prima\n\r");

        if (ptrFile->ident == IDENT_PC1211)
            error = WriteSyncToWav (151, ptrFile) ;
        else if (ptrFile->ident == IDENT_PC121_DAT)
            error = WriteSyncToWav (111, ptrFile) ;
        debug_printf("DEBUG WriteSyncToWav dopo\n\r");

        ptrFile->count = 0 ;
        ptrFile->sum   = 0 ;
        debug_printf("DEBUG fine\n\r");

    } while (0) ;
    return (error);
}

/* Head of Binary Data for New and Old series */
int WriteHeadToBinWav (ulong  addr,
                       ulong  size,
                       uchar  mode,
                   FileInfo*  ptrFile)
{
      int  ii ;
    ulong  len ;
    ulong  tmpL ;
      int  error ;

    do {
    /*    if (Qcnt == 0)
    {
        debug_printf ("Start Address: 0x%04X\n", (uint) addr);
        debug_printf ("End   Address: 0x%04X, Length: %d bytes\n", (uint) (addr + size -1), (uint) size);
    }
    */

        ptrFile->count = 0 ;
        ptrFile->sum   = 0 ;
        for ( ii = 0 ; ii < 4 ; ++ii ) {
            error = WriteByteSumToWav (0, ORDER_STD, mode, ptrFile) ;
            if (error != ERR_OK) break ;
        }

        /* Write the address, this method is necessary because of swapped checksums in the header. */
        tmpL = ((addr >> 4) & 0xF0) + (addr >> 12) ;        /* H swapped */
        error = WriteByteSumToWav (tmpL, ORDER_STD, mode, ptrFile) ;
        if (error != ERR_OK) break ;

        tmpL = ((addr << 4) & 0xF0) + ((addr >> 4) & 0x0F) ;/* L swapped */
        error = WriteByteSumToWav (tmpL, ORDER_STD, mode, ptrFile) ;
        if (error != ERR_OK) break ;

        /* Write the Length */
        len = size - 1 ;
        tmpL = ((len >> 4) & 0xF0) + (len >> 12) ;
        error = WriteByteSumToWav (tmpL, ORDER_STD, mode, ptrFile) ;
        if (error != ERR_OK) break ;

        tmpL = ((len << 4) & 0xF0) + ((len >> 4) & 0x0F) ;
        error = WriteByteSumToWav (tmpL, ORDER_STD, mode, ptrFile) ;
        if (error != ERR_OK) break ;

        ptrFile->count = 0 ;
        ptrFile->sum   = 0 ;

    } while (0) ;
    return (error);
}

int WriteFooterToNewWav (FileInfo*  ptrFile)
{
    int  error ;

    do {
        ptrFile->count = 0 ; /* no checksum writing from here until the end */

        error = WriteByteSumToWav(BAS_NEW_EOF, ORDER_STD, ptrFile->mode, ptrFile) ;
        // error = WriteByteSumToB13Wav (BAS_NEW_EOF, ptrFile) ;
        if (error != ERR_OK) break ;

        error = WriteByteToWav(BAS_NEW_EOF, ORDER_STD, ptrFile->mode, ptrFile) ;
        if (error != ERR_OK) break ;

        if ( (ptrFile->debug & 0x00C0) > 0 )
            debug_printf(" EOF:%02X", (uchar) BAS_NEW_EOF);

        error = WriteByteToWav(ptrFile->sum, ORDER_STD, ptrFile->mode, ptrFile) ;
        if (error != ERR_OK) break ;

        if ( (ptrFile->debug & 0x0040) > 0 )
            debug_printf(" (%02X)", (uchar) ptrFile->sum);

    /* there are 2bits more HIGH at the end of transmission (at least for PC-1402) M. NOSSWITZ */
     error = WriteBitToWav (1, ptrFile) ;
    if (error != ERR_OK) break ;

    error = WriteBitToWav (1, ptrFile) ;
    if (error != ERR_OK) break ;
 
    /* This puts 2 bits of silence (or 2 HIGH bits alternatively) to the end of the wave file. */
    /* CLOAD does not accept any sound, that could be interpreted as a start bit,              */
    /* during post-processing. Original CSAVE switches the signal low ms after the            */
    /* end of transmission, before the motor of the cassette recorder is switched off.        */
    /* This level out is visible in the CSAVE audio signal after the last bit. T. Muecker     */
    /* not needed for the MBed direct-write version
    error = WriteBitToWav (3, ptrFile) ; 125us High , 
    if (error != ERR_OK) break ;
    error = WriteBitToWav (2, ptrFile) ; 1 ms Midsignal
    if (error != ERR_OK) break ;
    */

    } while (0) ;
    return (error);
}

int WriteFooterToMemoWav (FileInfo*  ptrFile)
{
    int  error ;

    do {
        error = WriteByteToWav(ptrFile->sum, ORDER_STD, ptrFile->mode, ptrFile) ;
        if (error != ERR_OK) break ;

        if ( (ptrFile->debug & 0x0040) > 0 )
            debug_printf(" (%02X)", (uchar) ptrFile->sum);

    error = WriteBitToWav (1, ptrFile) ;
    if (error != ERR_OK) break ;

    error = WriteBitToWav (1, ptrFile) ;
    if (error != ERR_OK) break ;

    /* This puts 2 bits of silence (or 2 HIGH bits alternatively) to the end of the wave file. */
    /* not needed for the MBed send version
    error = WriteBitToWav (3, ptrFile) ;
    if (error != ERR_OK) break ;

    error = WriteBitToWav (2, ptrFile) ;
    if (error != ERR_OK) break ;
    */

    } while (0) ;
    return (error);
}

/////////////////////////////////////////////////////////////////////////////
// My file-send implementation
// Inspired to Pocket-Tools source code ...
int FileSend ( char* FileName, char* FileStream, uint FileSize, FileInfo*  ptrFile )
{
    int      pcId  =  PC_ID; 
    uchar    type ;
    
    uint32_t ii ;
    uint32_t nbByte;
    uint32_t nbSync = N_SYNC_BITS;
    uint32_t addr;
    int      error ;
    char     inVal;
 

    // BIN file stored statically, until I integrate an SD-Card...      
    nbByte = FileSize; 
    ptrFile->nbSync = nbSync;

    if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG FileName %s\n\r",FileName);
    if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG ptrFile->ident %u\n\r",ptrFile->ident);
    if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG nbSync %u\n\r",nbSync);
    if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG nbByte %u\n\r",nbByte);

    switch (ptrFile->ident) {
    /* . . .*/
        case IDENT_NEW_BIN :
            pcgrpId = GRP_NEW ; /*or GRP_EXT */
            type = TYPE_BIN ;
            break ;
    /* . . .*/
        case IDENT_NEW_BAS :
            pcgrpId = GRP_NEW ;
            type = TYPE_IMG ;
            break ;
        default :
            debug_printf ("%s: Unknown Ident\n", argP) ;
            return (ERR_ARG);
    }

    /* if (pcgrpId == GRP_E || pcgrpId == GRP_G || pcgrpId == GRP_16) { 
       . . .
    }   
    else { //   PC-1211 to PC-1500, QTape 
    */   
        if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG WriteSyncToWav\n\r");
        error = WriteSyncToWav (nbSync, ptrFile) ;
    /*
        . . .
    */
    if (error != ERR_OK) return ( error ) ;
                    
    switch (pcId) {
    /*
      . . . 
    */
    case 1403:
       /* if (type == TYPE_BIN && Acnt==0 && SHCc==0 && addr==0) */ addr = TYPE_BIN_ADDR;
    /*
      . . . 
    */
        break;
    default :
        debug_printf ("%s: Pocket computer %d is not implemented\n", argP, pcId) ;
        // MoreInfo (ERR_ARG);
        error = ERR_ARG ;
            // break ;
    }
    if (error != ERR_OK) return ( error )  ;

/*  ...
    else { // PC-121x ... PC-1475 
*/
    if ( (ptrFile->debug & 0x0040) > 0 ) 
        debug_printf("DEBUG set Header Mode (ident %d)\n\r", ptrFile->ident);
    switch (ptrFile->ident) { /* Header Mode */
    case IDENT_PC1211 :
        ptrFile->mode = ptrFile->mode_h = MODE_B20 ;
        break ;

    case IDENT_PC121_DAT :
    case IDENT_OLD_BAS :
    case IDENT_OLD_DAT :
    case IDENT_OLD_BIN :
    case IDENT_OLD_MEM :
        ptrFile->mode = ptrFile->mode_h = MODE_B19 ;
        break ;

    case IDENT_NEW_TEL :
    case IDENT_NEW_SCD :
    case IDENT_NEW_NOT :
    case IDENT_NEW_CRD :

    case IDENT_NEW_CSL :

    case IDENT_NEW_BAS :
    case IDENT_EXT_BAS :
    case IDENT_NEW_DAT :
    case IDENT_NEW_BIN :
        ptrFile->mode = ptrFile->mode_h = MODE_B16 ;
        break ;

    default :
        debug_printf ("%s: Unknown Ident\n", argP) ;
        ptrFile->mode = ptrFile->mode_h = MODE_B21 ;
        return (ERR_ARG);
    }
    if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG Header ptrFile->mode_h %u\n\r",ptrFile->mode_h);

    /* Write the TAPE code */
    if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG Write the TAPE code\n\r");
    error = WriteByteToWav ( (ulong) ptrFile->ident, ORDER_STD, ptrFile->mode_h, ptrFile) ;
    if (error != ERR_OK) return ( error )  ;

    /* Write the Name */
    if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG Write the Name\n\r");
    error = WriteSaveNameToWav ( FileName, ptrFile->mode_h, ptrFile) ;
    if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG error (ERR_OK) %d (%d)\n\r", error, ERR_OK);
    if (error != ERR_OK) return ( error )  ;

    if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG set Body Data Mode\n\r");
    switch (ptrFile->ident) { /* Body Data Mode */
        case IDENT_PC1211 :
            ptrFile->mode = MODE_B20 ;
            break ;
    
        case IDENT_PC121_DAT :
        case IDENT_OLD_BAS :
        case IDENT_OLD_BIN :
        case IDENT_OLD_MEM :
            ptrFile->mode = MODE_B19 ;
            break ;
    
        case IDENT_OLD_DAT :
        case IDENT_NEW_DAT :
            ptrFile->mode = MODE_B15 ;
        break ;
    
        case IDENT_EXT_BAS :
    
        case IDENT_NEW_BAS :
        case IDENT_NEW_CSL :
    
        case IDENT_NEW_TEL :
        case IDENT_NEW_SCD :
        case IDENT_NEW_NOT :
        case IDENT_NEW_CRD :
        case IDENT_NEW_BIN :
          // TODO (mr#2#): Check whitch MODE_B13 or _B14
            if (cnvstr_upr && pcId < 1440) ptrFile->mode = MODE_B13 ; /*older part of new series*/
            else  ptrFile->mode = MODE_B14 ;     /*new series and extended series*/
            break ;
    
        default :
            debug_printf ("%s: Unknown Ident\n", argP) ;
            return ( ERR_ARG );
        }
        if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG Body ptrFile->mode %u\n\r", ptrFile->mode);

        ptrFile->total = 0 ; /* count bytes of body only */
                
        switch (ptrFile->ident) { /* header was written, write all data now */
        /* ... 
        case 
        ... */
        case IDENT_NEW_BAS :
        case IDENT_NEW_CSL :
        case IDENT_EXT_BAS :
            if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG Write the datas\n\r");
            /* Write the datas */
//            info.mode = MODE_B13 ; /*PC-1403 and newer should be MODE_14 */
            /* the older simple algorithm seems to work as well, but this is now, what the PC does originally */
            for ( ii = 0 ; ii < nbByte - 1 ; ++ii ) {
                //if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG LOOP ii %u\n\r", ii);

                inVal = FileStream[ii] ; // fgetc (srcFd) ;
                if ( inVal == EOF ) break ; // premature ending shouldn't happen...

                if ( inVal == BAS_NEW_EOF ) {
                    if (ptrFile->count + 1 == BLK_NEW && ptrFile->sum == 0xE1) { /* Constellation will generate 2-times BAS_NEW_EOF */
                        printf ("\nERROR %i at %lu. byte, usually the low byte of a BASIC line number\n", ERR_SUM, ptrFile->total) ;
                        printf ("This binary constellation activates the CLOAD bug of this series. The line\n") ;
                        printf ("number must be changed or minor changes done in the BASIC text before.\n") ;
                        /* Seldom Bug in CLOAD, for PC-1402/(01) at known ROM address: 40666 */
                        if ((ptrFile->debug & 0x800) == 0 ) {
                            error = ERR_SUM ;
                            break ;
                        }
                    }
                }
                error = WriteByteSumToWav ( (uint) inVal, ORDER_STD, ptrFile->mode, ptrFile) ;
                if (error != ERR_OK) break ;
            }
            if (error != ERR_OK) break ;
            if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("\n\rDEBUG ii %u\n\r", ii);

            inVal = FileStream[ii] ; // fgetc (srcFd) ; /* Read the last byte before EOF mark */
            if (inVal == EOF) break ;

            if (inVal == BAS_NEW_EOF) {
                        /* EOF mark should not be included for this file type normally*/
                        if (Qcnt == 0) printf ("End of File mark %i should not be included in the image\n", inVal) ;
                        /* if end of block, then an additional checksum would be written, but this does work anyhow */
            }
            else {
                if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG last byte: %02X\n\r", (uchar) inVal);
                error = WriteByteToWav ( (uint) inVal, ORDER_STD, ptrFile->mode, ptrFile) ;
                if (error != ERR_OK) break ;
                CheckSumB1 ((uint) inVal, ptrFile) ; /* never write the checksum before BAS_NEW_EOF */

                ++ptrFile->total ;
                ++ptrFile->count ; /* for debug purposes only, WriteFooter will reset it */
            }

            /* Write the END code */
            if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG WriteFooterToNewWav\n\r");
            error = WriteFooterToNewWav (ptrFile) ;

            break ; // IDENT_NEW_BAS, IDENT_EXT_BAS

        case IDENT_NEW_BIN :
            /* Write the address and length */
            error = WriteHeadToBinWav (addr, nbByte, ptrFile->mode_h, ptrFile) ;
            if (error != ERR_OK) break ;
            /* no break */

        case IDENT_NEW_TEL :
        case IDENT_NEW_SCD :
        case IDENT_NEW_NOT :
        case IDENT_NEW_CRD :

            /* Write the datas */
            for ( ii = 0 ; ii < nbByte - 1 ; ++ii ) {
                inVal = FileStream[ii] ; // fgetc (srcFd) ;
                if (inVal == EOF) break ; // premature ending shouldn't happen ...

                error = WriteByteSumToWav ( (uint) inVal, ORDER_STD, ptrFile->mode, ptrFile) ;
                if (error != ERR_OK) break ;
            }
            if (error != ERR_OK) break ;

            inVal = FileStream[ii] ; // fgetc (srcFd) ; /* Read the last byte before EOF mark */
            if (inVal == EOF) break ;

            if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("DEBUG %02X", (uchar) inVal);
            error = WriteByteToWav ( (uint) inVal, ORDER_STD, ptrFile->mode, ptrFile) ;
            if (error != ERR_OK) break ;
            CheckSumB1 ( (uint) inVal, ptrFile ) ; /* never write the checksum before BAS_NEW_EOF */
            ++ptrFile->total ;
            ++ptrFile->count ; /* for debug purposes only, WriteFooter will reset it */

            /* Write the END code */
            if ( ptrFile->ident == IDENT_NEW_BIN) error = WriteFooterToNewWav ( ptrFile ) ;
            else WriteFooterToMemoWav ( ptrFile ) ;

            break ; // IDENT_NEW_BIN and IDENT_NEW_Memos
        default:
            printf ("%s: Unknown Ident\n", argP) ;
            error = ERR_ARG;
            break;
        }        

    if ( (ptrFile->debug & 0x0040) > 0 ) debug_printf("\n\rDEBUG FileSend error %d\n\r", error);
    return (error);

}