/*****************************************************************************
 SOURCE NAME : COM.H
 header of COM.CC
 part of the Atari File Designer (AFD) source files
 for the Communication module.
------------------------------------------------------------------------------
 Written by Philippe VUILLERME (c) PVBest 2001
 Based on "RS-232 Interrupts The C Way" by Mark R. Nelson (c)1990
------------------------------------------------------------------------------
MAIN FEATURES:
   This file contains the structure definitions, constants,
   and function prototypes needed to use the RS-232
   interface code supplied in COM.C.  It should be included
   by any routine using COM.C procedures.

******************************************************************************/

/*--- Definition des types --------------------------------------------*/
#define FALSE 0
#define TRUE 1

#define UBYTE unsigned char
#define BYTE signed char
#define UWORD unsigned int

/************************************************************************
 * This group of defines creates all the definitions used
 * to access registers and bit fields in the 8250 UART.
 * While the names may not be the best, they are the ones
 * used in the 8250 data sheets, so they are generally not
 * changed.
 */
#define RBR              0    /* Read Only / Receive buffer register */
#define THR              0    /* Write Only /Transmit holding reg.   */
#define IER              1    /* Interrupt Enable reg.   */
#define IIR              2    /* Interrupt ID register   */
#define LCR              3    /* Line control register   */
#define MCR              4    /* Modem control register  */
#define LSR              5    /* Line Status register    */
#define MSR              6    /* Modem Status register   */
#define DLL              0    /* Divisor latch LSB       */
#define DLM              1    /* Divisor latch MSB       */

#define IER_RX_DATA      1    /* xxxx 0001 Enable RX interrupt bit */
#define IER_TX_THRE      2    /* xxxx 0010 Enable TX interrupt bit */
#define IER_LINE_STATUS  4    /* xxxx 0100 Enable Line Stat int bit */
#define IER_MODEM_STATUS 8    /* xxxx 1000 Enable Modem Stat int bit */

#define IIR_MODEM_STATUS 0    /* 0000 Modem stat. interrupt ID*/
#define IIR_TRANSMIT     2    /* 0010 Transmit interrupt ID   */
#define IIR_RECEIVE      4    /* 0100 Receive interrupt ID    */
#define IIR_LINE_STATUS  6    /* 0110 Received Line stat. int. ID */

#define LCR_DLAB         0x80 /* 1xxx xxxx Divisor access bit      */

#define LCR_EVEN_PARITY  0x08  /* 0xx0 1xxx Set parity 'E' bits     */
#define LCR_ODD_PARITY   0x18 /* 0xx1 1xxx Set parity 'O' bits     */
#define LCR_NO_PARITY    0    /* 0xx0 0xxx Set parity 'N' bits     */

#define LCR_1_STOP_BIT   0    /* 0xxx x0xx Bits to set 1 stop bit  */
#define LCR_2_STOP_BITS  4    /* 0xxx x1xx Bits to set 2 stop bits */

#define LCR_5_DATA_BITS  0    /* 0xxx xx00 Bits to set 5 data bits */
#define LCR_6_DATA_BITS  1    /* 0xxx xx01 Bits to set 6 data bits */
#define LCR_7_DATA_BITS  2    /* 0xxx xx10 Bits to set 7 data bits */
#define LCR_8_DATA_BITS  3    /* 0xxx xx11 Bits to set 8 data bits */

#define MCR_DTR          1    /* Bit to turn on DTR      */
#define MCR_RTS          2    /* Bit to turn on RTS      */
#define MCR_OUT1         4    /* Bit to turn on OUT1     */
#define MCR_OUT2         8    /* Bit to turn on OUT2     */

#define LSR_OVERRUN    2   /* xxxx xx1x Overrun Error (rcvd data unread)*/
#define LSR_FRAMING    8   /* xxxx 1xxx Framing Error (stop bit) */

/**********************************************************************
 * This structure defines the 256 byte buffer used for
 * I/O buffers by the COM routines.  By using a buffer
 * size of 256 bytes, updating the indices is simplified.
 */
typedef struct {
                 UBYTE buffer[260];
                 unsigned int data_index;                 
                 unsigned int lenght;
                 UBYTE checksum;
                 UBYTE ctrl;
                 UBYTE error;
               } BUFFER;

/**********************************************************************
 * The fully qualified function prototypes.  All of these
 * routines actually reside in COM.C
 */

int port_open(void);

void port_set( long speed,
               char parity,
               int data,
               int stopbits );

void port_close(void);

/*********************************************************************/

UBYTE  port_sendcommand();
UBYTE  port_getframe();
UBYTE  port_sendframe();
void message_display();
void delais_cycle(long int duree);
void display_ack_code();
void raz_in_buffer();
void raz_out_buffer();
void port_save();
void port_display();
void port_restore();
int  image_file_save();
void transfer_file_in_buffer();
void sector_to_file_transfer();
void write_bad_sector(void);
void display_status_line();
int  drive_status_request();

int  Read_Sector_To_PC_File();
int  Init_Com_Port();
int  Exit_Com_Port();
void Display_Error_Log(int fin);
int  PC_File_Writing(char *seldef);
int  Display_Atari_DriveStatus();
int  Load_Memory_With_PC_File();
int  Compare_Atari_Disk_To_Memory();
int  Format_Atari_Disk();
int  Write_Sector_From_PC_File();
int  Read_Verify_Atari_Disk();


/********************************************************************
 * These are the standard UART addresses and interrupt
 * numbers for the two IBM compatible COM ports.
 */
#define COM1_UART         0x3f8
#define COM1_INTERRUPT    12
// COM1 = IRQ4
#define COM2_UART         0x2f8
#define COM2_INTERRUPT    11
// COM2 = IRQ3

/*******************************************************************
 * Color for AFD and structure CMDTEXT
 */

#define NOIR            00 // 0x00
#define BLEU            01 // 0x01
#define VERT            02 // 0x02
#define TURQUOISE       03 // 0x03
#define ROUGE           04 // 0x04
#define VIOLET          05 // 0x05
#define MARRON          06 // 0x06
#define GRIS_CLAIR      07 // 0x07
#define GRIS_FONCE      08 // 0x08
#define BLEU_CLAIR      09 // 0x09
#define VERT_CLAIR      10 // 0x0A
#define TURQUOISE_CLAIR 11 // 0x0B
#define ROUGE_CLAIR     12 // 0x0C
#define VIOLET_CLAIR    13 // 0x0D
#define JAUNE           14 // 0x0E
#define BLANC           15 // 0x0F

typedef struct {
                 unsigned char fxposX;
                 unsigned char fxposY;
                 char fxtext[40];
               } CMDTEXT;


