Saturn Lua Player
	for SEGA Saturn
		created by The Rockin'-B, www.rockin-b.de

Release 1 on 2008/07/01


"Saturn game development made easy 2.0"


About the Saturn Lua Player:
----------------------------

The Saturn Lua Player runs games written in the Lua programming language.
These Lua games have access to hundreds of API functions of popular libraries like SGL,
PCM, GFS and STM as well as other commonly used functions.

The SLP has been successfully used to run several high polygon count 3d demos
(DEMO_C, TORUS3, ...), as well as the complex 2d graphic adventure game Police Officer Smith.

It comes packed with everything needed to run or to develop Lua games for SEGA Saturn:
SLP runtime, SLP Lua demos, docs, emus, tools


So fire up your text editor and start coding, right now!


About Lua:
----------

Lua is a powerful, fast, light-weight, embeddable scripting language.
Lua combines simple procedural syntax with powerful data description constructs
based on associative arrays and extensible semantics. Lua is dynamically typed,
runs by interpreting bytecode for a register-based virtual machine,
and has automatic memory management with incremental garbage collection,
making it ideal for configuration, scripting, and rapid prototyping. 


Starting development:
---------------------

Go to subdirectory SaturnLuaPlayer/samples/ and make a copy of a sample directory of your choice, name it as you like.
Open SaturnLuaPlayer/samples/myprojectname/cd/0_orig.lua in a text editor.

Modify that Lua code or start from scratch with something like that:
	slInitSystem(TV_320x224, 0, 1)
	slPrint("Hello World!")
	while true do slSynch() end

Now run batch file SaturnLuaPlayer/samples/myprojectname/makeiso_for_debugging.bat
Finally launch ISO SaturnLuaPlayer/samples/myprojectname/sample.iso in your Saturn console or emu.
Yabause can launch the ISO directly, for SSF you need to mount the ISO in Daemon Tools.


You best start reading the Lua source code (always named 0_orig.lua) of some samples.
There you'll see a subset of the Saturn API functions that can be used and how they are used.
For information on the Lua programming language syntax, see the PDFs in SaturnLuaPlayer/docs/Lua.
For further information on the Saturn library API functions, see SaturnLuaPlayer/docs/SGL/ and SaturnLuaPlayer/docs/SBL/ .
Also, if you are in doubt what arguments an API call needs,
what it returns and what API functions are implemented, refer to the SLP runtime C source code in SaturnLuaPlayer/src/api/.

When you finished your Saturn Lua program, create the release ISO with batch file SaturnLuaPlayer/samples/myprojectname/makeiso.bat .
That one will strip down the size of the Lua code, which saves RAM, loading times and so on.



Programming tips:
-----------------

Opposed to writing in C, you don't need
	- no slLocate(), 
	- no toFIXED()
	- no ; at the end of each line
	- no compiler
	- variables are declared in place

Lua unfortunately got no ++ or -- operator. Bit manipulation is possible throught bitlib, like bit.band(a, b) or bit.lshift(var, cnt).

When the SLP starts up, it registers hundreds of C API calls, runs a small hardcoded startup Lua script and then it loads file 0.LUA from the CD
and executes that. A that point, slInitSystem() and GFS_Init() have already been called.


	Memory:
	-------

The Saturn Lua Player uses the BGET buffer memory allocator, which is capable to assign multiple different memory chunks for Lua.
However, to leave you as much freedom as possible (e.g. loading precompiled binary data to special locations),
initially only the free portions of high workrams are available for lua.
It is strongly recommended that you assign Lua more memory as soon as possible.
Typically, you use the low workram for temporary loading data files or for persistent storage of complex binary data for use for the Saturn SGL library.

	-- Ex1: To give Lua the complete low workram (1MB), write:
	bpool(CPU_WRAM_LO, CPU_WRAM_LO_LEN)	

	-- Ex2: Having loaded len bytes of persistent data to the low workram, assign the rest to Lua this way:
	bpool(CPU_WRAM_LO+len, CPU_WRAM_LO_LEN-len)

	-- Ex3: Check for a RAM cartridge and give Lua all that storage, no matter if 1MB or 4MB type:
	local CartID, CartRam0, CartRam0Size, CartRam1, CartRam1Size = RB_CartRAM_init()
	if CartRam0 ~= nil then
		bpool(CartRam0, CartRam0Size)
	end
	if CartRam1 ~= nil then
		bpool(CartRam1, CartRam1Size)
	end


	If you encounter strange errors due to memory allocation failure, consider to start Luas garbage collector manually:

	collectgarbage(collect)


	Data:
	-----

There are two different types of data:
	- A) raw graphic data and sound data, of array type, to be transfered to VRAM or SoundRAM as is.
	- B) complex C structures to be read and written by the SGL library or others.

There are two different ways to use data in Lua:
	- 1) write as Lua code
	- 2) binary data generated by tools like BGCON or precompiled with GCC


A lot of tools export Saturn graphics and sound data as C code (3DEditor of SS-SDK, BGCON, 3d SGL export plugin for Blender and a lot more).
You rarely need to access that data in Lua, you typically just transfer it to some API call. The best method is to compile and link it to a
combined binary data file with GCC. The linker writes a mapfile.txt where for each data component, the start offset in the data file can be found.

Just open anysample/data/data_coff.dev (SaturnOrbit instalation required), add your data files to the project, remove unused data from the project and press recompile all.
Automatically, the file data.bin is created and put into the folder anysample/cd/ .
Then open file anysample/data/mapfile.txt to see at which location in the file a specific data has been placed.

For data of type A), you can load it to any location you like, the data is not position dependent. Use GFS_Load_name_buffer("DATA.BIN", 0) to load file DATA.BIN from CD
into a buffer that Lua creates.

For data of Type B), you need to put the file "DATA.BIN" at the same RAM location that's been specified in the linkscript.txt.
In all samples, that's the start of low workram. Use GFS_Load_name("DATA.BIN", 0, CPU_WRAM_LO, CPU_WRAM_LO_LEN) to put it at the desired location.
Note: be sure that you haven't already assigned that memory to Lua with bpool(), before!!!!

If it happens that you need to access that data in Lua, either try fetching the data from memory or make that data Lua code.
Both options have been implemented for reading the MOTION data of DEMO_C (walking Akira).


	Limits:
	-------

	Some interesting constants (goes per object and per frame)

	MAX_POLYGON  = 1600
	MAX_VERTICES = 3280

	memory available to Lua at startup = 315 kB (in Release 1)
	-> assign 1MB low RAM and 1-4 MB cart RAM manually!!!!


--------------------------------------------------------------------------------------------

Hope to see you at the next Saturn Coding Contest, you're welcome!

Rockin'-B

www.rockin-b.de
www.policeofficersmith.de
www.saturncodingcontest.de	<- soon


