So, yeah, spent a few days (at night, as this is not for my day job) looking for a bug in this code...yep, the bug? Needed to place the return command "ret" after white space, ie. not at the start of a line. Side-effect? It doesn't return, just keeps running the next instruction in memory after ret.
DisplaySelectList:
cls()
;========================================
;
; Input:
; AppSelectListControl.TopPosition
; AppSelectListControl.ListItemsIndirect
; AppSelectListControl.LineStartAddys
;
;
; Start reading the LineStartAddys
; at the TopPosition position
;
; Set the TextInverse if the value in
; ListItemsIndirect at position is equal to
; SelectedItem value.
;
; Determine if TopPosition ptr is the first
; line of a list item, if so, then print
; that number.
;
; Print starting at the LineStartAddys[x]
; until NULL reached.
ld a, (AppSelectListControl.TopPosition)
ld (AppSelectListControl.PositionCount), a ; save to variable
;.breakpoint check_a_for_top_position
nop
LoopPositions:
;=======================================
; Check that PositionCount - TopPosition
; is not equal to 9, else stop printing
;=======================================
ld a, (AppSelectListControl.TopPosition)
ld b, a
ld a, (AppSelectListControl.PositionCount)
sub b
cp 9
.breakpoint check_z_for_reached_9_a_vs_b
;==============Base case displayed == 9 ? then stop
jp z, donePrintingSelectList
;=============================================
; Get the addy of the string and save to stack
;
;
;=============================================
ld de, AppSelectListControl.LineStartAddys ;calculate offset of array start
.breakpoint de_has_LineStartAddy
;calculate offset using TopPosition
xor a
ld h, a
ld a, (AppSelectListControl.PositionCount)
ld l, a
add hl, hl ;double because array is of 2byte items
add hl, de ; offset calculated
;.breakpoint check_hl_for_addy_of_LineStartAddys_of_index
;==============
;-->ld (hl), hl
;==============
ld c, (hl)
inc hl
ld b, (hl)
push bc ; Save the addy to list item's string
; to be used by print string.
;==========================
; Get next position's addy,
; could be 0000
;
;==========================
inc hl
ld a, (hl)
ld (AppSelectListControl.NextAddy), a
inc hl
ld a, (hl)
ld (AppSelectListControl.NextAddy+1), a
;reset hl back to ptr to addy of position
dec hl
dec hl
dec hl
.breakpoint just_pushed_bc_as_addy_string
;;===========================
;; Read value of addy into hl
;;===========================
;ld a, (de)
;ld l, a
;inc de
;ld a, (de)
;ld h, a
;;.breakpoint check_hl_has_value_of_listItem_string
;================================================
; if addy is null, then stop printing select list
;================================================
ld a, b
or a
;.breakpoint check_z_for_NULL_listitem_addy1
jp nz, {+} ; don't quit
ld a, c
or a
;.breakpoint check_z_for_NULL_listitem_addy2
jp nz, {+} ; don't quit
pop bc
;.breakpoint check_z_for_1
jp donePrintingSelectList
+:
;=====>base case: TopPosition == 0, consider this
; line the start so skip to that section
ld a, (AppSelectListControl.PositionCount)
;.breakpoint a_has_position_count
ld b, a
xor a
or b
jp nz, NextPosition
xor a
push af ; push 0 as list item number
jp printFormattedNumber
; b has current Position (initially was TopPosition)
NextPosition:
;check the value at PositionCounter - 1 in ListItemsIndirect
dec b
;.breakpoint check_b_for_positionCounter_minus_1
ld de, AppSelectListControl.ListItemsIndirect
;calc offset using TopPosition
xor a
ld h, a
ld l, b ; b has TopPosition - 1
add hl, de
ex de, hl
;.breakpoint check_de_for_addy_of_position_minus_1
; de has the address of the TopPosition - 1 index of
; the ListItemsIndirect array
ld a, (de) ; ld last position's list item number
;.breakpoint check_a_for_lastPositions_listItem_number
push af ; save last list item number
inc de
ld a, (de) ; de is at Position now
ld (AppSelectListControl.ListItemPtr), a ; save for later checking highlight status
;.breakpoint check_listItemPtr_and_de
ld b, a ; ld Position's list item number
;.breakpoint check_b_for_listItem_number
pop af ; get last position's list item number
cp b ; are they the same?
;.breakpoint check_z_for_are_they_same
jp z, printString ; jump past code for making formatted number
push bc
printFormattedNumber:
;===============================================
; Print a formatted number based on list item on
; the stack. (upper-byte)
;===============================================
pop af ; get list item number
.breakpoint check_a_for_listCount1
inc a
;.breakpoint check_a_for_listCount2
;.breakpoint a_has_listItem_number
call Convert.AToDecimalText ; hl will point to decimal text w/ null
;.breakpoint check_hl_addy_has_formatted_number
;======================
; Print each character
;======================
res textInverse, (iy + textFlags) ; do not print highlight
-:
ld a, (hl)
; Check for NULL
xor b
cp b
jp z, {+}
ld a, (hl)
bcall(_VPutMap)
inc hl
jp {-}
+:
;.breakpoint printed_formatted_number
ld a, '.'
bcall(_VPutMap)
ld a, ' '
bcall(_VPutMap)
ld a, ' '
bcall(_VPutMap)
ld a, ' '
bcall(_VPutMap)
printString:
;==============================
; set the text inverse
; if listCount == selectedItem
;==============================
ld a, (AppSelectListControl.ListItemPtr)
ld b, a
ld a, (AppSelectListControl.SelectedItem)
;.breakpoint check_a_for_listcount_and_b_for_selectedItem
cp b
jp z, SetHighlight
res textInverse, (iy + textFlags)
jp doneHighlight
setHighlight:
nop
;.breakpoint Setting_highlight
set textInverse, (iy + textFlags)
doneHighlight:
;=================================
; Print the string starting at the
; addy found on the stack
;=================================
pop hl
;.breakpoint printString_start_hl_has_addy
nop
;========================================
;Get the next line break to know when to
; stop printing the string.
;========================================
-:
ld a, (hl)
xor b
cp b
jp z, {+} ; quit if null reached
;========
; quit if hl matches addy of next line break
ld a, (AppSelectListControl.NextAddy)
ld e, a
ld a, (AppSelectListControl.NextAddy+1)
ld d, a
;.breakpoint check_de_for_next_addy
ld a, l
cp e
jp nz, {++}
ld a, h
cp d
jp nz, {++}
jp {+}
++:
ld a, (hl)
;.breakpoint check_hl_as_chr_ptr
bcall(_VPutMap)
inc hl
jp {-}
+:
;.breakpoint done_printing_this_line
;==================
; NewLine operation
;==================
ld a, (penrow)
ld b, 7
add a, b
ld (penrow), a
xor a
ld (pencol), a
;========================
; Increment PositionCount
; then check if addy
;========================
inc_byte(AppSelectListControl.PositionCount)
;.breakpoint moved_to_next_position
jp LoopPositions
donePrintingSelectList:
.breakpoint done_printing_select_list
nop
ret < ------- NEEDS SPACE IN-FRONT OF IT TO WORK...WTF
|