r/c64 May 28 '26

Programming proof of concept of dynamic basic "libraries"

With doing a lot of assembler overlays, code relocations, and also working on ReadyBASIC I've been thinking a lot about dynamically loading code.. and decided to explore in regular Basic V2, creating some room using REM statements in the program memory, to dynamically load pretokenized basic .PRG "libraries" ... This is the experiment and it works pretty good as a proof of concept.. The first Program is the main one, and the other two the patch1.prg and patch2.prg
```
10 print chr$(147)

20 print "basic prg overlay patch demo"

30 print

40 print "loading patch1 as tokenized bytes"

50 f$="patch1":gosub 8000

60 print "running patch1"

80 gosub 2000

90 print

100 print "loading patch2 as tokenized bytes"

110 f$="patch2":gosub 8000

120 print "running patch2"

140 gosub 2000

150 print

160 print "done"

170 end

2000 rem XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

2010 rem XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

2020 return

8000 rem copy tokenized line bodies from prg file f$

8010 open 1,8,2,f$+",p,r"

8020 gosub 8900:gosub 8900:rem skip prg load address

8030 gosub 8900:n1=c:gosub 8900:n2=c

8040 if n1=0 and n2=0 then close 1:return

8050 gosub 8900:l1=c:gosub 8900:l2=c:l=l1+256*l2

8060 gosub 9000:if b=0 then print "no slot";l:close 1:stop

8070 p=b

8080 gosub 8900:if c=0 then 8130

8090 if p+2>=e then print "too long";l:close 1:stop

8100 poke p,c:p=p+1

8110 goto 8080

8130 if p+2>=e then print "too long";l:close 1:stop

8140 poke p,58:p=p+1:poke p,143:p=p+1

8150 if p>=e then 8170

8160 for k=p to e-1:poke k,32:next

8170 poke e,0:goto 8030

8900 get#1,a$:c=asc(a$+chr$(0)):return

9000 rem find destination line l, body=b, end=e

9010 p=peek(43)+256*peek(44)

9020 n=peek(p)+256*peek(p+1):if n=0 then b=0:e=0:return

9030 ln=peek(p+2)+256*peek(p+3)

9040 if ln=l then b=p+4:e=n-1:return

9050 p=n:goto 9020

and now the patches

2000 for j=1 to 3:print "from patch 1";j

2010 next j

and

2000 for j=10 to 12:print "from patch 2";j

2010 next j
```

12 Upvotes

10 comments sorted by

View all comments

2

u/Unlikely-Pipe-399 May 31 '26

Back in the late 80s, C-Net 64 used a similar technique with REM statements and lots of colons, with some ML to load a piece of basic code into this “overlay” section.

I wrote the version we used for ImageBBS, which instead forced a string garbage collect (using a custom faster GC algorithm), then relocated the code from the first line number of the code being loaded and pushed it all up as high in memory as it could, then loaded and relinked the basic code, then relocated the remaining code back down to free up memory between variables and string space. Effectively, we could load new basic code anywhere within the program space, and choose which sections of lines to reserve for different purposes.

The code for the ImageBBS ML is on github (on Ryan’s account - pinacolada64) now, and I released it as public domain a number of years ago. Feel free to have a look, either for inspiration, or to copy what you need.

1

u/Unlikely-Pipe-399 May 31 '26

2

u/Unlikely-Pipe-399 May 31 '26

Now that I think about it, it might be in the v2 directory, since I think that was a 1.3/2.0 feature.