Code Snippets: Difference between revisions

From F256 Foenix
Jump to navigationJump to search
(Added Renumbering code)
No edit summary
Line 63: Line 63:
Here's a down and dirty basic routine you can use to renumber your program.  
Here's a down and dirty basic routine you can use to renumber your program.  


 
<code>
1000 rem "SuperBASIC renum routine"
1000 rem "SuperBASIC renum routine"<br>
 
1010 a=$2000:b=1000:c=10<br>
1010 a=$2000:b=1000:c=10
1020 while peek(a)<>0<br>
 
1030 print peekw(a+1);" --> ";b<br>
1020 while peek(a)<>0
1040 pokew a+1,b<br>
 
1050 b=b+c<br>
1030 print peekw(a+1);" --> ";b
1060 a=a+peek(a)<br>
 
1070 wend <br>
1040 pokew a+1,b
</code>
 
1050 b=b+c
 
1060 a=a+peek(a)
 
1070 wend  
 


'a' is the memory location of the basic line
'a' is the memory location of the basic line
'b' is the new line number
'b' is the new line number
'c' is the interval between new line numbers
'c' is the interval between new line numbers


This little program just goes around and changes the word value that hold line numbers. This does not fix any goto or gosub statements, you'll have to update those on your own. (Use at your own risk).
This little program just goes around and changes the word value that hold line numbers. This does not fix any GOTO or GOSUB statements, you'll have to update those on your own. (Use at your own risk).
__FORCETOC__
__FORCETOC__

Revision as of 18:44, 6 June 2025

Index of SUPERBASIC Code Snippets (Hopefully this will be sorted by Topic when they grow)

Poking and Peeking Memory in a different Bank

Contributed by Ernesto (econtreras in Discord)

SUPERBASIC uses a lot of the memory available, so when you want to store data on a different Bank you want to POKE or PEEK outside the normal memory range, to do so you can use the following PROCEDURES. PLease note that these are not by any means a fast way to do it since we reconfigure the memory LUT so that the memory under the registers location ($C000-$DF00) points to the address that we want to poke or peek on the fly, after that we get or set the content of the memory address and restore the segments so that the Kernel does not blow up after returning. Contrary to what I initially believed it works!.

REM "XPEEK - value is stored in peekvalue variable"
proc xpeek(addr)
local block:block=addr\8192:local prevblock
local offset:offset=addr&$1fff
?0=179:prevblock=?$E:?$E=block:?1=4
peekvalue=peek($C000+offset)
?1=0:?$E=prevblock
endproc

REM "XPOKE"
proc xpoke(addr,value)
local block:block=addr\8192:local prevblock
local offset:offset=addr&$1fff
?0=179:prevblock=?$E:?$E=block:?1=4
?($C000+offset)=value
?1=0:?$E=prevblock
endproc

Print Text at X,Y Position

Contributed by Ernesto (econtreras in Discord)

Chances are that you need to print text at a specific position in the screen, SUPERBASIC doesn't have a LOCATE command, so the best next thing is to use this routine to print at a certain X,Y coordinate onscreen. Oh you are not not using an 80 column screen mode?, don't worry the routine will compensate automagically!

proc printat(x,y,a$)
col=(?$D001)&2:if col=0 then col=1
local pos:pos=x+y*80\col:?1=2:rem "Set I/O to text memory"
for c=0 to len(a$)-1:?(pos+c+$C000)=asc(mid$(a$,c+1,1)):next
?1=0
endproc

Square Root

Contributed by Ernesto (econtreras in Discord)

You might need to calculate the square root of a number, it's very useful for a few algorithms, be aware that it will only work on numbers below 32768, square root is returned on the variable - guess#

REM "Newton's Method of Calculating Square Root"
proc sqr(n)
n#=n:guess#=n#
while (0.001<abs(guess#*guess#-n#))
guess#=guess#+n/guess#)/2
wend
endproc

Loading and Launching another SUPERBASIC program

Contributed by Ernesto (econtreras in Discord)

Let's say that you want to do a menu or intro in a SUPERBASIC program and then after it plays out or you select something this program loads and executes another SUPERBASIC program!, sounds impossible?, no it's not, it's actually easy, we can use the functionality of SUPERBASIC that allows you to create your BASIC program (text file) on your PC and transfer it and execute it in your Foenix computer. (I bet you didn't know you could do that!). Use these two lines to load and execute your program, obviously this erases the current program from memory.

memcopy $28000,$8000 poke 0: rem "Clear transfer area for new program"
a$="Program.bas":bload a$,$28000:xgo

Renumbering a Program

Contributed by Mike (mcassera in Discord)

Here's a down and dirty basic routine you can use to renumber your program.

1000 rem "SuperBASIC renum routine"
1010 a=$2000:b=1000:c=10
1020 while peek(a)<>0
1030 print peekw(a+1);" --> ";b
1040 pokew a+1,b
1050 b=b+c
1060 a=a+peek(a)
1070 wend

'a' is the memory location of the basic line 'b' is the new line number 'c' is the interval between new line numbers

This little program just goes around and changes the word value that hold line numbers. This does not fix any GOTO or GOSUB statements, you'll have to update those on your own. (Use at your own risk).