This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Millfork - New 6502 middle-language

Millfork - New 6502 middle-language
by on (#242899)
Hey all, so I was recently researching different options for developing NES games and found a pretty good option that I don't think many people have heard of:
Millfork - https://karols.github.io/millfork/

I found this project while looking for different programming options for the 6502, since straight assembly is fairly hard to maintain/read and programming in C using cc65 can get pretty messy/complex. I was originally considering making something in NESHLA, but since that project's been abandoned for over a decade I decided to look for other options and stumbled upon Millfork.

I've been making several example programs with it for the past couple of days and so far everything seems good, it's fairly easy to write clean readable code, and the compiler's optimizer seems to work well from what I've seen.
I've made my example programs available here: https://github.com/Garydos/millfork-nes-examples

I plan to make more example programs, eventually working my way up to a basic platformer demo so that I can put the language through its paces. The language itself still seems to be early in its development but all the features I've needed so far have worked without any bugs, and the maker seems to update it fairly regularly. I'd recommend reading through the documentation a bit, checking out the example NES programs, and then look at my pong example to get a better idea of how to implement an actual game in this language if you wanna give it a try.

The biggest caveat with this language that I've found so far is its lack of interoperability with other languages. It has its own built-in assembly syntax that's sort of NESASM-like, but for the most part if you want to use say an audio library like famitone or pently, you'll have to port it yourself. I plan to attempt porting over nesdoug's latest version of famitone to Millfork within the coming days, so I'll report back on how well that went later.
Re: Millfork - New 6502 middle-language
by on (#242902)
Garydos wrote:
The biggest caveat with this language that I've found so far is its lack of interoperability with other languages. It has its own built-in assembly syntax that's sort of NESASM-like, but for the most part if you want to use say an audio library like famitone or pently, you'll have to port it yourself. I plan to attempt porting over nesdoug's latest version of famitone to Millfork within the coming days, so I'll report back on how well that went later.


For me personally, and I've advised this in the past sometimes, I think for stuff like sound engines it's sometimes worthwhile to just assemble it with whatever it was written in, then include the binary into the main project. In larger games music usually given its own bank, so you end up effectively with the same binary result in the end. Usually all you have to do is relocate variables to reserved location, and put JMP Init / JMP Play at the beginning and it's easy to use. You see commercial games all the time doing similar calls like JSR $8000, JSR $8003 (and those are just going to jump instructions). Anyways, that only helps if it's less work to do it that way. I've seen Millfork and it does look interesting, but I haven't looked at all into how it handles assembly.

I haven't tried to use Millfork yet, but just from looking at it, one thing I don't understand is why it looks exactly like C, but without semicolons. So does it ignore semicolons, or does having them break the parser? Because it looks like one could almost treat Millfork as a subset of C, if not for that difference. Seems like that would make it a little more approachable.

Anyways, welcome, and thanks for making a thread about this, it looks pretty interesting.
Re: Millfork - New 6502 middle-language
by on (#242903)
Memblers wrote:
For me personally, and I've advised this in the past sometimes, I think for stuff like sound engines it's sometimes worthwhile to just assemble it with whatever it was written in, then include the binary into the main project. In larger games music usually given its own bank, so you end up effectively with the same binary result in the end. Usually all you have to do is relocate variables to reserved location, and put JMP Init / JMP Play at the beginning and it's easy to use.


Oh wow thanks for informing me of this, that sounds a lot easier than porting over the whole library. You probably just saved me a bit of time lol. I'll try that out first, thanks!

Memblers wrote:
I haven't tried to use Millfork yet, but just from looking at it, one thing I don't understand is why it looks exactly like C, but without semicolons. So does it ignore semicolons, or does having them break the parser? Because it looks like one could almost treat Millfork as a subset of C, if not for that difference. Seems like that would make it a little more approachable.


From what I've seen/made so far, it's essentially C with a few Python influences (import statements, line endings instead of semicolons to separate expressions, etc...), plus a couple of its own keywords. There's been a couple instances where I just assumed the syntax was the same as in C and usually it worked as expected. The only place where this has fallen apart for me is when you start mixing 16-bit and 8-bit operations. Typecasting was a bit confusing when I tried it out so now I make sure everything gets placed into a 16-bit variable before I start doing any 16-bit math.

There's also some completely new stuff in it as well, like the return dispatch:
Code:
    return [gamestate] {
        STATETITLE    @ game_title_logic
        STATEPLAYING  @ game_playing_logic
        STATEGAMEOVER @ game_gameover_logic
    }

which is essentially just easy syntax for a jump table. In that example above, gamestate is a variable, and the return dispatch matches gamestate's value to one of those 3 options then jumps to the function indicated by the right hand side of the @ symbol. The documentation details all of the language's features pretty concisely, I believe the only other thing that would trip up a C programmer is how to use pointers, which have some different syntax.

Memblers wrote:
Anyways, welcome, and thanks for making a thread about this, it looks pretty interesting.


Thanks, a lot of times in the past my questions on NES development have led me to threads on these forums that answered my question so when I had something to contribute I knew where to come lol.
Re: Millfork - New 6502 middle-language
by on (#242909)
Well, I've already covered alternatives to assembly here.

The problem is usually not finding alternative, but actually ending up with a decent development environment using them, and debuging code made with them. I could add Millfork to the list.
Re: Millfork - New 6502 middle-language
by on (#242917)
Missing from the list is Wiz.