1
0
mirror of https://github.com/hsoft/collapseos.git synced 2025-04-05 08:58:39 +11:00

Saved some bytes in parseHexPair and sdc

I replaced some doubled up nops with pushes and pops again, saving two bytes. There was also a nop in a loop that didn't look necessary, since the jump back to the top of the loop is already 13 cycles, so way more than 80 cycles are spent in that loop anyway.
I reworked things a little in parseHexPair and saved 5 bytes and 6 cycles, with more cycles saved in error cases.
This commit is contained in:
Clanmaster21 2019-10-17 20:20:13 +01:00 committed by GitHub
parent a2598a4498
commit c255903323
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 51 deletions

View File

@ -11,26 +11,16 @@
; On success, the carry flag is reset. On error, it is set.
parseHex:
; First, let's see if we have an easy 0-9 case
cp '0'
jr c, .error ; if < '0', we have a problem
cp '9'+1
jr nc, .alpha ; if >= '9'+1, we might have alpha
; We are in the 0-9 range
sub '0' ; C is clear
ret
add a, 0xc6 ; maps '0'-'9' onto 0xf6-0xff
sub 0xf6 ; maps to 0-9 and carries if not a digit
ret nc
.alpha:
call upcase
cp 'A'
jr c, .error ; if < 'A', we have a problem
cp 'F'+1
jr nc, .error ; if >= 'F', we have a problem
; We have alpha.
sub 'A'-10 ; C is clear
ret
.error:
scf
and 0xdf ; converts lowercase to uppercase
add a, 0xe9 ; map 0x11-x017 onto 0xFA - 0xFF
sub 0xfa ; map onto 0-6
ret c
add a, 10 ; C is clear, map back to 0xA-0xF
ret
; Parses 2 characters of the string pointed to by HL and returns the numerical
@ -39,34 +29,35 @@ parseHex:
; HL is set to point to the last char of the pair.
;
; On success, the carry flag is reset. On error, it is set.
parseHexPair:
push bc
ld a, (hl)
call parseHex
jr c, .end ; error? goto end, keeping the C flag on
rla \ rla \ rla \ rla ; let's push this in MSB
ld b, a
inc hl
ld a, (hl)
cp 0x21
jr c, .single ; special char? single digit
call parseHex
jr c, .end ; error?
or b ; join left-shifted + new. we're done!
; C flag was set on parseHex and is necessarily clear at this point
jr .end
.single:
; If we have a single digit, our result is already stored in B, but
; we have to right-shift it back.
ld a, b
and 0xf0
rra \ rra \ rra \ rra
dec hl
parseHexPair: ; 31 bytes, 78 cycles
ld a, (hl)
call parseHex
ret c ; faster and smaller than a conditional jump
; by delaying this push, we can use a conditional return above
push bc
ld b, a
inc hl
ld a, (hl)
cp 0x21
jr c, .single
call parseHex
jr c, .end
ld c, a
ld a, b
; by delaying shifting until the end, we save bytes in the single case.
rla \ rla \ rla \ rla
or c
.end:
pop bc
.end:
pop bc
ret
.single: ;53 cycles if single
ld a, b
dec hl
pop bc
ret
; Parse arguments at (HL) with specifiers at (DE) into (IX).
@ -86,6 +77,7 @@ parseHexPair:
; be placed in the next two bytes. This has to be the
; last argument of the list and it stops parsing.
; Sets A to nonzero if there was an error during parsing, zero otherwise.
parseArgs:
push bc
push de

View File

@ -85,14 +85,13 @@
; *** Code ***
; Wake the SD card up. After power up, a SD card has to receive at least 74
; dummy clocks with CS and DI high. We send 80.
; dummy clocks with CS and DI high. We send (way more than) 80.
sdcWakeUp:
out (SDC_PORT_CSHIGH), a
ld b, 10 ; 10 * 8 == 80
ld b, 10 ; 10 * 11 == 110
ld a, 0xff
.loop:
out (SDC_PORT_SPI), a
nop
djnz .loop
ret
@ -100,11 +99,9 @@ sdcWakeUp:
; is placed in A.
sdcSendRecv:
out (SDC_PORT_SPI), a
nop
nop
push hl ; nop, pairs with pop later
in a, (SDC_PORT_SPI)
nop
nop
pop hl ; nop, pairs with earlier push
ret
sdcIdle: