Retrochallenge: The First Piece of Code

Published Monday, January 14 2013

Alright, it’s not much to look at, but it’s a start.

This code generates ten seconds of 770Hz square wave, which is the tape header. It is both less compact and less elegant than the code that Woz wrote for the Apple 1 and Apple II, but for that I blame my inexperience (and the fact that Woz was a kind of 6502 savant who had no need for such niceties as assemblers). I hope that it is at least fairly easy to read and understand.

Next up is writing a sync bit, followed by shifting in data from memory and writing it to tape one bit at a time.

.alias IOA    $8001             ; 6522 ORA register
.alias DDRA   $8003             ; 6522 DDRA register

.alias TAPEST $40               ; Current tape state scratch area

.org $0300

INIT:   LDA     #$FF            ; Make all Port-A IO lines outputs
        STA     DDRA
        LDA     #$00            ; Initialize tape state scratch space
        STA     TAPEST

; Write 10 seconds of 770 Hz square wave to tape.
HEADR:  LDX     #$3C            ; 60 times thru inner loop
HEADR0: LDY     #$FF            ;   (60 * 255 = 15,300 half cycles)
HEADR1: LDA     #$7A            ; (122 * 5 uS) + overhead = 650 uS width
        JSR     DELAY
        JSR     WRTAPE
        BNE     HEADR1          ; Inner Loop
        BNE     HEADR0          ; Outer Loop

        ;; ----- END -------
        ;;  TODO: Write sync bit, then shift in data and write bit-by-bit.
        ;; ----- END -------

; Delay for 'A' cycles ('A' * 5 clocks). Modifies the Accumulator.
DELAY:  SBC     #$01
        BNE     DELAY

; Toggle the current tape output state. Modifies the Accumulator.
WRTAPE: LDA     TAPEST          ; Load current tape state
        EOR     #$01            ; Toggle it
        STA     TAPEST          ; Store back into tape state,
        STA     IOA             ;    and out to tape.