Skip to content

Commit

Permalink
Fix buffer size 0 behavior in GetStringInput, more optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
calc84maniac committed Jan 30, 2024
1 parent 340bb4f commit 65d7def
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 58 deletions.
62 changes: 29 additions & 33 deletions src/ce/getstringinput.src
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,34 @@ _ClrTxtShd := $20818
public _os_GetStringInput
_os_GetStringInput:
push ix
ld ix,0
add ix,sp
ld iy,flags
ld a,(iy + $0d)
ld (.smc.text_flag_0),a
ld a,(iy + $4c)
ld (.smc.text_flag_1),a
ld l,(iy + $0d)
ld h,(iy + $4c)
push hl ; save text flags
set 1,(iy + $0d) ; use text buffer
res 5,(iy + $4c) ; use text buffer
call _ClrTxtShd
ld ix,0
add ix,sp
ld hl,(ix+6) ; hl -> input string prompt
add hl,de
xor a,a
sbc hl,de
ld iy,flags
call nz,_PutS
ld de,(curRow)
ld hl,(curRow)
.start:
push hl ; save initial row/column
ld hl,(ix+12) ; buffer size
add hl,de
xor a,a
sbc hl,de
ret z
push de ; save initial row/column
push ix
push hl ; save remaining size
jr z,.empty
ld de,(ix+9)
.loop_save:
push de ; save buf pointer
.loop:
ld iy,flags
call _CursorOn
.getkey:
call _GetKey
Expand All @@ -70,11 +67,11 @@ _os_GetStringInput:
pop af
jr c,.getkey
cp a,kQuit
jr z,.done_trampoline
jr z,.done
dec a ; kRight=1
cp a,kEnter-kRight ; kEnter=kDown+1
jr c,.loop
jr z,.done_trampoline
jr z,.done
inc a
cp a,kIns
jr z,.loop
Expand All @@ -92,27 +89,41 @@ _os_GetStringInput:
call _GetTokString
pop de ; restore buf pointer
ld b,(hl) ; token string length
inc hl
.draw_string:
ex (sp),hl ; remaining size
add hl,de
scf
sbc hl,de ; decrement and check if done
ex (sp),hl
jr z,.full
inc hl
ld a,(hl)
ld (de),a
inc hl
inc de
call _PutC
djnz .draw_string
jr .loop_save

.done:
pop hl ; restore buf pointer
ld (hl),0
.empty:
pop hl ; remaining size
pop hl ; saved IX
pop hl ; initial row/column
call _ClrTxtShd
call _CursorOff
pop hl ; restore text flags
ld (iy + $0d),l
ld (iy + $4c),h
pop ix
ret

.full:
push de ; save buf pointer
.full_loop:
call _GetKey
cp a,kEnter
.done_trampoline:
jr z,.done
cp a,kClear
jr nz,.full_loop
Expand All @@ -131,21 +142,6 @@ _os_GetStringInput:
sbc hl,bc ; decrement and compare to buf start
add hl,bc
jr nz,.clear_loop
ld (curRow),de
ex de,hl
ld (curRow),hl
jp .start
.done:
call _ClrTxtShd
call _CursorOff
.smc.text_flag_0 := $+1
ld a,0
ld (iy + $0d),a
.smc.text_flag_1 := $+1
ld a,0
ld (iy + $4c),a
pop hl ; restore buf pointer
ld (hl),0
pop hl ; remaining size
pop hl ; saved IX
pop hl ; initial row/column
pop ix
ret
41 changes: 16 additions & 25 deletions src/ce/gettokeninput.src
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,30 @@ _ClrTxtShd := $20818
_os_GetTokenInput:
push ix
ld iy,flags
ld a,(iy + $0d)
ld (.smc.text_flag_0),a
ld a,(iy + $4c)
ld (.smc.text_flag_1),a
ld l,(iy + $0d)
ld h,(iy + $4c)
push hl ; save text flags
set 1,(iy + $0d) ; use text buffer
res 5,(iy + $4c) ; use text buffer
call _ClrTxtShd
ld ix,0
add ix,sp
ld hl,(ix+6) ; hl -> input string prompt
ld hl,(ix+3+6) ; hl -> input string prompt
add hl,de
xor a,a
sbc hl,de
ld iy,flags
call nz,_PutS
ld hl,(curRow)
.start:
push hl ; save initial row/column
or a,a
sbc hl,hl
push hl ; save string length
ld hl,(ix+12) ; buffer size
ld hl,(ix+3+12) ; buffer size
push hl ; save remaining size
ld hl,(ix+9)
ld hl,(ix+3+9)
push hl ; save buf pointer
.loop:
ld iy,flags
push ix
call _CursorOn
.getkey:
Expand Down Expand Up @@ -92,33 +89,30 @@ _os_GetTokenInput:
pop ix
pop hl ; restore buf pointer
ex (sp),hl
ld bc,1
ld bc,$80
xor a,a
cp a,d ; check if 2-byte token
rl c ; sets carry
sbc hl,bc ; subtract 1 or 2 from remaining size
ex (sp),hl
jr c,.full
or a,d ; check if 2-byte token
jr z,.one_byte_token
ld (hl),d
inc hl
.one_byte_token:
add hl,bc ; add 0 or 1 to buf pointer
ld (hl),e
inc hl
push hl ; save buf pointer
push ix
call _GetTokString
pop ix
ld b,(hl) ; token string length
inc hl
.draw_string:
ld de,(ix-6)
.draw_string:
inc de ; increment string length
ld (ix-6),de
ld a,(hl)
inc hl
ld a,(hl)
call _PutC
djnz .draw_string
ld (ix-6),de
jr .loop
.full:
push hl ; save buf pointer
Expand Down Expand Up @@ -151,16 +145,13 @@ _os_GetTokenInput:
.done:
call _ClrTxtShd
call _CursorOff
.smc.text_flag_0 := $+1
ld a,0
ld (iy + $0d),a
.smc.text_flag_1 := $+1
ld a,0
ld (iy + $4c),a
pop hl ; restore buf pointer
ld de,(ix+9)
ld de,(ix+3+9)
or a,a
sbc hl,de
ld sp,ix
pop de ; restore text flags
ld (iy + $0d),e
ld (iy + $4c),d
pop ix
ret

0 comments on commit 65d7def

Please sign in to comment.