<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Michael &#8211; sim6502</title>
	<atom:link href="https://www.sim6502.de/author/mjbohn/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sim6502.de</link>
	<description>Yet another...</description>
	<lastBuildDate>Fri, 08 Aug 2025 22:17:21 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.sim6502.de/wp-content/uploads/2025/07/mos6502-150x150.png</url>
	<title>Michael &#8211; sim6502</title>
	<link>https://www.sim6502.de</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>STX &#038; STY – Store the X and Y Registers</title>
		<link>https://www.sim6502.de/2025/08/09/stx-sty-store-the-x-and-y-registers/</link>
					<comments>https://www.sim6502.de/2025/08/09/stx-sty-store-the-x-and-y-registers/#respond</comments>
		
		<dc:creator><![CDATA[Michael]]></dc:creator>
		<pubDate>Fri, 08 Aug 2025 22:15:45 +0000</pubDate>
				<category><![CDATA[Knowledge]]></category>
		<category><![CDATA[Opcodes]]></category>
		<guid isPermaLink="false">https://www.sim6502.de/?p=542</guid>

					<description><![CDATA[After learning about STA, the instructions STX and STY follow naturally. They allow your program to store the contents of the X and Y index registers into memory. If you’re managing data structures, looping through arrays, or setting up pointers, you’ll frequently want to move index values into memory — and that’s where these instructions.]]></description>
										<content:encoded><![CDATA[
<p>After learning about <code>STA</code>, the instructions <code>STX</code> and <code>STY</code> follow naturally. They allow your program to store the contents of the <strong>X</strong> and <strong>Y</strong> index registers into memory.</p>



<p>If you’re managing data structures, looping through arrays, or setting up pointers, you’ll frequently want to move index values into memory — and that’s where these instructions come in.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">What Do STX and STY Do?</h3>



<p>Both instructions work exactly like <code>STA</code>, but they store the contents of the <strong>X</strong> or <strong>Y</strong> register instead of the accumulator.</p>



<p>They are purely output instructions — they move a value from a register into a given memory location.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Supported Addressing Modes</h3>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Instruction</th><th>Addressing Mode</th><th>Example</th><th>Opcode</th><th>Bytes</th><th>Cycles</th></tr></thead><tbody><tr><td><strong>STX</strong></td><td>Zero Page</td><td><code>STX $00</code></td><td><code>$86</code></td><td>2</td><td>3</td></tr><tr><td></td><td>Zero Page, Y</td><td><code>STX $10,Y</code></td><td><code>$96</code></td><td>2</td><td>4</td></tr><tr><td></td><td>Absolute</td><td><code>STX $1234</code></td><td><code>$8E</code></td><td>3</td><td>4</td></tr><tr><td><strong>STY</strong></td><td>Zero Page</td><td><code>STY $00</code></td><td><code>$84</code></td><td>2</td><td>3</td></tr><tr><td></td><td>Zero Page, X</td><td><code>STY $10,X</code></td><td><code>$94</code></td><td>2</td><td>4</td></tr><tr><td></td><td>Absolute</td><td><code>STY $1234</code></td><td><code>$8C</code></td><td>3</td><td>4</td></tr></tbody></table></figure>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Notice:</p>



<ul class="wp-block-list">
<li><code>STX</code> supports <strong>Zero Page, Y</strong></li>



<li><code>STY</code> supports <strong>Zero Page, X</strong></li>



<li>Neither supports <strong>absolute,X/Y</strong> or <strong>indirect</strong> modes</li>



<li>And like <code>STA</code>, <strong>no immediate mode</strong></li>
</ul>
</blockquote>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Flags Affected</h3>



<p><strong>None.</strong><br>Just like <code>STA</code>, these instructions <strong>do not modify any status flags</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Example Usage</h3>



<pre class="wp-block-code"><code>LDX #$FF
STX $D020      ; Set C64 border color using X register

LDY #$12
STY $C0        ; Store Y into zero page memory

LDX #$33
STX $0200,Y    ; Not valid! STX does not support (abs),Y
</code></pre>



<p>Indexed zero page example:</p>



<pre class="wp-block-code"><code>LDY #$02
LDX #$A0
STX $10,Y      ; Store X at address $12
</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Common Pitfalls</h3>



<ul class="wp-block-list">
<li><strong>Misunderstanding addressing support.</strong><br><code>STX</code> and <code>STY</code> are more limited than <code>STA</code>. If you need flexible addressing, move the value into A and use <code>STA</code>.</li>



<li><strong>Trying to use STX/STY with absolute,X/Y.</strong><br>These modes are not valid with <code>STX</code> or <code>STY</code>.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Summary</h3>



<p><code>STX</code> and <code>STY</code> are crucial for saving index values to memory:</p>



<ul class="wp-block-list">
<li>Store the contents of the <strong>X</strong> or <strong>Y</strong> register</li>



<li>Support <strong>zero page</strong>, <strong>indexed zero page</strong>, and <strong>absolute</strong> addressing</li>



<li>Don’t affect CPU flags</li>



<li>Great for setting up buffers, counters, and memory-mapped I/O</li>
</ul>



<p>Combined with <code>STA</code>, these instructions give you full control over writing all main registers to memory. </p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sim6502.de/2025/08/09/stx-sty-store-the-x-and-y-registers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>STA – Store Accumulator</title>
		<link>https://www.sim6502.de/2025/08/06/sta-store-accumulator/</link>
					<comments>https://www.sim6502.de/2025/08/06/sta-store-accumulator/#respond</comments>
		
		<dc:creator><![CDATA[Michael]]></dc:creator>
		<pubDate>Wed, 06 Aug 2025 16:00:00 +0000</pubDate>
				<category><![CDATA[Knowledge]]></category>
		<category><![CDATA[Opcodes]]></category>
		<guid isPermaLink="false">https://www.sim6502.de/?p=537</guid>

					<description><![CDATA[After learning how to load values into the accumulator using LDA, it’s time to store them back into memory. That’s exactly what the STA instruction does. Where LDA is about input, STA is about output. It allows your 6502 program to write the value currently held in the accumulator (A) to a specific memory address..]]></description>
										<content:encoded><![CDATA[
<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>After learning how to <strong>load</strong> values into the accumulator using <code>LDA</code>, it’s time to <strong>store</strong> them back into memory. That’s exactly what the <code>STA</code> instruction does.</p>



<p>Where <code>LDA</code> is about <strong>input</strong>, <code>STA</code> is about <strong>output</strong>. It allows your 6502 program to write the value currently held in the <strong>accumulator (A)</strong> to a specific memory address.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">What Does STA Do?</h3>



<p><code>STA</code> takes the value in the accumulator and writes it to a memory location. That location is specified using one of several <strong>addressing modes</strong>.</p>



<p>This is essential when you want to:</p>



<ul class="wp-block-list">
<li>Update variables</li>



<li>Write to memory-mapped hardware (like screen RAM or I/O)</li>



<li>Store intermediate results for later use</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Supported Addressing Modes</h3>



<p>Unlike <code>LDA</code>, <code>STA</code> does <strong>not</strong> support the <strong>immediate</strong> mode — because you can&#8217;t store to a literal value.</p>



<p>Here are the valid modes:</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Addressing Mode</th><th>Example</th><th>Opcode</th><th>Bytes</th><th>Cycles</th></tr></thead><tbody><tr><td>Zero Page</td><td><code>STA $00</code></td><td><code>$85</code></td><td>2</td><td>3</td></tr><tr><td>Zero Page, X</td><td><code>STA $10,X</code></td><td><code>$95</code></td><td>2</td><td>4</td></tr><tr><td>Absolute</td><td><code>STA $1234</code></td><td><code>$8D</code></td><td>3</td><td>4</td></tr><tr><td>Absolute, X</td><td><code>STA $1234,X</code></td><td><code>$9D</code></td><td>3</td><td>5</td></tr><tr><td>Absolute, Y</td><td><code>STA $1234,Y</code></td><td><code>$99</code></td><td>3</td><td>5</td></tr><tr><td>Indirect, X</td><td><code>STA ($20,X)</code></td><td><code>$81</code></td><td>2</td><td>6</td></tr><tr><td>Indirect, Y</td><td><code>STA ($20),Y</code></td><td><code>$91</code></td><td>2</td><td>6</td></tr></tbody></table></figure>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Flags Affected</h3>



<p><strong>None.</strong><br><code>STA</code> does not modify any status flags — not even Zero or Negative.</p>



<p>This makes sense: you’re writing data, not interpreting it.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Example Usage</h3>



<pre class="wp-block-code"><code>LDA #$41       ; Load ASCII 'A' into accumulator
STA $0400      ; Write it to screen memory at $0400

LDA #$00
STA $D020      ; Set border color on the C64
</code></pre>



<p>Or, with indexed addressing:</p>



<pre class="wp-block-code"><code>LDX #$05
LDA #$FF
STA $0200,X    ; Store $FF at address $0205
</code></pre>



<p>Indirect addressing:</p>



<pre class="wp-block-code"><code>LDA #$3C
STA ($20),Y    ; Store A to address pointed to by $20 + Y
</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Common Pitfalls</h3>



<ul class="wp-block-list">
<li><strong>Forgetting that STA doesn’t affect flags.</strong> If you plan to branch based on the accumulator value, test it <em>before</em> the STA.</li>



<li><strong>Using STA #$44</strong> — this is illegal. You can’t store to an immediate value!</li>



<li><strong>Misusing indirect modes.</strong> <code>STA ($20),Y</code> looks up a pointer from zero page <code>$20</code>, adds Y, and stores the value.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Summary</h3>



<p><code>STA</code> is the simplest way to send data from your CPU into memory — and that includes writing to hardware like video, sound, or I/O chips.</p>



<p>Key takeaways:</p>



<ul class="wp-block-list">
<li>Stores accumulator content into memory</li>



<li>Supports all basic addressing modes <strong>except immediate</strong></li>



<li><strong>Does not modify any flags</strong></li>



<li>Essential for output, memory updates, and I/O access</li>
</ul>



<p>With <code>LDA</code> and <code>STA</code>, you have the fundamental <strong>load-store mechanism</strong> of the 6502 down. Next up, we’ll explore storing other registers: <code>STX</code> and <code>STY</code>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sim6502.de/2025/08/06/sta-store-accumulator/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>LDX and LDY – Load Index Registers</title>
		<link>https://www.sim6502.de/2025/08/06/ldx-ldy/</link>
					<comments>https://www.sim6502.de/2025/08/06/ldx-ldy/#respond</comments>
		
		<dc:creator><![CDATA[Michael]]></dc:creator>
		<pubDate>Wed, 06 Aug 2025 16:00:00 +0000</pubDate>
				<category><![CDATA[Knowledge]]></category>
		<category><![CDATA[Opcodes]]></category>
		<guid isPermaLink="false">https://www.sim6502.de/?p=531</guid>

					<description><![CDATA[Let&#8217;s continue the blog series with a combined post about the 6502&#8217;s LDX and LDY instructions.The sibling instructions of LDA. In the 6502 instruction set, LDX and LDY are the counterparts to LDA. While LDA loads a value into the accumulator, LDX and LDY load values into the index registers X and Y, respectively. These.]]></description>
										<content:encoded><![CDATA[
<p>Let&#8217;s continue the blog series with a combined post about the 6502&#8217;s <code>LDX</code> and <code>LDY</code> instructions.<br>The <em>sibling</em> instructions of <code>LDA</code>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>In the 6502 instruction set, <code>LDX</code> and <code>LDY</code> are the counterparts to <code>LDA</code>. While <code>LDA</code> loads a value into the <strong>accumulator</strong>, <code>LDX</code> and <code>LDY</code> load values into the <strong>index registers X and Y</strong>, respectively.</p>



<p>These index registers are used for addressing, iteration, and temporary storage in a wide variety of programming scenarios. Understanding how to load them efficiently is crucial for tight, readable, and performant 6502 code.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">What Do LDX and LDY Do?</h3>



<ul class="wp-block-list">
<li><code>LDX</code> loads a value into the <strong>X register</strong></li>



<li><code>LDY</code> loads a value into the <strong>Y register</strong></li>
</ul>



<p>Both instructions support several addressing modes and affect two status flags: <strong>Zero (Z)</strong> and <strong>Negative (N)</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Supported Addressing Modes</h3>



<h4 class="wp-block-heading">LDX</h4>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Addressing Mode</th><th>Example</th><th>Opcode</th><th>Bytes</th><th>Cycles</th></tr></thead><tbody><tr><td>Immediate</td><td><code>LDX #$10</code></td><td><code>$A2</code></td><td>2</td><td>2</td></tr><tr><td>Zero Page</td><td><code>LDX $00</code></td><td><code>$A6</code></td><td>2</td><td>3</td></tr><tr><td>Zero Page, Y</td><td><code>LDX $10,Y</code></td><td><code>$B6</code></td><td>2</td><td>4</td></tr><tr><td>Absolute</td><td><code>LDX $1234</code></td><td><code>$AE</code></td><td>3</td><td>4</td></tr><tr><td>Absolute, Y</td><td><code>LDX $1234,Y</code></td><td><code>$BE</code></td><td>3</td><td>4 (+1)</td></tr></tbody></table></figure>



<h4 class="wp-block-heading">LDY</h4>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Addressing Mode</th><th>Example</th><th>Opcode</th><th>Bytes</th><th>Cycles</th></tr></thead><tbody><tr><td>Immediate</td><td><code>LDY #$10</code></td><td><code>$A0</code></td><td>2</td><td>2</td></tr><tr><td>Zero Page</td><td><code>LDY $00</code></td><td><code>$A4</code></td><td>2</td><td>3</td></tr><tr><td>Zero Page, X</td><td><code>LDY $10,X</code></td><td><code>$B4</code></td><td>2</td><td>4</td></tr><tr><td>Absolute</td><td><code>LDY $1234</code></td><td><code>$AC</code></td><td>3</td><td>4</td></tr><tr><td>Absolute, X</td><td><code>LDY $1234,X</code></td><td><code>$BC</code></td><td>3</td><td>4 (+1)</td></tr></tbody></table></figure>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Note: Indexed Absolute modes can take one extra cycle if a <strong>page boundary</strong> is crossed.</p>
</blockquote>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Flags Affected</h3>



<p>Just like <code>LDA</code>, both <code>LDX</code> and <code>LDY</code> affect:</p>



<ul class="wp-block-list">
<li><strong>Zero Flag (Z)</strong>: Set if the loaded value is <code>0</code></li>



<li><strong>Negative Flag (N)</strong>: Set if the high bit (bit 7) is set</li>
</ul>



<p>Other flags remain unchanged.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Practical Examples</h3>



<h4 class="wp-block-heading">Load X Register</h4>



<pre class="wp-block-code"><code>LDX #$0A      ; Load constant 10 into X
LDX $20       ; Load value from zero page address $0020
LDX $1000,Y   ; Load value from $1000 + Y
</code></pre>



<h4 class="wp-block-heading">Load Y Register</h4>



<pre class="wp-block-code"><code>LDY #$FF      ; Load constant 255 into Y
LDY $D000,X   ; Load value from $D000 + X
</code></pre>



<p>These instructions are often used for:</p>



<ul class="wp-block-list">
<li>Setting up offsets for indexed memory access</li>



<li>Loop counters and decrement operations</li>



<li>Calling <code>JSR</code> routines that expect parameters in X or Y</li>



<li>Configuring sprite data or screen memory locations</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Typical Usage Patterns</h3>



<pre class="wp-block-code"><code>LDX #$00
LOOP:
  LDA message,X
  BEQ done
  STA $0400,X
  INX
  JMP LOOP
done:
</code></pre>



<p>In this example, <code>LDX</code> is used to walk through a string and copy it to screen memory. A classic use case for the X register.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Common Pitfalls</h3>



<ul class="wp-block-list">
<li>Using the wrong indexed mode (e.g., <code>LDX $10,X</code> → <strong>invalid!</strong>) — LDX only supports <strong>Zero Page, Y</strong> and <strong>Absolute, Y</strong></li>



<li>Assuming <code>LDX</code> and <code>LDY</code> behave exactly like <code>LDA</code> — their <strong>indexed modes differ</strong></li>



<li>Forgetting that setting flags can affect branching after a load</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Summary</h3>



<p>While <code>LDA</code> is used to manipulate data, <code>LDX</code> and <code>LDY</code> are your tools for managing control and structure. They&#8217;re essential when looping through data, accessing arrays, or working with hardware registers.</p>



<p>Key takeaways:</p>



<ul class="wp-block-list">
<li>Both instructions support immediate, zero page, and absolute modes</li>



<li><code>LDX</code> uses Y as an index for zero-page; <code>LDY</code> uses X</li>



<li>Flags Z and N are always updated based on the loaded value</li>
</ul>



<p>Together, <code>LDA</code>, <code>LDX</code>, and <code>LDY</code> form the <strong>foundation of memory access</strong> on the 6502. Master these three, and you&#8217;re well on your way to becoming fluent in low-level 6502 programming.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sim6502.de/2025/08/06/ldx-ldy/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>LDA – Load Accumulator</title>
		<link>https://www.sim6502.de/2025/08/01/lda-load-accumulator/</link>
					<comments>https://www.sim6502.de/2025/08/01/lda-load-accumulator/#respond</comments>
		
		<dc:creator><![CDATA[Michael]]></dc:creator>
		<pubDate>Fri, 01 Aug 2025 04:00:00 +0000</pubDate>
				<category><![CDATA[Knowledge]]></category>
		<category><![CDATA[Opcodes]]></category>
		<guid isPermaLink="false">https://www.sim6502.de/?p=505</guid>

					<description><![CDATA[The LDA (Load Accumulator) instruction is one of the most frequently used opcodes on the 6502 processor. It loads an 8-bit value into the A register, also known as the accumulator. This value can come from memory or be embedded directly in the instruction itself. Understanding LDA is a great starting point for learning how.]]></description>
										<content:encoded><![CDATA[
<p>The <code>LDA</code> (Load Accumulator) instruction is one of the most frequently used opcodes on the 6502 processor. It loads an 8-bit value into the <strong>A register</strong>, also known as the <em>accumulator</em>. This value can come from memory or be embedded directly in the instruction itself.</p>



<p>Understanding LDA is a great starting point for learning how the 6502 interacts with memory and how addressing modes work.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>What LDA Does</strong></h3>



<p>At its core, LDA performs one simple task:<br><strong>It loads a value into the accumulator (A).</strong></p>



<p>But depending on the <strong>addressing mode</strong>, the source of that value can vary:</p>



<ul class="wp-block-list">
<li>A constant value (immediate)</li>



<li>A memory location (zero page, absolute)</li>



<li>A computed address (indexed, indirect)<br></li>
</ul>



<p>Each variation has its own binary opcode and CPU cycle cost.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Supported Addressing Modes</strong></h3>



<p>Here are all the addressing modes that LDA supports:</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td><strong>Addressing Mode</strong></td><td><strong>Example</strong></td><td><strong>Opcode</strong></td><td><strong>Bytes</strong></td><td><strong>Cycles</strong></td><td><strong>Description</strong></td></tr><tr><td>Immediate</td><td>LDA #$10</td><td>$A9</td><td>2</td><td>2</td><td>Load a constant value</td></tr><tr><td>Zero Page</td><td>LDA $00</td><td>$A5</td><td>2</td><td>3</td><td>Load from first 256 bytes</td></tr><tr><td>Zero Page, X</td><td>LDA $10,X</td><td>$B5</td><td>2</td><td>4</td><td>Add X to base zero-page address</td></tr><tr><td>Absolute</td><td>LDA $1234</td><td>$AD</td><td>3</td><td>4</td><td>Load from a full 16-bit address</td></tr><tr><td>Absolute, X</td><td>LDA $1234,X</td><td>$BD</td><td>3</td><td>4 (+1)</td><td>Add X to absolute address</td></tr><tr><td>Absolute, Y</td><td>LDA $1234,Y</td><td>$B9</td><td>3</td><td>4 (+1)</td><td>Add Y to absolute address</td></tr><tr><td>Indirect, X</td><td>LDA ($20,X)</td><td>$A1</td><td>2</td><td>6</td><td>Add X, then dereference pointer</td></tr><tr><td>Indirect, Y</td><td>LDA ($20),Y</td><td>$B1</td><td>2</td><td>5 (+1)</td><td>Dereference, then add Y</td></tr></tbody></table></figure>



<p>Note: Modes marked with (+1) may require an extra cycle if a page boundary is crossed.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Flags Affected</strong></h3>



<p>Executing LDA updates two status flags:</p>



<ul class="wp-block-list">
<li><strong>Zero Flag (Z)</strong>: Set if the loaded value is 0</li>



<li><strong>Negative Flag (N)</strong>: Set if the high bit (bit 7) of the value is set (i.e., if the value is negative in two’s complement)<br></li>
</ul>



<p>No other flags are affected.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Example: Loading a Constant</strong></h3>



<pre class="wp-block-code"><code>LDA #$42</code></pre>



<p>This loads the hexadecimal value $42 into the accumulator. The Z and N flags are updated accordingly.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Example: Reading from Memory</strong></h3>



<pre class="wp-block-code"><code>LDA $0400</code></pre>



<p>Loads the value stored at address $0400—which happens to be the start of the C64&#8217;s screen memory—into A.</p>



<p>If used in a loop, this kind of instruction is ideal for reading and modifying blocks of memory.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Example: Using Indirect Addressing</strong></h3>



<pre class="wp-block-code"><code>LDA ($10),Y</code></pre>



<p>This is a powerful form of pointer-based memory access. It allows you to store a base address at $10/$11, and then offset it with Y. This is often used for accessing tables or sprite data.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Common Pitfalls</strong></h3>



<ul class="wp-block-list">
<li>Forgetting that only <strong>Z</strong> and <strong>N</strong> flags are affected. If you&#8217;re branching based on carry or overflow after LDA, you&#8217;ll get misleading results.</li>



<li>Not accounting for the <strong>extra cycle</strong> on page crossings in Absolute,X and Indirect,Y modes. This matters for cycle-accurate code like raster interrupts or real-time effects.<br></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Summary</strong></h3>



<p>LDA is essential to nearly all 6502 programs. It’s how you get data into the accumulator to do something useful—whether that’s math, logic, display updates, or system calls.</p>



<p>Once you understand how LDA works across all addressing modes, you’ve unlocked a fundamental part of 6502 programming. The next steps? Explore its siblings LDX and LDY, and learn how the 6502 transfers and manipulates data across registers.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sim6502.de/2025/08/01/lda-load-accumulator/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Understanding the 6502: What is an Opcode?</title>
		<link>https://www.sim6502.de/2025/07/31/understanding-the-6502-what-is-an-opcode/</link>
					<comments>https://www.sim6502.de/2025/07/31/understanding-the-6502-what-is-an-opcode/#respond</comments>
		
		<dc:creator><![CDATA[Michael]]></dc:creator>
		<pubDate>Thu, 31 Jul 2025 21:15:47 +0000</pubDate>
				<category><![CDATA[Knowledge]]></category>
		<category><![CDATA[Opcodes]]></category>
		<guid isPermaLink="false">https://www.sim6502.de/?p=503</guid>

					<description><![CDATA[If you&#8217;re new to programming the 6502 microprocessor—or revisiting it from the world of modern computing—one of the first terms you&#8217;ll encounter is the opcode. This blog post lays the foundation for the rest of the series by explaining what opcodes are, how they relate to the 6502&#8217;s architecture, and why understanding them is key.]]></description>
										<content:encoded><![CDATA[
<p></p>



<p>If you&#8217;re new to programming the 6502 microprocessor—or revisiting it from the world of modern computing—one of the first terms you&#8217;ll encounter is the <strong>opcode</strong>. This blog post lays the foundation for the rest of the series by explaining what opcodes are, how they relate to the 6502&#8217;s architecture, and why understanding them is key to mastering the platform.</p>



<h3 class="wp-block-heading">What Is an Opcode?</h3>



<p>An <strong>opcode</strong> (short for <em>operation code</em>) is a single byte of machine code that tells the processor what operation to perform. The 6502 has <strong>151 documented opcodes</strong>, each representing a specific instruction like loading a value into a register, adding two numbers, or jumping to another location in memory.</p>



<p>Example:</p>



<ul class="wp-block-list">
<li><code>$A9</code> is the opcode for <code>LDA</code> with <em>immediate addressing</em> (Load Accumulator with a constant value).</li>
</ul>



<p>Every instruction the CPU executes starts with an opcode. Some are followed by <strong>operands</strong> (data or addresses), depending on the instruction and the addressing mode.</p>



<h3 class="wp-block-heading">Opcodes and Addressing Modes</h3>



<p>The same instruction—like <code>LDA</code>—can have different opcodes depending on how it accesses memory. The 6502 supports a variety of addressing modes, including:</p>



<ul class="wp-block-list">
<li><strong>Immediate</strong>: <code>LDA #$10</code> → load the value <code>$10</code></li>



<li><strong>Zero Page</strong>: <code>LDA $00</code> → load from address <code>$0000</code></li>



<li><strong>Absolute</strong>: <code>LDA $1234</code> → load from full 16-bit address</li>



<li><strong>Indexed</strong>: <code>LDA $20,X</code> → add <code>X</code> to the base address</li>



<li><strong>Indirect</strong>: for instructions like <code>JMP</code></li>
</ul>



<p>Each variant has its own opcode byte. For example:</p>



<ul class="wp-block-list">
<li><code>LDA #$10</code> → <code>$A9 10</code></li>



<li><code>LDA $10</code> → <code>$A5 10</code></li>



<li><code>LDA $1234</code> → <code>$AD 34 12</code></li>
</ul>



<p>So when we say “the 6502 has 151 opcodes,” we’re counting <em>instruction + addressing mode combinations</em>, not just the instruction names.</p>



<h3 class="wp-block-heading">The Structure of a 6502 Instruction</h3>



<p>A typical instruction has this structure:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#39404f;color:#c8d0e0">C#</span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>&#91;Opcode&#93; &#91;Operand (0–2 bytes)&#93;</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #ECEFF4">&#91;</span><span style="color: #D8DEE9FF">Opcode</span><span style="color: #ECEFF4">&#93;</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&#91;</span><span style="color: #D8DEE9FF">Operand </span><span style="color: #ECEFF4">(</span><span style="color: #B48EAD">0</span><span style="color: #D8DEE9FF">–</span><span style="color: #B48EAD">2</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">bytes</span><span style="color: #ECEFF4">)&#93;</span></span></code></pre></div>



<p>Examples:</p>



<ul class="wp-block-list">
<li><code>CLC</code> (Clear Carry) → one byte: <code>$18</code></li>



<li><code>LDA #$42</code> → two bytes: <code>$A9 42</code></li>



<li><code>JMP $1234</code> → three bytes: <code>$4C 34 12</code></li>
</ul>



<p>The number of bytes used and the number of <strong>CPU cycles</strong> required to execute the instruction depends on the addressing mode.</p>



<h3 class="wp-block-heading">Why Learn Opcodes?</h3>



<p>Understanding opcodes gives you low-level control over the CPU. This knowledge allows you to:</p>



<ul class="wp-block-list">
<li>Optimize code for speed and size</li>



<li>Write routines for graphics, sound, and input</li>



<li>Build emulators, disassemblers, and debuggers</li>



<li>Understand what’s really going on beneath a BASIC program</li>
</ul>



<p>Even when using an assembler that lets you write <code>LDA #$10</code>, it&#8217;s the opcode <code>$A9</code> that gets assembled and stored in memory.</p>



<h3 class="wp-block-heading">What’s Next in the Series?</h3>



<p>This post is the beginning of a journey through <strong>every single 6502 opcode</strong>. In the upcoming entries, we’ll group instructions by functionality—such as loading data, arithmetic, branching, and stack operations—and break them down in detail.</p>



<p>Each post will include:</p>



<ul class="wp-block-list">
<li>Instruction description</li>



<li>Addressing modes</li>



<li>Effects on flags</li>



<li>CPU cycles</li>



<li>Practical examples</li>
</ul>



<p>Stay tuned, and let’s start unlocking the power of the 6502—one opcode at a time.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sim6502.de/2025/07/31/understanding-the-6502-what-is-an-opcode/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Understanding 6502 Memory Layout on the Commodore 64</title>
		<link>https://www.sim6502.de/2025/07/30/understanding-6502-memory-layout-on-the-commodore-64/</link>
					<comments>https://www.sim6502.de/2025/07/30/understanding-6502-memory-layout-on-the-commodore-64/#respond</comments>
		
		<dc:creator><![CDATA[Michael]]></dc:creator>
		<pubDate>Wed, 30 Jul 2025 16:22:00 +0000</pubDate>
				<category><![CDATA[Knowledge]]></category>
		<guid isPermaLink="false">https://www.sim6502.de/?p=500</guid>

					<description><![CDATA[When diving into programming for the Commodore 64, one of the most crucial aspects to grasp is how memory is organized and accessed by the 6502-based CPU. Unlike modern systems with gigabytes of RAM and virtual memory management, the C64 operates within a tight 64KB address space, and every byte counts. The 6502&#8217;s 16-bit Address.]]></description>
										<content:encoded><![CDATA[
<p>When diving into programming for the Commodore 64, one of the most crucial aspects to grasp is how memory is organized and accessed by the 6502-based CPU. Unlike modern systems with gigabytes of RAM and virtual memory management, the C64 operates within a tight 64KB address space, and every byte counts.</p>



<h3 class="wp-block-heading">The 6502&#8217;s 16-bit Address Space</h3>



<p>The 6502 microprocessor at the heart of the C64 has a 16-bit address bus, which allows it to address 2¹⁶ = 65,536 bytes (64KB) of memory. This entire memory space is shared between RAM, ROM, I/O registers, and special-purpose areas like zero page and stack.</p>



<h3 class="wp-block-heading">Key Memory Regions on the C64</h3>



<p>Here&#8217;s a breakdown of the most important memory regions:</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Address Range</th><th>Size</th><th>Purpose</th></tr></thead><tbody><tr><td><code>$0000-$00FF</code></td><td>256 B</td><td><strong>Zero Page</strong> &#8211; fast access area</td></tr><tr><td><code>$0100-$01FF</code></td><td>256 B</td><td><strong>Stack</strong></td></tr><tr><td><code>$0200-$03FF</code></td><td>512 B</td><td>Screen input buffer, scratch</td></tr><tr><td><code>$0400-$07E7</code></td><td>~2 KB</td><td><strong>Screen RAM</strong></td></tr><tr><td><code>$0800-$9FFF</code></td><td>~37 KB</td><td><strong>Main RAM</strong></td></tr><tr><td><code>$A000-$BFFF</code></td><td>8 KB</td><td><strong>BASIC ROM</strong> (can be banked out)</td></tr><tr><td><code>$C000-$CFFF</code></td><td>4 KB</td><td>More usable RAM</td></tr><tr><td><code>$D000-$DFFF</code></td><td>4 KB</td><td><strong>I/O Area &amp; Character ROM</strong></td></tr><tr><td><code>$E000-$FFFF</code></td><td>8 KB</td><td><strong>KERNAL ROM</strong></td></tr></tbody></table></figure>



<h3 class="wp-block-heading">Bank Switching and Memory Control</h3>



<p>The C64’s memory map is more flexible than it seems, thanks to <strong>bank switching</strong>. Certain regions (especially <code>$A000-$BFFF</code>, <code>$D000-$DFFF</code>, and <code>$E000-$FFFF</code>) can be switched between RAM and ROM (or I/O) by writing to the processor port at address <code>$0001</code>.</p>



<p>This allows, for example:</p>



<ul class="wp-block-list">
<li>Replacing BASIC ROM with RAM for custom code.</li>



<li>Temporarily disabling I/O to access the underlying RAM at <code>$D000-$DFFF</code>.</li>
</ul>



<h3 class="wp-block-heading">Zero Page and Stack</h3>



<p>The <strong>zero page</strong> is a special 256-byte memory region (<code>$0000-$00FF</code>) with optimized access: many 6502 instructions support special addressing modes for it, resulting in faster and smaller code.</p>



<p>The <strong>stack</strong> is fixed at page 1 (<code>$0100-$01FF</code>) and used for subroutine calls, interrupts, and manual pushing/popping of data.</p>



<h3 class="wp-block-heading">I/O and Peripherals</h3>



<p>Memory-mapped I/O registers reside at <code>$D000-$DFFF</code>, controlling:</p>



<ul class="wp-block-list">
<li><strong>VIC-II</strong> (graphics)</li>



<li><strong>SID</strong> (sound)</li>



<li><strong>CIA</strong> chips (timers, I/O ports)</li>



<li><strong>Color RAM</strong> (at <code>$D800-$DBFF</code>, though not part of main RAM)</li>
</ul>



<h3 class="wp-block-heading">Final Thoughts</h3>



<p>Understanding the C64’s memory layout is key to effective programming. While the 64KB may seem limited, careful planning and use of bank switching, zero page, and hardware registers allows for surprising complexity and performance.</p>



<p>Whether you&#8217;re writing assembly, optimizing BASIC routines, or building your own emulator, mastering this map is your first step toward truly understanding the C64’s architecture.</p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sim6502.de/2025/07/30/understanding-6502-memory-layout-on-the-commodore-64/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Hello 8bit world!</title>
		<link>https://www.sim6502.de/2025/07/28/hello-world-2/</link>
					<comments>https://www.sim6502.de/2025/07/28/hello-world-2/#respond</comments>
		
		<dc:creator><![CDATA[Michael]]></dc:creator>
		<pubDate>Mon, 28 Jul 2025 15:05:34 +0000</pubDate>
				<category><![CDATA[News]]></category>
		<guid isPermaLink="false">http://wptest.michael-bohn.net/?p=1</guid>

					<description><![CDATA[So there we are. My blog about my project, the 6502 simulator. And surely you're wondering why? Why another 6502 simulator?]]></description>
										<content:encoded><![CDATA[
<p>Welcome to my blog.</p>



<p>So there we are. My blog about my project, the 6502 simulator. And surely you&#8217;re wondering why? Why another 6502 simulator?<br>Just look at the </p>



<p>&#8216;<a href="/so-why" data-type="URL">So </a><a href="/index.php/so-why/" data-type="URL" data-id="/index.php/so-why/">Why&#8230;</a>&#8216; section <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>What am I going to write about here?<br>&#8211; about the progress of the project<br>&#8211; new versions and releases<br>&#8211; screenshots<br>and maybe more. Let&#8217;s see.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sim6502.de/2025/07/28/hello-world-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>The progress&#8230;</title>
		<link>https://www.sim6502.de/2025/07/28/the-progress/</link>
		
		<dc:creator><![CDATA[Michael]]></dc:creator>
		<pubDate>Mon, 28 Jul 2025 14:57:35 +0000</pubDate>
				<category><![CDATA[News]]></category>
		<guid isPermaLink="false">https://www.sim6502.de/?p=481</guid>

					<description><![CDATA[If you want to see what opcodes are implemented yet, milestones etc. …]]></description>
										<content:encoded><![CDATA[
<p>If you want to see what opcodes are implemented yet, milestones etc. &#8230;</p>



<p></p>



<p></p>



<p> <a href="https://www.sim6502.de/whats-done-so-far/" data-type="page" data-id="476">Go to this page</a></p>



<p></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Decoupling the CPU and Memory</title>
		<link>https://www.sim6502.de/2025/07/22/decoupling-the-cpu-and-memory/</link>
					<comments>https://www.sim6502.de/2025/07/22/decoupling-the-cpu-and-memory/#respond</comments>
		
		<dc:creator><![CDATA[Michael]]></dc:creator>
		<pubDate>Tue, 22 Jul 2025 16:00:00 +0000</pubDate>
				<category><![CDATA[Knowledge]]></category>
		<guid isPermaLink="false">https://www.sim6502.de/?p=357</guid>

					<description><![CDATA[When building a 6502 emulator, one of the most fundamental architectural decisions you’ll face is how to organize memory access. It’s tempting to embed the memory array directly into the CPU class – especially for early prototypes – but as your emulator grows, this tight coupling becomes a liability. A better approach is to implement.]]></description>
										<content:encoded><![CDATA[
<p>When building a 6502 emulator, one of the most fundamental architectural decisions you’ll face is how to organize memory access. It’s tempting to embed the memory array directly into the CPU class – especially for early prototypes – but as your emulator grows, this tight coupling becomes a liability. A better approach is to implement a dedicated memory bus and pass it into the CPU. Here’s why, and how.</p>



<h3 class="wp-block-heading">Why Separate Memory from CPU Logic?</h3>



<p>At a hardware level, the 6502 CPU doesn&#8217;t <em>own</em> memory – it communicates with memory-mapped peripherals and RAM through the system bus. Mirroring that in your code makes your emulator more accurate, modular, and testable.</p>



<p>Benefits of separating the CPU from memory:</p>



<ul class="wp-block-list">
<li><strong>Cleaner abstraction:</strong> The CPU should only know how to read and write bytes, not <em>where</em> they come from.</li>



<li><strong>Peripheral integration:</strong> You can easily map memory addresses to I/O devices (e.g., screen, keyboard, VIA, etc.).</li>



<li><strong>Bank switching:</strong> Dynamic memory mapping becomes trivial with a proper bus.</li>



<li><strong>Testing:</strong> You can mock memory for unit testing the CPU without dragging the entire system along.</li>
</ul>



<h3 class="wp-block-heading">A Simple Bus Interface</h3>



<p>Start by defining a bus interface:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#39404f;color:#c8d0e0">C#</span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>public interface IMemoryBus
{
    byte Read(ushort address);
    void Write(ushort address, byte data);
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">public</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">interface</span><span style="color: #D8DEE9FF"> IMemoryBus</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">byte</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">Read</span><span style="color: #ECEFF4">(</span><span style="color: #81A1C1">ushort</span><span style="color: #D8DEE9FF"> address</span><span style="color: #ECEFF4">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">Write</span><span style="color: #ECEFF4">(</span><span style="color: #81A1C1">ushort</span><span style="color: #D8DEE9FF"> address</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">byte</span><span style="color: #D8DEE9FF"> data</span><span style="color: #ECEFF4">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p>CPU only interacts with this interface:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#39404f;color:#c8d0e0">C#</span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>public class CPU
{
    private readonly IMemoryBus _bus;

    public CPU(IMemoryBus bus)
    {
        _bus = bus;
    }

    public void ExecuteNextInstruction()
    {
        ushort pc = /* fetch program counter */;
        byte opcode = _bus.Read(pc);
        // decode &amp; execute
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">public</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">class</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">CPU</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">private</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">readonly</span><span style="color: #D8DEE9FF"> IMemoryBus _bus</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">public</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">CPU</span><span style="color: #ECEFF4">(</span><span style="color: #D8DEE9FF">IMemoryBus bus</span><span style="color: #ECEFF4">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #D8DEE9">_bus</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">bus</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">public</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">ExecuteNextInstruction</span><span style="color: #ECEFF4">()</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">ushort</span><span style="color: #D8DEE9FF"> pc </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #616E88">/* fetch program counter */</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">byte</span><span style="color: #D8DEE9FF"> opcode </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">_bus</span><span style="color: #ECEFF4">.</span><span style="color: #88C0D0">Read</span><span style="color: #ECEFF4">(</span><span style="color: #D8DEE9">pc</span><span style="color: #ECEFF4">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">        </span><span style="color: #616E88">// decode &amp; execute</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p>Modeling your emulator with a decoupled bus architecture brings your code closer to real hardware behavior. It simplifies future expansion and makes the CPU a truly generic component – unaware of the memory layout or even what devices are attached. <br>If you&#8217;re serious about maintainability and hardware accuracy, decoupling CPU and memory isn&#8217;t just good style – it&#8217;s essential.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sim6502.de/2025/07/22/decoupling-the-cpu-and-memory/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Opcode Attribute</title>
		<link>https://www.sim6502.de/2025/07/11/opcode-attribute/</link>
					<comments>https://www.sim6502.de/2025/07/11/opcode-attribute/#respond</comments>
		
		<dc:creator><![CDATA[Michael]]></dc:creator>
		<pubDate>Fri, 11 Jul 2025 14:52:25 +0000</pubDate>
				<category><![CDATA[News]]></category>
		<guid isPermaLink="false">https://www.sim6502.de/?p=309</guid>

					<description><![CDATA[Regarding observing the cycles, I changed my mind. At first, I didn’t attach any importance to it. After some more reading and getting a better understanding of the importance the cycles have, even in a simulation, I will will do some changes to the code.To do this in a way that keeps the code flexible.]]></description>
										<content:encoded><![CDATA[
<p>Regarding observing the cycles, I changed my mind. At first, I didn’t attach any importance to it. After some more reading and getting a better understanding of the importance the cycles have, even in a simulation, I will will do some changes to the code.<br>To do this in a way that keeps the code flexible and maintable, I want to introduce an attribute for the opcode-methods. It will handle the cycles this opcode takes, the adressing mode and probably some more parameters. We&#8217;ll see.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p></p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers cbp-highlight-hover" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(201, 218, 248, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#39404f;color:#c8d0e0">C#</span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>&#91;AttributeUsage(AttributeTargets.Method)&#93;
class OpcodeAttribute : Attribute {
    public int Cycles { get; }
    public string AddressingMode { get; }

    public OpcodeAttribute(int cycles, string mode) {
        Cycles = cycles;
        AddressingMode = mode;
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #ECEFF4">&#91;</span><span style="color: #D8DEE9FF">AttributeUsage</span><span style="color: #ECEFF4">(</span><span style="color: #D8DEE9">AttributeTargets</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9">Method</span><span style="color: #ECEFF4">)&#93;</span></span>
<span class="line"><span style="color: #81A1C1">class</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">OpcodeAttribute</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">:</span><span style="color: #D8DEE9FF"> Attribute </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">public</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">int</span><span style="color: #D8DEE9FF"> Cycles </span><span style="color: #ECEFF4">{</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">get;</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">public</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">string</span><span style="color: #D8DEE9FF"> AddressingMode </span><span style="color: #ECEFF4">{</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">get;</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">public</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">OpcodeAttribute</span><span style="color: #ECEFF4">(</span><span style="color: #81A1C1">int</span><span style="color: #D8DEE9FF"> cycles</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">string</span><span style="color: #D8DEE9FF"> mode</span><span style="color: #ECEFF4">)</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #D8DEE9">Cycles</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">cycles</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #D8DEE9">AddressingMode</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">mode</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<hr class="wp-block-separator aligncenter has-alpha-channel-opacity"/>
</blockquote>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers cbp-highlight-hover" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(201, 218, 248, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#39404f;color:#c8d0e0">C#</span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>&#91;Opcode(2, "Immediate")&#93;
public void Cmd_A9() {
    // LDA #immediate
    A = FetchByte();
    SetFlags(A);
}


MethodInfo method = this.GetType().GetMethod("Cmd_" + opcode.ToString("X2"));
if (method != null) {
    var attr = method.GetCustomAttribute&lt;OpcodeAttribute>();
    method.Invoke(this, null);
    if (attr != null) {
        CycleCount += attr.Cycles;
        # use attr.AddressingMode to chose the mode for this opcode
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #ECEFF4">&#91;</span><span style="color: #D8DEE9FF">Opcode</span><span style="color: #ECEFF4">(</span><span style="color: #B48EAD">2</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&quot;</span><span style="color: #A3BE8C">Immediate</span><span style="color: #ECEFF4">&quot;</span><span style="color: #ECEFF4">)&#93;</span></span>
<span class="line"><span style="color: #81A1C1">public</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">Cmd_A9</span><span style="color: #ECEFF4">()</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #ECEFF4">    </span><span style="color: #616E88">// LDA #immediate</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">A</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">FetchByte</span><span style="color: #ECEFF4">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #88C0D0">SetFlags</span><span style="color: #ECEFF4">(</span><span style="color: #D8DEE9">A</span><span style="color: #ECEFF4">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">MethodInfo method </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">this</span><span style="color: #ECEFF4">.</span><span style="color: #88C0D0">GetType</span><span style="color: #ECEFF4">().</span><span style="color: #88C0D0">GetMethod</span><span style="color: #ECEFF4">(</span><span style="color: #ECEFF4">&quot;</span><span style="color: #A3BE8C">Cmd_</span><span style="color: #ECEFF4">&quot;</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">+</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">opcode</span><span style="color: #ECEFF4">.</span><span style="color: #88C0D0">ToString</span><span style="color: #ECEFF4">(</span><span style="color: #ECEFF4">&quot;</span><span style="color: #A3BE8C">X2</span><span style="color: #ECEFF4">&quot;</span><span style="color: #ECEFF4">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">(</span><span style="color: #D8DEE9">method</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">!=</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">null</span><span style="color: #ECEFF4">)</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">var</span><span style="color: #D8DEE9FF"> attr </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">method</span><span style="color: #ECEFF4">.</span><span style="color: #88C0D0">GetCustomAttribute</span><span style="color: #ECEFF4">&lt;</span><span style="color: #D8DEE9FF">OpcodeAttribute</span><span style="color: #ECEFF4">&gt;()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">method</span><span style="color: #ECEFF4">.</span><span style="color: #88C0D0">Invoke</span><span style="color: #ECEFF4">(</span><span style="color: #81A1C1">this</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">null</span><span style="color: #ECEFF4">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">(</span><span style="color: #D8DEE9">attr</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">!=</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">null</span><span style="color: #ECEFF4">)</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #D8DEE9">CycleCount</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">+=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">attr</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9">Cycles</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #5E81AC">        </span><span style="color: #ECEFF4">#</span><span style="color: #5E81AC"> use attr.AddressingMode to chose the mode for this opcode</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p></p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sim6502.de/2025/07/11/opcode-attribute/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
