Trials of Mana (SNES)/Threading System

From Data Crystal
Jump to navigation Jump to search

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)


Permanent Threads

Thread ID Memory Location Thread ID Name
n/a 2 Input/Monitor Thread
7e2003 3 Audio/Clock Thread
7e2001 4 Thread 4
7e2000 5 Thread 5
7e2002 6 Thread 6
7e2007 7 Thread 7
7e2008 8 Thread 8
7e2006 9 Thread 9
7f0386 a Thread 0A
7e33f1 b Character Overlay UI Thread

Input/Monitor Thread

some stuff

Audio/Clock Thread

some stuff

Temporary/Transient Threads