2019-07-13 21:08:16 -04:00
|
|
|
; cmd - parse and interpret command
|
|
|
|
;
|
|
|
|
; *** Consts ***
|
|
|
|
|
|
|
|
; address type
|
|
|
|
|
|
|
|
.equ ABSOLUTE 0
|
|
|
|
; handles +, - and ".". For +, easy. For -, addr is negative. For ., it's 0.
|
|
|
|
.equ RELATIVE 1
|
2019-10-04 13:49:33 -04:00
|
|
|
.equ EOF 2
|
2019-07-13 21:08:16 -04:00
|
|
|
|
|
|
|
; *** Variables ***
|
|
|
|
|
|
|
|
; An address is a one byte type and a two bytes line number (0-indexed)
|
|
|
|
.equ CMD_ADDR1 CMD_RAMSTART
|
2019-10-30 16:59:35 -04:00
|
|
|
.equ CMD_ADDR2 @+3
|
|
|
|
.equ CMD_TYPE @+3
|
|
|
|
.equ CMD_RAMEND @+1
|
2019-07-13 21:08:16 -04:00
|
|
|
|
|
|
|
; *** Code ***
|
|
|
|
|
|
|
|
; Parse command line that HL points to and set unit's variables
|
|
|
|
; Sets Z on success, unset on error.
|
|
|
|
cmdParse:
|
|
|
|
ld a, (hl)
|
|
|
|
cp 'q'
|
2019-07-21 11:39:00 -04:00
|
|
|
jr z, .simpleCmd
|
|
|
|
cp 'w'
|
|
|
|
jr z, .simpleCmd
|
2019-07-13 21:08:16 -04:00
|
|
|
ld ix, CMD_ADDR1
|
2019-07-13 22:09:17 -04:00
|
|
|
call .readAddr
|
|
|
|
ret nz
|
|
|
|
; Before we check for the existence of a second addr, let's set that
|
|
|
|
; second addr to the same value as the first. That's going to be its
|
|
|
|
; value if we have to ",".
|
|
|
|
ld a, (ix)
|
|
|
|
ld (CMD_ADDR2), a
|
|
|
|
ld a, (ix+1)
|
|
|
|
ld (CMD_ADDR2+1), a
|
|
|
|
ld a, (ix+2)
|
|
|
|
ld (CMD_ADDR2+2), a
|
|
|
|
ld a, (hl)
|
|
|
|
cp ','
|
|
|
|
jr nz, .noaddr2
|
|
|
|
inc hl
|
|
|
|
ld ix, CMD_ADDR2
|
|
|
|
call .readAddr
|
|
|
|
ret nz
|
|
|
|
.noaddr2:
|
|
|
|
; We expect HL (rest of the cmdline) to be a null char or an accepted
|
|
|
|
; cmd, otherwise it's garbage
|
|
|
|
ld a, (hl)
|
|
|
|
or a
|
|
|
|
jr z, .nullCmd
|
|
|
|
cp 'p'
|
|
|
|
jr z, .okCmd
|
2019-07-14 10:32:28 -04:00
|
|
|
cp 'd'
|
|
|
|
jr z, .okCmd
|
2019-07-14 17:29:00 -04:00
|
|
|
cp 'a'
|
|
|
|
jr z, .okCmd
|
|
|
|
cp 'i'
|
|
|
|
jr z, .okCmd
|
2019-07-13 22:09:17 -04:00
|
|
|
; unsupported cmd
|
|
|
|
ret ; Z unset
|
|
|
|
.nullCmd:
|
|
|
|
ld a, 'p'
|
|
|
|
.okCmd:
|
|
|
|
ld (CMD_TYPE), a
|
|
|
|
ret ; Z already set
|
|
|
|
|
2019-07-21 11:39:00 -04:00
|
|
|
.simpleCmd:
|
2019-07-13 21:08:16 -04:00
|
|
|
; Z already set
|
|
|
|
ld (CMD_TYPE), a
|
|
|
|
ret
|
|
|
|
|
|
|
|
; Parse the string at (HL) and sets its corresponding address in IX, properly
|
|
|
|
; considering implicit values (current address when nothing is specified).
|
|
|
|
; advances HL to the char next to the last parsed char.
|
|
|
|
; It handles "+" and "-" addresses such as "+3", "-2", "+", "-".
|
|
|
|
; Sets Z on success, unset on error. Line out of bounds isn't an error. Only
|
|
|
|
; overflows.
|
|
|
|
.readAddr:
|
|
|
|
ld a, (hl)
|
|
|
|
cp '+'
|
|
|
|
jr z, .plusOrMinus
|
|
|
|
cp '-'
|
|
|
|
jr z, .plusOrMinus
|
2019-10-04 13:49:33 -04:00
|
|
|
cp '.'
|
|
|
|
jr z, .dot
|
|
|
|
cp '$'
|
|
|
|
jr z, .eof
|
2019-10-30 16:59:35 -04:00
|
|
|
|
Decimal parse optimisations (#45)
* Optimised parsing functions and other minor optimisations
UnsetZ has been reduced by a byte, and between 17 and 28 cycles saved based on branching. Since branching is based on a being 0, it shouldn't have to branch very often and so be 28 cycles saved most the time. Including the initial call, the old version was 60 cycles, so this should be nearly twice as fast.
fmtHex has been reduced by 4 bytes and between 3 and 8 cycles based on branching.
fmtHexPair had a redundant "and" removed, saving two bytes and seven cycles.
parseHex has been reduced by 7 bytes. Due to so much branching, it's hard to say if it's faster, but it should be since it's fewer operations and now conditional returns are used which are a cycle faster than conditional jumps. I think there's more to improve here, but I haven't come up with anything yet.
* Major parsing optimisations
Totally reworked both parseDecimal and parseDecimalDigit
parseDecimalDigit no longer exists, as it could be replaced by an inline alternative in the 4 places it appeared. This saves one byte overall, as the inline version is 4 bytes, 1 byte more than a call, and removing the function saved 5 bytes. It has been reduced from between 52 and 35 cycles (35 on error, so we'd expect 52 cycles to be more common unless someone's really bad at programming) to 14 cycles, so 2-3 times faster.
parseDecimal has been reduced by a byte, and now the main loop is just about twice as fast, but with increased overhead. To put this into perspective, if we ignore error cases:
For decimals of length 1 it'll be 1.20x faster, for decimals of length 2, 1.41x faster, for length 3, 1.51x faster, for length 4, 1.57x faster, and for length 5 and above, at least 1.48x faster (even faster if there's leading zeroes or not the worst case scenario).
I believe there is still room for improvement, since the first iteration can be nearly replaced with "ld l, c" since 0*10=0, but when I tried this I could either add a zero check into the main loop, adding around 40 cycles and 10 bytes, or add 20 bytes to the overhead, and I don't think either of those options are worth it.
* Inlined parseDecimalDigit
See previous commit, and /lib/parse.asm, for details
* Fixed tabs and spacing
* Fixed tabs and spacing
* Better explanation and layout
* Corrected error in comments, and a new parseHex
5 bytes saved in parseHex, again hard to say what that does to speed, the shortest possible speed is probably a little slower but I think non-error cases should be around 9 cycles faster for decimal and 18 cycles faster for hex as there's now only two conditional returns and no compliment carries.
* Fixed the new parseHex
I accidentally did `add 0xe9` without specifying `a`
* Commented the use of daa
I made the comments surrounding my use of daa much clearer, so it isn't quite so mystical what's being done here.
* Removed skip leading zeroes, added skip first multiply
Now instead of skipping leading zeroes, the first digit is loaded directly into hl without first multiplying by 10. This means the first loop is skipped in the overhead, making the method 2-3 times faster overall, and is now faster for the more common fewer digit cases too. The number of bytes is exactly the same, and the inner loop is slightly faster too thanks to no longer needing to load a into c.
To be more precise about the speed increase over the current code, for decimals of length 1 it'll be 3.18x faster, for decimals of length 2, 2.50x faster, for length 3, 2.31x faster, for length 4, 2.22x faster, and for length 5 and above, at least 2.03x faster. In terms of cycles, this is around 100+(132*length) cycles saved per decimal.
* Fixed erroring out for all number >0x1999
I fixed the errors for numbers >0x1999, sadly it is now 6 bytes bigger, so 5 bytes larger than the original, but the speed increases should still hold.
* Fixed more errors, clearer choice of constants
* Clearer choice of constants
* Moved and indented comment about fmtHex's method
* Marked inlined parseDecimalDigit uses
* Renamed .error, removed trailing whitespace, more verbose comments.
2019-10-24 07:58:32 -04:00
|
|
|
; inline parseDecimalDigit
|
|
|
|
add a, 0xff-'9' ; maps '0'-'9' onto 0xf6-0xff
|
|
|
|
sub 0xff-9 ; maps to 0-9 and carries if not a digit
|
2019-10-30 16:59:35 -04:00
|
|
|
|
2019-07-13 21:08:16 -04:00
|
|
|
jr c, .notHandled
|
|
|
|
; straight number
|
|
|
|
ld a, ABSOLUTE
|
|
|
|
ld (ix), a
|
|
|
|
call .parseDecimalM
|
|
|
|
ret nz
|
|
|
|
dec de ; from 1-based to 0-base
|
|
|
|
jr .end
|
2019-10-04 13:49:33 -04:00
|
|
|
.dot:
|
|
|
|
inc hl ; advance cmd cursor
|
|
|
|
; the rest is the same as .notHandled
|
2019-07-13 21:08:16 -04:00
|
|
|
.notHandled:
|
2019-10-04 13:49:33 -04:00
|
|
|
; something else. It's probably our command. Our addr is therefore "."
|
2019-07-13 21:08:16 -04:00
|
|
|
ld a, RELATIVE
|
|
|
|
ld (ix), a
|
|
|
|
xor a ; sets Z
|
|
|
|
ld (ix+1), a
|
|
|
|
ld (ix+2), a
|
|
|
|
ret
|
2019-10-04 13:49:33 -04:00
|
|
|
.eof:
|
|
|
|
inc hl ; advance cmd cursor
|
|
|
|
ld a, EOF
|
|
|
|
ld (ix), a
|
|
|
|
ret ; Z set during earlier CP
|
2019-07-13 21:08:16 -04:00
|
|
|
.plusOrMinus:
|
|
|
|
push af ; preserve that + or -
|
|
|
|
ld a, RELATIVE
|
|
|
|
ld (ix), a
|
|
|
|
inc hl ; advance cmd cursor
|
|
|
|
ld a, (hl)
|
|
|
|
ld de, 1 ; if .pmNoSuffix
|
2019-10-30 16:59:35 -04:00
|
|
|
|
Decimal parse optimisations (#45)
* Optimised parsing functions and other minor optimisations
UnsetZ has been reduced by a byte, and between 17 and 28 cycles saved based on branching. Since branching is based on a being 0, it shouldn't have to branch very often and so be 28 cycles saved most the time. Including the initial call, the old version was 60 cycles, so this should be nearly twice as fast.
fmtHex has been reduced by 4 bytes and between 3 and 8 cycles based on branching.
fmtHexPair had a redundant "and" removed, saving two bytes and seven cycles.
parseHex has been reduced by 7 bytes. Due to so much branching, it's hard to say if it's faster, but it should be since it's fewer operations and now conditional returns are used which are a cycle faster than conditional jumps. I think there's more to improve here, but I haven't come up with anything yet.
* Major parsing optimisations
Totally reworked both parseDecimal and parseDecimalDigit
parseDecimalDigit no longer exists, as it could be replaced by an inline alternative in the 4 places it appeared. This saves one byte overall, as the inline version is 4 bytes, 1 byte more than a call, and removing the function saved 5 bytes. It has been reduced from between 52 and 35 cycles (35 on error, so we'd expect 52 cycles to be more common unless someone's really bad at programming) to 14 cycles, so 2-3 times faster.
parseDecimal has been reduced by a byte, and now the main loop is just about twice as fast, but with increased overhead. To put this into perspective, if we ignore error cases:
For decimals of length 1 it'll be 1.20x faster, for decimals of length 2, 1.41x faster, for length 3, 1.51x faster, for length 4, 1.57x faster, and for length 5 and above, at least 1.48x faster (even faster if there's leading zeroes or not the worst case scenario).
I believe there is still room for improvement, since the first iteration can be nearly replaced with "ld l, c" since 0*10=0, but when I tried this I could either add a zero check into the main loop, adding around 40 cycles and 10 bytes, or add 20 bytes to the overhead, and I don't think either of those options are worth it.
* Inlined parseDecimalDigit
See previous commit, and /lib/parse.asm, for details
* Fixed tabs and spacing
* Fixed tabs and spacing
* Better explanation and layout
* Corrected error in comments, and a new parseHex
5 bytes saved in parseHex, again hard to say what that does to speed, the shortest possible speed is probably a little slower but I think non-error cases should be around 9 cycles faster for decimal and 18 cycles faster for hex as there's now only two conditional returns and no compliment carries.
* Fixed the new parseHex
I accidentally did `add 0xe9` without specifying `a`
* Commented the use of daa
I made the comments surrounding my use of daa much clearer, so it isn't quite so mystical what's being done here.
* Removed skip leading zeroes, added skip first multiply
Now instead of skipping leading zeroes, the first digit is loaded directly into hl without first multiplying by 10. This means the first loop is skipped in the overhead, making the method 2-3 times faster overall, and is now faster for the more common fewer digit cases too. The number of bytes is exactly the same, and the inner loop is slightly faster too thanks to no longer needing to load a into c.
To be more precise about the speed increase over the current code, for decimals of length 1 it'll be 3.18x faster, for decimals of length 2, 2.50x faster, for length 3, 2.31x faster, for length 4, 2.22x faster, and for length 5 and above, at least 2.03x faster. In terms of cycles, this is around 100+(132*length) cycles saved per decimal.
* Fixed erroring out for all number >0x1999
I fixed the errors for numbers >0x1999, sadly it is now 6 bytes bigger, so 5 bytes larger than the original, but the speed increases should still hold.
* Fixed more errors, clearer choice of constants
* Clearer choice of constants
* Moved and indented comment about fmtHex's method
* Marked inlined parseDecimalDigit uses
* Renamed .error, removed trailing whitespace, more verbose comments.
2019-10-24 07:58:32 -04:00
|
|
|
; inline parseDecimalDigit
|
|
|
|
add a, 0xff-'9' ; maps '0'-'9' onto 0xf6-0xff
|
|
|
|
sub 0xff-9 ; maps to 0-9 and carries if not a digit
|
2019-10-30 16:59:35 -04:00
|
|
|
|
2019-07-13 21:08:16 -04:00
|
|
|
jr c, .pmNoSuffix
|
|
|
|
call .parseDecimalM ; --> DE
|
|
|
|
.pmNoSuffix:
|
|
|
|
pop af ; bring back that +/-
|
|
|
|
cp '-'
|
|
|
|
jr nz, .end
|
|
|
|
; we had a "-". Negate DE
|
|
|
|
push hl
|
|
|
|
ld hl, 0
|
|
|
|
sbc hl, de
|
|
|
|
ex de, hl
|
|
|
|
pop hl
|
|
|
|
.end:
|
|
|
|
; we still have to save DE in memory
|
|
|
|
ld (ix+1), e
|
|
|
|
ld (ix+2), d
|
2019-07-13 22:09:17 -04:00
|
|
|
cp a ; ensure Z
|
2019-07-13 21:08:16 -04:00
|
|
|
ret
|
|
|
|
|
|
|
|
; call parseDecimal and set HL to the character following the last digit
|
|
|
|
.parseDecimalM:
|
|
|
|
push bc
|
|
|
|
push ix
|
|
|
|
push hl
|
|
|
|
.loop:
|
|
|
|
inc hl
|
|
|
|
ld a, (hl)
|
2019-10-30 16:59:35 -04:00
|
|
|
|
Decimal parse optimisations (#45)
* Optimised parsing functions and other minor optimisations
UnsetZ has been reduced by a byte, and between 17 and 28 cycles saved based on branching. Since branching is based on a being 0, it shouldn't have to branch very often and so be 28 cycles saved most the time. Including the initial call, the old version was 60 cycles, so this should be nearly twice as fast.
fmtHex has been reduced by 4 bytes and between 3 and 8 cycles based on branching.
fmtHexPair had a redundant "and" removed, saving two bytes and seven cycles.
parseHex has been reduced by 7 bytes. Due to so much branching, it's hard to say if it's faster, but it should be since it's fewer operations and now conditional returns are used which are a cycle faster than conditional jumps. I think there's more to improve here, but I haven't come up with anything yet.
* Major parsing optimisations
Totally reworked both parseDecimal and parseDecimalDigit
parseDecimalDigit no longer exists, as it could be replaced by an inline alternative in the 4 places it appeared. This saves one byte overall, as the inline version is 4 bytes, 1 byte more than a call, and removing the function saved 5 bytes. It has been reduced from between 52 and 35 cycles (35 on error, so we'd expect 52 cycles to be more common unless someone's really bad at programming) to 14 cycles, so 2-3 times faster.
parseDecimal has been reduced by a byte, and now the main loop is just about twice as fast, but with increased overhead. To put this into perspective, if we ignore error cases:
For decimals of length 1 it'll be 1.20x faster, for decimals of length 2, 1.41x faster, for length 3, 1.51x faster, for length 4, 1.57x faster, and for length 5 and above, at least 1.48x faster (even faster if there's leading zeroes or not the worst case scenario).
I believe there is still room for improvement, since the first iteration can be nearly replaced with "ld l, c" since 0*10=0, but when I tried this I could either add a zero check into the main loop, adding around 40 cycles and 10 bytes, or add 20 bytes to the overhead, and I don't think either of those options are worth it.
* Inlined parseDecimalDigit
See previous commit, and /lib/parse.asm, for details
* Fixed tabs and spacing
* Fixed tabs and spacing
* Better explanation and layout
* Corrected error in comments, and a new parseHex
5 bytes saved in parseHex, again hard to say what that does to speed, the shortest possible speed is probably a little slower but I think non-error cases should be around 9 cycles faster for decimal and 18 cycles faster for hex as there's now only two conditional returns and no compliment carries.
* Fixed the new parseHex
I accidentally did `add 0xe9` without specifying `a`
* Commented the use of daa
I made the comments surrounding my use of daa much clearer, so it isn't quite so mystical what's being done here.
* Removed skip leading zeroes, added skip first multiply
Now instead of skipping leading zeroes, the first digit is loaded directly into hl without first multiplying by 10. This means the first loop is skipped in the overhead, making the method 2-3 times faster overall, and is now faster for the more common fewer digit cases too. The number of bytes is exactly the same, and the inner loop is slightly faster too thanks to no longer needing to load a into c.
To be more precise about the speed increase over the current code, for decimals of length 1 it'll be 3.18x faster, for decimals of length 2, 2.50x faster, for length 3, 2.31x faster, for length 4, 2.22x faster, and for length 5 and above, at least 2.03x faster. In terms of cycles, this is around 100+(132*length) cycles saved per decimal.
* Fixed erroring out for all number >0x1999
I fixed the errors for numbers >0x1999, sadly it is now 6 bytes bigger, so 5 bytes larger than the original, but the speed increases should still hold.
* Fixed more errors, clearer choice of constants
* Clearer choice of constants
* Moved and indented comment about fmtHex's method
* Marked inlined parseDecimalDigit uses
* Renamed .error, removed trailing whitespace, more verbose comments.
2019-10-24 07:58:32 -04:00
|
|
|
; inline parseDecimalDigit
|
|
|
|
add a, 0xff-'9' ; maps '0'-'9' onto 0xf6-0xff
|
|
|
|
sub 0xff-9 ; maps to 0-9 and carries if not a digit
|
2019-10-30 16:59:35 -04:00
|
|
|
|
2019-07-13 21:08:16 -04:00
|
|
|
jr nc, .loop
|
|
|
|
; We're at the first non-digit char. Let's save it because we're going
|
|
|
|
; to temporarily replace it with a null.
|
2019-07-13 22:09:17 -04:00
|
|
|
ld b, (hl) ; refetch (HL), A has been mucked with in
|
|
|
|
; parseDecimalDigit
|
2019-07-13 21:08:16 -04:00
|
|
|
xor a
|
|
|
|
ld (hl), a
|
|
|
|
; Now, let's go back to the beginning of the string and parse it.
|
|
|
|
; but before we do this, let's save the end of string in DE
|
|
|
|
ex de, hl
|
|
|
|
pop hl
|
|
|
|
call parseDecimal
|
|
|
|
; Z is set properly at this point. nothing touches Z below.
|
|
|
|
ld a, b
|
|
|
|
ld (de), a
|
|
|
|
ex de, hl ; put end of string back from DE to HL
|
|
|
|
; Put addr in its final register, DE
|
|
|
|
push ix \ pop de
|
|
|
|
pop ix
|
|
|
|
pop bc
|
|
|
|
ret
|
|
|
|
|