added memory checks and some other bits to array creation routine

This commit is contained in:
FeralChild64 committed 2020-07-26 22:03:06 +02:00
1 parent 07bbc21e50
commit b7dee84390
3 files changed
+133 -63

No files matched your search

+55 -47
View File
@@ -88,14 +88,21 @@ array_create_store_dims:
phx_trash_a
jsr varstr_garbage_collect
// __FAC1+0/+1 will be used to calculate number of elements
// __FAC1+1/+2 will be used to calculate number of elements
ldy #$01
sty __FAC1+0
dey
sty __FAC1+1
dey
sty __FAC1+2
// Create the initial array structure (format checked by creating arrays with original ROM)
// Check if there is enough free memory for the header
pla // .A - number of dimensions
sta __FAC1+0
jsr helper_array_create_checkmem
lda __FAC1+0
// Create the initial array structure (header) - format checked by creating arrays with original ROM
// Bytes 0/1 - array name, but we can not fetch it easily now, so skip it for now
// Bytes 2/3 - offset to the next array, skip it for now too
@@ -103,13 +110,15 @@ array_create_store_dims:
// Bytes 5+ - max index in each dimension (big endian!), this will be set in a loop
ldy #$04
pla
#if CONFIG_MEMORY_MODEL_60K
// XXX
// XXX: implement this
// XXX
stx INDEX+2
ldx #<STREND
jsr poke_under_roms
ldx INDEX+2
#else // CONFIG_MEMORY_MODEL_38K || CONFIG_MEMORY_MODEL_46K || CONFIG_MEMORY_MODEL_50K
@@ -118,7 +127,7 @@ array_create_store_dims:
#endif
iny // .Y - index to store dimension sizes
tax // .X - dimensions not sotred yet
tax // .X - dimensions not stored yet
// FALLTROUGH
@@ -126,19 +135,29 @@ array_create_store_loop:
#if CONFIG_MEMORY_MODEL_60K
// XXX
// XXX: implement this
// XXX
stx INDEX+2
ldx #<STREND
pla
jsr poke_under_roms
sta __FAC1+4
iny
pla
jsr poke_under_roms
sta __FAC1+3
iny
ldx INDEX+2
#else // CONFIG_MEMORY_MODEL_38K || CONFIG_MEMORY_MODEL_46K || CONFIG_MEMORY_MODEL_50K
pla
sta (STREND), y
sta __FAC1+3
sta __FAC1+4
iny
pla
sta (STREND), y
sta __FAC1+2
sta __FAC1+3
iny
#endif
@@ -156,12 +175,14 @@ array_create_store_dims_done:
pha
sta FOUR6 // XXX do we need to store this value in FOUR6? is it still needed?
sta __FAC1+2
stx __FAC1+3 // .X is 0 at this point
sta __FAC1+3
stx __FAC1+4 // .X is 0 at this point
jsr helper_array_create_mul
// XXX check if we have enough memory
// Check if there is enough free memory
jsr helper_array_create_checkmem
// Retrieve and store array name
@@ -169,9 +190,13 @@ array_create_store_dims_done:
#if CONFIG_MEMORY_MODEL_60K
// XXX
// XXX: implement this
// XXX
ldx #<STREND
pla
jsr poke_under_roms
iny
pla
jsr poke_under_roms
#else // CONFIG_MEMORY_MODEL_38K || CONFIG_MEMORY_MODEL_46K || CONFIG_MEMORY_MODEL_50K
@@ -188,33 +213,20 @@ array_create_store_dims_done:
lda #$02
jsr helper_INDEX_up_A
#if CONFIG_MEMORY_MODEL_60K
// XXX
// XXX: implement this
// XXX
#elif CONFIG_MEMORY_MODEL_46K || CONFIG_MEMORY_MODEL_50K
// XXX
// XXX: implement this
// XXX
#else // CONFIG_MEMORY_MODEL_38K
ldy #$04
lda (STREND), y
#endif
lda __FAC1+0
asl
jsr helper_INDEX_up_A
#if CONFIG_MEMORY_MODEL_60K
// .X already contains STREND
// XXX
// XXX: implement this
// XXX
ldy #$02
lda INDEX+0
jsr poke_under_roms
iny
lda INDEX+1
jsr poke_under_roms
#else // CONFIG_MEMORY_MODEL_38K || CONFIG_MEMORY_MODEL_46K || CONFIG_MEMORY_MODEL_50K
@@ -227,11 +239,7 @@ array_create_store_dims_done:
#endif
// Clear newly alocated area
// XXX implement this
// Adjust STREND
// Clear newly alocated area, adjust STREND
// XXX implement this
@@ -0,0 +1,62 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Checks if there is enough memory free for array
//
// Input:
// __FAC1+0 - number of dimensions
// __FAC1+1/+2 - bytes needed for storage
//
// Uses INDEX+2/+3, preserves .X and .Y
//
helper_array_create_checkmem:
// First calculate FRETOP - STREND
sec
lda FRETOP+0
sbc STREND+0
sta INDEX+2
lda FRETOP+1
sbc STREND+1
sta INDEX+3
bcc helper_array_create_checkmem_fail
// INDEX+2/+3 contains the amount of memory left, subtract the space needed for storage
lda INDEX+2
sbc __FAC1+1
sta INDEX+2
lda INDEX+3
sbc __FAC1+2
sta INDEX+3
bcc helper_array_create_checkmem_fail
// If high byte (INDEX+3) > 0, then we certainly have enough space
lda INDEX+3
bne helper_array_create_checkmem_success
// We have very little memory left - we need at least number of dimensions*2 + 5 bytes
lda __FAC1+0
asl
clc
adc #$04
cmp INDEX+2
bcs helper_array_create_checkmem_fail
// FALLTROUGH
helper_array_create_checkmem_success:
rts
helper_array_create_checkmem_fail:
jmp do_OUT_OF_MEMORY_error
@@ -2,7 +2,7 @@
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
// 16-bit multiplication, INDEX+0/+1 = __FAC1+0/+1 * __FAC1+2/+3; __FAC1+0/+1 = INDEX+0/+1
// 16-bit multiplication, INDEX+0/+1 = __FAC1+1/+2 * __FAC1+3/+4; __FAC1+1/+2 = INDEX+0/+1
// uses INDEX+2/+3 (for helper routine) as work area
@@ -12,37 +12,37 @@ helper_array_create_mul: // preserves .X and .Y
// We will reuse 8x8 multiplication routine used by floating point operations
// __FAC1+0 (lo byte) * __FAC1+2 (lo byte)
// __FAC1+1 (lo byte) * __FAC1+3 (lo byte)
lda __FAC1+0
lda __FAC1+1
sta INDEX+2
lda __FAC1+2
lda __FAC1+3
jsr mul_FAC2_FAC1_8x8 // this preserves .Y
sta INDEX+0 // result - lo byte
stx INDEX+1 // result - hi byte
// __FAC1+0 (lo byte) * __FAC1+3 (hi byte)
lda __FAC1+0
sta INDEX+2
lda __FAC1+3
jsr helper_array_create_mul_8x8_add // multiply and add to high byte of the result
// __FAC1+1 (hi byte) * __FAC1+2 (lo byte)
// __FAC1+1 (lo byte) * __FAC1+4 (hi byte)
lda __FAC1+1
sta INDEX+2
lda __FAC1+2
lda __FAC1+4
jsr helper_array_create_mul_8x8_add // multiply and add to high byte of the result
// __FAC1+1 (hi byte) * __FAC1+3 (hi byte) has to be 0,
// __FAC1+2 (hi byte) * __FAC1+3 (lo byte)
lda __FAC1+2
sta INDEX+2
lda __FAC1+3
jsr helper_array_create_mul_8x8_add // multiply and add to high byte of the result
// __FAC1+2 (hi byte) * __FAC1+4 (hi byte) has to be 0,
// otherwise the result would be out of range
// do not calculate
lda __FAC1+0
lda __FAC1+2
beq helper_array_create_mul_success
lda __FAC1+3
lda __FAC1+4
beq helper_array_create_mul_success
// FALLTROUGH