/* 
 * This software is licensed under terms of the GNU GENERAL PUBLIC LICENSE
 *
 * This software 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 software  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 software.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Source code is available at http://ppcenter.free.fr
 */


#ifndef SATCOM_GLOBAL_DATA_H
#define SATCOM_GLOBAL_DATA_H


//----------------------------------------------------------------------
// SatCom Global Data.
// It consists in 256 bytes (unsigned long * 64) of data put at the
// at the end of the SGL "user program and data" area.
// 
// This is used in the case we can't use local data.
// Examples:
//  - Communication with yabause emulator.
//  - Communication with PC debugger.
//  - Communication with external program.
//
// Satcom global data's structure end address is 0x002FFFC0
// 
//----------------------------------------------------------------------


// GCC have alternative #pragma pack(N) syntax and old gcc version not
// support pack(push,N), also any gcc version not support it at some platform
#if defined( __GNUC__ )
#pragma pack(1)
#else
#pragma pack(push,1)
#endif


typedef struct _sc_global_t
{
    /******************************************/
    /* ---      Copystub parameters.       ---*/
    /* Program's buffer start address. */
    unsigned long cs_src_stt;                   // 0x002F FFC0
    /* Program's length. */
    unsigned long cs_lenght;                    // 0x002F FFC4
    /* Program's execution start address. */
    unsigned long cs_dst_stt;                   // 0x002F FFC8

    /******************************************/
    /* ---            Unsused.             ---*/
    unsigned long unused12;                     // 0x002F FFCC


    /******************************************/
    /* ---        Crappy Debugger.         ---*/
    /* Incremented when a new debug message is added. */
    unsigned long crd_counter;                  // 0x002F FFD0
    /* Pointer to the current debug message. */
    unsigned long crd_msg_ptr;                  // 0x002F FFD4

    /******************************************/
    /* ---        SatCom Debugger.         ---*/
    /* Address to the SCD data. */
    unsigned long scd_ptr;                      // 0x002FFFD8
    /* Magic number in order to check if we can use SCD or not. */
    unsigned long scd_magic;                    // 0x002F FFDC


    /******************************************/
    /* ---            Unsused.             ---*/
    unsigned long unused07;                     // 0x002F FFE0
    unsigned long unused06;                     // 0x002F FFE4
    unsigned long unused05;                     // 0x002F FFE8


    /******************************************/
    /* ---        Fast Data Link.          ---*/
    /**
     * About FDL:
     *
     * FDL allows data (de)compression without needing to
     * change Data Link's firmware (instead, ARP's firmware
     * is changed).
     * (De)Compression algorithm used is the same as extended comm's one.
     *
     * There are 2 memory area:
     *  - "internal" which is provided by SatCom.
     *  - "user" which is freely set by sending pointers from PC.
     * In legacy comms mode, when receiving data from PC has
     *  finished (*), fdl_req is checked.
     * If fdl_req equals 'C', compress "user" data to
     *  "internal" data, then reset fdl_req to zero.
     * If fdl_req equals 'D', decompress "internal"
     *  data to "user" data, then reset fdl_req to zero.
     *
     * (*) There is no need to check after sending data to
     *     PC, since memory here shouldn't have changed.
    **/

    /* Internal buffer and its length, used as a temporary area for (de)compression. */
    unsigned long fdl_int_buff;                 // 0x002F FFEC
    unsigned short fdl_int_leng;                // 0x002F FFF0
    /* Magic numbers in order to check if we can use FDL or not. */
    unsigned char fdl_magic1;                   // 0x002F FFF2
    unsigned char fdl_magic2;                   // 0x002F FFF3

    /* ^^^ Read until here on init.    */

    /* (De)Compression return value (read after (de)compression request). */
    unsigned short fdl_return;                  // 0x002F FFF4

    unsigned char fdl_reserved;                 // 0x002F FFF6

    /* vvv Write from here on request. */

    /* Compress/Decompress request set from PC. */
    /* Need to be set to 'C' in order to compress, or 'D' in order to decompress. */
    unsigned char fdl_req;                      // 0x002F FFF7
    /* (De)Compression output parameters set from PC. */
    unsigned long fdl_usr_buff;                 // 0x002F FFF8
    unsigned long fdl_usr_leng;                 // 0x002F FFFC
} sc_global_t;

// GCC have alternative #pragma pack() syntax and old gcc version not
// support pack(pop), also any gcc version not support it at some platform
#if defined( __GNUC__ )
#pragma pack()
#else
#pragma pack(pop)
#endif


///**
// * Set global data start address at the end of the SGL "user program and data" area.
//**/
//#define SC_GLOBAL_DATA_ADR (0x060C0000 - sizeof(sc_global_t))
/**
 * Set global data start address at the end of LRAM.
**/
#define SC_GLOBAL_DATA_ADR (0x00300000 - sizeof(sc_global_t))




/**
 * Used by Fast Data Link (FDL).
 * It would be better to put this in a separate header file, but ...
 * Note: FDL_MAGIC1 is a number incremented on a new revision.
 *       FDL_MAGIC2 is a random number.
**/
#define FDL_MAGIC1 0x03
#define FDL_MAGIC2 0x50



#endif //SATCOM_GLOBAL_DATA_H

