Seiken Densetsu 3:Threading System
Most of the game's code is executed inside its threading system. A thread is basically an execution context. Each thread has its own direct page memory and stack memory (between 7e:0000 and 7e:1bff). Threads can send messages to each other, and when a thread wants to receive a message, the system takes that opportunity to switch threads. Since return addresses are part of the stack, the system switches to the proper stack pointer and returns to where that thread left off.
The thread system code is in bank C0, between 495b and roughly 62ed. When looking at game ASM you'll often see variations on the following pattern:
C1/2A44: lda #$80 ; Message Type C1/2A46: jsr $C00520 ; Create new message of Type A C1/2A4A: lda #$8E C1/2A4C: sta $0004,X ; First param. Often an opcode. C1/2A4F: pla C1/2A50: sta $0005,X ; Additional param C1/2A53: lda $22 C1/2A55: sta $0006,X ; Additional param C1/2A58: lda $7E2008 ; A specific Thread ID C1/2A5C: jsr $C00528 ; Send message X to Thread A
Thread IDs for permanent threads are stored at 7e:2000-7e:2008 (one byte each). Some threads live forever during gameplay, and other threads are temporary and complete an operation and are then recycled.
Looking at this code, it's triggering something, but there's no way to determine what it's actually doing without just observing the game behavior or finding the code that the other thread executes and seeing how it responds to a message with a code of #8e. The documentation for each thread will make that easier.
(todo: explain creating threads and thread params)
- Input/Monitor Thread
- Audio/Game Clock Thread
- Thread 4
- Thread 5
- Thread 6
- Thread 7
- Thread 8
- Thread 9
- Thread 0A
- Character Overlay UI Thread