1
0
mirror of https://github.com/hsoft/collapseos.git synced 2025-04-12 23:28:16 +10: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. ; On success, the carry flag is reset. On error, it is set.
parseHex: parseHex:
; First, let's see if we have an easy 0-9 case ; 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
.alpha: add a, 0xc6 ; maps '0'-'9' onto 0xf6-0xff
call upcase sub 0xf6 ; maps to 0-9 and carries if not a digit
cp 'A' ret nc
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: and 0xdf ; converts lowercase to uppercase
scf 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 ret
; Parses 2 characters of the string pointed to by HL and returns the numerical ; Parses 2 characters of the string pointed to by HL and returns the numerical
@ -39,36 +29,37 @@ parseHex:
; HL is set to point to the last char of the pair. ; HL is set to point to the last char of the pair.
; ;
; On success, the carry flag is reset. On error, it is set. ; On success, the carry flag is reset. On error, it is set.
parseHexPair:
push bc
parseHexPair: ; 31 bytes, 78 cycles
ld a, (hl) ld a, (hl)
call parseHex call parseHex
jr c, .end ; error? goto end, keeping the C flag on ret c ; faster and smaller than a conditional jump
rla \ rla \ rla \ rla ; let's push this in MSB ; by delaying this push, we can use a conditional return above
push bc
ld b, a ld b, a
inc hl inc hl
ld a, (hl) ld a, (hl)
cp 0x21 cp 0x21
jr c, .single ; special char? single digit jr c, .single
call parseHex call parseHex
jr c, .end ; error? jr c, .end
or b ; join left-shifted + new. we're done! ld c, a
; 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 ld a, b
and 0xf0 ; by delaying shifting until the end, we save bytes in the single case.
rra \ rra \ rra \ rra rla \ rla \ rla \ rla
dec hl or c
.end: .end:
pop bc pop bc
ret ret
.single: ;53 cycles if single
ld a, b
dec hl
pop bc
ret
; Parse arguments at (HL) with specifiers at (DE) into (IX). ; Parse arguments at (HL) with specifiers at (DE) into (IX).
; ;
; Args specifiers are a series of flag for each arg: ; Args specifiers are a series of flag for each arg:
@ -86,6 +77,7 @@ parseHexPair:
; be placed in the next two bytes. This has to be the ; be placed in the next two bytes. This has to be the
; last argument of the list and it stops parsing. ; last argument of the list and it stops parsing.
; Sets A to nonzero if there was an error during parsing, zero otherwise. ; Sets A to nonzero if there was an error during parsing, zero otherwise.
parseArgs: parseArgs:
push bc push bc
push de push de

View File

@ -85,14 +85,13 @@
; *** Code *** ; *** Code ***
; Wake the SD card up. After power up, a SD card has to receive at least 74 ; 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: sdcWakeUp:
out (SDC_PORT_CSHIGH), a out (SDC_PORT_CSHIGH), a
ld b, 10 ; 10 * 8 == 80 ld b, 10 ; 10 * 11 == 110
ld a, 0xff ld a, 0xff
.loop: .loop:
out (SDC_PORT_SPI), a out (SDC_PORT_SPI), a
nop
djnz .loop djnz .loop
ret ret
@ -100,11 +99,9 @@ sdcWakeUp:
; is placed in A. ; is placed in A.
sdcSendRecv: sdcSendRecv:
out (SDC_PORT_SPI), a out (SDC_PORT_SPI), a
nop push hl ; nop, pairs with pop later
nop
in a, (SDC_PORT_SPI) in a, (SDC_PORT_SPI)
nop pop hl ; nop, pairs with earlier push
nop
ret ret
sdcIdle: sdcIdle: