/* 
 * 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_DEBUGGER_COMMON_H
#define SATCOM_DEBUGGER_COMMON_H


//----------------------------------------------------------------------
// Saturn debugger common stuff.
// All the common stuff between Saturn and PC are gathered in a
//  structure shared between Saturn and PC.
// 
//----------------------------------------------------------------------


// 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

/**
 * SatCom debugger version.
 * Format is (Major.Minor), and minor is ignored during version checking on PC.
 * If Saturn/PC versions mismatch, the debugger will throw an error (better than crashing).
 *
 * Versions history:
 *  2010/10/15, Ver 5.1: added fileio_size to debugger structure.
 *  2010/09/10, Ver 5.0: gathered "rw_complete" and "prompt_complete" flag into "proc_complete" flag.
 *  2010/09/09, Ver 4.2: added version field to debugger structure.
**/
#define SCD_VERSION_MAJOR 5
#define SCD_VERSION_MINOR 1

/**
 * Note : the order of the elements in the structure is
 * important, so you shouldn't change it !
**/
#define SCD_CIRCBUFF_MAXSIZE 65536
#define SCD_CIRCBUFF_SIZE    4096
#define SCD_PROMPT_SIZE      256

typedef struct _scd_data_t
{
    /* Circular buffer R/W pointers (address is relative from the begining of circular buffer). */
    unsigned short readptr;
    unsigned short writeptr;

    /* Read until here when polling (4 bytes).             */
    /*-----------------------------------------------------*/

    /* Set to 1 by the PC in order to indicate that debugger is ON. */
    unsigned char dbg_on;
    /* Indicate that the file read/write from PC is completed. */
    /* Indicates that the prompt answer has been sent from PC. */
    unsigned char proc_complete;
    /* Reserved bytes for future stuff, and for 4 bytes alignment. */
    unsigned char reserved1[2];
    /* Set to the transfered size (in bytes) after a file request with PC. */
    unsigned long fileio_size;


    /* Structure/debugger version (used for version checking). */
    unsigned char version[2];
    /* This structure size (used for version checking). */
    unsigned short structsz;
    /* Reserved bytes for future stuff, and for 4 bytes alignment. */
    unsigned char reserved2[4];


    /* Circular buffer internal. */
    unsigned long buffer_size;
    unsigned long start_address;
    unsigned long end_address;

    /* Read until here when init (32 bytes).               */
    /*-----------------------------------------------------*/


    /* Circular buffer used when reading/writing debug data. */
    unsigned char rbuffer[SCD_CIRCBUFF_SIZE];

    /* Place where to write prompt's answer. */
    unsigned char prompt_answer[SCD_PROMPT_SIZE];
} scd_data_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


/**
 * Absolute data:
 *  - address to scd_data_t debugger data.
 *  - magic value in order to check if the debugger is running.
 * Their addresses are defined in sc_global_data.h
**/
#include "../sc_global_data.h"
#define SCD_ABS_SIZE  8
#define SCD_ABS_MAGIC 0xCEC576EC




/**
 * Circular buffer:
 * Generic data format: 
 *  UCHAR        len
 *  UCHAR        type
 *  UCHAR[len-2] data
**/

/**
 * Type values.
 * It can be used to add new commands, such as string request,
 * file data request, etc.
**/

/*
 * Debug message output:
 * Data format:
 *  UCHAR         len
 *  UCHAR         type (='M')
 *  UCHAR[len-2]  debug_message_string
 */
#define SCD_TYPE_MSG 'M'
/*
 * Message prompt:
 * Data format:
 *  UCHAR         len
 *  UCHAR         type (='P')
 *  UCHAR[len-2]  prompt_message_string
 */
#define SCD_TYPE_PROMPT 'P'

/*
 * File input/output request:
 * Data format:
 *  UCHAR         len
 *  UCHAR         type (='R', 'W', 'A', 'S')
 *  ULONG         file_offset
 *  ULONG         rw_address
 *  ULONG         rw_len
 *  UCHAR[len-14] file_name
 */
#define SCD_TYPE_RFILE 'R'
#define SCD_TYPE_WFILE 'W'
#define SCD_TYPE_AFILE 'A'
#define SCD_TYPE_SFILE 'S'



/*--- Saturn-side functions. ---*/
/**
 * Init circular buffer.
**/
void scd_cbinit(scd_data_t* d);
/**
 * Write data in circular buffer.
 * It modifies circular buffer's write pointer.
**/
void scd_cbwrite(scd_data_t* d, unsigned char* data, unsigned long datalen);





#endif // SATCOM_DEBUGGER_COMMON_H

