Guest User

task-1

a guest
May 25th, 2026
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | Software | 0 0
  1. ## 6502 Emulation — Step by Step
  2.  
  3. ### Initial State
  4. ```
  5. A=$00 X=$00 Y=$00 SP=$FD PC=$0600
  6. N=0 V=0 B=0 I=0 Z=0 C=0 → NV-BDIZC = 00000000
  7. ```
  8.  
  9. ### Program Bytes (loaded at $0600)
  10. ```
  11. $0600: A9 03
  12. $0602: 18
  13. $0603: 69 05
  14. $0605: 8D 00 02
  15. $0608: 00
  16. ```
  17.  
  18. ---
  19.  
  20. ### Execution Trace
  21.  
  22. **Instruction 1: Load immediate value**
  23. ```
  24. [$0600] A9 03 → LDA #$03
  25. Fetch opcode A9 at $0600 (immediate mode, 2 bytes)
  26. Fetch operand $03 at $0601
  27. Execute: A ← $03
  28. Flags: N=(bit7 of $03)=0, Z=($03==0)?0
  29. PC ← $0602
  30. ```
  31. > `[$0600] LDA #$03 → A=$03 X=$00 Y=$00 SP=$FD | NV-BDIZC=00000000 | PC=$0602`
  32.  
  33. ---
  34.  
  35. **Instruction 2: Clear carry flag**
  36. ```
  37. [$0602] 18 → CLC
  38. Fetch opcode 18 at $0602 (implied mode, 1 byte)
  39. Execute: C ← 0
  40. PC ← $0603
  41. ```
  42. > `[$0602] CLC → A=$03 X=$00 Y=$00 SP=$FD | NV-BDIZC=00000000 | PC=$0603`
  43.  
  44. ---
  45.  
  46. **Instruction 3: Add with carry**
  47. ```
  48. [$0603] 69 05 → ADC #$05
  49. Fetch opcode 69 at $0603 (immediate mode, 2 bytes)
  50. Fetch operand $05 at $0604
  51. Execute: A + operand + C = $03 + $05 + $00 = $08 ($08 fits in 8 bits, no carry out)
  52. C: result $08 ≤ $FF → C=0
  53. V: (A ^ result) & (operand ^ result) & $80 = ($03 ^ $08) & ($05 ^ $08) & $80 = $0B & $0D & $80 = $08 & $80 = $00 → V=0
  54. N: bit7 of $08 = 0
  55. Z: $08 != 0 → Z=0
  56. PC ← $0605
  57. ```
  58. > `[$0603] ADC #$05 → A=$08 X=$00 Y=$00 SP=$FD | NV-BDIZC=00000000 | PC=$0605`
  59.  
  60. ---
  61.  
  62. **Instruction 4: Store accumulator absolute**
  63. ```
  64. [$0605] 8D 00 02 → STA $0200
  65. Fetch opcode 8D at $0605 (absolute mode, 3 bytes)
  66. Fetch low addr: $00 at $0606
  67. Fetch high addr: $02 at $0607
  68. Address = $0200
  69. Execute: mem[$0200] ← A = $08
  70. PC ← $0608
  71. ```
  72. > `[$0605] STA $0200 → A=$08 X=$00 Y=$00 SP=$FD | NV-BDIZC=00000000 | PC=$0608`
  73.  
  74. ---
  75.  
  76. **Instruction 5: Halt**
  77. ```
  78. [$0608] 00 → BRK
  79. Halt execution.
  80. ```
  81. > `[$0608] BRK → A=$08 X=$00 Y=$00 SP=$FD | NV-BDIZC=00000000 | PC=$0608`
  82.  
  83. ---
  84.  
  85. ## Final Results
  86.  
  87. ### Output Memory ($0200–$02FF)
  88. ```
  89. $0200: $08 (8)
  90. ```
  91. No other bytes written to output region.
  92.  
  93. ### Final CPU State
  94. ```
  95. A=$08 X=$00 Y=$00 SP=$FD PC=$0608
  96. NV-BDIZC = 00000000
  97. ```
  98.  
  99. ### Summary
  100. - **Instructions executed:** 5
  101. - **Errors:** None
  102. - **Result:** `3 + 5 = 8` → stored as `$08` at `$0200` ✓ (matches expected output)
Advertisement
Add Comment
Please, Sign In to add comment