Code Snippets: Difference between revisions
Eacontrerasd (talk | contribs) |
(Added Sin of an angle) |
||
Line 48: | Line 48: | ||
wend<br> | wend<br> | ||
endproc</code> | endproc</code> | ||
===== Calculate SIN of an angle ===== | |||
<small>Contributed by Raúl SQ</small> | |||
<code>50 input "Enter an angle in degrees (float): ";d#</code> | |||
<code>100 sin(d#)</code> | |||
<code>150 end</code> | |||
<code>1200 proc sin(s#)</code> | |||
<code>1250 deg_to_rad#=0.01745329</code> | |||
<code>1260 s#=s#*deg_to_rad#</code> | |||
<code>1300 x=6: rem "Factorial of 3"</code> | |||
<code>1400 y=120: rem "Factorial of 5"</code> | |||
<code>1500 z=5040: rem "Factorial of 7"</code> | |||
<code>1600 pow2#=s#*s#:q#=pow2#*s#: rem "Power of 3"</code> | |||
<code>1700 r#=q#*pow2#: rem "Power of 5"</code> | |||
<code>1800 t#=r#*pow2#: rem "Power of 7"</code> | |||
<code>1900 sine#=s#-(q#/x)+(r#/y)-(t#/z)</code> | |||
<code>2000 print sine#</code> | |||
<code>2100 endproc</code> | |||
===== Loading and Launching another SUPERBASIC program ===== | ===== Loading and Launching another SUPERBASIC program ===== |
Revision as of 01:14, 9 August 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
Calculate SIN of an angle
Contributed by Raúl SQ
50 input "Enter an angle in degrees (float): ";d#
100 sin(d#)
150 end
1200 proc sin(s#)
1250 deg_to_rad#=0.01745329
1260 s#=s#*deg_to_rad#
1300 x=6: rem "Factorial of 3"
1400 y=120: rem "Factorial of 5"
1500 z=5040: rem "Factorial of 7"
1600 pow2#=s#*s#:q#=pow2#*s#: rem "Power of 3"
1700 r#=q#*pow2#: rem "Power of 5"
1800 t#=r#*pow2#: rem "Power of 7"
1900 sine#=s#-(q#/x)+(r#/y)-(t#/z)
2000 print sine#
2100 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).
Getting a Directory from BASIC
Contributed by Mike (mcassera in Discord)
I had written an ML program to work with BASIC to dump the directory into memory so I could then copy the filenames into an array. I've pulled it out here for a simple basic program you could use in your program. I've only extracted the names because at the time I didn't need file sizes. The basic program just prints the directory to the screen but you could modify it to place the names in an array
The machine language routine can be obtained from the following link:
https://github.com/mcassera/get_dir/blob/5bb017985793c89455746616493f092ef6a47045/get_dir.bin
The sample BASIC code is here:
10 bload "get_dir.bin",$7F00
15 buffer=$7700:buffloc=$7FBB
20 directory(buffer,0,"")
30 for n=buffer to peekw(buffloc)
40 cprint chr$(peek(n));
50 if peek(n)=0 then print
60 next
70 print
80 end
1000 proc directory(loc,len,path$)
1010 buffloc=$7FBB
1020 pathlength=$7FBD
1030 pathloc=$7FBE
1040 getdir=$7F00
1050 pokew buffloc,loc
1060 poke pathlength,len
1070 for n=0 to len
1080 poke pathloc+n,asc(mid$(path$,n+1,1))
1090 next
1100 call getdir
1120 endproc
Code Explanation
- The basic program loads the binary and sets aside 2k of ram below BASIC rom for the directory buffer.
- Line 20 is where you call the directory routine. You send the buffer info (already set), the length of your path (if not reading the main directory) and the path.
- So if I was reading a directory called 'bitmaps' the command would change to : directory(buffer,7,"bitmaps")
- Lines 30 to 60 print the directory from memory. This is where you could read the data into an array. Files are separated with a zero byte hence line 50.
- The file names are stored in memory $7700 to $7eff. You can change the buffer location by changing the value of buffer in line 15, but 2k of ram will be cleared out after that address.
- The rest of the variables are (buffloc, pathlength,pathloc,getdir) are used by the ML routine and will break it if changed