NESFab

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

NESFab

Post by pubby »

Image

I've been working on a new programming language and compiler for making NES games in.

Website: https://pubby.games/nesfab.html

Features:
  • Easy to build ROMs. No linker configurations necessary.
  • Imports and converts many file formats automatically. You can make games without writing tools.
  • Built-in support for custom character sets, along with text compression.
  • Optimizes generated code and compresses data.
  • Banks are abstracted. Just specify your data and the compiler will smartly handle bank switches behind the scenes.
  • No forward declarations. Unlike C, definitions can exist in any order.
  • Variable allocation is smartly handled for you, including a type-safe alternative to unions.
  • Built-in support for fixed-point math.
  • Easy byte-level manipulation.
  • Compile-time evaluation, like C++'s constexpr.
  • Inline assembly (on a function level, not statement level).
  • Compiler is fast and multi-threaded.
Downsides:
  • Only targets discrete mappers, like BNROM. I doubt it'll ever support ASICs like MMC3, unless someone wants to design one with 32k banks.
  • It only makes NES games.
  • Toolchain is just the monolithic, non-incremental compiler. Limited debugging features so far.
  • Error messages and frontend optimizations are obviously worse than Clang with LLVM-MOS.
  • No recursion (except in inline assembly).
Misc:
  • Indentation is significant, like Python
Last edited by pubby on Mon Mar 06, 2023 10:21 pm, edited 1 time in total.
User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

Re: NESFab

Post by pubby »

I'll use this thread for tracking progress.

I've been adding loop optimizations to the compiler. It now does inlining and some rewrites and strength reductions.

For example, if you write code like this:

Code: Select all

    for x = 0; x < 30; x += 1
        array[x] = (U(1) << x)
It spits out this assembly:

Code: Select all

    LDY #1
    STY temp
    LDX #29
l2:
    LDA temp
    STA, array, x
    ASL
    STA, array-1, x
    ASL
    STA, array-2, x
    ASL
    STA, array-3, x
    ASL
    STA, array-4, x
    ASL
    STA, array-5, x
    ASL
    STA temp
    TXA
    AXS #6
    BPL l2
I haven't done much profiling yet, but from what I can tell it's producing faster code than other compilers. Below is the same loop with other tools.

VBCC:

Code: Select all

l19:
	lda	#0
	sta	r2
	cmp	#30
	bcs	l14
l13:
	lda	#1
	ldy	r2
	beq	l20
l21:
	asl
	dey
	bne	l21
l20:
	ldy	r2
	sta	0+_array,y ;am(r2)
	inc	r2
	lda	r2
	cmp	#30
	bcc	l13
l14:
KickC:

Code: Select all

  __b1:
    lda.z x
    cmp #$1e
    bcc __b2
    rts
  __b2:
    lda #1
    ldy.z x
    cpy #0
    beq !e+
  !:
    asl
    dey
    bne !-
  !e:
    ldy.z x
    sta array,y
    inc.z x
    jmp __b1
LLVM-MOS:

Code: Select all

	ldx	#0
.LBB1_1:                                ; =>This Inner Loop Header: Depth=1
	stx	mos8(__rc2)
	stx	mos8(__rc20)
	ldx	#0
	lda	#1
	jsr	__ashlhi3
	ldx	mos8(__rc20)
	sta	array,x
	inx
	cpx	#30
	bne	.LBB1_1
User avatar
NovaSquirrel
Posts: 483
Joined: Fri Feb 27, 2009 2:35 pm
Location: Fort Wayne, Indiana
Contact:

Re: NESFab

Post by NovaSquirrel »

I'm really interested in seeing where this goes! I especially like that there's built-in fixed point and bank support, and that it's a specialized 6502 language without just being a thin wrapper around assembly. I feel like that approach is actually the best way to make something friendly and easy to use.
strat
Posts: 409
Joined: Mon Apr 07, 2008 6:08 pm
Location: Missouri

Re: NESFab

Post by strat »

Very nice, especially the support for fixed-point math. I would put in bcd math, also. My attempt at a programming language had built-in bcd routines automatically called on vars declared with "bcd".
User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

Re: NESFab

Post by pubby »

NovaSquirrel wrote: Wed Oct 26, 2022 11:03 am I'm really interested in seeing where this goes! I especially like that there's built-in fixed point and bank support, and that it's a specialized 6502 language without just being a thin wrapper around assembly. I feel like that approach is actually the best way to make something friendly and easy to use.
Thanks. I reckon fixed-point and automatic banking are two of the nicest features. I think the language will be easier to use than C, but it's certainly targeting power users over beginners.
strat wrote: Sat Oct 29, 2022 9:39 pm Very nice, especially the support for fixed-point math. I would put in bcd math, also. My attempt at a programming language had built-in bcd routines automatically called on vars declared with "bcd".
That's a good idea. I'll probably add a few basic BCD operations someday, but nothing much.
User avatar
nesrocks
Posts: 563
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Re: NESFab

Post by nesrocks »

Hi Pubby! This seems really interesting! How easy do you reckon it would be to pick this up and get something running compared to batari BASIC with a Gui? I don't mean a super complex game, maybe something like a very casual game. I like the idea of automatic bank management. Is there an entities manager?
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!
https://www.patreon.com/bitinkstudios <- Support me on Patreon!
User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

Re: NESFab

Post by pubby »

Sometimes "easy" means "familiar". If you know other programming languages like Python, Java, or C, then NESFab is going to be attractive for being similar.

The language has a few features like image loading built-in, but it's still a language; not framework. There's no GUI to it, and there's no concept of entities. I've written a few small libraries to help people with common tasks, but other than that you're in full control and have to implement entities yourself. This can lead to more work, but it grants a lot of flexibility.

I think the biggest benefit of this language occurs when transitioning from small games to large. Stuff like the automatic bank switching is mostly irrelevant to small games, but saves a lot of time when making large ones. I want to make the language easy and approachable to newbies, but the real goal is making a tool I can use myself.

I'm nearing a point of releasing NESFab (in beta form). If you, or anyone else, would like to test it, let me know and I'll be happy to help.
User avatar
gauauu
Posts: 779
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

Re: NESFab

Post by gauauu »

I just wanted to chime in and say that I'm very excited about this, and seeing where you go with it.
User avatar
oRBIT2002
Posts: 687
Joined: Sun Mar 19, 2006 3:06 am
Location: Gothenburg/Sweden

Re: NESFab

Post by oRBIT2002 »

Cool project. How did you learn to compile your own language to assembly? It has to be pretty complicated, building a parser etc etc.?
User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

Re: NESFab

Post by pubby »

oRBIT2002 wrote: Thu Jan 12, 2023 1:43 pm Cool project. How did you learn to compile your own language to assembly? It has to be pretty complicated, building a parser etc etc.?
Mostly learned from Wikipedia and academic papers. A compiler can be complex or it can be simple depending on how far you want to take it. I made a
FORTH-like language a few years ago and that was only 100 line of code. This new project is closer to 100,000 though.
User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

Re: NESFab

Post by pubby »

It's still rough around the edges, but I've decided to release the software already. You can get it at: https://pubby.games/nesfab.html

There's still a decent amount of work to be done, but since I was able to compile a small game already, it seemed like a good point to share.

Image
User avatar
nesrocks
Posts: 563
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Re: NESFab

Post by nesrocks »

This is really great. I hope to get something done next week! Can't wait to get working with this. Thank you pubby!
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!
https://www.patreon.com/bitinkstudios <- Support me on Patreon!
User avatar
Individualised
Posts: 310
Joined: Mon Sep 05, 2022 6:46 am

Re: NESFab

Post by Individualised »

Very interested in this project. Sad that advanced mappers won't be supported, but I understand the reasoning.
User avatar
NovaSquirrel
Posts: 483
Joined: Fri Feb 27, 2009 2:35 pm
Location: Fort Wayne, Indiana
Contact:

Re: NESFab

Post by NovaSquirrel »

Individualised wrote: Sun Mar 19, 2023 5:18 pm Very interested in this project. Sad that advanced mappers won't be supported, but I understand the reasoning.
NESFab 0.3 actually added an MMC3 variant (mapper 189) which should take care of most of the things you'd want to do with an ASIC mapper.
User avatar
Individualised
Posts: 310
Joined: Mon Sep 05, 2022 6:46 am

Re: NESFab

Post by Individualised »

NovaSquirrel wrote: Sun Mar 19, 2023 6:09 pm
Individualised wrote: Sun Mar 19, 2023 5:18 pm Very interested in this project. Sad that advanced mappers won't be supported, but I understand the reasoning.
NESFab 0.3 actually added an MMC3 variant (mapper 189) which should take care of most of the things you'd want to do with an ASIC mapper.
That's great! Definitely makes me consider using this more over assembly, if I ever advance to that level.
Post Reply