Table of Contents Previous topic Next topic
Extension Examples->Counters 0 and 1->Extension Source Code
/*
* This single file is a complete extension, written in C, for the Crossware 8051 Virtual Workshop.
* It increments counter 0 every 5 machine cycles and increments counter 1 every 50 machine cycles.
*
* As written (ie using DllExport), it will compile with the Microsoft Visual C++ compiler.
*
* Copyright (c) 1998 Crossware Products.
*
* This source code is provided as an example to complement the Crossware 8051 Virtual Workshop.
*/
#define DllExport __declspec( dllexport )
#define LONG long
#define BYTE unsigned char
#define TRUE 1
#define FALSE 0
LONG g_nMachineCycles;
LONG g_nCounter0Cycles;
LONG g_nCounter1Cycles;
LONG* g_pnError; // placing a non-zero value in here will halt the simulator
DllExport LONG Initialise(LONG* pnError)
{
g_pnError = pnError; // save the address of the simulators error indicator
return 1; // must return a non-zero value otherwise extension will be ignored
}
DllExport void SetMachineCycles(LONG nExtensionState, LONG nCycles)
{
g_nMachineCycles = nCycles;
}
DllExport void IncMachineCycles(LONG nExtensionState, LONG nCycles)
{
// we need to keep track of the machine cycles so intercept this call
g_nMachineCycles += nCycles;
}
DllExport void GetPortPins(LONG nExtensionState, BYTE nPortAddress, BYTE* pnPins, BYTE* pnHandledPins, LONG bSimulating)
{
// Clear or set only the pins relevant to this extension
// If you set or clear any of the pins, then set the corresponding bit in pnHandledPins
if (nPortAddress == 0XB0)
{
// Port 3
// one count every 5 machine cycles
if (g_nMachineCycles - g_nCounter0Cycles > 5)
{
*pnPins &= ~0X10; // clear the pin P3.4 to trigger counter 0
g_nCounter0Cycles = g_nMachineCycles;
}
else
*pnPins |= 0X10; // set the pin
*pnHandledPins |= 0X10; // tell the simulator that we have handled this pin
// one count every 50 machine cycles
if (g_nMachineCycles - g_nCounter1Cycles > 50)
{
*pnPins &= ~0X20; // clear the pin P3.5 to trigger counter 1
g_nCounter1Cycles = g_nMachineCycles;
}
else
*pnPins |= 0X20; // set the pin
*pnHandledPins |= 0X20; // tell the simulator that we have handled this pin
}
}
DllExport void ResetState(LONG nExtensionState)
{
// simulator is being reset, reset our own variables
g_nMachineCycles = 0;
g_nCounter0Cycles = 0;
g_nCounter1Cycles = 0;
}