Thursday, October 14, 2021

Some 6502 disassembly for Apple II fans

This code is from an article in COMPUTE! magazine I was pointed to when I tried porting some BASIC raytracing code to the Apple II.  It's not quite what I needed for that, but it does support some things I added to the latest MC-10 ROM I've been working on.
It took a couple hours to reverse engineer it back to source code. I didn't look up the ROM calls or BASIC tokens, but for the most part I thought that was obvious.  Since I haven't used the 6502 in a while there may be a couple syntax issues, but it should be easy to fix if there are.

One possible change is the comment related to P.PWR.  That may be the current DATA pointer.

Apple enhancer file maker

10 FOR I = 768 TO 887: READ A : POKE I,A:CK = CK + A: NEXT
20 IF CK < > 14482 THEN PRINT"ERROR IN DATA STATEMENTS.": STOP
30 PRINT CHR$ (4)"BSAVE APPLE.ENHANCER,A$300,L$77"
40 DATA 169,76,141,245,3,169,16,141
50 DATA 246,3,169,3,141,247,3,96
60 DATA 160,0,177,184,217,115,3,240
70 DATA 11,200,192,3,240,3,76,20
80 DATA 3,32,201,222,140,118,3,230
90 DATA 184,208,2,230,185,32,103,221
100 DATA 172,118,3,192,1,240,10,192
110 DATA 2,240,35,32,82,231,76,65
120 DATA 217,169,3,32,214,211,165,185
130 DATA 72,165,184,72,165,118,72,165
140 DATA 117,72,169,176,72,32,82,231
150 DATA 32,65,217,76,210,215,32,82
160 DATA 231,32,26,214,56,165,155,233
170 DATA 1,164,156,176,1,136,133,125
180 DATA 132,126,96,171,176,174,0,0




;******************************************
;* Program:
;*  APPLE.ENHANCER
;*  COMPUTE! ISSUE 72 / MAY 1986 / PAGE 75
;******************************************
;* Author:
;*  Mark Russinovich
;*
;* Disassembly:
;*  James Diffendaffer
;******************************************
;* Expands Applesoft BASIC to use a variable containing a line number as a parameter for the following keywords:
;* & GOTO
;* & GOSUB
;* & RESTORE
;******************************************

    ; code start
    org    $0300
    
    ; change the BASIC RAM hook to JMP to our patch
    lda    #$4c        ; opcode for jmp
    sta    $3f5
    lda    #<L0310        ; LSB of the patch entry point
    sta    $03f6
    lda    #>L0310        ; MSB of the patch entry point
    sta    $03f7
    rts                ; return after patching BASIC

; entry point for expanded BASIC functions
L0310:
    ldy    #$00        ; clear Y for no offset from pointer
    lda    (TXTPTRL),y    ; get current character pointed to by BASIC parser

    ; is it one of the tokens?
L0314:
    cmp    L0373,y
    beq    L0324        ; branch if it is
    iny                ; next address
    cpy    #$03        ; only loop 2 times
    beq    L0321        ; branch to Syntax Error
    jmp    L0314        ; loop
    
;    bne    L0314        ; loop  (replaces previous 2 lines)

L0321:
    jsr    SYNERR        ; exit with Syntax Error?  $DEC9
    sty    L0376        ; save
    inc    <TXTPTR        ; increment LSB of current program execution pointer
    bne    L032D
    inc    >TXTPTR        ; increment MSB of current program execution pointer
L032D:
    jsr    FRMNUM        ; jump to format number $DD67
    ldy    L0376        ; load TOKEN from temp
    cpy    #$01        ; GOSUB?
    beq    L0341        ;
    cpy    #$02        ; RESTORE?
    beq L035e
    jsr    GETADR        ; get address from variable
    jmp    $d941        ; call to ROM GOTO code following line # parse ?

; & GOSUB variable
L0341:
    lda    #$03        ; how much space we need on the stack
    jsr CHKMEM        ; is there enough memory?
    
    ; Does CHKMEM give an error or did this code just ignore it?

    ; push info for BASIC's RETURN from GOSUB on to stack
    ;
    ; push the current program parse pointer onto the stack
    lda    >TXTPTR        ; MSB
    pha
    lda    <TXTPTR        ; LSB
    pha
    ; push the current line number onto the stack
    lda    >CURLIN
    pha
    lda    <CURLIN
    pha
    ; push type of stack frame onto stack
    lda    #$b0        ; GOSUB token
    pha

L0355:
    ; get address from variable
    jsr    GETADR
    jsr    $D941        ;??
    jmp    NEWSTT        ; jump to new start address GOTO GOSUB

; & RESTORE variable or #
L035e:
    jsr    GETADR        ; get address
    jsr    FNDLIN        ; find the line
    sec
    lda    LOWTR

    sbc    #$01        ; decrement LOWTR
    ldy    EXPSGN        ; put EXPSGN
    bcs    L036e        ;
    dey
; save address returned in A & Y
L036e:
    sta    P.PWR
    sty    P.PWR+1
    rts
L0373:
    dcb    $AB            ; Applesoft GOTO token
L0374:
    dcb    $B0            ; Applesoft GOSUB token
L0375:
    dcb    $AE            ; Applesoft RESTORE token
L0376:
    $00,$00            ; variables