Jump to content
desertfish

new Prog8 compiler released that supports cx16

Recommended Posts

Posted (edited)

So after piecing together the memory map of the Commander X16 and getting the emulator working I set out to make the necessary changes in my programming language Prog8 compiler to support a second target machine architecture beside the C64.

Prog8 is a cross-compiler that works on any modern machine and compiles into a machine code binary for 6502 8-bit machine targets such as the Commodore-64.  I've just released an updated version that supports CommanderX16 as a second compilation target!

Some micro-benchmarks (not by me) to compare it to other languages and compilers such as cc65 can be seen here.

Here is one of the simple programs that I made in Prog8 for the Cx16, it renders the well-known Mandelbrot fractal (using floating point calculations):

image.png.af44f93c6f3e80ffbf9d0953b0194625.png

 

For those interested this is the source code of the above program:

%import textio
%import floats
%zeropage basicsafe

main {
    const uword width = 60
    const uword height = 50
    const ubyte max_iter = 16

    sub start()  {
        txt.print("calculating mandelbrot fractal...\n\n")

        ubyte pixelx
        ubyte pixely

        for pixely in 0 to height-1 {
            float yy = (pixely as float)/0.40/height - 1.3

            for pixelx in 0 to width-1 {
                float xx = (pixelx as float)/0.32/width - 2.2

                float xsquared = 0.0
                float ysquared = 0.0
                float x = 0.0
                float y = 0.0
                ubyte iter = 0

                while iter<max_iter and xsquared+ysquared<4.0 {
                    y = x*y*2.0 + yy
                    x = xsquared - ysquared + xx
                    xsquared = x*x
                    ysquared = y*y
                    iter++
                }
                txt.color2(1, max_iter-iter)
                txt.chrout(' ')
            }
            txt.chrout('\n')
        }
    }
}

 

Edited by desertfish
added link to micro-benchmarks
  • Like 6

Share this post


Link to post
Share on other sites

I just updated the post to mention the availability of the compiler now and where it can be found.

I'm excited to improve the Commander-X16 support even further in future releases!

One improvement I'm already thinking of, is perhaps making the set of 16-bit pseudo registers part of the language so you don't have to access them anymore as zeropage variables.

Share this post


Link to post
Share on other sites

Nice one.

What editor are you using for syntax highlighting in those screenshots ? 

 

Share this post


Link to post
Share on other sites

Incredible work you have done! You must have worked really hard to accomplish this. I am very happy to see that you have added support for the X16. It is a great gift to the community.

  • Thanks 1

Share this post


Link to post
Share on other sites
Posted (edited)

@Bones it's copied from the editor window from IntelliJ IDEA where I configured the major syntax highlighting rules for my language syntax. Which is incredibly easy to do in IDEA. To be honest, it was quite unexpected that pasting the source in here retained the coloring!  (it's not a screenshot- it's the actual source code pasted as text)

@Johan Kårlin Thank you so much for the kind words it means a lot to me. Yeah it has been a long time coming and there's still much work left to do on the code generation part  (projects are never "finished" are they? haha)  but I'm really happy today already with where the language and its compiler are.

Edited by desertfish

Share this post


Link to post
Share on other sites

Version 4.1 was just released with many improvements in the CommanderX16 support.   Most importantly is that floating point operations are now working.  (and a few example programs are now included that use this)

Share this post


Link to post
Share on other sites

Prog8 - that's a very interesting language you developed. What's the history of the language?

Share this post


Link to post
Share on other sites

@geek504 Hi glad you asked, lets answer this here 🙂

I started working on it about three years ago. I wanted to learn how to create a programming language, a parser, and a compiler (I have written several interpreters over the years but never an actual compiler) . I started implementing it in Python but as the project grew larger, switched to Kotlin as an implementation language because it became too unwieldy to constantly refactor it  in Python even with IDE support.

Because I like retro computing and the Commodore-64 in particular, I thought it would be fun to try to target that platform to be able to write programs for it in a medium/high level language.  With the possibility to directly access the hardware and use inline assembler code when needed for the nitty gritty stuff.   Only recently I started adding CommanderX16 support into the compiler.

Prog8 the language itself is essentially a C like langue but with heavy Python influences because I really like Python.

It would be cool if people would use Prog8 for larger projects, but other than the few dozen example programs I've not actually written a large piece of software in Prog8 yet 😄    Mostly because I'm still too busy tinkering with the compiler itself and fixing bugs, and now, CommanderX16 support for it.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
16 hours ago, desertfish said:

@geek504 Hi glad you asked, lets answer this here 🙂

I started working on it about three years ago. I wanted to learn how to create a programming language, a parser, and a compiler (I have written several interpreters over the years but never an actual compiler) . I started implementing it in Python but as the project grew larger, switched to Kotlin as an implementation language because it became too unwieldy to constantly refactor it  in Python even with IDE support.

Because I like retro computing and the Commodore-64 in particular, I thought it would be fun to try to target that platform to be able to write programs for it in a medium/high level language.  With the possibility to directly access the hardware and use inline assembler code when needed for the nitty gritty stuff.   Only recently I started adding CommanderX16 support into the compiler.

Prog8 the language itself is essentially a C like langue but with heavy Python influences because I really like Python.

It would be cool if people would use Prog8 for larger projects, but other than the few dozen example programs I've not actually written a large piece of software in Prog8 yet 😄    Mostly because I'm still too busy tinkering with the compiler itself and fixing bugs, and now, CommanderX16 support for it.

So the result is similar to a compiled basic or a compiled c code? 

Do you use the 65c02 opcodes already? They could speed up certain things (e.g. bittests or bitsets etc).

Share this post


Link to post
Share on other sites

I had to look up what Kotlin was... looks a lot like Pascal (or Delphi). Did you have to re-implement a lexical analyzer in Kotlin or did you use an available library? Did you finish all the grammar for Prog8?

In the mid-90s I did a SPARC assembly compiler using the traditional C tools, lex and yacc. It'll be interesting to dig up that old source code and re-implement for 65c02 and X16. It might just be a waste of time since cc65 already exists... and time is a rare commodity.

Share this post


Link to post
Share on other sites

@geek504 I used the Antlr parser generator library as-is (Kotlin/JVM integrates seamlessly with Java). I did wrap the generated AST classes in easier to use Kotlin versions though.

@SerErris yes Prog8 is compiled into assembly code. The cx16 compiler target shares most of its code with the C64 target so no, most of the time the new 65c02 opcodes are not yet used.  I have been using STZ and PHX/PLX a bit here and there in certain library routines but that's about it.

thanks for your questions!

  • Like 1

Share this post


Link to post
Share on other sites

Those are five of the best instructions, the other three I really like are LDA (zp) : STA (zp)  : SBC (zp) (SBC in particular because the three logic and ADC are commutative so you can typically LDA (zp1) : CLC : ADC a,X : STA (zp2) but subtraction is picky about which one comes first.

Share this post


Link to post
Share on other sites
On 8/30/2020 at 4:41 PM, desertfish said:

@Bones it's copied from the editor window from IntelliJ IDEA where I configured the major syntax highlighting rules for my language syntax. Which is incredibly easy to do in IDEA. To be honest, it was quite unexpected that pasting the source in here retained the coloring!  (it's not a screenshot- it's the actual source code pasted as text)

@Johan Kårlin Thank you so much for the kind words it means a lot to me. Yeah it has been a long time coming and there's still much work left to do on the code generation part  (projects are never "finished" are they? haha)  but I'm really happy today already with where the language and its compiler are.

cool .. 

I should revisit my prog8 ide project. Like so many things I start, it somehow just fell off my radar..

image.thumb.png.375be4723cb06d5fe66d7e7deddfe80d.png

image.thumb.png.d327dac87868697af5181ab532fc4aa1.png

image.thumb.png.4341292d6533c3209d690b49f19d488c.png

 

  • Like 2

Share this post


Link to post
Share on other sites

I just released a new version (4.2) that contains a lot of bug fixes. Most if not all issues I've ran into while attempting to compile for the Cx16 have been solved in this version 😅 .  I've updated the initial post. Download links can still be found there.

  • Like 3

Share this post


Link to post
Share on other sites

The next version is going to introduce an exiting new feature: It can actually compile the exact same program  for both the C64 and the CommanderX16.  As long as you stick to using generic kernel subroutines and Prog8 library subroutines. And don't hardcode 40x25 screen size 🙂

Many of Prog8's example programs can now be compiled for both systems by just flipping a compiler command line option.

 

  • Like 3

Share this post


Link to post
Share on other sites

The upcoming release has some pretty significant improvements in the generated code, sometimes it is now actually slightly faster than equivalent C code compiled by CC65.  That's exciting, but it is only for a few silly benchmarks. Other prog8 code is still slower than equivalent compiled C but I'm quite happy with the steps I've been able to make over the past few releases.

There's already the Cobra MK3 space ship from Elite animating in 3d and my next goal is to port the "text elite" trading engine to Prog8.   However this requires a few fundamental changes to the language such as the ability to have arrays that contain other things than just numbers, and perhaps also arrays that are larger than one page in memory.... 😞     Because there are 256 planets with their properties in a single galaxy.     I have no idea how to solve this problem at this time because the "array is one memory page maximum" is quite fundamental in Prog8

Share this post


Link to post
Share on other sites

Just not related to prog8 but to Elite. You may want to lookup how the 256 planets worked in Elite. (check the source code I linked) It has a very good writeup how that works.

In short:

You have a pseudo random generator that is seeded by the galaxy seed (4 bytes). This seed is then taken through iterative random numbers to allways generate the same parameters for a specific planet. 

So if you feed in 240 into this generator (System Number), it will allways get the same result back to you, which is then feeded into the the System generator and gets you the name and the description. All of that is also generated and not stored anywhere. 

The marked prices are as well generated but also only varried with a random difference. 

/Edit: ah forgot the galaxies ... So if you jump the galaxy. the 4 bytes above will be all rotated left by 1 bit. That results into a different seed for each galaxy and will also return back to 1 for if you jump through 8 galaxies back to the first one. 

Edited by SerErris

Share this post


Link to post
Share on other sites

The whole procedureal generation is the real secret sauce in Elite ... besides the unparalleled graphics of that time.

Also need to correct - the Seed is 6 bytes = 3 words. 

Start reading here:

Sub TT20 and Sub TT54 gives you more or less the whole picture.

TT24 is then calculating the real system data.

Everything comes together than with the tokenized text routines (which is a masterpiece in itself). That will put out the information based on the system data. None of the strings you see on screen can be found anywhere in the code. They are also generated.

That code and description can be found in QQ18.

https://raw.githubusercontent.com/markmoxon/elite-beebasm/master/sources/elite-source.asm

But now enough on that here... it is confusing the original post with non relevant data.

 

Edited by SerErris

Share this post


Link to post
Share on other sites
On 8/27/2020 at 4:30 PM, desertfish said:

One improvement I'm already thinking of, is perhaps making the set of 16-bit pseudo registers part of the language so you don't have to access them anymore as zeropage variables.

That is a great idea and one of the things that should be used and abused!

On 8/31/2020 at 5:52 PM, desertfish said:

It would be cool if people would use Prog8 for larger projects, but other than the few dozen example programs I've not actually written a large piece of software in Prog8 yet 😄    Mostly because I'm still too busy tinkering with the compiler itself and fixing bugs, and now, CommanderX16 support for it.

I have been thinking about NOT starting yet another 6502 BASIC compiler but instead HELP out with Prog8! Gives me a reason to learn yet another programming language (actually two... Kotlin and Prog8). After Googling Kotlin I read that it drastically reduces boilerplate code which was the big reason why I kept Java away from me with a ten-foot pole! I hope to be able to sift through your code without getting bamboozled...

  • Thanks 1

Share this post


Link to post
Share on other sites

That's a bold decision but I really appreciate your interest in helping me with Prog8 !   Any sort of help is welcome.  Documentation improvements, actual compiler code, example programs, usage suggestions, whatever 🙂

About Prog8 if you want to get a hang of how the compiler works don't hesitate to ask me stuff about it, it's a test for me too to see if the code is understandable for others (It should be, if it's not , it has to be improved).  I strongly suggest ignoring the  optimizer folder altogether for a while.   Maybe start with familiarizing yourself with the classes that make up the AST (see the ast folder)  because almost everything else uses that?

About Kotlin: it's WAY nicer than the Java I remember from the past and which I'm avoiding strongly, as you do.  (although Java has had some important improvements since)  Kotlin's overtaking Python for me as a favorite programming language, to be honest.  You can notice that Kotlin is inspiredby Python and the "feel" is somewhat similar.    I needed a fast, good solid language and IDE to support the scale and the dozens of large refactorings I was doing in   the Prog8 project and Kotlin + IntelliJ IDEA fit the bill nicely.

 

About the 16 pseudo registers:  Well, they're not (yet) first class citizens in the Prog8 language. However, because Prog8 allows you to define memory-mapped variables they're pretty close to what I wanted anyway. Youc an already simply write :

cx16.r0 = 32
cx16.r1 = 10
cx16.r2 = 256
cx16.r3 = 185
cx16.r4 = 0
cx16.GRAPH_draw_rect(true)

I suppose the "ideal" way to write this is:

cx16.GRAPH_draw_rect(32, 10, 256, 185, 0, true)

but to support that would require quite hefty changes to the parser and those changes don't really blend well I think with the original language as it should work for the C64

Edited by desertfish

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Please review our Terms of Use