mirror of
https://github.com/kalymos/PsNee.git
synced 2026-02-27 17:14:06 +00:00
optimize digital input reading with boolean normalization
- Updated all input read macros (PIN_SQCK_READ, PIN_SUBQ_READ, etc.) to use double logical NOT (!!) operators. - This ensures bitwise masks are normalized to a pure 0 or 1 boolean value. - Optimization: Allows the compiler to use faster AVR branch instructions (SBIC/SBIS) and improves compatibility with direct equality tests. - Updated inline documentation for better clarity on bitwise operations.
This commit is contained in:
91
PSNee/MCU.h
91
PSNee/MCU.h
@@ -49,21 +49,23 @@
|
||||
|
||||
//******************************************************************************************************************
|
||||
|
||||
// Example: (PIND & (1<<PIND6));
|
||||
// Example: (!!(PIND & (1<<PIND6)));
|
||||
|
||||
// Read the state of the input pins Equivalent.
|
||||
// Read the state of the input pins with Boolean Normalization.
|
||||
|
||||
// Create a mask by shifting the bit 1 to the left by PIND6's position (bit 6),
|
||||
// and then apply a bitwise AND operation to read the state of the PIND6 pin.
|
||||
// The result will be non-zero (true) if the PIND6 pin is high (1), and zero (false) if the PIND6 pin is low (0).
|
||||
// Create a mask by shifting the bit 1 to the left by PIND6's position (bit 6),
|
||||
// then apply a bitwise AND operation to read the state of the PIND6 pin.
|
||||
// Finally, apply a double logical NOT (!!) to normalize the result to 0 or 1.
|
||||
// The result will be 1 (true) if the PIND6 pin is high (1), and 0 (false) if the PIND6 pin is low (0).
|
||||
//
|
||||
// For instance, if PIND = b10101010 (binary value), the operation shifts the bit 1 to the left to create the mask
|
||||
// (1<<PIND6) = b01000000. The AND operation between PIND and the mask checks if the 6th bit is set to 1 (high).
|
||||
//
|
||||
// Before: PIND = b10101010 // Initial value of PIND (register containing input pin states)
|
||||
// Mask: (1<<PIND6) = b01000000 // Mask generated by shifting the bit 1 to the left to position PIND6
|
||||
// Operation: PIND & b01000000 = b00000000 // If PIND6 is low (0)
|
||||
// Operation: PIND & b01000000 = b01000000 // If PIND6 is high (1)
|
||||
// For instance, if PIND = b01000000 (binary value), the operation shifts the bit 1 to the left to create the mask
|
||||
// (1<<PIND6) = b01000000. The AND operation isolates the bit, and !! converts it to a pure boolean 1.
|
||||
//
|
||||
// Before: PIND = b01000000 // Initial value (PIN 6 is HIGH)
|
||||
// Mask: (1<<PIND6) = b01000000 // Mask for bit 6
|
||||
// Step 1: PIND & Mask = b01000000 (Value: 64)
|
||||
// Step 2: !64 = 0
|
||||
// Step 3: !0 = 1 // Final Normalized Result
|
||||
//
|
||||
//******************************************************************************************************************
|
||||
|
||||
@@ -220,36 +222,34 @@
|
||||
//
|
||||
//******************************************************************************************************************
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#ifdef ATmega328_168
|
||||
|
||||
static inline void optimizePeripherals(void) {
|
||||
// Configuring Port C (A0-A5) as Digital Inputs
|
||||
// DDRC at 0 = Input. Ensure that the first 6 bits are 0.
|
||||
DDRC &= ~0x3F;
|
||||
static inline void optimizePeripherals(void) {
|
||||
// Configuring Port C (A0-A5) as Digital Inputs
|
||||
// DDRC at 0 = Input. Ensure that the first 6 bits are 0.
|
||||
DDRC &= ~0x3F;
|
||||
|
||||
// Disable the ADC (Analog-to-Digital Converter)
|
||||
// ADEN at 0 disables the module. PRADC at 1 disables the module's clock.
|
||||
ADCSRA &= ~(1 << ADEN);
|
||||
PRR |= (1 << PRADC);
|
||||
// Disable the ADC (Analog-to-Digital Converter)
|
||||
// ADEN at 0 disables the module. PRADC at 1 disables the module's clock.
|
||||
ADCSRA &= ~(1 << ADEN);
|
||||
PRR |= (1 << PRADC) | (1 << PRTIM2);
|
||||
|
||||
// Configure DIDR0 (Digital Input Disable Register)
|
||||
// To read digitally via PINC, the bits of DIDR0 MUST be set to 0.
|
||||
// (0 = Digital Buffer enabled)
|
||||
DIDR0 &= ~0x3F;
|
||||
// Configure DIDR0 (Digital Input Disable Register)
|
||||
// To read digitally via PINC, the bits of DIDR0 MUST be set to 0.
|
||||
// (0 = Digital Buffer enabled)
|
||||
DIDR0 &= ~0x3F;
|
||||
|
||||
// Stop Timer 0 (Stops Arduino millis/micros)
|
||||
// Setting TCCR0B to 0 stops the clock source. Setting TIMSK0 to 0 disables interrupts.
|
||||
TCCR0B = 0;
|
||||
//#define F TIMSK0 = 0;
|
||||
// Stop Timer 0 (Stops Arduino millis/micros)
|
||||
// Setting TCCR0B to 0 stops the clock source. Setting TIMSK0 to 0 disables interrupts.
|
||||
TCCR0B = 0;
|
||||
//#define F TIMSK0 = 0;
|
||||
|
||||
// Disable the Analog Comparator (Frees up resources on PD6/PD7)
|
||||
// ACD at 1 = Comparator off.
|
||||
ACSR |= (1 << ACD);
|
||||
}
|
||||
// Disable the Analog Comparator (Frees up resources on PD6/PD7)
|
||||
// ACD at 1 = Comparator off.
|
||||
ACSR |= (1 << ACD);
|
||||
}
|
||||
|
||||
// Define the clock speed for the microcontroller
|
||||
#define F_CPU 16000000L
|
||||
@@ -285,9 +285,9 @@ static inline void optimizePeripherals(void) {
|
||||
#define PIN_WFCK_CLEAR PORTB &= ~(1 << PB1) // Set PORTB register to make PINB1 low
|
||||
|
||||
// Read the state of the main input pins
|
||||
#define PIN_SQCK_READ (PIND & (1 << PIND6)) // Check if the value of PIND6 is high (1)
|
||||
#define PIN_SUBQ_READ (PIND & (1 << PIND7)) // Check if the value of PIND7 is high (1)
|
||||
#define PIN_WFCK_READ (PINB & (1 << PINB1)) // Check if the value of PIND1 is high (1)
|
||||
#define PIN_SQCK_READ (!!(PIND & (1 << PIND6))) // Check if the value of PIND6 is high (1)
|
||||
#define PIN_SUBQ_READ (!!(PIND & (1 << PIND7))) // Check if the value of PIND7 is high (1)
|
||||
#define PIN_WFCK_READ (!!(PINB & (1 << PINB1))) // Check if the value of PIND1 is high (1)
|
||||
|
||||
// LED pin handling (for indication)
|
||||
#ifdef LED_RUN
|
||||
@@ -316,7 +316,7 @@ static inline void optimizePeripherals(void) {
|
||||
#define PIN_DX_CLEAR PORTD &= ~(1 << PD4) // Set PORTD register to make PIND4 low
|
||||
|
||||
// Read the input pins for the BIOS patch
|
||||
#define PIN_AX_READ (PIND & (1 << PIND2)) // Read the state of PIND2
|
||||
#define PIN_AX_READ (!!(PIND & (1 << PIND2))) // Read the state of PIND2
|
||||
|
||||
// External interrupt configuration for BIOS patch
|
||||
#define PIN_AX_INTERRUPT_ENABLE EIMSK |= (1 << INT0) // Enable external interrupt on INT0 (PINB2)
|
||||
@@ -330,7 +330,7 @@ static inline void optimizePeripherals(void) {
|
||||
// Defin PIN_AY for HIGH_PATCH
|
||||
#if defined(SCPH_3000) || defined(SCPH_1000)
|
||||
#define PIN_AY_INPUT DDRD &= ~(1 << DDD3) // Set DDRD register to configure PIND3 as input
|
||||
#define PIN_AY_READ (PIND & (1 << PIND3)) // Read the state of PIND3
|
||||
#define PIN_AY_READ (!!(PIND & (1 << PIND3))) // Read the state of PIND3
|
||||
#define PIN_AY_INTERRUPT_ENABLE EIMSK |= (1 << INT1) // Enable external interrupt on INT1 (PINB3)
|
||||
#define PIN_AY_INTERRUPT_DISABLE EIMSK &= ~(1 << INT1) // Disable external interrupt on INT1
|
||||
#define PIN_AY_INTERRUPT_RISING EICRA |= (1 << ISC11) | (1 << ISC10) // Configure INT1 for rising edge trigger
|
||||
@@ -338,17 +338,14 @@ static inline void optimizePeripherals(void) {
|
||||
#define PIN_AY_INTERRUPT_VECTOR INT1_vect // Interrupt vector for INT1 (external interrupt)
|
||||
#endif
|
||||
// Handle switch input for BIOS patch
|
||||
#if defined(SCPH_7000)
|
||||
#define PIN_SWITCH_INPUT DDRD &= ~(1 << DDD5) // Configure PIND5 as input for switch
|
||||
#define PIN_SWITCH_SET PORTD |= (1 << PD5) // Set PIND5 high (enable pull-up)
|
||||
#define PIN_SWITCH_READ (PIND & (1 << PIND5)) // Read the state of PIND5 (switch input)
|
||||
#endif
|
||||
|
||||
#if defined(SCPH_7000)
|
||||
#define PIN_SWITCH_INPUT DDRD &= ~(1 << DDD5) // Configure PIND5 as input for switch
|
||||
#define PIN_SWITCH_SET PORTD |= (1 << PD5) // Set PIND5 high (enable pull-up)
|
||||
#define PIN_SWITCH_READ (!!(PIND & (1 << PIND5))) // Read the state of PIND5 (switch input)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// #if defined(PSNEE_DEBUG_SERIAL_MONITOR)
|
||||
// #define DEBUG_PRINT(x) Serial.print(x)
|
||||
// #define DEBUG_PRINTHEX(x) Serial.print(x, HEX)
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
SCPH model number // region code | region
|
||||
-------------------------------------------------------------------------------------------------*/
|
||||
//#define SCPH_xxxx // | Universal.
|
||||
#define SCPH_xxxx // | Universal.
|
||||
//#define SCPH_xxx1 // NTSC U/C | America.
|
||||
//#define SCPH_xxx2 // PAL | Europ.
|
||||
//#define SCPH_xxx3 // NTSC J | Asia.
|
||||
|
||||
Reference in New Issue
Block a user