Yabasic / PS2 (aka Playstation-Basic):
"Programming reference"

Last Changed: 15 jan 2001 21:33

The following is a reference for Yabasic on the PS2. It's almost complete, bar a few descriptions that need a bit of touching up. The aim of this page is to help you find the commands you need, and also to help you see whether or not a function of Yabasic is implemented on the PS2 version of the programming language. Basically, if you don't see a Yabasic command on this page, it's not implemented in Yabasic on the PS2. If I've missed something though, please let me know at a.arum@wxs.nl with PS2: Yabasic Missed Command as the subject title.

General Tips

Get a keyboard. It's not an absolute must, but it's a very strong recommendation. Fortunately, you don't have to buy a specific Playstation keyboard - any USB keyboard for the PC will do. I myself use a Microsoft Internet Pro USB keyboard, but there are much cheaper keyboards around that will work just fine. If you hit on any that don't work fine (even though they all should), then tell me and I'll list the offending keyboard on this site. Other people wanting to get a USB keyboard can then steer clear of that one until Sony includes support for it in a later version. 

If you don't know about programming, read my beginners guide here.

After loading Yabasic, you can remove the demo-disk, which spares you the frantic disc seek that sometimes occurs for quite a while shortly after Yabasic has loaded, and resuming occasionally for no apparent reason  (strongly recommended). 

If you do know about programming, but were used to using all sorts of fancy libraries for user-interface purposes that are currently unavailable on Yabasic PS2, read my sections on programming the likes from scratch. 

Oh, and print this page. ;-)

Legend

In the descriptions below, the following legend is used: 
bold type The (name of the) instruction
arg Argument to the function
( ) Parameters of the function
[] Optional
| Or

Variable Declarations

dim var Tells Yabasic you are going to use the variable var as a numeric
dim var$ Tells Yabasic you are going to use the variable var$ as a string
dim var$(arg) Tells Yabasic you are going to use the variable var$ as an array with size arg.
local var | var$ | var$() Tells Yabasic to use this variable as a local variable only - e.g. a variable that is only valid and remembered inside a sub ... end sub section.
static var | var$ | var$() Tells Yabasic to keep the value of this local variable throughout subsequent calls to the sub ... end sub section. 

Control of Flow Commands

Whenever it says in the following descriptions that you can use an expression, this means you can use =, <, >, >=, <= and () to compose an expression which will return a true or false value. For instance, if the variable a contains a value of five, then all of the next expressions are true: 
a = 5
, a < 6, a > 4, a <= 5, a >= 5, (a < 6) and (a > 5), (a <4) or (a > 4), etc. 

for var = arg to arg [step arg] 
    [...]
next var
The commands between for and next will be repeated for each value of var where var is a range of values between the first arg and the second arg. 

By default, this range consists of all integer values between the first arg and the second arg. Any other range (eg. smaller or greater steps than 1 or negative steps) can be specified by step's arg.

if expression then
    [...]
[elsif expression]
    [...]
[else]
    [...]
endif | fi
Set a condition for the code between if and endif | fi. If the expression is tested true, then the code after then is executed. 

If you specify elseif, which you can do any number of times within an if ... then ... endif condition, then it will test its expression only of none of the preceding conditions had been tested true.

If you specify else, then the commands after else will be executed only if none of the preceding if and elseif expressions have tested true

repeat 
    [...] until(expression)
Repeats the code between repeat and until until expression tests true.
while(expression) 
    
[...] 
wend
Repeats the code between while and wend until expression tests true. Note that the difference between repeat ... until and while ... wend is the place in the loop where the expression is tested. 
label arg | arg$
    [...]
return
[goto | gosub arg | arg$]
Specify a label, either a numeric or a string value, and then jump to that label using a goto or a gosub command. A goto command is a simple jump - the program will continue from label onwards; a gosub command can return from the jump to the label if it encounters a return somewhere in the code following the label

The on ... gosub command can also have a numeric variable as an index for a list of labels to jump to. E.g. to randomly jump to labels one, two, or three, use on int(ran(3))+1 gosub one, two, thee.

sub name(arg, [arg-2], [arg-n])
    [...]
    [return arg]
end sub
Specifies a subroutine or function with name name and n optional parameters arg. The code between and including sub ... end sub is treated as a separate program, to which you can pass variables and receive a modified variable in return. The value that the subroutine returns is specified after the optional return command. 

Using this function you can add your own commands to the Yabasic language within your program. Use this for code that you use or reuse often in your programs, for instance for a routine that draws a menu on the screen or receives a certain string of commands from the controller.

Not all arguments need to be specified, and you can check yourself how many arguments have been passed to the subroutine using the system variable numparams

Text Output

print [using mask$] arg | arg$  , | ; Prints a numeric or string value in Text mode (as opposed to Graphics mode). By default, a newline is added to the printed string or numeric value. By specifying ; you can prevent this newline. With , you can specify a tab instead of the newline. 

With using mask$ you can influence the appearance of numbers. For instance, if you want a value in Euros to be presented according to the EU currency standard (a precision of five digits after the decimal point), you can specify a mask as follows: "##.#####". 

Operators

- Substraction
3-3 = 0
+ Addition
3+3 = 6
* Multiplication
3*3 = 9
/ Division
3/3 = 1
^ Power
3^3 = 3 * 3 * 3 = 27

Mathematical Functions

mod(arg, arg) Remainder
If A is 10 and B is 3, then the result would be 1.
sqr(arg) The square of a number
If arg is 2 then sqr(arg) would be 4
sqrt(arg) The square root of a number
If arg is 2 then sqrt(arg) would be 1.41421
int(arg) Return the integer part of a number, which is what is on the left-hand side of the decimal point. 

If you want this function to round off a variable (e.g. 6.6 -> 7), then add 0.5 to arg before performing the int() function.

frac(arg) Return the fractional part of a number, which is what is on the right-hand side of the decimal point
min(arg, arg) Returns the smaller number of the two
max(arg, arg) Returns the greater number of the two
ran(arg) Returns a random number between 0 and arg

The result is a real number (e.g. 6.3452), so if you only want an integer, then use the int() function as well, e.g. int(ran(19))

If you leave out arg, you get a real number between 0 and 1

abs(arg) Returns the absolute value of a number, which is the number with the minus sign removed (if it was there in the first place)
sig(arg) Returns the sign value of a number, which is -1 if it was negative (had a minus sign before it), or 1 if it was positive (didn’t have a minus sign before it).
sin(arg) Get the sinus value of arg
asin(arg) Get the arcsinus value of arg
cos(arg) Get the cosinus value of arg
acos(arg) Get the arccosinus value of arg
tan(arg) Get the tanges of arg
atan(arg, arg) Get the arctangens of arg. The second arg indicates ... 
log(arg) Get the logarithm value of arg
exp(arg) Get the exponential value of arg
pi Equals the value 3.14159
euler Equals the value 2.17828
hex$(arg) Convert arg to hexadecimal notation
dec(arg$) Convert arg to decimal notation
and(arg, arg) Bitwise 'and' of values arg and arg
or(arg, arg) Bitwise 'or' of values arg and arg
eor(arg, arg) Exclusive 'or' of values arg and arg

Input from the Keyboard

input [prompt$arg | arg$ Get a number or string from the keyboard. The string is accepted into the variable arg or arg$ as soon as the user presses the enter key. A "?" is always printed at the place where the user can type his or her input. 

With prompt$ you can provide a prompt for the user (eg. input "What is your name" name$). This supplied string will then replace the "?", which is good - always more fun to have your own prompt.  

inkey$( [arg] ) Receives a key value from the keyboard. The following keyvalues are recognised
Key or Keyrange Stringvalue returned by Inkey$
a..z ..
A..Z ..
0..9 ..
~, !, @, #, $, etc ..
Insert "ins"
Delete "del"
Home "home"
End "end"
Page Up "scrnup"
Page Down "scrndown"
Tab "tab"
Enter "enter"
Arrow Up "up"
Arrow Down "down"
Arrow Left "left"
Arrow Right "right"
Space " "
BackSpace "backspace"
F1..F10 "f1" .. "f10"

Notice that if you set the keyboard to "US", as for instance I do since in the Netherlands we mainly use "US" keyboards, then you will have trouble spotting the key for "|" and "\", as this has been mapped to "<" and ">", and you won't find "|" and "\" anywhere else on the keyboard either (or at least I didn't). 

In "UK" mode, this problem does not exist. I haven't checked the other language settings ("German", "French"). If you find anything interesting, let me know at a.arum@wxs.nl and head the message PS2: Keyboard Layouts.

Input from the Controller

There are two ways to read from the PS2 controller: using peek and using inkey$. Only through peek can you read button combinations (more than one button pressed at the same time) and button presses from the second controller. Only through inkey$ can you read the position of the PS2's two analog-sticks (though limited to left, up, down and right). 

peek( "port1" | "port2" ) Receives a number from controller port1 or port2 that represents the status of the buttons of that controller. Each button is represented by a bit, as follows
Bit no Value Button
0 1 SELECT
1 2 L3
2 4 R3
3 8 START
4 16 Up
5 32 Right
6 64 Down
7 128 Left
8 256 L2
9 512 R2
10 1024 L1
11 2048 R1
12 4096 Triangle
13 8192 Circle
14 16384 Cross
15 32768 Square
inkey$( [arg] ) Although this is as yet undocumented by Sony, the inkey$ command also reads commands from the first PS2 controller. It is even the only way to read the position of the analogue sticks, albeit in a crude (non-analog) manner, only reading if the stick is pointing up, down, left, or right. But, as they say, something is better than nothing. Here's a full list
Controller Stringvalue returned by Inkey$
Circle "circle"
Square "square"
Triangle "triangle"
Cross "cross"
Up (DPad) "up"
Down (DPad) "down"
Left (DPad) "left"
Right (DPad) "right"
Up (Left Analog) "la_up"
Down (Left Analog) "la_down"
Left (Left Analog) "la_left"
Right (Left Analog) "la_right"
Up (Right Analog) "ra_up"
Down (Right Analog) "ra_down"
Left (Right Analog) "ra_left"
Right (Right Analog) "ra_right"
L1 "l1"
L2 "l2"
R1 "r1"
R2 "r2"
L3 (press LA stick) "l3"
R3 (press RA stick) "r3"
Start "start"
Select "select"

String manipulation

len(arg$) Returns the length, in characters, of arg$.
left$(arg$, arg) Returns arg characters from the left of arg$.
right$(arg$, arg) Returns arg characters from the right of arg$.
mid$(arg$, arg [,arg]) Returns characters from arg$ starting at the position of the first arg. The optional second arg specifies how many characters from the first arg should be taken. If the second arg is left out, all characters from the position indicated by the first arg to the end of the string will be taken. 
str$(arg) Converts a numeric value into a string value. 
val(arg$) Converts a string value into a numeric value.
asc(arg$) Returns the ASCII value of the (first letter of) the string value specified by arg$. An ASCII value is a numeric representation of characters that computers use to store characters in binary code in memory. In this code, pretty much each key you regularly use when typing is represented by a number. 

For instance, the return key is represented by the value 13, the space key is represented by the value 31, and "A" is represented by 65. 

If you want to check the ASCII code of a special key like the return key which you cannot type into a string in your program, then you can also use an escape sequence, as described at the end of this section.

chr$(arg) Returns the string character of the ASCII value specified by arg (see also asc above).  

For instance, "A" is 65, and "a" is 97, so chr$(65) returns "A" and chr$(97) returns "a".

lower$(arg$) Converts arg$ to lower case characters.
upper$(arg$) Converts arg$ to upper case characters.
instr$(arg$,arg$) Tries to find the second arg$ in the first arg$ and if found, returns the position of the first character at which the pattern has been found, searching from left to right. If no match is found, then 0 is returned. 
ltrim$(arg$) Removes spaces at the left side of arg$
rtrim$(arg$) Removes spaces at the right side of arg$
trim$(arg$) Removes spaces at both the left and the right side of arg$
token(arg$, arg$() [,arg$]) Slices a string in parts at a given character or string and puts the sliced bits into an array. By default, token slices a string at the position of a space. 

For instance, if you would supply a string variable containing a regular English sentence as the first arg$ of the token command, and an array as the second arg$() of the token command, then after the command has executed then arg$() will have become an array containing each of the words in the sentence. 

Optionally, you can supply a third argument with which you can indicate other character(s) at which the token command should split your string. Slices that are empty, for instance as a result of two or more adjacent spaces, are ignored and not put into the array.

split(arg$, arg$() [,arg$]) Does the same as token() above, except empty slices are not ignored.  
glob(arg$, mask$) Searches text that matches mask$ within a string. mask$ is a string that may contain the special characters ? and * to indicate wildcards. ? will try to match exactly 1 character, while * will match any character. If the match is found, then glob returns true, else it returns false. Thus "h*o" will for instance match both "hello" and "h2o", but "h?o" will only match "h2o". 
date$ Returns the current date. Consists of weekday (0-6 represent Sunday-Saturday), month (00-12), day of the month (01-31), year (2001), day name (first-three characters, first capitalised) and month name (first-three characters, first capitalised). 

Each is separated by a dash ("-") and an easy way of reading these values separately is by using the token and split functions described above with the "-" as a third argument.   

time$ Returns the current time. Consists of hours (00-24), minutes (00-59), seconds (00-59), and seconds since start of program (0 - ...).

Each is separated by a dash ("-") and an easy way of reading these values separately is by using the token and split functions described above with the "-" as a third argument.    

escape sequences Allow you to enter a special character into a string value that would otherwise be interpreted as a command directly by Yabasic. 

The backslash, for instance, is part of indicating an escape sequence, and if you use it in a string for a different purpose it will still not display the backslash, but try to interpret an escape sequence. But you just want it to be a backslash, and in order to do so, you must use an escape sequence, namely "\\". 

escape sequence result in string
\n newline
\t tabulator
\v vertical tabulator
\b backspace
\r carriage return
\f formfeed
\a alert
\\ "\"
\' \'
\" \"

Graphics Output

open window width, height Opens a graphical window, with width width and height height in pixels. Maximum resolution that can be specified on a PS2 in PAL mode (currently the only supported mode) is a width of 640 pixels and a height of 512 pixels. You can read these maximum values from the system parameters "screenwidth" and "screenheight" (see also below, under system parameters).

This command is required before you can execute any of the other graphics output commands. You can also use the open command as an expression, as it returns true or false depending on whether the open command was successfully executed. 

clear window Clears the content of the currently open window represented by the currently active buffer (0 or 1). Some graphical output commands require you to do a clear screen prior to their execution. The clear screen effectively fills the screen with color 0 (see setrgb). 
close window Closes the currently open window
window origin arg$ Specifies the window origin, where arg$ indicates the origin in two string characters. The first character indicates the horizontal position (l - left, c - centre, r - right), and the second character indicates the vertical position (t - top, c - centre, b - bottom). 

E.g. window origin "lb" sets the window origin to left - bottom. 

Default origin is left - top ("lt").

new curve Starts a new line to fragment
[clear] line to x,y Draws a line to the specified second coordinate. The line will be drawn from the last coordinate used by a prior line to command. 

You can use new curve to indicate you which to start a new line-section, so that there is no line between this and the previous line. 

If you specify clear, you will effectively draw the line in color 0 (the background color), which has the effect of 'clearing' a line. 

[clear] [fill] circle x,y,r Draws a circle with its center at the first coordinate and with r as its radius. clear as above. If you specify fill then the circle will be filled with color 1, otherwise it will only draw the outline.   
[clear] [fill] rectangle x,y to x,y Draws a rectangle with its first coordinate as the top-left corner, and its second coordinate as its bottom-right corner. clear and fill as above.
[clear] [fill] traingle x,y to x,y to x,y Draws a triangle by specifying three coordinates. clear and fill as above.
gtraingle x,y to x,y to x,y Draws a gradient triangle, by specifying three colors and filling the triangle with a gradient that flows from the color of each color to the next (very pretty). The colors can be specified using the setrgb function, as described under that function. 
[clear] dot x,y Plots a single dot at the specified coordinate. 
text x,y,[arg$,]arg$ Places the text in the second arg$ at the coordinates specified by x and y

If the optional first arg$ is supplied, you can indicate the position of the text in relation to the specified coordinate in a two-character string. The first character indicates the horizontal position (l - left, c - centre, r - right), and the second character indicates the vertical position (t - top, c - centre, b - bottom). 

setrgb c,redvalue, greenvalue, bluevalue Sets color c to a 24-bit RGB color value, specifying 8 bit values (0-255) for red, green, and blue. c may be one of the following values.
0 Background color. Doesn't actually change the background color until the next clear screen command has been executed. 
1 Foreground color. The next object drawn will use the new color value.  
2 Secondary foreground color. Is currently only used by gtriangle, for the second corner of the gradient fill.
3 Tertiary foreground color. Is currently only used by gtraingle, for the third corner of the gradient fill. 

Other Graphics

Use the following commands to enhance your graphics. By always drawing in the buffer which is not currently being displayed and switching at well chosen regular intervals, you can make your graphics and animations appear silkily smooth.  

setdispbuf 0 | 1 Sets the currently displayed buffer to one of two buffers ( 0 or 1 ) the next time the television finishes displaying a frame. 
setdrawbuf 0 | 1 Sets the current target for graphical output commands to one of two buffers ( 0 or 1 ).

Read/Write Data

At this time, only in-source data is supported, in the form of read and restore commands. No reading or writing files from memory cards or reading data from CD-ROMs as yet, which is a great pity. I've also found it to be rather slow and try to avoid using these commands, certainly if there are alternatives. 

data Specify data separated by comma values that can be restored to an array through using the restore and read commands
restore arg Where arg is a label heading the data to be restored. Use this prior to read.
read arg | arg$() Counts the number of data items and puts the result into arg. By using arg$(), you can read the data into an array. Use the number you got when using arg to dimension (dim) the array to the correct size, and then use a for ... next loop using 1 and arg to read in all the data.  

Sound and Other

beep The only way to produce any kind of sound at the moment. Nothing spectacular, but something is better than nothing (most of the time)
wait/pause val The program will wait val seconds before continueing. You can also supply a real number of seconds, e.g. 0.3 or 1.25
rem Everything on the same line after a rem statement is considered a remark

System Parameters

The system parameters are variables maintained by Yabasic which reflect certain settings that influence your pogram in some way. You can read and write their content through using the peek and poke commands. 

poke arg$,arg | arg$ Puts value arg or second arg$ into the system variable arg$
peek(arg$) | peek$(arg$) Returns the value of the system variable arg$. If the returned value is a sting value, then you have to use peek$, otherwise you use peek.

The names of the currently supported values follow below. Some of these have been discussed in more detail under commands that bear some kind of relation to these parameters. 

You cannot always change these settings and not all of them are always useful; others, like port1 and port2, are extremely useful. I haven't yet fully indicated which ones you can only read and which ones you can also write - this will follow soon, but of course you can always try it out and see what happens. ;-)

"fontheight" Displays the current fontheight for characters outputted using the text function. Currently this value is always 10
"infolevel" Displays the level of debugging that Yabasic is currently supporting. 
"library" Displays the current library that is being used with Yabasic. At the moment this parameter is read-only and always returns the value "main"
"os" Displays the operating system that Yabasic is running on ("Playstation 2" if you run this command on PS2)

This can be very useful if you want to write a program that should also run on other operating systems, like Unix or Windows, and you want to test whether or not you can use a particular function that is only supported on that platform, or supported in a limited or extended way.  

"port1" Reflects the status of the buttons on the controller in port 1
"port2" Reflects the status of the buttons on the controller in port 2
"read_controls" Displays whether or not input commands should read control commands or not (like backspace and enter)
"textalign" Reflects the current text-alignment
"screenwidth" Displays the current screenwidth. On PAL, this will be 640 pixels. You can use this system parameter to anticipate future upgrades of Yabasic on the PS/2 that will support different screen resolutions or write programs that also work on other platforms supporting different screen resolutions.
"screenheight" Displays the current screenheight. On PAL, this will be 512 pixels. See also "screenwidth", above. 
"version" Reflects the version of the Yabasic program (currently 2.64). 

This can be very useful if you want to make sure your Yabasic program is running on a version of the Yabasic interpreter that supports the commands you are using in your program.

Sources

www.playstation-basic.com
Yabasic PS2 internal help-file
www.yabasic.de

(c) 2001 Arwin van Arum

Sony and Playstation 2 are registered trademarks owned by Sony Inc.