
Commodore Basic as a Scripting Language - harel
https://github.com/mist64/cbmbasic
======
hnlmorg
While this is cool pet project (intentional pun), Microsoft Basic 6502 was, in
my personal opinion, one of the worst BASIC implementations of that era. If
we're going early 80s BASIC then I'd prefer to use BBC BASIC which supported
functions amongst other things -- albeit still not as readable as modern
structured programming.

There's still a few BASIC implementations floating about for modern systems
too. FreeBASIC ([https://www.freebasic.net/](https://www.freebasic.net/)) is
one worth checking out.

Personally I still have quite a few of those 80s micro computers and
occasionally hook them up to teach my kids programming.

~~~
bartread
Came here to say this. Commodore BASIC (which was really by Microsoft, as
you've pointed out) was by far my least favourite of the 8-bit BASICs I used.

It was even worse than Sinclair BASIC due to the lack of drawing commands
(yes, I know you could get these as extensions on a cartridge - I didn't have
that). The main issues with Sinclair BASIC were limited functions and the fact
you had to use key combinations to enter most keywords.

I also remember it as being slow as all hell when compared to other BASICs,
although that might just be the opposite of whatever rose-tinted nostalgia is.

I loved the Commodore 64 but its BASIC _sucked_. Commodore continued to
royally eff up with Amiga BASIC which, if memory serves, was also basically[1]
by Microsoft. I immediately switched over to AMOS by Francois Lionet, which
was great, although with hindsight wish I'd given Blitz a go too.

 _[1] Pun also intended, even though dreadful._

~~~
simmons
To be fair to Microsoft, the C64 BASIC was notoriously outdated when the C64
shipped. Jack Tramiel managed to buy an unlimited-use license to Microsoft's
BASIC in 1977, and was determined to get his money's worth. On the bright
side, the C64 BASIC was great at motivating new programmers to learn assembly
programming so they could avoid it, and in the process finding out how
computers _really_ work. :)

~~~
lstodd
This. I remember the C64 BASIC mostly as a glorified loader for the machine
code. I even typed in a monitor published in some magazine just so I could
push the machine nearer its limits.

~~~
fit2rule
Cut my teeth on Oric Atmos BASIC, and I can verify that my eyes still scan
magazine listings intuitively looking for the DATA statements that indicate
fast BLIT code ..

------
abiogenesis
[https://github.com/mist64/cbmbasic/blob/master/runtime.c](https://github.com/mist64/cbmbasic/blob/master/runtime.c):

    
    
        * CBMBASIC implements CHRGET/CHRGOT as self-modifying
        * code in the zero page.
    

I did quite a bit of programming on the C64 but I didn't know that. I always
thought that, although there is no technical reason not to, zero page is only
used for storing data and not code.

~~~
classichasclass
It's entirely for speed reasons. It's a very hot routine and exists in RAM to
facilitate speedily loading and updating the BASIC text pointer, which is
embedded within it, so having it in zero page optimizes it further. Modifying
this code fragment to point to your own code and add additional commands to
BASIC was called a "wedge" and for some systems was the only easy way to do
so. It is a central component of all 6502 Microsoft BASIC variants. See
[https://www.pagetable.com/?p=774](https://www.pagetable.com/?p=774) for the
routine in its entirety.

~~~
AlbertoGP
Wow, I did my own BASIC extension back in the day and just now I learned from
you where the name “wedge BASIC” came from. Thanks!

------
tenebrisalietum
Interesting.

However, there's really nothing special about Commodore BASIC 2.0. It was a
very basic Microsoft BASIC; not as minimal as Apple Integer BASIC but not as
good as something like GW-BASIC on the PC.

The Commodore BASIC 2.0 supported none of the hardware features of the C64
that weren't provided by the Commodore Kernal through PRINT statements. So you
can clear the screen and change text color through embedding the appropriate
control codes in strings or use ASC function, but accessing the VIC-II or SID
had to be done through assembly or PEEKs and POKEs. So honestly making it a
general scripting runtime can work easily.

If you want to use a real BASIC as a scripting language you want to check out
FreeBASIC. It's an reimplementation of Microsoft's QBASIC with OOP and other
extensions.

------
noobermin
The main interpreter (cbmbasic.c) seems to be generated code but it's not
clear where or how it was generated (specifically, what it was generated
from). The rest of the code looks for the most part looks handwritten.

~~~
lokedhs
I read about this project when it came out. If I understand correctly, it's
actually the original C64 implementation, decompiled into C. It's a really
neat implementation.

~~~
skissane
Yes. From reading cbmbasic.c, I can see a couple of things clearly:

1) This code was generated by the LLVM C-backend (llvm_cbe), which converts
LLVM IR to C code

2) The code contains variables for the 6502 registers.

This suggests it was generated by translating 6502 machine code to LLVM IR,
and then converting the LLVM IR to C.

~~~
bdowling
The structure of a loop around a big switch statement (based on value at PC)
to blocks of code that manipulate the register variables (A, X, Y, PC, etc)
and RAM looks exactly like an emulator core.

The code also appears to include the Commodore BASIC ROM. (See initialization
of llvm_cbe_ROM in cbmbasic.c).

So, it looks like an emulator that was written in some other language then
converted to C using the LLVM C backend.

------
kjs3
If you want BASIC as a scripting language, and VB isn't the answer, then you
can get the remarkably capable BBC BASIC for many platforms for not much money
at
[http://www.bbcbasic.co.uk/bbcbasic.html](http://www.bbcbasic.co.uk/bbcbasic.html).

A couple of years ago I got an software exception request (regulated
industry...don't ask) from a developer who wanted permission to use this. No
one had any idea what to make of it, so it bubbled up to me. Turns out the
developer was British, started out on BBC BASIC, and even though nothing in
the app was BASIC, he'd written an amazingly comprehensive testing scaffold in
it. I granted the request on the condition that they train at least 2 other
devs to maintain it. It was impressive enough I bought my own copy and used it
for various scripting tasks on Windows until PowerShell became what the cool
kids used.

------
pharaohgeek
I love this. I got my start programming in BASIC on my C64 at the ripe old age
of 6. While something like this has little practical value, it certainly
provides a lot of nostalgia for someone like me.

------
burlesona
Last updated in 2014... should it have 2014 in the headline?

This is kind of neat, OP what interested you about it to submit it?

~~~
harel
I originally submitted it a while ago and it failed to attract attention. I
received a note from HN inviting me to resubmit so I did. I love it. I learned
to program the VIC-20 before I could properly speak English. The experience is
so embedded in my psyche that I couldn't NOT re/post this.

------
ivanhoe
But can we POKE in it? :)

------
jwr
Might very well be a better solution for writing software than bash, which
seems to be the trend lately.

