#include    <stdio.h>
#include    <stdlib.h>
#include    <ctype.h>
#include    "defs.h"
#ifdef  TOWNS
#include    <time.h>
#include    <egb.h>
#include    <fmcfrb.h>
#include    <msdos.cf>
#include    <rs.h>
#endif

#define	MENU_MAX    11

#define	SJIS	    0
#define	EUC	    1
#define JIS	    2

#define	BUF_SIZ	4096		/* looking RSBLIB.ASM */

extern UCHAR *cvram;
extern char  *SPCSTR;
extern int   Line_no;
extern short ReWrt_flg;
extern int   TrmCol;
extern int   DspCnt;

extern char *kanji_chk();
extern BOOL Input();
extern void Dmy_form();
extern void wrtstr();
extern void Soft_Timer();
extern int  kbhit();
extern int  Get_key();
extern void BakPut();
extern void Dsp_vram();
extern int  Sel_menu();
extern void Dsp_free();
extern int  strlen();
extern void strcpy();
extern void Dsp_vram_flash();
extern void BP_Term_ENQ();
extern void BP_DLE_Seen();
extern void ins_chr();
extern void Set_vram();
extern void Del_buf();
extern void Cur_btm_file();
extern int  File_sel();
extern void Cut_buf();
extern int  Undo_cpy();
extern void memset();
extern int  Con_init();
extern void CD_menu();
extern void Con_end();
extern void Dsp_PFKEY();
/* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
extern int cd_m_stat;
/* ---------------------------------------------------------- */

       int  port=0,bps=0x05;
static int  mode=0x11,brk=0,swait=0,KanMod=0;
static int  Down_flg=FALSE,Line_flg=FALSE,Line_X=0;
static FILE *Down_fp=NULL;
static int  Flaing=FALSE;
static unsigned brktime[]={ 25,50,100,200 };
static int  swtime[]={1,5000,10000,20000,50000 };

static	char	para[20];
static	char	buffer[65536];       /*    受信バッファ    */

static int      cons_od1=0;
static int      cons_od2=0;
static int      cons_kan=0;
static int      cons_sft=FALSE;
static int      term_kan=0;
static int      term_sft=FALSE;
/* --------------- RSB for L20  93. 1.20 Pumpkin ------------- */
static int      neednt_close=FALSE;
/* ---------------------------------------------------------- */

void	send_aux();
void	send_str();

/*--------------- Fast Down 92/03/23 Pumpkin  ----------------*/
void	send_one_flush();
void	send_one(int ch);
/* ---------------------------------------------------------- */

/*------ Bplus Xmodem/1024/SUM/CRC 92/02/17 Pumpkin  ---------*/
#include    "xmodem2.c"
/******
#include    "xmodem.c"
*****/
/*------------------------------------------------------------*/

#ifdef	TOWNS
/* --------------- RSB for L20  92.12.27 Pumpkin ------------- */
extern	char *getCMOSpara();
void RSB_start()
{

    char *rsb_para;

    rsb_para = getCMOSpara();
    RSB_Init();

    if (rsb_para == NULL) return;
    mode = rsb_para[0x63];     		/*  通信モード           */
    bps  = rsb_para[0x62];           	/*  ボーレート　         */
}
/* ---------------------------------------------------------- */
/******
void	Test_RSB_mode()
{
    static short flg=FALSE;
    RSB_PARA rsb_para;

    if ( flg != FALSE )
	return;
    flg = TRUE;

    RSB_Rdpara(port,(char *)&rsb_para);
    mode = rsb_para.mode;
    bps  = rsb_para.baud;
}
*****/
void	RSB_init(pt,md,bp)
int	pt,md,bp;
{

    WORD(buffer+0) = BUF_SIZ;
    BYTE(para+0) = md;
    BYTE(para+1) = bp;
    DWORD(para+2) = (unsigned int)buffer;
    WORD(para+6) = 0x14;
    WORD(para+8) = 0;
    WORD(para+10) = 0;
    DWORD(para+12) = 0;
    WORD(para+16) = 0;

/* --------------- RSB for L20  93. 1.20 Pumpkin ------------- */
    if (neednt_close==FALSE) {
	    RSB_Dtr(pt,1);		/* DTR信号の保持 */
	    RSB_Close(pt);
    }
/* ---------------------------------------------------------- */
/******
    RSB_Dtr(pt,1);		/+ DTR信号の保持 +/
    RSB_Close(pt);
******/
    RSB_Setpara(pt, para);
    RSB_Open(pt);
}
#endif

/*********************************************************
012345678901234567890123456789012345678901234567890123
          1         2         3         4         5
           *     *     *     *     *     *     *
ﾎﾟ-ﾄNo.    0     1     2     3     4
速  度     300   600   1200  2400  4800  9600  19200
ﾋﾞｯﾄ長     7ﾋﾞｯﾄ 8ﾋﾞｯﾄ
ﾊﾟﾘﾃｨ-     無し  偶数  奇数
ｽﾄｯﾌﾟﾋﾞｯﾄ  1ﾋﾞｯﾄ 2ﾋﾞｯﾄ
ﾌﾛ-制御    無し  XON/OFF
ﾌﾞﾚ-ｸ時間  250ms 500ms 1sec  2sec
文字色     青色  赤色  紫色  緑色  水色  黄色  白色
文字制御   有り  無し  表示
改行待ち   無し  50ms  100ms 200ms 500ms
漢字コ−ド SJIS  EUC   JIS
**********************************************************/
void	Set_RSB_mode(x,y)
int	x,y;
{
    unsigned int ec;
    int    i,n,no,ch,cl,fg;
    char   tmp[160];
    static int  pt,bp,bit,party,stop,xfls,br,col,ctl,swt,km;
    static struct {
	int     *flg_mn;
	int     max_mn;
	char    *ttl_mn;
	char    *sub_mn[8];
    } MODE_menu[]={
{ &pt,5,"ﾎﾟ-ﾄNo.   ","0","1","2","3","4" },
{ &bp,7,"速  度    ","300","600","1200","2400","4800","9600","19200"},
{ &bit,2,"ﾋﾞｯﾄ長    ","7ﾋﾞｯﾄ","8ﾋﾞｯﾄ" },
{ &party,3,"ﾊﾟﾘﾃｨ-    ","なし","奇数","偶数" },
{ &stop,2,"ｽﾄｯﾌﾟﾋﾞｯﾄ ","1ﾋﾞｯﾄ","2ﾋﾞｯﾄ" },
{ &xfls,2,"ﾌﾛ-制御   ","なし","あり" },
{ &br,4,"ﾌﾞﾚ-ｸ時間 ","250ms","500ms","1sec","2sec" },
{ &col,7,"文字色    ","青色","赤色","紫色","緑色","水色","黄色","白色" },
{ &ctl,3,"文字制御  ","有り","無し","表示" },
{ &swt,5,"改行WAIT  ","無し","50ms","100ms","200ms","500ms" },
{ &km, 3,"漢字ｺ-ﾄ   ","SJIS","EUC","JIS" }
    };

    pt = port;
    bp = bps;
    bit = ((mode & 0x01) == 0 ? 0 : 1);
    party = ((mode & 0x02) == 0 ? 0 : ((mode & 0x04) == 0 ? 1 : 2));
    stop = ((mode & 0x08) == 0 ? 0 : 1);
    xfls = ((mode & 0x10) == 0 ? 0 : 1);
    br = brk;
    col = TrmCol-1;
    ctl = DspCnt;
    swt = swait;
    km = KanMod;

    Dmy_form(tmp,54,0x98,0x95,0x99); 
    wrtstr(tmp,x,y,0x07);
    Dmy_form(tmp,54,0x96,0x20,0x96); 
    for ( i = 0 ; i < MENU_MAX ; i++ )
	wrtstr(tmp,x,y+i+1,0x07);
    Dmy_form(tmp,54,0x9A,0x95,0x9B); 
    wrtstr(tmp,x,y+MENU_MAX+1,0x07);
    x += 2; y += 1;

    for ( fg = FALSE,no = 0 ; fg == FALSE ; ) {
	for ( i = 0 ; i < MENU_MAX ; i++ ) {
	    if ( i == no ) cl = 0x14; else cl = 0x07;
	    wrtstr(MODE_menu[i].ttl_mn,x,y+i,cl);
	    for ( n = 0 ; n < MODE_menu[i].max_mn ; n++ ) {
		if ( n == *(MODE_menu[i].flg_mn) ) cl = 0x14; else cl = 0x07;
		wrtstr(MODE_menu[i].sub_mn[n],x+11+n*6,y+i,cl);
	    }
	}
	ch = Get_key(&ec);
	if ( ch == '\x1B' || ec == 0x7200 )
	    goto ENDOF;
        else if ( ch == '\x0D' || ec == 0x7300 )
	    break;	
	else if ( ch == ' ' || ch == '\x1F' ) {
	    if ( ++no >= MENU_MAX )
		no = 0;
	} else if ( ch == '\x08' || ch == '\x1E' ) {
	    if ( --no < 0 )
		no = MENU_MAX-1;
	} else if ( ch == '\x1C' ) {
	    *(MODE_menu[no].flg_mn) += 1;
	    if ( *(MODE_menu[no].flg_mn) >= MODE_menu[no].max_mn )
		*(MODE_menu[no].flg_mn) = 0;
	} else if ( ch == '\x1D' ) {
	    *(MODE_menu[no].flg_mn) -= 1;
	    if ( *(MODE_menu[no].flg_mn) < 0 )
		*(MODE_menu[no].flg_mn) = MODE_menu[no].max_mn - 1;
	}
    }
    mode = 0;
    if ( bit != 0 ) mode |= 0x01;
    if ( party == 1 ) mode |= 0x02;
    if ( party == 2 ) mode |= 0x06;
    if ( stop != 0 ) mode |= 0x08;
    if ( xfls != 0 ) mode |= 0x10;
    if ( pt != port ) {
	RSB_Close(port);
	port = pt;
    }
    bps = bp;
    brk = br;
    TrmCol = col+1;
    DspCnt = ctl;
    swait = swt;
/* --------------- RSB for L20  93. 1.20 Pumpkin ------------- */
    neednt_close=TRUE;
/* ---------------------------------------------------------- */
    RSB_init(port,mode,bps);
/* --------------- RSB for L20  93. 1.20 Pumpkin ------------- */
    neednt_close=FALSE;
/* ---------------------------------------------------------- */
    KanMod = km;
ENDOF:
#ifdef	TOWNS
    Dsp_vram_flash();
#endif
    Dsp_vram(cvram);
    Dsp_free();
}

/**********************************
	RS232C Charcter Terminal
***********************************/

void	send_aux(int ch)
{
    unsigned st;
    time_t   s,n;

    if ( RSB_Send(port,ch,&st) != 0 ) {
	time(&s);
	do {
	    if ( RSB_Send(port,ch,&st) == 0 )
		break;
	    time(&n);
	} while ( (n - s) < 5 );
    }
}
int	recv_aux()
{
    int      ch;
    unsigned st;

    if ( RSB_Receive(port,&ch,&st) == 0 )
	return (ch & 0xff);
    else
	return ERR;
}
void	send_str(char *str,int n)
{
    while ( n-- > 0 )
	send_aux(*(str++));
}
int	jis2sjis(int chr)
{
    int    hi,lo;

    hi = (chr >> 8) & 0xff;
    lo = chr & 0xff;
    if ( (hi & 1) != 0 )
        lo += 0x1F;
    else
        lo += 0x7D;
    if ( lo >= 0x7F )
        lo++;
    hi = (hi - 0x21 >> 1) + 0x81;
    if ( hi > 0x9F )
        hi += 0x40;
    return (hi << 8 | lo);
}
int	sjis2jis(int cd)
{
    int    hi,lo;

    hi = (cd >> 8) & 0xff;
    lo = cd & 0xff;
    hi -= ( hi <= 0x9f) ? 0x71 : 0xb1;
    hi = hi * 2 +1;
    if ( lo > 0x7f )
        lo--;
    if ( lo >= 0x9e ) {
        lo -= 0x7d;
        hi++;
    }
    else
        lo -= 0x1f;
    return (hi << 8 | lo);
}
int	han2zen(int code)
{
    static unsigned short int ank_tbl[]={
        0x8140,0x8149,0x8168,0x8194,0x8190,0x8193,0x8195,0x8166,
        0x8169,0x816A,0x8196,0x817B,0x8143,0x817C,0x8144,0x815E,
        0x824F,0x8250,0x8251,0x8252,0x8253,0x8254,0x8255,0x8256,
        0x8257,0x8258,0x8146,0x8147,0x8183,0x8181,0x8184,0x8148,
        0x8197,0x8260,0x8261,0x8262,0x8263,0x8264,0x8265,0x8266,
        0x8267,0x8268,0x8269,0x826A,0x826B,0x826C,0x826D,0x826E,
        0x826F,0x8270,0x8271,0x8272,0x8273,0x8274,0x8275,0x8276,
        0x8277,0x8278,0x8279,0x816D,0x818F,0x816E,0x814F,0x8151,
        0x8166,0x8281,0x8282,0x8283,0x8284,0x8285,0x8286,0x8287,
        0x8288,0x8289,0x828A,0x828B,0x828C,0x828D,0x828E,0x828F,
        0x8290,0x8291,0x8292,0x8293,0x8294,0x8295,0x8296,0x8297,
        0x8298,0x8299,0x829A,0x816F,0x8162,0x8170,0x8150,0x85A1 };
    static unsigned short int kana_tbl[]={
        0x8140,0x8142,0x8175,0x8176,0x8141,0x8145,0x8392,0x8340,
        0x8342,0x8344,0x8346,0x8348,0x8383,0x8385,0x8387,0x8362,
        0x815B,0x8341,0x8343,0x8345,0x8347,0x8349,0x834A,0x834C,
        0x834E,0x8350,0x8352,0x8354,0x8356,0x8358,0x835A,0x835C,
        0x835E,0x8360,0x8363,0x8365,0x8367,0x8369,0x836A,0x836B,
        0x836C,0x836D,0x836E,0x8371,0x8374,0x8377,0x837A,0x837D,
        0x837E,0x8380,0x8381,0x8382,0x8384,0x8386,0x8388,0x8389,
        0x838A,0x838B,0x838C,0x838D,0x838F,0x8393,0x814A,0x814B };

    code &= 0xFF;
    if ( 0x20 <= code && code <= 0x7F )
        return ank_tbl[code-0x20];
    else if ( 0xA0 <= code && code <= 0xDF )
        return kana_tbl[code-0xA0];
    else
        return 0x85A1;
}
void	send_shift(int sw)
{
    if ( sw != term_sft ) {
        term_sft = sw;
        send_str((sw == FALSE ? "\033(J":"\033$@"),3);
    }
}
void	Send_char(int ch)
{
    ch &= 0xFF;

    switch(KanMod) {
    case SJIS:
        send_aux(ch);
        break;

    case JIS:
        if ( term_kan != 0 ) {
            if ( iskanji2(ch) ) {
                ch |= (term_kan << 8);
                ch = sjis2jis(ch);
                send_shift(TRUE);
                send_aux(ch >> 8);
                send_aux(ch & 0xFF);
                term_kan = 0;
                break;

            } else {
                term_kan = sjis2jis(han2zen(term_kan));
                send_shift(TRUE);
                send_aux(term_kan >> 8);
                send_aux(term_kan & 0xFF);
                term_kan = 0;
            }
        }

        if ( iskanji(ch) )
            term_kan = ch;
        else if ( (ch & 0x80) != 0 ) {
            ch = sjis2jis(han2zen(ch));
            send_shift(TRUE);
            send_aux(ch >> 8);
            send_aux(ch & 0xFF);
        } else {
            send_shift(FALSE);
            send_aux(ch);
        }
        break;

    case EUC:
        if ( term_kan != 0 ) {
            if ( iskanji2(ch) ) {
                ch |= (term_kan << 8);
                ch = sjis2jis(ch);
                ch |= 0x8080;
                send_aux(ch >> 8);
                send_aux(ch & 0xFF);
                term_kan = 0;
                break;

            } else {
                term_kan = sjis2jis(han2zen(term_kan));
                term_kan |= 0x8080;
                send_aux(term_kan >> 8);
                send_aux(term_kan & 0xFF);
                term_kan = 0;
            }
        }

        if ( iskanji(ch) )
            term_kan = ch;
        else if ( (ch & 0x80) != 0 ) {
            ch = sjis2jis(han2zen(ch));
            ch |= 0x8080;
            send_aux(ch >> 8);
            send_aux(ch & 0xFF);
        } else
            send_aux(ch);
        break;
    }

    if ( ch == 0x0D && swait != 0 )
	Soft_Timer(swtime[swait]);
}

void	Send_cons(int ch)
{
    ch &= 0xFF;

    switch(KanMod) {
    case SJIS:
        send_one(ch);
        break;

    case JIS:
        ch &= 0x7F;
        if ( cons_kan != 0 ) {
            if ( cons_sft == FALSE || ch < ' ' ) {
                send_one(cons_kan);
                cons_kan = 0;
            } else {
                cons_od2 = cons_od1;
                cons_od1 = ch;
                ch |= (cons_kan << 8);
                ch &= 0x7F7F;
                ch = jis2sjis(ch);
                send_one(ch >> 8);
                send_one(ch & 0xFF);
                cons_kan = 0;
                break;
            }
        }

        if ( ch < ' ' ) {
            cons_sft = FALSE;
            send_one(ch);
        } else if ( cons_sft != FALSE ) {
            cons_kan = ch;
        } else {
            send_one(ch);
        }

        if ( (ch == 'B' || ch == '@') &&
             cons_od1 == '$' && cons_od2 == '\033' )
            cons_sft = TRUE;
        else if ( (ch == 'B' || ch == 'J') &&
             cons_od1 == '(' && cons_od2 == '\033' )
            cons_sft = FALSE;

        cons_od2 = cons_od1;
        cons_od1 = ch;
        break;

    case EUC:
        if ( cons_kan != 0 ) {
            if ( (ch & 0x80) != 0 ) {
                ch |= (cons_kan << 8);
                ch &= 0x7F7F;
                ch = jis2sjis(ch);
                send_one(ch >> 8);
                send_one(ch & 0xFF);
                cons_kan = 0;
                break;
            }
            send_one(cons_kan);
            cons_kan = 0;
        }
        if ( (ch & 0x80) != 0 )
            cons_kan = ch;
        else
            send_one(ch);
        break;
    }
}
void	recv_chk()
{
    int	     ch,len;
    unsigned st;

    RSB_Read(port,&len);
    if ( len <= 0 )
	return;

    while ( len > 0 ) {
	if ( RSB_Receive(port,&ch,&st) != 0 )
	    break;

        if ( ch == 0x05 ) { 	/* ENQ Then B Plus Protocol Start ? */
	    BP_Term_ENQ();
	    break;
	} if ( ch == 0x10 ) {	/* DLE Then B Plus Protocol Start ? */
	    BP_DLE_Seen();
	    break;
	} else
	    Send_cons(ch);

	len--;
    }

    Dsp_vram(cvram);
}
void    Dsp_RSBKEY()
{
    int     i;
    UCHAR   tmp[200];

    static char *menu[]={
	" 設定 "," 入力 "," AUTO ","      ","CD演奏",
	"ダウン","アップ"," ﾌﾞﾚ-ｸ"," 終了 "," 中断 " };

    for ( i = 0 ; i < 10 ; i++ )
        wrtstr(menu[i],i*7+(i/5),MENU_Y,0x16);

    memset(tmp,' ',47); tmp[48] = '\0';
    wrtstr(tmp,71,MENU_Y,0x16);
}
void	Down_load()
{
    int    x,y;
    char   tmp[80];
    static int  no=ERR;
/* --------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin --------------- */
    static char *menu[]={
	"1 編集バッファに (新規)    ",
	"2 編集バッファに (追加)    ",
	"",
	"3 ファイルに(新規/追加)    ",
	"",
	"4 X-MODEM/SUMでダウンする  ",
	"5 X-MODEM/SUMでダウン(高速)",
	"6 X-MODEM/CRCでダウン      ",
	"7 Y-MODEM/1024でダウン     ",
	"8 Y-MODEM-gでダウン        ",
	NULL };
/**********
    static char *menu[]={
	"1 編集バッファに (新規) ",
	"2 編集バッファに (追加) ",
	"",
	"3 ファイルに(新規/追加) ",
	"",
	"4 X-Modemでダウンする   ",
	"5 X-Modemでダウン(高速) ",
	NULL };
*********/
/* ---------------------------------------------------------------- */

    wrtstr(SPCSTR,30,1,0x1F);
    if ( Down_flg != FALSE || Down_fp != NULL ) {
	if ( Down_fp != NULL ) {
	    fputc('\x1A',Down_fp);
	    fclose(Down_fp);
	    Down_fp = NULL;
	}
/* --------------- Fast Down 92/03/23 Pumpkin  ---------------- */
    	send_one_flush();
/* ------------------------------------------------------------ */
	Down_flg = FALSE;
	wrtstr("                  ",32,0,0x1F);
	return;
    }

    if ( Sel_menu(menu,25,2,&no) != FALSE )
	goto ENDOF;

    if ( no == 0 ) {
	Del_buf(0l,btm_ptr);
	top_ptr = lin_ptr = ent_ptr = btm_ptr = 0;
	Line_no = 0;
        Down_flg = TRUE;
    } else if ( no == 1 ) {
    	x = Cur_X; y = Cur_Y;
    	Cur_btm_file();
    	Cur_X = x; Cur_Y = y;
    	Down_flg = TRUE;
/* ------------------ Bug Fix  91.04.20 Pumpkin ----------------- */
	ent_ptr = btm_ptr;
/* -------------------------------------------------------------- */
    } else if ( no == 2 ) {
	tmp[0] = '\0';
	if ( Input(tmp,"ﾌｧｲﾙ名 ") != FALSE || File_sel(tmp) != FALSE )
	    goto ENDOF;
	if ( (Down_fp = fopen(tmp,"ab+")) == NULL ) {
	    wrtstr("(ファイルがオ−プン出来ません)",30,1,0x12);
	    goto ENDOF;
	}
/* --------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin --------------- */
    } else if ( no == 3 || no == 4 || no == 5 ) {
	tmp[0] = '\0';
	if ( Input(tmp,"ﾌｧｲﾙ名 ") != FALSE || File_sel(tmp) != FALSE )
	    goto ENDOF;
	if ( no == 4 ) Flaing = TRUE; else Flaing = FALSE;
	if ( no == 5 ) crcmode = TRUE; else crcmode = FALSE;
    	Dsp_vram(cvram);
        RSB_init(port,mode & 0xEF,bps);
	Down_Xmodem(tmp);
        RSB_init(port,mode,bps);
	goto ENDOF;
    } else if ( no == 6 || no == 7 ) {
	if ( no == 6 ) Flaing = FALSE; else Flaing = TRUE;
    	Dsp_vram(cvram);
        RSB_init(port,mode & 0xEF,bps);
	Down_Ymodem();
        RSB_init(port,mode,bps);
	goto ENDOF;
/*****
    } else if ( no == 3 || no == 4 ) {
	tmp[0] = '\0';
	if ( Input(tmp,"ﾌｧｲﾙ名 ") != FALSE || File_sel(tmp) != FALSE )
	    goto ENDOF;
	Flaing = (no == 3 ? FALSE : TRUE);
        RSB_init(port,mode & 0xEF,bps);
	Down_Xmodem(tmp);
        RSB_init(port,mode,bps);
	goto ENDOF;
*****/
/* -------------------------------------------------------------- */
    }
    wrtstr("ダウンロ−ド実行中",32,0,0x15);
ENDOF:
    Dsp_vram(cvram);
    Dsp_free();
}
void	Up_load()
{
    int	    ch,i,n,ct;
    unsigned int ec;
    FILE   *fp;
    char   tmp[256];
    char   *p;
    LONG   ptr,sz;
    static int  no=ERR;
/* ------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin ------------- */
    static char *menu[]={
	"1 編集バッファをアップ    ",
	"2 Undoバッファをアップ    ",
	"",
	"3 ファイルをアップする    ",
	"",
	"4 X-MODEM/SUMでアップする ",
	"5 X-MODEM/CRCでアップする ",
	NULL };
/*****
    static char *menu[]={
	"1 編集バッファをアップ  ",
	"2 Undoバッファをアップ  ",
	"",
	"3 ファイルをアップする  ",
	"",
	"4 X-Modemでアップする   ",
	NULL };
*****/
/* -------------------------------------------------------------- */

    wrtstr(SPCSTR,30,1,0x1F);
    if ( Sel_menu(menu,30,2,&no) != FALSE )
	goto ENDOF;

    if ( no == 0 ) {
	ptr = 0;
	sz = btm_ptr;
	n = i = 0;
    } else if ( no == 1 ) {
	ptr = 0;
	n = 0;
    } else if ( no == 2 ) {
	tmp[0] = '\0';
	if ( Input(tmp,"ﾌｧｲﾙ名 ") != FALSE || File_sel(tmp) != FALSE )
	    goto ENDOF;
	if ( (fp = fopen(tmp,"rb")) == NULL ) {
	    wrtstr("(ファイルがオ−プン出来ません)",30,1,0x12);
	    goto ENDOF;
	}
/* ------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin ------------- */
    } else if ( no == 3  || no == 4 ) {
/*****
    } else if ( no == 3 ) {
*****/
/* -------------------------------------------------------------- */
	tmp[0] = '\0';
	if ( Input(tmp,"ﾌｧｲﾙ名 ") != FALSE || File_sel(tmp) != FALSE )
	    goto ENDOF;
        RSB_init(port,mode & 0xEF,bps);
/* ------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin ------------- */
	if ( no == 3 ) {
		crcmode= FALSE;
	} else {
		crcmode= TRUE;
	}
	Dsp_vram(cvram);
/* -------------------------------------------------------------- */
	Up_Xmodem(tmp);
        RSB_init(port,mode,bps);
	goto ENDOF;
    }

    wrtstr("アップロ−ド実行中",32,1,0x14);
    Dsp_vram(cvram);
    for ( ct = 0 ; ; ct++ ) {
	if ( kbhit() != 0 ) {
	    ch = Get_key(&ec);
	    if ( ch == 0x1B || ec == 0x7200 )
		break;
	}
	if ( no == 0 ) {
	    if ( i >= n ) {
	        if ( sz <= 0 )
		    break;
		if ( sz < 256 ) n = sz; else n = 256;
		Cut_buf(tmp,n,ptr);
		ptr += n;
		sz -= n;
		i = 0;
	    }
	    ch = tmp[i++];
	} else if ( no == 1 ) {
	    if ( n <= 0 ) {
		if ( (n = Undo_cpy(tmp,256,ptr)) == 0 )
		    break;
		p = tmp;
		ptr += n;
	    }
	    ch = *(p++); n--;
	} else if ( no == 2 ) {
	    if ( (ch = fgetc(fp)) == EOF )
		break;
	}
	if ( ct > 50 ) {
	    recv_chk();
	    ct = 0;
        }
	if ( ch != '\x0A' && ch != '\x1A' )
	    Send_char(ch & 0xff);
    }
    if ( no == 3 )
	fclose(fp);
    wrtstr(SPCSTR,30,1,0x1F);
ENDOF:
    Dsp_vram(cvram);
    Dsp_free();
}
void	RSB_end()
{
/* --------------- RSB for L20  93. 1.20 Pumpkin ------------- */
    if ( cvram != NULL ) {
	neednt_close=TRUE;
        RSB_Close(port);
    }
/* ---------------------------------------------------------- */
/*********
    if ( cvram != NULL )
        RSB_Close(port);
********/
    wrtstr("    ",10,0,0x1F);
    free(cvram);
    cvram = NULL; 
}

/* ------------------ 1l Input 93/02/10 Pumpkin ---------------- */

extern void MOJI_input1Char(UCHAR *moji_area
		,unsigned int ky,int max_moji,int *lcur_x);


void	Line_edit(int ch,unsigned int ec,int *lcur_x)
{
    static unsigned char Line_tmp[81]={0};
    int	    len,i;
    char    tmp[81],tmp2[3];


    if ( Line_flg == FALSE ) {	/* 非一行入力状態 */
	if ( ch != 0xFFFF )
	    Send_char(ch & 0xff);
	if ( Line_X > 0 ) {
	    Line_X = 0;		/* 入力行の消去  */
	    *lcur_x=0;
	    memset(Line_tmp,' ',80); Line_tmp[80] = 0;
	    wrtstr(Line_tmp,0,MAX_Y2+OFF_Y,0);
	}
	return;
    }

    if ( ch == 0xFFFF ) {	/* 初期化  */
	Line_tmp[0]=0;
        Line_X = len = *lcur_x=0;
        goto jump;

    }

    if (Line_X == 0 && ch < ' ') {
	Send_char(ch & 0xff);
        goto jump;
    }

    MOJI_input1Char(Line_tmp,(unsigned int)ch,80,lcur_x);
    Line_X = len = strlen(Line_tmp);
    if ( ch == '\x0D' || ec == 0x7300 ) {	/* 送信 */
	for ( i = 0 ; i < len ; i++ ) Send_char(Line_tmp[i] & 0xff);
	Send_char(0x0D);
	Line_tmp[0]=0;
        Line_X = len = *lcur_x = 0;
    } else if ( ch == '\x0B' || ch == '\x1B' || ec == 0x7200) {
	Line_tmp[0]=0;
        Line_X = len = *lcur_x = 0;
    } else if ( ec == 0x4f04) {
        *lcur_x=0;
    } else if ( ec == 0x5104) {
	*lcur_x = strlen(Line_tmp);
    }

jump:
    strcpy(tmp,Line_tmp);		/* 文字表示 */
    memset(tmp+len,' ',81-len);
    tmp[80]=0;
    wrtstr(tmp,0,MAX_Y2+OFF_Y,0x1D);

    if (*lcur_x == len) {		/* カーソル表示 */
	tmp2[0] = 0xFE;
	tmp2[1] = 0x1B;
	tmp2[2] = 0;
	wrtstr(tmp2,*lcur_x,MAX_Y2+OFF_Y,0x1D);
    }else{
	if (iskanji(Line_tmp[*lcur_x])) {
		tmp2[0] = Line_tmp[*lcur_x];
		tmp2[1] = Line_tmp[*lcur_x+1];
		tmp2[2] = 0;
	}else{
		tmp2[0] = Line_tmp[*lcur_x];
		tmp2[1] = 0;
	}
	wrtstr(tmp2,*lcur_x,MAX_Y2+OFF_Y,0x17);
    }
    ReWrt_flg = (-2);

}
/*********
void	Line_edit(ch)
int     ch;
{
    int    i;
    static char tmp[180];

    if ( Line_flg == FALSE ) {
	if ( ch != 0xFFFF )
	    Send_char(ch & 0xff);
	if ( Line_X > 0 ) {
	    Line_X = 0;
	    memset(tmp,' ',80); tmp[80] = 0;
	    wrtstr(tmp,0,MAX_Y2+OFF_Y,0);
	    wrtstr(tmp,0,MAX_Y2+OFF_Y+1,0);
	}
	return;
    }
    if ( ch == 0x08 && Line_X > 0 ) {
	Line_X--;
	if ( &tmp[Line_X] != kanji_chk(tmp,Line_X) )
	   Line_X--;
    } else if ( Line_X == 0 && ch < ' ' ) {
	Send_char(ch & 0xff);
    } else if ( ch != 0x08 && ch != 0x1B && ch != 0xFFFF )
        tmp[Line_X++] = ch;
    if ( Line_X >= 158 || ch == 0x0D || ch == 0x1B ) {
	for ( i = 0 ; i < Line_X ; i++ )
	    Send_char(tmp[i] & 0xff);
	if ( Line_X >= 80 ) {
	    memset(tmp,' ',80); tmp[80] = 0;
	    wrtstr(tmp,0,MAX_Y2+OFF_Y+1,0);
	}
	Line_X = 0;
    }
    tmp[Line_X] = 0xFE; tmp[Line_X+1] = 0x1B;
    memset(tmp+Line_X+2,' ',164-Line_X);
    tmp[161] = 0;
    if ( Line_X < 80 )
        tmp[81] = '\0';
    wrtstr(tmp,0,MAX_Y2+OFF_Y,0x1D);
    ReWrt_flg = (-2);
}
********/
/* ---------------------------------------------------------- */

void	RSB_loop()
{
    int     ch,ef;
    int     Bk_X,Bk_Y;
    unsigned ec;
/* ------------------ 1l Input 93/02/10 Pumpkin ---------------- */
    int	lcur_x=0;
/* ---------------------------------------------------------- */

    wrtstr(SPCSTR,30,1,0x1F);
    if ( cvram == NULL ) {
/* --------------- RSB for L20  92.12.27 Pumpkin ------------- */
/*******
        Test_RSB_mode();
*******/
/* ---------------------------------------------------------- */
        RSB_init(port,mode,bps);
/* --------------- RSB for L20  93. 1.20 Pumpkin ------------- */
	neednt_close=FALSE;
/* ---------------------------------------------------------- */
    }
    if ( Con_init() != FALSE ) {
        RSB_Close(port);
	return;
    }
    wrtstr("通信",10,0,0x17);
    Dsp_RSBKEY();

/* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
    cd_m_stat = TRUE;
/* ---------------------------------------------------------- */
    for ( ef = FALSE ; ef == FALSE ; ) {
	recv_chk();
	for ( ; ; ) {
/* ------------------ 1l Input 93/02/10 Pumpkin ---------------- */
	    if ( Line_flg != FALSE ) {
	        Bk_X = Cur_X; Bk_Y = Cur_Y;
		Cur_X = lcur_x; Cur_Y = MAX_Y2;
		if ( ReWrt_flg == ERR )
		    ReWrt_flg = TRUE;
	    }
/********
	    if ( Line_flg != FALSE ) {
	        Bk_X = Cur_X; Bk_Y = Cur_Y;
	        Cur_X = Line_X % 80; Cur_Y = MAX_Y2 + Line_X / 80;
		if ( ReWrt_flg == ERR )
		    ReWrt_flg = TRUE;
	    }
**********/
/* ---------------------------------------------------------- */
	    if ( kbhit() != 0 )
	        ch = Get_key(&ec);
	    else
		ec = 0xFFFF;
	    if ( Line_flg != FALSE ) {
	        Cur_X = Bk_X; Cur_Y = Bk_Y;
	    }
	    if ( ec == 0xFFFF )
		break;
	    else if ( ec == 0x6600 )
		ef = TRUE;
	    else if ( ec == 0x6500 )
		ef = ERR;
	    else if ( ec == 0x5D00 )
		Set_RSB_mode(1,2);
	    else if ( ec == 0x5E00 ) {
		Line_flg = (Line_flg == FALSE ? TRUE : FALSE);
		if ( Line_flg == FALSE )
		    Line_X = 1;
		Line_edit(0xFFFF,0,&lcur_x);
	    } else if ( ec == 0x5F00 )
		Auto_log();
	    else if ( ec == 0x6100 ) {
#ifdef	TOWNS
		CD_menu();
#endif
    		Dsp_vram(cvram);
	    } else if ( ec == 0x6200 )
		Down_load();
	    else if ( ec == 0x6300 )
		Up_load();
	    else if ( ec == 0x6400 )
		RSB_Break(port,brktime[brk]);
	    else if ( ch != 0xFFFF )
		Line_edit(ch,ec,&lcur_x);
	}
    }
    Con_end();
    if ( Down_flg != FALSE )
	Down_load();
    if ( ef == ERR )
	RSB_end();
    else
        wrtstr("通信",10,0,0x1F);
    Dsp_PFKEY();    

    Line_flg = FALSE; Line_X = 1;
    Line_edit(0xFFFF,0,&lcur_x);

/* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
    cd_m_stat = FALSE;
/* ---------------------------------------------------------- */
    wrtstr(SPCSTR,30,1,0x1F);
}

/*--------------- Fast Down 92/03/23 Pumpkin  ----------------*/
static  char	sc_tmp[520];
static  int	n = 0;
void	send_one_flush()
{
    int     x,y;

    if ( Down_flg != FALSE && n != 0 ) {
	x = Cur_X; y = Cur_Y;
        ins_chr(sc_tmp,n);
	Set_vram();
	Dsp_free();
	Cur_X = x; Cur_Y = y;
	n = 0;
    }
}

void	send_one(int ch)
{

    BakPut(ch);

    if ( Down_fp != NULL && ch != '\x08' && ch != '\0' )
	fputc(ch,Down_fp);

    if ( Down_flg != FALSE && ch != '\0' && ch != '\x0D' ) {
	if ( ch == '\x0A' )
	    sc_tmp[n++] = 0x0D;
	sc_tmp[n++] = ch;
	if (n >= 512) send_one_flush();
    }
}
/* ---------------------------------------------------------- */

