What it is

BasicV2 is an attempt to write a BASIC interpreter/compiler in Java that is compatible with the BASIC dialect that older Commodore home computers (Commodore on WikiPedia) were using. It can run BASIC programs written in BASIC V2 or ANSI BASIC.
It doesn't use any parts of the implementation of the original Commodore BASIC like similar projects do. It's a complete reimplementation in Java.

What it isn't

It's not an emulator! It doesn't provide any of the graphics or sound capabilities of a C64 or a VIC20. It just runs the programs and outputs the results onto the console. In addition, it supports native cross compilation to compile your BASIC V2 programs to executables that you can run on your C64 or in an emulator.

How is works

It's basically two solutions in one. You can either compile the BASIC programs so that you can run them inside a Java environment/application using the runtime that the project provides. Or you can use the native cross compiler to compile it for the real C64, VIC20 or the Commander X16 (not(!) the Commodore 16) and run it on the actual machine or in an emulator. You'll find this compiler called MoSpeed as a command line application in the dist-directory of the project. It can be run on its own, you don't have to download/clone the whole project for this. There's also an experimental BASIC V2 -> Javascript compiler included. The result of that one is a set of html/js files that can be run in a browser.

Is it BASIC only?

No, not anymore. It also includes a symbolic assembler for 6502 assembly language and a 6502 emulation layer to execute the compiled assembler programs. In contrast to the BASIC compilation, assembler will actually by compiled to real machine language just like a C64 understands and executes it.

Compatibility

All Basic V2 commands are implemented and all should behave in the way in which they would on a real machine. But because the actual hardware isn't emulated, no PEEKs or POKES will do anything useful and no SYS calls will execute anything (unless you change the corresponding listener by setting a RamSystemCallListener instead, which actually will execute machine code stored in RAM by using a 6502 emulator that's included in the project.)
However, the implementation supports 64KB of memory from which programs can read and in which they can write by using PEEKs and POKEs.
File I/O is supported to a certain degree, i.e. you can read and write files to virtual, in-memory discs and tapes, but disc commands and such aren't supported.

Limitations

As mentioned, the Java runtime doesn't really support any of the hardware features of Commodore's machines, so any program that manipulates memory addresses or uses control characters or PETSCII characters will not run as intended. It will most likely run without crashing but the visual or oral output that the program intends to generate simply won't be there.
If you use the native compiler, these features are all supported, of course.
Error messages differ from the ones of the original interpreter. Some are better and more detailed while others might be not as good.
It has no direct mode. One could emulate it by adding some additional code, but it comes without it. Commands only feasible in direct mode will stop the program or do nothing because of this.

Enhancements

Compared to the original BASIC V2 interpreter, this one features some enhancements:

  1. No memory limitation of 38911 bytes. BASIC programs can be as large as the JVM's memory settings allow them to be.
  2. Virtually no line number limitations. Line numbers can exceed 63999 without a problem.
  3. Lower and upper case can both be used and even mixed in a BASIC program.
  4. Integer numbers aren't limited to 16bit anymore, but to 32bit. Real numbers are mapped to Java's floats, which provides a higher accuracy and range.
  5. It's much faster.

Speed

It's not as fast as plain Java, of course. But it's actually pretty fast. On a 4Ghz Core i7, it's approx. 19,000-60,000 times faster than a C64 when it comes to number crunching.
Compared to PHP and tested with a simple prime number calculation (you can find an example in /src/test/resources/prime2.bas) it was 6 times faster than PHP 5.6 and 3.5 times faster than PHP 7 on the same machine.

How to use

You can find basic usage examples in the test directory. Running a BASIC program is quite simple:

String[] code= Loader.loadProgram("src/test/resources/basic/example.bas");
Basic inty = new Basic(code);
inty.run();

Compiling and running an assembler program isn't very challenging either:

String[] code = Loader.loadProgram("src/test/resources/asm/example3.asm");
Assembler asm = new Assembler(code);
asm.run();

You can combine both (BASIC and assembler) as well. Have a look at the "mixed" examples for more details on how to do that.

There are some more options, but that's basically it.
In addition, you can use it as a templating engine. You can find an example for this in the TemplateTest.java file.

Why? Just...why?

Because I could.

Support or Contact

Having trouble? Send me an email.