12.5 The AlphaCPU Run Loop

<< Click to Display Table of Contents >>

Navigation:  ASA-EMulatR Reference Guide > Introduction > Architecture Overview > Chapter 12 – AlphaCPU Core >

12.5 The AlphaCPU Run Loop

12.5.1 executeLoop()

 

The run loop (executeLoop() in AlphaCPU.cpp) is the clocked execution engine. Each iteration represents one hardware cycle. The loop structure:

 

void AlphaCPU::executeLoop() {

 m_running.store(true);

 m_alphaPipeline->injectOtherBoxes(m_eBox, m_fBox, m_mBox, m_pBox, m_cBox);

 

 while (!m_stopRequested.load()) {

 if (m_paused.load()) { QThread::msleep(1); continue; }

 if (m_halted.load()) { QThread::msleep(10);

 if (checkForWakeup()) m_halted.store(false);

 continue; }

 

 runOneInstruction(); // HOT PATH

 ++m_localInstrCount;

 }

 shutdownGracefully();

 m_running.store(false);

}

 

Thread control uses three atomic flags: m_stopRequested (external shutdown), m_paused (pause/resume), m_halted (HALT instruction, wakes on interrupt). The halted state sleeps for 10ms between wakeup checks; the paused state sleeps for 1ms.

 


 

12.5.2 runOneInstruction()

 

The hot-path method that executes one complete instruction cycle:

 

void runOneInstruction() {

 // 1. Check for external events (interrupts)

 if (m_pending->hasDeliverable(currentIPL)) {

 handleInterrupt();

 return;

 }

 

 // 2. Fetch and decode

 FetchResult fetchResult = m_iBox->fetchNext();

 

 // 3. Supply to pipeline

 BoxResult boxResult = m_alphaPipeline->tick(fetchResult);

 

 // 4. Handle BoxResult flags

 if (boxResult.hasFault() && boxResult.faultWasDispatched()) {

 m_alphaPipeline->flush("flush::Box-faultWasDispatched");

 m_pBox->enterPal(getFaultReason(...), vector, faultPC);

 if (boxResult.needsWriteDrain()) m_cBox->drainWriteBuffers();

 if (boxResult.needsMemoryBarrier()) m_cBox->issueMemoryBarrier(PAL);

 if (boxResult.needsHalted()) haltCPU();

 }

}

 

The four-step sequence: (1) check interrupts, (2) IBox fetch, (3) pipeline tick (advances all stages, returns BoxResult from EX), (4) handle result flags (faults → enterPal, drains, halts). Faults are only handled if faultWasDispatched() is true — meaning the faulting instruction reached WB stage retirement, preserving precise exception semantics.

 

12.5.3 Run Loop Invariants

 

The run loop enforces: one cycle per iteration, deterministic ordering (interrupt check → fetch → tick → handle), no mid-instruction interruption (interrupt check is before fetch, not during execution), no speculative privilege leakage, and precise exception semantics (faults dispatched only from WB). The run loop is the only place where execution advances.

 

See Also: cpuCoreLib/AlphaCPU.cpp (executeLoop); cpuCoreLib/AlphaCPU.h (runOneInstruction).