mirror of
https://github.com/fadden/6502bench.git
synced 2024-11-03 23:06:09 +00:00
198 lines
7.5 KiB
HTML
198 lines
7.5 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 & Ends - SourceGen Tutorial</title>
|
||
|
</head>
|
||
|
|
||
|
<body>
|
||
|
|
||
|
<div id="masthead">
|
||
|
<!-- START: /masthead-incl.html -->
|
||
|
<script>$("#masthead").load("/masthead-incl.html");</script>
|
||
|
<!-- END: /masthead-incl.html -->
|
||
|
</div>
|
||
|
|
||
|
<div id="topnav">
|
||
|
<!-- START: /topnav-incl.html active:#topnav-sgtutorial -->
|
||
|
<script>
|
||
|
// Load global topnav content, and mark current page active.
|
||
|
$("#topnav").load("/topnav-incl.html", function() {
|
||
|
$("#topnav-sgtutorial").addClass("active");
|
||
|
});
|
||
|
</script>
|
||
|
<!-- END: /topnav-incl.html -->
|
||
|
</div>
|
||
|
|
||
|
<div id="sidenav">
|
||
|
<!-- START: /sidenav-incl.html active:#sidenav-odds-ends -->
|
||
|
<script>
|
||
|
// Load local sidenav content, and mark current page active.
|
||
|
$("#sidenav").load("sidenav-incl.html", function() {
|
||
|
$("#sidenav-odds-ends").addClass("active");
|
||
|
});
|
||
|
</script>
|
||
|
<!-- END: /sidenav-incl.html -->
|
||
|
</div>
|
||
|
|
||
|
<div id="main">
|
||
|
|
||
|
<h2>Odds & 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 > 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 > 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 > 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="#" class="btn-previous">« Previous</a>
|
||
|
<a href="#" class="btn-next">Next »</a>
|
||
|
</div>
|
||
|
|
||
|
<div id="footer">
|
||
|
<!-- START: /footer-incl.html -->
|
||
|
<script>$("#footer").load("/footer-incl.html");</script>
|
||
|
<!-- END: /footer-incl.html -->
|
||
|
</div>
|
||
|
|
||
|
</body>
|
||
|
</html>
|