;; Note! This module must not be loaded into a secondary bank, it must execute from either home bank ;; or work RAM. Running it from $4000..$7FFF will make it fail, as bank remapping is ;; performed during verify, erase and write SECTION "flashwrite", HOME GLOBAL flash_erase_sector GLOBAL flash_verify_compatible ;; a = sector, hl => addr, a => bank sector_to_addr: ld h, a sla h sla h sla h sla h sla h ld l, 0 srl a srl a srl a srl a ret ;; This function verifies that we're talking to a SST39F040, other devices may not ;; be compatible. Sets carry flag if incompatible ;; b = bank to switch to on return flash_verify_compatible: push af ld a, 0 ld [$2000], a ld a, $aa ld [$9555], a ld a, $55 ld [$6aaa], a ld a, $90 ld [$6aaa], a ld a, [$4000] cp $bf jr nz,.error ld a, [$4000] cp $b7 jr nz,.error ld a, $f0 ld [$4000], a ld a, b ld [$2000], a pop af scf ccf ret .error: ld a, $f0 ld [$4000], a ld a, b ld [$2000], b pop af scf ret ;; a = bank to erase, b = bank to switch to on return flash_erase_sector: push bc push de push hl ld d, a ld a, $0 ld [$2000], a ld a, d call sector_to_addr ld e, a ;; e = bank, hl = sector address for bank ld a, $aa ld [$9555], a ld a, $55 ld [$6aaa], a ld a, $80 ld [$9555], a ld a, $aa ld [$9555], a ld a, $55 ld [$6aaa], a ld a, e ld [$2000], a ld a, $30 ld [hl], a .loop ld a, [hl] bit 7, a jr z, .loop ld a, b ld [$2000], a pop hl pop de pop bc ret