289 lines
4.9 KiB
Markdown
289 lines
4.9 KiB
Markdown
< [Back](chapter-8.md) | [Summary](table-of-contents.md) | [Next](chapter-10.md) >
|
|
|
|
# ⚡ **Events and Interrupts**
|
|
|
|
## 1. Overview
|
|
|
|
PROMETEU clearly distinguishes between **normal execution**, **events**, and **interrupts**.
|
|
|
|
Nothing occurs "out of time".
|
|
|
|
Nothing interrupts the system without cost.
|
|
|
|
Nothing happens without a well-defined point in the execution cycle.
|
|
|
|
> Events are signals.
|
|
> Interrupts are machine decisions.
|
|
>
|
|
|
|
This chapter defines:
|
|
|
|
- what PROMETEU considers an event
|
|
- how interrupts are modeled
|
|
- when they can occur
|
|
- how they relate to cycles, CAP, and determinism
|
|
|
|
---
|
|
|
|
## 2. Event Philosophy in PROMETEU
|
|
|
|
PROMETEU **does not use invisible asynchronous callbacks**.
|
|
|
|
Every event:
|
|
|
|
- is registered
|
|
- is delivered at a predictable moment
|
|
- is handled within the main loop
|
|
|
|
This model avoids:
|
|
|
|
- implicit concurrency
|
|
- hidden race conditions
|
|
- non-deterministic side effects
|
|
|
|
PROMETEU favors:
|
|
|
|
> explicit control over reactivity.
|
|
>
|
|
|
|
---
|
|
|
|
## 3. Events
|
|
|
|
### 3.1 Definition
|
|
|
|
An **event** in PROMETEU is a **logical signal** generated by the system or the program, indicating that something has occurred.
|
|
|
|
Examples of events:
|
|
|
|
- end of frame
|
|
- timer expired
|
|
- system state change
|
|
- execution error
|
|
|
|
Events **do not execute code automatically**.
|
|
|
|
They only **inform**.
|
|
|
|
---
|
|
|
|
### 3.2 Event Queue
|
|
|
|
PROMETEU maintains an **event queue**:
|
|
|
|
- events are queued
|
|
- the queue is processed in order
|
|
- processing occurs at defined points in the frame
|
|
|
|
Events:
|
|
|
|
- do not interrupt execution arbitrarily
|
|
- do not execute outside the loop
|
|
|
|
---
|
|
|
|
## 4. Interrupts
|
|
|
|
### 4.1 Definition
|
|
|
|
An **interrupt** is a special event, treated by the system as **priority**, which can:
|
|
|
|
- change the normal flow of execution
|
|
- execute specific system code
|
|
- impact cycles and budget
|
|
|
|
Interrupts are **rare and explicit**.
|
|
|
|
---
|
|
|
|
### 4.2 What is NOT an interrupt
|
|
|
|
In PROMETEU, the following are **not interrupts**:
|
|
|
|
- button input
|
|
- collisions
|
|
- common timers
|
|
- game logic
|
|
|
|
These are treated as **state or normal events**.
|
|
|
|
---
|
|
|
|
## 5. Types of Interrupts in PROMETEU
|
|
|
|
PROMETEU defines a small and well-controlled set of interrupts.
|
|
|
|
### 5.1 Frame Interrupt (Conceptual VBlank)
|
|
|
|
The end of each frame generates a **logical synchronization interrupt**, responsible for:
|
|
|
|
- framebuffer swap
|
|
- audio commit
|
|
- state synchronization
|
|
|
|
This interrupt:
|
|
|
|
- occurs at SYNC
|
|
- has a fixed cost
|
|
- does not execute user code
|
|
|
|
---
|
|
|
|
### 5.2 System Interrupt
|
|
|
|
Generated by exceptional conditions:
|
|
|
|
- fatal VM error
|
|
- memory violation
|
|
- invalid instruction
|
|
|
|
Result:
|
|
|
|
- execution is halted
|
|
- VM state is preserved
|
|
- detailed report is generated
|
|
|
|
---
|
|
|
|
### 5.3 Timed Interrupts (Timers)
|
|
|
|
PROMETEU can offer **system timers**, modeled as:
|
|
|
|
- counters based on frames
|
|
- signals generated upon reaching zero
|
|
|
|
Timers:
|
|
|
|
- do not trigger code automatically
|
|
- generate queryable events
|
|
|
|
Conceptual example:
|
|
|
|
```
|
|
if (timer.expired(T1)) {
|
|
// handle event
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 6. Relationship between Events, Interrupts, and the Loop
|
|
|
|
The complete flow can be represented as follows:
|
|
|
|
```
|
|
FRAME N
|
|
──────────────
|
|
SAMPLEINPUT
|
|
PROCESS EVENTS
|
|
UPDATE
|
|
DRAW
|
|
AUDIO
|
|
INTERRUPT: VBLANK
|
|
SYNC
|
|
──────────────
|
|
```
|
|
|
|
Important:
|
|
|
|
- events are processed before main logic
|
|
- interrupts occur only at safe points
|
|
- no interrupt occurs "in the middle" of an instruction
|
|
|
|
---
|
|
|
|
## 7. Costs and Budget
|
|
|
|
Events and interrupts:
|
|
|
|
- consume cycles
|
|
- participate in the CAP
|
|
- appear in certification
|
|
|
|
Example report:
|
|
|
|
```
|
|
Frame 18231:
|
|
Event processing:120cycles
|
|
VBlank interrupt:80cycles
|
|
```
|
|
|
|
Nothing is free.
|
|
|
|
---
|
|
|
|
## 8. Determinism and Reproducibility
|
|
|
|
PROMETEU guarantees:
|
|
|
|
- same sequence of events → same behavior
|
|
- interrupts always at the same point in the frame
|
|
- timers based on frame count, not real time
|
|
|
|
This allows:
|
|
|
|
- reliable replays
|
|
- precise debugging
|
|
- fair certification
|
|
|
|
---
|
|
|
|
## 9. Best Practices
|
|
|
|
PROMETEU encourages:
|
|
|
|
- treating events as data
|
|
- querying events explicitly
|
|
- avoiding heavy logic in handlers
|
|
- using timers instead of excessive polling
|
|
|
|
PROMETEU discourages:
|
|
|
|
- simulating asynchronous callbacks
|
|
- depending on implicit order
|
|
- using events as a "shortcut" for complex logic
|
|
|
|
---
|
|
|
|
## 10. Relationship with Microcontrollers
|
|
|
|
The model reflects real MCUs:
|
|
|
|
| MCU | PROMETEU |
|
|
| --- | --- |
|
|
| ISR | Explicit interrupt |
|
|
| Main Loop | PROMETEU Loop |
|
|
| Flags | Events |
|
|
| Timers | Per-frame timers |
|
|
|
|
But without:
|
|
|
|
- real concurrency
|
|
- unpredictable interrupts
|
|
|
|
PROMETEU **teaches the concept**, not the chaos.
|
|
|
|
---
|
|
|
|
## 11. Pedagogical Implications
|
|
|
|
This model allows teaching:
|
|
|
|
- the difference between event and interrupt
|
|
- safe synchronization
|
|
- flow control in reactive systems
|
|
- the impact of temporal decisions
|
|
|
|
Everything with **order, clarity, and measurement**.
|
|
|
|
---
|
|
|
|
## 12. Summary
|
|
|
|
- events inform, they do not execute
|
|
- interrupts are rare and controlled
|
|
- no execution occurs outside the loop
|
|
- costs are explicit
|
|
- behavior is deterministic
|
|
|
|
< [Back](chapter-8.md) | [Summary](table-of-contents.md) | [Next](chapter-10.md) > |