Author |
Message |
- Joined: 25 Feb 2006
- Posts: 864
- Location: Belo Horizonte, MG, Brazil
|
Femto VM (Now with BASIC compiler)
Posted: Fri Jun 20, 2008 11:53 pm Last edited by haroldoop on Wed Aug 20, 2008 3:39 am; edited 4 times in total
|
Here's a small project I'm doing for fun:
Essentially it has a compiler that translates a Forth-like language into a compact bytecode, wich can then be run using the interpreter that runs on the Master System.
The attached files include both the interpreter, with a small demo program, and the compiler.
The interpreter is not yet complete; it already reads the joysticks, moves sprites around and draws to the background map, but there's still some things to implement, like tile loading and sound.
Also, as you may notice, its language is a bit strange, and difficult to read; that's because I've designed it to act as an intermediate language target, meaning that I intend to write a compiler that will take code written in some more common language (BASIC, for example), translate it to Femto VM's language, that can then be converted into bytecode that can be read by the interpreter.
Comments are welcome.
(Updated in Wed Aug 20, 2008 12:37 am; see last post)
|
|
|
- Joined: 12 Apr 2005
- Posts: 391
- Location: London, United Kingdom
|
Posted: Sat Jun 21, 2008 11:33 pm
|
An interesting project! I suspect Forth's (or a similar language) simple stack-based structure makes it suitable (and efficient) on the Z80?
|
|
|
- Joined: 25 Feb 2006
- Posts: 864
- Location: Belo Horizonte, MG, Brazil
|
Posted: Sun Jun 22, 2008 5:07 pm
|
Well, it all depends on the way the code is generated. Some Forth compilers generate optimized machine code, while others generate various kinds of threaded code, each having their own advantages and disadvantages, both in terms of space and speed.
Femto VM, by the way, generates a kind of token-threaded code: it reads a byte; if the top bit is zero, then it concatenates it with the next byte and pushes the result to the data stack; on the other hand, if the top bit is one, it uses the byte as a lookup index to a jump table. This is very space-efficient, but, while much faster than a full interpreter, it's obviously slower than compiled code, though I've never measured how much.
|
|
|
- Joined: 25 Feb 2006
- Posts: 864
- Location: Belo Horizonte, MG, Brazil
|
Posted: Wed Jul 30, 2008 12:25 am
|
After a long delay, here's a new version of FemtoVM, including a BASIC-to-FemtoVM compiler.
I must admit that, unfortunately, progress has been slow, but there should be an actually "presentable" version soon.
FemtoVM-2008-05-29b.zip (994.02 KB)
Femto VM for Sega Master System (VM source code, assembler, BASIC compiler and test program)
|
|
|
- Joined: 10 Oct 1999
- Posts: 211
- Location: Lebanon, New Hampshire, USA
|
Posted: Wed Jul 30, 2008 2:37 pm
|
Sr. Oop,
Can I just say thank you for all of your compiler-crafting efforts? The SMS world's casbah is thoroughly rocked, thanks to you.
|
|
|
- Joined: 25 Feb 2006
- Posts: 864
- Location: Belo Horizonte, MG, Brazil
|
Posted: Fri Aug 01, 2008 12:37 am
|
Here's a new update of BASIC compiler + FemtoVM for Sega Master System.
Among other things, it includes a bare-bones (not to mention ugly and buggy) implementation of Pong
LET Y0 = 0
LET Y1 = 160
LET X2 = 128
LET Y2 = 96
LET X3 = 1
LET Y3 = 1
SPRITE 0 SIZE 1 2 TILE 6 POS 8 Y0
SPRITE 1 SIZE 1 2 TILE 6 POS 240 Y1
SPRITE 2 SIZE 1 1 TILE 4 POS X2 Y2
WHILE TRUE
REM 1P movement
IF JOY1 UP LET Y0 = Y0 - 1
IF JOY1 DOWN LET Y0 = Y0 + 1
REM 2P movement
IF Y1 > Y2 + 8 LET Y1 = Y1 - 1
IF Y1 < Y2 - 32 LET Y1 = Y1 + 1
REM Ball movement
LET X2 = X2 + X3
IF X2 <0> 248 THEN
LET X2 = 248
LET X3 = -1
ENDIF
LET Y2 = Y2 + Y3
IF Y2 <0> 184 THEN
LET Y2 = 184
LET Y3 = -1
ENDIF
REM Bounce from 1P Paddle
IF (X2 > 8) & (X2 <16> Y0 - 8) & (Y2 <Y0> 232) & (X2 <240> Y1 - 8) & (Y2 < Y1 + 32) THEN
LET X2 = 232
LET X3 = -1
ENDIF
SPRITE 0 POS 8 Y0
SPRITE 1 POS 240 Y1
SPRITE 2 POS X2 Y2
FVM refresh
WEND
FemtoVM-2008-07-31b.zip (938.98 KB)
Femto VM for Sega Master System (VM source code, assembler, BASIC compiler and test program)
|
|
|
- Joined: 11 Aug 2007
- Posts: 87
- Location: Coimbra, Portugal
|
Posted: Thu Aug 07, 2008 2:26 am
|
Excellent!
If "your" basic supports defining functions with inline assembly (with parameter passing / returning), it is ready to fly!
|
|
|
- Joined: 25 Feb 2006
- Posts: 864
- Location: Belo Horizonte, MG, Brazil
|
Posted: Thu Aug 07, 2008 3:27 am
|
Well, currently it doesn't support inline Z80 ASM; one way to work around this would be to add the command as a VM instruction, by adding its address to the VM's jump table (In FemtoVM.asm, look for FemtoVMJumpTable); of course, you would have to obey the interpreter's calling convention (See the existing VM instructions for examples).
For the next version, I intend to:
- Implement tileset loading, so the user can load his/her own tileset (in other words, actual graphics support)
- Implement color palette loading
- Create a tool to automate the creation of the resource table (currently, it's hardcoded; in FemtoVM.asm, search for FemtoVMResTable)
Intended for the version after that one:
- Reorganization of the VM's source code
- Creation of a tool to automate program building
- Sound support
- Mod2Psg2 support
|
|
|
- Joined: 25 Feb 2006
- Posts: 864
- Location: Belo Horizonte, MG, Brazil
|
Posted: Wed Aug 20, 2008 3:37 am
|
Well, here's a new version of the compiler, with support for tileset loading, palette setting, and a tool to automate the building of the source code.
I'll post the source code for the tools tomorrow. I gotta sleep now. =P
|
|
|
- Joined: 25 Feb 2006
- Posts: 864
- Location: Belo Horizonte, MG, Brazil
|
Posted: Thu Aug 21, 2008 2:50 am
|
As promised, here's the source code for the tools.
|
|
|
- Joined: 25 Feb 2006
- Posts: 864
- Location: Belo Horizonte, MG, Brazil
|
Posted: Fri Aug 22, 2008 2:10 am
|
Here's a sligtly improved version, with improvements on palette setting and a slightly better demo program.
|
|
|