PX-BASIC
The PX-42F calculator's BASIC, based on Tiny BASIC, with the calculator's own 34-digit decimal numbers.
1. What it is
PX-BASIC turns the PX-42F into a small computer you program in BASIC, the language of the first home computers. It follows Palo Alto Tiny BASIC, from 1976: numbered lines, PRINT, INPUT, IF, GOTO, GOSUB, FOR. To that it adds the calculator itself:
- Its numbers. 34-digit decimals, from the library Plus42 computes with.
0.1+0.2is exactly0.3, andSIN,LOGandSQRhave the calculator's precision. - Its screen, keys and speaker. Lines and points, the keys as input, the calculator's tones.
- Its drive. Programs are saved as text files that a computer can open too.
2. Getting started
Installing
- Make sure the calculator's firmware is the PX-42F-Plus with app API version 3 or later.
- On the calculator, open Settings → USB Drive → Connect to Computer.
- Copy
BASIC.binintoBIN/APPSon the drive namedPX-42F. From the project folder,make installdoes it. - Optionally, copy the example programs (
.BASfiles, in the project'ssim/drive/BIN/APPS/BASIC/) intoBIN/APPS/BASICon the drive. - Eject the drive and press EXIT on the calculator.
Starting and leaving
Start it from Settings → Games & Apps → Apps → BASIC. It shows how much room there is for a program, then the prompt, >.
To leave, press ON at the prompt. The status bar asks LEAVE BASIC? ENTER=YES: ENTER (or ON again) goes back to the list of apps, and any other key stays in BASIC. Held for 2 seconds, ON turns the calculator off without asking, and the Sleep Timer does too.
The program you were working on is saved when you leave, as AUTOSAVE.BAS, and comes back the next time you start BASIC.
Your first program
BASIC keeps lines that start with a number, in number order, as the program. A line without a number runs at once. Type these three lines, pressing ENTER after each. Use the keyword map (SHIFT ▼, below) for FOR, PRINT and NEXT, and SHIFT before a key for one digit or symbol, or SHIFT ENTER (ALPHA) twice for NUM:
10 FOR I=1 TO 5
20 PRINT I,SQR(I)
30 NEXT
Then type RUN (or choose it from the keyword map): it prints each number and its square root. LIST shows the program again.
3. Typing
The status bar
The top right of the screen says what the keys do. The prompt, Edit mode and the GTO prompt all show the mode there:
| It says | Meaning |
|---|---|
[A] | ABC: the keys type capital letters. Every line typed at the prompt starts here. |
[a] | abc: the keys type small letters, for text in quotes. BASIC reads its words and variables as capitals. |
[#] | NUM: the keys type digits and symbols. A program's INPUT starts in NUM: it wants a number. |
SHIFT (next to it) | SHIFT is on: ENTER goes to the next mode (ALPHA); another key types from NUM once, or from ABC if you're in NUM. |
RUN | A program is running. R/S stops it. |
ABC and abc: letters
The letters are where the calculator's ALPHA keyboard has them, as on the DM42: A to F on the top row, G to L on the second, M N O next to ENTER, then P to Z, row by row. = is after Z, and the bottom row types : . ? and a space.
NUM: digits and symbols
The digits and + − × ÷ are where they're printed (* and / for times and divide), with . and a space on the bottom row. The top rows type BASIC's symbols. E, on the calculator's E key, writes a number's exponent: 2E-3.
The other keys
| Key | Does |
|---|---|
| SHIFT ENTER | ALPHA, as printed below ENTER: the next mode, ABC, abc, NUM and round again. |
| SHIFT | With any other key, types from NUM once, or from ABC if you're in NUM: SHIFT 7 types 7 in ABC, SHIFT Σ+ types A in NUM. |
| ▲ | At the prompt: the last command, again the one before that, back through the last 8. After the oldest it stays there and plays the error sound. |
| ▼ | On an empty line: List mode (section 4). On a command ▲ brought back: one newer, then an empty line. |
| ▲ ▼ in a line | Once you've changed a line at the prompt, or in Edit mode: move the cursor left and right. ▼ at the end of the line completes the keyword being typed, like TAB on the terminal, and lists the words when several fit: PR becomes PRINT . |
| SHIFT ▼ | The keyword map (below). |
| SHIFT ▲ | List mode, from any line at the prompt. What you've typed is still there when you come back. |
| ← | Deletes the last character. SHIFT ← deletes the whole line. |
| ENTER | Enters the line. |
| R/S | While a program runs, or waits in INPUT: stops it. Typing: ? in ABC, a space in NUM. |
| SHIFT R/S | At the prompt: RUN. It runs the program at once, in place of whatever is typed on the line. |
| ON | While a program runs or waits in INPUT: stops it, as R/S does. At the prompt: asks before leaving BASIC. Held 2 seconds: turns the calculator off. |
The keyword map
Typing RETURN letter by letter takes six keys. SHIFT ▼ shows a map of BASIC's words laid over the keys: press a key to type its word, with the space or ( after it. The words are in eight pages by kind, and in alphabetical order on each page, reading along the rows. ▼ shows the next page and ▲ the one before (the page number is in the top right corner); the pages go round, so ▼ on the last page shows the first, and ▲ on the first shows the last. ON (shown as BACK), or a key with no word on it (an empty box), goes back without typing anything.
| Page | Words |
|---|---|
| 1. Statements | DIM END FOR GOSUB GOTO IF INPUT LET NEXT PRINT REM RETURN STEP STOP THEN TO |
| 2. Functions | ABS( ACS( ASN( ATN( COS( EXP( INT( LOG( LOG10( PI RND( SGN( SIN( SQR( TAN( |
| 3. Strings | $ ASC( CHR$( INKEY$ INSTR( LEFT$( LEN( MID$( RIGHT$( STR$( VAL( |
| 4. Input / Output | ? AT INKEY$ INPUT KEY() PRINT |
| 5. Graphics | BOX CIRCLE CLS ELLIPSE LINE PEN POLYGON PSET |
| 6. Sound | MIDI() PLAYMIDI PLAYWAV STOPMIDI STOPWAV TONE |
| 7. System | CMDS DEG DIGITS ERASE EXIT FILES HELP LIST LOAD MEM NEW RAD RUN SAVE SIZE VER WAIT |
| 8. Operators | @( AND MOD NOT OR |
SHIFT R/S runs the program without the map.
Typing from a computer
With the calculator connected by USB, open its console port ("PX-42F Console") in any terminal program; the speed setting doesn't matter. A Mac lists it with ls /dev/cu.usbmodem* (the first of two); screen /dev/cu.usbmodem2101 opens it. Then:
- What you type there goes to BASIC as if typed on the keys, in any case: lowercase becomes uppercase, except inside quotes.
- Paste a whole program: each line goes in as typed.
- Everything the screen shows also goes to the terminal, so a long listing can be read there.
- Ctrl-C stops a running program, as R/S does; at the prompt it clears the line.
- The arrow keys work as you'd expect: ← → move in the line, Home and End go to its ends, Delete deletes under the cursor, ↑ ↓ step through the history. Ctrl-A Ctrl-E Ctrl-B Ctrl-F Ctrl-P Ctrl-N and Ctrl-U do the same, for terminals that don't send the arrows.
- TAB completes the keyword being typed, as in a Linux shell:
PRTAB givesPRINT,SQTAB givesSQR(. When several words fit, it goes as far as they agree (STObecomesSTOP); when that adds nothing, it lists them. Not inside quotes. A TAB inside a pasted program is a space. - Editing the middle of a line redraws it with ANSI codes (carriage return, clear to the end of the line, cursor back): any ordinary terminal handles them. If CoolTerm shows odd characters or never sends the arrows or Ctrl-C, set it to Raw Mode (Connection → Options → Terminal), which sends each key at once.
4. Writing a program
- A line with a number (1 to 32767) goes into the program. Lines are kept in number order, whatever order they're typed in. Number them in tens (10, 20, 30) to leave room for lines in between.
- The same number again replaces that line. The number alone deletes it.
- A line without a number runs at once:
PRINT 2^64, orRUN. - Several statements fit on one line, with
:between them:10 X=1:Y=2:PRINT X+Y. - A line holds up to 80 characters, more than the screen's 31: the text wraps.
| Command | Does |
|---|---|
RUN | Runs the program from its first line, with every variable set to 0. SHIFT R/S does the same without typing it; so does the R/S key's place in the keyword map, which types RUN for ENTER. |
LIST | Shows the whole program. LIST 100: from line 100 on. LIST 100,200: lines 100 to 200. R/S stops a long listing. |
NEW | Erases the program and the variables. |
GOTO 100 | Typed at the prompt: runs from line 100 without clearing the variables. |
Editing the program
You don't have to retype a line to change it. On the calculator, ▼ at an empty prompt or SHIFT ▲ at any prompt opens List mode. It works in two steps, on one screen:
- List mode. The program is shown a line to a row, the current line highlighted, and the header says
LIST. It opens on the line you last entered, or on the first line. ▲ and ▼ move the highlight, and the list scrolls; SHIFT with them moves a screenful. The list goes round: ▲ on the first line goes to the last, and ▼ on the last goes to the first. To go to a line, press SHIFT GTO (SHIFT XEQ), its number and ENTER. With no line of that number you get the nearest one, and of two as near the earlier one. ← deletes a digit, and EXIT cancels. SHIFT ← deletes the highlighted line: the header asksDELETE 20? ENTER=YES, ENTER deletes it and any other key keeps it. ENTER edits the highlighted line. EXIT (the ON key) goes back to the prompt. - Edit mode. The line is edited where it is, in the list: all of its text, as many rows as it needs, with the rest of the program still shown around it. The header says
EDITand what the keys do, with the mode at the right. ▲ moves the cursor left and ▼ right (the cursor is the white block). What you type goes in at the cursor; ← deletes the character before it; SHIFT ← clears the line; SHIFT ▼ is the keyword map; SHIFT ENTER goes to the next mode (the header then shows[a], or[#]).- ENTER accepts the change: the line replaces the old one.
- EXIT cancels: the line stays as it was.
SQR.RUN shows the change.- A new line from an old one: change the number while editing. The edited line goes in under the new number, and the old line stays.
- Deleting a line: edit it down to just its number and press ENTER. The line is removed, and the highlight moves to its neighbour.
- A line with no number (the number edited away) is dropped as if discarded: it would run as a command, which isn't what you meant.
At the prompt, ▲ brings back the last commands, for running them again, as the terminal’s up arrow does. To change one first, press ← or type something: from then on the arrows move the cursor. At a program's INPUT, ▲ and ▼ only move in what you've typed.
INPUT. ^ shows as ↑, the HP-42S's own character.BREAK IN 10); LIST shows it.5. Numbers
Every number is a decimal of up to 34 significant digits, from ±10−6143 to ±106144, the same numbers Plus42 computes with. Because they're decimal, the sums that go wrong in most computer languages come out right: 0.1+0.2 is exactly 0.3. A fraction with no exact decimal still rounds, as on any calculator: 1/3 is 0.333…3, 34 threes.
How PRINT shows them
- Rounded to 12 significant digits, like the HP-42S's display.
DIGITS nchanges that, from 1 to 34. AfterDIGITS 34,PRINT SQR(2)shows 1.414213562373095048801688724209698. - No zeros at the end:
2.50prints as2.5. - With
Ewhen the number is too large for the digits, or below 0.000001:1.5E-7,6.02214076E23. - The number is kept to all 34 digits;
DIGITSonly changes how it's shown.
Writing them
12, -3.5, .25, 2E-3, 6.02214076E23. An E is the exponent only with digits after it, so 2*E is two times the variable E.
Variables and the array
- Variables: 26,
AtoZ, one letter each. They start at 0, andRUNsets them to 0. - The array:
@(0)to@(1023), for lists and tables:@(I)=I*I. - Your own arrays:
DIMmakes lists and tables with one or two indices, named by a letter:DIM A(10),DIM M(5,5). See Arrays below.
6. Expressions and functions
| Operators, the first done first | Meaning |
|---|---|
( ) | grouping |
^ | power, right to left: 2^3^2 is 29. On the screen it shows as ↑. |
- | minus: -2^2 is −4 |
* / MOD | times, divided by, and the remainder. a MOD b has the sign of b, as the HP-42S's MOD: -7 MOD 3 is 2. |
+ - | plus, minus |
= <> < > <= >= | comparisons: 1 if true, 0 if false |
NOT | 1 if 0, else 0 |
AND | 1 if both are true (not 0) |
OR | 1 if either is true |
| Function | Gives |
|---|---|
SIN(x) COS(x) TAN(x) | trigonometry, in radians, or in degrees after DEG |
ASN(x) ACS(x) ATN(x) | arcsine, arccosine, arctangent, in the same unit |
SQR(x) | square root |
EXP(x), LOG(x), LOG10(x) | ex, the natural logarithm, the base-10 logarithm |
ABS(x), SGN(x) | without its sign; −1, 0 or 1 |
INT(x) | the whole number at or below x: INT(-2.5) is −3 |
RND(n) | a random whole number from 1 to n. RND(0): a random fraction from 0 to 1. |
PI | π to 34 digits |
KEY() | a key pressed while the program runs (section 8) |
MIDI() | 1 while a song or a WAV plays, otherwise 0 |
SIZE | bytes left for the program |
Arrays: DIM
DIM A(n) makes a list of numbers, A(0) to A(n). DIM M(r,c) makes a table: M(0,0) to M(r,c), so DIM M(2,3) has 3 rows of 4. DIM S$(n) makes a list of strings, S$(0) to S$(n). Several can be made in one statement: DIM A(9), M(4,4), N$(20).
10 DIM M(2,2), N$(2)
20 FOR I=0 TO 2:FOR J=0 TO 2
30 M(I,J)=I*3+J
40 NEXT J:NEXT I
50 N$(0)="ONE":N$(1)="TWO"
60 PRINT M(1,2);" ";N$(1)
- An element is used like a variable: in expressions (
A(I)*2), on the left of=, inINPUT A(I,J)andINPUT S$(I). The index can be any expression,A(A(1)+1)too. - A letter names a number array and a string array, apart from the plain variable:
A,A(3),A$andA$(3)are four different things. - The number arrays share room for 1,024 elements, with 16 bytes each; the string arrays share 512 elements, whose text is in the strings' pool. Elements start at 0 and empty. Too many gives
SORRY?;MEMshows what is left. - An index outside the array, an array that was never dimensioned, or
DIMof a name twice givesHOW?. The wrong number of indices givesWHAT?.RUNandNEWremove all the arrays, so a program dimensions them in its first lines.
Strings
Text is kept in string variables, A$ to Z$ (the name, then a $). They are separate from the numbers: A and A$ are two variables. Type the $ on its key in NUM (the one STO has above it, row 2 right of =), or from the third page of the keyword map. All the strings share one pool of 4,096 characters; one string can be up to 255 characters long, and a variable that was never set is empty. RUN and NEW empty them all.
10 A$="HELLO"
20 B$=A$+", WORLD"
30 PRINT B$;" HAS ";LEN(B$);" LETTERS"
40 IF LEFT$(B$,1)="H" THEN PRINT "STARTS WITH H"
50 INPUT "YOUR NAME";N$
60 PRINT "HI ";N$
A$="text",A$=B$+C$:+joins strings.LETis optional. Text is kept as typed, lowercase too.PRINTtakes strings and numbers in any order.INPUT A$takes the whole line typed. With no prompt it showsA$?.IF A$="X", and<> < > <= >=: strings compare by their character codes, then by length. Both sides must be strings. The answer is 1 or 0, as with numbers.- An answer longer than 255 characters, or strings that together would not fit the pool, give
SORRY?; the variable keeps its old value.
| Function | Gives |
|---|---|
LEN(s$) | the number of characters |
LEFT$(s$,n) RIGHT$(s$,n) | the first or last n characters (all of them if there are fewer) |
MID$(s$,start,n) | n characters from start, counting from 1; without n, to the end. start 0 is HOW? |
CHR$(n) ASC(s$) | the character with code 1 to 255, and the code of a string's first character |
STR$(x) VAL(s$) | a number as text, as PRINT shows it, and text as a number (0 if it isn't one) |
INSTR(a$,b$) | where b$ first appears in a$, counting from 1; 0 if it doesn't |
INKEY$ | the next key pressed while the program runs, as a one-character string, or an empty string if there was none. Digit and operator keys give what they type in NUM; the other keys give their letter; ENTER, ←, ▲ and ▼ give CHR$(13), CHR$(8), CHR$(30) and CHR$(31). KEY() gives the key's place instead. |
A string can be given wherever a name is wanted: LOAD A$, SAVE "X"+A$, PLAYMIDI A$, PLAYWAV "TONES/TONE"+STR$(N).
7. Statements
| Statement | Does | Example |
|---|---|---|
PRINT or ? | Shows numbers and "text". ; between items keeps them together, , moves to the next column of 8. A ; or , at the end keeps the line open for the next PRINT. | PRINT "X=";X,"Y=";Y |
DIM | Makes number arrays (DIM A(10), DIM M(5,5)) and string arrays (DIM N$(20)); see Arrays. | DIM M(2,3), N$(9) |
INPUT | Asks for numbers, or for a line of text into a string variable (INPUT A$). Without a prompt it shows the variable's name: R? . Something that isn't a number gets HOW? and the question again. | INPUT "RADIUS? ",R |
LET | Gives a variable a value. LET can be left out. | X=X+1, @(I)=0 |
IF … THEN | If the condition is true (not 0), does the rest of the line; if false, skips it, including statements after :. THEN 100 goes to line 100. | IF X>9 THEN PRINT "BIG":X=0 |
GOTO | Goes to a line. The number can be an expression. | GOTO 100+10*K |
GOSUB, RETURN | Goes to a line, and RETURN comes back after the GOSUB. | GOSUB 500 |
FOR … TO … STEP, NEXT | Repeats the lines up to NEXT, the variable going from the first value to the last. STEP can be a fraction or negative; without it, 1. | FOR A=0 TO 1 STEP 0.1 |
END, STOP | Ends the program. | |
REM | A note: the rest of the line is ignored. | REM DRAW THE AXES |
DEG, RAD | Angles in degrees, or radians (the default). RUN starts in radians, so a program that wants degrees says DEG itself. | DEG:PRINT SIN(30) |
DIGITS | Digits PRINT shows, 1 to 34; 12 at first, and again at each RUN. | DIGITS 34 |
CLS, AT, PSET, LINE, BOX, CIRCLE, ELLIPSE, POLYGON, PEN, TONE, PLAYMIDI, STOPMIDI, PLAYWAV, STOPWAV, WAIT | The screen, sound and time: section 8. | |
SAVE, LOAD, FILES, ERASE | Files: section 9. | |
VER | Shows the PX-BASIC version. | VER |
MEM | How much is left of the program space and of the string pool. (SIZE is the program space alone, as a number.) | MEM |
HELP (or CMDS) | Shows the keyword map (below) to read, without typing anything: every word BASIC knows, in the same eight pages. ▲ and ▼ turn the pages, round and round; ON or any key with a word on it closes it and brings back the prompt. | HELP |
EXIT | Leaves BASIC for the calculator's menu, as ON does, but without asking. The program is kept for next time. | EXIT |
8. The screen, sound and keys
Text
The text fills 9 lines of 31 characters under the status bar, and scrolls up when it reaches the bottom.
CLSclears the screen and puts the text cursor at the top left.AT col,rowmoves it, from 0,0 to 30,8, for the next PRINT.
Graphics
The screen is 250 × 122 pixels, with x from 0 (left) to 249 and y from 0 (top) to 121. A colour of 1 is black (the default), 0 white, which erases.
| Statement | Draws |
|---|---|
PSET x,y or PSET x,y,c | one pixel |
LINE x1,y1,x2,y2 (,c,t) | a line, t pixels thick (1 if left out) |
BOX x,y,w,h (,c,t) | a rectangle w wide, h high, from its top-left corner |
CIRCLE x,y,r (,c,t) | a circle around x,y |
ELLIPSE x,y,rx,ry (,c,t) | the same, rx across and ry up and down from the centre |
POLYGON n,X,Y (,c,t) | the n corners (1 to 64) in the lists X() and Y(), closed for you; where it crosses itself, a filled one has holes. DIM X(2),Y(2) holds 3 corners. |
t is the thickness: 0 or left out is filled (a line: 1), 1 one pixel, n n pixels. An outline grows inward, keeping the outer size; a line or a polygon's edge is centred, with round ends. To give t, give c first: CIRCLE 125,61,40,1,1. | |
PEN t or PEN t,c | draws nothing: the shapes after it use this t and c when they leave those out, and PSET the colour. PEN 2:CIRCLE 125,61,40 is an outline 2 pixels thick; PEN 0,0 erases, filled. What a shape gives itself still counts. RUN and NEW forget it. |
The shapes are the calculator's own, in its firmware, and Plus42 draws with the same ones: PEN, BOX, ELLIPSE and POLYGON there, with DLINE for LINE. Plus42 counts from 1,1, so a position there is one more than here, and its pen starts at 1, an outline. Here, until a PEN, a shape that leaves out t is filled.
Drawing and text share the screen. While a program runs the status bar isn't redrawn, so the program can use the whole screen; it comes back at the prompt.
Sound and time
TONE nplays the calculator's tonen, 0 (low) to 9 (high), or 10 for the error sound. The program waits until it has played.PLAYMIDI "name"plays the song/MIDI/name.midfrom the calculator's drive (the same songs as the MIDI Player app) and goes on at once: the music plays in the background while the program draws, reads keys or waits. The song keeps playing after the program ends, until it finishes,STOPMIDIstops it, or you leave BASIC. The name is as it is on the drive, in quotes (or without quotes if it has no spaces); up to 28 characters. A song that isn't there, or isn't a MIDI file the synthesizer can play, givesHOW?; one over 128 KB givesSORRY.STOPMIDIstops the song.PLAYWAV "name"plays the WAV file/SOUNDS/name.wavon the calculator's drive and goes on at once. A/in the name is a folder:PLAYWAV "TONES/TONE3". A file that isn't there or can't be played givesHOW?. The speaker plays one sound at a time, so a newPLAYWAV,PLAYMIDIorTONEreplaces the one playing.STOPWAVstops the sound; it does the same asSTOPMIDI.MIDI()is 1 while a song (or a WAV) is playing and 0 when it isn't, so a program can wait for the end:WAIT 100in a loop whileMIDI()is 1.WAIT mspauses that many milliseconds; R/S still stops the program during it.
Keys: KEY()
While a program runs, the keys pressed (except R/S and ON, which stop it) are kept for KEY(). Each call gives the next one, in the order pressed, as its row and column from 1, or 0 when there's none left:
10 REM MOVE A BOX WITH 4 AND 6, R/S STOPS
20 CLS:X=120
30 BOX X,55,8,8
40 K=KEY()
50 IF K=0 THEN WAIT 20:GOTO 40
60 BOX X,55,8,8,0
70 IF K=52 THEN X=X-8
80 IF K=54 THEN X=X+8
90 GOTO 30
Line 50 waits for a key, asleep, 20 ms at a time. Line 60 erases the box (colour 0) before it moves. A single pixel (PSET) is hard to see on the calculator's screen: for something that moves, draw a BOX.
9. Saving programs
Programs are saved on the calculator's drive as plain text files, NAME.BAS, in the folder /BIN/APPS/BASIC/. A name has up to 20 letters, digits, _ or -. The quotes can be left out: LOAD RINGS3D is LOAD "RINGS3D". Inside quotes a name can also have spaces: SAVE "My Ring"; without quotes a name ends at the first space. The SAVE, LOAD and ERASE keys of the keyword map type the word and a space, no quote. Lower case is the same name in capitals: "Ring_3d" is saved and listed as RING_3D.
| Command | Does |
|---|---|
SAVE "NAME" | Saves the program, replacing a saved one of that name. |
LOAD "NAME" | Loads a saved program, in place of the one in memory. |
FILES | Lists the saved programs. |
ERASE "NAME" | Deletes a saved program. |
On a computer. In USB Drive mode the files are in BIN/APPS/BASIC. A .BAS file is the program as LIST shows it, so you can read and write programs in any text editor. Lines without a number are skipped when loading.
AUTOSAVE. When you leave BASIC, the program in memory is saved as AUTOSAVE.BAS, and loaded again when BASIC starts. It's the program as it is at that moment, whatever you saved or loaded under other names meanwhile: after LOAD "GAME" and leaving, BASIC starts with GAME. It's written when you leave by ON (and ENTER to confirm), when you hold ON to turn off, and when the Sleep Timer or an empty battery turns the calculator off. It isn't written if the calculator crashes or is reset, so SAVE a program you can't lose. FILES doesn't list it.
10. Errors
The three messages of the original Tiny BASIC, plus BREAK. In a program each also says the line: HOW? IN 120.
| Message | Means | For example |
|---|---|---|
WHAT? | It can't be read. | a misspelling (PRIMT), a missing ) or = |
HOW? | It can't be done. | division by zero, SQR(-1), GOTO a line that isn't there, RETURN without GOSUB, @(5000), LOAD a file that isn't there |
SORRY | There's no room. | the program is full (16 KB), or GOSUBs and FORs nest more than 32 deep |
BREAK | R/S, ON or Ctrl-C stopped it. |
11. Examples
These come with the project, in sim/drive/BIN/APPS/BASIC/. Copy them to BIN/APPS/BASIC on the calculator's drive, then LOAD "PRIMES" and RUN.
HELLO
10 REM THE FIRST PROGRAM
20 CLS
30 PRINT "HELLO FROM THE PX-42F!"
40 FOR I=1 TO 5
50 PRINT I;" SQUARED IS ";I*I
60 NEXT I
70 TONE 5
PRIMES
10 REM PRIMES BELOW 100
20 FOR N=2 TO 100
30 FOR D=2 TO SQR(N)
40 IF N MOD D=0 THEN 70
50 NEXT D
60 PRINT N;" ";
70 NEXT N
80 PRINT
GUESS
10 REM GUESS THE NUMBER, 1 TO 100
20 N=RND(100):T=0
30 INPUT "GUESS? ",G
40 T=T+1
50 IF G<N THEN PRINT "HIGHER":GOTO 30
60 IF G>N THEN PRINT "LOWER":GOTO 30
70 PRINT "YES, IN ";T;" GUESSES"
80 TONE 9
SPIRAL
10 REM A SPIRAL OF LINES: R/S STOPS
20 CLS:DEG
30 X=125:Y=61
40 FOR A=0 TO 1440 STEP 15
50 R=A/24
60 U=125+R*COS(A):V=61+R*SIN(A)
70 LINE X,Y,U,V
80 X=U:Y=V
90 NEXT A
100 WAIT 3000
JUKEBOX
Plays /MIDI/Canon.mid (put a song of that name there, or change the name in line 50) and stops when you press a key.
10 REM PLAYS A SONG FROM /MIDI
20 CLS
30 PRINT "PLAYING CANON"
40 PRINT "ANY KEY STOPS IT"
50 PLAYMIDI "Canon"
60 WAIT 100
70 IF KEY()<>0 THEN STOPMIDI
80 IF MIDI() THEN GOTO 60
90 PRINT "DONE"
SINE
10 REM SIN AND COS, PLOTTED
20 CLS:RAD
30 LINE 0,61,249,61:LINE 125,0,125,121
40 FOR X=0 TO 249
50 T=(X-125)/20
60 PSET X,61-40*SIN(T)
70 PSET X,61-40*COS(T)
80 NEXT X
90 WAIT 3000
SHAPES
Each kind of shape, a page at a time, with PEN; a key goes on. Line 650 draws over the ball with a white pen.
10 REM SHAPES: WHAT THE CALCULATOR DRAWS. A KEY GOES ON
20 REM PEN T,C SETS THE THICKNESS AND COLOUR OF WHAT FOLLOWS
30 DEG:DIM X(5),Y(5)
100 REM ----- THE WHOLE SCREEN -----
110 CLS:PEN 1
120 BOX 0,0,250,122:LINE 0,0,249,121:LINE 0,121,249,0
130 GOSUB 900
200 REM ----- A STAR OF LINES, THEN EACH THICKNESS -----
210 CLS:PEN 1
220 FOR A=0 TO 345 STEP 15
230 LINE 69,61,INT(69.5+56*COS(A)),INT(61.5+56*SIN(A))
240 NEXT A
250 LINE 139,19,239,19
260 PEN 2:LINE 139,39,239,39
270 PEN 4:LINE 139,61,239,61
280 PEN 8:LINE 139,83,239,83
290 PEN 16:LINE 149,105,229,105
295 GOSUB 900
300 REM ----- BOXES: OUTLINES, FILLED, WHITE INSIDE -----
310 CLS
320 PEN 1:BOX 9,9,70,45
330 PEN 2:BOX 89,9,70,45
340 PEN 4:BOX 169,9,70,45
350 PEN 0:BOX 9,64,110,50:BOX 129,64,110,50
360 PEN 0,0:BOX 34,77,60,24
370 PEN 3,0:BOX 141,72,86,34
380 GOSUB 900
400 REM ----- CIRCLES AND ELLIPSES -----
410 CLS
420 PEN 1:CIRCLE 41,31,28
430 PEN 3:CIRCLE 124,31,28
440 PEN 7:CIRCLE 207,31,28
450 PEN 0:ELLIPSE 67,91,60,26
460 PEN 0,0:ELLIPSE 67,91,30,12
470 PEN 2:ELLIPSE 189,91,50,20
480 GOSUB 900
500 REM ----- POLYGONS: A FILLED STAR, WITH ITS HOLE; A HEXAGON -----
510 CLS
520 PEN 0:N=5:S=144:C=66:GOSUB 800
530 PEN 2:N=6:S=60:C=186:GOSUB 800
540 GOSUB 900
600 REM ----- A WHITE PEN ERASES: A BALL HOPS ACROSS -----
610 CLS
620 FOR D=19 TO 229 STEP 30
630 PEN 0:CIRCLE D,61,14
640 WAIT 300
650 PEN 0,0:CIRCLE D,61,14
660 NEXT D
670 END
800 REM N CORNERS, S DEGREES APART, AROUND C,62
810 A=-90
820 FOR I=0 TO N-1
830 X(I)=INT(C-0.5+50*COS(A)):Y(I)=INT(61.5+50*SIN(A))
840 A=A+S
850 NEXT I
860 POLYGON N,X,Y
870 RETURN
900 REM WAIT FOR A KEY
910 IF INKEY$="" THEN WAIT 20:GOTO 910
920 RETURN
CATCHER
A game that shows the graphics and the sound. Stars and rocks fall; 4 and 6 (or ▲ ▼) move the basket. A star caught plays a tone without stopping the game (PLAYWAV "TONES/TONE"+STR$(C)), one higher for each star in a row; a rock costs a life, with sparks drawn by LINE and the error sound (TONE 10). Every 10 stars the game speeds up, with a jingle. Three rocks and it's over; ENTER plays again, R/S stops.
10 REM STAR CATCHER: CATCH THE STARS, DODGE THE ROCKS
20 REM 4 AND 6 (OR THE ARROWS) MOVE THE BASKET, R/S QUITS
30 REM GRAPHICS: LINE BOX PSET AT. SOUND: TONE AND PLAYWAV
40 DIM X(3),Y(3),T(3)
50 H=0
100 REM ----- TITLE: A BURST OF LINES, A FANFARE -----
110 CLS:RAD
120 FOR A=0 TO 6.2 STEP 0.2
130 LINE 121,66,121+55*COS(A),66+38*SIN(A)
140 NEXT A
150 BOX 66,58,110,17,0
160 LINE 66,58,175,58:LINE 66,74,175,74
170 AT 9,4:PRINT "STAR CATCHER";
180 AT 4,8:PRINT "ENTER: PLAY R/S: QUIT";
190 TONE 3:TONE 5:TONE 7:TONE 9
200 REM TWINKLE IN THE CORNERS UNTIL ENTER
210 N=RND(60)-1:IF RND(2)=1 THEN N=N+190
220 PSET N,RND(80)+13,RND(2)-1
230 K$=INKEY$:IF K$<>CHR$(13) THEN WAIT 20:GOTO 210
300 REM ----- A NEW GAME -----
310 CLS:S=0:L=3:V=2:C=0:P=113:G=10
320 FOR I=0 TO 3:T(I)=0:NEXT I
330 FOR I=1 TO 40:PSET RND(250)-1,RND(80)+26:NEXT I
340 LINE 0,121,249,121
350 GOSUB 900:GOSUB 800
400 REM ----- EACH FRAME: THE KEYS, THEN EVERYTHING FALLS -----
410 K$=INKEY$:D=0
420 IF K$="4" OR K$=CHR$(30) THEN D=-12
430 IF K$="6" OR K$=CHR$(31) THEN D=12
440 IF D THEN BOX P,112,24,8,0:P=P+D:IF P<0 THEN P=0
450 IF P>226 THEN P=226
460 IF D THEN GOSUB 800
500 FOR I=0 TO 3
510 IF T(I)=0 THEN GOSUB 600:GOTO 590
520 BOX X(I)-3,Y(I)-3,7,7,0
530 Y(I)=Y(I)+V
540 IF Y(I)>=110 THEN GOSUB 700:GOTO 590
550 IF T(I)=1 THEN LINE X(I)-3,Y(I),X(I)+3,Y(I):LINE X(I),Y(I)-3,X(I),Y(I)+3
560 IF T(I)=2 THEN BOX X(I)-3,Y(I)-3,7,7,1:PSET X(I)-1,Y(I)-1,0
590 NEXT I
595 IF L=0 THEN 1000
598 WAIT 20
599 GOTO 410
600 REM ----- MAYBE A NEW STAR OR ROCK IN SLOT I -----
610 IF RND(12)>1 THEN RETURN
620 X(I)=RND(234)+7:Y(I)=29:T(I)=1
630 IF RND(10)<=2+INT(S/15) THEN T(I)=2
640 RETURN
700 REM ----- SLOT I REACHED THE BASKET'S LINE -----
710 Z=T(I):T(I)=0
720 IF X(I)<P-2 OR X(I)>P+25 THEN 770
730 IF Z=2 THEN 780
740 S=S+1:C=C+1:IF C>9 THEN C=9
750 PLAYWAV "TONES/TONE"+STR$(C)
760 IF S=G THEN GOSUB 850
765 GOSUB 900:GOSUB 800:RETURN
770 IF Z=1 THEN C=0
775 GOSUB 800:RETURN
780 L=L-1:C=0:GOSUB 950:GOSUB 900:GOSUB 800:RETURN
800 REM ----- THE BASKET -----
810 BOX P,112,24,8,0
820 LINE P,112,P+3,119:LINE P+23,112,P+20,119:LINE P+3,119,P+20,119
830 LINE P+1,115,P+22,115
840 RETURN
850 REM ----- NEXT LEVEL: FASTER, AND A JINGLE -----
860 V=V+1:G=G+10
870 AT 11,4:PRINT "LEVEL ";V-1;
880 TONE 5:TONE 7:TONE 9
890 BOX 0,61,250,12,0:RETURN
900 REM ----- SCORE, BEST, LIVES -----
910 AT 0,0:PRINT "SCORE ";S;" ";
920 AT 13,0:PRINT "BEST ";H;" ";
930 FOR J=0 TO 2:BOX 218+J*10,15,8,8,1:IF J>=L THEN BOX 219+J*10,16,6,6,0
940 NEXT J
945 LINE 0,25,249,25:RETURN
950 REM ----- A ROCK HIT: SPARKS AND THE ERROR SOUND -----
960 FOR A=0 TO 3.2 STEP 0.4
970 LINE X(I),111,X(I)+12*COS(A),111-12*SIN(A)
980 NEXT A
985 TONE 10
990 BOX X(I)-13,98,27,14,0:RETURN
1000 REM ----- GAME OVER: A FALLING SCALE -----
1010 IF S>H THEN H=S
1020 BOX 50,47,150,52,0
1030 LINE 50,47,199,47:LINE 199,47,199,98:LINE 199,98,50,98:LINE 50,98,50,47
1040 AT 11,3:PRINT "GAME OVER";
1050 AT 11,5:PRINT "SCORE ";S;
1060 FOR N=9 TO 0 STEP -3:TONE N:NEXT N
1070 IF INKEY$<>"" THEN 1070
1080 AT 10,6:PRINT "ENTER: AGAIN";
1090 K$=INKEY$:IF K$<>CHR$(13) THEN WAIT 20:GOTO 1090
1100 GOTO 300
12. Limits
| Limit | |
|---|---|
| Program | 16,384 bytes: each line takes its text plus 3 |
| A line | 80 characters |
| Line numbers | 1 to 32767 |
| Variables | 26, A to Z, and the array @(0) to @(1023) |
| DIM arrays | one or two indices, indices from 0; 1,024 number elements in all, and 512 string elements (one index) |
| Numbers | 34 significant digits, exponents to ±6144 |
| GOSUB and FOR | 32 deep each |
| Text | strings A$ to Z$: 255 characters each, 4,096 in all |
13. Quick reference
| Commands | RUN LIST NEW SAVE LOAD FILES ERASE VER MEM HELP CMDS EXIT |
|---|---|
| Statements | DIM PRINT ? INPUT LET IF THEN GOTO GOSUB RETURN FOR TO STEP NEXT END STOP REM |
| The calculator | CLS AT PSET LINE BOX CIRCLE ELLIPSE POLYGON PEN TONE PLAYMIDI STOPMIDI PLAYWAV STOPWAV WAIT DEG RAD DIGITS |
| Functions | SIN COS TAN ASN ACS ATN SQR EXP LOG LOG10 ABS SGN INT RND PI KEY MIDI SIZE LEN ASC VAL INSTR LEFT$ RIGHT$ MID$ CHR$ STR$ INKEY$ |
| Operators | ^ * / MOD + - = <> < > <= >= NOT AND OR |
| Keys | SHIFT ENTER ABC, abc, NUM · SHIFT from NUM once · ▲ last commands · ▼ List mode · ▲ ▼ in a changed line move the cursor · ▼ at the end complete · SHIFT ▼ keywords · SHIFT ▲ List mode · in List mode SHIFT GTO a line number, SHIFT ← delete the line (asks) · ← delete · R/S stop · SHIFT R/S RUN · ON stop, or leave (asks) |