Skip to content

Commit

Permalink
PWM: fix non PWM mode for PWM only drivers
Browse files Browse the repository at this point in the history
Fix #404
  • Loading branch information
vbousquet committed Jan 12, 2025
1 parent 65871a3 commit b2e355c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 41 deletions.
9 changes: 3 additions & 6 deletions src/win32com/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,7 @@ STDMETHODIMP CController::get_Lamp(int nLamp, VARIANT_BOOL *pVal)
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
*pVal= false;
else {
if (options.usemodsol & (CORE_MODOUT_FORCE_ON | CORE_MODOUT_ENABLE_PHYSOUT_LAMPS))
core_update_pwm_outputs(CORE_MODOUT_LAMP0 + nLamp - 1, 1);
core_update_pwm_lamps();
*pVal = vp_getLamp(nLamp)?VARIANT_TRUE:VARIANT_FALSE;
}

Expand All @@ -420,8 +419,7 @@ STDMETHODIMP CController::get_Solenoid(int nSolenoid, VARIANT_BOOL *pVal)
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
*pVal= false;
else {
if (options.usemodsol & (CORE_MODOUT_FORCE_ON | CORE_MODOUT_ENABLE_PHYSOUT_SOLENOIDS | CORE_MODOUT_ENABLE_MODSOL))
core_update_pwm_outputs(CORE_MODOUT_SOL0 + nSolenoid - 1, 1);
core_update_pwm_solenoids();
*pVal = vp_getSolenoid(nSolenoid)?VARIANT_TRUE:VARIANT_FALSE;
}
return S_OK;
Expand Down Expand Up @@ -1459,8 +1457,7 @@ STDMETHODIMP CController::get_GIString(int nString, int *pVal) {
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
*pVal = 0;
else {
if (options.usemodsol & (CORE_MODOUT_FORCE_ON | CORE_MODOUT_ENABLE_PHYSOUT_GI))
core_update_pwm_outputs(CORE_MODOUT_GI0 + nString - 1, 1);
core_update_pwm_gis();
*pVal = vp_getGI(nString);
}

Expand Down
18 changes: 0 additions & 18 deletions src/wpc/capcom.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,24 +141,6 @@ static NVRAM_HANDLER(cc);
static void Skip_Error_Msg(void);

static INTERRUPT_GEN(cc_vblank) {
/*-------------------------------
/ copy local data to interface
/--------------------------------*/
// TODO Why do we delay lamps/solenoids until VBLANK ? this creates artificial latency, direct set/callback would be better at least for solenoids

/*-- lamps --*/
memset(coreGlobals.lampMatrix, 0, sizeof(coreGlobals.lampMatrix));
for (int i = 0; i < 8 + core_gameData->hw.lampCol; i++)
for (int j = 0; j < 8; j++)
if (coreGlobals.physicOutputState[CORE_MODOUT_LAMP0 + i * 8 + j].value >= 0.1f)
coreGlobals.lampMatrix[i] |= 1 << j;

/*-- solenoids --*/
coreGlobals.solenoids = 0;
for (int i = 0; i < 32; i++)
if (coreGlobals.physicOutputState[CORE_MODOUT_SOL0 + i].value >= 0.5f)
coreGlobals.solenoids |= 1 << i;

/*-- update leds (they are PWM faded, so uses physic output) --*/
coreGlobals.diagnosticLed = (coreGlobals.physicOutputState[CORE_MODOUT_LAMP0 + 8 * 8 + (core_gameData->hw.lampCol - 1) * 8 ].value >= 0.5f ? 1 : 0)
| (coreGlobals.physicOutputState[CORE_MODOUT_LAMP0 + 8 * 8 + (core_gameData->hw.lampCol - 1) * 8 + 1].value >= 0.5f ? 2 : 0);
Expand Down
23 changes: 23 additions & 0 deletions src/wpc/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2817,6 +2817,29 @@ void core_update_pwm_outputs(const int startIndex, const int count)
// Perform integration of stable state up to now
output->integrator(now, index, FALSE, output->lastIntegrationFlipPos & 1);
}
// Also update non PWM data structure if needed
if (options.usemodsol & CORE_MODOUT_FORCE_ON)
{
if (((options.usemodsol & CORE_MODOUT_ENABLE_PHYSOUT_LAMPS) == 0) && (startIndex < CORE_MODOUT_LAMP0 + CORE_MODOUT_LAMP_MAX) && (startIndex + count >= CORE_MODOUT_LAMP0))
{
for (int i = 0; i < 8 + core_gameData->hw.lampCol; i++)
{
UINT8 col = 0;
for (int j = 0; j < 8; j++)
if (coreGlobals.physicOutputState[CORE_MODOUT_LAMP0 + i * 8 + j].value >= 0.1f)
col |= 1 << j;
coreGlobals.lampMatrix[i] = col;
}
}
if (((options.usemodsol & (CORE_MODOUT_ENABLE_MODSOL | CORE_MODOUT_ENABLE_PHYSOUT_SOLENOIDS)) == 0) && (startIndex < CORE_MODOUT_SOL0 + CORE_MODOUT_SOL_MAX) && (startIndex + count >= CORE_MODOUT_SOL0))
{
UINT32 sols = 0;
for (int i = 0; i < 32; i++)
if (coreGlobals.physicOutputState[CORE_MODOUT_SOL0 + i].value >= 0.5f)
sols |= 1 << i;
coreGlobals.solenoids = sols;
}
}
}

// Write binary state of outputs, taking care of PWM integration based on physical model of the connected device
Expand Down
17 changes: 0 additions & 17 deletions src/wpc/sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1453,10 +1453,6 @@ static MACHINE_INIT(sam) {
for (int i = 0; i < coreGlobals.nSolenoids; i++)
if (coreGlobals.physicOutputState[i].type != CORE_MODOUT_SOL_2_STATE)
core_set_pwm_output_type(CORE_MODOUT_SOL0, 1, CORE_MODOUT_LEGACY_SOL_2_STATE);
if ((options.usemodsol & CORE_MODOUT_ENABLE_PHYSOUT_LAMPS) == 0)
core_set_pwm_output_type(CORE_MODOUT_LAMP0, 80 /*coreGlobals.nLamps*/, CORE_MODOUT_LEGACY_SOL_2_STATE);
if ((options.usemodsol & CORE_MODOUT_ENABLE_PHYSOUT_GI) == 0)
core_set_pwm_output_type(CORE_MODOUT_GI0, coreGlobals.nGI, CORE_MODOUT_LEGACY_SOL_2_STATE);
}

void sam_init(void)
Expand Down Expand Up @@ -2104,25 +2100,12 @@ static INTERRUPT_GEN(sam_interface_update) {
/--------------------------------*/
samlocals.vblankCount++;

/*-- lamps --*/
memset(coreGlobals.lampMatrix, 0, sizeof(coreGlobals.lampMatrix));
for (int i = 0; i < 8 + core_gameData->hw.lampCol; i++)
for (int j = 0; j < 8; j++)
if (coreGlobals.physicOutputState[CORE_MODOUT_LAMP0 + i * 8 + j].value >= 0.25f)
coreGlobals.lampMatrix[i] |= 1 << j;

/*-- display --*/
if ((samlocals.vblankCount % SAM_DISPLAYSMOOTH) == 0) {
coreGlobals.diagnosticLed = samlocals.diagnosticLed;
samlocals.diagnosticLed = 0;
}

/*-- solenoids --*/
coreGlobals.solenoids = 0;
for (int i = 0; i < 32; i++)
if (coreGlobals.physicOutputState[CORE_MODOUT_SOL0 + i].value >= 0.5f)
coreGlobals.solenoids |= 1 << i;

/*-- GameOn for Fast Flips --*/
if (samlocals.fastflipaddr > 0 && cpu_readmem32ledw(samlocals.fastflipaddr) == 0x01)
{
Expand Down

2 comments on commit b2e355c

@PowoFR
Copy link

@PowoFR PowoFR commented on b2e355c Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix but I'm not yet familiar with github. To get the fix for the random lamps should I wait for a new release ?
Am I supposed to download the source and copy paste your code modification in it ? I think it's a bit much for me, I have no idea how it's compiled afterwards.
My programming skills are limited to PLCs

@vbousquet
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we commit a fix, a 'nightly build' is automatically created with it. The latest is here: https://github.com/vpinball/pinmame/actions/runs/12734392769

Please sign in to comment.