2013-06-20 19:00:07 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: GPL - See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS Virtual DOS Machine
|
|
|
|
* FILE: hardware.c
|
|
|
|
* PURPOSE: Minimal hardware emulation
|
|
|
|
* PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *******************************************************************/
|
|
|
|
|
|
|
|
#include "ntvdm.h"
|
|
|
|
|
|
|
|
typedef struct _PIC
|
|
|
|
{
|
|
|
|
BOOLEAN Initialization;
|
|
|
|
BYTE MaskRegister;
|
|
|
|
BYTE InServiceRegister;
|
|
|
|
BYTE IntOffset;
|
|
|
|
BYTE ConfigRegister;
|
|
|
|
BYTE CascadeRegister;
|
|
|
|
BOOLEAN CascadeRegisterSet;
|
|
|
|
BOOLEAN AutoEoi;
|
|
|
|
BOOLEAN Slave;
|
|
|
|
BOOLEAN ReadIsr;
|
|
|
|
} PIC, *PPIC;
|
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PIT_MODE_INT_ON_TERMINAL_COUNT,
|
|
|
|
PIT_MODE_HARDWARE_ONE_SHOT,
|
|
|
|
PIT_MODE_RATE_GENERATOR,
|
|
|
|
PIT_MODE_SQUARE_WAVE,
|
|
|
|
PIT_MODE_SOFTWARE_STROBE,
|
|
|
|
PIT_MODE_HARDWARE_STROBE
|
|
|
|
};
|
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
typedef struct _PIT_CHANNEL
|
|
|
|
{
|
2013-06-21 13:55:31 +00:00
|
|
|
WORD ReloadValue;
|
|
|
|
WORD CurrentValue;
|
|
|
|
WORD LatchedValue;
|
|
|
|
INT Mode;
|
2013-06-21 00:47:07 +00:00
|
|
|
BOOLEAN Pulsed;
|
2013-06-21 13:55:31 +00:00
|
|
|
BOOLEAN LatchSet;
|
|
|
|
BOOLEAN InputFlipFlop;
|
|
|
|
BOOLEAN OutputFlipFlop;
|
2013-06-21 00:47:07 +00:00
|
|
|
BYTE AccessMode;
|
|
|
|
} PIT_CHANNEL, *PPIT_CHANNEL;
|
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
static PIC MasterPic, SlavePic;
|
2013-06-21 00:47:07 +00:00
|
|
|
static PIT_CHANNEL PitChannels[PIT_CHANNELS];
|
2013-06-21 21:25:01 +00:00
|
|
|
static BYTE KeyboardQueue[KEYBOARD_BUFFER_SIZE];
|
|
|
|
static BOOLEAN KeyboardQueueEmpty = TRUE;
|
|
|
|
static UINT KeyboardQueueStart = 0;
|
|
|
|
static UINT KeyboardQueueEnd = 0;
|
|
|
|
|
|
|
|
static BOOLEAN KeyboardQueuePush(BYTE ScanCode)
|
|
|
|
{
|
|
|
|
/* Check if the keyboard queue is full */
|
|
|
|
if (!KeyboardQueueEmpty && (KeyboardQueueStart == KeyboardQueueEnd))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Insert the value in the queue */
|
|
|
|
KeyboardQueue[KeyboardQueueEnd] = ScanCode;
|
|
|
|
KeyboardQueueEnd++;
|
|
|
|
KeyboardQueueEnd %= KEYBOARD_BUFFER_SIZE;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Since we inserted a value, it's not empty anymore */
|
|
|
|
KeyboardQueueEmpty = FALSE;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static BOOLEAN KeyboardQueuePop(BYTE *ScanCode)
|
|
|
|
{
|
|
|
|
/* Make sure the keyboard queue is not empty */
|
|
|
|
if (KeyboardQueueEmpty) return FALSE;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Get the scan code */
|
|
|
|
*ScanCode = KeyboardQueue[KeyboardQueueStart];
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Remove the value from the queue */
|
|
|
|
KeyboardQueueStart++;
|
|
|
|
KeyboardQueueStart %= KEYBOARD_BUFFER_SIZE;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Check if the queue is now empty */
|
|
|
|
if (KeyboardQueueStart == KeyboardQueueEnd)
|
|
|
|
{
|
|
|
|
KeyboardQueueEmpty = TRUE;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
2013-06-21 00:47:07 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* PUBLIC FUNCTIONS ***********************************************************/
|
|
|
|
|
|
|
|
BYTE PicReadCommand(BYTE Port)
|
|
|
|
{
|
|
|
|
PPIC Pic;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* Which PIC are we accessing? */
|
|
|
|
if (Port == PIC_MASTER_CMD) Pic = &MasterPic;
|
|
|
|
else Pic = &SlavePic;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
if (Pic->ReadIsr)
|
|
|
|
{
|
|
|
|
/* Read the in-service register */
|
|
|
|
Pic->ReadIsr = FALSE;
|
|
|
|
return Pic->InServiceRegister;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The IRR is always 0, as the emulated CPU receives the interrupt instantly */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID PicWriteCommand(BYTE Port, BYTE Value)
|
|
|
|
{
|
|
|
|
PPIC Pic;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* Which PIC are we accessing? */
|
|
|
|
if (Port == PIC_MASTER_CMD) Pic = &MasterPic;
|
|
|
|
else Pic = &SlavePic;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
if (Value & PIC_ICW1)
|
|
|
|
{
|
|
|
|
/* Start initialization */
|
|
|
|
Pic->Initialization = TRUE;
|
|
|
|
Pic->IntOffset = 0xFF;
|
|
|
|
Pic->CascadeRegisterSet = FALSE;
|
|
|
|
Pic->ConfigRegister = Value;
|
|
|
|
return;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
if (Value & PIC_OCW3)
|
|
|
|
{
|
|
|
|
/* This is an OCR3 */
|
|
|
|
if (Value == PIC_OCW3_READ_ISR)
|
|
|
|
{
|
|
|
|
/* Return the ISR on next read from command port */
|
|
|
|
Pic->ReadIsr = TRUE;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* This is an OCW2 */
|
|
|
|
if (Value & PIC_OCW2_EOI)
|
|
|
|
{
|
|
|
|
if (Value & PIC_OCW2_SL)
|
|
|
|
{
|
|
|
|
/* If the SL bit is set, clear a specific IRQ */
|
|
|
|
Pic->InServiceRegister &= ~(1 << (Value & PIC_OCW2_NUM_MASK));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, clear all of them */
|
|
|
|
Pic->InServiceRegister = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BYTE PicReadData(BYTE Port)
|
|
|
|
{
|
|
|
|
/* Read the mask register */
|
|
|
|
if (Port == PIC_MASTER_DATA) return MasterPic.MaskRegister;
|
|
|
|
else return SlavePic.MaskRegister;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID PicWriteData(BYTE Port, BYTE Value)
|
|
|
|
{
|
|
|
|
PPIC Pic;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* Which PIC are we accessing? */
|
|
|
|
if (Port == PIC_MASTER_DATA) Pic = &MasterPic;
|
|
|
|
else Pic = &SlavePic;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* Is the PIC ready? */
|
|
|
|
if (!Pic->Initialization)
|
|
|
|
{
|
|
|
|
/* Yes, this is an OCW1 */
|
|
|
|
Pic->MaskRegister = Value;
|
|
|
|
return;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* Has the interrupt offset been set? */
|
|
|
|
if (Pic->IntOffset == 0xFF)
|
|
|
|
{
|
|
|
|
/* This is an ICW2, set the offset (last three bits always zero) */
|
|
|
|
Pic->IntOffset = Value & 0xF8;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* Check if we are in single mode and don't need an ICW4 */
|
|
|
|
if ((Pic->ConfigRegister & PIC_ICW1_SINGLE)
|
|
|
|
&& !(Pic->ConfigRegister & PIC_ICW1_ICW4))
|
|
|
|
{
|
|
|
|
/* Yes, done initializing */
|
|
|
|
Pic->Initialization = FALSE;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* Check if we are in cascade mode and the cascade register was not set */
|
|
|
|
if (!(Pic->ConfigRegister & PIC_ICW1_SINGLE) && !Pic->CascadeRegisterSet)
|
|
|
|
{
|
|
|
|
/* This is an ICW3 */
|
|
|
|
Pic->CascadeRegister = Value;
|
|
|
|
Pic->CascadeRegisterSet = TRUE;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* Check if we need an ICW4 */
|
|
|
|
if (!(Pic->ConfigRegister & PIC_ICW1_ICW4))
|
|
|
|
{
|
|
|
|
/* No, done initializing */
|
|
|
|
Pic->Initialization = FALSE;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* This must be an ICW4, we will ignore the 8086 bit (assume always set) */
|
|
|
|
if (Value & PIC_ICW4_AEOI)
|
|
|
|
{
|
|
|
|
/* Use automatic end-of-interrupt */
|
|
|
|
Pic->AutoEoi = TRUE;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/* Done initializing */
|
|
|
|
Pic->Initialization = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID PicInterruptRequest(BYTE Number)
|
2013-06-23 00:11:45 +00:00
|
|
|
{
|
2013-06-21 01:44:56 +00:00
|
|
|
BYTE i;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
if (Number >= 0 && Number < 8)
|
|
|
|
{
|
2013-06-21 01:44:56 +00:00
|
|
|
/* Check if any of the higher-priorirty interrupts are busy */
|
|
|
|
for (i = 0; i <= Number ; i++)
|
2013-06-20 19:00:07 +00:00
|
|
|
{
|
2013-06-21 01:44:56 +00:00
|
|
|
if (MasterPic.InServiceRegister & (1 << Number)) return;
|
2013-06-20 19:00:07 +00:00
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 01:44:56 +00:00
|
|
|
/* Check if the interrupt is masked */
|
|
|
|
if (MasterPic.MaskRegister & (1 << Number)) return;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 01:44:56 +00:00
|
|
|
/* Set the appropriate bit in the ISR and interrupt the CPU */
|
2013-06-21 00:47:07 +00:00
|
|
|
if (!MasterPic.AutoEoi) MasterPic.InServiceRegister |= 1 << Number;
|
2013-06-20 19:00:07 +00:00
|
|
|
EmulatorInterrupt(MasterPic.IntOffset + Number);
|
|
|
|
}
|
|
|
|
else if (Number >= 8 && Number < 16)
|
|
|
|
{
|
|
|
|
Number -= 8;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-20 19:00:07 +00:00
|
|
|
/*
|
|
|
|
* The slave PIC is connected to IRQ 2, always! If the master PIC
|
|
|
|
* was misconfigured, don't do anything.
|
|
|
|
*/
|
|
|
|
if (!(MasterPic.CascadeRegister & (1 << 2))
|
|
|
|
|| SlavePic.CascadeRegister != 2)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 01:44:56 +00:00
|
|
|
/* Check if any of the higher-priorirty interrupts are busy */
|
|
|
|
if (MasterPic.InServiceRegister != 0) return;
|
|
|
|
for (i = 0; i <= Number ; i++)
|
2013-06-20 19:00:07 +00:00
|
|
|
{
|
2013-06-21 01:44:56 +00:00
|
|
|
if (SlavePic.InServiceRegister & (1 << Number)) return;
|
2013-06-20 19:00:07 +00:00
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 01:44:56 +00:00
|
|
|
/* Check if the interrupt is masked */
|
|
|
|
if (SlavePic.MaskRegister & (1 << Number)) return;
|
|
|
|
|
|
|
|
/* Set the IRQ 2 bit in the master ISR */
|
|
|
|
if (!MasterPic.AutoEoi) MasterPic.InServiceRegister |= 1 << 2;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 01:44:56 +00:00
|
|
|
/* Set the appropriate bit in the ISR and interrupt the CPU */
|
2013-06-21 00:47:07 +00:00
|
|
|
if (!SlavePic.AutoEoi) SlavePic.InServiceRegister |= 1 << Number;
|
2013-06-20 19:00:07 +00:00
|
|
|
EmulatorInterrupt(SlavePic.IntOffset + Number);
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:47:07 +00:00
|
|
|
|
|
|
|
VOID PitWriteCommand(BYTE Value)
|
|
|
|
{
|
|
|
|
BYTE Channel = Value >> 6;
|
|
|
|
BYTE Mode = (Value >> 1) & 0x07;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Check if this is a counter latch command */
|
|
|
|
if (((Value >> 4) & 3) == 0)
|
|
|
|
{
|
|
|
|
PitChannels[Channel].LatchSet = TRUE;
|
|
|
|
PitChannels[Channel].LatchedValue = PitChannels[Channel].CurrentValue;
|
|
|
|
return;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Set the access mode and reset flip-flops */
|
2013-06-21 00:47:07 +00:00
|
|
|
PitChannels[Channel].AccessMode = (Value >> 4) & 3;
|
2013-06-21 13:55:31 +00:00
|
|
|
PitChannels[Channel].Pulsed = FALSE;
|
|
|
|
PitChannels[Channel].LatchSet = FALSE;
|
|
|
|
PitChannels[Channel].InputFlipFlop = FALSE;
|
|
|
|
PitChannels[Channel].OutputFlipFlop = FALSE;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
switch (Mode)
|
|
|
|
{
|
|
|
|
case 0:
|
2013-06-21 13:55:31 +00:00
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2013-06-21 00:47:07 +00:00
|
|
|
case 4:
|
2013-06-21 13:55:31 +00:00
|
|
|
case 5:
|
2013-06-21 00:47:07 +00:00
|
|
|
{
|
2013-06-21 13:55:31 +00:00
|
|
|
PitChannels[Channel].Mode = Mode;
|
2013-06-21 00:47:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
case 6:
|
2013-06-21 00:47:07 +00:00
|
|
|
{
|
2013-06-21 13:55:31 +00:00
|
|
|
PitChannels[Channel].Mode = PIT_MODE_RATE_GENERATOR;
|
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
case 7:
|
|
|
|
{
|
|
|
|
PitChannels[Channel].Mode = PIT_MODE_SQUARE_WAVE;
|
2013-06-21 00:47:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
BYTE PitReadData(BYTE Channel)
|
|
|
|
{
|
|
|
|
WORD CurrentValue = PitChannels[Channel].CurrentValue;
|
|
|
|
BYTE AccessMode = PitChannels[Channel].AccessMode;
|
|
|
|
|
|
|
|
/* Check if the value was latched */
|
|
|
|
if (PitChannels[Channel].LatchSet)
|
|
|
|
{
|
|
|
|
CurrentValue = PitChannels[Channel].LatchedValue;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
if (AccessMode == 1 || AccessMode == 2)
|
|
|
|
{
|
|
|
|
/* The latched value was read as one byte */
|
|
|
|
PitChannels[Channel].LatchSet = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Use the flip-flop for access mode 3 */
|
|
|
|
if (AccessMode == 3)
|
|
|
|
{
|
|
|
|
AccessMode = PitChannels[Channel].InputFlipFlop ? 1 : 2;
|
|
|
|
PitChannels[Channel].InputFlipFlop = !PitChannels[Channel].InputFlipFlop;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Check if this was the last read for the latched value */
|
|
|
|
if (!PitChannels[Channel].InputFlipFlop)
|
|
|
|
{
|
|
|
|
/* Yes, the latch value was read as two bytes */
|
|
|
|
PitChannels[Channel].LatchSet = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
switch (AccessMode)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
/* Low byte */
|
|
|
|
return CurrentValue & 0x00FF;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
/* High byte */
|
|
|
|
return CurrentValue >> 8;
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Shouldn't get here */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
VOID PitWriteData(BYTE Channel, BYTE Value)
|
|
|
|
{
|
2013-06-21 13:55:31 +00:00
|
|
|
BYTE AccessMode = PitChannels[Channel].AccessMode;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
/* Use the flip-flop for access mode 3 */
|
|
|
|
if (PitChannels[Channel].AccessMode == 3)
|
|
|
|
{
|
2013-06-21 13:55:31 +00:00
|
|
|
AccessMode = PitChannels[Channel].InputFlipFlop ? 1 : 2;
|
|
|
|
PitChannels[Channel].InputFlipFlop = !PitChannels[Channel].InputFlipFlop;
|
2013-06-21 00:47:07 +00:00
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
switch (AccessMode)
|
2013-06-21 00:47:07 +00:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
/* Low byte */
|
|
|
|
PitChannels[Channel].ReloadValue &= 0xFF00;
|
|
|
|
PitChannels[Channel].ReloadValue |= Value;
|
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 00:47:07 +00:00
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
/* High byte */
|
|
|
|
PitChannels[Channel].ReloadValue &= 0x00FF;
|
|
|
|
PitChannels[Channel].ReloadValue |= Value << 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
VOID PitDecrementCount()
|
2013-06-21 00:47:07 +00:00
|
|
|
{
|
2013-06-21 13:55:31 +00:00
|
|
|
INT i;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
for (i = 0; i < PIT_CHANNELS; i++)
|
|
|
|
{
|
|
|
|
switch (PitChannels[i].Mode)
|
|
|
|
{
|
|
|
|
case PIT_MODE_INT_ON_TERMINAL_COUNT:
|
|
|
|
{
|
|
|
|
/* Decrement the value */
|
|
|
|
PitChannels[i].CurrentValue--;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Did it fall to the terminal count? */
|
|
|
|
if (PitChannels[i].CurrentValue == 0 && !PitChannels[i].Pulsed)
|
|
|
|
{
|
|
|
|
/* Yes, raise the output line */
|
|
|
|
if (i == 0) PicInterruptRequest(0);
|
|
|
|
PitChannels[i].Pulsed = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
case PIT_MODE_RATE_GENERATOR:
|
|
|
|
{
|
|
|
|
/* Decrement the value */
|
|
|
|
PitChannels[i].CurrentValue--;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Did it fall to zero? */
|
|
|
|
if (PitChannels[i].CurrentValue != 0) break;
|
2013-06-21 00:47:07 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Yes, raise the output line and reload */
|
|
|
|
if (i == 0) PicInterruptRequest(0);
|
|
|
|
PitChannels[i].CurrentValue = PitChannels[i].ReloadValue;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
case PIT_MODE_SQUARE_WAVE:
|
|
|
|
{
|
|
|
|
/* Decrement the value by 2 */
|
|
|
|
PitChannels[i].CurrentValue -= 2;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Did it fall to zero? */
|
|
|
|
if (PitChannels[i].CurrentValue != 0) break;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Yes, toggle the flip-flop */
|
|
|
|
PitChannels[i].OutputFlipFlop = !PitChannels[i].OutputFlipFlop;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Did this create a rising edge in the signal? */
|
|
|
|
if (PitChannels[i].OutputFlipFlop)
|
|
|
|
{
|
|
|
|
/* Yes, IRQ 0 if this is channel 0 */
|
|
|
|
if (i == 0) PicInterruptRequest(0);
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
/* Reload the value, but make sure it's even */
|
|
|
|
if (PitChannels[i].ReloadValue % 2)
|
|
|
|
{
|
|
|
|
/* It's odd, reduce it by 1 */
|
|
|
|
PitChannels[i].CurrentValue = PitChannels[i].ReloadValue - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It was even */
|
|
|
|
PitChannels[i].CurrentValue = PitChannels[i].ReloadValue;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
case PIT_MODE_SOFTWARE_STROBE:
|
|
|
|
{
|
|
|
|
// TODO: NOT IMPLEMENTED
|
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 13:55:31 +00:00
|
|
|
case PIT_MODE_HARDWARE_ONE_SHOT:
|
|
|
|
case PIT_MODE_HARDWARE_STROBE:
|
|
|
|
{
|
|
|
|
/* These modes do not work on x86 PCs */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:47:07 +00:00
|
|
|
}
|
2013-06-21 13:55:31 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
VOID CheckForInputEvents()
|
|
|
|
{
|
|
|
|
PINPUT_RECORD Buffer;
|
|
|
|
HANDLE ConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
DWORD i, j, Count, TotalEvents;
|
|
|
|
BYTE ScanCode;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Get the number of input events */
|
|
|
|
if (!GetNumberOfConsoleInputEvents(ConsoleInput, &Count)) return;
|
|
|
|
if (Count == 0) return;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Allocate the buffer */
|
|
|
|
Buffer = (PINPUT_RECORD)HeapAlloc(GetProcessHeap(), 0, Count * sizeof(INPUT_RECORD));
|
|
|
|
if (Buffer == NULL) return;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Peek the input events */
|
|
|
|
if (!ReadConsoleInput(ConsoleInput, Buffer, Count, &TotalEvents)) goto Cleanup;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
for (i = 0; i < TotalEvents; i++)
|
|
|
|
{
|
|
|
|
/* Check if this is a key event */
|
|
|
|
if (Buffer[i].EventType != KEY_EVENT) continue;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Get the scan code */
|
|
|
|
ScanCode = Buffer[i].Event.KeyEvent.wVirtualScanCode;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* If this is a key release, set the highest bit in the scan code */
|
|
|
|
if (!Buffer[i].Event.KeyEvent.bKeyDown) ScanCode |= 0x80;
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Push the scan code onto the keyboard queue */
|
|
|
|
for (j = 0; j < Buffer[i].Event.KeyEvent.wRepeatCount; j++)
|
|
|
|
{
|
|
|
|
KeyboardQueuePush(ScanCode);
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Yes, IRQ 1 */
|
|
|
|
PicInterruptRequest(1);
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
/* Stop the loop */
|
|
|
|
break;
|
|
|
|
}
|
2013-06-23 00:11:45 +00:00
|
|
|
|
2013-06-21 21:25:01 +00:00
|
|
|
Cleanup:
|
|
|
|
HeapFree(GetProcessHeap(), 0, Buffer);
|
|
|
|
}
|