BASIC Preservation after ,8,1

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

BASIC Preservation after ,8,1

Post by chysn »

I have a utility that I'm loading into high memory with LOAD ,8,1

This, as we all know, messes with some BASIC end-of-things pointers and causes out of memory errors and other ill effects. Traditionally, the user is expected to fix this with NEW after loading.

I'd like the user's BASIC program, if any, to remain untouched, so the utility can be loaded without trepidation or extra steps. My current solution is the code below, which scans for three consecutive bytes and sets start of variables, start of arrays, and end of arrays to this address.

This code seems to work, but it seems like the kind of thing that there might be a BASIC routine for, instead of doing it the hard way. Is this the most efficient way to accomplish this task? I'm willing to put 48 bytes into this, but, you know...

Code: Select all

Install:    lda IGONE+1         ; If the wedge is aleady installed, skip
            cmp #>main          ;   the memory adjustment. Otherwise, set BASIC
            beq installed       ;   pointers as a courtesy
            lda $2b             ; Copy start-of-basic to start-of-variables
            sta $2d             ;   to be the starting point for search
            lda $2c             ;   ,,
            sta $2e             ;   ,,
            ldy #$00
reset_c0c:  ldx #$00            ; Reset consecutive-zero count
adv_sov:    lda ($2d),y
            inc $2d             ; Advance start-of-variables pointer after
            bne check_0         ;   reading, because we want the final value to
            inc $2e             ;   be the third zero location + 1
check_0:    cmp #$00            ; If the current value is not a zero, then
            bne reset_c0c       ;   reset the consecutive-zero count
            inx                 ; A zero was found
            cpx #$03            ; Is it the third?
            bne adv_sov         ; If not, search the next character
            lda $2d             ; Copy the newly-found start-of-variables
            sta $2f             ;   into end-of-everthing-elses
            sta $31             ;   ,,
            lda $2e             ;   ,,
            sta $30             ;   ,,
            sta $32             ;   ,,
installed:  etc...
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: BASIC Preservation after ,8,1

Post by Mike »

Try this:

Code: Select all

.0140  A9 01     LDA #$01
.0142  A8        TAY
.0143  91 2B     STA ($2B),Y
.0145  20 33 C5  JSR $C533
.0148  8A        TXA
.0149  69 02     ADC #$02
.014B  85 2D     STA $2D
.014D  A5 23     LDA $23
.014F  20 55 C6  JSR $C655
.0152  4C 74 C4  JMP $C474
http://www.sleepingelephant.com/ipw-web ... 3&start=15

... ;)
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: BASIC Preservation after ,8,1

Post by chysn »

Nice, thanks! So follow the BASIC program line links to the end and reset the pointers based on that.
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: BASIC Preservation after ,8,1

Post by chysn »

A side effect of that code is that it un-NEWs the BASIC program if it was NEWed, so I'm playing with a modification:

Code: Select all

Install:    ldy #$01 
            lda ($2b),y
            sta $07    ; originally pha
            tya 
            sta ($2b),y 
            jsr $c533 
            txa 
            adc #$02
            sta $2d 
            lda $23  
            jsr $c655
            lda $07    ; originally pla
            bne installed
            ldy #$03
-loop:      sta ($2b),y
            dey
            bpl loop
installed: etc...
My preference would have been to stash the ($2b),y byte on the stack instead of in $07, but somewhere along the line in those BASIC calls, there's a byte getting put on the stack that's throwing pha/pla off. When the program is NEWed, I'm pushing $00 onto the stack and getting back... something else.
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: BASIC Preservation after ,8,1

Post by Mike »

You can just omit vetting the first link pointer, which then leads to an early exit of JSR $C533 when there's no BASIC program.

In that case however X does not contain the value of $22 used for (re-)initialising $2D, so you'd proceed as follows:

Code: Select all

 JSR $C533 ; re-link BASIC program, establish null link ptr. in $22/$23
;CLC
 LDA $22
 ADC #$02
 STA $2D
 LDA $23
 JSR $C655 ; restore $2E, reset TXTPTR, CLR (<- purge stack!)

[... rest of initialising routine ...]

 JMP $C474 ; restart BASIC
That being said, the L command in a functioning (ho-hum) monitor usually does not disturb the BASIC pointers, which makes this largely a non-issue (provided the monitor is resident and doesn't need to be loaded itself with ",8,1").
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: BASIC Preservation after ,8,1

Post by chysn »

That seems to have done the trick.

Thanks again!
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: BASIC Preservation after ,8,1

Post by chysn »

There's a really neat quality to this code, which is that it allows one to easily place BASIC programs anywhere. For example, to free up 3K of memory expansion at $0400 and basically turn a +3K VIC into an unexpanded VIC, I can do

Code: Select all

POKE 44,16
SYS 40960 (the location of my utility initializer)
Since I plan on my medium-of-choice for the foreseeable future being "unexpanded VIC," it's cool to be able to partition off BASIC memory. It's like a RAM disk. I have to watch out for the top, still, but the stakes are low enough that it's not a huge concern.

I might even add a utility to my EPROM suite that handles this banking with a menu, in which case I'll set the top pointers, too.
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: BASIC Preservation after ,8,1

Post by Mike »

That method easily runs into problems as you're calling the BASIC re-linker with essentially uninitialised memory at the new place.

Especially, when you're unlucky and there are more than 255 non-0 bytes at the new BASIC start, the re-linker will hang.

The canonical method of locking away BASIC memory at both ends normally involves setting 641/642 (RAMBOT) and 643/644 (RAMTOP) to new values, and then entering the KERNAL reset routine behind JSR RAMTAS (which would otherwise set RAMBOT and RAMTOP again from available physical memory), at $FD32.

To temporarily 'unexpand' the VIC-20 from any plugged-in RAM expansion, you'd do:

POKE642,16:POKE644,30:POKE648,30:SYS64818

... which not only sets RAMBOT and RAMTOP appropriately (assuming their low-bytes are 0), but also places screen memory and colour RAM at $1E00 and $9600, respectively.

That method can be automated, so one doesn't need to remember that incantation above. Try my 3k.prg with LOAD"...",8,1. :wink:
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: BASIC Preservation after ,8,1

Post by chysn »

Mike wrote: Wed May 20, 2020 10:15 am POKE642,16:POKE644,30:POKE648,30:SYS64818
That's quite useful! Especially the part about putting the screen back.
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
Post Reply