.global scan_cards .global find_first_card .global get_all_cards scan_cards: ; Read the types of all the cards in the system ; The values in HL, BC, and A are destroyed ld hl, card_types+255 ld bc, 0xFFFF ; Start at card 255 scan_loop: ; Read the types of card 255 through to card 1 in a, (c) ld (hl), a dec hl djnz scan_loop in a, (c) ; Read card 0's type ld (hl), a ret get_all_cards: ; Gets the indexes of the cards with the specified type ; IX is a pointer to the buffer to store the results in ; B is the length of the buffer (0 is a 256 byte length) ; C is the type of card to find ; The zero flag is set if the buffer is full ; The values in A, B, HL, and IX are destroyed ld hl, card_types+1 gac_loop: ld a, (hl) ; Get card type cp c ; Check whether the type of the current card is the type we want jp nz, gac_next ; Skip the card if it is not ld (ix), l inc ix dec b gac_next: inc hl ; Go to the next card ld a, l ; Check if we have gone through all the cards or a ; (or a == cp 0) jp Z, gac_done ; If so, return ld a, b ; Check if we have filled the buffer or a ; (or a == cp 0) jp Z, gac_full ; If so, return with the zero flag set jp gac_loop gac_done: or 1 ; Reset zero flag to provide correct "buffer filed" indication gac_full: ret find_first_card: ; Finds the first instance of a card type specified in B ; A is zero if no such card was found, and nonzero if a card was found ; If a card was found, the ID of the card is returned in L ; If a card was not found, 0 is returned in L ; The values in A and H are destroyed ld hl, card_types+1 ffc_loop: ld a, (hl) ; Get card type cp b ; Check whether the type of the current card is the type we want jp Z, ffc_done ; If so, return the type in L or a ; (or a == cp 0) Check whether the card type is 0 (nonexistent card) jp Z, ffc_notfound ; If so, return 0 in L inc hl ; Go to the next card jp ffc_loop ffc_notfound: ld l, 0 ffc_done: ret .bss .balign 256 card_types: .ds.b 256