/* SDLTRS version Copyright (c): 2006, Mark Grebe */

/* Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
*/
/* Copyright (c) 2000, Timothy Mann */
/* $Id: trs_uart.h,v 1.2 2008/06/26 04:39:56 mann Exp $ */

/* This software may be copied, modified, and used for any purpose
 * without fee, provided that (1) the above copyright notice is
 * retained, and (2) modified versions are clearly marked as having
 * been modified, with the modifier's name and the date included.  */

/*
   Modified by Mark Grebe, 2006
   Last modified on Wed May 07 09:12:00 MST 2006 by markgrebe
*/

/*
 * Emulation of the Radio Shack TRS-80 Model I/III/4/4P serial port.
 */

#include <errno.h>
#include "trs.h"
#include "trs_hard.h"

extern void trs_uart_init(int reset_button);
extern int trs_uart_check_avail();
extern int trs_uart_modem_in();
extern void trs_uart_reset_out(int value);
extern int trs_uart_switches_in();
extern void trs_uart_baud_out(int value);
extern int trs_uart_status_in();
extern void trs_uart_control_out(int value);
extern int trs_uart_data_in();
extern void trs_uart_data_out(int value);
extern char trs_uart_name[FILENAME_MAX];
extern int trs_uart_switches;

#define TRS_UART_MODEM    0xE8 /* in */
#define TRS_UART_RESET    0xE8 /* out */
#define TRS_UART_SWITCHES 0xE9 /* in, model I only */
#define TRS_UART_BAUD     0xE9 /* out */
#define TRS_UART_STATUS   0xEA /* in */
#define TRS_UART_CONTROL  0xEA /* out */
#define TRS_UART_DATA     0xEB /* in/out */

/* Bits in TRS_UART_MODEM port */
#define TRS_UART_CTS      0x80
#define TRS_UART_DSR      0x40
#define TRS_UART_CD       0x20
#define TRS_UART_RI       0x10
#define TRS_UART_RCVIN    0x02

/* Bits in TRS_UART_BAUD port */
#define TRS_UART_SNDBAUD(v) (((v)&0xf0)>>4)
#define TRS_UART_RCVBAUD(v) (((v)&0x0f)>>0)
#define TRS_UART_50       0x00
#define TRS_UART_75       0x01
#define TRS_UART_110      0x02
#define TRS_UART_134      0x03
#define TRS_UART_150      0x04
#define TRS_UART_300      0x05
#define TRS_UART_600      0x06
#define TRS_UART_1200     0x07
#define TRS_UART_1800     0x08
#define TRS_UART_2000     0x09
#define TRS_UART_2400     0x0a
#define TRS_UART_3600     0x0b
#define TRS_UART_4800     0x0c
#define TRS_UART_7200     0x0d
#define TRS_UART_9600     0x0e
#define TRS_UART_19200    0x0f
#define TRS_UART_BAUD_TABLE \
  { 50.0, 75.0, 110.0, 134.5, 150.0, 300.0, 600.0, 1200.0, 1800.0, \
    2000.0, 2400.0, 3600.0, 4800.0, 7200.0, 9600.0, 19200.0 }

/* Bits in TRS_UART_STATUS port */
#define TRS_UART_RCVD     0x80
#define TRS_UART_SENT     0x40
#define TRS_UART_OVRERR   0x20
#define TRS_UART_FRMERR   0x10
#define TRS_UART_PARERR   0x08

/* Bits in TRS_UART_CONTROL port */
#define TRS_UART_EVENPAR  0x80
#define TRS_UART_WORDMASK 0x60
#define TRS_UART_WORD5    0x00
#define TRS_UART_WORD6    0x40
#define TRS_UART_WORD7    0x20
#define TRS_UART_WORD8    0x60
#define TRS_UART_WORDBITS(v) (((v)&TRS_UART_WORDMASK)>>5)
#define TRS_UART_WORDBITS_TABLE { 5, 7, 6, 8 }
#define TRS_UART_STOP2    0x10
#define TRS_UART_NOPAR    0x08
#define TRS_UART_NOTBREAK 0x04
#define TRS_UART_DTR      0x02 /* mislabelled as RTS in Model I manual */
#define TRS_UART_RTS      0x01 /* mislabelled as DTR in Model I manual */
