Advanced Program Logic Review
ZX Spectrum • Phase 1 • Tier 1
Comprehensive review and integration of all program flow concepts. Build sophisticated control systems demonstrating mastery of Z80 assembly programming logic.
Learning Objectives
- Integrate all program flow concepts into complex systems
- Design sophisticated control architectures
- Master advanced logical programming patterns
- Build complete interactive applications
- Demonstrate professional-level programming skills
Key Concepts
Lesson 24: Advanced Program Logic Review
You’ve mastered the individual elements of program flow - conditionals, loops, subroutines, algorithms, debugging, and flags. Now you’ll integrate all these concepts into sophisticated control systems that demonstrate professional-level assembly programming skills. This lesson challenges you to build complex, efficient, and maintainable code.
Integration of Core Concepts
The Complete Programming Toolkit
You now have mastery of:
- Conditional Logic: Complex decision trees with flag-based branching
- Iteration: Efficient loops with DJNZ and conditional structures
- Modularity: Subroutines with parameter passing and error handling
- Algorithms: Sorting, searching, and optimization techniques
- Debugging: Error detection, validation, and systematic troubleshooting
- Flag Mastery: Advanced comparisons and state management
Combining Concepts Effectively
; Example: Sophisticated data processing pipeline
DataProcessor:
; 1. Input validation (defensive programming)
CALL ValidateInputData
JR C, ProcessorError
; 2. Algorithmic processing (sorting/searching)
CALL SortInputData
CALL FilterValidData
; 3. State-based control flow
LD A, (ProcessorState)
CALL DispatchProcessorState
; 4. Error handling and reporting
LD A, (LastError)
OR A
JR NZ, HandleProcessorError
; 5. Performance monitoring (optional)
CALL UpdatePerformanceMetrics
RET
ProcessorError:
HandleProcessorError:
RET
Advanced Control System Architectures
Hierarchical State Machines
; Multi-level state management system
SystemState: DB 0 ; Main system state
SubState: DB 0 ; Sub-state within main state
MicroState: DB 0 ; Micro-state within sub-state
AdvancedStateMachine:
; Hierarchical state dispatch
LD A, (SystemState)
OR A
JP Z, HandleInitializationState
DEC A
JP Z, HandleOperationalState
DEC A
JP Z, HandleShutdownState
JP HandleErrorState
HandleOperationalState:
; Sub-state dispatch within operational state
LD A, (SubState)
OR A
JP Z, HandleIdleSubState
DEC A
JP Z, HandleActiveSubState
DEC A
JP Z, HandleProcessingSubState
JP HandleOperationalError
HandleActiveSubState:
; Micro-state dispatch within active sub-state
LD A, (MicroState)
OR A
JP Z, HandleSetupMicroState
DEC A
JP Z, HandleExecuteMicroState
JP Z, HandleCleanupMicroState
RET
HandleInitializationState:
HandleIdleSubState:
HandleProcessingSubState:
HandleShutdownState:
HandleErrorState:
HandleOperationalError:
HandleSetupMicroState:
HandleExecuteMicroState:
HandleCleanupMicroState:
RET
Event-Driven Architecture with Priorities
; Priority-based event system
EventQueue: DS 32 ; Event data
EventPriority: DS 32 ; Priority for each event (0=highest)
QueueHead: DB 0
QueueTail: DB 0
EventCount: DB 0
; Add event with priority
; Input: A = event type, B = priority
AddPriorityEvent:
; Check queue space
LD A, (EventCount)
CP 32
RET NC ; Queue full
; Find insertion point based on priority
CALL FindInsertionPoint
; Insert event at correct priority position
CALL InsertEventAtPosition
RET
ProcessPriorityEvents:
; Process events in priority order
LD A, (EventCount)
OR A
RET Z ; No events
; Get highest priority event (head of queue)
CALL GetHighestPriorityEvent
CALL DispatchEvent
CALL RemoveProcessedEvent
; Continue processing
JR ProcessPriorityEvents
FindInsertionPoint:
InsertEventAtPosition:
GetHighestPriorityEvent:
DispatchEvent:
RemoveProcessedEvent:
RET
Advanced State Machine:
; Sophisticated multi-level state machine
; System states
STATE_INIT EQU 0
STATE_MENU EQU 1
STATE_GAME EQU 2
STATE_PAUSE EQU 3
STATE_GAMEOVER EQU 4
; Game sub-states
GAME_LOADING EQU 0
GAME_PLAYING EQU 1
GAME_ENDING EQU 2
; Menu sub-states
MENU_MAIN EQU 0
MENU_OPTIONS EQU 1
MENU_HIGHSCORE EQU 2
; System state
MainState: DB STATE_INIT
GameSubState: DB GAME_LOADING
MenuSubState: DB MENU_MAIN
StateTimer: DB 0
; Main state machine controller
UpdateStateMachine:
; Increment state timer for timing-based transitions
LD A, (StateTimer)
INC A
LD (StateTimer), A
; Dispatch to main state handler
LD A, (MainState)
; Use computed jump for efficiency
SLA A ; × 2 for word addresses
LD HL, StateHandlers
LD C, A : LD B, 0
ADD HL, BC
LD A, (HL)
INC HL
LD H, (HL)
LD L, A
JP (HL)
; State handler table
StateHandlers:
DW HandleInit
DW HandleMenu
DW HandleGame
DW HandlePause
DW HandleGameOver
; Initialization state
HandleInit:
; Simple initialization process
LD A, (StateTimer)
CP 30 ; Initialize for 30 ticks
JR C, InitContinue
; Initialization complete - transition to menu
LD A, STATE_MENU
LD (MainState), A
LD A, 0
LD (StateTimer), A ; Reset timer
LD A, MENU_MAIN
LD (MenuSubState), A
InitContinue:
LD B, 100 ; Init state indicator
RET
; Menu state with sub-states
HandleMenu:
; Process menu sub-states
LD A, (MenuSubState)
OR A
JP Z, HandleMainMenu
DEC A
JP Z, HandleOptionsMenu
JP HandleHighScoreMenu
HandleMainMenu:
; Simulate menu input processing
LD A, (StateTimer)
AND 31 ; Every 32 ticks, simulate input
JR NZ, MainMenuDone
; Simulate
Professional Programming Patterns
Model-View-Controller Architecture
; MVC pattern adapted for assembly
; Model: Game data and logic
; View: Screen rendering and display
; Controller: Input handling and system control
; Model - Game Data
GameModel:
PlayerX: DB 100
PlayerY: DB 50
Score: DW 1500
Lives: DB 3
Level: DB 1
; Model operations
UpdatePlayerPosition:
; Update player position based on input
; Input: A = direction (0=up, 1=down, 2=left, 3=right)
OR A
JP Z, MoveUp
DEC A
JP Z, MoveDown
DEC A
JP Z, MoveLeft
; Must be right
LD HL, PlayerX
INC (HL)
RET
MoveUp:
LD HL, PlayerY
DEC (HL)
RET
MoveDown:
LD HL, PlayerY
INC (HL)
RET
MoveLeft:
LD HL, PlayerX
DEC (HL)
RET
; View - Display system
RenderGame:
CALL RenderPlayer
CALL RenderScore
CALL RenderLives
RET
RenderPlayer:
; Render player at current position
LD A, (PlayerX)
LD B, (PlayerY)
; Convert to screen coordinates and draw
RET
RenderScore:
; Display current score
LD HL, (Score)
; Convert to displayable format and show
RET
RenderLives:
; Display remaining lives
LD A, (Lives)
; Show lives indicator
RET
; Controller - Input and system management
ProcessInput:
CALL ReadInputDevice
CALL ValidateInput
CALL UpdateModel
CALL UpdateView
RET
ReadInputDevice:
; Read keyboard/joystick
RET
ValidateInput:
; Validate input is legal
RET
UpdateModel:
; Update game model based on input
RET
UpdateView:
; Update display based on model changes
RET
Factory Pattern for Object Creation
; Factory pattern for creating different entity types
EntityFactory:
; Input: A = entity type, HL = position
OR A
JP Z, CreatePlayer
DEC A
JP Z, CreateEnemy
DEC A
JP Z, CreatePowerup
JP CreateGeneric
CreatePlayer:
; Initialize player entity
LD (EntityType), A
LD (EntityX), H
LD (EntityY), L
LD A, 100
LD (EntityHealth), A
RET
CreateEnemy:
; Initialize enemy entity
LD A, 1
LD (EntityType), A
LD (EntityX), H
LD (EntityY), L
LD A, 50
LD (EntityHealth), A
RET
CreatePowerup:
CreateGeneric:
RET
EntityType: DB 0
EntityX: DB 0
EntityY: DB 0
EntityHealth: DB 0
Observer Pattern for Event Handling
; Observer pattern for event notifications
ObserverList: DS 16 ; Up to 16 observers
ObserverCount: DB 0
RegisterObserver:
; Input: HL = observer function address
LD A, (ObserverCount)
CP 16
RET NC ; Too many observers
; Add observer to list
SLA A ; × 2 for addresses
LD BC, 0
LD C, A
LD DE, ObserverList
EX DE, HL
ADD HL, BC
EX DE, HL
LD (DE), L ; Store address
INC DE
LD (DE), H
; Increment count
LD HL, ObserverCount
INC (HL)
RET
NotifyObservers:
; Input: A = event type
LD B, A ; Save event type
LD A, (ObserverCount)
OR A
RET Z ; No observers
LD C, A ; C = observer count
LD HL, ObserverList
NotifyLoop:
; Call each observer
LD A, (HL) ; Get address low
INC HL
LD H, (HL) ; Get address high
LD L, A ; HL = observer address
PUSH BC
PUSH HL
LD A, B ; Restore event type
CALL CallObserver ; Call observer function
POP HL
POP BC
INC HL ; Next observer
DEC C
JR NZ, NotifyLoop
RET
CallObserver:
JP (HL)
Complex Algorithmic Integration
Hybrid Sorting with Intelligent Selection
; Intelligent sort that chooses algorithm based on data characteristics
IntelligentSort:
; Input: HL = array, B = size
; Analyze data characteristics first
CALL AnalyzeDataPattern
; A = 0 (random), 1 (mostly sorted), 2 (reverse sorted), 3 (many duplicates)
; Choose optimal algorithm
OR A
JP Z, UseQuickSort ; Random data - use quicksort
DEC A
JP Z, UseInsertionSort ; Mostly sorted - use insertion sort
DEC A
JP Z, UseSelectionSort ; Reverse sorted - use selection sort
; Many duplicates - use specialized algorithm
JP UseDuplicateOptimizedSort
AnalyzeDataPattern:
; Analyze data to determine best sorting approach
PUSH BC
PUSH HL
; Check for sorted pattern
CALL CheckSortedness
LD D, A ; D = sortedness score
; Check for duplicates
CALL CheckDuplicates
LD E, A ; E = duplicate score
; Combine analysis results
; Implementation details...
POP HL
POP BC
RET
CheckSortedness:
CheckDuplicates:
UseQuickSort:
UseInsertionSort:
UseSelectionSort:
UseDuplicateOptimizedSort:
RET
Multi-Criteria Search and Filtering
; Advanced search with multiple criteria and ranking
MultiCriteriaSearch:
; Input: HL = data array, B = size, DE = criteria structure
; Initialize result ranking system
CALL InitializeRanking
; Apply each search criterion
CALL ApplyCriterion1
CALL ApplyCriterion2
CALL ApplyCriterion3
; Sort results by ranking
CALL SortByRanking
; Return top results
CALL ExtractTopResults
RET
InitializeRanking:
; Set up ranking arrays
RET
ApplyCriterion1:
; Apply first search criterion and update rankings
RET
ApplyCriterion2:
; Apply second search criterion and update rankings
RET
ApplyCriterion3:
; Apply third search criterion and update rankings
RET
SortByRanking:
; Sort results by accumulated ranking scores
RET
ExtractTopResults:
; Extract highest-ranked results
RET
Complex Algorithmic System:
; Advanced data processing system with multiple algorithms
; Data structure for complex processing
DataArray: DB 64, 23, 45, 12, 78, 34, 89, 56, 21, 67
ArraySize: EQU 10
SearchResults: DS 10 ; Results storage
ResultCount: DB 0
; Intelligent data processor
ProcessDataIntelligently:
; Stage 1: Analyze data characteristics
CALL AnalyzeData
LD C, A ; Save analysis result
; Stage 2: Choose processing strategy based on analysis
OR A
JP Z, ProcessSmallData
DEC A
JP Z, ProcessLargeData
JP ProcessSpecialData
AnalyzeData:
; Analyze data complexity and size
LD A, (ArraySize)
CP 20 ; Size threshold
JR C, SmallDataSet
; Large data set - check for patterns
CALL CheckDataPatterns
OR A
JR Z, LargeDataSet
LD A, 2 ; Special pattern detected
RET
SmallDataSet:
LD A, 0 ; Small data
RET
LargeDataSet:
LD A, 1 ; Large data
RET
CheckDataPatterns:
; Check if data has special patterns (sorted, etc.)
LD HL, DataArray
LD B, ArraySize
DEC B ; Check B-1 pairs
PatternLoop:
LD A, (HL)
INC HL
CP (HL) ; Compare adjacent elements
JR NC, NotSorted ; Not in ascending order
DJNZ PatternLoop
; Found sorted pattern
LD A, 1
RET
NotSorted:
LD A, 0 ; No special pattern
RET
ProcessSmallData:
; Optimized processing for small data sets
LD HL, DataArray
LD B, ArraySize
; Use simple linear search for multiple criteria
CALL LinearSearchMultiple
LD B, 100 ; Small data processing code
RET
ProcessLargeData:
; Optimized processing for large data sets
LD HL, DataArray
LD B, ArraySize
; Sort first, then use binary search
CALL QuickSort
CALL BinarySearchMultiple
LD B, 200 ; Large data processing code
RET
ProcessSpecialData:
; Optimized processing for special patterns
LD HL, DataArray
LD B, ArraySize
; Use pattern-specific optimizations
CALL PatternOptimizedSearch
LD B, 300 ; Special data processing code
RET
; Multi-criteria search functions
LinearSearchMultiple:
; Search for elements matching multiple criteria
LD C, 0 ; Result count
LD DE, SearchResults
LinearMultiLoop:
LD A, (HL) ; Get element
; Apply multiple criteria
CALL TestCriterion1
JR NC, LinearNext ; Skip if doesn't match
CALL TestCriterion2
JR NC, LinearNext ; Skip if doesn't match
; Element matches all criteria
LD (DE), A ; Store result
INC DE
INC C ; Increment result count
LinearNext:
INC HL ; Next element
DJNZ LinearMultiLoop
LD A, C
LD (ResultCount), A
RET
BinarySearchMultiple:
; Binary search with multiple criteria (assumes sorted data)
; Simplified implementation
LD C, 0 ; Result count
LD DE, SearchResults
; Search for each criterion value
LD A, 45 ; Search for 45
CALL BinarySearchSingle
JR NC, BinaryNext1
LD (DE), A
INC DE
INC C
BinaryNext1:
LD A, 67 ; Search for 67
CALL BinarySearchSingle
JR NC, BinaryNext2
LD (DE), A
INC DE
INC C
BinaryNext2:
LD A, C
LD (ResultCount), A
RET
PatternOptimizedSearch:
; Optimized search for known patterns
LD C, 0 ; Result count
LD DE, SearchResults
; Since data is sorted, use range search
CALL FindRangeStart
CALL FindRangeEnd
CALL ExtractRange
LD A, C
LD (ResultCount), A
RET
; Criterion testing functions
TestCriterion1:
; Test if element is > 30
CP 31
RET ; Carry clear if >= 31
TestCriterion2:
; Test if element is < 80
CP 80
CCF ; Complement carry (carry set if < 80)
RET
BinarySearchSingle:
; Simplified binary search for single value
; Input: A = search value, HL = sorted array, B = size
; Output: Carry set if found
PUSH BC
PUSH HL
; Simple linear search for demonstration
LD C, A ; Save search value
BinaryLoop:
LD A, (HL)
CP C
JR Z, BinaryFound
INC HL
DJNZ BinaryLoop
; Not found
OR A ; Clear carry
JR BinaryDone
BinaryFound:
SCF ; Set carry
BinaryDone:
LD A, C ; Restore search value
POP HL
POP BC
RET
FindRangeStart:
FindRangeEnd:
ExtractRange:
RET
; Simple quicksort implementation
QuickSort:
; Simplified sort for demonstration
; Use bubble sort for simplicity
LD A, B
DEC A
RET Z ; Skip if <= 1 element
LD C, A ; Number of passes
QuickPassLoop:
PUSH BC
PUSH HL
LD B, C ; Comparisons this pass
QuickCompareLoop:
LD A, (HL)
INC HL
CP (HL)
JR C, QuickNoSwap ; Already in order
; Swap elements
LD D, (HL)
LD (HL), A
DEC HL
LD (HL), D
INC HL
QuickNoSwap:
DJNZ QuickCompareLoop
POP HL
POP BC
DEC C
JR NZ, QuickPassLoop
RET
; Test the intelligent processor
TestIntelligentProcessor:
CALL ProcessDataIntelligently
; B contains processing method used
LD C, B ; Save method
; Check results
LD A, (ResultCount)
LD D, A ; Save result count
RET
; Results: C = method used, D = result count
Performance Optimization Integration
Cache-Friendly Memory Access Patterns
; Optimize memory access patterns for better performance
OptimizedDataProcessing:
; Principle 1: Sequential access is faster than random access
LD HL, DataStart
LD B, DataSize
SequentialLoop:
; Process data sequentially
LD A, (HL)
; Process A
INC HL ; Sequential access
DJNZ SequentialLoop
; Principle 2: Batch similar operations
LD HL, DataStart
LD B, DataSize
; First pass: Read and validate all data
ValidationPass:
LD A, (HL)
CALL ValidateElement
INC HL
DJNZ ValidationPass
; Second pass: Process all validated data
LD HL, DataStart
LD B, DataSize
ProcessingPass:
LD A, (HL)
CALL ProcessElement
INC HL
DJNZ ProcessingPass
RET
ValidateElement:
ProcessElement:
RET
DataStart: DS 100
DataSize: EQU 100
Loop Optimization Techniques
; Advanced loop optimization patterns
OptimizedLoops:
; Technique 1: Loop unrolling for small, fixed loops
LD HL, Buffer
; Unrolled loop (faster for small, known iterations)
LD (HL), 0 : INC HL
LD (HL), 0 : INC HL
LD (HL), 0 : INC HL
LD (HL), 0 : INC HL
; Technique 2: Strength reduction (replace expensive operations)
LD HL, Array
LD DE, 0 ; Index counter
LD B, 10 ; Loop count
StrengthReduced:
; Instead of: LD A, index : CALL Multiply by 3 : ADD HL, A
; Use: ADD HL, 3 (much faster)
LD (HL), 1
LD A, L
ADD A, 3 ; Add constant instead of multiply
LD L, A
JR NC, NoCarry
INC H
NoCarry:
DJNZ StrengthReduced
; Technique 3: Loop invariant motion
LD HL, Array
LD A, (ConstantValue) ; Move constant load outside loop
LD B, 10
InvariantLoop:
ADD A, (HL) ; Use pre-loaded constant
LD (HL), A
INC HL
DJNZ InvariantLoop
RET
Buffer: DS 16
Array: DS 30
ConstantValue: DB 5
Algorithmic Complexity Management
; Choose algorithms based on performance characteristics
PerformanceAwareProcessing:
; Input: HL = data, B = size, A = performance requirement
; 0 = minimum memory, 1 = balanced, 2 = maximum speed
OR A
JP Z, MinimumMemoryPath
DEC A
JP Z, BalancedPath
JP MaximumSpeedPath
MinimumMemoryPath:
; Use in-place algorithms to minimize memory usage
CALL InPlaceBubbleSort ; O(1) space, O(n²) time
CALL LinearSearch ; O(1) space, O(n) time
RET
BalancedPath:
; Use algorithms with good time/space tradeoffs
CALL QuickSort ; O(log n) space, O(n log n) average time
CALL BinarySearch ; O(1) space, O(log n) time
RET
MaximumSpeedPath:
; Use fastest algorithms regardless of memory usage
CALL RadixSort ; O(n) space, O(n) time for certain data
CALL HashTableLookup ; O(n) space, O(1) average lookup
RET
InPlaceBubbleSort:
LinearSearch:
QuickSort:
BinarySearch:
RadixSort:
HashTableLookup:
RET
Professional Quality Assurance
Comprehensive Testing Framework
; Professional testing system
TestFramework:
; Initialize test environment
CALL InitializeTests
; Run unit tests
CALL RunUnitTests
; Run integration tests
CALL RunIntegrationTests
; Run performance tests
CALL RunPerformanceTests
; Generate test report
CALL GenerateTestReport
RET
InitializeTests:
; Set up test environment
LD A, 0
LD (TestsRun), A
LD (TestsPassed), A
LD (TestsFailed), A
RET
RunUnitTests:
; Test individual functions
CALL TestSortingFunctions
CALL TestSearchFunctions
CALL TestValidationFunctions
RET
RunIntegrationTests:
; Test component interactions
CALL TestDataPipeline
CALL TestStateMachineTransitions
CALL TestEventHandling
RET
RunPerformanceTests:
; Test performance characteristics
CALL TestSortingPerformance
CALL TestSearchPerformance
CALL TestMemoryUsage
RET
GenerateTestReport:
; Compile test results
LD A, (TestsPassed)
LD B, (TestsFailed)
ADD A, B
LD (TestsRun), A
; Generate summary
RET
TestsRun: DB 0
TestsPassed: DB 0
TestsFailed: DB 0
TestSortingFunctions:
TestSearchFunctions:
TestValidationFunctions:
TestDataPipeline:
TestStateMachineTransitions:
TestEventHandling:
TestSortingPerformance:
TestSearchPerformance:
TestMemoryUsage:
RET
Code Quality Metrics
; Code quality assessment
QualityMetrics:
; Measure code complexity
CALL MeasureCyclomaticComplexity
; Check coding standards compliance
CALL CheckCodingStandards
; Analyze performance characteristics
CALL AnalyzePerformance
; Generate quality score
CALL CalculateQualityScore
RET
MeasureCyclomaticComplexity:
; Count decision points in code
RET
CheckCodingStandards:
; Verify adherence to coding standards
RET
AnalyzePerformance:
; Analyze execution time and memory usage
RET
CalculateQualityScore:
; Combine metrics into overall quality score
RET
Master-Level Practice Exercise
Create a complete, professional-grade system that demonstrates mastery of all concepts:
Master Practice Exercise - Complete Game Engine:
; Complete mini game engine demonstrating all advanced concepts
; Engine states
ENGINE_INIT EQU 0
ENGINE_MENU EQU 1
ENGINE_GAME EQU 2
ENGINE_PAUSE EQU 3
ENGINE_SHUTDOWN EQU 4
; Entity types
ENTITY_PLAYER EQU 0
ENTITY_ENEMY EQU 1
ENTITY_BULLET EQU 2
ENTITY_POWERUP EQU 3
; Engine state
EngineState: DB ENGINE_INIT
FrameCounter: DW 0
GameScore: DW 0
; Entity system (simplified)
EntityCount: DB 0
MaxEntities: EQU 8
EntityArray: DS 32 ; 4 bytes per entity: type, x, y, status
; Performance tracking
FrameTime: DB 0
TargetFPS: DB 50
; Main engine loop
RunGameEngine:
; Performance monitoring start
CALL StartFrameTimer
; Update engine state
CALL UpdateEngineState
; Process entities
CALL UpdateAllEntities
; Handle collisions
CALL ProcessCollisions
; Render frame
CALL RenderFrame
; Performance monitoring end
CALL EndFrameTimer
; Check frame rate
CALL CheckFrameRate
; Update frame counter
LD HL, (FrameCounter)
INC HL
LD (FrameCounter), HL
; Continue engine loop
LD A, (EngineState)
CP ENGINE_SHUTDOWN
JR NZ, RunGameEngine
RET
; Advanced state management
UpdateEngineState:
LD A, (EngineState)
; Use computed jump for efficiency
SLA A
LD HL, EngineStateTable
LD C, A : LD B, 0
ADD HL, BC
LD A, (HL)
INC HL
LD H, (HL)
LD L, A
JP (HL)
EngineStateTable:
DW HandleEngineInit
DW HandleEngineMenu
DW HandleEngineGame
DW HandleEnginePause
DW HandleEngineShutdown
HandleEngineInit:
; Initialize engine systems
CALL InitializeEntitySystem
CALL InitializeGraphics
CALL InitializeAudio
; Transition to menu
LD A, ENGINE_MENU
LD (EngineState), A
RET
HandleEngineMenu:
; Menu logic
CALL ProcessMenuInput
CALL UpdateMenuAnimations
; Check for game start
CALL CheckStartGame
JR NC, MenuContinue
; Start game
LD A, ENGINE_GAME
LD (EngineState), A
CALL InitializeGameSession
MenuContinue:
RET
HandleEngineGame:
; Main game logic
CALL ProcessGameInput
CALL UpdateGameLogic
CALL UpdateAI
; Check for pause
CALL CheckPauseGame
JR NC, GameContinue
LD A, ENGINE_PAUSE
LD (EngineState), A
GameContinue:
RET
HandleEnginePause:
; Pause logic
CALL ProcessPauseInput
; Check for resume
CALL CheckResumeGame
JR NC, PauseContinue
LD A, ENGINE_GAME
LD (EngineState), A
PauseContinue:
RET
HandleEngineShutdown:
; Cleanup and shutdown
CALL CleanupEntitySystem
CALL CleanupGraphics
CALL CleanupAudio
RET
; Entity management system
UpdateAllEntities:
LD A, (EntityCount)
OR A
RET Z ; No entities
LD B, A ; Entity count
LD HL, EntityArray ; Entity data
EntityUpdateLoop:
; Get entity type
LD A, (HL) ; Entity type
CP ENTITY_PLAYER
JP Z, UpdatePlayer
CP ENTITY_ENEMY
JP Z, UpdateEnemy
CP ENTITY_BULLET
JP Z, UpdateBullet
CP ENTITY_POWERUP
JP Z, UpdatePowerup
; Unknown entity type - skip
JR NextEntity
UpdatePlayer:
CALL UpdatePlayerEntity
JR NextEntity
UpdateEnemy:
CALL UpdateEnemyEntity
JR NextEntity
UpdateBullet:
CALL UpdateBulletEntity
JR NextEntity
UpdatePowerup:
CALL UpdatePowerupEntity
NextEntity:
; Move to next entity (4 bytes per entity)
INC HL : INC HL : INC HL : INC HL
DJNZ EntityUpdateLoop
RET
; Advanced collision detection
ProcessCollisions:
LD A, (EntityCount)
CP 2
RET C ; Need at least 2 entities
; Nested loop for collision checking
LD B, A ; Outer loop counter
LD HL, EntityArray ; First entity
OuterCollisionLoop:
PUSH BC
PUSH HL
; Inner loop starting from next entity
LD DE, HL
INC DE : INC DE : INC DE : INC DE ; Next entity
LD A, B
DEC A ; Inner loop counter
JR Z, EndInnerLoop ; Skip if no more entities
LD C, A
InnerCollisionLoop:
; Check collision between (HL) and (DE)
CALL CheckEntityCollision
JR NC, NoCollision
; Handle collision
CALL HandleEntityCollision
NoCollision:
; Move to next inner entity
INC DE : INC DE : INC DE : INC DE
DEC C
JR NZ, InnerCollisionLoop
EndInnerLoop:
POP HL
POP BC
; Move to next outer entity
INC HL : INC HL : INC HL : INC HL
DJNZ OuterCollisionLoop
RET
; Performance monitoring
StartFrameTimer:
; Start timing current frame
LD A, 0
LD (FrameTime), A
RET
EndFrameTimer:
; End timing current frame
LD A, (FrameTime)
INC A
LD (FrameTime), A
RET
CheckFrameRate:
; Check if frame rate is acceptable
LD A, (FrameTime)
LD B, A
LD A, (TargetFPS)
CP B
JR NC, FrameRateOK
; Frame rate too slow - optimize
CALL OptimizePerformance
FrameRateOK:
RET
OptimizePerformance:
; Dynamic performance optimization
; Reduce entity count if performance is poor
LD A, (EntityCount)
CP 2
RET C ; Don't reduce below 2
DEC A
LD (EntityCount), A
RET
; Simplified implementations for demonstration
InitializeEntitySystem:
LD A, 0
LD (EntityCount), A
RET
InitializeGraphics:
InitializeAudio:
ProcessMenuInput:
UpdateMenuAnimations:
CheckStartGame:
SCF ; Simulate game start after some time
RET
InitializeGameSession:
ProcessGameInput:
UpdateGameLogic:
UpdateAI:
CheckPauseGame:
OR A ; No pause for demo
RET
ProcessPauseInput:
CheckResumeGame:
SCF ; Simulate resume
RET
UpdatePlayerEntity:
UpdateEnemyEntity:
UpdateBulletEntity:
UpdatePowerupEntity:
RET
CheckEntityCollision:
OR A ; No collision for demo
RET
HandleEntityCollision:
RenderFrame:
CleanupEntitySystem:
CleanupGraphics:
CleanupAudio:
RET
; Test the complete engine
TestGameEngine:
; Initialize and run for several frames
LD B, 10 ; Run 10 frames
EngineTestLoop:
CALL RunGameEngine
DEC B
JR NZ, EngineTestLoop
; Engine should have run through initialization and started game
RET
Integration Mastery Checklist
Core Skills Integration
✓ Conditional Logic: Complex decision trees with multiple flag tests
✓ Loop Mastery: Efficient iteration with DJNZ and conditional structures
✓ Subroutine Design: Modular code with proper parameter passing
✓ Algorithm Implementation: Optimized sorting, searching, and processing
✓ Error Handling: Comprehensive validation and defensive programming
✓ Flag Utilization: Advanced comparison and state management techniques
Advanced Patterns
✓ State Machines: Hierarchical state management systems
✓ Event Systems: Priority-based event processing
✓ MVC Architecture: Separation of concerns in complex systems
✓ Performance Optimization: Cache-friendly and optimized code patterns
✓ Quality Assurance: Testing frameworks and code quality metrics
What You’ve Mastered
In this comprehensive review, you’ve demonstrated:
- Integration of all program flow concepts into sophisticated control systems
- Professional-grade programming patterns and architectural designs
- Advanced algorithmic thinking with performance optimization
- Complex state management and event-driven programming
- Quality assurance practices and systematic testing approaches
- Master-level assembly programming skills ready for real-world applications
Looking Ahead to Spectrum Saga
You’re now ready to tackle the Spectrum Saga Foundation section (lessons 25-32), where you’ll apply all these advanced programming skills to build a complete graphics application. You’ll use:
- State machines for managing drawing modes and tool selection
- Event handling for user input and interface interaction
- Advanced algorithms for graphics operations and image processing
- Performance optimization for smooth real-time drawing
- Error handling for robust user experience
- Flag mastery for efficient pixel manipulation and collision detection
Your journey through Z80 assembly programming logic has given you the tools to build sophisticated, professional-quality software that rivals anything created in high-level languages!
Fun Fact
The programming patterns and architectural concepts you’ve mastered in this lesson are the same ones used in modern software development, from mobile apps to operating systems! The state machines, event systems, and MVC patterns you’ve implemented in Z80 assembly directly inspired the frameworks used in today’s programming languages. Many legendary games and applications from the 8-bit era used exactly these techniques - complex state machines for game flow, event-driven input handling, and sophisticated algorithms optimized for the hardware constraints. By mastering these concepts at the assembly level, you understand not just how to use these patterns, but why they work and how they can be optimized. This deep understanding makes you a better programmer in any language and gives you insight into how all software really works under the hood!