CHIP-12 BASIC Compiler

Updated: 08 Sep 2018 

(*****Still being developed therefore contents will be updated as new commands, statements and functions are added*****)

 

Table of Contents

IDE Overview   4

Guidelines and special instructions for using the compiler  8

General Format  8

Identifiers  8

Line Labels  9

Numeric Representations  10

Dimensioning Numeric Variables  10

Dimensioning String Variables  10

Initializing Numeric and String Variables  11

Using Arrays in CHIP-12  11

Relational Operators  14

Boolean Logic Operators  15

Compiling Errors and Warnings  15

Using Data In Your Program    15

Variables Used by BASIC Compiler  18

Known Issue With Keyboard CAPS LOCK in CHIP-12  18

Using CHIP-12 Background and Foreground Color Registers  18

Commands, Statements and Functions Overview   19

ASC   19

BackGnd   19

Call 19

CHIP12BEGIN   23

CHIP12END   23

CHR   23

CLS   23

Color  24

CopyBlock  25

CSTR   27

DefMemPtr  28

DefNewChar  28

Delay  29

DeleteSprite  30

Dim    30

DisplaySprite  33

Do Loop   33

Do Inkey  34

Do VOCEnd   35

Do WAVEnd   36

DrawPattern   37

END   42

ERASETEXTFLAGOFF   42

ERASETEXTFLAGON   42

For Next  43

GetFile  44

GetSpriteX   45

GetSpriteY   45

GInput  46

Gosub   49

Goto   50

GPrint  51

HEX   51

If Then   52

If Inkey  54

If SpriteX   55

If SpriteY   55

If Timer  56

If VOCEnd   57

If WAVEnd   58

Inkey  60

Input  60

INSTR   62

LEFT   64

LEN   65

LoadLargeFont  66

LoadSmallFont  66

LoadSprite  71

LoadSprite2  73

MID   76

MoveSpriteDown   77

MoveSpriteLeft  78

MoveSpriteRight  79

MoveSpriteUp   80

Palette  81

PauseWAV   82

Peek  83

PlayVOC   86

PlayWAV   87

Poke  88

Print  89

Random    90

RefreshSprites  92

ResumeWAV   92

RIGHT   93

SCREEN   93

SetCOLH   101

SetSpriteXY   106

SetSpriteX   107

SetSpriteY   107

SHIFT (Right or Left)  107

ShowSprite  109

STOP   110

StopWAV   111

STRDUP   111

Sub End Sub   112

Timer  112

Tone  112

Val 113

WriteFile  113

How to setup DOSBox  115

How to setup and use CHIP-12  118

How to setup and use the CHIP-12 BASIC Compiler  121

Understanding Sprites  128

Tutorial - How to create and use Sprites  129

Tutorial - How to create and use Sprites (Part 2) 132

Tutorial - How to create a bigger Sprite  139

Tutorial - How to create and use WAV Sound Files  144

Tutorial - How to create and use VOC Sound Files  146

ASCII Character Codes - Chart 1 (codes 0–127) 149

ASCII Character Codes - Chart 2 (codes 128–255) 150

 

IDE Overview

 

This compiler converts easy to use BASIC commands, statements and functions into CHIP-12 instruction codes.

The Interactive Development Environment (IDE) is pretty much simple to use.

You can use any text editor, such as Microsoft® Notepad or Word, to enter the BASIC program.

 

 

 

File has two options:
Open -
The source file is a text file of the BASIC program.
Exit - Program.

 

Compile - The compiler will create the following files:

 '.ASM' , '.C12' , '.CHP' , '.LST'

Once a program has successfully compiled, the compiler will remember the last source filename so that if you are working on the same program, you don't need to Open it each time you launch the application.

 

Open and Assemble a CHIP-12 File - The compiler will create the following 2 files:

 '.ASM' , '.C12'

This allows you to assemble a program written with CHIP-12 instructions. Since you are not using the 'A'   - ASSEMBLE A CHIP12 ASSEMBLY TEXT FILE command, you are no longer limited to 256† 896 address labels and labels can be longer than 13† 29 characters.

As of Version 11.5 changes were made to allow more labels and longer names.

 

 

The assembly '.ASM' file shows the Memory Address, Opcode, Instruction and the Address Label Table with Memory Addresses at the end.


The binary '.C12' file. To run this, you must change to Segment 2 and 'G'et file, load at 0000 and run 'Z' or use the short cut '0' which will ask you for the binary file and execute it.

 

 

 

The '.LST' file comments all BASIC lines so that you can see how the statements and function were compiled in CHIP-12 instructions.

All BASIC and CHIP-12 comments are removed in the '.CHP' file thus making it smaller.

 

 


Guidelines and special instructions for using the compiler

 

Note: It is highly recommended to read the DOSBOX CHIP12 Instruction document to familiarize yourself with all of its features, commands and instructions set.

In the directory where you unzipped the CHIP12 files and demos, you'll find a Word document named 'DOSBOX CHIP12 Instructions.doc' on how to use and learn CHIP-12.

 

General Format

The compiler is case sensitive. Therefore the syntax for commands and statements must be observed.

Dim Var1 As Byte

'The following If-Then statement will generate compile errors

If Var1 = 1 then

  Print "Hello!"

End If

 

Identifiers

An identifier is a programmer-chosen name to identify a variable, a memory address label or a subroutine name.

There are a few rules on how they can be formed:

• They may contain between 1 and 127 characters.

• They are case sensitive.

Variable and Labels must only contain alphanumeric characters.

The following table shows the valid characters you may use:

Group

Example

Uppercase letters

Lowercase letters

A,B,C...

a,b,c..

Numerals

0,1,2,3,4,5,6,7,8,9

 

You may also use the underscore '_', but please be aware that the compiler uses this symbol in auto-generated labels.

The following non-alphanumeric characters and symbols are not to be used in labels or when dimensioning numeric and string variables:

( ) ` ~ ! # $ % ^ & * - + = | \ { } [ ] : ; " ' < > , . ? /

 

NOTE 1: If you intend to use the 'A' command in CHIP-12, Variable and Label lengths cannot be longer than 13 29characters.

As of Version 11.5 changes were made to allow more labels and longer names.

NOTE 2: When using some BASIC functions and statements such as the Peek function or the Poke statement, since they can have either a variable or label, you must prefix labels with the @ symbol to differentiate them with variables.

• An identifier cannot be the same as a BASIC reserved word.

 

In general, it’s a good idea to use descriptive names for identifiers.

Sure, there's nothing wrong in using something like ABCD1234 as a variable or label, but

there’s no reason to make your identifiers hard to understand!

Here are some examples of well-formed names:

LineCount; firstValue; index; line_count; myFile; xCoord; Main; x_Coord

 

Example

'Var1 and var1 are not the same variable

Dim Var1 As Byte = 123

Dim var1 As Word = 456

 

Main:

Print var1

Print Var1

Goto main 'This will generate an error

 

Line Labels

You use line labels to mark statements that the program will reference with BASIC functions and statements like the Goto, Gosub, DefMemPtr, Peek , or Poke.

 

A line label is identified by a colon ':'.

Label:

Print "Hello World"

Goto Label

 

Numeric Representations

The compiler recognises several different numeric representations:

Binary is prefixed by %. i.e. %0101...

Character byte is surrounded by single quotes. i.e. 'a' represents a value of 97

Decimal values have no prefix.

Hexadecimal is prefixed by $. i.e. $0A

 

Dimensioning Numeric Variables

 

See also the Dim statement.

 

CHIP-12 has 256 variables designated as V00 to VFF.

Some variables are used by CHIP-12 and the BASIC compiler.

The following variables are reserved:

V00 to V0F.

The V7F register doubles as a flag for some instructions, thus you should avoid using it.

For example, in math operations, V7F is used as a carry/borrow flag.

In the draw instructions, V7F is set upon pixel/color collision.

In Shift or Rotate instructions, V7F is used as a carry flag.

The BASIC compiler will skip V7F when assigning CHIP-12 variables to your numeric variables.

Variables VC0 to VFF are dedicated to hold the X and Y coordinates when sprites are used.

You may use any variables that are not being used for the sprite(s).

For Sprite 00, the X coordinate is VC0 and the Y coordinate is VC1.

For Sprite 01, the X coordinate is VC2 and the Y coordinate is VC3 and so forth.

The compiler will still give you a warning message to alert you when a numeric variable in your program uses a CHIP-12 variable that has exceeded 191 ($BF).

Therefore, when variables are created with the Dim statement, depending on their data type - Byte, SByte, Word, SWord, DWord, SDWord, and Float - the first variable the compiler will use will be V10.

Byte type variables hold an unsigned value from 0 to 255. Uses one CHIP-12 variable.

SByte type variables hold the 2's complemented signed value from -128 to +127. Uses one CHIP-12 variable.

Word type variables hold an unsigned value from 0 to 65535. Uses two CHIP-12 variables.

SWord type variables hold a 2's complemented signed value from -32768 to +32767. Uses two CHIP-12 variables.

DWord type variables hold an unsigned value from 0 to 4294967295 making this the largest of the numeric integer variable family types. Uses four CHIP-12 variables.

SDWord type variables hold a 2's complemented signed value from -2147483648 to +2147483647.

Uses four CHIP-12 variables.

Float type variables hold a IEEE-754 32-bit value. Uses four CHIP-12 variables.

When a float number gets printed, if it is larger than 9 digits, the number gets printed in exponential format. i.e. 1,234,000,000,000,000,000 = 1.234E18

You may also use the exponential format when entering a number using the Input or GInput command.

 

Dimensioning String Variables

The compiler supports string variables.

You specify the length by adding the size in brackets at the end of the String directive in the Dim statement. If size is not defined, the compiler will assume the maximum length of 79 characters.

All strings are terminated by the ETX - End of Text ASCII character code $03.

In other words, the actual size of string is size + 1.


Initializing Numeric and String Variables

The following shows different ways in which variables can be initialized.

General Format for Numeric Variables:

Variable = {Number (Dec, Hex, or Binary) } {Variable} {+,-,*,/} {Number (Dec, Hex, or Binary) } {Variable}

 

Examples on how to initialize Numeric Variables:

Dim Tries As Word = 10 - You can initialize a variable when you Dimension it.

Mask = $F0 - When using a numeric type identifier

Counter = Counter + 15

Counter2 = Counter

Final_Result = 200 - Counter

Result = Num1 * Num2 - You can Add, Subtract, Multiply or Divide 2 variables, or a variable and a number, or 2 numbers.

All math operations of addition, subtraction, multiplication, and division use 32-bit floating point precision when at least one of the variables is a Float

 

Note: I have not worked out an algorithm for complex math operations on one line, therefore for now, only one math operation can be done at once.

A math operation like this ((2 + 3) - 1) * 10 / 5 must be done in 4 steps (not counting the Dim and Print in the example):

Dim TempVar As Byte

TempVar = 2 + 3

TempVar = TempVar - 1

TempVar = TempVar * 10

TempVar = TempVar / 5

Print TempVar

 

General Format for String Variables:

{Dim} String Variable {As String(size)} = {"Alphanumeric Text"}{String Variable}{&}{"Alphanumeric Text"}{String Variable}

 

Examples on how to initialize String Variables:

Dim VarStr As String="Hello, my name is " - You can initialize when you Dimension it.

VarStr=VarStr & CHR(1) & "CHIP-12" & CHR(1) - Use the ampersand '&' to add strings together

Dim TestStr As String(15)="Marc Bertrand" - TestStr cannot be larger than 15 characters

VarStr=LEFT(VarStr,7) & RIGHT(TestStr,8) & ". Do you like " & MID(VarStr,20,7) & "?"

 

With following modifiers:

Variable = @Label - Get the memory address of Label.

Variable = ASC('ASCII Code' or String Variable) - See appropriate section

Variable = CHR(Number or Variable) - See appropriate section

Variable = HEX(Number or Variable) - See appropriate section

Variable = Inkey - See appropriate section

Variable = INSTR(String Variable) - See appropriate section

Variable = LEFT(String Variable,) - See appropriate section

Variable = LEN(String Variable) - See appropriate section

Variable = MID(String Variable,,) - See appropriate section

Variable = Peek(n),Address - See appropriate section

Variable = Random(Number or Variable) - See appropriate section

Variable = RIGHT(String Variable) - See appropriate section

Variable = STRDUP(Number or Variable, "Character" or String Variable) - See appropriate section

Variable = Val(String Variable) - See appropriate section

Variable = Variable And {Number (Dec, Hex, or Binary) } {Variable} - Logically AND's the contents

Variable = Variable Or {Number (Dec, Hex, or Binary) } {Variable} - Logically OR's the contents

Variable = Variable Xor {Number (Dec, Hex, or Binary) } {Variable} - Logically XOR's the contents

NOTE: When using the logical operations, the first parameter must be a variable.

 

Using Arrays in CHIP-12

The compiler supports 1-dimensional arrays for the following numeric data types - Byte, SByte, Word, SWord, DWord, SDWord, and Float.

You declare an array variable the same way as any other variable, by using the Dim statement.

In your declaration, you specify the array size inside a pair of parentheses after the variable name:

Dim ArrayName(arraysize) As DataType - See also Example 2 in Dim statement for using arrays.

The arraysize defines the maximum number of elements the array will have. The arraysize is 0-based.

Therefore, when declaring the array the maximum number of elements will be arraysize+1.

Arrays are stored in the 64K DOS Segment 7 starting at memory address 0000.

The number of arrays you can have depends on the array size and the data type.

For instance, if you only have 1 array and the data type is Byte or SByte, then the maximum number of elements you can have is 65536.

If that 1 array data type is changed to Word or SWord, then the maximum number of elements you can have is 32768.

If that 1 array data type is changed to DWord, SDWord or Float, then the maximum number of elements you can have is 16384.

When you use arrays, the compiler reserves 12 of the CHIP-12 variables to perform operations such as addition, subtraction, copying values to/from variables or arrays, etc.

You cannot use Arrays and Floats with all Sprite functions and statements such as DeleteSprite, DisplaySprite, LoadSprite, MoveSpriteRight, MoveSpriteUp, MoveSpriteDown, SetSpriteXY, SetSpriteX, SetSpriteY, ShowSprite, If SpriteX, If SpriteY

NOTE: What if you need a 2D array in CHIP12 BASIC Compiler? There is a workaround. You can do it by converting a 2D array to 1D array. I found a cool explanation here.

The following is a snapshot in case the link no longer works:

 

Relational Operators

Relational operators are used to compare two values. The result can be used to make a decision

regarding program flow.

The list below shows the valid relational operators accepted by the compiler:

 

Operator

Relation

Expression Type

=

Equality

X = Y

<> 

Inequality

 X <> Y

< 

 Less than

X < Y

> 

Greater than

X > Y

<=

 Less than or Equal to

X <= Y

>=

Greater than or Equal to

X >= Y

 

 

 

Boolean Logic Operators

The If-Then-Else-End If and Do-Loop statements support the logical operators And, Or, and Xor.

You may use as many test conditions as you want.

Dim Var1 As Byte

Dim Var2 As Byte

 

Main:

Input "Var1=",Var1

Input "Var2=",Var2

If Var1 = 50 And Var2 <> Var1 Or Var2 <= 20 Or Var1 >=40  Then

  Print "Condition is True."

Else

  Print "Condition is False."

End If

Goto Main

 

Compiling Errors and Warnings

I've written the compiler to be able to catch most errors I could think of. It will also display warnings to alert you that the result will most likely not be what you expect.

Errors are demonstrated by showing the line of text in your program with a brief explanation all highlighted in  red .

Warnings are also demonstrated by showing the line of text in your program with a brief explanation all highlighted in  yellow .

 

 

Using Data In Your Program

You insert data in your program using the CHIP12BEGIN and CHIP12END directives.

Labels are designated by the '@' symbol.

Data strings are designated by the '$' symbol.

Data bytes are designated by the '*' symbol.

Technically, data should be put at the end of your BASIC program. If you put it any where else, make sure to put a Goto statement to 'skip over' the data area as shown in the following example:

 

Goto Jump_Over_Random_Number

CHIP12BEGIN

@Get_Random_Number

* 1E 56 B8 00 20 8E D8 B8 95 00 8B F0 B4 2C CD 21

* 52 B8 E8 03 33 C9 8A CE F7 E1 89 44 03 5A B8 0A

* 00 33 C9 8A CA F7 E1 01 44 03 33 DB 8A 1C 83 FB

* 00 0F 84 4C 00 83 7C 01 00 0F 85 0B 00 C7 44 01

* 5A A5 BA 5A A5 EB 04 90 8B 54 01 8B 44 03 F7 E2

* 05 5A 5A F7 E2 89 44 01 8B CB 3B C3 0F 86 05 00

* 2B C3 49 79 F5 3B C3 0F 86 07 00 25 FF 01 D1 E8

* EB E6 3B C3 0F 85 09 00 83 E2 01 0F 85 02 00 B0

* 00 88 04 5E 1F CB

@Random_Number_Parameters

* FF 5A 5A 00 00

CHIP12END

Jump_Over_Random_Number:

 

Dim Iterations As Word

Dim RndLimit As Word

 

Dim ExitVal As Byte=2

Do Until ExitVal=0

  If ExitVal = 2 Then

    Input"Enter maximum limit for Random",RndLimit

    Input"Enter number of iterations",Iterations

  End If

  CLS

  Dim Table_Start As Word

  Table_Start = @Random_Number_Table

  Print"Table_Start=";HEX(Table_Start)

  Print At 40,0,"Table_Offset=";

  Print At 40,1,"Random#=";

  Print At 40,2,"Seed=";

  Print At 0,1,"Iterations=";

 

  Print At 20,2, "Random Results";

  Print At 20,3, "--------------";

  Color Blue, Bright White

  Print At 5,4,"00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F";

  Dim Loop As Word

  Gosub Random_Results

  For Loop=1 To Iterations

    Print At 11,1;Loop;

    Poke(A),@Random_Number_Parameters,RndLimit

    Call Get_Random_Number

    Dim Count As Byte

    Count = Peek(A),@Random_Number_Parameters

    Print At 48,1,HEX(Count);

    Dim Table_Offset As Word

    Table_Offset = Table_Start + Count

    Print At 53,0;HEX(Table_Offset);

    Count = Peek(A), Table_Offset

    Count = Count + 1

    Poke(A),Table_Offset, Count

 

    Dim seedMemAddPtr As Word

    seedMemAddPtr=@Random_Number_Parameters

    seedMemAddPtr = seedMemAddPtr + 4

    Dim MemByte As Byte

    MemByte = Peek(5),seedMemAddPtr

    Print At 45,2,HEX(MemByte);

    seedMemAddPtr = seedMemAddPtr - 1

    MemByte = Peek(5),seedMemAddPtr

    Print HEX(MemByte);

    Gosub Random_Results

  Next

  'Clear table

  Position = 0

  For Ypos = 5 To 20

    For Xpos = 3 To 48 Step 3

      Table_Offset = Table_Start + Position

      Position = Position + 1

      Poke(A),Table_Offset, 0

    Next

  Next

  Color Black,Bright White

  Print At 0,22,"Press 0 to quit"

  Print"      1 to run with same parameters"

  Input"      2 to run with new parameters",ExitVal

Loop

END

 

Sub Random_Results

  Dim Xpos As Byte

  Dim Ypos As Byte

  Dim Position As Word = 0

  For Ypos = 5 To 20

    Color Blue, Bright White

    Print At 0,Ypos, HEX(Position);" ";

    For Xpos = 3 To 48 Step 3

      Table_Offset = Table_Start + Position

      Position = Position + 1

      Count = Peek(A), Table_Offset

      If Count = 0 Then

        Color Black,Yellow

      Else

        Color Black,Bright White

      End If

      Print HEX(Count);" ";

    Next

    Print

  Next

End Sub

 

CHIP12BEGIN

@Random_Number_Table

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

*00000000000000000000000000000000

CHIP12END

 

NOTE: The compiler has its own Random function. The above example also shows how you can write your own machine language subroutine to accomplish any desired task.


Variables Used by BASIC Compiler

 

As mentioned earler in the Dimensioning Numeric Variables section, CHIP-12 variables V00 to V0F are reserved for BASIC compiler.

V00, V01, and V7F are used by some CHIP-12 instructions, therefore they should only be used as temporary variables.

Memory pointers 0,1 and F are used by BASIC compiler.

V05 serves for two purposes:

In alphanumeric modes 0 to 6, it is used for the maximum number of display lines before scrolling the characters on the screen

In graphic modes 7 to 9, it's used to select the small (V05=9) or large fonts (V05=16).

V03 and V04 keeps track of the X and Y coordinates when printing characters.

 

Known Issue With Keyboard CAPS LOCK in CHIP-12

 

The issue is actually with DOSbox. You basically need to press CAPS LOCK twice to toggle between lower and upper case. In other words, the LED doesn't necessarily reflect which state your in.

 

Understanding CHIP-12 Background and Foreground Color Registers

 

The following diagram shows how to use the color registers:

In alphanumeric modes 0 to 6, two bytes are used to show each character on the screen. The first byte shows the character and the second byte describes its color and background attribute. The BackGround register is used to set the attribute byte as shown in the Color section.

In graphic modes 7 to 9, each byte sent to the screen memory represents a colored pixel. The color is determined by the value of the byte sent which is the ForeGround color register COLR.
Commands, Statements and Functions Overview

 

ASC

 

Syntax

ASC('ASCII character' or String variable)

 

Overview

Returns a Byte value that represents the character code corresponding to a character.

Operators

'ASCII character' - must be a single alpha-numeric character.

String variable -  only the first character of the string variable is used.

 

Example

Dim TestStr As String = "CHIP-12"

Dim Var1 As Byte = ASC('M')

Print "The ASCII code for TestStr is ";ASC(TestStr)

Print "The ASCII code for Var1 is ";ASC(Var1)

 

 

_______________________________________________________________________________________________________________

 

BackGnd

 

Syntax

BackGnd(Number or Variable)

 

Overview

Changes the background color in alphanumeric modes 0 to 6.

 

Operators

Number or Variable - value can be from 0 to 255, but only multiples of 16 up to 112 will select one of the following 8 colors:

0=Black, 16=Blue, 32=Green, 48=Cyan, 64=Red, 80=Magenta, 96=Brown, 112=White

NOTE: You may use data types other than Byte for variable, but take into account that the compiler only uses the value contained in the LSB.

 

The following program will demonstrate the multiples of 16 up to 112.

Example

SCREEN 4

Dim BGColor As Byte = 0

Do Until BGColor = 255

  Input BGColor

  BackGnd(BGColor)

Loop

_______________________________________________________________________________________________________________

 

Call

 

Syntax

Call Label

 

Overview

Calls a machine language subroutine at Label.

 

Operators

Label must comprise of alphanumeric characters.

 

 

Example

CAUTION: Don't press the 'ESC' key to terminate this program.

The program must restore interrupt vector 1C before returning to CHIP-12. Failure to let the program terminate properly will crash DOSBox.

 

Goto Jump_Over_Interrupt_Vector

'        .MODEL SMALL

'        .STACK 100h

'        .DATA

'CountByte   db 0AAh

'oldvectorseg dw 1234h     ;old vector segment

'            dw 5678h     ;old vector offset

'      .CODE

'start:             push ds

'            push es

'            push di

'            mov ax,SEG oldvectorseg

'            mov ds,ax

'            mov ax,OFFSET oldvectorseg

'            mov di,ax

'            mov ax,351Ch

'            int 21h

'            mov [di],es

'            mov [di+2],bx

'            mov ax,SEG new_iv_handler

'            mov ds,ax

'            mov dx,OFFSET new_iv_handler

'            mov ax,251Ch

'            int 21h

'            pop di

'            pop es

'            pop ds

'            retf

'resetiv1C:  push ds

'            push si

'            mov ax,SEG oldvectorseg

'            mov ds,ax

'            mov ax,OFFSET oldvectorseg

'            mov si,ax

'            mov cx,[si]  ;Get old vector segment

'            mov dx,[si+2] ;Get old vector offset

'            mov ds,cx

'            mov ax,251Ch

'            int 21h

'            pop si

'            pop ds

'            retf

'new_iv_handler:push ds

'            push si

'            mov ax,SEG CountByte

'            mov ds,ax

'            mov si,OFFSET CountByte

'            mov al,[si]

'            inc al

'            mov [si],al

'            pop si

'            pop ds

'            iret

'            END    start

CHIP12BEGIN

@Set_New_Interrupt_Vector

* 1E 06 57 B8 00 20 8E D8 B8 66 00 8B F8 B8 1C 35

* CD 21 8C 05 89 5D 02 B8 00 20 8E D8 BA 52 00 B8

* 1C 25 CD 21 5F 07 1F CB

@Reset_Interrupt_Vector

* 1E 56 B8 05 00 8E D8 B8 66 00 8B F0 8B 0C 8B 54

* 02 8E D9 B8 1C 25 CD 21 5E 1F CB

;New Interrupt Vector 1C

* 1E 56 B8 00 20 8E D8 BE 65 00 8A 04 FE C0 88 04 5E 1F CF

@IV_Counter

* AA

;Old Vector Segment

* 34 12

;Old Vector Offset

* 78 56

CHIP12END

Jump_Over_Interrupt_Vector:

Call Set_New_Interrupt_Vector

 

Dim Loop As Word

Print"Counter  = ";

For Loop=0 To 100

  Dim Counter As Byte

  Counter = Peek(A), @IV_Counter

  Print HEX(Counter);" ";

  Delay(5)

Next

Print

Print "Done loop"

Dim KeyStr As String

Input"Run again (Y/N)",KeyStr

If KeyStr = "Y" Or KeyStr = "y" Then

  Goto Jump_Over_Interrupt_Vector

End If

Call Reset_Interrupt_Vector

Print "Program STOPPED"

STOP

 

_______________________________________________________________________________________________________________


 

CHIP12BEGIN

>...

CHIP12END

 

Syntax

CHIP12BEGIN

> - Instruction

>...

>...

CHIP12END

 

Overview

Incorporate in-line CHIP-12 Instruction codes and labels in the BASIC program. The instructions are compiled exactly as they are in between the CHIP12BEGIN and CHIP12END. Any syntax or formatting errors will be flagged by the compiler.

This allows a great deal of flexibility that cannot always be achieved using BASIC commands alone. The compiler will resolve all label and offset addresses.

Note: You can also use these directives for data and calling machine language subroutines.

 

Example 1

See Example 1 in Call

 

Example 2

See Examples for LoadLargeFont and LoadSmallFont

_______________________________________________________________________________________________________________

 

CHR

 

Syntax

CHR(Number or Variable)

 

Overview

Returns a String containing the ASCII character associated with the specified character code.

 

Operators

Number  - a value from  0 to 255. If number is greater than 255, only the least significant byte (LSB) will be used.

Variable  - if data type is larger than a byte, only the LSB will be used.

 

Example

Dim TestCHR As Word=12345 '3039 in hexadecimal

Print "The hex value of TestCHR is ",HEX(TestCHR)

Print "The ASCII character code of TestCHR is ";CHR(TestCHR)

Print "The ASCII character code 66 is ";CHR(66)

 

_______________________________________________________________________________________________________________

 

CLS

 

Syntax 1

CLS

 

Syntax 2

CLS({Number1,Number2} or {Variable1,Variable2})

 

Syntax 3

CLS(Number or Variable)

 

Overview

Syntax 1 and 2 are used only for alphanumeric graphic modes 0 to 6.

Clears the screen, resets cursor at X,Y coordinates to 0,0 (Top - Left),background is Black, character or foreground is White.

 

Syntax 3 is used only for graphic modes 7 to 9.

Clears or sets the background to Color in Number or Variable, resets cursor at X,Y coordinates to 0,0 (Top - Left)

 

Operators

Syntax 1

None

Syntax 2 and 3

Number or Variable - Range is from 0 to 255.

NOTE: You may use data types other than Byte for variable, but take into account that the compiler only uses the value contained in the LSB.

Syntax 2

NOTE: Parameters must be of the same type. It's either 2 numbers or 2 variables

Number1 or Variable1 is the ASCII character code that will be displayed (See Character Table at the end of document).

Number2 or Variable2 is Color. See Color statement.

 

Example 1

'Not much to this one right! :)

CLS

 

Example 2

Dim BackGround As Byte=3

Dim ForeGround As Byte=2

DoItAgain:

SCREEN 2

'Displays ASCII character code 2 in Yellow

CLS(2,14)

Delay(500)

'Displays ASCII character code 3 in Green

CLS(BackGround,ForeGround)

Delay(500)

SCREEN 8

'Background is Cyan

CLS(BackGround)

Delay(500)

'Background is Magenta

CLS(5)

Delay(500)

Goto DoItAgain

 

 

_______________________________________________________________________________________________________________

 

Color

 

Syntax 1

Color BackgroundColor, CharacterColor, {Blink}

Syntax 2

Color(Number or Variable)

 

Overview

Syntax 1 is used to set the background color, character color, and blink in alphnumeric modes 0 to 6.

If you use syntax 1 in modes 7 to 9, the BackgroundColor stays black and the CharacterColor is set to the index color of the current palette. Usually the colors are as contained in the default VGA palette as shown in the Palette section unless another custom palette was loaded.

Can use Syntax 2 for all video modes.

Use table shown below to find the appropriate color attribute byte in alphanumeric modes 0 to 6.

In modes 7 to 9, the color is set to the index color of the current palette as described above.

 

Operators

BackgroundColor - may be a variable or one of the following constants: Black, Blue, Green, Cyan, Red, Magenta, Brown, White

CharacterColor -  may be a variable or one of the following constants: Black, Blue, Green, Cyan, Red, Magenta, Brown, White, Gray, Light Blue, Light Green, Light Cyan, Light Red, Light Magenta, Yellow, Bright White

Blink - Optional. Value may be a number or variable;

         - a value of zero (0) will turn character blinking off - default if missing

         - a non-zero value will turn character blinking on

Number or Variable - value can be from 0 to 255. If number is greater than 255, the compiler will use 255.

NOTE: You may use data types other than Byte for variable, but take into account that the compiler only uses the value contained in the LSB.

 

The following table shows the color display attributes byte:

Background

 

 

Foreground

 

 

Bits (7,6,5,4)

F  R  G  B

Number

Color

Bits (3,2,1,0)

I  R  G  B

Number

Color

0  0  0  0

0  0  0  1

0  0  1  0

0  0  1  1

0  1  0  0

0  1  0  1

0  1  1  0

0  1  1  1

1  0  0  0

1  0  0  1

1  0  1  0

1  0  1  1

1  1  0  0

1  1  0  1

1  1  1  0

1  1  1  1

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

Black

Blue

Green

Cyan (Cyan)

Red

Magenta

Brown

White

Black blink

Blue blink

Green blink

Cyan blink

Red blink

Magenta blink

Brown blink

White blink

0  0  0  0

0  0  0  1

0  0  1  0

0  0  1  1

0  1  0  0

0  1  0  1

0  1  1  0

0  1  1  1

1  0  0  0

1  0  0  1

1  0  1  0

1  0  1  1

1  1  0  0

1  1  0  1

1  1  1  0

1  1  1  1

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

Black

Blue

Green

Cyan (Cyan)

Red

Magenta

Brown

White

Gray

Light Blue

Light Green

Light Cyan

Light Red

Light Magenta

Yellow

Bright White

F = Flashing bit

R = Red bit

G = Green bit

I = Intensity bit

B = Blue bit

 

 

Example

Color Blue, Yellow  'Blue background, Yellow character and since 3rd parameter missing - no blinking

Print "Blue background, Yellow character and since 3rd parameter missing - no blinking"

Color Red, Bright White, 1 'Red background, Bright White character and blinking on

Print "Red background, Bright White character and blinking on"

 

(Of course, if you compile and run this example, the white text in the red background will be flashing)

 

_______________________________________________________________________________________________________________

 

CopyBlock

 

Syntax

CopyBlock(Source Memory Address, Destination Memory Address, Number of Bytes To Copy)

 

Overview

Copies a number of bytes from one place to another.

 

Operators

Source Memory Address - can be a Number, Variable or @Label. Must use '@' symbol to differentiate between a Variable and Address Label. Uses CHIP-12 Memory Pointer 0. Therefore, if you need to access any other Segment besides the default Segment 2, use DefMemPtr(MH0)=Segment Address.

Destination Memory Address - can be a Number, Variable or @Label. Uses CHIP-12 Memory Pointer 1. Therefore, if you need to access any other Segment besides the default Segment 2, use DefMemPtr(MH1)=Segment Address.

Number of Bytes To Copy - can be a Number or Variable. Maximum number of bytes that can be copied is 65535.

 

Example

Sub Display_Mem_Block

  For StartMem=StartMem To EndMem

    Dim BlockByte As Byte

    BlockByte=Peek(2),StartMem

    Print CHR(BlockByte);

  Next

  Print

End Sub

 

Color Blue,Yellow

Print "Block1 Contents=";

Dim StartMem As Word=@Block1

Dim EndMem As Word=@Block1End

Gosub Display_Mem_Block

 

Color Green,Bright White

Print "Block2 before copy=";

StartMem=@Block2

EndMem=@Block2End

Gosub Display_Mem_Block

 

CopyBlock(@Block1,@Block2,20)

 

Color Red,Bright White,1

Print "Block2 after copy=";

StartMem=@Block2

Gosub Display_Mem_Block

STOP

 

CHIP12BEGIN

@Block1

* 01

$ Copy Block Example

@Block1End

* 01

@Block2

* 02

$ Thistextwillchange

@Block2End

* 02

CHIP12END

 

_______________________________________________________________________________________________________________

 

 

 

CSTR

 

Syntax

StringVariable=CSTR(Number or Variable)

 

Overview

Converts the Number or value in Variable to a String.

 

Operators

StringVariable - if limiting length of string, make sure it's big enough to hold the value being converted to a string.

Number  - a value from  0 to 4294967295 (FFFFFFFF).

Variable  - only data types Byte, Word and DWord are allowed.

 

Example

Dim DataStr1 As String(3)

Dim DataStr2 As String(5)

Dim DataStr3 As String(9)

Dim DataStr4 As String(3)

Dim DataStr5 As String(5)

Dim DataStr6 As String(10)

DataStr1=CSTR(123)

Print DataStr1

DataStr2=CSTR(45678)

Print DataStr2

DataStr3=CSTR(999999999)

Print DataStr3

Dim NumByte As Byte=$FF

DataStr4=CSTR(NumByte)

Print DataStr4

Dim NumWord As Word=$FFFF

DataStr5=CSTR(NumWord)

Print DataStr5

Dim NumDWord As DWord=$FFFFFFFF

DataStr6=CSTR(NumDWord)

Print DataStr6

Print CSTR(213)

Print CSTR(56178)

Print CSTR(888888888)

NumByte=$EF

Print CSTR(NumByte)

NumWord=$FEEF

Print CSTR(NumWord)

NumDWord=$FEFEFEFE

Print CSTR(NumDWord)

 

_______________________________________________________________________________________________________________

 

DefMemPtr

 

Syntax

DefMemPtr(MHn or MLn) = Address

 

Overview

Defines High or Low CHIP-12 Memory Pointer.

 

Operators

MH or  ML - Must select if defining the High or Low CHIP-12 Memory Pointer.

n - choose a hexadecimal digit from 2 to F for CHIP-12 Memory Pointer.

Address - can either be a Number(Dec or Hex), Variable or @Label.

 

See Also

Peek, Poke

 

Example

'This program will print the following characters

'0123456789:;<=>?@ABCDEFGHIJKLMONPQRSTUVWXYZ

'directly to the top-left corner screen.

DefMemPtr(MHA) = $B000

Dim MemAddPtr As Word = $8000

Dim Varbyte1 As Byte = 48

Dim Lop As Byte

For Lop = 1 To 43

  'Print character

  Poke(A),MemAddPtr,Varbyte1

  Varbyte1 = Varbyte1 + 1

  MemAddPtr = MemAddPtr + 1

  'Character color will be Bright White

  Poke(A),MemAddPtr,15

  MemAddPtr = MemAddPtr + 1

Next

 

 

_______________________________________________________________________________________________________________

 

DefNewChar

 

Syntax

DefNewChar(CharacterToBeChanged,@NewCharLabel)

 

Overview

Defines a new ASCII character.

 

Operators

CharacterToBeChanged - Required. Can be a number or variable. If numeric variable, only the first Byte will be used.

If string variable, only the first character of the expression will be used.

 

@NewCharLabel - Required. Points to the memory label where the new character data is. The pixel matrix is 8x16.

 

Example 1

 

Print "The following char ':' will change in a couple seconds."

Delay(200)

DefNewChar(":" ,@NEWCHAR)

STOP

CHIP12BEGIN

@NEWCHAR

* FF 18 3C 66 66 C3 99 A5

* A5 99 C3 66 66 3C 18 FF

CHIP12END

 

 

Example 2

Print "The following characters ':', '[', ']' and '";CHR(128);"' will change in a couple seconds."

Delay(200)

DefNewChar(":" ,@NEWCHAR_0)

Dim ChrToChange_1 As String="["

DefNewChar(ChrToChange_1 , @NEWCHAR_1)

Dim ChrToChange_2 As Byte=93  ' Change ] character

DefNewChar(ChrToChange_2,  @NEWCHAR_2)

DefNewChar(128,  @NEWCHAR_3)  ' Change ç character

STOP

CHIP12BEGIN

@NEWCHAR_0

* FF 81 81 81 81 81 81 81

* 81 81 81 81 81 81 81 FF

@NEWCHAR_1

* FF FF FF FF FF FF FF FF

* 81 81 81 81 81 81 81 FF

@NEWCHAR_2

* FF 81 81 81 81 81 81 81

* 81 FF FF FF FF FF FF FF

@NEWCHAR_3

* FF AA AA AA AA AA AA AA

* AA AA AA AA AA AA AA FF

CHIP12END

 

 

_______________________________________________________________________________________________________________

 

Delay

 

Syntax

Delay(Number or Variable)

 

Overview

Delays execution of program

 

Operators

Number or Variable - value can be from 0 to 65535

 

Example

Delay(72) 'Pause execution for 1 second

Delay(4240) 'Pause execution for 1 minute

Delay(Var1) 'Pause execution as per value in Var1

_______________________________________________________________________________________________________________

 

DeleteSprite

 

Syntax

DeleteSprite(Sprite Variable or Number)

 

Overview

Delete the sprite which was shown with ShowSprite.

 

Operators

SpriteVariable or Number - Specify which variable holds the sprite number or just use the sprite number.

 

See Also

DisplaySprite, LoadSprite, MoveSpriteRight, MoveSpriteLeft, MoveSpriteUp, MoveSpriteDown, Palette, SetSpriteXY, SetSpriteX, SetSpriteY, ShowSprite

 

Example

SCREEN 9

Dim Sprite10 As Byte=10 'Use SPRITE 10

LoadSprite(Sprite10,20,15,$B1) 'Load B1.SPR sprite file

ShowSprite(Sprite10, WVRT)

Delay(100)

'Even though this will delete the sprite, it will still show until the next WVRT instruction

DeleteSprite(Sprite10)

_______________________________________________________________________________________________________________

 

Dim

 

Syntax 1

Dim Variable As DataType {=Number or String}

 

Syntax 2

Dim [CHIP-12 Variable] As Numeric Size {=Number}

 

Syntax 3

Dim ArrayName(arraysize) As DataType

 

Overview

Declare either a numeric or string variable.

You can use the Dim statement any where in the program.

With the exception of arrays, you can also initialize the variable at the same time.

In other words, if you assign a value to the variable in the statement, the compiler will generate the instruction code(s) to initialize it at that particular place in your program.

Numeric and String variables are Global. Meaning that once they have been dimensioned, they are accessible throughout the program.

 

Operators

Variable - Required. Can be any alphanumeric character or string.

[CHIP-12 Variable] - Required. Must be 3 characters specifying which CHIP-12 variable enclosed in these open and closed brackets [ ]. In other words, a capital V followed by a hexdecimal byte - [V00] to [VFF].

DataType - Required. The physical size of the variable. Enter one of the following numeric data types: Byte, SByte, Word, SWord, DWord, SDWord, Float, or string data type: String. Variable string size can be from 1 to 79 characters by specifying the desired length in brackets.

Numeric Size - Required. The physical size of the CHIP-12 variable. Enter one of the following: Byte, SByte, Word, SWord, DWord, SDWord. The CHIP-12 variables will be sequential if data type is bigger than Byte. For example, if you select [V7F], and a size Word, the compiler will use V7F and V80 in all instructions and functions. Cannot use String.

Number - Optional. Any numeric representation as previously explained in the Guidelines and special instructions for using the compiler section.

String - cannot be larger than 79 characters or String(size).

 

Example 1

Dim VarByte As Byte

Dim VarWord As Word = 1234

'StrVar1 defaults to maximum size (79 characters)

Dim StrVar1 As String

'StrVar2 cannot be larger than 12 characters

Dim StrVar2 As String(12) = "Hello World!"

Dim [V7F] As Byte 'Allows access to Flag

Dim [VA0] As DWord 'Compiler will use VA0, VA1, VA2 and VA3 in all instructions and functions

 

Example 2

'This program will store 21 random numbers from 0 to 50 in an array

'then it will show all numbers that appear 2 or more times.

'To help differentiate the numbers, 6 different colors is used.

'Press any key to show next series of numbers.

Dim Array1(20) As Byte

Dim Array1Shadow(20) As Byte

Dim [V04] As Byte

Dim ColourCharacter(5) As Byte

ColourCharacter(0)=$9E 'Blue,Yellow,Blink

ColourCharacter(1)=$AF 'Green,Bright White,Blink

ColourCharacter(2)=$BE 'Cyan,Yellow,Blink

ColourCharacter(3)=$CF 'Red,Bright White,Blink

ColourCharacter(4)=$DF 'Magenta,Bright White,Blink

ColourCharacter(5)=$EF 'Brown,Bright White,Blink

Dim ColourIndex As Byte=0

Main:

For Index = 0 To 20

  Array1(Index) = Random(50)

  Array1Shadow(Index) = 0

Next

Dim Index As Byte

For Index = 0 To 20

  Gosub  ShowArrayValueWhite

Next

Dim Position As Byte=0

Do While Position < 20

  If PositionFlag = 1 Then

    ColourIndex = ColourIndex + 1

    If ColourIndex = 6 Then

      ColourIndex=0

    End If

  End If

  Color(ColourCharacter(ColourIndex))

  Dim PositionFlag As Byte=0

  Dim TempVar As Byte=Position

  TempVar=TempVar + 1

  For Index = TempVar To 20

    If Array1(Position) = Array1(Index) And Array1Shadow(Index) = 0 Then

      Array1Shadow(Index) = Array1(Index)

      If PositionFlag = 0 Then

        Dim Xpos As Byte=Position * 3

        'Using [V04] allows you to use Print At without affecting the Y position

        Print At Xpos,[V04];

        Dim SaveIndex As Byte=Index

        Index=Position

        Gosub PrintArray

        Index=SaveIndex 'Restore Index

        PositionFlag = 1

      End If

      Xpos = Index * 3

      Print At Xpos,[V04];

      Gosub PrintArray

    End If

  Next

  Position = Position +1

Loop

Print

Dim KeyVal As Byte

KeyVal=Inkey

Goto Main

 

Sub PrintArray

  Print HEX(Array1(Index));" ";

End Sub

 

Sub ShowArrayValueWhite

  Color(15)

  Gosub PrintArray

End Sub

 

 

Example 3

SCREEN 3

Dim Tries As Word = 10 ' You can initialize a variable when you Dimension it.

Dim Mask As Byte

Mask = $F0 ' When using a numeric type identifier

Dim Counter As DWord

Counter = $ABCD0123

Counter = Counter + 1234567890

Dim Counter2 As SByte

Counter2 = Counter

Dim Final_Result As Float

' Since the number 1.234E19 is much greater than Counter the Final_Result will 1.234E19

Final_Result = 12340000000000000000- Counter

Dim Result As SDWord

Dim Num1 As Word = %1111101001011111

Dim Num2 As Word = $EF

' You can Add, Subtract, Multiply or Divide 2 variables, or a variable and a number, or 2 numbers.

Result = Num1 * Num2

Print Tries

Print Mask

Print Counter

Print Counter2

Print Final_Result

Print Result

_______________________________________________________________________________________________________________

 

DisplaySprite

 

Syntax

DisplaySprite(Sprite Variable,Sprite File,kValue)

 

Overview

Display a sprite file. Use this if sprite does not need to be moved as it cannot be deleted with DeleteSprite.

 

Operators

SpriteVariable - Specify which variable will hold the sprite number.

Sprite File - can use either a number or variable to specify which sprite file to use.

kValue - specify how you want to display the sprite:

5F xx 0k       DISP Vxx #k            DISPLAY SPRITE Vxx - CANNOT BE DELETED WITH DELE Vxx

                                      k = 0 - USES Video at 9000, needs WVRT, Does not check background - FAST

                                                            k = 1 - USES Video at A000, Does not check background - FAST

                                      k = 2 - USES Video at 9000, needs WVRT, Checks background - SLOWER

                                      k = 3 - USES Video at A000, Checks background - SLOWER

 

See Also

DeleteSprite, LoadSprite, MoveSpriteRight, MoveSpriteLeft, MoveSpriteUp, MoveSpriteDown, Palette, SetSpriteXY, SetSpriteX, SetSpriteY, ShowSprite

 

Example

SCREEN 9

Dim Sprite14 As Byte=20 'Use SPRITE 20

SetSpriteXY(Sprite14,46,37)

DisplaySprite(Sprite14,202,1) 'Display CA.SPR sprite file

_______________________________________________________________________________________________________________

 

Do Loop

 

Syntax

Do {Until or While} test condition(s)

   [ statements ] 

Loop

 

Overview

Execute a block of instructions {Until or While} the test condition(s) is(are) true.

Do Loop's may be nested.

 

Operators

The first parameter in the test condition must be a variable followed by a valid logical operator as explained in the Relational Operators and Boolean Operators sections under the Guidelines and special instructions for using the compiler.

Test condition(s) is(are) evaluated from left to right. Consider the following example.

 

NOTES: Test condition(s) is(are) also evaluated from left to right for the If-Then-Else-End If statement.

              When using logical operators such as And, Or, and Xor, put at least one space to seperate each parameters.

              For example must have at least one space as shown in red:

                If Var1 And $80=128 Then 'OK

                If Var1 And $80=128 Then 'Not OK

             You cannot use Float and Array variables with Do-Loop.

 

Example

Dim Var1 As Byte

Dim Var2 As Byte

Dim Var3 As Byte=128

 

Do While Var1 <> 40 And Var2 >= 180 Or Var3 = 128

  Print "Var1=",Var1 ," Var2=",Var2," Var3=",Var3

  Input "Var1=",Var1

  Input "Var2=",Var2

  Input "Var3=",Var3

Loop

Print "Done"

 

The compiler will first evaluate if 'Var1 <> 40' is TRUE (1) or FALSE (0). Lets call the result x.

Then it will evaluate if 'Var2 >= 180' is TRUE or FALSE. Lets call the result y.

Then it will And both x and y to produce result, lets call this (x&y).

Then it will evaluate if 'Var3 = 128' is TRUE or FALSE. Lets call the result z.

Then it will Or both (x&y) and z to produce the final result.

_______________________________________________________________________________________________________________

 

Do Inkey

 

Syntax

Do {While or Until}  Inkey{= or <>}'ASCII char' or Variable

    [ statements ] 

Loop

 

Overview

Conditionally executes a group of statements, depending on the character read from the keyboard.

NOTES: Unlike the Do Loop, you can only have one condition. In other words, you cannot use the logical operators And,Or,Xor because this is a special Do statement which will use these CHIP-12 instructions:

42 00 kk       EXIF KEY = #kk                EXEC NEXT INSTR IF KEY = kk

43 00 xx       EXIF KEY = Vxx                EXEC NEXT INSTR IF KEY = Vxx

32 00 kk       SKIF KEY = #kk                SKIP NEXT INSTR IF KEY = kk

33 00 xx       SKIF KEY = Vxx                SKIP NEXT INSTR IF KEY = Vxx

        Cannot use Float and Array variables with Do Inkey.

 

Operators

The first parameter in expression must begin with Inkey.

The second parameter can only be equal (=) or not equal (<>).

The third parameter can be an 'ASCII char' or Variable.

statements - One or more statements following Do...Loop that are executed if condition evaluates to True.

Loop - Required. Terminates the Do...Loop...block.

 

See Also

If Inkey

 

Example

Dim ExitVal As Byte=$58

 

Sub EditMemory

  Print "Now in EditMemory Sub"

  Do Until Inkey <> 'M'

    'Wait till 'M' key released

  Loop

End Sub

 

Sub EditRegs

  Print "Now in EditRegs Sub"

  Do While Inkey = 'R'

    'Wait till 'R' key released

  Loop

End Sub

 

Print "Press 'M' to edit Memory"

Print "      'R' to edit Registers"

Print "      'X' to edit eXit"

Main:

If Inkey = 'M' Then

  Gosub EditMemory

End If

If Inkey = 'R' Then

  Gosub EditRegs

End If

If Inkey = ExitVal Then

  Print "Program is STOPPED"

  STOP

End If

Goto Main

 

_______________________________________________________________________________________________________________

 

Do VOCEnd

 

Syntax

Do {While or Until} VOCEnd{= or <>}0

    { statements }

Loop

 

Overview

Conditionally executes a group of statements, depending on if the VOC file that is currently playing has ended or not.

NOTES: Unlike the Do Loop, you can only have one condition. In other words, you cannot use the logical operators And,Or,Xor because this is a special Do statement which will use these CHIP-12 instructions:

3B 00 00       SKIF END = 0                  SKIP next instruction if VOC ended play

4B 00 00       EXIF END = 0                  EXEC next instruction if VOC ended play

 

Operators

The first parameter in expression must begin with VOCEnd.

The second parameter can only be equal (=) or not equal (<>).

The third parameter can only be 0.

statements - One or more statements following Do...Loop that are executed if condition evaluates to True.

Loop - Required. Terminates the Do...Loop...block.

 

See Also

If VOCEnd, PlayVOC

 

The sound files for the following demo are in the MIX folder in the CHIP12Files.ZIP

 

Example

'VDEMO4.BAS (With Do VOCEnd Loop)

'This will loop through and play VOC files F1, 0A, F2, and 0B.

StartAgain:

Dim PlayCount As Byte=3

Print "Playing F1.VOC"

Sound1:

PlayVOC($F1)

Do While VOCEnd <> 0

Loop

PlayCount = PlayCount - 1

If PlayCount <> 0 Then

  Delay(20)

  Goto Sound1

End If

Delay(20)

PlayVOC(10)

Print "Playing 0A.VOC"

Do Until VOCEnd = 0

Loop

Delay(20)

PlayCount = 4

Print "Playing F2.VOC"

Sound3:

PlayVOC($F2)

Do While VOCEnd <> 0

Loop

PlayCount = PlayCount - 1

If PlayCount <> 0 Then

  Delay(20)

  Goto Sound3

End If

Delay(20)

PlayVOC(11)

Print "Playing 0B.VOC"

Do Until VOCEnd = 0

Loop

Delay(20)

Goto StartAgain

_______________________________________________________________________________________________________________

 

Do WAVEnd

 

Syntax

Do {While or Until} WAVEnd{= or <>}0

    { statements }

Loop

 

Overview

Conditionally executes a group of statements, depending on if the WAV file that is currently playing has ended or not.

NOTES: Unlike the Do Loop, you can only have one condition. In other words, you cannot use the logical operators And,Or,Xor because this is a special Do statement which will use these CHIP-12 instructions:

3A 00 00       SKIF PEND = 0                 SKIP NEXT INSTR IF WAV FILE ENDED PLAY

4A 00 00       EXIF PEND = 0                 EXEC NEXT INSTR IF WAV FILE ENDED PLAY

 

Operators

The first parameter in expression must begin with WAVEnd.

The second parameter can only be equal (=) or not equal (<>).

The third parameter can only be 0.

statements - One or more statements following Do...Loop that are executed if condition evaluates to True.

Loop - Required. Terminates the Do...Loop...block.

 

See Also

If WAVEnd, PlayWAV, PauseWAV, ResumeWAV, StopWAV

 

The sound files for the following demo are in the MIX folder in the CHIP12Files.ZIP

 

Example

'WDEMO4.BAS (With Do WAVEnd Loop)

'This will loop through and play WAV files F1, 0A, F2, and 0B.

Dim WAVFileNum As Byte=10

Dim WAVFileNum2 As Byte=11

StartAgain:

Dim PlayCount As Byte=3

Print "Playing F1.WAV"

Sound1:

PlayWAV($F1)

Do While WAVEnd <> 0

Loop

PlayCount = PlayCount - 1

If PlayCount <> 0 Then

  Delay(20)

  Goto Sound1

End If

Delay(20)

PlayWAV(WAVFileNum)

Print "Playing ",HEX(WAVFileNum),".WAV"

Do Until WAVEnd = 0

Loop

Delay(20)

PlayCount = 4

Print "Playing F2.WAV"

Sound3:

PlayWAV($F2)

Do While WAVEnd <> 0

Loop

PlayCount = PlayCount - 1

If PlayCount <> 0 Then

  Delay(20)

  Goto Sound3

End If

Delay(20)

PlayWAV(WAVFileNum2)

Print "Playing ",HEX(WAVFileNum2),".WAV"

Do Until WAVEnd = 0

Loop

Delay(20)

Goto StartAgain

_______________________________________________________________________________________________________________

 

DrawPattern

 

Syntax

DrawPattern(Address Label,x_Coordinate,y_Coordinate,Height)

 

Overview

Draws, at the X-Y coordinates, a pattern with the number of bytes starting at the memory address label.

Contents of the pattern are XOR’d with the current data on screen. Therefore an unset pixel in a pattern will have no effect on the data currently on screen. A set pixel in a pattern will cause the pixel on screen to be set if it is currently unset, or unset if it is currently set.

In the latter case, a collision flag will also be set to show the pattern data overlapped with existing data on screen.

CHIP-12 V7F is used as the flag and will be set to $01 if any set pixel in the pattern overwrites an existing set pixel on the display, otherwise V7F = $00. See Example 3 further down below.

The pattern color is set with the Color statement.

For those familiar with CHIP-8, this is the same as the DXYN instruction.

For a more advanced and detailed way of drawing a pattern is using the DisplaySprite function.

 

Operators

Address Label - can be a Number, Variable or @Label. Must use '@' symbol to differentiate between a Variable and Address Label. Uses CHIP-12 Memory Pointer 0. Therefore, if you need to access any other Segment besides the default Segment 2, use DefMemPtr(MH0)=Segment Address.

x_Coordinate,y_Coordinate - can be a Number or Variable.

Height - specifies the number of bytes to draw. Number can be between 1 and15. The width of the pattern is fixed at eight pixels (one byte). You can still have a pattern narrower than a byte by just setting the unwanted pixels to the right of the image to 0, as shown below in Example 2.

 

See Also

DefNewChar

 

Example 1

 

 

 

 

 

 

 

Image courtesy of Laurence Scotford - http://laurencescotford.co.uk/

SCREEN 4

Dim Blue As Byte=1

Color(Blue)

DrawPattern(@FacePattern, 0, 0, 9)

'Erase the blue eyes before drawing red eyes

DrawPattern(@Eyes_Mouth, 0, 3, 1)

Dim Red As Byte=4

Color(Red)

DrawPattern(@Eyes_Mouth, 0, 3, 1)

'Erase the blue mouth before drawing cyan mouth

Color(Blue)

DrawPattern(@Eyes_Mouth, 0, 5, 2)

Dim Cyan As Byte=3

Color(Cyan)

DrawPattern(@Eyes_Mouth, 0, 5, 2)

STOP

CHIP12BEGIN

@FacePattern

* 3C 42 81 A5 81 A5 99 42 3C

@Eyes_Mouth

* 24 18

CHIP12END


 

Example 2

 

 

Image courtesy of Laurence Scotford - http://laurencescotford.co.uk

 

 

 

 

SCREEN 4

'Default draw color is white

DrawPattern(@DownArrowPattern, 0, 0, 9)

STOP

CHIP12BEGIN

@DownArrowPattern

* 20 20 20 20 20 20 F8 70 20

CHIP12END

 

Example 3

 

 

Image courtesy of Laurence Scotford - http://laurencescotford.co.uk

SCREEN 4

'Default draw color is white

'1st draw screen data before pattern is displayed

DrawPattern(@SquareBlock, 0, 4, 5)

'Then draw pattern

DrawPattern(@FacePattern, 0, 0, 9)

STOP

CHIP12BEGIN

@FacePattern

* 3C 42 81 A5 81 A5 99 42 3C

@SquareBlock

* FF FF FF FF FF

CHIP12END

 

Example 4

Sub ShowByte

  HexDigit=Number SHIFT>> 4

  Gosub ShowHexDigit

  x_Coord=x_Coord+5

  HexDigit=Number And 15

  Gosub ShowHexDigit

End Sub

 

Sub ShowHexDigit

  If HexDigit = 0 Then

    DrawPattern(@HexDigit_0,x_Coord,y_Coord,5)

  Else

    If HexDigit = 1 Then

      DrawPattern(@HexDigit_1,x_Coord,y_Coord,5)

    Else

      If HexDigit = 2 Then

        DrawPattern(@HexDigit_2,x_Coord,y_Coord,5)

      Else

        If HexDigit = 3 Then

          DrawPattern(@HexDigit_3,x_Coord,y_Coord,5)

        Else

          If HexDigit = 4 Then

            DrawPattern(@HexDigit_4,x_Coord,y_Coord,5)

          Else

            If HexDigit = 5 Then

              DrawPattern(@HexDigit_5,x_Coord,y_Coord,5)

            Else

              If HexDigit = 6 Then

                DrawPattern(@HexDigit_6,x_Coord,y_Coord,5)

              Else

                If HexDigit = 7 Then

                  DrawPattern(@HexDigit_7,x_Coord,y_Coord,5)

                Else

                  If HexDigit = 8 Then

                    DrawPattern(@HexDigit_8,x_Coord,y_Coord,5)

                  Else

                    If HexDigit = 9 Then

                      DrawPattern(@HexDigit_9,x_Coord,y_Coord,5)

                    Else

                      If HexDigit = 10 Then

                        DrawPattern(@HexDigit_A,x_Coord,y_Coord,5)

                      Else

                        If HexDigit = 11 Then

                          DrawPattern(@HexDigit_B,x_Coord,y_Coord,5)

                        Else

                          If HexDigit = 12 Then

                            DrawPattern(@HexDigit_C,x_Coord,y_Coord,5)

                          Else

                            If HexDigit = 13 Then

                              DrawPattern(@HexDigit_D,x_Coord,y_Coord,5)

                            Else

                              If HexDigit = 14 Then

                                DrawPattern(@HexDigit_E,x_Coord,y_Coord,5)

                              Else

                                DrawPattern(@HexDigit_F,x_Coord,y_Coord,5)

                              End If

                            End If

                          End If

                        End If

                      End If

                    End If

                  End If

                End If

              End If

            End If

          End If

        End If

      End If

    End If

  End If

End Sub

 

Dim y_Coord As Byte=10

Dim Offset As Byte=5

Dim Colour As Byte=1

Dim HexDigit As Byte=0

Dim Number As Byte=0

SCREEN 4

 

Up:

Dim x_Coord As Byte=12

Gosub ShowByte

 

Print At 0,0, "Enter a number(0 to 255)";

Dim Temp As Byte

Input Temp

 

'Erase number

x_Coord=12

Gosub ShowByte

Number=Temp

 

Colour = Colour + 1

If Colour = 16 Then

  Colour=1

End If

Color(Colour)

Goto Up

 

CHIP12BEGIN

@HexDigit_E

* F0 80

@HexDigit_F

* F0 80

@HexDigit_C

* F0 80 80 80

@HexDigit_B

* F0 50 70 50

@HexDigit_D

* F0 50 50 50

@HexDigit_5

* F0 80

@HexDigit_2

* F0 10

@HexDigit_6

* F0 80

@HexDigit_8

* F0 90

@HexDigit_9

* F0 90

@HexDigit_3

* F0 10 F0 10

@HexDigit_A

* F0 90

@HexDigit_0

* F0 90 90 90

@HexDigit_7

* F0 10 10 10 10

@HexDigit_1

* 60 20 20 20 70

@HexDigit_4

* A0 A0 F0 20 20

CHIP12END

 

 

_______________________________________________________________________________________________________________

 

 

END

 

Syntax

END

 

Overview

Exits from CHIP-12 program and returns to CHIP-12 Command prompt.

 

Operators

None

 

Example

Dim Count As SByte

 

For Count = 100 To 0 Step -1

  If Count = 49 Then

    END

  End If

  Print "Count=";Count;

Next

'This never gets executed

Print"Done"

 

See Also

STOP

_______________________________________________________________________________________________________________

 

ERASETEXTFLAGOFF

ERASETEXTFLAGON

 

Syntax 1

ERASETEXTFLAGOFF

Syntax 2

ERASETEXTFLAGON

 

Overview

These functions allow you to erase the text that was printed using GPrint and GInput when you are using one or more sprites.

Use these only in graphic modes 7 to 9.

 

Operators

None

 

Example

Copy the HappySprites demo at found here or on my website at http://marcpic.com/CHIP12/Happy%20Sprites%20Demo.htm

Add the following at the end:

If Inkey = 't' Then

  ERASETEXTFLAGON

  Dim StrText As String

  GInput "Enter text",StrText

  ERASETEXTFLAGOFF

  GPrint StrText

End If

Goto Main


 

The 'Enter text' will disappear after you press ENTER. What you type will be visible.

(If you put ERASETEXTFLAGOFF after the GPrint StrText, then no text will be visible).

 

_______________________________________________________________________________________________________________

 

For Next

 

Syntax

For Variable = Start To End {Step Number or Variable}

   [ statements ] 

Next

 

Overview

Repeats a group of statements a specified number of times.

For Next's may be nested.

 

Operators

Variable - Required.

Start - Required. The initial value of counter. It can be a Number (Dec or Hex) or Variable.

End - Required. The final value of counter. It can be a Number (Dec or Hex) or Variable. Please read important known issue                    below in NOTES.

Step is Optional. The amount by which counter is incremented or decremented each time through the loop. It can be a signed or unsigned integer Number (Dec or Hex) or Variable.

NOTES: You cannot use Float and Array variables with For Next.

             Remember to use the appropriate data type for the Variable or else you will not get the proper result.

             See Example 2 below.

             Known issue - the statement(s) between the For Next loop is(are) executed at least once. Initially when I wrote the             algorithm for the For Next loop, I put the test at the end whereas I should have put it at the beginning. To change it             would be too time consuming (actually a big headache) and since most of the time you do a For Next loop for the              given range, I have decided to leave it as is.

Example 1

Dim Count As Byte

Dim Max As Byte = 100

 

For Count = 0 To Max Step 20

  Print "Count=",Count

Next

Print "Done"

 

 

 

Example 2

'Example to show when not using the proper data type for the For Next variable

'Of course, if you change the 1001 to 1000, it will work as Count will be 0 on the 5th iteration

Dim Count As Word

 

For Count = 1001 To 1 Step -200

  Print "Count=",Count

Next

Print "Done"

_______________________________________________________________________________________________________________

 

GetFile

 

Syntax

GetFile(MemPtr,"Filename String" or String Variable)

 

Overview

Retrieves all data bytes from a file and stores them in the memory specified by memory pointer.

You must initialize the Memory Pointer with the DefMemPtr statement before using this statement.

Number of bytes read is contained in MLn where n=MemPtr

 

Operators

MemPtr - Required. Enter a hexadecimal digit from 2 to E to specify which memory pointer will be used.

"Filename String" or String Variable  - Required. The file name must comprise of only hexadecimal numbers and must have the following format:

HH.AAA - where H is a hexadecimal digit 0 to F and AAA is an alphanumeric character A to Z and/or number 0 to 9.

NOTE: File must be in the same directory as the CHIP-12 program.

 

See Also

WriteFile

 

Example

Dim NumOfBytes As Word=$1FF

Dim FileName As String="F1.TXT"

 

DefMemPtr(MH2)=$8000

DefMemPtr(ML2)=0

WriteFile(2,FileName,NumOfBytes)

 

DefMemPtr(MH3)=$2000

DefMemPtr(ML3)=0

WriteFile(3,"A2.C12",255)

 

DefMemPtr(MH4)=$1000

DefMemPtr(ML4)=512

GetFile(4,FileName)

 

DefMemPtr(MH5)=$7000

DefMemPtr(ML5)=8192

GetFile(5,"A2.C12")

_______________________________________________________________________________________________________________

 

GetSpriteX

 

Syntax

GetSpriteX(Sprite Variable,Variable)

 

Overview

Gets only the X screen coordinate of the sprite.

 

Operators

Sprite Variable - Specify which variable holds the sprite number.

Variable  - variable to hold the X coordinate.

 

See Also

DeleteSprite, DisplaySprite, LoadSprite, MoveSpriteRight, MoveSpriteLeft, MoveSpriteUp, MoveSpriteDown, Palette,

SetSpriteXY, SetSpriteX, SetSpriteY, ShowSprite

_______________________________________________________________________________________________________________

 

GetSpriteY

 

Syntax

GetSpriteY(Sprite Variable,Variable)

 

Overview

Gets only the Y screen coordinate of the sprite.

 

Operators

Sprite Variable - Specify which variable holds the sprite number.

Variable  - variable to hold the Y coordinate.

 

See Also

DeleteSprite, DisplaySprite, LoadSprite, MoveSpriteRight, MoveSpriteLeft, MoveSpriteUp, MoveSpriteDown, Palette,

SetSpriteXY, SetSpriteX, SetSpriteY, ShowSprite

_______________________________________________________________________________________________________________

 

GInput

 

Syntax

GInput {At xpos,ypos}{SKIPQM}{"Prompt String"}{,}Variable or String{,}{Variable or String}...{, or ;}

 

Overview

Accepts keyboard entries. Same as Input except can only be used with Graphics modes 7, 8, and 9.

 

Operators

Variable or String is required.

At xpos,ypos - Optional. Assigns a specified X-Y position for entry or prompt string.

SKIPQM - Optional. Skips printing the '?'.

Prompt String - Optional. Please note that if you are to include a string variable in the prompt, you must use the '&' sign. See Example 2.

comma - must be used to separate between At, Prompt String and Variables.

Ending comma or semicolon - Optional. If you don't want a Carriage Return / Line Feed after entry.

NOTE: Graphic Input uses sprites to display character fonts, therefore use CLS(16) at the beginning of program if you want to use backspace in the GInput function. 16 is also Black in the default VGA.pal when CHIP12 is started. See Palette.

 

See Also

Input

 

Example 1

SCREEN 7

Dim Xpos As Byte

Dim Ypos As Byte

Dim Colour As Byte

Up:

GInput "Xpos=",Xpos

GInput "Ypos=",Ypos

Colour = Random(14)

Colour = Colour + 1

Color(Colour)

Dim StrText As String

GInput"Enter text Msg",StrText

GPrint At Xpos, Ypos, StrText

Dim KeyVal As Byte

KeyVal=Inkey

CLS(0)

Goto Up

Example 2

'The idea for the following program was taken from Basic Computer Games By David Ahl, page 29 - Batnum

SCREEN 7

Dim C As Byte

Dim Q As SByte

Dim P As SByte

CLS(16) 'Clear screen with 16 so that backspace will erase character

GPrint STRDUP(5, " ");"Battle of Numbers"

GPrint STRDUP(5, " ");STRDUP(17, "*")

GPrint

GPrint "The object of the game is"

GPrint "to alternately remove"

GPrint "objects from an assumed"

GPrint "pile."

GPrint "Winning is defined in"

GPrint "advance as taking the last"

GPrint "object or not."

GPrint "You can also specify if you"

GPrint "or the opponent (computer)"

GPrint "starts first."

GPrint "DO NOT USE ZERO WHILE"

GPrint "PLAYING THE GAME!"

Main:

Dim NumOfObjects As SByte = 0

Do While NumOfObjects <= 0

  GInput "Enter pile size",NumOfObjects

Loop

GPrint "Enter win option:"

  Dim M As SByte = 0

Do While M <= 0 Or M > 2

  GInput "1=Take last, 2=Avoid last:",M

Loop

  Dim A As Byte = 0

  Dim B As Byte = 0

Do While A < 1 Or A > B

  GInput "Enter MIN and MAX ";A,B

Loop

  Dim S As SByte = 0

GPrint "Enter start option:"

Do While S <= 0 Or S > 2

  GPrint "1=Computer first"

  GInput "2=You first:",S

Loop

S = S - 1

C = A + B

GameNotOver:

If S = 0 Then

  Gosub ComputerMove

Else

  Gosub YourMove

End If

If W = 1 Then

  Goto Main

End If

S = S Xor 1

Goto GameNotOver

 

Sub ComputerMove

  Q = NumOfObjects

  If M <> 1 Then

    Q = Q - 1

  End If

  If M = 1 Then

    Goto JUMP680

  End If

  If NumOfObjects > A Then

    Goto JUMP720

  End If

  Dim W As Byte = 1

  GPrint "Computer takes ";NumOfObjects;" and loses!"

  Exit Sub

JUMP680:

  If NumOfObjects > B Then

    Goto JUMP720

  End If

  W = 1

  GPrint "Computer takes ";NumOfObjects;" and wins!"

  Exit Sub

JUMP720:

  P = Q / C

  P = C * P

  P = Q - P

  If P < A Then

    P = A

  Else

    If P > B Then

      P = B

    End If

  End If

  NumOfObjects = NumOfObjects - P

  GPrint "Computer takes ";P;" and leaves ";NumOfObjects

  W = 0

End Sub

 

Sub YourMove

  GPrint "Your move";

YourMoveJump:

  GInput P

  If P = 0 Then

    GPrint "I told you NOT TO USE ZERO!"

    GPrint "Computer wins by forfeit."

    W = 1

    Exit Sub

  End If

  If P >= A And P <= B Or P = NumOfObjects Then

    If P = NumOfObjects Then

      If M = 1 Then

        GPrint "Congratulations, you WIN!"

      Else

        GPrint "Tough luck, you LOSE!"

      End If

      W = 1

      Exit Sub

    Else

      NumOfObjects = NumOfObjects - P

      If NumOfObjects <> 0 Then

        If NumOfObjects >= 0 Then

          W = 0

          Exit Sub

        End If

        NumOfObjects = NumOfObjects + P

      End If

    End If

  End If

  GPrint "Illegal move! Reenter it."

  Goto YourMoveJump

End Sub

_______________________________________________________________________________________________________________

 

Gosub

 

Syntax

Gosub Subname

 

Overview

Branches to and returns from a subroutine. You can place subroutine(s) anywhere in the program.

 

Operators

SubName can be any valid identifier as explained in the Guidelines and special instructions for using the compiler section.

 

See Also

Sub End Sub

 

Example

Dim Lop As Byte

Dim RandomNum As Word=0

 

Sub GetNewNum

  RandomNum = Random(200)

  Print "Random(200)=",RandomNum

  RandomNum = RandomNum + 150

End Sub

 

For Lop=0 To 14

  Gosub GetNewNum

  Print "RandomNum=",RandomNum

Next

 

 

_______________________________________________________________________________________________________________

 

Goto

 

Syntax

Goto Label

 

Overview

Branches to Label.

 

Operators

Label must comprise of alpha-numeric characters.

 

 

Example

UpHere:

Print "Hello World"

Goto UpHere

_______________________________________________________________________________________________________________

 

GPrint

 

Syntax

GPrint {At xpos,ypos}{; or ,}{HEX(variable or number){; or ,}{; or ,}{CHR(variable or number) {; or ,}{"Prompt String"}

{; or ,}{Variable or String}{; or ,}

 

Overview

Displays textual output on the screen. Same as Print except can only be used with Graphics modes 7, 8, and 9.

 

Operators

At xpos,ypos - Optional. Assigns a specified X-Y position on where to start printing.

Prompt String - Optional.

semicolon or comma - must be used to separate between At, Prompt String and Variables.

Ending semicolon or comma - Optional. If you don't want a Carriage Return / Line Feed after printing.

NOTE: contrary to normal BASIC versions where the comma is used to separate values by advancing the cursor to the next tab stop position, semicolons and commas are interchangeable (See example for Print).

 

See Examples 2 and 3 for LoadLargeFont and LoadLargeFont

_______________________________________________________________________________________________________________

 

HEX

 

Syntax

HEX(Number or Variable)

 

Overview

Returns a String representing the hexadecimal value of a number.

 

Operators

Number or Variable - Any numeric value which results up to a maximum of eight hexadecimal characters.

 

Example

Dim TestHEX1 As Byte = 123

Dim TestHEX2 As SByte = -123

Dim TestHEX3 As Word = 23456

Dim TestHEX4 As SWord = -23456

Dim TestHEX5 As DWord = 987612345

Dim TestHEX6 As SDWord = -987612345

 

Print "The hex value of TestHEX1 is ",HEX(TestHEX1)

Print "The hex value of TestHEX2 is ",HEX(TestHEX2)

Print "The hex value of TestHEX3 is ",HEX(TestHEX3)

Print "The hex value of TestHEX4 is ",HEX(TestHEX4)

Print "The hex value of TestHEX5 is ",HEX(TestHEX5)

Print "The hex value of TestHEX6 is ",HEX(TestHEX6)

Print "The hex value of -12 is ",HEX(-12)

Print "The hex value of -2345 is ",HEX(-2345)

Print "The hex value of -19860617 is ",HEX(-19860617)

 

_______________________________________________________________________________________________________________

 

 

If Then

 

Syntax

If condition {And Or Xor condition ...} Then

    [ statements ] 

{ Else 

    [ elsestatements ] } 

End If

 

Overview

Conditionally executes a group of statements, depending on the value of an expression.

 

Operators

condition is a required expression. Must evaluate to True or False. Can use And,Or,Xor between conditions.

The first parameter in expression must begin with a variable.

The second parameter can be one of any relational or boolean operator as explained in the Relational Operators and Boolean Operators sections under the Guidelines and special instructions for using the compiler.

Test condition(s) is(are) evaluated from left to right.

The third parameter can be a variable or a number.

statements - One or more statements following If...Then that are executed if condition evaluates to True.

elsestatements - Optional. One or more statements that are executed if previous condition evaluates to False.

End If - Required. Terminates the If...Then...Else block.

 

NOTES: Test condition(s) is(are) also evaluated from left to right for the Do-Loop statement.

             You can also mask a variable with a number or variable as shown in example 1.

             You can nest If-Then statements as shown in example 2.

              When using logical operators such as And, Or, and Xor, put at least one space to seperate each parameters.

              For example must have at least one space as shown in red:

                If Var1 And $80=128 Then 'OK

                If Var1 And $80=128 Then 'Not OK

 

Example 1

Dim Num1 As Byte

Dim Mask As Byte = $0F

 

Main:

Input "Enter a number (0 to 255)",Num1

If Num1 And Mask < 10 Then

  Print "Character masks into a number from 0 to 9."

Else

  Print "Character does not mask into a number from 0 to 9."

End If

Print "Hex value of Num1 = ",HEX(Num1)

Goto Main

 

The compiler first And's the value in Num1 with value in Mask, then if the result is less than 10, the condition is True.

 

Example 2

SCREEN 3

Main:

Dim RndNum As Byte

RndNum = Random(7)

If RndNum = 0 Then

  Color White, Black

  Print"Black"

Else

  If RndNum = 1 Then

    Color White, Blue

    Print"Blue"

  Else

    If RndNum = 2 Then

      Color White, Green

      Print"Green"

    Else

      If RndNum = 3 Then

        Color White, Cyan

        Print"Cyan"

      Else

        If RndNum = 4 Then

          Color White, Red

          Print"Red"

        Else

          If RndNum = 5 Then

            Color White, Magenta

            Print"Magenta"

          Else

            If RndNum = 6 Then

              Color White, Brown

              Print"Brown"

            Else

              Color Black, Bright White

              Print"White"

            End If

          End If

        End If

      End If

    End If

  End If

End If

Delay(100)

Goto Main

 

_______________________________________________________________________________________________________________

 

If Inkey

 

Syntax

If Inkey{= or <>}'ASCII char' or Variable Then

    [ statements ] 

End If

 

Overview

Conditionally executes a group of statements, depending on the character read from the keyboard.

NOTES: Unlike the If Then, you can only have one condition. In other words, you cannot use the logical operators And,Or,Xor because this is a special If statement which will use these CHIP-12 instructions:

42 00 kk       EXIF KEY = #kk                EXEC NEXT INSTR IF KEY = kk

43 00 xx       EXIF KEY = Vxx                EXEC NEXT INSTR IF KEY = Vxx

32 00 kk       SKIF KEY = #kk                SKIP NEXT INSTR IF KEY = kk

33 00 xx       SKIF KEY = Vxx                SKIP NEXT INSTR IF KEY = Vxx

        Cannot use Float and Array variables with If Inkey.

 

Operators

The first parameter in expression must begin with Inkey.

The second parameter can only be equal (=) or not equal (<>).

The third parameter can be an 'ASCII char' or Variable.

statements - One or more statements following If...Then that are executed if condition evaluates to True.

End If - Required. Terminates the If...Then...block.

 

See Also

Do Inkey

 

Example

Dim ExitVal As Byte=$58

 

Sub EditMemory

  Print "Now in EditMemory Sub"

  Do Until Inkey <> 'M'

    'Wait till 'M' key released

  Loop

End Sub

 

Sub EditRegs

  Print "Now in EditRegs Sub"

  Do While Inkey = 'R'

    'Wait till 'R' key released

  Loop

End Sub

 

Print "Press 'M' to edit Memory"

Print "      'R' to edit Registers"

Print "      'X' to eXit"

Main:

If Inkey = 'M' Then

  Gosub EditMemory

End If

If Inkey = 'R' Then

  Gosub EditRegs

End If

If Inkey = ExitVal Then

  EXIT

End If

Goto Main

_______________________________________________________________________________________________________________

 

If SpriteX

If SpriteY

 

Syntax 1

If SpriteX(Sprite Variable){= ; <> ; > or < } Number or Variable Then

    { statements }

End If

Syntax 2

If SpriteY(Sprite Variable){= ; <> ; > or < } Number or Variable Then

    { statements }

End If

 

Overview

Conditionally executes a group of statements, depending on if the X or Y is equal to or not equal to zero.

NOTES: Unlike the If Then, you can only have one condition. In other words, you cannot use the logical operators And,Or,Xor because these are special If statements which will use these CHIP-12 instructions:

F0 xx kk       SKIF Vxx X = #kk              SKIP NEXT INSTR IF SPRITE Vxx X = kk

F1 xx kk       SKIF Vxx Y = #kk              SKIP NEXT INSTR IF SPRITE Vxx Y = kk

F2 xx yy       SKIF Vxx X = Vyy              SKIP NEXT INSTR IF SPRITE Vxx X = Vyy

F3 xx yy       SKIF Vxx Y = Vyy              SKIP NEXT INSTR IF SPRITE Vxx Y = Vyy

F4 xx kk       SKIF Vxx X > #kk              SKIP NEXT INSTR IF SPRITE Vxx X > kk

F5 xx kk       SKIF Vxx Y > #kk              SKIP NEXT INSTR IF SPRITE Vxx Y > kk

F6 xx kk       SKIF Vxx X < #kk              SKIP NEXT INSTR IF SPRITE Vxx X < kk

F7 xx kk       SKIF Vxx Y < #kk              SKIP NEXT INSTR IF SPRITE Vxx Y < kk

F8 xx kk       EXIF Vxx X = #kk              EXEC NEXT INSTR IF SPRITE Vxx X = kk

F9 xx kk       EXIF Vxx Y = #kk              EXEC NEXT INSTR IF SPRITE Vxx Y = kk

FA xx yy       EXIF Vxx X = Vyy              EXEC NEXT INSTR IF SPRITE Vxx X = Vyy

FB xx yy       EXIF Vxx Y = Vyy              EXEC NEXT INSTR IF SPRITE Vxx Y = Vyy

FC xx kk       EXIF Vxx X > #kk              EXEC NEXT INSTR IF SPRITE Vxx X > kk

FD xx kk       EXIF Vxx Y > #kk              EXEC NEXT INSTR IF SPRITE Vxx Y > kk

FE xx kk       EXIF Vxx X < #kk              EXEC NEXT INSTR IF SPRITE Vxx X < kk

FF xx kk       EXIF Vxx Y < #kk              EXEC NEXT INSTR IF SPRITE Vxx Y < kk

        Cannot use Float and Array variables with If SpriteX and If SpriteY.

 

Operators

SpriteVariable - Specify which variable will hold the sprite number.

The second parameter can be equal (=); not equal (<>); greater than (>) or Less than (<).

The third parameter can be a Number or Variable.

statements - Optional. One or more statements following If...Then that are executed if condition evaluates to True.

End If - Required. Terminates the If...Then...block.

 

Example

See Tutorial - How to create and use Sprites

_______________________________________________________________________________________________________________

 

If Timer

 

Syntax

If Timer{= or <>}0 Then

    { statements }

End If

 

Overview

Conditionally executes a group of statements, depending on if the Timer is equal to or not equal to zero.

NOTES: Unlike the If Then, you can only have one condition. In other words, you cannot use the logical operators And,Or,Xor because this is a special If statement which will use these CHIP-12 instructions:

48 00 00       EXIF TIMR = 0                 EXEC NEXT INSTR IF TIMER = 0

38 00 00       SKIF TIMR = 0                 SKIP NEXT INSTR IF TIMER = 0

 

Operators

The first parameter in expression must begin with Timer.

The second parameter can only be equal (=) or not equal (<>).

The third parameter can only be 0.

statements - Optional. One or more statements following If...Then that are executed if condition evaluates to True.

End If - Required. Terminates the If...Then...block.

 

See Also

Timer

 

Example

Dim TmrValue As Byte=30

Timer(TmrValue)

Main:

If Timer = 0 Then

  Color Green, Bright White

  Print"Timer is 0"

  Timer(30)

End If

If Timer <> 0 Then

  Color Blue, Yellow

  Print"Timer is not 0"

  Delay(100)

End If

Goto Main

 

_______________________________________________________________________________________________________________

 

If VOCEnd

 

Syntax

If VOCEnd{= or <>}0 Then

    { statements }

End If

 

Overview

Conditionally executes a group of statements, depending on if the VOC file that is currently playing has ended or not.

NOTES: Unlike the If Then, you can only have one condition. In other words, you cannot use the logical operators And,Or,Xor because this is a special If statement which will use these CHIP-12 instructions:

3B 00 00       SKIF END = 0                  SKIP next instruction if VOC ended play

4B 00 00       EXIF END = 0                  EXEC next instruction if VOC ended play

 

Operators

The first parameter in expression must begin with VOCEnd.

The second parameter can only be equal (=) or not equal (<>).

The third parameter can only be 0.

statements - Optional. One or more statements following If...Then that are executed if condition evaluates to True.

End If - Required. Terminates the If...Then...block.

 

See Also

Do VOCEnd, PlayVOC

 

The sound files for the following demo are in the MIX folder in the CHIP12Files.ZIP

 

Example

'VDEMO2.BAS (With If VOCEnd Then)

'This will loop through and play VOC files F1, 0A, F2, and 0B.

StartAgain:

Dim PlayCount As Byte=3

Print "Playing F1.VOC"

Sound1:

PlayVOC($F1)

Sound1NotFinished:

If VOCEnd <> 0 Then

  Delay(20)

  Goto Sound1NotFinished

End If

PlayCount = PlayCount - 1

If PlayCount <> 0 Then

  Delay(20)

  Goto Sound1

End If

Delay(20)

PlayVOC(10)

Print "Playing 0A.VOC"

Sound2:

If VOCEnd <> 0 Then

  Delay(20)

  Goto Sound2

End If

Delay(20)

PlayCount = 4

Print "Playing F2.VOC"

Sound3:

PlayVOC($F2)

Sound3NotFinished:

If VOCEnd <> 0 Then

  Delay(20)

  Goto Sound3NotFinished

End If

PlayCount = PlayCount - 1

If PlayCount <> 0 Then

  Delay(20)

  Goto Sound3

End If

Delay(20)

PlayVOC(11)

Print "Playing 0B.VOC"

Sound4:

If VOCEnd <> 0 Then

  Delay(20)

  Goto Sound4

End If

Delay(20)

Goto StartAgain

_______________________________________________________________________________________________________________

 

 

If WAVEnd

 

Syntax

If WAVEnd{= or <>}0 Then

    { statements }

End If

 

Overview

Conditionally executes a group of statements, depending on if the WAV file that is currently playing has ended or not.

NOTES: Unlike the If Then, you can only have one condition. In other words, you cannot use the logical operators And,Or,Xor because this is a special If statement which will use these CHIP-12 instructions:

3A 00 00       SKIF PEND = 0                 SKIP NEXT INSTR IF WAV FILE ENDED PLAY

4A 00 00       EXIF PEND = 0                 EXEC NEXT INSTR IF WAV FILE ENDED PLAY

 

Operators

The first parameter in expression must begin with WAVEnd.

The second parameter can only be equal (=) or not equal (<>).

The third parameter can only be 0.

statements - Optional. One or more statements following If...Then that are executed if condition evaluates to True.

End If - Required. Terminates the If...Then...block.

 

See Also

Do WAVEnd, PlayWAV, PauseWAV, ResumeWAV, StopWAV

 

The sound files for the following demo are in the MIX folder in the CHIP12Files.ZIP

 

Example

'WDEMO2.BAS (With If WAVEnd Then)

'This will loop through and play WAV files F1, 0A, F2, and 0B.

Dim WAVFileNum As Byte=10

Dim WAVFileNum2 As Byte=11

StartAgain:

Dim PlayCount As Byte=3

Print "Playing F1.WAV"

Sound1: