Skip to content

Instantly share code, notes, and snippets.

@logiclrd
Last active June 26, 2026 15:05
Show Gist options
  • Select an option

  • Save logiclrd/bdf326395bd6c719e9006b7171ac8bda to your computer and use it in GitHub Desktop.

Select an option

Save logiclrd/bdf326395bd6c719e9006b7171ac8bda to your computer and use it in GitHub Desktop.
C64 VARPTR
10 dim r%(25)
11 input "index"; y%
12 input "value"; r%(y%)
13 print "okay, r("; y%; ") has been set to "; r%(y%)
20 x$ = "r": rem the other parameter is y%, already populated
21 gosub 90: print "return value: varptr("; x$; "("; y%; ")) = "; vp
22 if vp < 0 then goto 27
23 print "reading element "; x$; "("; y%; ") directly"
24 print "from memory:"
25 print peek(vp) + peek(vp + 1) * 256
26 end
27 print "could not find array"
28 end
85 rem varptr(): integer array name (1 letter) in x$, index in y%
86 rem returns vp, which is -1 if the array was not found
87 rem nb: if you create new variables after varptr() returns, the
88 rem value ceases to be meaningful because the variable and
89 rem array tables will be moved around in memory
90 rem precalculate name bytes
91 b1% = asc(x$) or 128: b2% = 128: rem high bit for integers
92 if len(x$) = 1 then goto 94
93 b2% = b2% or asc(mid$(x$, 2, 1))
94 c1% = 0: c2% = 0: as = 0: nd% = 0: vp = -1
95 rem find arytab, make sure all necessary variables are created first
96 se = peek(49) + peek(50) * 256: ar = peek(47) + peek(48) * 256
97 rem find the target array
98 c1% = peek(ar)
99 c2% = peek(ar + 1)
100 if c1% = b1% and c2% = b2% then goto 110
101 rem no match, skip this array
102 as = peek(ar + 2) + peek(ar + 3) * 256
103 nd% = peek(ar + 4)
104 ar = ar + 5 + 2 * nd% + as
105 if ar < se then goto 98
106 rem didn't find it, vp is already -1
107 return
109 rem array found, find data start and return pointer to element
110 nd% = peek(ar + 4)
111 vp = ar + 6 + (2 * nd%) + (2 * y%)
112 return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment