/*
 * Copyright (c) 1992, 1993, 1996
 *	Berkeley Software Design, Inc.  All rights reserved.
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Berkeley Software
 *	Design, Inc.
 *
 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``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 Berkeley Software Design, Inc. 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.
 *
 *	BSDI doscmd.h,v 2.3 1996/04/08 19:32:32 bostic Exp
 */

#ifdef __NetBSD__
#define USE_VM86
#endif

#include <sys/types.h>
#include <sys/time.h>

#include <dirent.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <machine/frame.h>
#include <machine/psl.h>
#include <machine/npx.h>
#ifdef USE_VM86
#include <machine/vm86.h>
#endif

#include "dos.h"
#include "callback.h"
#include "cwd.h"
#include "config.h"

#define	MAX_AVAIL_SEG 0xa000
char *dosmem;
char cmdname[];
int dosmem_size;

int pspseg;                                     /* segment # of PSP */
int curpsp;

struct timeval boot_time;
 
#ifndef USE_VM86
struct vconnect_area {
        int     int_state;
        int     magic;                  /* 0x4242 -> PRB format */
        u_long  passthru[256>>5];       /* bitmap of INTs to handle */
        u_long  magiciret[2];           /* Bounds of "magic" IRET */
};

#define PRB_V86_FORMAT  0x4242
#endif

extern struct sigframe *saved_sigframe;

int vflag;
int tmode;

#define	GET16(x)	(x & 0xffff)
#define	GET8L(x)	(x & 0xff)
#define	GET8H(x)	((x >> 8) & 0xff)
#define	SET16(x, y)	(x = (x & ~0xffff) | (y & 0xffff))
#define	SET8L(x, y)	(x = (x & ~0xff) | (y & 0xff))
#define	SET8H(x, y)	(x = (x & ~0xff00) | ((y & 0xff) << 8))

#define	PUTVEC(s, o, x)	(SET16(s, x >> 16), SET16(o, x))
#define	MAKEVEC(s, o)	(((s) << 16) + (o))
#define	GETVEC(s, o)	MAKEVEC(GET16(s), GET16(o))

#define	PUTPTR(s, o, x)	(SET16(s, (x & 0xf0000) >> 4), SET16(o, x))
#define	MAKEPTR(s, o)	(((s) << 4) + (o))
#define	GETPTR(s, o)	MAKEPTR(GET16(s), GET16(o))

#define	VECPTR(x)	MAKEPTR((x) >> 16, (x) & 0xffff)

inline static void
PUSH(u_short x, struct sigcontext *sc)
{

	SET16(sc->sc_esp, GET16(sc->sc_esp) - 2);
	*(u_short *)GETPTR(sc->sc_ss, sc->sc_esp) = x;
}

inline static u_short
POP(struct sigcontext *sc)
{
	u_short x;

	x = *(u_short *)GETPTR(sc->sc_ss, sc->sc_esp);
	SET16(sc->sc_esp, GET16(sc->sc_esp) + 2);
	return (x);
}

unsigned long disk_transfer_addr;

u_char	*VREG;

#define	BIOSDATA	((u_char *)0x400)

unsigned long rom_config;

void dump_regs ();

void fake_int(struct sigcontext *sc, int);

void mem_init (void);
int mem_alloc (int, int, int *);
int mem_adjust (int, int, int *);

void fatal (char *fmt, ...);
void debug (int flags, char *fmt, ...);

int translate_filename(u_char *, u_char *, int *);

int debug_flags;

/* Lower 8 bits are int number */
#define D_ALWAYS 	0x0000100
#define D_TRAPS 	0x0000200
#define D_FILE_OPS	0x0000400
#define D_MEMORY	0x0000800
#define D_HALF		0x0001000 /* for "half-implemented" system calls */
#define	D_FLOAT		0x0002000
#define	D_DISK		0x0004000
#define	D_TRAPS2	0x0008000
#define	D_PORT		0x0010000
#define	D_EXEC		0x0020000
#define	D_ITRAPS	0x0040000
#define	D_REDIR		0x0080000
#define	D_PRINTER	0x0100000
#define	D_TRAPS3	0x0200000
#define	D_DEBUGIN	0x0400000

#define	TTYF_ECHO	0x00000001
#define	TTYF_ECHONL	0x00000003
#define	TTYF_CTRL	0x00000004
#define	TTYF_BLOCK	0x00000008
#define	TTYF_POLL	0x00000010
#define	TTYF_REDIRECT	0x00010000	/* Cannot have 0xffff bits set */

#define	TTYF_ALL	(TTYF_ECHO | TTYF_CTRL | TTYF_REDIRECT)
#define	TTYF_BLOCKALL	(TTYF_ECHO | TTYF_CTRL | TTYF_REDIRECT | TTYF_BLOCK)

extern int redirect0;
extern int redirect1; 
extern int redirect2;
extern int xmode;
extern int dosmode;
extern int raw_kbd;
extern int kbd_fd;
extern int booting;
extern unsigned long *ivec;
extern int jmp_okay;
extern int dead;
extern FILE *debugf;

extern int nfloppies;
extern int ndisks;
extern int nserial;
extern int nparallel;
extern int nmice;

extern struct sigframe *saved_sigframe;
extern int saved_valid;

int init_hdisk(int drive, int cyl, int head, int tracksize,
	       char *file, char *boot_sector);
int init_floppy(int drive, int type, char *file);
int disk_fd(int drive);
void put_dosenv(char *value);

int tty_eread(struct sigcontext *, int, int);
void tty_write(int, int);
void tty_rwrite(int, int, int);
void tty_move(int, int);
void tty_report(int *, int *);
void tty_flush();
void tty_index();
void tty_pause();
int tty_peek(struct sigcontext *, int);
int tty_state();
void tty_scroll(int, int, int, int, int, int);
void tty_rscroll(int, int, int, int, int, int);
int tty_char(int, int);
void video_setborder(int);

void unknown_int2(int, int, struct sigcontext *);
void unknown_int3(int, int, int, struct sigcontext *);
void unknown_int4(int, int, int, int, struct sigcontext *);

void sigtrap(struct sigframe *);
void sigtrace(struct sigframe *);
void sigalrm(struct sigframe *);
void sigill(struct sigframe *);
void sigurg(struct sigframe *);
void sigfpe(struct sigframe *);
void breakpoint(struct sigframe *);

void outb_traceport(int, unsigned char);
unsigned char inb_traceport(int);

void quit(int);
void call_on_quit(void (*)(void *), void *);
