Files
86Box/src/device/postcard.c

195 lines
6.0 KiB
C
Raw Normal View History

2020-03-23 17:03:28 -03: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.
2020-03-23 17:03:28 -03:00
*
* This file is part of the 86Box distribution.
2020-03-23 17:03:28 -03:00
*
* Implementation of a port 80h POST diagnostic card.
2020-03-23 17:03:28 -03:00
*
2020-03-25 00:46:02 +02:00
*
2020-03-23 17:03:28 -03:00
*
* Authors: RichardG, <richardg867@gmail.com>
2020-06-07 16:03:15 -03:00
*
* Copyright 2020 RichardG.
2020-03-23 17:03:28 -03:00
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <wchar.h>
#define HAVE_STDARG_H
#include <86box/86box.h>
#include <86box/io.h>
#include <86box/device.h>
#include <86box/machine.h>
#include <86box/plat.h>
#include <86box/ui.h>
#include <86box/postcard.h>
#include "cpu.h"
2020-03-23 17:03:28 -03:00
uint8_t postcard_codes[POSTCARDS_NUM];
2022-09-18 17:13:28 -04:00
static uint16_t postcard_port;
static uint8_t postcard_written[POSTCARDS_NUM];
static uint8_t postcard_ports_num = 1;
static uint8_t postcard_prev_codes[POSTCARDS_NUM];
2023-08-12 08:39:36 -04:00
#define UISTR_LEN 32
2022-09-18 17:13:28 -04:00
static char postcard_str[UISTR_LEN]; /* UI output string */
2020-03-23 17:03:28 -03:00
2022-09-18 17:13:28 -04:00
extern void ui_sb_bugui(char *__str);
2020-03-23 17:03:28 -03:00
#ifdef ENABLE_POSTCARD_LOG
int postcard_do_log = ENABLE_POSTCARD_LOG;
static void
postcard_log(const char *fmt, ...)
{
va_list ap;
if (postcard_do_log) {
2022-09-18 17:13:28 -04:00
va_start(ap, fmt);
pclog_ex(fmt, ap);
va_end(ap);
2020-03-23 17:03:28 -03:00
}
}
#else
int postcard_do_log = 0;
2022-09-18 17:13:28 -04:00
# define postcard_log(fmt, ...)
2020-03-23 17:03:28 -03:00
#endif
static void
postcard_setui(void)
{
if (postcard_ports_num > 1) {
2023-08-14 18:59:02 -04:00
char ps[2][POSTCARDS_NUM][3] = { { { 0 },
{ 0 },
} };
for (uint8_t i = 0; i < POSTCARDS_NUM; i++) {
if (!postcard_written[i]) {
2023-08-12 08:39:36 -04:00
snprintf(ps[0][i], sizeof(ps[0][i]), "--");
snprintf(ps[1][i], sizeof(ps[1][i]), "--");
} else if (postcard_written[i] == 1) {
2023-08-12 08:39:36 -04:00
snprintf(ps[0][i], sizeof(ps[0][i]), "%02X", postcard_codes[i]);
snprintf(ps[1][i], sizeof(ps[1][i]), "--");
} else {
2023-08-12 08:39:36 -04:00
snprintf(ps[0][i], sizeof(ps[0][i]), "%02X", postcard_codes[i]);
snprintf(ps[1][i], sizeof(ps[1][i]), "%02X", postcard_prev_codes[i]);
}
}
switch (postcard_ports_num) {
default:
case 2:
2023-08-12 08:39:36 -04:00
snprintf(postcard_str, sizeof(postcard_str), "POST: %s%s %s%s",
ps[0][0], ps[0][1], ps[1][0], ps[1][1]);
break;
case 3:
2023-08-12 08:39:36 -04:00
snprintf(postcard_str, sizeof(postcard_str), "POST: %s/%s%s %s/%s%s",
ps[0][0], ps[0][1], ps[0][2], ps[1][0], ps[1][1], ps[1][2]);
break;
case 4:
2023-08-12 08:39:36 -04:00
snprintf(postcard_str, sizeof(postcard_str), "POST: %s%s/%s%s %s%s/%s%s",
ps[0][0], ps[0][1], ps[0][2], ps[0][3],
ps[1][0], ps[1][1], ps[1][2], ps[1][3]);
break;
}
} else {
if (!postcard_written[0])
2023-08-12 08:39:36 -04:00
snprintf(postcard_str, sizeof(postcard_str), "POST: -- --");
else if (postcard_written[0] == 1)
2023-08-12 08:39:36 -04:00
snprintf(postcard_str, sizeof(postcard_str), "POST: %02X --", postcard_codes[0]);
else
2023-08-12 08:39:36 -04:00
snprintf(postcard_str, sizeof(postcard_str), "POST: %02X %02X", postcard_codes[0], postcard_prev_codes[0]);
}
2020-03-23 17:03:28 -03:00
ui_sb_bugui(postcard_str);
if (postcard_do_log) {
2022-09-18 17:13:28 -04:00
/* log same string sent to the UI */
postcard_log("[%04X:%08X] %s\n", CS, cpu_state.pc, postcard_str);
2020-03-23 17:03:28 -03:00
}
}
static void
postcard_reset(void)
{
memset(postcard_written, 0x00, POSTCARDS_NUM * sizeof(uint8_t));
memset(postcard_codes, 0x00, POSTCARDS_NUM * sizeof(uint8_t));
memset(postcard_prev_codes, 0x00, POSTCARDS_NUM * sizeof(uint8_t));
2020-03-23 17:03:28 -03:00
postcard_setui();
}
static void
postcard_write(uint16_t port, uint8_t val, UNUSED(void *priv))
2020-03-23 17:03:28 -03:00
{
if (postcard_written[port & POSTCARD_MASK] &&
(val == postcard_codes[port & POSTCARD_MASK]))
2022-09-18 17:13:28 -04:00
return;
postcard_prev_codes[port & POSTCARD_MASK] = postcard_codes[port & POSTCARD_MASK];
postcard_codes[port & POSTCARD_MASK] = val;
if (postcard_written[port & POSTCARD_MASK] < 2)
postcard_written[port & POSTCARD_MASK]++;
2020-03-23 17:03:28 -03:00
postcard_setui();
}
static void *
2023-06-26 12:47:04 -04:00
postcard_init(UNUSED(const device_t *info))
2020-03-23 17:03:28 -03:00
{
postcard_ports_num = 1;
2020-03-23 17:03:28 -03:00
if (machine_has_bus(machine, MACHINE_BUS_MCA))
2022-09-18 17:13:28 -04:00
postcard_port = 0x680; /* MCA machines */
else if (strstr(machines[machine].name, " PS/2 ") || strstr(machine_getname_ex(machine), " PS/1 "))
2022-09-18 17:13:28 -04:00
postcard_port = 0x190; /* ISA PS/2 machines */
2020-10-30 20:54:47 -03:00
else if (strstr(machines[machine].name, " IBM XT "))
2022-09-18 17:13:28 -04:00
postcard_port = 0x60; /* IBM XT */
else if (strstr(machines[machine].name, " IBM PCjr")) {
2022-09-18 17:13:28 -04:00
postcard_port = 0x10; /* IBM PCjr */
postcard_ports_num = 3; /* IBM PCjr error ports 11h and 12h */
} else if (strstr(machines[machine].name, " Compaq ") && !machine_has_bus(machine, MACHINE_BUS_PCI))
2022-09-18 17:13:28 -04:00
postcard_port = 0x84; /* ISA Compaq machines */
else if (strstr(machines[machine].name, "Olivetti"))
postcard_port = 0x378; /* Olivetti machines */
2020-03-23 17:03:28 -03:00
else
2022-09-18 17:13:28 -04:00
postcard_port = 0x80; /* AT and clone machines */
2020-03-23 17:03:28 -03:00
postcard_log("POST card initializing on port %04Xh\n", postcard_port);
postcard_reset();
2022-09-18 17:13:28 -04:00
if (postcard_port)
io_sethandler(postcard_port, postcard_ports_num,
2022-09-18 17:13:28 -04:00
NULL, NULL, NULL, postcard_write, NULL, NULL, NULL);
2020-03-23 17:03:28 -03:00
return postcard_write;
}
static void
postcard_close(UNUSED(void *priv))
{
2022-09-18 17:13:28 -04:00
if (postcard_port)
io_removehandler(postcard_port, postcard_ports_num,
2022-09-18 17:13:28 -04:00
NULL, NULL, NULL, postcard_write, NULL, NULL, NULL);
2020-03-23 17:03:28 -03:00
}
const device_t postcard_device = {
2022-09-18 17:13:28 -04:00
.name = "POST Card",
2022-03-13 09:28:28 -04:00
.internal_name = "postcard",
2022-09-18 17:13:28 -04:00
.flags = DEVICE_ISA,
.local = 0,
.init = postcard_init,
.close = postcard_close,
.reset = NULL,
2022-03-13 09:28:28 -04:00
{ .available = NULL },
.speed_changed = NULL,
2022-09-18 17:13:28 -04:00
.force_redraw = NULL,
.config = NULL
2020-03-23 17:03:28 -03:00
};