From 0460777fe911fdf76d319103e60328c31c540812 Mon Sep 17 00:00:00 2001 From: FeralChild64 Date: Sun, 28 Jun 2020 11:35:29 +0200 Subject: [PATCH] fixed NEW/CLR commands, implemented RESTORE --- src/aliases/,aliases_ram_lowmem.s | 4 +- src/basic/basic_commands/cmd__unimplemented.s | 2 - src/basic/basic_commands/cmd_clr.s | 2 +- src/basic/basic_commands/cmd_load_verify.s | 2 +- src/basic/basic_commands/cmd_new.s | 2 +- src/basic/basic_commands/cmd_restore.s | 12 ++ src/basic/basic_commands/cmd_run_goto.s | 2 +- src/basic/basic_commands_CC/cmd_merge.s | 2 +- src/basic/basic_do_new.s | 99 ----------------- .../board_m65/4000.mega65_vectable_basic_1.s | 10 +- src/basic/board_m65/mega65_imports.s | 1 + src/basic/board_m65/mega65_proxies.s | 8 ++ src/basic/editor/shell.s | 2 +- src/basic/engine/do_new_clr_restore.s | 103 ++++++++++++++++++ src/basic/init/initmsg_bytes_free.s | 55 +++++++++- 15 files changed, 191 insertions(+), 115 deletions(-) create mode 100644 src/basic/basic_commands/cmd_restore.s delete mode 100644 src/basic/basic_do_new.s create mode 100644 src/basic/engine/do_new_clr_restore.s diff --git a/src/aliases/,aliases_ram_lowmem.s b/src/aliases/,aliases_ram_lowmem.s index 3e206102..08c6d7e9 100644 --- a/src/aliases/,aliases_ram_lowmem.s +++ b/src/aliases/,aliases_ram_lowmem.s @@ -37,14 +37,14 @@ .label VARTAB = $2D // $2D-$2E end of BASIC code, start of variables .label ARYTAB = $2F // $2F-$30 -- NOT IMPLEMENTED -- .label STREND = $31 // $31-$32 -- NOT IMPLEMENTED -- - .label FRETOP = $33 // $33-$34 -- NOT IMPLEMENTED -- + .label FRETOP = $33 // $33-$34 pointer to bottom of strings area .label FRESPC = $35 // $35-$36 [!] our implementation uses this as temporary string pointer .label MEMSIZ = $37 // $37-$38 highest address of BASIC memory + 1 .label CURLIN = $39 // $39-$3A current BASIC line number .label OLDLIN = $3B // $3B-$3C previous BASIC line number .label OLDTXT = $3D // $3D-$3E current BASIC line pointer .label DATLIN = $3F // $3F-$40 -- NOT IMPLEMENTED -- - .label DATPTR = $41 // $41-$42 -- NOT IMPLEMENTED -- + .label DATPTR = $41 // $41-$42 pointer for READ/DATA copmmands .label INPPTR = $43 // $43-$44 -- NOT IMPLEMENTED -- .label VARNAM = $45 // $45-$46 -- NOT IMPLEMENTED -- .label VARPNT = $47 // $47-$48 -- NOT IMPLEMENTED -- diff --git a/src/basic/basic_commands/cmd__unimplemented.s b/src/basic/basic_commands/cmd__unimplemented.s index 4e67d479..6495feb6 100644 --- a/src/basic/basic_commands/cmd__unimplemented.s +++ b/src/basic/basic_commands/cmd__unimplemented.s @@ -27,8 +27,6 @@ cmd_let: nop cmd_if: nop -cmd_restore: - nop cmd_gosub: nop cmd_return: diff --git a/src/basic/basic_commands/cmd_clr.s b/src/basic/basic_commands/cmd_clr.s index 6b4b493c..f944eacc 100644 --- a/src/basic/basic_commands/cmd_clr.s +++ b/src/basic/basic_commands/cmd_clr.s @@ -5,7 +5,7 @@ cmd_clr: - jsr basic_do_clr + jsr do_clr // CLR command does not stop execution (confirmed on a C64) diff --git a/src/basic/basic_commands/cmd_load_verify.s b/src/basic/basic_commands/cmd_load_verify.s index 868c7406..9a0d59fd 100644 --- a/src/basic/basic_commands/cmd_load_verify.s +++ b/src/basic/basic_commands/cmd_load_verify.s @@ -56,7 +56,7 @@ cmd_load_no_error: // C64 BASIC apparently does not clear variables after a LOAD in the // middle of a program. For safety, we do. - jsr basic_do_clr + jsr do_clr // Now relink the loaded program, as we cannot trust the line // links supplied. For example, the VICE virtual drive emulation diff --git a/src/basic/basic_commands/cmd_new.s b/src/basic/basic_commands/cmd_new.s index d2186c3f..e7d7189f 100644 --- a/src/basic/basic_commands/cmd_new.s +++ b/src/basic/basic_commands/cmd_new.s @@ -5,7 +5,7 @@ cmd_new: - jsr basic_do_new + jsr do_new // NEW command terminates execution (confirmed on a C64) diff --git a/src/basic/basic_commands/cmd_restore.s b/src/basic/basic_commands/cmd_restore.s new file mode 100644 index 00000000..034db54e --- /dev/null +++ b/src/basic/basic_commands/cmd_restore.s @@ -0,0 +1,12 @@ +// #LAYOUT# STD * #TAKE +// #LAYOUT# * BASIC_0 #TAKE +// #LAYOUT# * * #IGNORE + + +cmd_restore: + + jsr do_restore + + // RESTORE command does not stop execution (confirmed on a C64) + + jmp execute_statements diff --git a/src/basic/basic_commands/cmd_run_goto.s b/src/basic/basic_commands/cmd_run_goto.s index dc93da96..4282ca1c 100644 --- a/src/basic/basic_commands/cmd_run_goto.s +++ b/src/basic/basic_commands/cmd_run_goto.s @@ -5,7 +5,7 @@ cmd_run: // RUN clears all variables - jsr basic_do_clr + jsr do_clr cmd_goto: // Disable Kernal messages lda #$00 diff --git a/src/basic/basic_commands_CC/cmd_merge.s b/src/basic/basic_commands_CC/cmd_merge.s index 049f049e..6bcabe8d 100644 --- a/src/basic/basic_commands_CC/cmd_merge.s +++ b/src/basic/basic_commands_CC/cmd_merge.s @@ -40,7 +40,7 @@ cmd_merge_no_error: sty VARTAB+1 // Clear the variables - jsr basic_do_clr + jsr do_clr // Now relink the loaded program, line links supplied are almost certainly wrong diff --git a/src/basic/basic_do_new.s b/src/basic/basic_do_new.s deleted file mode 100644 index 0c9119eb..00000000 --- a/src/basic/basic_do_new.s +++ /dev/null @@ -1,99 +0,0 @@ -// #LAYOUT# STD * #TAKE -// #LAYOUT# M65 * #TAKE -// #LAYOUT# X16 BASIC_0 #TAKE -// #LAYOUT# * * #IGNORE - - -basic_do_new: - -#if (ROM_LAYOUT_M65 && SEGMENT_BASIC_0) - - jsr map_BASIC_1 - jsr_ind VB1__basic_do_new - jmp map_NORMAL - -basic_do_clr: - - jsr map_BASIC_1 - jsr_ind VB1__basic_do_clr - jmp map_NORMAL - -#else - - // Setup pointers to memory storage XXX should depend on text start vector - lda #<$0801 - sta TXTTAB+0 - lda #>$0801 - sta TXTTAB+1 - sta VARTAB+1 - lda #<$0803 - sta VARTAB+0 - - // Zero line pointer - // XXX - We should also zero $0800, i.e., TXTTAB - 1 - ldy #$00 - tya - sta (TXTTAB),y - iny - sta (TXTTAB),y - - - // Get top of memory. - // If a cart is present, then it is $7FFF, - // if not, then it is $F7FF, since we support having - // programs and variables under ROM. - // i.e., we support 56KB RAM for BASIC, keeping some space - // free for some optimisation structures, e.g., FOR/NEXT loop - // records (without them going on the stack), GOSUB stack - // (same story), expression value cache? - - sec // Read, not write value - jsr JMEMTOP - cpx #$80 - beq !+ -#if CONFIG_MEMORY_MODEL_60K - lda #>$F7FF -#elif CONFIG_MEMORY_MODEL_50K - lda #>$D000 -#elif CONFIG_MEMORY_MODEL_46K - lda #>$C000 -#else // CONFIG_MEMORY_MODEL_38K - lda #>$A000 -#endif - skip_2_bytes_trash_nvz -!: - lda #>$8000 - sta MEMSIZ+1 - sta FRETOP+1 -#if CONFIG_MEMORY_MODEL_60K - cpx #$80 - beq !+ - lda #$FF - skip_2_bytes_trash_nvz -!: - lda #<$00 -#else // CONFIG_MEMORY_MODEL_38K || CONFIG_MEMORY_MODEL_46K || CONFIG_MEMORY_MODEL_50K - lda #<$00 -#endif - sta MEMSIZ+0 - sta FRETOP+0 - - // FALLTHROUGH - -basic_do_clr: - - // XXX shouldnt FRETOP go here? - - // Clear variables, arrays and strings - - lda VARTAB+0 - sta ARYTAB+0 - sta STREND+0 - lda VARTAB+1 - sta ARYTAB+1 - sta STREND+1 - - rts - - -#endif // ROM layout diff --git a/src/basic/board_m65/4000.mega65_vectable_basic_1.s b/src/basic/board_m65/4000.mega65_vectable_basic_1.s index 9239e233..d83bbfdd 100644 --- a/src/basic/board_m65/4000.mega65_vectable_basic_1.s +++ b/src/basic/board_m65/4000.mega65_vectable_basic_1.s @@ -17,8 +17,9 @@ .label VB1__list_single_line = $4000 + 2 * 3 .label VB1__print_packed_error = $4000 + 2 * 4 .label VB1__print_packed_misc_str = $4000 + 2 * 5 - .label VB1__basic_do_clr = $4000 + 2 * 6 - .label VB1__basic_do_new = $4000 + 2 * 7 + .label VB1__do_new = $4000 + 2 * 6 + .label VB1__do_clr = $4000 + 2 * 7 + .label VB1__do_restore = $4000 + 2 * 8 #else @@ -30,7 +31,8 @@ .word list_single_line .word print_packed_error .word print_packed_misc_str - .word basic_do_clr - .word basic_do_new + .word do_new + .word do_clr + .word do_restore #endif diff --git a/src/basic/board_m65/mega65_imports.s b/src/basic/board_m65/mega65_imports.s index 3cf9df91..4235e8f7 100644 --- a/src/basic/board_m65/mega65_imports.s +++ b/src/basic/board_m65/mega65_imports.s @@ -11,6 +11,7 @@ .label peek_via_OLDTXT = BASIC_0.proxy_B1_peek_via_OLDTXT .label poke_via_OLDTXT = BASIC_0.proxy_B1_poke_via_OLDTXT .label JCHROUT = BASIC_0.proxy_B1_JCHROUT +.label JCLALL = BASIC_0.proxy_B1_JCLALL .label JMEMTOP = KERNAL_0.JMEMTOP .label plot_set = KERNAL_0.plot_set .label init_oldtxt = BASIC_0.init_oldtxt diff --git a/src/basic/board_m65/mega65_proxies.s b/src/basic/board_m65/mega65_proxies.s index c746d06f..191f4e81 100644 --- a/src/basic/board_m65/mega65_proxies.s +++ b/src/basic/board_m65/mega65_proxies.s @@ -30,6 +30,13 @@ proxy_B1_poke_via_OLDTXT: jmp map_BASIC_1 +proxy_B1_JCLALL: + + jsr map_NORMAL + jsr JCLALL + jmp map_BASIC_1 + + proxy_B1_JCHROUT: jsr map_NORMAL @@ -57,6 +64,7 @@ proxy_B1_print_space: jsr print_space jmp map_BASIC_1 + #if CONFIG_SHOW_FEATURES proxy_B1_print_features: diff --git a/src/basic/editor/shell.s b/src/basic/editor/shell.s index d4a366d7..845ab364 100644 --- a/src/basic/editor/shell.s +++ b/src/basic/editor/shell.s @@ -147,7 +147,7 @@ shell_process_line: shell_add_delete_line: // First, clear all variables, so that we only have to shove BASIC text around - jsr basic_do_clr + jsr do_clr // Check if line already present jsr find_line diff --git a/src/basic/engine/do_new_clr_restore.s b/src/basic/engine/do_new_clr_restore.s new file mode 100644 index 00000000..3a249d2a --- /dev/null +++ b/src/basic/engine/do_new_clr_restore.s @@ -0,0 +1,103 @@ +// #LAYOUT# STD * #TAKE +// #LAYOUT# M65 * #TAKE +// #LAYOUT# X16 BASIC_0 #TAKE +// #LAYOUT# * * #IGNORE + + +// See https://www.c64-wiki.com/wiki/Memory_(BASIC) for BASIC memory organization + + +do_new: + +#if (ROM_LAYOUT_M65 && SEGMENT_BASIC_0) + + jsr map_BASIC_1 + jsr_ind VB1__do_new + jmp map_NORMAL + +do_clr: + + jsr map_BASIC_1 + jsr_ind VB1__do_clr + jmp map_NORMAL + +do_restore: + + jsr map_BASIC_1 + jsr_ind VB1__do_restore + jmp map_NORMAL + +#else + + // See Computes Mapping the Commodore 64 - page 96 + + ldy #$00 + tya + sta (TXTTAB),y + iny + sta (TXTTAB),y + + // The book does not mention this, but IMHO it is necessary to initialize VARTAB here too + + clc + lda TXTTAB+0 + adc #$02 + sta VARTAB+0 + lda TXTTAB+1 + adc #$00 + sta VARTAB+1 + +do_clr: + + // See Computes Mapping the Commodore 64 - page 96 + + // XXX check on real ROMs that it is being done too when entering new line + jsr JCLALL + + lda MEMSIZ+0 + sta FRETOP+0 + lda MEMSIZ+1 + sta FRETOP+1 + + // The book is not precise here $31-$32 is definitely not the end of BASIC text + + lda VARTAB+0 + sta ARYTAB+0 + sta STREND+0 + + lda VARTAB+1 + sta ARYTAB+1 + sta STREND+1 + + // FALLTROUGH (confirmed on real C64 that CLR indeed resets DATPTR) + +do_restore: + + // See Computes Mapping the Commodore 64 - page 98 + // Initial DATPTR value checked on real C64 + +#if HAS_OPCODES_65CE02 + + lda TXTTAB+0 + sta DATPTR+0 + lda TXTTAB+1 + sta DATPTR+1 + + dew DATPTR + +#else + + sec + lda TXTTAB+0 + sbc #$01 + sta DATPTR+0 + lda TXTTAB+1 + sbc #$00 + sta DATPTR+1 + +#endif + + rts + + +#endif // ROM layout diff --git a/src/basic/init/initmsg_bytes_free.s b/src/basic/init/initmsg_bytes_free.s index b277340f..ed8db16f 100644 --- a/src/basic/init/initmsg_bytes_free.s +++ b/src/basic/init/initmsg_bytes_free.s @@ -10,7 +10,59 @@ initmsg_bytes_free: - jsr basic_do_new + // Setup pointers to BASIC memory storage + + lda #<$0801 + sta TXTTAB+0 + lda #>$0801 + sta TXTTAB+1 + + // Get top of memory. + // If a cart is present, then it is $7FFF, + // if not, then it is $F7FF, since we support having + // programs and variables under ROM. + // i.e., we support 56KB RAM for BASIC, keeping some space + // free for some optimisation structures, e.g., FOR/NEXT loop + // records (without them going on the stack), GOSUB stack + // (same story), expression value cache? + + sec // Read, not write value + jsr JMEMTOP + cpx #$80 + beq !+ +#if CONFIG_MEMORY_MODEL_60K + lda #>$F7FF +#elif CONFIG_MEMORY_MODEL_50K + lda #>$D000 +#elif CONFIG_MEMORY_MODEL_46K + lda #>$C000 +#else // CONFIG_MEMORY_MODEL_38K + lda #>$A000 +#endif + skip_2_bytes_trash_nvz +!: + lda #>$8000 + sta MEMSIZ+1 +#if CONFIG_MEMORY_MODEL_60K + cpx #$80 + beq !+ + lda #$FF + skip_2_bytes_trash_nvz +!: + lda #<$00 +#else // CONFIG_MEMORY_MODEL_38K || CONFIG_MEMORY_MODEL_46K || CONFIG_MEMORY_MODEL_50K + lda #<$00 +#endif + sta MEMSIZ+0 + + // XXX move the part above to a more suitable place + + // Initialize other variables by performing NEW + + jsr do_new + + // Print number of bytes free + sec lda MEMSIZ+0 sbc TXTTAB+0 @@ -20,6 +72,5 @@ initmsg_bytes_free: jsr print_integer - // Print rest of the start up message ldx #IDX__STR_BYTES jmp print_packed_misc_str