uvmac/src/PROGMAIN.c

630 lines
12 KiB
C
Raw Normal View History

2019-07-23 02:50:34 +00:00
/*
PROGMAIN.c
Copyright (C) 2009 Bernd Schmidt, Philip Cummins, Paul C. Pratt
You can redistribute this file and/or modify it under the terms
of version 2 of the GNU General Public License as published by
the Free Software Foundation. You should have received a copy
of the license along with this file; see the file COPYING.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
license for more details.
*/
/*
PROGram MAIN.
*/
2020-03-03 20:13:19 +00:00
#include <string.h>
2020-03-03 18:31:11 +00:00
#include "SYSDEPNS.h"
#include "UI/MYOSGLUE.h"
#include "EMCONFIG.h"
#include "GLOBGLUE.h"
#include "HW/M68K/M68KITAB.h"
#include "HW/M68K/MINEM68K.h"
#include "HW/VIA/VIAEMDEV.h"
#include "HW/VIA/VIA2EMDV.h"
#include "HW/DISK/IWMEMDEV.h"
#include "HW/SCC/SCCEMDEV.h"
#include "HW/RTC/RTCEMDEV.h"
#include "PATCHES/ROMEMDEV.h"
#include "HW/SCSI/SCSIEMDV.h"
#include "HW/DISK/SONYEMDV.h"
#include "HW/SCREEN/SCRNEMDV.h"
#include "HW/VIDCARD/VIDEMDEV.h"
#include "HW/KBRD/KBRDEMDV.h"
#include "HW/POWERMAN/PMUEMDEV.h"
#include "HW/ADB/ADBEMDEV.h"
#include "HW/SOUND/ASCEMDEV.h"
#include "HW/SOUND/SNDEMDEV.h"
#include "HW/MOUSE/MOUSEMDV.h"
2019-07-23 02:50:34 +00:00
#include "PROGMAIN.h"
// Temporary location for config variables
uint16_t vMacScreenHeight = 342;
uint16_t vMacScreenWidth = 512;
uint16_t vMacScreenDepth = 0;
2020-03-03 18:31:11 +00:00
2020-03-03 20:13:19 +00:00
// Let's define a bunch of function structure thingies
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(*array))
typedef struct DevMethods {
bool (*init)(void);
void (*reset)(void);
void (*starttick)(void);
void (*endtick)(void);
2020-03-06 01:51:39 +00:00
void (*subtick)(int);
2020-03-03 20:13:19 +00:00
void (*timebegin)(void);
void (*timeend)(void);
} DevMethods_t;
const DevMethods_t DEVICES[] = {
// RTC
{
.init = EmRTC ? RTC_Init : NULL,
.reset = NULL,
.starttick = NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// ROM
{
.init = ROM_Init,
.reset = NULL,
.starttick = NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// Memory
{
.init = AddrSpac_Init,
.reset = Memory_Reset,
.starttick = NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// ICT
{
.init = NULL,
.reset = ICT_Zap,
.starttick = NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// IWM
{
.init = NULL,
.reset = IWM_Reset,
.starttick = NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// SCC
{
.init = NULL,
.reset = SCC_Reset,
.starttick = NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// SCSI
{
.init = NULL,
.reset = SCSI_Reset,
.starttick = NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// VIA1
{
.init = NULL,
.reset = VIA1_Zap,
.starttick = NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = VIA1_ExtraTimeBegin,
.timeend = VIA1_ExtraTimeEnd,
},
// VIA2
{
.init = NULL,
.reset = EmVIA2 ? VIA2_Zap : NULL,
.starttick = NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = EmVIA2 ? VIA2_ExtraTimeBegin : NULL,
.timeend = EmVIA2 ? VIA2_ExtraTimeEnd : NULL,
},
// Sony disk drive
{
.init = NULL,
.reset = Sony_Reset,
.starttick = Sony_Update,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// Extn
{
.init = NULL,
.reset = Extn_Reset,
.starttick = NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// m68k
{
.init = NULL,
.reset = m68k_reset,
.starttick = NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// Mouse
{
.init = NULL,
.reset = NULL,
.starttick = Mouse_Update,
.endtick = Mouse_EndTickNotify,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// Classic Keyboard
{
.init = NULL,
.reset = NULL,
.starttick = EmClassicKbrd ? KeyBoard_Update : NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// ADB
{
.init = NULL,
.reset = NULL,
.starttick = EmADB ? ADB_Update : NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// LocalTalk
/*{
.init = NULL,
.reset = NULL,
.starttick = EmLocalTalk ? LocalTalkTick : NULL,
.endtick = NULL,
.timebegin = NULL,
.timeend = NULL,
},*/
// Video card
{
.init = EmVidCard ? Vid_Init : NULL,
.reset = NULL,
.starttick = EmVidCard ? Vid_Update : NULL,
.endtick = NULL,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
// Screen
{
.init = NULL,
.reset = NULL,
.starttick = Sixtieth_PulseNtfy, // VBlank interrupt
.endtick = Screen_EndTickNotify,
2020-03-06 01:51:39 +00:00
.subtick = NULL,
2020-03-03 20:13:19 +00:00
.timebegin = NULL,
.timeend = NULL,
},
2020-03-06 01:51:39 +00:00
// ASC
{
.init = NULL,
.reset = NULL,
.starttick = NULL,
.endtick = NULL,
.subtick = EmASC ? ASC_SubTick : NULL,
.timebegin = NULL,
.timeend = NULL
},
// Sound (non-ASC)
{
.init = NULL,
.reset = NULL,
.starttick = NULL,
.endtick = NULL,
.subtick = (SoundEnabled && (CurEmMd != kEmMd_PB100)) ? MacSound_SubTick : NULL,
.timebegin = NULL,
.timeend = NULL
}
2020-03-03 20:13:19 +00:00
};
2019-07-23 02:50:34 +00:00
LOCALPROC EmulatedHardwareZap(void)
{
2020-03-03 20:13:19 +00:00
int i;
for ( i = 0; i < ARRAY_SIZE(DEVICES); i++ ) {
if (DEVICES[i].reset != NULL) { DEVICES[i].reset(); }
}
2019-07-23 02:50:34 +00:00
}
LOCALPROC DoMacReset(void)
{
Sony_EjectAllDisks();
EmulatedHardwareZap();
}
LOCALPROC InterruptReset_Update(void)
{
2020-03-06 01:51:39 +00:00
SetInterruptButton(false); // don't keep held over 1/60 sec
2019-07-23 02:50:34 +00:00
if (WantMacInterrupt) {
2020-02-11 05:34:32 +00:00
SetInterruptButton(true);
WantMacInterrupt = false;
2019-07-23 02:50:34 +00:00
}
if (WantMacReset) {
DoMacReset();
2020-02-11 05:34:32 +00:00
WantMacReset = false;
2019-07-23 02:50:34 +00:00
}
}
LOCALPROC SubTickNotify(int SubTick)
{
2020-03-06 01:51:39 +00:00
int i;
for ( i = 0; i < ARRAY_SIZE(DEVICES); i++ ) {
if (DEVICES[i].subtick != NULL) { DEVICES[i].subtick(SubTick); }
}
2019-07-23 02:50:34 +00:00
}
#define CyclesScaledPerTick (130240UL * ClockMult * kCycleScale)
2019-07-23 02:50:34 +00:00
#define CyclesScaledPerSubTick (CyclesScaledPerTick / kNumSubTicks)
LOCALVAR uint16_t SubTickCounter;
2019-07-23 02:50:34 +00:00
LOCALPROC SubTickTaskDo(void)
{
SubTickNotify(SubTickCounter);
++SubTickCounter;
if (SubTickCounter < (kNumSubTicks - 1)) {
/*
final SubTick handled by SubTickTaskEnd,
since CyclesScaledPerSubTick * kNumSubTicks
might not equal CyclesScaledPerTick.
*/
ICT_add(kICT_SubTick, CyclesScaledPerSubTick);
}
}
LOCALPROC SubTickTaskStart(void)
{
SubTickCounter = 0;
ICT_add(kICT_SubTick, CyclesScaledPerSubTick);
}
LOCALPROC SubTickTaskEnd(void)
{
SubTickNotify(kNumSubTicks - 1);
}
LOCALPROC SixtiethSecondNotify(void)
{
2020-03-03 20:13:19 +00:00
int i;
2020-03-06 01:51:39 +00:00
// Begin new frame
2019-07-23 02:50:34 +00:00
InterruptReset_Update();
2020-03-03 20:13:19 +00:00
for ( i = 0; i < ARRAY_SIZE(DEVICES); i++ ) {
if (DEVICES[i].starttick != NULL) { DEVICES[i].starttick(); }
}
2019-07-23 02:50:34 +00:00
SubTickTaskStart();
}
LOCALPROC SixtiethEndNotify(void)
{
2020-03-03 20:13:19 +00:00
int i;
2019-07-23 02:50:34 +00:00
SubTickTaskEnd();
2020-03-03 20:13:19 +00:00
for ( i = 0; i < ARRAY_SIZE(DEVICES); i++ ) {
if (DEVICES[i].endtick != NULL) { DEVICES[i].endtick(); }
}
2020-03-06 01:51:39 +00:00
// End frame
2019-07-23 02:50:34 +00:00
}
LOCALPROC ExtraTimeBeginNotify(void)
{
2020-03-06 01:51:39 +00:00
int i;
for ( i = 0; i < ARRAY_SIZE(DEVICES); i++ ) {
if (DEVICES[i].timebegin != NULL) { DEVICES[i].timebegin(); }
}
2019-07-23 02:50:34 +00:00
}
LOCALPROC ExtraTimeEndNotify(void)
{
2020-03-06 01:51:39 +00:00
int i;
for ( i = 0; i < ARRAY_SIZE(DEVICES); i++ ) {
if (DEVICES[i].timeend != NULL) { DEVICES[i].timeend(); }
}
2019-07-23 02:50:34 +00:00
}
GLOBALPROC EmulationReserveAlloc(void)
{
ReserveAllocOneBlock(&RAM, kRAM_Size + RAMSafetyMarginFudge, 5, false);
2019-07-23 02:50:34 +00:00
#if EmVidCard
2020-03-03 18:31:11 +00:00
ReserveAllocOneBlock(&VidROM, kVidROM_Size, 5, false);
2019-07-23 02:50:34 +00:00
#endif
#if IncludeVidMem
ReserveAllocOneBlock(&VidMem, kVidMemRAM_Size + RAMSafetyMarginFudge, 5, true);
2019-07-23 02:50:34 +00:00
#endif
#if SmallGlobals
MINEM68K_ReserveAlloc();
#endif
}
2020-02-11 05:34:32 +00:00
LOCALFUNC bool InitEmulation(void)
2019-07-23 02:50:34 +00:00
{
2020-03-03 20:13:19 +00:00
int i;
for ( i = 0; i < ARRAY_SIZE(DEVICES); i++ ) {
if (DEVICES[i].init != NULL) {
bool retval = DEVICES[i].init();
if (retval == false) { return false; }
}
}
2020-03-03 18:31:11 +00:00
2020-03-03 20:13:19 +00:00
EmulatedHardwareZap();
return true;
2019-07-23 02:50:34 +00:00
}
LOCALPROC ICT_DoTask(int taskid)
{
switch (taskid) {
case kICT_SubTick:
SubTickTaskDo();
break;
#if EmClassicKbrd
case kICT_Kybd_ReceiveEndCommand:
DoKybd_ReceiveEndCommand();
break;
case kICT_Kybd_ReceiveCommand:
DoKybd_ReceiveCommand();
break;
#endif
#if EmADB
case kICT_ADB_NewState:
ADB_DoNewState();
break;
#endif
#if EmPMU
case kICT_PMU_Task:
PMU_DoTask();
break;
#endif
case kICT_VIA1_Timer1Check:
VIA1_DoTimer1Check();
break;
case kICT_VIA1_Timer2Check:
VIA1_DoTimer2Check();
break;
#if EmVIA2
case kICT_VIA2_Timer1Check:
VIA2_DoTimer1Check();
break;
case kICT_VIA2_Timer2Check:
VIA2_DoTimer2Check();
break;
#endif
default:
ReportAbnormalID(0x1001, "unknown taskid in ICT_DoTask");
break;
}
}
LOCALPROC ICT_DoCurrentTasks(void)
{
2020-03-06 01:51:39 +00:00
int i;
uimr m;
// For each ICT...
for (m = ICTactive, i = 0; m != 0; m >>=1, i += 1) {
// If no ICT here, continue
if (0 == (m & 1)) { continue; }
if (ICTwhen[i] != NextiCount) { continue; }
/*
A Task may set the time of any task, including
itself. But it cannot set any task to execute
immediately, so one pass is sufficient.
*/
ICTactive &= ~(1 << i); // Clear active bit
ICT_DoTask(i);
2019-07-23 02:50:34 +00:00
}
}
LOCALFUNC uint32_t ICT_DoGetNext(uint32_t maxn)
2019-07-23 02:50:34 +00:00
{
2020-03-06 01:51:39 +00:00
int i;
uimr m;
uint32_t v = maxn;
2019-07-23 02:50:34 +00:00
2020-03-06 01:51:39 +00:00
for (m = ICTactive, i = 0; m != 0; m >>=1, i += 1) {
uint32_t d = ICTwhen[i] - NextiCount;
if (0 == (m & 1)) { continue; }
if (d < v) { v = d; }
2019-07-23 02:50:34 +00:00
}
return v;
}
LOCALPROC m68k_go_nCycles_1(uint32_t n)
2019-07-23 02:50:34 +00:00
{
uint32_t n2;
uint32_t StopiCount = NextiCount + n;
2019-07-23 02:50:34 +00:00
do {
ICT_DoCurrentTasks();
n2 = ICT_DoGetNext(n);
NextiCount += n2;
m68k_go_nCycles(n2);
n = StopiCount - NextiCount;
} while (n != 0);
}
LOCALVAR uint32_t ExtraSubTicksToDo = 0;
2019-07-23 02:50:34 +00:00
LOCALPROC DoEmulateOneTick(void)
{
// AutoSlow
uint32_t NewQuietTime = QuietTime + 1;
uint32_t NewQuietSubTicks = QuietSubTicks + kNumSubTicks;
2019-07-23 02:50:34 +00:00
if (NewQuietTime > QuietTime) {
/* if not overflow */
QuietTime = NewQuietTime;
}
if (NewQuietSubTicks > QuietSubTicks) {
/* if not overflow */
QuietSubTicks = NewQuietSubTicks;
2019-07-23 02:50:34 +00:00
}
SixtiethSecondNotify();
m68k_go_nCycles_1(CyclesScaledPerTick);
SixtiethEndNotify();
if ((uint8_t) -1 == SpeedValue) {
ExtraSubTicksToDo = (uint32_t) -1;
2019-07-23 02:50:34 +00:00
} else {
uint32_t ExtraAdd = (kNumSubTicks << SpeedValue) - kNumSubTicks;
uint32_t ExtraLimit = ExtraAdd << 3;
2019-07-23 02:50:34 +00:00
ExtraSubTicksToDo += ExtraAdd;
if (ExtraSubTicksToDo > ExtraLimit) {
ExtraSubTicksToDo = ExtraLimit;
}
}
}
2020-02-11 05:34:32 +00:00
LOCALFUNC bool MoreSubTicksToDo(void)
2019-07-23 02:50:34 +00:00
{
2020-02-11 05:34:32 +00:00
bool v = false;
2019-07-23 02:50:34 +00:00
if (ExtraTimeNotOver() && (ExtraSubTicksToDo > 0)) {
if ( (QuietSubTicks >= 16384) && (QuietTime >= 34)) {
2019-07-23 02:50:34 +00:00
ExtraSubTicksToDo = 0;
} else {
2020-02-11 05:34:32 +00:00
v = true;
2019-07-23 02:50:34 +00:00
}
}
return v;
}
2020-03-06 01:51:39 +00:00
/*
DoEmulateExtraTime is used for anything over emulation speed of 1x. It
periodically calls ExtraTimeNotOver and stops when this returns false
(or it is finished with emulating the extra time).
*/
2019-07-23 02:50:34 +00:00
LOCALPROC DoEmulateExtraTime(void)
{
if (MoreSubTicksToDo()) {
ExtraTimeBeginNotify();
do {
uint32_t NewQuietSubTicks = QuietSubTicks + 1;
2019-07-23 02:50:34 +00:00
if (NewQuietSubTicks > QuietSubTicks) {
/* if not overflow */
QuietSubTicks = NewQuietSubTicks;
2019-07-23 02:50:34 +00:00
}
m68k_go_nCycles_1(CyclesScaledPerSubTick);
--ExtraSubTicksToDo;
} while (MoreSubTicksToDo());
ExtraTimeEndNotify();
}
}
2020-03-06 01:51:39 +00:00
/*
The number of ticks that have been emulated so far.
That is, the number of times "DoEmulateOneTick" has been called.
*/
LOCALVAR uint32_t CurEmulatedTime = 0;
2019-07-23 02:50:34 +00:00
2020-03-06 01:51:39 +00:00
/*
The general idea is to call DoEmulateOneTick once per tick.
But if emulation is lagging, we'll try to catch up by calling
DoEmulateOneTick multiple times, unless we're too far behind, in
which case we forget it.
2019-07-23 02:50:34 +00:00
2020-03-06 01:51:39 +00:00
If emulating one tick takes longer than a tick we don't want to sit
here forever. So the maximum number of calls to DoEmulateOneTick is
determined at the beginning, rather than just calling DoEmulateOneTick
until CurEmulatedTime >= TrueEmulatedTime.
*/
2019-07-23 02:50:34 +00:00
LOCALPROC RunEmulatedTicksToTrueTime(void)
{
2020-03-06 01:51:39 +00:00
int8_t lag = OnTrueTime - CurEmulatedTime;
2019-07-23 02:50:34 +00:00
2020-03-06 01:51:39 +00:00
if (lag > 0) {
DoEmulateOneTick();
CurEmulatedTime += 1;
2019-07-23 02:50:34 +00:00
DoneWithDrawingForTick();
2020-03-06 01:51:39 +00:00
if (lag > 8) {
2019-07-23 02:50:34 +00:00
/* emulation not fast enough */
2020-03-06 01:51:39 +00:00
lag = 8;
CurEmulatedTime = OnTrueTime - lag;
} else {
lag -= 1;
2019-07-23 02:50:34 +00:00
}
2020-03-06 01:51:39 +00:00
while (ExtraTimeNotOver() && (lag > 0)) {
/* Missed vblank due to lag; catch up */
2020-02-11 05:34:32 +00:00
EmVideoDisable = true;
2020-03-06 01:51:39 +00:00
DoEmulateOneTick();
CurEmulatedTime += 1;
lag -= 1;
2020-02-11 05:34:32 +00:00
EmVideoDisable = false;
2019-07-23 02:50:34 +00:00
}
2020-03-06 01:51:39 +00:00
EmLagTime = lag;
2019-07-23 02:50:34 +00:00
}
}
LOCALPROC MainEventLoop(void)
{
2020-03-06 01:51:39 +00:00
while (true) {
2019-07-23 02:50:34 +00:00
WaitForNextTick();
2020-03-06 01:51:39 +00:00
if (ForceMacOff) { return; }
2019-07-23 02:50:34 +00:00
RunEmulatedTicksToTrueTime();
DoEmulateExtraTime();
}
}
GLOBALPROC ProgramMain(void)
{
2020-03-06 01:51:39 +00:00
if (InitEmulation() == false) {return;}
MainEventLoop();
2019-07-23 02:50:34 +00:00
}