Files
86Box/src/device/hwm_vt82c686.c

198 lines
4.7 KiB
C
Raw Normal View History

/*
* 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.
*
* This file is part of the 86Box distribution.
*
* Emulation of the VIA VT82C686A/B integrated hardware monitor.
*
*
*
* Author: RichardG, <richardg867@gmail.com>
*
* Copyright 2020 RichardG.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define HAVE_STDARG_H
#include <wchar.h>
#include <86box/86box.h>
#include <86box/device.h>
#include <86box/io.h>
#include <86box/hwm.h>
2020-10-30 20:41:15 -03:00
#define CLAMP(a, min, max) (((a) < (min)) ? (min) : (((a) > (max)) ? (max) : (a)))
#define VT82C686_RPM_TO_REG(r, d) ((r) ? CLAMP(1350000 / (r * d), 1, 255) : 0)
2020-10-30 20:41:15 -03:00
/* Temperature/voltage formulas and factors derived from Linux's via686a.c driver */
#define VT82C686_TEMP_TO_REG(t) (-1.160370e-10*(t*t*t*t*t*t) + 3.193693e-08*(t*t*t*t*t) - 1.464447e-06*(t*t*t*t) - 2.525453e-04*(t*t*t) + 1.424593e-02*(t*t) + 2.148941e+00*t + 7.275808e+01)
2020-10-30 20:41:15 -03:00
#define VT82C686_VOLTAGE_TO_REG(v, f) CLAMP((((v) * (2.628 / (f))) - 120.5) / 25, 0, 255)
typedef struct {
hwm_values_t *values;
device_t *lm75[2];
uint8_t enable;
uint16_t io_base;
uint8_t regs[80];
} vt82c686_t;
2020-11-10 22:02:30 -03:00
static double voltage_factors[5] = {1.25, 1.25, 1.67, 2.6, 6.3};
static void vt82c686_reset(vt82c686_t *dev, uint8_t initialization);
static uint8_t
vt82c686_read(uint16_t addr, void *priv)
{
vt82c686_t *dev = (vt82c686_t *) priv;
uint8_t ret;
2020-11-10 22:02:30 -03:00
addr -= dev->io_base;
switch (addr) {
case 0x1f: case 0x20: case 0x21: /* temperatures */
ret = VT82C686_TEMP_TO_REG(dev->values->temperatures[(addr == 0x1f) ? 2 : (addr & 1)]);
break;
2020-11-10 22:02:30 -03:00
case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: /* voltages */
ret = VT82C686_VOLTAGE_TO_REG(dev->values->voltages[addr - 0x22], voltage_factors[addr - 0x22]);
break;
2020-11-10 22:02:30 -03:00
case 0x29: case 0x2a: /* fan speeds */
ret = VT82C686_RPM_TO_REG(dev->values->fans[addr - 0x29], 1 << ((dev->regs[0x47] >> ((addr == 0x29) ? 4 : 6)) & 0x3));
break;
2020-11-10 22:02:30 -03:00
default: /* other registers */
ret = dev->regs[addr];
break;
2020-11-10 22:02:30 -03:00
}
return ret;
}
static void
vt82c686_write(uint16_t port, uint8_t val, void *priv)
{
vt82c686_t *dev = (vt82c686_t *) priv;
uint8_t reg = port - dev->io_base;
if ((reg == 0x41) || (reg == 0x42) || (reg == 0x45) || (reg == 0x46) || (reg == 0x48) || (reg == 0x4a) || (reg >= 0x4c))
return;
2020-11-10 22:02:30 -03:00
if ((reg == 0x40) && (val & 0x80)) {
val &= 0x7f;
2020-11-10 22:02:30 -03:00
vt82c686_reset(dev, 1);
}
dev->regs[reg] = val;
}
/* Writes to hardware monitor-related configuration space registers
of the VT82C686 power management function are sent here by via_pipc.c */
void
vt82c686_hwm_write(uint8_t addr, uint8_t val, void *priv)
{
vt82c686_t *dev = (vt82c686_t *) priv;
if (dev->io_base)
2020-10-30 20:41:15 -03:00
io_removehandler(dev->io_base, 0x0050,
vt82c686_read, NULL, NULL, vt82c686_write, NULL, NULL, dev);
switch (addr) {
2020-10-30 20:41:15 -03:00
case 0x70:
dev->io_base &= 0xff00;
dev->io_base |= val & 0x80;
break;
2020-10-30 20:41:15 -03:00
case 0x71:
dev->io_base &= 0x00ff;
dev->io_base |= val << 8;
break;
case 0x74:
dev->enable = val & 0x01;
break;
}
if (dev->enable && dev->io_base)
2020-10-30 20:41:15 -03:00
io_sethandler(dev->io_base, 0x0050,
vt82c686_read, NULL, NULL, vt82c686_write, NULL, NULL, dev);
}
static void
vt82c686_reset(vt82c686_t *dev, uint8_t initialization)
{
memset(dev->regs, 0, 80);
dev->regs[0x40] = 0x08;
dev->regs[0x47] = 0x50;
2020-11-10 22:02:30 -03:00
dev->regs[0x4b] = 0x15;
if (!initialization)
vt82c686_hwm_write(0x74, 0x00, dev);
}
static void
vt82c686_close(void *priv)
{
vt82c686_t *dev = (vt82c686_t *) priv;
free(dev);
}
static void *
vt82c686_init(const device_t *info)
{
vt82c686_t *dev = (vt82c686_t *) malloc(sizeof(vt82c686_t));
memset(dev, 0, sizeof(vt82c686_t));
2020-10-30 20:41:15 -03:00
/* Set default values. Since this hardware monitor has a complex voltage factor system,
the values struct contains voltage values *before* applying their respective factors. */
hwm_values_t defaults = {
{ /* fan speeds */
2020-11-10 22:02:30 -03:00
3000, /* usually CPU */
3000 /* usually Chassis */
2020-10-30 20:41:15 -03:00
}, { /* temperatures */
2020-11-10 22:02:30 -03:00
30, /* usually CPU */
30, /* usually System */
30
2020-10-30 20:41:15 -03:00
}, { /* voltages */
hwm_get_vcore(), /* Vcore */
2020-11-10 22:02:30 -03:00
2500, /* +2.5V */
3300, /* +3.3V */
5000, /* +5V */
12000 /* +12V */
2020-10-30 20:41:15 -03:00
}
};
hwm_values = defaults;
dev->values = &hwm_values;
vt82c686_reset(dev, 0);
return dev;
}
const device_t via_vt82c686_hwm_device = {
"VIA VT82C686 Integrated Hardware Monitor",
DEVICE_ISA,
0,
vt82c686_init, vt82c686_close, NULL,
WARNING: CONFIGS MIGHT PARTIALLY BREAK WHERE DEVICE NAMES HAVE CHANGED. Changes to device_t struct to accomodate the upcoming PCI IRQ arbitration rewrite; Added device.c/h API to obtain name from the device_t struct; Significant changes to win/win_settings.c to clean up the code a bit and fix bugs; Ported all the CPU and AudioPCI commits from PCem; Added an API call to allow ACPI soft power off to gracefully stop the emulator; Removed the Siemens PCD-2L from the Dev branch because it now works; Removed the Socket 5 HP Vectra from the Dev branch because it now works; Fixed the Compaq Presario and the Micronics Spitfire; Give the IBM PC330 its own list of 486 CPU so it can have DX2's with CPUID 0x470; SMM fixes; Rewrote the SYSENTER, SYSEXIT, SYSCALL, and SYSRET instructions; Changed IDE reset period to match the specification, fixes #929; The keyboard input and output ports are now forced in front of the queue when read, fixes a number of bugs, including the AMI Apollo hanging on soft reset; Added the Intel AN430TX but Dev branched because it does not work; The network code no longer drops packets if the emulated network card has failed to receive them (eg. when the buffer is full); Changes to PCI card adding and renamed some PCI slot types, also added proper AGP bridge slot types; USB UHCI emulation is no longer a stub (still doesn't fully work, but at least Windows XP chk with Debug no longer ASSERT's on it); Fixed NVR on the the SMC FDC37C932QF and APM variants; A number of fixes to Intel 4x0 chipsets, including fixing every register of the 440LX and 440EX; Some ACPI changes.
2020-11-16 00:01:21 +01:00
{ NULL }, NULL, NULL,
NULL
};