Files
86Box/src/sio/sio_pc87306.c

502 lines
13 KiB
C
Raw Normal View History

/*
2023-01-06 15:36:05 -05:00
* 86Box A hypervisor and IBM PC system emulator that specializes in
* running old operating systems and software designed for IBM
* PC systems and compatibles from 1981 through fairly recent
* system designs based on the PCI bus.
*
2023-01-06 15:36:05 -05:00
* This file is part of the 86Box distribution.
*
2023-01-06 15:36:05 -05:00
* Emulation of the NatSemi PC87306 Super I/O chip.
*
2020-03-25 00:46:02 +02:00
*
*
2023-01-06 15:36:05 -05:00
* Authors: Miran Grca, <mgrca8@gmail.com>
*
* Copyright 2016-2018 Miran Grca.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <86box/86box.h>
#include <86box/io.h>
#include <86box/timer.h>
#include <86box/device.h>
#include <86box/lpt.h>
#include <86box/mem.h>
#include <86box/nvr.h>
#include <86box/pci.h>
#include <86box/rom.h>
#include <86box/serial.h>
#include <86box/hdc.h>
#include <86box/hdc_ide.h>
#include <86box/fdd.h>
#include <86box/fdc.h>
#include <86box/sio.h>
2023-06-28 13:46:28 -04:00
#include <86box/plat_unused.h>
2023-10-08 01:14:26 +02:00
#include <86box/machine.h>
2023-06-28 13:46:28 -04:00
typedef struct pc87306_t {
uint8_t tries;
uint8_t regs[29];
uint8_t gpio[2];
2023-10-07 05:13:38 +02:00
uint16_t gpioba;
2022-09-18 17:17:00 -04:00
int cur_reg;
fdc_t *fdc;
serial_t *uart[2];
2022-09-18 17:17:00 -04:00
nvr_t *nvr;
} pc87306_t;
static void
pc87306_gpio_write(uint16_t port, uint8_t val, void *priv)
{
pc87306_t *dev = (pc87306_t *) priv;
uint32_t gpio = 0xffff0000;
dev->gpio[port & 0x0001] = val;
if (port & 0x0001) {
gpio |= ((uint32_t) val) << 8;
gpio |= dev->gpio[0];
} else {
gpio |= ((uint32_t) dev->gpio[1]) << 8;
gpio |= val;
}
(void) machine_handle_gpio(1, gpio);
}
uint8_t
2025-01-26 15:15:53 -05:00
pc87306_gpio_read(uint16_t port, UNUSED(void *priv))
{
uint32_t ret = machine_handle_gpio(0, 0xffffffff);
if (port & 0x0001)
ret = (ret >> 8) & 0xff;
else
ret &= 0xff;
return ret;
}
static void
pc87306_gpio_remove(pc87306_t *dev)
{
2023-10-07 05:13:38 +02:00
if (dev->gpioba != 0x0000) {
io_removehandler(dev->gpioba, 0x0001,
pc87306_gpio_read, NULL, NULL, pc87306_gpio_write, NULL, NULL, dev);
io_removehandler(dev->gpioba + 1, 0x0001,
pc87306_gpio_read, NULL, NULL, pc87306_gpio_write, NULL, NULL, dev);
}
}
static void
pc87306_gpio_init(pc87306_t *dev)
{
2023-10-07 05:13:38 +02:00
dev->gpioba = ((uint16_t) dev->regs[0x0f]) << 2;
if (dev->gpioba != 0x0000) {
if ((dev->regs[0x12]) & 0x10)
2023-10-08 18:15:27 +02:00
io_sethandler(dev->gpioba, 0x0001,
2023-10-07 05:13:38 +02:00
pc87306_gpio_read, NULL, NULL, pc87306_gpio_write, NULL, NULL, dev);
2023-10-07 05:13:38 +02:00
if ((dev->regs[0x12]) & 0x20)
2023-10-08 18:15:27 +02:00
io_sethandler(dev->gpioba + 1, 0x0001,
2023-10-07 05:13:38 +02:00
pc87306_gpio_read, NULL, NULL, pc87306_gpio_write, NULL, NULL, dev);
}
}
static void
pc87306_gpio_handler(pc87306_t *dev)
{
pc87306_gpio_remove(dev);
pc87306_gpio_init(dev);
}
static void
lpt1_handler(pc87306_t *dev)
{
2022-09-18 17:17:00 -04:00
int temp;
2023-05-29 01:30:51 -04:00
uint16_t lptba;
uint16_t lpt_port = LPT1_ADDR;
2022-09-18 17:17:00 -04:00
uint8_t lpt_irq = LPT2_IRQ;
2022-09-18 17:17:00 -04:00
temp = dev->regs[0x01] & 3;
lptba = ((uint16_t) dev->regs[0x19]) << 2;
2020-03-28 20:10:31 +01:00
switch (temp) {
2022-09-18 17:17:00 -04:00
case 0:
lpt_port = LPT1_ADDR;
lpt_irq = (dev->regs[0x02] & 0x08) ? LPT1_IRQ : LPT2_IRQ;
break;
case 1:
if (dev->regs[0x1b] & 0x40)
lpt_port = lptba;
else
lpt_port = LPT_MDA_ADDR;
lpt_irq = LPT_MDA_IRQ;
break;
case 2:
lpt_port = LPT2_ADDR;
lpt_irq = LPT2_IRQ;
break;
case 3:
lpt_port = 0x000;
lpt_irq = 0xff;
break;
2023-06-28 13:46:28 -04:00
default:
break;
}
2020-03-28 20:10:31 +01:00
if (dev->regs[0x1b] & 0x10)
2022-09-18 17:17:00 -04:00
lpt_irq = (dev->regs[0x1b] & 0x20) ? 7 : 5;
2020-03-28 20:10:31 +01:00
if (lpt_port)
lpt1_setup(lpt_port);
Added the IBM 5161 ISA expansion for PC and XT; Cleaned up the parallel port emulation, added IRQ support, and made enabling/disabling per port; Added the Award 430NX and the Intel Classic/PCI (Alfredo, 420TX); Finished the 586MC1; Added 8087 emulation; Moved Cyrix 6x86'es to the Dev branch; Sanitized/cleaned up memregs.c/h and intel.c/h; Split the chipsets from machines and sanitized Port 92 emulation; Added support for the 15bpp mode to the Compaq ATI 28800; Moved the MR 386DX and 486 machines to the Dev branch; Ported the new dynamic recompiler from PCem, but it remains in Dev branch until after v2.00; Ported the new timer code from PCem; Cleaned up the CPU table of unused stuff and better optimized its structure; Ported the Open-XT and Open-AT from VARCem, the Open-AT is in the Dev branch; Ported the XT MFM controller rewrite and adding of more controllers (incl. two RLL ones), from VARCem; Added the AHA-1540A and the BusTek BT-542B; Moved the Sumo SCSI-AT to the Dev branch; Minor IDE, FDC, and floppy drive code clean-ups; Made NCR 5380/53C400-based cards' BIOS address configurable; Got rid of the legacy romset variable; Unified (video) buffer and buffer32 into one and make the unified buffer 32-bit; Added the Amstead PPC512 per PCem patch by John Elliott; Switched memory mapping granularity from 16k to 4k (less than 1k not possible due to internal pages); Rewrote the CL-GD 54xx blitter, fixes Win-OS/2 on the 54x6 among other thing; Added the Image Manager 1024 and Professional Graphics Controller per PCem patch by John Elliott and work done on VARCem; Added Headland HT-216, GC-205 and Video 7 VGA 1024i emulation based on PCem commit; Implemented the fuction keys for the Toshiba T1000/T1200/T3100 enhancement; Amstrad MegaPC does now works correctly with non-internal graphics card; The SLiRP code no longer casts a packed struct type to a non-packed struct type; The Xi8088 and PB410a no longer hang on 86Box when PS/2 mouse is not present; The S3 Virge on BeOS is no longer broken (was broken by build #1591); OS/2 2.0 build 6.167 now sees key presses again; Xi8088 now work on CGA again; 86F images converted from either the old or new variants of the HxC MFM format now work correctly; Hardware interrupts with a vector of 0xFF are now handled correctly; OPTi 495SX boards no longer incorrectly have 64 MB maximum RAM when 32 MB is correct; Fixed VNC keyboard input bugs; Fixed AT RTC periodic interrupt - Chicago 58s / 73f / 73g / 81 MIDI play no longer hangs with the build's own VTD driver; Fixed mouse polling with internal mice - Amstrad and Olivetti mice now work correctly; Triones ATAPI DMA driver now correctly reads a file at the end of a CD image with a sectors number not divisible by 4; Compaq Portable now works with all graphics cards; Fixed various MDSI Genius bugs; Added segment limit checks and improved page fault checks for several CPU instructions - Memphis 15xx WINSETUP and Chicago 58s WINDISK.CPL no longer issue a GPF, and some S3 drivers that used to have glitches, now work correctly; Further improved the 808x emulation, also fixes the noticably choppy sound when using 808x CPU's, also fixes #355; OS/2 installer no logner locks up on splash screen on PS/2 Model 70 and 80, fixes #400. Fixed several Amstead bugs, GEM no longer crashes on the Amstrad 1640, fixes #391. Ported John Elliott's Amstrad fixes and improvement from PCem, and fixed the default language so it's correctly Engliish, fixes #278, fixes #389. Fixed a minor IDE timing bug, fixes #388. Fixed Toshiba T1000 RAM issues, fixes #379. Fixed EGA/(S)VGA overscan border handling, fixes #378; Got rid of the now long useless IDE channel 2 auto-removal, fixes #370; Fixed the BIOS files used by the AMSTRAD PC1512, fixes #366; Ported the Unicode CD image file name fix from VARCem, fixes #365; Fixed high density floppy disks on the Xi8088, fixes #359; Fixed some bugs in the Hercules emulation, fixes #346, fixes #358; Fixed the SCSI hard disk mode sense pages, fixes #356; Removed the AMI Unknown 386SX because of impossibility to identify the chipset, closes #349; Fixed bugs in the serial mouse emulation, fixes #344; Compiled 86Box binaries now include all the required .DLL's, fixes #341; Made some combo boxes in the Settings dialog slightly wider, fixes #276.
2019-09-20 14:02:30 +02:00
lpt1_irq(lpt_irq);
}
static void
serial_handler(pc87306_t *dev, int uart)
{
2022-09-18 17:17:00 -04:00
int temp;
2023-05-29 01:30:51 -04:00
uint8_t fer_irq;
uint8_t pnp1_irq;
uint8_t fer_shift;
uint8_t pnp_shift;
uint8_t irq;
temp = (dev->regs[1] >> (2 << uart)) & 3;
2022-09-18 17:17:00 -04:00
fer_shift = 2 << uart; /* 2 for UART 1, 4 for UART 2 */
pnp_shift = 2 + (uart << 2); /* 2 for UART 1, 6 for UART 2 */
/* 0 = COM1 (IRQ 4), 1 = COM2 (IRQ 3), 2 = COM3 (IRQ 4), 3 = COM4 (IRQ 3) */
2022-09-18 17:17:00 -04:00
fer_irq = ((dev->regs[1] >> fer_shift) & 1) ? 3 : 4;
pnp1_irq = ((dev->regs[0x1c] >> pnp_shift) & 1) ? 4 : 3;
irq = (dev->regs[0x1c] & 1) ? pnp1_irq : fer_irq;
switch (temp) {
2022-09-18 17:17:00 -04:00
case 0:
serial_setup(dev->uart[uart], COM1_ADDR, irq);
break;
case 1:
serial_setup(dev->uart[uart], COM2_ADDR, irq);
break;
case 2:
switch ((dev->regs[1] >> 6) & 3) {
case 0:
serial_setup(dev->uart[uart], COM3_ADDR, irq);
break;
case 1:
serial_setup(dev->uart[uart], 0x338, irq);
break;
case 2:
serial_setup(dev->uart[uart], COM4_ADDR, irq);
break;
case 3:
serial_setup(dev->uart[uart], 0x220, irq);
break;
2023-06-28 13:46:28 -04:00
default:
break;
2022-09-18 17:17:00 -04:00
}
break;
case 3:
switch ((dev->regs[1] >> 6) & 3) {
case 0:
serial_setup(dev->uart[uart], COM4_ADDR, irq);
break;
case 1:
serial_setup(dev->uart[uart], 0x238, irq);
break;
case 2:
serial_setup(dev->uart[uart], 0x2e0, irq);
break;
case 3:
serial_setup(dev->uart[uart], 0x228, irq);
break;
2023-06-28 13:46:28 -04:00
default:
break;
2022-09-18 17:17:00 -04:00
}
break;
2023-06-28 13:46:28 -04:00
default:
break;
}
}
static void
pc87306_write(uint16_t port, uint8_t val, void *priv)
{
pc87306_t *dev = (pc87306_t *) priv;
2023-05-29 01:30:51 -04:00
uint8_t index;
uint8_t valxor;
index = (port & 1) ? 0 : 1;
if (index) {
2022-09-18 17:17:00 -04:00
dev->cur_reg = val & 0x1f;
dev->tries = 0;
return;
} else {
2022-09-18 17:17:00 -04:00
if (dev->tries) {
if ((dev->cur_reg == 0) && (val == 8))
val = 0x4b;
valxor = val ^ dev->regs[dev->cur_reg];
dev->tries = 0;
if ((dev->cur_reg <= 28) && (dev->cur_reg != 8)) {
if (dev->cur_reg == 0)
val &= 0x5f;
dev->regs[dev->cur_reg] = val;
} else
return;
} else {
dev->tries++;
return;
}
}
2022-09-18 17:17:00 -04:00
switch (dev->cur_reg) {
2023-10-08 18:15:27 +02:00
case 0x00:
2022-09-18 17:17:00 -04:00
if (valxor & 1) {
lpt1_remove();
if ((val & 1) && !(dev->regs[2] & 1))
lpt1_handler(dev);
}
if (valxor & 2) {
serial_remove(dev->uart[0]);
if ((val & 2) && !(dev->regs[2] & 1))
serial_handler(dev, 0);
}
if (valxor & 4) {
serial_remove(dev->uart[1]);
if ((val & 4) && !(dev->regs[2] & 1))
serial_handler(dev, 1);
}
if (valxor & 0x28) {
fdc_remove(dev->fdc);
if ((val & 8) && !(dev->regs[2] & 1))
fdc_set_base(dev->fdc, (val & 0x20) ? FDC_SECONDARY_ADDR : FDC_PRIMARY_ADDR);
}
break;
2023-10-08 18:15:27 +02:00
case 0x01:
2022-09-18 17:17:00 -04:00
if (valxor & 3) {
lpt1_remove();
if ((dev->regs[0] & 1) && !(dev->regs[2] & 1))
lpt1_handler(dev);
}
if (valxor & 0xcc) {
serial_remove(dev->uart[0]);
if ((dev->regs[0] & 2) && !(dev->regs[2] & 1))
serial_handler(dev, 0);
}
if (valxor & 0xf0) {
serial_remove(dev->uart[1]);
if ((dev->regs[0] & 4) && !(dev->regs[2] & 1))
serial_handler(dev, 1);
}
break;
2023-10-08 18:15:27 +02:00
case 0x02:
if (valxor & 0x01) {
2022-09-18 17:17:00 -04:00
lpt1_remove();
serial_remove(dev->uart[0x00]);
serial_remove(dev->uart[0x01]);
2022-09-18 17:17:00 -04:00
fdc_remove(dev->fdc);
if (!(val & 1)) {
if (dev->regs[0x00] & 0x01)
2022-09-18 17:17:00 -04:00
lpt1_handler(dev);
if (dev->regs[0x00] & 0x02)
2022-09-18 17:17:00 -04:00
serial_handler(dev, 0);
if (dev->regs[0x00] & 0x04)
2022-09-18 17:17:00 -04:00
serial_handler(dev, 1);
if (dev->regs[0x00] & 0x08)
fdc_set_base(dev->fdc, (dev->regs[0x00] & 0x20) ? FDC_SECONDARY_ADDR : FDC_PRIMARY_ADDR);
2022-09-18 17:17:00 -04:00
}
}
if (valxor & 0x08) {
2022-09-18 17:17:00 -04:00
lpt1_remove();
if ((dev->regs[0x00] & 1) && !(dev->regs[0x02] & 1))
2022-09-18 17:17:00 -04:00
lpt1_handler(dev);
}
break;
2023-10-08 18:15:27 +02:00
case 0x04:
if (valxor & 0x80)
nvr_lock_set(0x00, 256, !!(val & 0x80), dev->nvr);
break;
2023-10-08 18:15:27 +02:00
case 0x05:
if (valxor & 0x08)
nvr_at_handler(!!(val & 0x08), 0x0070, dev->nvr);
if (valxor & 0x20)
nvr_bank_set(0, !!(val & 0x20), dev->nvr);
break;
2023-10-08 18:15:27 +02:00
case 0x09:
2022-09-18 17:17:00 -04:00
if (valxor & 0x44) {
fdc_update_enh_mode(dev->fdc, (val & 4) ? 1 : 0);
fdc_update_densel_polarity(dev->fdc, (val & 0x40) ? 1 : 0);
}
break;
2023-10-08 18:15:27 +02:00
case 0x0f:
2022-09-18 17:17:00 -04:00
if (valxor)
2023-10-07 05:13:38 +02:00
pc87306_gpio_handler(dev);
2022-09-18 17:17:00 -04:00
break;
case 0x12:
if (valxor & 0x01)
nvr_wp_set(!!(val & 0x01), 0, dev->nvr);
2022-09-18 17:17:00 -04:00
if (valxor & 0x30)
2023-10-07 05:13:38 +02:00
pc87306_gpio_handler(dev);
2022-09-18 17:17:00 -04:00
break;
case 0x19:
if (valxor) {
lpt1_remove();
if ((dev->regs[0] & 1) && !(dev->regs[2] & 1))
lpt1_handler(dev);
}
break;
case 0x1b:
2022-09-18 17:17:00 -04:00
if (valxor & 0x70) {
lpt1_remove();
if (!(val & 0x40))
dev->regs[0x19] = 0xEF;
if ((dev->regs[0] & 1) && !(dev->regs[2] & 1))
lpt1_handler(dev);
}
break;
case 0x1c:
2022-09-18 17:17:00 -04:00
if (valxor) {
serial_remove(dev->uart[0]);
serial_remove(dev->uart[1]);
if ((dev->regs[0] & 2) && !(dev->regs[2] & 1))
serial_handler(dev, 0);
if ((dev->regs[0] & 4) && !(dev->regs[2] & 1))
serial_handler(dev, 1);
}
break;
2023-06-28 13:46:28 -04:00
default:
break;
}
}
uint8_t
pc87306_read(uint16_t port, void *priv)
{
pc87306_t *dev = (pc87306_t *) priv;
2023-05-29 01:30:51 -04:00
uint8_t ret = 0xff;
uint8_t index;
index = (port & 1) ? 0 : 1;
dev->tries = 0;
if (index)
2022-09-18 17:17:00 -04:00
ret = dev->cur_reg & 0x1f;
else {
2022-09-18 17:17:00 -04:00
if (dev->cur_reg == 8)
ret = 0x70;
else if (dev->cur_reg < 28)
ret = dev->regs[dev->cur_reg];
}
return ret;
}
void
pc87306_reset_common(void *priv)
{
pc87306_t *dev = (pc87306_t *) priv;
memset(dev->regs, 0, 29);
dev->regs[0x00] = 0x0B;
dev->regs[0x01] = 0x01;
dev->regs[0x03] = 0x01;
dev->regs[0x05] = 0x0D;
dev->regs[0x08] = 0x70;
dev->regs[0x09] = 0xC0;
dev->regs[0x0b] = 0x80;
dev->regs[0x0f] = 0x1E;
dev->regs[0x12] = 0x30;
dev->regs[0x19] = 0xEF;
/*
2022-09-18 17:17:00 -04:00
0 = 360 rpm @ 500 kbps for 3.5"
2023-01-06 15:36:29 -05:00
1 = Default, 300 rpm @ 500, 300, 250, 1000 kbps for 3.5"
*/
lpt1_remove();
lpt1_handler(dev);
serial_remove(dev->uart[0]);
serial_remove(dev->uart[1]);
serial_handler(dev, 0);
serial_handler(dev, 1);
fdc_reset(dev->fdc);
pc87306_gpio_init(dev);
nvr_lock_set(0x00, 256, 0, dev->nvr);
nvr_at_handler(0, 0x0070, dev->nvr);
nvr_at_handler(1, 0x0070, dev->nvr);
nvr_bank_set(0, 0, dev->nvr);
nvr_wp_set(0, 0, dev->nvr);
}
void
pc87306_reset(void *priv)
{
pc87306_t *dev = (pc87306_t *) priv;
pc87306_gpio_write(0x0000, 0xff, dev);
pc87306_gpio_write(0x0001, 0xff, dev);
pc87306_reset_common(dev);
}
static void
pc87306_close(void *priv)
{
pc87306_t *dev = (pc87306_t *) priv;
free(dev);
}
static void *
2023-06-28 13:46:28 -04:00
pc87306_init(UNUSED(const device_t *info))
{
2025-01-07 00:42:06 -05:00
pc87306_t *dev = (pc87306_t *) calloc(1, sizeof(pc87306_t));
dev->fdc = device_add(&fdc_at_nsc_device);
dev->uart[0] = device_add_inst(&ns16550_device, 1);
dev->uart[1] = device_add_inst(&ns16550_device, 2);
dev->nvr = device_add(&at_mb_nvr_device);
2020-02-29 19:12:23 +01:00
dev->gpio[0] = dev->gpio[1] = 0xff;
pc87306_reset_common(dev);
io_sethandler(0x02e, 0x0002,
2022-09-18 17:17:00 -04:00
pc87306_read, NULL, NULL, pc87306_write, NULL, NULL, dev);
return dev;
}
const device_t pc87306_device = {
2022-09-18 17:17:00 -04:00
.name = "National Semiconductor PC87306 Super I/O",
2022-03-13 09:57:57 -04:00
.internal_name = "pc87306",
2022-09-18 17:17:00 -04:00
.flags = 0,
.local = 0,
.init = pc87306_init,
.close = pc87306_close,
.reset = pc87306_reset,
2025-01-07 01:12:42 -05:00
.available = NULL,
2022-03-13 09:57:57 -04:00
.speed_changed = NULL,
2022-09-18 17:17:00 -04:00
.force_redraw = NULL,
.config = NULL
};