mirror of
https://github.com/n64decomp/mk64
synced 2026-06-15 13:51:28 -04:00
147 lines
9.5 KiB
HTML
147 lines
9.5 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
|
<meta name="generator" content="Doxygen 1.9.1"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Mario Kart 64: Control Flow</title>
|
|
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="jquery.js"></script>
|
|
<script type="text/javascript" src="dynsections.js"></script>
|
|
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="resize.js"></script>
|
|
<script type="text/javascript" src="navtreedata.js"></script>
|
|
<script type="text/javascript" src="navtree.js"></script>
|
|
<link href="search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="search/searchdata.js"></script>
|
|
<script type="text/javascript" src="search/search.js"></script>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
|
<link href="doxygen-awesome.css" rel="stylesheet" type="text/css"/>
|
|
<link href="doxygen-awesome-sidebar-only.css" rel="stylesheet" type="text/css"/>
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr style="height: 56px;">
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<div id="projectname">Mario Kart 64
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.9.1 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="menudata.js"></script>
|
|
<script type="text/javascript" src="menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(function() {
|
|
initMenu('',true,false,'search.php','Search');
|
|
$(document).ready(function() { init_search(); });
|
|
});
|
|
/* @license-end */</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(document).ready(function(){initNavTree('controlflow.html',''); initResizable(); });
|
|
/* @license-end */
|
|
</script>
|
|
<div id="doc-content">
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<iframe src="javascript:void(0)" frameborder="0"
|
|
name="MSearchResults" id="MSearchResults">
|
|
</iframe>
|
|
</div>
|
|
|
|
<div class="PageDoc"><div class="header">
|
|
<div class="headertitle">
|
|
<div class="title">Control Flow </div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="textblock"><h1><a class="anchor" id="autotoc_md24"></a>
|
|
Introduction to State</h1>
|
|
<p>The developers wrote mk64 using a state-driven paradigm. This manifests itself in a variety of ways but as a quick example lets discuss the following variables. <code>gGamestate</code>, <code>gGameMode</code>, and <code>gCurrentCourseId</code>. Code for menus, racing, and credits must be modularized in some fashion. The gamestate variable contains the current state and during runtime branches into the relevant code while skipping code not pertaining to the current state. The racing code does not run while the user operates the menu. Let's inspect a simple example of a state-driven mechanism: </p><div class="fragment"><div class="line"><span class="keywordflow">switch</span>(<a class="code" href="code__800029B0_8c.html#a0b35aa576b8ad5250674993b72300f0d">gCurrentCourseId</a>) {</div>
|
|
<div class="line"> <span class="keywordflow">case</span> YOSHI_VALLEY:</div>
|
|
<div class="line"> load_and_render_yoshi_egg();</div>
|
|
<div class="line"> <span class="keywordflow">break</span>;</div>
|
|
<div class="line"> <span class="keywordflow">case</span> MARIO_RACEWAY:</div>
|
|
<div class="line"> load_and_render_pipe();</div>
|
|
<div class="line"> <span class="keywordflow">break</span>;</div>
|
|
<div class="line">}</div>
|
|
<div class="ttc" id="acode__800029B0_8c_html_a0b35aa576b8ad5250674993b72300f0d"><div class="ttname"><a href="code__800029B0_8c.html#a0b35aa576b8ad5250674993b72300f0d">gCurrentCourseId</a></div><div class="ttdeci">s16 gCurrentCourseId</div><div class="ttdef"><b>Definition:</b> code_800029B0.c:30</div></div>
|
|
</div><!-- fragment --><p> This code loads resources based on a specific course. An entire spaghetti tree of code may be imagined based on many series of conditional statements.</p>
|
|
<h1><a class="anchor" id="autotoc_md25"></a>
|
|
Threading</h1>
|
|
<p>The game begins by setting up its four threads; idle, video, audio, and the game loop. The idle thread allows the cpu to sleep. Without it, if at any time execution of all threads were paused, the cpu would never be able to continue. The idle thread is active if all the other threads are paused.</p>
|
|
<p>As such, the idle thread runs the following loop: <code>while(TRUE);</code> (it runs in a perpetual loop of nothing; sleep). In mips assembly it looks like this: </p><div class="fragment"><div class="line">.L800005B8:</div>
|
|
<div class="line">b .L800005B8</div>
|
|
<div class="line">nop </div>
|
|
</div><!-- fragment --><p> b stands for branch which acts akin to a goto. In this case, branch to the label <code>.L800005B8</code>. This creates an infinite loop. Whenever the cpu branches it always runs the next instruction as well which is called a delay slot. This means the cpu will continuously branch then run <code>nop</code> or <code>no operation</code> with no method of escaping the loop (except for when another thread has a higher priority which means the cpu switches to that thread and stops running the idle thread).</p>
|
|
<p>N64 threads are ran based on priority running whichever thread holds the most of it. Threads can also pause and wait for events. <a class="el" href="structNote.html">Note</a> that the N64 is not multi-threaded by modern standards as the other threads contain specific purposes which slightly differs from the concept of splitting a single program into multiple processes for efficiency.</p>
|
|
<h1><a class="anchor" id="autotoc_md26"></a>
|
|
Overall Control Flow</h1>
|
|
<div class="fragment"><div class="line">init_threads:</div>
|
|
<div class="line"> idle, video, audio, gameloop</div>
|
|
<div class="line">gameloop:</div>
|
|
<div class="line"> audio</div>
|
|
<div class="line"> jumpTo a specific menu or race based on a gameState flag.</div>
|
|
<div class="line"> profiler</div>
|
|
<div class="line"> config_gfx_pool</div>
|
|
<div class="line"> read_controllers</div>
|
|
<div class="line"> game_state_controller</div>
|
|
<div class="line"> endDL/vsync</div>
|
|
<div class="line">game_state_controller:</div>
|
|
<div class="line"> switch(loc)</div>
|
|
<div class="line"> menus -> switch(menu) { // do menu stuff }</div>
|
|
<div class="line"> race_logic_loop -> spaghetti</div>
|
|
<div class="line"> podium_ceremony</div>
|
|
<div class="line"> credits</div>
|
|
<div class="line">video:</div>
|
|
<div class="line"> handles interaction between video/audio threads.</div>
|
|
<div class="line"> handles vblanking and some elements pertaining to framebuffer</div>
|
|
<div class="line"> most of all, handles which step of rendering a frame the cpu is in.</div>
|
|
<div class="line"> Checks when new to start new sp tasks</div>
|
|
</div><!-- fragment --><p> If mk64 is in a menu state it will branch off to the menu code, running relevant bits of code based on more flags such as which particular menu the user is in. This will loop until the state changes to a different one such as race mode. If mk64 is in a race state, then race related code is ran and it spaghetti's off into a wide series of branches. This may include concepts such as <code>isLinedUp, isRacing, isRaceFinished, gotoNextCourse, isHuman, and isAI</code>.</p>
|
|
<p>This relatively primitive design could be defined as a state machine from an abstract point of view. This would differ from an OOP design that uses objects and hierarchy. You will become very familiar with this design principle as you explore the code-base. During any step of the game loop, a switch can be setup to check a flag then run code relevant to the situation. For instance, a flag can check whether a race is in-progress or complete. If in-progress set the player to human controlled. If complete, set player to AI controlled. </p>
|
|
</div></div><!-- contents -->
|
|
</div><!-- PageDoc -->
|
|
</div><!-- doc-content -->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="navelem"><a class="el" href="basics.html">Understanding the Basics</a></li>
|
|
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|