new line number parser, saved over 150 bytes

This commit is contained in:
FeralChild64 committed 2020-07-12 10:22:32 +02:00
1 parent 2b5c01d3bb
commit d1ac8ac371
14 files changed
+167 -332

No files matched your search

+1 -1
View File
@@ -23,7 +23,7 @@ cmd_goto:
jsr end_of_statement_check
bcs !+
jsr basic_parse_line_number
jsr fetch_line_number
jsr find_line
bcc !+
// Line does not exist, so report error
+1 -1
View File
@@ -41,7 +41,7 @@ cmd_sys_hack_not_needed:
// XXX End of hack
//
jsr basic_parse_line_number
jsr fetch_line_number
cmd_sys_setup_call:
lda #$4C // JMP opcode
@@ -12,7 +12,7 @@ helper_bload_fetch_address:
jsr injest_comma
bcs_16 do_SYNTAX_error
jsr basic_parse_line_number // XXX detect errors here
jsr fetch_line_number // XXX detect errors here
ldx LINNUM+0
ldy LINNUM+1
-31
View File
@@ -1,31 +0,0 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
basic_parse_line_number:
jsr injest_number
// Check if the number is a valid line number
// i.e., 16 bits, no exponent, no decimal
lda FAC1_mantissa+2
ora FAC1_mantissa+3
ora FAC1_exponent
beq !+
ml_bad_line_number:
// Invalid line number
jmp do_ILLEGAL_QUANTITY_error
!:
// Line number are not allowed to have decimal points in them
lda __tokenise_work4
cmp #$FF
bne ml_bad_line_number
// Got a valid line number.
lda FAC1_mantissa+0
sta LINNUM+0
lda FAC1_mantissa+1
sta LINNUM+1
clc
rts
+1 -1
View File
@@ -142,7 +142,7 @@ shell_process_line:
lda #>BUF
sta TXTPTR+1
jsr basic_parse_line_number
jsr fetch_line_number
// Get pointer to next char
lda TXTPTR+0
@@ -0,0 +1,21 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Tries to convert PETSCII in .A to a digit (0-9 values), sets Carry to indicate failure
//
convert_PETSCII_to_digit:
sec
sbc #$30
bcc convert_PETSCII_to_digit_fail
cmp #$0A
rts
convert_PETSCII_to_digit_fail:
sec
rts
+99
View File
@@ -0,0 +1,99 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Fetches line number, stores it in LINNUM, sets Carry to indicate failure (not a number), prints SYNTAX ERROR if above 65535
//
// Preserves both .X and .Y
//
fetch_line_number:
// Fetch the first character (skip spaces), check if 0-9
jsr fetch_character
cmp #$20
beq fetch_line_number
jsr convert_PETSCII_to_digit
bcs fetch_line_number_rts
// First character seems to be correct - store it
sta LINNUM+0
lda #$00
sta LINNUM+1
// FALLTROUGH
fetch_line_number_loop:
// Try to fetch another digit
jsr fetch_character
jsr convert_PETSCII_to_digit
bcs fetch_line_number_end
// We got another digit - store it
pha
// Multiply current LINNUM by 10 (x * 10 = (x * 2) + (x * 2) * 4)
jsr fetch_line_number_mul_LINNUM_2
lda LINNUM+1
pha
lda LINNUM+0
pha
jsr fetch_line_number_mul_LINNUM_2
jsr fetch_line_number_mul_LINNUM_2
clc
pla
adc LINNUM+0
sta LINNUM+0
pla
adc LINNUM+1
sta LINNUM+1
bcs_16 do_SYNTAX_error
// Add the new digit to LINNUM
pla
clc
adc LINNUM+0
sta LINNUM+0
lda LINNUM+1
adc #$00
sta LINNUM+1
bcs_16 do_SYNTAX_error
// Next iteration
#if HAS_OPCODES_65C02
bra fetch_line_number_loop
#else
jmp fetch_line_number_loop
#endif
fetch_line_number_end:
jsr unconsume_character
clc
// FALLTROUGH
fetch_line_number_rts:
rts
fetch_line_number_mul_LINNUM_2:
asl LINNUM+0
rol LINNUM+1
bcs_16 do_SYNTAX_error
rts
+1 -1
View File
@@ -17,7 +17,7 @@ fetch_coma_uint8:
fetch_uint8:
jsr injest_spaces
jsr basic_parse_line_number // XXX detect errors here
jsr fetch_line_number // XXX detect errors here
lda LINNUM+1
bne_16 do_ILLEGAL_QUANTITY_error
lda LINNUM+0
+3 -3
View File
@@ -16,7 +16,7 @@ fetch_variable:
cmp #$20 // ignore all the spaces
beq !-
jsr fetch_variable_is_AZ
jsr is_AZ
bcs_16 do_SYNTAX_error
sta VARNAM+0
@@ -24,7 +24,7 @@ fetch_variable:
// Fetch the (optional) second character
jsr fetch_character
jsr fetch_variable_is_09_AZ
jsr is_09_AZ
bcs fetch_variable_check_type
sta VARNAM+1
@@ -32,7 +32,7 @@ fetch_variable:
// Fetch the remaining variable characters - ignore them
!:
jsr fetch_character
jsr fetch_variable_is_09_AZ
jsr is_09_AZ
bcc !-
// FALLTROUGH
@@ -1,35 +0,0 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Check .A content, if within 0-9, A-Z ranges, sets Carry if check fails
//
fetch_variable_is_09_AZ:
cmp #$41 // fail if below '0'
bcc fetch_variable_cmp_fail
cmp #$3A // continue checking if above '9'
bcc fetch_variable_cmp_rts
// FALLTROUGH
fetch_variable_is_AZ:
cmp #$41 // fail if below 'A'
bcc fetch_variable_cmp_fail
cmp #$7B // fail if above 'Z'
// FALLTROUGH
fetch_variable_cmp_rts:
rts
fetch_variable_cmp_fail:
sec
rts
+38
View File
@@ -0,0 +1,38 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Check .A content, if within 0-9, A-Z ranges, sets Carry if check fails
//
is_09_AZ:
jsr is_AZ
bcs is_09
rts
is_09:
cmp #$30 // if below '0' - check failed
bcc is_alphanumeric_fail
cmp #$3A // if above '9' - check failed
rts
is_AZ:
cmp #$41 // if below 'A' - check failed
bcc is_alphanumeric_fail
cmp #$7B // if above 'Z' - check failed
rts
is_alphanumeric_fail:
sec
rts
-101
View File
@@ -1,101 +0,0 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
// Multiply Floating Point Accumulator by 10
// = 8x + 2x
// Implement by shifting MAC left one bit,
// and then keeping that value to add on after
// shifting two more bits left.
// This should be done using BITS to
// hold the overflow bits, so that the result can
// be exponent adjusted after, if required.
fac1_mul10:
lda #$00
sta BITS
// Multiply mantissa by 2
jsr fac1_mantissa_mul2
// copy down to BASIC numeric work area at $57
// (TEMPF1)
// (We copy all 7 bytes off the FAC1, so that we have
// the overflow bits, and the copy is as small as possible)
ldx #7
f1m10_l1:
lda FAC1_mantissa,x
sta TEMPF1,x
dex
bpl f1m10_l1
// Multiply mantissa by 4, so it is now 8x original
jsr fac1_mantissa_mul2
jsr fac1_mantissa_mul2
// Now add stored value of 2x to it, to get the total x10
ldy #3
ldx #0
clc
f1m10_l2:
lda FAC1_mantissa,x
adc TEMPF1,x
sta FAC1_mantissa,x
inx
dey
bpl f1m10_l2
lda BITS
adc TEMPF1+7
sta BITS
// Now shift right and increase exponent until overflow
// bits = $00
f1m10_l3:
lda BITS
bne !+
// All done, return
clc
rts
!:
// Divide mantissa by two, and increment exponent
jsr fac1_mantissa_div2
inc FAC1_exponent
lda FAC1_exponent
bpl f1m10_l3
// Exponent has bit 7 set, so has wrapped.
// = OVERFLOW ERROR
jmp do_OVERFLOW_error
fac1_mantissa_mul2:
ldy #3
ldx #0
clc
f1m1_l1:
// Multiply mantissa by 2, and store overflow
// in FAC1_bits
lda FAC1_mantissa,x
rol
sta FAC1_mantissa,x
inx
dey
bpl f1m1_l1
lda BITS
rol
sta BITS
rts
fac1_mantissa_div2:
// Divide mantissa by 2, including using overflow bits
clc
lda BITS
ror
sta BITS
ldy #3
f1md2_l1:
lda FAC1_mantissa,y
ror
sta FAC1_mantissa,y
dey
bpl f1md2_l1
rts
-156
View File
@@ -1,156 +0,0 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
// Parse a number from the input buffer at (TXTPTR)
// Result is put into FAC1.
// TXTPTR is updated with first position after the number.
// XXX - Remember decimal position and exponent, and apply them
// after to produce normalised floating point number.
// XXX - Does not use routines to access memory under ROMs etc.
// This is important, because this routine operates on numbers in BASIC text.
ij_not_a_digit:
// XXX check for "." or "E"
// Other chars terminate the accumulation.
// Look for exponentiation marker
// XXX - Implement me!
// Probably should have a flag so that we know if we are accumulating
// exponent or mantissa digits
// Look for decimal point
cmp #$2e
bne ij_not_decimal
lda __tokenise_work4
cmp #$ff
beq ij_found_decimal
// More than one decimal point in a number means we have reached the end of the number
clc
rts
ij_found_decimal:
// Found decimal.
// Thus start counting digits after the decimal, so that we
// can do the necessary number of divide by 10s on the final
// number.
lda #$00
sta __tokenise_work4
jmp ij_consider_next_digit
ij_not_decimal:
clc
rts
injest_number:
// Clear FAC1
jsr erase_fac1
// Clear decimal position flag
lda #$ff
sta __tokenise_work4
// Check if leading char is a - sign
// Note: the - will have been tokenised to $AB
// XXX - implement
ldy #$00
lda (TXTPTR),y
cmp #$AB
bne ij_loop1 // not minus
// Set sign to negative
lda #$ff
sta FAC1_sign
// Skip over the minus sign
jsr consume_character
ij_loop1:
ldy #$00
lda (TXTPTR),y
cmp #$30
bcc ij_not_a_digit
cmp #$3a
bcs ij_not_a_digit
// It is a digit
// Only add digit if we have not overflowed the accuracy of
// the mantissa
lda FAC1_exponent
beq ij_accept_precision
// We have an extra digit after saturating our precision.
// Thus we should ignore the digit, and just multiply the FAC
// by 10, but only if we have not seen a decimal point.
// If we have seen a decimal point, then we can completely ignore
// the digit, as it is contributing not recordable information
lda __tokenise_work4
cmp #$ff
bne ij_seen_decimal_point
jsr fac1_mul10
// Fall through
ij_seen_decimal_point:
jmp ij_consider_next_digit
ij_accept_precision:
// We can accept more precision, so multiply the mantissa by 10, so that
// we can add the new digit in
jsr fac1_mul10
lda FAC1_exponent
bne ij_consider_next_digit
// Get digit
ldy #$00
lda (TXTPTR),y
sec
sbc #$30
// Add to FAC1 mantissa
clc
adc FAC1_mantissa+0
sta FAC1_mantissa+0
ldx #1
ldy #3
ij_loop2:
lda FAC1_mantissa,x
adc #$00
sta FAC1_mantissa,x
inx
dey
bne ij_loop2
bcc !+
// Carry set, so we have overflowed.
// We should shift everything right one digit, and increase
// the exponent by one. The non-zero exponent tells us that we
// can not fit any more precision in.
jsr fac1_mantissa_div2
!:
ij_consider_next_digit:
// Consider next digit
jsr consume_character
jmp ij_loop1
rts
erase_fac1:
lda #$00
ldx #8
!:
sta FAC1_exponent,x
dex
bpl !-
sta FACOV
rts
+1 -1
View File
@@ -84,7 +84,7 @@ wedge_dos_command:
wedge_dos_change_drive:
// Reuse the line number parser for device number retrieval
jsr basic_parse_line_number
jsr fetch_line_number
lda LINNUM+1
beq !+
jmp do_ILLEGAL_QUANTITY_error