Brainfuck compiler and VM

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Brainfuck compiler and VM
by on (#234198)
Image

Why? Why not. Use this awesome language to write your subroutines, when you feel that assembler is too difficult to master.

* uses 8 bit memory cells
* input is supported but limited

Source is here: https://github.com/veremenko-y/nes-brainfuck

Code:
; Hello World
BF_run:
BF_compile "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."
jsr BF_run


Result:

Image

Code:
; Factorial
BF_run:
BF_compile "+++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++>++++++>>+<<[>++++++++++++++++++++++++++++++++++++++++++++++++.------------------------------------------------<<<<.-.>.<.+>>>>>>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>[<+>-]>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]<[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]<<<++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<.>>+>[>>+<<-]>>[<<<[>+>+<<-]>>[<<+>>-]>-]<<<<-]"
jsr BF_run


Result:

Image

Edit: Updated version with fixed screen glitch
Re: Brainfuck compiler using ca65 macroses
by on (#234205)
Now with bottles of beer =) "Compiled version" is 6647 bytes...
Re: Brainfuck compiler using ca65 macroses
by on (#234462)
Too bad, ca65 macroses are too slow to be useful (I should say abused). As soon as I started doing small optimizations, execution time decreased dramatically, so I rewrote compiler to C#.

I also changed compilation from jsr opcode to opcodes with jumptable to save space. It runs slower, but now it can actually run "Adv" - text adventure by Jon Ripley (http://jonripley.com/brainfuck/games/adv.html).

Controls:
left, right, up, down = west, east, north, south
select = down
start = up
a = look
b = inventory