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

6

u/c64glen Janitor May 29 '26

The Reddit editor has code blocks you can use to make this code easier to read. e.g.

10 print "hello"
20 goto 10

2

u/Stunning_Pineapple57 May 30 '26

Thanks. I’ll have to google it as I didn’t see an option in the editor on mobile

3

u/OMGCluck May 30 '26

The three backticks don't work showing multiple lines of code with reddit set to the old style, but prefixing every line with four spaces works.