/* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
   Distributed by Free Software Foundation, Inc.

This file is part of Ghostscript.

Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing.  Refer
to the Ghostscript General Public License for full details.

Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License.  A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities.  It should be in a file named COPYING.  Among other
things, the copyright notice and this notice must be preserved on all
copies.  */

/* gp_pcat.c */
/* Part of DJ's GCC-specific routines for Ghostscript */
/* Machine dependent routines for IBM PC/AT compatibles */

#include <pc.h>
#include "gx.h"
#include "dos_.h"
#include "gp_djgpp.h"
#include "djgpp.h"

private gp_djgpp_identify_proc(gp_pcat_identify);
private gp_djgpp_open_printer_proc(gp_pcat_open_printer);
private gp_djgpp_close_printer_proc(gp_pcat_close_printer);
private gp_djgpp_flush_printer_proc(gp_pcat_port_flush);
private gp_djgpp_flush_printer_proc(gp_pcat_bios_flush);

gp_djgpp_system_procs gp_pcat_system_procs = {
  gp_printer_to_bios,
  gp_pcat_identify,
  0, /* open console */
  0, /* close console */
  gp_pcat_open_printer,
  gp_pcat_close_printer,
  0, /* allocate buffer */
  0, /* free buffer */
  gp_pcat_port_flush,
  gp_pcat_bios_flush,
};

private ushort gp_pcat_printer_port;

private int gp_pcat_identify(int oem_no)
{
   ushort rs = *(ushort *)(PC_CONV_BASE + 0x400);
   ushort pr = *(ushort *)(PC_CONV_BASE + 0x408);
   ushort ks = *(ushort *)(PC_CONV_BASE + 0x480);
   ushort ke = *(ushort *)(PC_CONV_BASE + 0x482);

   return ( (ks == 0x001e && ke == (0x001e + 16 *2)) ||
            ((rs == 0x03f8 || rs == 0x02f8) &&
             (pr == 0x0378 || pr == 0x0278 || pr == 0x03bc)) );
}

private int gp_pcat_open_printer(int t,int n)
{
  ushort *p = (ushort *)(PC_CONV_BASE+0x408);
  
  /* LPTn port address table */
  
  switch (t) {
  case gp_printer_to_port:
    gp_pcat_printer_port = *(p + n);
    if (gp_pcat_printer_port == 0x0000)
      gp_pcat_printer_port = 0x0378;
    break;
  case gp_printer_to_bios:
  case gp_printer_to_stdprn:
    break;
  }
  return 0;
}
private int gp_pcat_close_printer(int t,int n)
{  return 0;
}

private int
  gp_pcat_port_flush(byte *buf,int size)
{
  register int i;
  byte ctrl;
  
  for ( ;size>0;size--,buf++) {
    while (!(inportb(gp_pcat_printer_port + 1) & 0x80)){
      ;   /* wait busy */
    }
    outportb(gp_pcat_printer_port, *buf); /* output data */
    ctrl = inportb(gp_pcat_printer_port + 2);
    outportb(gp_pcat_printer_port + 2, (ctrl & 0xDF) | 0x01); /* out strobe */
    for (i = 4; i; i--)   /* for wait */
      ctrl &= 0xFE;                               /* reset strobe */
    outportb(gp_pcat_printer_port + 2, ctrl);
  }
  
  return 0;
}

private int
  gp_pcat_bios_flush(byte *buf,int size)
{
  union REGS iregs,oregs;
  
  for ( ; size > 0; size--,buf++) {
    do {
      iregs.h.ah = 0x02;
      iregs.x.dx = 0;
      int86(0x17, &iregs, &oregs);
    } while ((oregs.h.ah & 0x29) != 0 || (oregs.h.ah & 0x80) == 0);
    iregs.h.ah = 0;
    iregs.h.al = *buf;
    int86(0x17, &iregs, &oregs);
  }
  return 0;
}
