# Skill: 6510 Assembly for the Commodore 64 ## Use this skill when The task involves 6510/6502 machine language, addressing modes, registers, stack, flags, subroutines, speed optimization, or assembly source. ## CPU model The Commodore 64 uses the 6510, which is software-compatible with the 6502 instruction set and adds an on-chip I/O port at `$0000-$0001` used heavily for memory banking and cassette control. ## Registers | Register | Purpose | |---|---| | A | Accumulator; arithmetic, logic, data movement | | X | Index register; loops, offsets, counters | | Y | Index register; loops, offsets, KERNAL/BASIC calling conventions | | SP | Stack pointer, stack page `$0100-$01FF` | | PC | Program counter | | P | Processor status flags: N V - B D I Z C | ## Addressing modes to prefer - Immediate: `lda #$01` - Zero page: `lda $fb` faster/smaller than absolute. - Zero page indexed: `lda $fb,x` - Absolute: `lda $c000` - Absolute indexed: `lda $0400,x` - Indirect indexed: `lda ($fb),y` for pointer walking. - Absolute indirect only works with `JMP (addr)`. ## Subroutine discipline Called from BASIC with `SYS`: ```asm * = $c000 start: ; work here rts ``` If using registers for temporary work but caller expects preservation: ```asm pha txa: pha tya: pha ; work pla: tay pla: tax pla rts ``` ## Flags - `C` carry: arithmetic carry/borrow; set before `SBC`, clear before `ADC` unless chaining multi-byte arithmetic. - `Z` zero: many KERNAL calls and comparisons rely on it. - `N` negative: mirrors bit 7 of result. - `I` interrupt disable: `SEI` disables maskable IRQ; `CLI` enables. - `D` decimal mode: normally keep clear on C64 unless using BCD intentionally. ## Multi-byte addition pattern ```asm clc lda lo1 adc lo2 sta resultlo lda hi1 adc hi2 sta resulthi ``` ## Pointer walking pattern ```asm ptr = $fb lda #source sta ptr+1 ldy #0 loop: lda (ptr),y beq done jsr $ffd2 iny bne loop inc ptr+1 bne loop done: rts ``` ## Avoid unless requested - Undocumented opcodes. - Self-modifying code without comments. - Cycle-exact code without target PAL/NTSC note. - Overwriting zero-page locations not reserved for your routine. ## Agent checklist For assembly answers: 1. Identify assembler syntax. 2. Give load address. 3. Show how to run it. 4. Name KERNAL calls used. 5. Declare clobbered registers. 6. Explain zero-page usage.