Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## 6502 Emulation — Step by Step
- ### Initial State
- ```
- A=$00 X=$00 Y=$00 SP=$FD PC=$0600
- N=0 V=0 B=0 I=0 Z=0 C=0 → NV-BDIZC = 00000000
- ```
- ### Program Bytes (loaded at $0600)
- ```
- $0600: A9 03
- $0602: 18
- $0603: 69 05
- $0605: 8D 00 02
- $0608: 00
- ```
- ---
- ### Execution Trace
- **Instruction 1: Load immediate value**
- ```
- [$0600] A9 03 → LDA #$03
- Fetch opcode A9 at $0600 (immediate mode, 2 bytes)
- Fetch operand $03 at $0601
- Execute: A ← $03
- Flags: N=(bit7 of $03)=0, Z=($03==0)?0
- PC ← $0602
- ```
- > `[$0600] LDA #$03 → A=$03 X=$00 Y=$00 SP=$FD | NV-BDIZC=00000000 | PC=$0602`
- ---
- **Instruction 2: Clear carry flag**
- ```
- [$0602] 18 → CLC
- Fetch opcode 18 at $0602 (implied mode, 1 byte)
- Execute: C ← 0
- PC ← $0603
- ```
- > `[$0602] CLC → A=$03 X=$00 Y=$00 SP=$FD | NV-BDIZC=00000000 | PC=$0603`
- ---
- **Instruction 3: Add with carry**
- ```
- [$0603] 69 05 → ADC #$05
- Fetch opcode 69 at $0603 (immediate mode, 2 bytes)
- Fetch operand $05 at $0604
- Execute: A + operand + C = $03 + $05 + $00 = $08 ($08 fits in 8 bits, no carry out)
- C: result $08 ≤ $FF → C=0
- V: (A ^ result) & (operand ^ result) & $80 = ($03 ^ $08) & ($05 ^ $08) & $80 = $0B & $0D & $80 = $08 & $80 = $00 → V=0
- N: bit7 of $08 = 0
- Z: $08 != 0 → Z=0
- PC ← $0605
- ```
- > `[$0603] ADC #$05 → A=$08 X=$00 Y=$00 SP=$FD | NV-BDIZC=00000000 | PC=$0605`
- ---
- **Instruction 4: Store accumulator absolute**
- ```
- [$0605] 8D 00 02 → STA $0200
- Fetch opcode 8D at $0605 (absolute mode, 3 bytes)
- Fetch low addr: $00 at $0606
- Fetch high addr: $02 at $0607
- Address = $0200
- Execute: mem[$0200] ← A = $08
- PC ← $0608
- ```
- > `[$0605] STA $0200 → A=$08 X=$00 Y=$00 SP=$FD | NV-BDIZC=00000000 | PC=$0608`
- ---
- **Instruction 5: Halt**
- ```
- [$0608] 00 → BRK
- Halt execution.
- ```
- > `[$0608] BRK → A=$08 X=$00 Y=$00 SP=$FD | NV-BDIZC=00000000 | PC=$0608`
- ---
- ## Final Results
- ### Output Memory ($0200–$02FF)
- ```
- $0200: $08 (8)
- ```
- No other bytes written to output region.
- ### Final CPU State
- ```
- A=$08 X=$00 Y=$00 SP=$FD PC=$0608
- NV-BDIZC = 00000000
- ```
- ### Summary
- - **Instructions executed:** 5
- - **Errors:** None
- - **Result:** `3 + 5 = 8` → stored as `$08` at `$0200` ✓ (matches expected output)
Advertisement
Add Comment
Please, Sign In to add comment