2017-05-30 03:38:38 +02: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.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of the 86Box distribution.
|
|
|
|
|
*
|
|
|
|
|
* CMOS NVRAM emulation.
|
|
|
|
|
*
|
2017-06-04 02:11:19 -04:00
|
|
|
* Version: @(#)nvr.c 1.0.1 2017/06/03
|
2017-05-30 03:38:38 +02:00
|
|
|
*
|
2017-06-04 02:11:19 -04:00
|
|
|
* Authors: Sarah Walker, <http://pcem-emulator.co.uk/>
|
2017-05-30 03:38:38 +02:00
|
|
|
* Miran Grca, <mgrca8@gmail.com>
|
|
|
|
|
* Mahod,
|
|
|
|
|
* Copyright 2008-2017 Sarah Walker.
|
|
|
|
|
* Copyright 2016-2017 Miran Grca.
|
|
|
|
|
* Copyright 2016-2017 Mahod.
|
|
|
|
|
*/
|
2016-06-26 00:34:39 +02:00
|
|
|
#include <stdio.h>
|
2017-06-08 00:58:47 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <wchar.h>
|
2016-06-26 00:34:39 +02:00
|
|
|
#include "ibm.h"
|
2017-06-08 00:58:47 +02:00
|
|
|
#include "CPU/cpu.h"
|
|
|
|
|
#include "device.h"
|
2016-06-26 00:34:39 +02:00
|
|
|
#include "io.h"
|
2017-05-06 04:02:03 +02:00
|
|
|
#include "mem.h"
|
2017-06-08 00:58:47 +02:00
|
|
|
#include "model.h"
|
2016-06-26 00:34:39 +02:00
|
|
|
#include "nvr.h"
|
|
|
|
|
#include "pic.h"
|
2017-05-06 04:02:03 +02:00
|
|
|
#include "rom.h"
|
2016-06-26 00:34:39 +02:00
|
|
|
#include "timer.h"
|
2016-07-11 01:27:23 +02:00
|
|
|
#include "rtc.h"
|
2016-06-26 00:34:39 +02:00
|
|
|
|
2017-06-08 00:58:47 +02:00
|
|
|
int oldmodel;
|
2016-06-26 00:34:39 +02:00
|
|
|
int nvrmask=63;
|
2017-05-05 01:49:42 +02:00
|
|
|
char nvrram[128];
|
2016-06-26 00:34:39 +02:00
|
|
|
int nvraddr;
|
|
|
|
|
|
|
|
|
|
int nvr_dosave = 0;
|
|
|
|
|
|
2016-11-07 06:39:20 +01:00
|
|
|
static int nvr_onesec_time = 0, nvr_onesec_cnt = 0;
|
2016-06-26 00:34:39 +02:00
|
|
|
|
2016-12-23 03:16:24 +01:00
|
|
|
static int rtctime;
|
|
|
|
|
|
2017-06-04 02:11:19 -04:00
|
|
|
void getnvrtime(void)
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
time_get(nvrram);
|
2016-06-26 00:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-04 02:11:19 -04:00
|
|
|
void nvr_recalc(void)
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
|
|
|
|
int c;
|
|
|
|
|
int newrtctime;
|
2016-08-15 23:40:02 +02:00
|
|
|
c = 1 << ((nvrram[RTC_REGA] & RTC_RS) - 1);
|
2016-06-26 00:34:39 +02:00
|
|
|
newrtctime=(int)(RTCCONST * c * (1 << TIMER_SHIFT));
|
|
|
|
|
if (rtctime>newrtctime) rtctime=newrtctime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nvr_rtc(void *p)
|
|
|
|
|
{
|
|
|
|
|
int c;
|
2016-08-15 23:40:02 +02:00
|
|
|
if (!(nvrram[RTC_REGA] & RTC_RS))
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
|
|
|
|
rtctime=0x7fffffff;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-08-15 23:40:02 +02:00
|
|
|
c = 1 << ((nvrram[RTC_REGA] & RTC_RS) - 1);
|
2016-06-26 00:34:39 +02:00
|
|
|
rtctime += (int)(RTCCONST * c * (1 << TIMER_SHIFT));
|
2016-08-15 23:40:02 +02:00
|
|
|
nvrram[RTC_REGC] |= RTC_PF;
|
|
|
|
|
if (nvrram[RTC_REGB] & RTC_PIE)
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
nvrram[RTC_REGC] |= RTC_IRQF;
|
2016-06-26 00:34:39 +02:00
|
|
|
if (AMSTRAD) picint(2);
|
|
|
|
|
else picint(0x100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 01:27:23 +02:00
|
|
|
int nvr_update_status = 0;
|
|
|
|
|
|
|
|
|
|
#define ALARM_DONTCARE 0xc0
|
|
|
|
|
|
|
|
|
|
int nvr_check_alarm(int nvraddr)
|
|
|
|
|
{
|
|
|
|
|
return (nvrram[nvraddr + 1] == nvrram[nvraddr] || (nvrram[nvraddr + 1] & ALARM_DONTCARE) == ALARM_DONTCARE);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-07 06:39:20 +01:00
|
|
|
int nvr_update_end_count = 0;
|
2016-07-19 02:44:32 +02:00
|
|
|
|
|
|
|
|
void nvr_update_end(void *p)
|
|
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
if (!(nvrram[RTC_REGB] & RTC_SET))
|
2016-07-19 02:44:32 +02:00
|
|
|
{
|
|
|
|
|
getnvrtime();
|
|
|
|
|
/* Clear update status. */
|
|
|
|
|
nvr_update_status = 0;
|
|
|
|
|
|
2016-08-15 23:40:02 +02:00
|
|
|
if (nvr_check_alarm(RTC_SECONDS) && nvr_check_alarm(RTC_MINUTES) && nvr_check_alarm(RTC_HOURS))
|
2016-07-19 02:44:32 +02:00
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
nvrram[RTC_REGC] |= RTC_AF;
|
|
|
|
|
if (nvrram[RTC_REGB] & RTC_AIE)
|
2016-07-19 02:44:32 +02:00
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
nvrram[RTC_REGC] |= RTC_IRQF;
|
2016-07-19 02:44:32 +02:00
|
|
|
if (AMSTRAD) picint(2);
|
|
|
|
|
else picint(0x100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The flag and interrupt should be issued on update ended, not started. */
|
2016-08-15 23:40:02 +02:00
|
|
|
nvrram[RTC_REGC] |= RTC_UF;
|
|
|
|
|
if (nvrram[RTC_REGB] & RTC_UIE)
|
2016-07-19 02:44:32 +02:00
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
nvrram[RTC_REGC] |= RTC_IRQF;
|
2016-07-19 02:44:32 +02:00
|
|
|
if (AMSTRAD) picint(2);
|
|
|
|
|
else picint(0x100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nvr_update_end_count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-26 00:34:39 +02:00
|
|
|
void nvr_onesec(void *p)
|
|
|
|
|
{
|
|
|
|
|
nvr_onesec_cnt++;
|
2016-07-19 02:44:32 +02:00
|
|
|
if (nvr_onesec_cnt >= 100)
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
if (!(nvrram[RTC_REGB] & RTC_SET))
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
nvr_update_status = RTC_UIP;
|
2016-08-15 23:33:51 +02:00
|
|
|
rtc_tick();
|
2016-07-11 01:27:23 +02:00
|
|
|
|
2016-07-19 02:44:32 +02:00
|
|
|
nvr_update_end_count = (int)((244.0 + 1984.0) * TIMER_USEC);
|
2016-07-11 01:27:23 +02:00
|
|
|
}
|
2016-08-15 23:33:51 +02:00
|
|
|
nvr_onesec_cnt = 0;
|
2016-06-26 00:34:39 +02:00
|
|
|
}
|
2016-07-19 02:44:32 +02:00
|
|
|
nvr_onesec_time += (int)(10000 * TIMER_USEC);
|
2016-06-26 00:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void writenvr(uint16_t addr, uint8_t val, void *priv)
|
|
|
|
|
{
|
|
|
|
|
int c, old;
|
|
|
|
|
if (addr&1)
|
|
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
if (nvraddr==RTC_REGC || nvraddr==RTC_REGD)
|
|
|
|
|
return; /* Registers C and D are read-only. There's no reason to continue. */
|
|
|
|
|
if (nvraddr > RTC_REGD && nvrram[nvraddr] != val)
|
2016-06-26 00:34:39 +02:00
|
|
|
nvr_dosave = 1;
|
2016-07-11 01:27:23 +02:00
|
|
|
|
|
|
|
|
old = nvrram[nvraddr];
|
|
|
|
|
nvrram[nvraddr]=val;
|
2016-06-26 00:34:39 +02:00
|
|
|
|
2016-08-15 23:40:02 +02:00
|
|
|
if (nvraddr == RTC_REGA)
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
if (val & RTC_RS)
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
c = 1 << ((val & RTC_RS) - 1);
|
2016-06-26 00:34:39 +02:00
|
|
|
rtctime += (int)(RTCCONST * c * (1 << TIMER_SHIFT));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
rtctime = 0x7fffffff;
|
|
|
|
|
}
|
2016-07-11 01:27:23 +02:00
|
|
|
else
|
|
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
if (nvraddr == RTC_REGB)
|
2016-07-11 01:27:23 +02:00
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
if (((old ^ val) & RTC_SET) && (val & RTC_SET))
|
2016-07-11 01:27:23 +02:00
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
nvrram[RTC_REGA] &= ~RTC_UIP; /* This has to be done according to the datasheet. */
|
|
|
|
|
nvrram[RTC_REGB] &= ~RTC_UIE; /* This also has to happen per the specification. */
|
2016-07-11 01:27:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-15 23:40:02 +02:00
|
|
|
if ((nvraddr < RTC_REGA) || (nvraddr == RTC_CENTURY))
|
2016-07-11 01:27:23 +02:00
|
|
|
{
|
2016-07-11 02:28:22 +02:00
|
|
|
if ((nvraddr != 1) && (nvraddr != 3) && (nvraddr != 5))
|
2016-07-11 01:27:23 +02:00
|
|
|
{
|
|
|
|
|
if ((old != val) && !enable_sync)
|
|
|
|
|
{
|
|
|
|
|
time_update(nvrram, nvraddr);
|
|
|
|
|
nvr_dosave = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-26 00:34:39 +02:00
|
|
|
}
|
|
|
|
|
else nvraddr=val&nvrmask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t readnvr(uint16_t addr, void *priv)
|
|
|
|
|
{
|
|
|
|
|
uint8_t temp;
|
|
|
|
|
if (addr&1)
|
|
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
if (nvraddr == RTC_REGA)
|
|
|
|
|
return ((nvrram[RTC_REGA] & 0x7F) | nvr_update_status);
|
|
|
|
|
if (nvraddr == RTC_REGD)
|
|
|
|
|
nvrram[RTC_REGD] |= RTC_VRT;
|
|
|
|
|
if (nvraddr == RTC_REGC)
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
|
|
|
|
if (AMSTRAD) picintc(2);
|
|
|
|
|
else picintc(0x100);
|
2016-08-15 23:40:02 +02:00
|
|
|
temp = nvrram[RTC_REGC];
|
|
|
|
|
nvrram[RTC_REGC] = 0;
|
2016-06-26 00:34:39 +02:00
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
return nvrram[nvraddr];
|
|
|
|
|
}
|
|
|
|
|
return nvraddr;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-04 02:11:19 -04:00
|
|
|
void loadnvr(void)
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
2017-06-08 01:52:23 +02:00
|
|
|
FILE *f = NULL;
|
2016-06-26 00:34:39 +02:00
|
|
|
int c;
|
|
|
|
|
nvrmask=63;
|
2017-06-08 00:58:47 +02:00
|
|
|
oldmodel = model;
|
|
|
|
|
|
|
|
|
|
wchar_t *model_name;
|
|
|
|
|
wchar_t *nvr_name;
|
|
|
|
|
|
|
|
|
|
model_name = (wchar_t *) malloc((strlen(model_get_internal_name_ex(model)) << 1) + 2);
|
|
|
|
|
mbstowcs(model_name, model_get_internal_name_ex(model), strlen(model_get_internal_name_ex(model)) + 1);
|
|
|
|
|
nvr_name = (wchar_t *) malloc((wcslen(model_name) << 1) + 2 + 8);
|
|
|
|
|
_swprintf(nvr_name, L"%s.nvr", model_name);
|
|
|
|
|
|
|
|
|
|
pclog_w(L"Opening NVR file: %s...\n", nvr_name);
|
|
|
|
|
|
|
|
|
|
if (model_get_nvrmask(model) != 0)
|
|
|
|
|
{
|
|
|
|
|
f = nvrfopen(nvr_name, L"rb");
|
|
|
|
|
nvrmask = model_get_nvrmask(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(nvr_name);
|
|
|
|
|
free(model_name);
|
|
|
|
|
|
2017-06-08 01:52:23 +02:00
|
|
|
if (!f || (model_get_nvrmask(model) == 0))
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
2017-06-08 01:52:23 +02:00
|
|
|
if (f)
|
|
|
|
|
{
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
2016-06-26 00:34:39 +02:00
|
|
|
memset(nvrram,0xFF,128);
|
2016-07-11 01:27:23 +02:00
|
|
|
if (!enable_sync)
|
|
|
|
|
{
|
2016-08-15 23:40:02 +02:00
|
|
|
nvrram[RTC_SECONDS] = nvrram[RTC_MINUTES] = nvrram[RTC_HOURS] = 0;
|
|
|
|
|
nvrram[RTC_DOM] = nvrram[RTC_MONTH] = 1;
|
|
|
|
|
nvrram[RTC_YEAR] = BCD(80);
|
|
|
|
|
nvrram[RTC_CENTURY] = BCD(19);
|
|
|
|
|
nvrram[RTC_REGB] = RTC_2412;
|
2016-07-11 01:27:23 +02:00
|
|
|
}
|
2016-06-26 00:34:39 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
fread(nvrram,128,1,f);
|
2016-08-15 23:33:51 +02:00
|
|
|
if (enable_sync)
|
|
|
|
|
time_internal_sync(nvrram);
|
|
|
|
|
else
|
|
|
|
|
time_internal_set_nvrram(nvrram); /* Update the internal clock state based on the NVR registers. */
|
2016-06-26 00:34:39 +02:00
|
|
|
fclose(f);
|
2016-08-15 23:40:02 +02:00
|
|
|
nvrram[RTC_REGA] = 6;
|
|
|
|
|
nvrram[RTC_REGB] = RTC_2412;
|
|
|
|
|
c = 1 << ((nvrram[RTC_REGA] & RTC_RS) - 1);
|
2016-06-26 00:34:39 +02:00
|
|
|
rtctime += (int)(RTCCONST * c * (1 << TIMER_SHIFT));
|
|
|
|
|
}
|
2017-06-08 00:58:47 +02:00
|
|
|
|
2017-06-04 02:11:19 -04:00
|
|
|
void savenvr(void)
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
2017-06-08 01:52:23 +02:00
|
|
|
FILE *f = NULL;
|
2017-06-08 00:58:47 +02:00
|
|
|
|
|
|
|
|
wchar_t *model_name;
|
|
|
|
|
wchar_t *nvr_name;
|
|
|
|
|
|
|
|
|
|
model_name = (wchar_t *) malloc((strlen(model_get_internal_name_ex(oldmodel)) << 1) + 2);
|
|
|
|
|
mbstowcs(model_name, model_get_internal_name_ex(oldmodel), strlen(model_get_internal_name_ex(oldmodel)) + 1);
|
|
|
|
|
nvr_name = (wchar_t *) malloc((wcslen(model_name) << 1) + 2 + 8);
|
|
|
|
|
_swprintf(nvr_name, L"%s.nvr", model_name);
|
|
|
|
|
|
|
|
|
|
pclog_w(L"Saving NVR file: %s...\n", nvr_name);
|
|
|
|
|
|
|
|
|
|
if (model_get_nvrmask(oldmodel) != 0)
|
|
|
|
|
{
|
|
|
|
|
f = nvrfopen(nvr_name, L"wb");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(nvr_name);
|
|
|
|
|
free(model_name);
|
|
|
|
|
|
2017-06-08 01:52:23 +02:00
|
|
|
if (!f || (model_get_nvrmask(oldmodel) == 0))
|
2017-06-08 00:58:47 +02:00
|
|
|
{
|
2017-06-08 01:52:23 +02:00
|
|
|
if (f)
|
|
|
|
|
{
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
2017-06-08 00:58:47 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-26 00:34:39 +02:00
|
|
|
fwrite(nvrram,128,1,f);
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-04 02:11:19 -04:00
|
|
|
void nvr_init(void)
|
2016-06-26 00:34:39 +02:00
|
|
|
{
|
|
|
|
|
io_sethandler(0x0070, 0x0002, readnvr, NULL, NULL, writenvr, NULL, NULL, NULL);
|
|
|
|
|
timer_add(nvr_rtc, &rtctime, TIMER_ALWAYS_ENABLED, NULL);
|
|
|
|
|
timer_add(nvr_onesec, &nvr_onesec_time, TIMER_ALWAYS_ENABLED, NULL);
|
2016-07-19 02:44:32 +02:00
|
|
|
timer_add(nvr_update_end, &nvr_update_end_count, &nvr_update_end_count, NULL);
|
2016-08-15 23:40:02 +02:00
|
|
|
|
2016-06-26 00:34:39 +02:00
|
|
|
}
|