189 lines
14 KiB
HTML
189 lines
14 KiB
HTML
<!-- HTML header for doxygen 1.10.0-->
|
|
<!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" lang="en-US">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.13.2"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Mario Kart 64: Basic Concepts</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>
|
|
<script type="text/javascript" src="clipboard.js"></script>
|
|
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="navtreedata.js"></script>
|
|
<script type="text/javascript" src="navtree.js"></script>
|
|
<script type="text/javascript" src="resize.js"></script>
|
|
<script type="text/javascript" src="cookie.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"/>
|
|
<link href="doxygen-awesome-sidebar-only-darkmode-toggle.css" rel="stylesheet" type="text/css"/>
|
|
<link href="docs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="doxygen-awesome-darkmode-toggle.js"></script>
|
|
<script type="text/javascript">
|
|
DoxygenAwesomeDarkModeToggle.init()
|
|
</script>
|
|
</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 id="projectrow">
|
|
<td id="projectalign">
|
|
<div id="projectname">Mario Kart 64
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.13.2 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "search/",'.html');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function() { codefold.init(0); });
|
|
/* @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:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function() {
|
|
initMenu('',true,false,'search.php','Search',true);
|
|
$(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:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function(){initNavTree('concepts.html',''); initResizable(true); });
|
|
/* @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">
|
|
<div id="MSearchResults">
|
|
<div class="SRPage">
|
|
<div id="SRIndex">
|
|
<div id="SRResults"></div>
|
|
<div class="SRStatus" id="Loading">Loading...</div>
|
|
<div class="SRStatus" id="Searching">Searching...</div>
|
|
<div class="SRStatus" id="NoMatches">No Matches</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div><div class="header">
|
|
<div class="headertitle"><div class="title">Basic Concepts</div></div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="textblock"><p>See <a href="terminology.html">terminology</a> for explanations of terms. <br />
|
|
</p>
|
|
<h1><a class="anchor" id="addresses"></a>
|
|
Addresses</h1>
|
|
<p>Computers use addresses to jump through code. This works similar to a house number.</p><ul>
|
|
<li>A house contains a series of instructions or some data (ex. a texture).</li>
|
|
<li>A house can contain more jumps.</li>
|
|
<li>When business in a house completes, the computer jumps to the last house it was in.</li>
|
|
<li>The program execution completes when there are no more branches left.</li>
|
|
<li>Programs run in a big infinite loop so it cannot run out of branches.</li>
|
|
<li>A program may be imagined as a big spaghetti monster.</li>
|
|
<li><a class="el" href="structNote.html">Note</a> that n64 only runs one command/thread at a time.</li>
|
|
</ul>
|
|
<p>A typical N64 address: <code>0x80160158</code></p>
|
|
<p>In an N64 program, addresses begin at <code>0x80000000</code>. As you create code, data, and the like, the compiler will add onto this base number and assign variables to an address. </p><div class="fragment"><div class="line">s32 a = 2; <span class="comment">// 0x80000004</span></div>
|
|
<div class="line">s32 b = 7; <span class="comment">// 0x80000008</span></div>
|
|
<div class="line">s32 c = 5; <span class="comment">// 0x8000000C</span></div>
|
|
<div class="line">s32 d = 1; <span class="comment">// 0x80000010</span></div>
|
|
<div class="line">s32 e = 3; <span class="comment">// 0x80000014</span></div>
|
|
</div><!-- fragment --><p> Replacing a variable with a hard-coded address grabs the value at that address. </p><div class="fragment"><div class="line"><a class="code hl_function" href="is__debug_8c.html#a42e65774c872bb2ffd35e7827f481776">print</a>(0x80000004); <span class="comment">// or</span></div>
|
|
<div class="line"><a class="code hl_function" href="is__debug_8c.html#a42e65774c872bb2ffd35e7827f481776">print</a>(a);</div>
|
|
<div class="ttc" id="ais__debug_8c_html_a42e65774c872bb2ffd35e7827f481776"><div class="ttname"><a href="is__debug_8c.html#a42e65774c872bb2ffd35e7827f481776">print</a></div><div class="ttdeci">void print(const char *fmt,...)</div><div class="ttdef"><b>Definition</b> is_debug.c:83</div></div>
|
|
</div><!-- fragment --><p> Both would output <code>2</code>. This allows some trickery. Such as pointer math: <code>print(0x800000004 + 0x80000004)</code> would output <code>7</code>. Pointers may be similarily influenced: <code>&a + 4</code> results in <code>0x800000008</code> thus printing the value at <code>b</code>.</p>
|
|
<p>The compiler may <code>align</code> or offset variables to certain addresses. Generally, certain types of data is aligned to 0x10.</p>
|
|
<p>If <code>c</code> were placed in a new file, the compiler would place <code>c</code> at <code>0x80000010</code> and <code>e</code> would be placed at <code>0x80000020</code>.</p>
|
|
<p>Data can take up tonnes of space: </p><div class="fragment"><div class="line">Texture a_texture[] = {</div>
|
|
<div class="line"> 0x01, 0x0A, 0xAA, 0x34, 0x23, // Imagine five-hundred lines of this</div>
|
|
<div class="line">};</div>
|
|
</div><!-- fragment --><p> The address of this could extend from <code>0x80000100</code> to <code>0x800000534</code>. However, you would not see data begin at <code>0x80000533</code>. The compiler generally aligns objects to 4, 8, 0xC, or 0x10. With the exception being when you're working with s16 and s8. If you have: </p><div class="fragment"><div class="line">s16 a = 3; <span class="comment">// Value compiled to 0x0003</span></div>
|
|
<div class="line">s32 b = 5; <span class="comment">// Value compiled to 0x00000005</span></div>
|
|
</div><!-- fragment --><p> In a hexadecimal editor you may expect to see this: <code>000300000005</code> or <code>0x0003 0x00000005</code> But actually, it will be this: </p><div class="fragment"><div class="line">0003000000000005 or 0x0003 0x0000 0x00000005</div>
|
|
</div><!-- fragment --><p> The compiler aligned <code>b</code> to the nearest 0x4. If there were two s16's then the blank <code>0x0000</code> will be used.</p>
|
|
<h1><a class="anchor" id="how-errors-happen"></a>
|
|
How Errors Happen</h1>
|
|
<p>The maximum value of an s8 (0x00) is 0xFF or 255. <code>0xFF + 1 = 0x100</code> imagine this scenario: </p><div class="fragment"><div class="line">s8 a = 1;</div>
|
|
<div class="line">s8 b = 2;</div>
|
|
<div class="line">s8 c = 3;</div>
|
|
<div class="line">s8 d = 255;</div>
|
|
<div class="line">f32 e = 5.0f;</div>
|
|
</div><!-- fragment --><p> If <code>d</code> was set to 0x100, the final <code>0</code> would be written to the first bit of <code>e</code>. This could result in an invalid float value or perhaps flipping the signedness of an s32 (from negative to positive and vice-versa).</p>
|
|
<p>Issues such as this could result in glitches or crashes. Once humble math calculating to immeasurable values.</p>
|
|
<h2><a class="anchor" id="array-overflows"></a>
|
|
Array Overflows</h2>
|
|
<p>In the below example, my_func writes a value at the fifth index which does not exist. This results in writing 5.0f into the next variable lkely resulting in a crash. Array overflows may be easy to miss in complex programs and sometimes only crash on rare occasion. </p><div class="fragment"><div class="line">f32 myArray[4] = {8.0f, 3.0f, 9.0f, 1.0f};</div>
|
|
<div class="line"> </div>
|
|
<div class="line">void my_func(index) {</div>
|
|
<div class="line"> myArray[index] = 5.0f;</div>
|
|
<div class="line">}</div>
|
|
<div class="line">my_func(5);</div>
|
|
</div><!-- fragment --><h1><a class="anchor" id="code"></a>
|
|
Code</h1>
|
|
<p>Code contains addresses too. Lets take a look at some example assembly: </p><div class="fragment"><div class="line">glabel entry_point</div>
|
|
<div class="line">/* 001000 80000400 3C08800F */ lui $t0, %hi(_mainSegmentEnd) # $t0, 0x800f</div>
|
|
<div class="line">/* 001004 80000404 3C09000A */ lui $t1, (0x000A0FC0 >> 16) # lui $t1, 0xa</div>
|
|
<div class="line">/* 001008 80000408 25086910 */ addiu $t0, %lo(_mainSegmentEnd) # addiu $t0, $t0, 0x6910</div>
|
|
<div class="line">/* 00100C 8000040C 35290FC0 */ ori $t1, (0x000A0FC0 & 0xFFFF) # ori $t1, $t1, 0xfc0</div>
|
|
<div class="line">.L80000410:</div>
|
|
<div class="line">/* 001010 80000410 2129FFF8 */ addi $t1, $t1, -8</div>
|
|
<div class="line">/* 001014 80000414 AD000000 */ sw $zero, ($t0)</div>
|
|
<div class="line">/* 001018 80000418 AD000004 */ sw $zero, 4($t0)</div>
|
|
</div><!-- fragment --><p> Format: <code>rom address (file on disc), ram address, machine-code, assembly, arguments/parameters</code></p>
|
|
<p>The <code>lui, addiu, ori, etc.</code> are just representations of the machine-code. For example: <code>0x3C08800F</code> is what the cpu is actually running. Lets break down this one command: <code>op-code, parameters</code> 0x3C is the op-code (lui or load-upper-immediate) this command loads an address. The <code>08</code> tells the CPU to use the register <code>t0</code> and the <code>0x800F</code> is the first-half of the address to load. So, in the register t0 (which is the size of an int: 0x00000000) the value <code>0x800F0000</code> is written.</p>
|
|
<p><a class="el" href="structNote.html">Note</a> that one machine-code command is the size of an int/s32/word. How do we load an address (which is the size of an int). There isn't enough room when you include the op-code and register: <code>0x3C08800F6990</code>. That no worky. As such, loading data at an address requires two commands. The first half of the address is called a hi and the second half is called a lo: </p><div class="fragment"><div class="line">/* 001008 80000408 25086910 */ addiu $t0, %lo(_mainSegmentEnd) # addiu $t0, $t0, 0x6910</div>
|
|
</div><!-- fragment --><p> Now, we're going to <em>add</em> the <em>offset</em>: <code>t0: 0x800F0000 + 0x6910 = 0x800F6910</code>. If you run the game and use a memory viewer at this location. You will quickly realize this region of data (it's actually .bss but lets not get into that), is the controller data. The data alters to reflect which controller buttons are pressed. So if I just wanted to read one of these values, the cpu will run two commands to put the address into the register. Then another command can read the address in that register to receive the value at that location. This value can be compared to see if the button is active. Ex. zero might mean not pressed, whereas one might mean pressed.</p>
|
|
<p>Now, earlier it was mentioned that code contains addresses. If I wanted to, I could do this: <code>Jump to: 0x80000410</code>. Which would make the CPU start running code at that location. Or I could read the value that exists there: <code>0x2129FFF8</code>. Not sure what I might do with that number, but it does act like a normal number. In decimal it is: <code>556,400,632</code>. I could add and subtract to it (Which would likely crash the game if it tried to run that command after). Now, jumping to any code address in the middle of a function would almost certainly crash the game or result in strange behaviour. The purpose of this explanation is really to show that everything is just a bunch of numbers or values, represented by an address. Even the code itself.</p>
|
|
<p>For a more refined explanation of addresses and pointers its suggested to use the Googles. However, this explanation was written with the N64 in-mind. </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.13.2 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|