Skip to content

Commit

Permalink
Merge pull request #33 from sijnstra/main
Browse files Browse the repository at this point in the history
Add decimal output and usage to ls, optimize 32 bit decimal output
  • Loading branch information
Zeal8bit authored Jul 21, 2024
2 parents de57b10 + 6605560 commit 64159f8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 65 deletions.
44 changes: 21 additions & 23 deletions romdisk/ls.asm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; SPDX-FileCopyrightText: 2023 Zeal 8-bit Computer <contact@zeal8bit.com>
; SPDX-FileCopyrightText: 2023-2024 Zeal 8-bit Computer <contact@zeal8bit.com>; Shawn Sijnstra <shawn@sijnstra.com>;
;
; SPDX-License-Identifier: Apache-2.0

Expand All @@ -20,6 +20,7 @@
EXTERN date_to_ascii
EXTERN byte_to_ascii
EXTERN dword_to_ascii
EXTERN dword_to_ascii_dec

; A static buffer that can be used across the commands implementation
EXTERN init_static_buffer
Expand All @@ -43,13 +44,13 @@ ls_main:
; Check that argc is 1 or 2 (command itself is part of argc)
or c
cp 3
jp nc, _ls_too_many_param
jp nc, _ls_usage
dec a ; No parameters is okay
jp z, _ls_no_option
; We have one parameter
ld de, valid_params
call get_options
ERR_CHECK(_ls_invalid_param)
ERR_CHECK(_ls_usage)
; Here C contains the bitmap of given options
; Save it to a RMA location to retrieve it later
ld a, c
Expand Down Expand Up @@ -166,10 +167,17 @@ _ls_detailed:
inc hl
ex de, hl
; FIXME: Print decimal value?
ld a,(given_params)
bit 2,a
jr nz,_ls_use_hex
call dword_to_ascii_dec
jp _ls_decimal_done
_ls_use_hex:
ld a, '$' ; Hex value only at the moment
ld (de), a
inc de
call dword_to_ascii
_ls_decimal_done:
; Finally, format the date to ascii
ld (de), ' '
inc de
Expand Down Expand Up @@ -197,10 +205,10 @@ newline_ret:
; Close the opened directory
; H already contains the opendir entry
CLOSE()
; If any parameter was given, we have already printed a new line,
; If any parameter except h on its own was given, we have already printed a new line,
; no need to add one.
ld a, (given_params)
or a
and 3
; Set A to 0 (success) without modifying the flags
ret nz
S_WRITE3(DEV_STDOUT, newline, 1)
Expand Down Expand Up @@ -252,23 +260,15 @@ _ls_stat_error:
str_stat: DEFM "stat error: "
str_stat_end:

_ls_invalid_param:
ld de, str_invalid
ld bc, str_invalid_end - str_invalid
call error_print
_ls_usage:
S_WRITE3(DEV_STDOUT, str_usage, str_usage_end - str_usage)
ld a, 1
ret
str_invalid: DEFM "invalid parameter: "
str_invalid_end:

_ls_too_many_param:
ld de, str_params
ld bc, str_params_end - str_params
call error_print
ld a, 1
ret
str_params: DEFM "too many parameters: "
str_params_end:
str_usage: DEFM "usage: ls <-options>\n"
DEFM " l - list details\n"
DEFM " 1 - 1 entry per line\n"
DEFM " x - hex output\n"
str_usage_end:

PUBLIC open_error
open_error:
Expand All @@ -294,9 +294,7 @@ str_rddir_err_end:
SECTION DATA
cur_path: DEFM ".", 0 ; This is a string, it needs to be NULL-terminated
newline: DEFM "\n" ; This isn't a proper string, it'll be used with WRITE
valid_params: DEFM "l1", 0

SECTION BSS
; Given it one more byte to add a '\n' or '\0'
dir_entry_struct: DEFS ZOS_DIR_ENTRY_SIZE + 1
valid_params: DEFM "l1x", 0
given_params: DEFS 1
69 changes: 27 additions & 42 deletions romdisk/strutils.asm
Original file line number Diff line number Diff line change
Expand Up @@ -375,32 +375,20 @@ _parse_upper_hex_digit:
PUBLIC dword_to_ascii_dec
dword_to_ascii_dec:
push bc ;preserve bc
push de ;destination address
push hl ;put the source address on the stack
ld b,10 ;10 digits maximum
ld h,d ;de has outbuf
ld l,e
ld bc,2560 + 32 ;b=10 digits to clear, c=4*8 for the loop
ld hl,9
add hl,de
ld d,h ;de now has outbuf + 9
ld e,l
xor a
_pde_u_zerobuf:
ld (hl),0 ;zero out the output
inc hl
ld (hl),a ;zero out the output
dec hl
djnz _pde_u_zerobuf

ld b,4 * 8 ; number of loops through = number of bytes * 8

_bcd_Convert:
ld c,10 ;num output digits
ld h,d
ld l,e

_bcd_Add3:
ld a,(hl)
cp 5 ;only add 3 if the digit is 5 or above
jr c,_bcd_no_carry
add a,3
ld (hl),a
_bcd_no_carry:
inc hl
dec c
jr nz,_bcd_Add3 ;

pop hl ;hl has source address again
push hl
Expand All @@ -412,31 +400,29 @@ _bcd_no_carry:
inc hl
rl (hl)

ld c,10 ;num output digits
ld hl,9 ;HL = string destination + 10
push af ;preserve carry
add hl,de ;so we can go backwards through the bitshift
pop af
_bcd_BitShift:
ld b,10 ;num output digits
ld h,d
ld l,e

_bcd_Add6:
ld a,(hl)
rla
bit 4,a
jr z,_bcd_BitShift_clear
and 0x0F ;retain these bits
scf ;roll carry into next digit
_bcd_BitShift_clear:
adc a
daa ;built-in add 6 routine
cp 0x10 ;Check for half-carry
ccf ;make carry available for next byte
res 4,a ;clear bit without changing flags
ld (hl),a
dec hl
djnz _bcd_Add6 ;it's add 6 instead of 3 because it's done after the shift
dec c
jr nz,_bcd_BitShift
djnz _bcd_Convert
jr nz, _bcd_Convert

ld h,d ;hl has the start of the buffer again
ld l,e
ld b,10-1
pop de ;de now has the source address
pop hl ;hl now has the string address
ld bc,0x930 ;b = 9, c = 0x30
xor a
_pde_u_make_ascii:
ld a,(hl)
or a
or (hl)
jr nz,_pde_u_make_ascii2
ld (hl),' '
inc hl
Expand All @@ -445,13 +431,12 @@ _pde_u_make_ascii2:
inc b
_pde_u_make_ascii3:
ld a,(hl)
or 0x30 ;turn into ascii
or c ;turn into ascii
ld (hl),a
inc hl
djnz _pde_u_make_ascii3
;hl is now just after the end of the buffer string
ex de,hl
pop hl
inc hl ;add 4 for consistency with hex version
inc hl
inc hl
Expand Down

0 comments on commit 64159f8

Please sign in to comment.