Files
mk64/basics.html
T

150 lines
12 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: &quot;Understanding the Basics&quot;</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&amp;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&amp;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&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('basics.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">"Understanding the Basics" </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h4><a class="anchor" id="autotoc_md12"></a>
Definitions</h4>
<ul>
<li><code>Jump/Branch/Function</code> - Tells the cpu to execute instructions somewhere else and come back here when done.</li>
<li><code>Hexadecimal</code> - A <a href="https://en.wikipedia.org/wiki/Hexadecimal">numbering system</a> just like decimal (also called base-ten) or roman numerals. An easy method for programmers and computers to understand each other which groups/separates bytes of information in a readable format. Representing information as decimal results in an unreadable mess.</li>
<li><code>s32/word/int</code> - A max size for a number. Data represented as 32 bits or 4 bytes: <code>0x00000001</code>. It consists of eight digits.</li>
<li><code>s16/half-word/short</code> - A number represented as 16 bits or 2 bytes: <code>0x0001</code>. It consists of four digits. Maximum value 65,535 (0xFFFF).</li>
<li><code>s8/byte/char</code> - A number represented as 8 bits or 1 byte: <code>0x01</code>. It consists of two digits. Maximum value 255 (0xFF).</li>
</ul>
<h2><a class="anchor" id="autotoc_md13"></a>
Addresses</h2>
<p>Computers use addresses to jump through code. This works similar to finding a house using its address. Except, that reaching the house is nearly instantaneous and it only contains more instructions and jumps or some data such as a texture or a text string. Also, when finished with the house, you travel back to the previous destination which may have some left-over instructions or more jumps. You can imagine this quickly becoming a spaghetti monster jumping all over the place.</p>
<p>A typical N64 address: <code>0x8029E158</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_value = 2; // Note that a_value can be any name: fox, pig, siodfj, etc.</div>
<div class="line">s32 b_value = 7;</div>
</div><!-- fragment --><p> Might compile a_value to <code>0x80000004</code> and b_value to <code>0x80000008</code>. At anytime if you manually wrote: </p><div class="fragment"><div class="line">print(0x80000004); // or</div>
<div class="line">print(a_value);</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>.</p>
<p>Addresses generally count up by fours: <code>0x4, 0x8, 0xC, 0x10, 0x14, etc.</code></p>
<p>It does not when working with s8 and s16 integer types. An s32 or int always takes up eight digits: <code>00000001</code> </p><div class="fragment"><div class="line">s32 a = 1;</div>
<div class="line">print(a);</div>
</div><!-- fragment --><p> Will output <code>1</code> but if you opened the program in a hexadecimal file you will notice that its written to disc as <code>0x00000001</code></p>
<p>An s16 looks like: <code>0x0001</code> an s8 looks like: <code>0x01</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 <code>0x80000533</code>. The compiler generally aligns objects to 4, 8, 0xC, or 0x10 (<a class="el" href="structNote.html">Note</a> that files are generally aligned to 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;</div>
<div class="line">s32 b = 5;</div>
</div><!-- fragment --><p> You might 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> This is called alignment. The compiler <em>aligned</em> <code>b</code> to the next 'major' address. Of course, if you program two s16's then the blank <code>0x0000</code> will be used. The max size of an s8 is 0xFF (255). Trying to write a number higher than 255 to an s8 type may crash the program as it might try to write onto the next variable. <br />
</p>
<h3><a class="anchor" id="autotoc_md14"></a>
Code</h3>
<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 &gt;&gt; 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 &amp; 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. So we have to do this with two commands called a hi and 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 assembly will do two commands to put the address in 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 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="md_docs_Understanding_the_Basics_index.html">index</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>