Trials of Mana (SNES)/Threading System

From Data Crystal
< Trials of Mana (SNES)
Revision as of 22:27, 17 October 2019 by Chronogeran (talk | contribs) (Created page with "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 (betwe...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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

Temporary/Transient Threads