1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-11-04 15:05:03 +00:00
6502bench/docs/sgtutorial/odds-ends.html
2021-06-07 17:29:42 -07:00

246 lines
9.6 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"
integrity="sha384-vtXRMe3mGCbOeY7l30aIg8H9p3GdeSe4IFlP6G8JMa7o7lXvnz3GFKzPxzJdPfGK" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"/>
<link rel="stylesheet" href="/main.css"/>
<title>Odds &amp; Ends - SourceGen Tutorial</title>
</head>
<body>
<div id="masthead">
<!-- START: /masthead-incl.html -->
<!--<div class="masthead-title" style="background-image: url('images/screenshot-mainwin.png');">-->
<div class="masthead-title">
6502bench
</div>
<!-- END: /masthead-incl.html -->
</div>
<div id="topnav">
<!-- START: /topnav-incl.html active:#topnav-sgtutorial -->
<!-- top navigation bar contents -->
<nav>
<a id="topnav-home" href="/">HOME</a>
<a id="topnav-sgtutorial" class="active" href="/sgtutorial">SourceGen Tutorial</a>
<a id="topnav-menuicon" href="javascript:void(0);" class="icon" onclick="toggleSidenav()">
<i class="fa fa-bars"></i>
</a>
</nav>
<script>
// If page has no sidenav, don't show the sidenav toggle button.
if (document.getElementById("sidenav") == undefined) {
$("#topnav-menuicon").hide();
}
// Sidenav toggle function.
//
// Use a jQuery function to toggle the sidenav bar. The initial state
// is undefined / inherited, so it will pop in and out as the screen
// resizes around the "large" breakpoint.
function toggleSidenav() {
$("#sidenav").toggle("fast");
}
</script>
<!-- END: /topnav-incl.html -->
</div>
<div id="sidenav">
<!-- START: sidenav-incl.html active:#sidenav-odds-ends -->
<!-- side navigation bar contents -->
<ul>
<li id="sidenav-index"><a href="./">Introduction</a></li>
<li id="sidenav-about-disasm"><a href="about-disasm.html">About Disassembly</a></li>
<li id="sidenav-using-sourcegen"><a href="using-sourcegen.html">Using SourceGen</a>
<ul>
<li id="sidenav-moving-around"><a href="moving-around.html">Moving Around</a></li>
<li id="sidenav-simple-edits"><a href="simple-edits.html">Simple Edits</a></li>
<li id="sidenav-labels-symbols"><a href="labels-symbols.html">Labels &amp; Symbols</a></li>
<li id="sidenav-editing-data"><a href="editing-data.html">Editing Data Operands</a></li>
<li id="sidenav-generating-code"><a href="generating-code.html">Generating Code</a></li>
</ul></li>
<li id="sidenav-digging-deeper"><a href="digging-deeper.html">Digging Deeper</a>
<ul>
<li id="sidenav-string-formatting"><a href="string-formatting.html">String Formatting</a></li>
<li id="sidenav-local-variables"><a href="local-variables.html">Local Variables</a></li>
<li id="sidenav-inline-data"><a href="inline-data.html">Inline Data</a></li>
<li id="sidenav-odds-ends" class="active"><a href="odds-ends.html">Odds &amp; Ends</a></li>
</ul></li>
<li id="sidenav-advanced-topics"><a href="advanced-topics.html">Advanced Topics</a>
<ul>
<li id="sidenav-address-tables"><a href="address-tables.html">Address Tables</a></li>
<li id="sidenav-extension-scripts"><a href="extension-scripts.html">Extension Scripts</a></li>
<li id="sidenav-visualizations"><a href="visualizations.html">Visualizations</a></li>
</ul></li>
</ul>
<!-- END: sidenav-incl.html -->
</div>
<div id="main">
<h2>Odds &amp; Ends</h2>
<div class="grid-container">
<div class="grid-item-text">
<p>The rest of the code isn't really intended to do anything useful. It
just exists to illustrate some odd situations.</p>
</div>
</div>
<div class="grid-container">
<div class="grid-item-image">
<img src="images/t2-2078.png" alt="t2-2078"/>
</div>
<div class="grid-item-text">
<p>Look at the code starting at $2078. It ends with a <code>BRK</code>
at $2081, which as noted earlier is a bad sign. If you look two lines
above the <code>BRK</code>, you'll see that it's loading the accumulator
with zero, then doing a <code>BNE</code>, which should never be
taken (note the cycle count for the <code>BNE</code> is 2).
The trick is in the two lines before that, which use self-modifying code to
change the <code>LDA</code> immediate operand from $00 to $ff.
The <code>BNE</code> is actually a branch-always.</p>
</div>
</div>
<div class="grid-container">
<div class="grid-item-image">
<img src="images/t2-override-status.png" alt="t2-override-status.png"/>
</div>
<div class="grid-item-text">
<p>We can fix this by correcting the status flags. Select line $207F,
and then <samp>Actions &gt; Override Status Flags</samp>. This lets us specify what
the flags should be before the instruction is executed. For each flag,
we can override the default behavior and specify that the flag is
clear (0), set (1), or indeterminate (could be 0 or 1). In this case,
we know that the self-modified code will be loading a non-zero value, so
in the "<samp>Z</samp>" column click on the button in the "<samp>Zero</samp>" row.
Click "<samp>OK</samp>".</p>
</div>
</div>
<div class="grid-container">
<div class="grid-item-image">
<img src="images/t2-2078-done.png" alt="t2-2078-done"/>
</div>
<div class="grid-item-text">
<p>The <code>BNE</code> is now an always-taken branch, and the code
list rearranges itself appropriately (and the cycle count is now 3).</p>
</div>
</div>
<div class="grid-container">
<div class="grid-item-image">
<img src="images/t2-2086.png" alt="t2-2086"/>
</div>
<div class="grid-item-text">
<p>Continuing on, the code at $2086 touches a few consecutive locations
that have auto-generated labels.</p>
</div>
</div>
<div class="grid-container">
<div class="grid-item-image">
<img src="images/t2-2081-stuff.png" alt="t2-2081-stuff"/>
</div>
<div class="grid-item-text">
<p>Edit the label on line $2081, setting it to <kbd>STUFF</kbd>.
Notice how the references to $2081 through $2084 have changed from
auto-generated labels to references to <code>STUFF</code>.</p>
</div>
</div>
<div class="grid-container">
<div class="grid-item-image">
<img src="images/t2-seek-nearby.png" alt="t2-seek-nearby"/>
</div>
<div class="grid-item-text">
<p>For some projects this may be undesirable. Use
<samp>Edit &gt; Project Properties</samp>, then in the
<samp>Analysis Parameters</samp> box un-check
<samp>Seek nearby targets</samp>, and click <samp>OK</samp>.</p>
</div>
</div>
<div class="grid-container">
<div class="grid-item-text">
<p>You'll notice that the references to $2081 and later have switched
back to auto labels. If you scroll up, you'll see that the references to
<code>PTR1+1</code> and <code>PTR2+1</code> were
not affected, because local variables use explicit widths rather
than the "nearby" logic.</p>
<p>The nearby-target behavior is generally desirable, because it lets you
avoid explicitly labeling every part of a multi-byte data item. For now,
use <samp>Edit &gt; Undo</samp> to switch it back on.
(Changes to project properties are added to the undo/redo buffer
just like any other change to the project.)</p>
</div>
</div>
<div class="grid-container">
<div class="grid-item-image">
<img src="images/t2-2092.png" alt="t2-2092"/>
</div>
<div class="grid-item-text">
<p>The code at $2092 looks a bit strange. <code>LDX</code>, then a
<code>BIT</code> with a weird symbol, then another <code>LDX</code>. If
you look at the "bytes" column, you'll notice that the three-byte
<code>BIT</code> instruction has only one byte on its line.</p>
</div>
</div>
<div class="grid-container">
<div class="grid-item-text">
<p>The trick here is that the <code>LDX #$01</code> is embedded inside the
<code>BIT</code> instruction. When the code runs through here, X is set
to $00, then the <code>BIT</code> instruction sets some flags, then the
<code>STA</code> runs. Several lines down there's a <code>BNE</code>
to $2095, which is in the middle of the <code>BIT</code> instruction.
It loads X with $01, then also continues to the <code>STA</code>.</p>
<p>Embedded instructions are unusual but not unheard-of. (This trick is
used extensively in Microsoft BASICs, such as Applesoft.) When you see the
extra symbol in the opcode field, you need to look closely at what's going
on.</p>
</div>
</div>
<hr style="width:80%;"/>
<div class="grid-container">
<div class="grid-item-text">
<p>This is the end of the basic tutorial (congratulations!).
The next sections explore some advanced topics.</p>
</div>
</div>
</div> <!-- #main -->
<div id="prevnext">
<a href="inline-data.html" class="btn-previous">&laquo; Previous</a>
<a href="advanced-topics.html" class="btn-next">Next &raquo;</a>
</div>
<div id="footer">
<!-- START: /footer-incl.html -->
<hr/>
<p>Copyright 2021 faddenSoft</p>
<!-- <p id="screen-size"></p>
<script>
var w = window.innerWidth;
var h = window.innerHeight;
var x = document.getElementById("screen-size");
x.innerHTML = "DEBUG: initial window size " + w + "x" + h;
</script> -->
<!-- END: /footer-incl.html -->
</div>
</body>
</html>