diff --git a/docs/BASIC_DEVELOPMENT.md b/docs/BASIC_DEVELOPMENT.md new file mode 100644 index 0000000..d44038d --- /dev/null +++ b/docs/BASIC_DEVELOPMENT.md @@ -0,0 +1,510 @@ +This kernel uses a modified version of the MikeOS BASIC interpreter.
+# Overview +## Features +The MikeOS BASIC interpreter runs a simple dialect of the BASIC programming language. There are commands for taking input, handling the screen, performing nested loops, loading/saving files, and so forth. You will find a full list of the included instructions later in this document. Here are the essentials you need to know. + + Numeric variables -- These are A to Z, each storing one positive integer word (ie 0 to 65535). The R and S variables have special roles with LOAD and SAVE commands, as explained in the instruction list below. + String variables -- These are $1 to $8, each 128 bytes long. + Arrays -- You can use string variables as arrays via the PEEK and POKE commands. For instance, X = & $1 places the memory location of the $1 string variable into X. You can then put data into it with eg POKE 77 X (put 77 into the memory location pointed to by X). + Labels -- Such as box_loop: etc. Used by GOTO and GOSUB, they must have a trailing colon and be at the start of a line. + Ending -- Programs must finish with an END statement. + +GOSUB calls can be nested up to ten times. FOR loops can be nested using different variables (eg you can have a FOR X = 1 TO 10 ... NEXT X loop surrounding a FOR Y = 30 TO 50 loop). You can enter code in uppercase or lowercase -- the interpreter doesn't mind either way. However, labels are case-sensitive. + +If a MikeOS BASIC program is run from the command line, and one or more parameters was provided with the command, they are copied into the $1 string when the program starts. + +## Example + +Here's a small example program demonstrating a FizzBuzz program. + +rem *** MikeOS BASIC example program *** + +``` +# A cool fizzbuzz program +PRINT "FizzBuzz!" +FOR A = 1 TO 16 + PRINT A ; # Prints the number + B = A % 3 + C = A % 5 + IF B = 0 then PRINT "Fizz" ; + IF C = 0 then PRINT "Buzz" ; + PRINT "" +NEXT A +END +``` + +This example should be mostly self-explanatory. You can see that the subroutine is indented with tabs, but that's not necessary if you don't want it. You can follow IF ... THEN with any other instruction. Regarding this part: +``` + PRINT A ; # Prints the number + B = A % 3 +``` + +The space and semi-colon character (;) after the quoted string tells the interpreter not to print a newline after the string. So here, we print the user's name on the same line. You can do this with numerical variables as well, eg PRINT X ; etc. + +See the Samples section at the end for more demonstration programs. + +## Assignment + +The following are valid ways to assign numeric variables in MikeOS BASIC: + +``` +a = 10 +a = b +a = b + 10 +a = b + c +a = b - 10 +a = b - c +a = b * 10 +a = b * c +a = b / 10 +a = b / c +a = b % 10 +a = b % c +``` + +So you can use combinations of numbers and variables. Note that you can perform multiple operations in the same line: +``` +a = b + c * 2 / 3 +``` +But note that there is no operator precedence; the calculation is simply worked out one step at a time from left to right. For string variables: +``` +$1 = "Hello" +$2 = $1 +``` +You can get the location of a string variable to use as an array: +``` +x = & $3 +``` +You can get variables from the user with INPUT like this: +``` +input x +input $1 +``` + +## Keywords + +You can do this to get the MikeOS API version number: +``` +x = VERSION +``` +Or you can retrieve the lower word of the system clock like this: +``` +x = TIMER +``` +To get the current ink colour, use: +``` +x = INK +``` + +## The editor + +You can run your .BAS programs by using the BAS core util eg. +``` +bas .bas +``` + +# Instructions +## BREAK + +Halts execution of the BASIC program and prints the line number in the BASIC file. Useful for debugging. + +## CALL + +Moves machine code execution to the specified point in RAM (using the x86 call instruction). The code must be terminated with a ret (C3 hex, 195 decimal) instruction. In this example, we simply add a ret instruction into RAM location 40000 and call it, which returns control straight back to the BASIC interpreter: +``` +poke 195 40000 +call 40000 +``` + +## CASE + +Changes the contents of a string to all upper-case or lower-case. +``` +case lower $1 +case upper $2 +``` + +## CLS + +Clears the screen and returns the cursor to the top-left corner of the screen. Example: +``` +cls +``` + +## CURSOR + +Determines whether to show the text cursor or not. Example: + +cursor off +print "The cursor is off for five seconds!" +pause 50 +cursor on +print "And now it's back on." + + +## CURSCHAR + +Stores the character underneath the cursor location into the specified variable. Example: +``` +move 0 0 +print "Hello world" + +move 0 0 +curschar x + +rem *** The next command will print 'H' *** +print chr x + +move 1 0 +curschar x + +rem *** The next command will print 'e' *** +print chr x +``` + +## CURSCOL + +Get the colour of the character under the cursor. Example: +``` +move 20 15 +curscol x +``` + +## CURSPOS + +Get the position of the cursor. Example: +``` +rem *** First is column, then row *** +curspos a b +``` + +## DELETE + +Removes a file from the disk. Returns 0 in the R variable if the operation was a success, 1 if the delete operation couldn't be completed, or 2 if the file doesn't exist. Example: +``` +delete "myfile.txt" +``` + +## DO + +Perform a loop until a condition is met (UNTIL or WHILE). You can also set up an infinite loop with LOOP ENDLESS at the end. Example: +``` +do + rem *** Code goes here *** +loop until x = 10 +``` + +## ELSE + +Executes code if the previous IF condition didn't match. Example: +``` +x = 1 +if x = 1 then print "Hello" +else print "Goodbye" +``` + +## END + +Terminates execution of the BASIC program and hands control back to the operating system. + +## FILES + +Prints a list of files that are on the disk on the screen. + +## FOR + +Begins a loop, counting upwards using a variable. The loop must be finished with a NEXT instruction and the relevant variable. Example: +``` +for x = 1 to 10 + print "In a loop! X is " ; + print x +next x +``` + +## GETKEY + +Checks the keyboard buffer for a key, and if one has been pressed, places it into the specified variable. +``` +loop: + print "Infinite loop until m or Esc is pressed..." ; + getkey x + if x = 'm' then goto done + goto loop + +done: + print "Finished loop!" +``` + +## GOSUB + +Takes a label. It executes a subroutine, which must be finished with a RETURN instruction. You can nest GOSUB routines up to 10 times. Example: +``` +print "About to go into a subroutine..." +gosub mylabel +print "Subroutine done!" +end + +mylabel: + print "Inside a GOSUB here!" +return +``` + +## GOTO + +Takes a label, and jumps to that label in the code. Example: +``` +print "Going to miss the next 'PRINT' line of code..." +goto skippy + +print "This'll never be printed." + +skippy: +print "And now we're back home" +``` + +## IF + +Executes a command depending on a condition (or multiple conditions with AND). After stating the condition (eg whether one number is bigger than another, or whether two strings match) you must use THEN and follow with another instruction. Examples: +``` +if x = 10 then print "X is 10! Woohoo" + +if x = y then print "X is the same as Y" + +if x = 'm' then print "X contains the letter m" + +if x < y then print "Now X is less than Y" + +if x > y then goto xbiggerthany + +if $1 = "quit" then end + +if $1 = $2 then gosub stringsmatch +``` + +## INPUT + +Gets input from the user and stores the result into a numeric or string variable. Examples: +``` +input x +input $1 +``` + +## LEN + +Stores the length of a string variable in a numeric variable. Example: +``` +$1 = "Hello world" +len $1 x +``` + +## LOAD + +Loads the specified file into RAM at the specified point. The first argument is the filename, and the second the location into which it should be loaded. If the file cannot be found or loaded, the R variable contains 1 after the instruction; otherwise it contains 0 and the S variable contains the file size. Examples: +``` +load "example.txt" 40000 +if r = 1 then goto fail +print "File size is:" +print s +end + +fail: +print "File couldn't be loaded" +end + +$1 = "example.txt" +x = 40000 + +load $1 x +if r = 1 then goto fail +print "File size is:" +print s +end + +fail: +print "File couldn't be loaded" +end +``` + +## NEXT + +Continues the FOR loop specified previously, and must be followed by a variable. See FOR above. Example: +``` +next x +``` + +## NUMBER + +Converts strings to numbers and vice versa. Examples: +``` +number $1 a + +number a $1 +``` + +## PAUSE + +Delays execution of the program by the specified 10ths of a second. This ultimately results in a BIOS call and may be slower or faster in some PC emulators. Try it on real hardware to be sure. Example: +``` +print "Now let's wait for three seconds..." +pause 30 + +print "Hey, and one more, this time with a variable..." +x = 10 +pause x +``` + +## PAGE + +Switch between working and active (display) pages. Example: +``` +page 1 0 +``` + +## PEEK + +Retrieve the byte stored in the specifed location in RAM. Examples: +``` +peek a 40000 +print "The number stored in memory location 40000 is..." +print a + +x = 32768 +peek a x +``` +> [!NOTE] +> You can use PEEKINT to work with words instead of bytes (up to 65536). + +## POKE + +Insert the byte (0 to 255) value into the specified location in RAM. Example: +``` +print "Putting the number 126 into location 40000 in memory..." +poke 126 40000 + +print "Now doing the same, but using variables..." +x = 126 +y = 40000 +poke x y +``` +> [!NOTE] +> You can use POKEINT to work with words instead of bytes (up to 65536). + +## PORT + +Sends and receives bytes from the specified port. Examples: +``` +x = 1 +port out 1234 x +port out 1234 15 +port in 1234 x +``` + +## PRINT + +Displays text or the contents of a variable onto the screen. This will also move the cursor onto a new line after printing, unless the command is followed by a space and semi-colon. Example: +``` +print "Hello, world!" + +$1 = "Some text" +print $1 + +x = 123 +print x + +$2 = "Mike" +print "No newlines here, " ; +print $2 +``` +> [!NOTE] +> For numerical variables, the PRINT command also supports two extra keywords: +``` +x = 109 +print x +print chr x +print hex x +``` +In the first print command, the output is 109. In the second, the output is the ASCII character for 109 -- that is, 'm'. And in the third command, it shows the hexadecimal equivalent of 109. + +## RAND + +Generate a random integer number between two values (inclusive) and store it in a variable. Example: +``` +rem *** Make X a random number between 50 and 2000 *** + +rand x 50 2000 +``` + +## READ + +Read data bytes from a label, first specifying the offset and a variable into which to read. For instance, in the following example we read a small program and poke it into memory locations 50000 to 50012. We then call that location to run the program: +``` +y = 1 + +for x = 50000 to 50012 + read mydata y a + poke a x + y = y + 1 +next x + +call 50000 + +waitkey x +end + +mydata: +190 87 195 232 173 60 195 89 111 33 13 10 0 +``` + +## REM + +A remark -- ie a comment that the BASIC interpreter ignores. Example: +``` +rem *** This routine calculates the cuteness of kittens *** +``` + +## RENAME + +Renames one file to another. Literal strings and string variables are allowed. Afterwards, R contains 0 if the operation was a success, 1 if the file was not found, 2 if the write operation failed, or 3 if the target file already exists. Example: +``` +$1 = "myfile.dat" +rename "oldfile.txt" $1 +``` + +## RETURN + +Switches execution back to the position of the prior GOSUB statement. See GOSUB above for more information. Example: +``` +return +``` + +## SAVE + +Saves data from memory to a file on the disk. The first parameter is the filename, the second the location in RAM where the data starts, and the third the number (in bytes) the amount of data to save. After execution, the R variable contains 1 if the file save operation failed (eg if it's a write-only medium), 2 if the file already exists, or 0 if it was successful. Examples: +``` +save "myfile.txt" 40000 256 + +$1 = "myfile.txt" +x = 40000 +y = 256 +save $1 x y +``` + +## STRING + +Get or set bytes in a string variable, specified by an offset. Examples: +``` +$1 = "Hello world" + +rem *** 121 = ASCII for "y" character *** +b = 121 + +string set $1 5 b + +rem *** Now $1 contains "Helly world" *** +print $1 +``` + +# Modifictions +The modifications include + - Inline comments starting with a hashtag (#) + - Start of line whitespaces for indentation, spaces or tabs (not fussy about indentation depth) diff --git a/MEMORY_LAYOUT.md b/docs/MEMORY_LAYOUT.md similarity index 100% rename from MEMORY_LAYOUT.md rename to docs/MEMORY_LAYOUT.md