+ SECTION "wee",HOME[$0]
+ ld hl, $D000
+ ld sp, hl
+
+ xor a
+ ld [$FF60], a
+ ld [$FF61], a
+ ld [$FF62], a
+
+ ld a, $50 ; Clear SR
+ ld [$FF65], a
+
+ call unlock+$D800
+ xor a
+ ld d, a
+ ld e, a
+ call erase+$D800
+
+ xor a
+ ld d, a
+ ld e, a
+
+loop: ld a, $04
+ cp d
+ jr z,done
+
+ xor a
+ ld c, a ; Count
+
+ifl: call program+$D800
+ xor a
+ cp c
+ jr nz, ifl
+ifldone:
+
+ ld a, $70
+ ld [$FF65], a
+ ld a, [$FF65] ; Load from the status register
+ call puthex+$D800
+
+; ld a, $2E ;.
+; call putc+$D800
+
+ inc de
+ jr loop
+done:
+ rst $00
+
+putc:
+ push af
+.wport:
+ ld a,[$FF53]
+ and $01
+ jr nz,.wport
+ pop af
+ ld [$FF52],a
+ ret
+
+puthex:
+ push hl
+ ld hl, hexdig+$D800
+ push hl
+ push af
+ swap a
+ and $0F
+ add l
+ ld l, a
+ ld a, [hl]
+ call putc+$D800
+ pop af
+ and $0F
+ pop hl
+ add l
+ ld l, a
+ ld a, [hl]
+ pop hl
+ jr putc
+
+hexdig:
+ db "0123456789ABCDEF"
+
+unlock:
+ ld a, $55
+ call putc+$D800
+ ld a, $60 ; Unlock the device
+ ld [$FF65], a
+ ld a, $D0
+ ld [$FF65], a
+ call waitrdy+$D800
+ ld a, [$FF65]
+ call puthex+$D800
+ ret
+
+erase:
+ ld a, $45
+ call putc+$D800
+ ld a, d
+ ld [$FF60], a
+ ld a, e
+ ld [$FF61], a
+ xor a
+ ld [$FF62], a
+ ld a, $20 ; Unlock the device
+ ld [$FF65], a
+ ld a, $D0
+ ld [$FF65], a
+ call waitrdy+$D800
+ ld a, [$FF65]
+ call puthex+$D800
+ ret
+
+; need to save d, e, and c
+program:
+ ld a, d
+ ld [$FF60], a
+ ld a, e
+ ld [$FF61], a
+ ld a, c
+ ld [$FF62], a
+ ld a, $40
+ ld [$FF65], a
+ ld a, d
+ ld [$FF60], a
+ ld a, e
+ ld [$FF61], a
+ ld a, c
+ ld [$FF62], a
+ ld a, [$FF63]
+ ld b, a
+ ld a, d
+ ld [$FF60], a
+ ld a, e
+ ld [$FF61], a
+ ld a, c
+ ld [$FF62], a
+ ld a, b
+ ld [$FF65], a
+ call waitrdy+$D800
+ inc c
+ ret
+
+waitrdy:
+ ld a, $70 ; Wait status register.
+ ld [$FF65], a
+.wrl: ld a, [$FF65] ; Wait for bit 7 to come back on again.
+ bit 7,a
+ jr z, .wrl
+ ret
+
+ section "a", HOME[$FF]
+ nop