Forums

Sega Master System / Mark III / Game Gear
SG-1000 / SC-3000 / SF-7000 / OMV
Home - Forums - Games - Scans - Maps - Cheats - Credits
Music - Videos - Development - Hacks - Translations - Homebrew

View topic - Femto VM (Now with BASIC compiler)

Reply to topic
Author Message
  • Joined: 25 Feb 2006
  • Posts: 608
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Femto VM (Now with BASIC compiler)
Post 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)
FemtoVMAssembler-2008-06-17a.zip (18.8 KB)
Source code for the compiler that converts Femto VM source code into bytecode.
FemtoVM-2008-06-20.zip (199.85 KB)
Femto VM for Sega Master System, source code included.

  View user's profile Send private message Visit poster's website
  • Joined: 12 Apr 2005
  • Posts: 381
  • Location: London, United Kingdom
Reply with quote
Post 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?
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 608
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Post 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.
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 608
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Post 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)
FemtoVMAssembler-2008-07-25a.zip (23.06 KB)
Source code for the bytecode assembler
FemtoVMBasic-2008-07-29a.zip (417.84 KB)
Source code for the basic compiler

  View user's profile Send private message Visit poster's website
  • Joined: 10 Oct 1999
  • Posts: 211
  • Location: Lebanon, New Hampshire, USA
Reply with quote
Post 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.
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 608
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Post 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)
FemtoVMBasic-2008-07-31c.zip (419.91 KB)
Source code for the basic compiler
FemtoVMAssembler-2008-07-31a.zip (23.8 KB)
Source code for the bytecode assembler

  View user's profile Send private message Visit poster's website
  • Joined: 11 Aug 2007
  • Posts: 87
  • Location: Coimbra, Portugal
Reply with quote
Post 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!
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 608
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Post 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
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 608
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Post 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
FemtoVM-2008-08-20a.zip (1.99 MB)
Femto VM for Sega Master System

  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 608
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Post Posted: Thu Aug 21, 2008 2:50 am
As promised, here's the source code for the tools.
FemtoVMAssembler-2008-08-18a.zip (35.95 KB)
Source code for the bytecode assembler
FemtoVMBasic-2008-08-18a.zip (438.1 KB)
Source code for the basic compiler
FemtoVMBuilder-2008-08-20a.zip (1.55 MB)
Source code for the build tool
FemtoVMBuilderWrapper-2008-08-20a.zip (358.84 KB)
Source code for the build tool launcher

  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 608
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Post 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.
FemtoVM-2008-08-21b.zip (1.99 MB)
Femto VM for Sega Master System

  View user's profile Send private message Visit poster's website
Reply to topic



Back to the top of this page

Back to SMS Power!