Difference between revisions of "TIC-80"

From SizeCoding
Jump to: navigation, search
m (Childishbeat moved page Tic80 to TIC-80: TIC-80's the correct spelling, and the page is inconvenient or harder to find.)
(Optimizing with a packer: Moved the part about hiding the mouse cursor here)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
=== Setting up ===
 
=== Setting up ===
As the TIC-80 fantasy computer is an all-in-one creation and execution platform, setting up TIC-80 is very easy:
+
The TIC-80 fantasy computer is an all-in-one creation and execution platform.
  
Just go to the https://github.com/nesbox/TIC-80/releases page  
+
Setup is easy, simply download an executable from the [https://github.com/nesbox/TIC-80/releases releases] page for your platform of choice (Windows, OSX, Linux and even Raspberry Pi).
 
 
and download the package for your platform of choice (Windows, OSX, Linux and even Raspberry Pi).
 
  
 
Or if you are just curious you can just start doodling online at http://tic80.com/
 
Or if you are just curious you can just start doodling online at http://tic80.com/
  
 
=== Getting started ===
 
=== Getting started ===
Most TIC-80 programs are coded using the Lua Scripting language. However it is possible to select different scripting language like javascript at the cost of a couple of bytes/characters like so (respectively for JavaScript, MoonScript, Wren, Fennel, Squirrel):
+
Most TIC-80 programs are coded using the [https://www.lua.org/ Lua] scripting language. However it is possible to use different scripting languages at the cost of a couple of bytes/characters as shown below (respectively for JavaScript, MoonScript, Wren, Fennel, Squirrel)
  
 
<syntaxhighlight lang="js">//script: js</syntaxhighlight>
 
<syntaxhighlight lang="js">//script: js</syntaxhighlight>
Line 17: Line 15:
 
<syntaxhighlight lang="js">// script: squirrel</syntaxhighlight>
 
<syntaxhighlight lang="js">// script: squirrel</syntaxhighlight>
  
The main function used for updating the screen (and called 60 times a second) is the TIC() function, so this function is also a requirement for doing anything with graphics. Additionally you can also setup a sub=function SCN() that is called once per scanline at the costs of more bytes/characters.  
+
The main function used for updating the screen (called 60 times a second) is the TIC() function, and is a requirement for animated graphics. Additionally you can also create a SCN() function that is called once per scanline at the cost of more bytes/characters.  
  
Most animated effects will also need to use some kind of a timer, so you are likely to also use the built-in time() function or keep track of your time (t) yourself as well.So a minimal setup would look something like this:
+
Most animated effects will also need to use some kind of a timer, so you are likely to also use the built-in time() function or keep track of your time yourself.  A minimal setup would look something like this:
  
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
Line 27: Line 25:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
[https://github.com/nesbox/TIC-80/wiki/tic See here] to know how tic() is called in the different language supported by TIC-80.
+
[https://github.com/nesbox/TIC-80/wiki/tic See here] to learn how TIC() is called in the different languages supported by TIC-80.
  
A full overview of the TIC80 memory map and most common used function is available in this handy TIC80 cheatsheet, as well as the TIC80 wiki page.
+
A full overview of the TIC-80 memory map and most commonly used functions is available in this [https://zenithsal.com/assets/documents/tic-80_cheatsheet.pdf TIC-80 cheatsheet], as well as the [https://github.com/nesbox/TIC-80/wiki TIC-80 wiki page].
 
 
https://zenithsal.com/assets/documents/tic-80_cheatsheet.pdf
 
  
 
=== Video display ===
 
=== Video display ===
The TIC-80 has a 240x136 pixel display with 16colors which can be accessed via a wide range of graphics functions or by writing directly to VRAM at memory address 0x0000 using the <code>poke4</code> instruction, that just change 4 bits. The address have to be multiplied by 2 when using poke4. access to 0x1000 for example is 0x02000 (high nibble) and 0x02001 (low nibble).
+
The TIC-80 has a 240x136 pixel display with 16 colors which can be accessed via a wide range of graphics functions or by writing directly to VRAM at memory address 0x0000 using the <code>poke4</code> instruction, setting four bits that represent a pixel's color. Note that the addresses have to be doubled when using poke4 because the memory is 4-bit addressable. Memory address 0x1000 would become 0x02000 (high nibble) and 0x02001 (low nibble).
  
 
==== Draw functions ====
 
==== Draw functions ====
Line 65: Line 61:
  
 
==== Color Palette ====
 
==== Color Palette ====
The best way to start is to use the default sweetie16 palette (https://lospec.com/palette-list/sweetie-16) as this palette
+
The best way to start is to use the default [https://lospec.com/palette-list/sweetie-16 sweetie16 palette] as this palette offers a nice selection of 16 colors arranged in such a way that they are easily accessible.
offers a nice selection of 16 colors arranged in such a way that they are easily accessable. From the verion 0.9b version and beyond you can initialise the new default sweetie16 palette at startup by adding a 0x11 Chunk to your TIC-80 cartridge.  
+
From version 0.9b onward you can initialize the new default sweetie16 palette at startup by adding a 0x11 Chunk to your TIC-80 cartridge.  
  
Normally a chunk would contain 4 bytes of header + data, but as this chunk has no data, it is possible to omit the extra 3 bytes of chunk-header if you place it at the end of your TIC cartridge. The new TIC-Packer linked below has the option to do this for you.
+
Normally a chunk would contain 4 bytes of header + data, but as this chunk has no data, it is possible to omit the extra 3 bytes of chunk-header if you place it at the end of your TIC cartridge. Some packers linked below have the option to do this.
  
 
==== Setting your own color palette ====
 
==== Setting your own color palette ====
 
Alternatively you can setup your own palette by writing to the palette area located at 0x3fc0 like so:
 
Alternatively you can setup your own palette by writing to the palette area located at 0x3fc0 like so:
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
for i=0,47 do poke (0x3fc0+i,i*5)end
+
for i=0,47 do poke(0x3fc0+i,i*5)end
 
</syntaxhighlight>
 
</syntaxhighlight>
 
This produces a nice grayscale palette of 16 shades to work with.
 
This produces a nice grayscale palette of 16 shades to work with.
  
 
==== Color index shuffling ====
 
==== Color index shuffling ====
If you don't want to use the sweetie16 palette you can revert back to the pre 0.8 db16 palette by simply not including a 0x11 chunk in you cartridge. Although the arrangement of color-indices is not as ideal as sweetie16, you can shuffle your color indices a bit to get 'somewhat workable' colors.
+
If you don't want to use the sweetie16 palette you can revert back to the pre-0.8 db16 palette by simply not including a 0x11 chunk in you cartridge. Although the arrangement of color-indices is not as ideal as sweetie16, you can shuffle your color indices a bit to get 'somewhat workable' colors.
  
 
A couple of examples for this are
 
A couple of examples for this are
Line 88: Line 84:
  
 
=== Sound ===
 
=== Sound ===
The TIC-80 has soundregisters and 32 byte waveforms to access which are located at address 0FF9C in memory.
+
The TIC-80 has sound registers and 32 byte waveforms to access which are located at address 0x0FF9C in memory.
  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 102: Line 98:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
A more the "proper" way involves something like : define the waveform yourself (f.e. sawtooth), repeatedly (because for some reason one time is not enough), then write low part of the frequency to one byte, and the high nibble combined with the volume to another)
+
A more the "proper" way involves something like : define the waveform yourself (f.e. sawtooth) repeatedly, then write low part of the frequency to one byte, and the high nibble combined with the volume to another.
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
TIC=function()
 
TIC=function()
for i=0,31 do poke4(2*65438+i,i/2) end -- setup waveforem
+
for i=0,31 do poke4(2*65438+i,i/2) end -- setup waveform
 
t=time()/10  
 
t=time()/10  
 
-- write frequencies
 
-- write frequencies
Line 114: Line 110:
 
But as you can see this costs considerably more bytes to setup.
 
But as you can see this costs considerably more bytes to setup.
  
=== Final Optimisations ===
+
=== Final Optimizations ===
 +
==== Optimizing with a packer ====
 +
Packers will shorten variable names, remove excess whitespace, and reorder operators and statements to make the code short and highly-compressible.
 +
It is therefore more important to write code that a packer can compress effectively as opposed to simply aiming for the smallest uncompressed code.
 +
 
 +
Repeating patterns within source code will typically be compressed very well.
 +
Thus, it is best to use the same literals and reserved keywords as often as possible, while avoiding introducing new ones.
 +
Additionally, if a variable or short function is used in multiple places, replacing each occurrence of the variable or function call with the calculation used to obtain their value will typically reduce cartridge size by a few bytes.
 +
 
 +
Tic-Tool provides an option for a live compression heatmap (in gzthermal style) to show, how well different parts of the code could be compressed.
 +
 
 +
With the appearance of a mouse cursor in version 1.0 one common question was, how it could be disabled efficiently. The clean way would be a poke(16379,0), but taking into account code compression, a shorter version might be possible by reusing similar code. Here is an example from "Danger Inbound" of reusing palette setup code:
 +
 
 +
<syntaxhighlight lang="lua">
 +
-- set grey palette
 +
for i=0,47 do
 +
    poke(16320+i,i*5)
 +
end
 +
-- switch off mouse cursor
 +
-- (looks big, but due to compression and reusing palette code, this is just 5-6 bytes)
 +
i=59 poke(16320+i,i*5)
 +
</syntaxhighlight>
 +
 
 +
==== Optimizing without a packer====
 
When you are happy with your intro and want to get it ready for release, it becomes time to look at squeezing those last bytes.
 
When you are happy with your intro and want to get it ready for release, it becomes time to look at squeezing those last bytes.
 
As a goal-post, you should always aim to have your uncompressed effect around the target size, and work from there.
 
As a goal-post, you should always aim to have your uncompressed effect around the target size, and work from there.
  
Final optimisation can be done by stringing as much code together on single lines and removing any extra spaces and whitelines.
+
Final optimization can be done by stringing as much code together on single lines and removing any extra spaces and newlines.
 
A rule of thumb for this is that of the first or last character of a variable or function isn't a valid hex number (i.e. A-F) you can omit whitespace (so that: x=0 y=0 z=0 can become x=0y=0z=0)
 
A rule of thumb for this is that of the first or last character of a variable or function isn't a valid hex number (i.e. A-F) you can omit whitespace (so that: x=0 y=0 z=0 can become x=0y=0z=0)
 +
 +
Check the [[Byte Battle]] article for more optimization ideas.
  
 
=== Release ===
 
=== Release ===
 
For releasing an intro at a demoscene event, a raw TIC cartridge file without any additional graphics/sound/metadata is needed.
 
For releasing an intro at a demoscene event, a raw TIC cartridge file without any additional graphics/sound/metadata is needed.
  
Creating a http://www.sizecoding.org/index.php?title=Fantasy_Consoles&action=edit&section=13 TIC cartridge file adds a 4 byte header + 1 extra byte for a 0x11 sweetie16 chunk.
+
Creating a TIC cartridge file adds a 4 byte header + 1 extra byte for a 0x11 sweetie16 chunk.  
 
+
There are various packers that help you convert your (LUA) Script to a empty TIC Cartridge with a single ZLIB compressed code block and optional 0x11 (sweetie16) palette chunk. See the additional links for links to these packers.
Luckily there are various packers that help you convert your (LUA) Script to a empty TIC Cartridge with a single ZLIB compressed code block and optional 0x11 (sweetie16) palette chunk. See the additional links for links to these packers.
 
 
   
 
   
 
==== Exporting Video as Animated GIF ====
 
==== Exporting Video as Animated GIF ====
The TIC80 environment has a neat feature that lets you export your intro directly as an animated GIF file to converted to video later, by Pressing the F9 key to start and stop recording. However, there is a default recording limit capped to a fixed number of frames or seconds. You can change this in the tic80 config to a bigger number to match your recording-size.  
+
The TIC-80 environment has a neat feature that lets you export your intro directly as an animated GIF file to converted to video later, by pressing the F9 key to start and stop recording. However, there is a default recording limit capped to a fixed number of frames or seconds. You can change this in the tic80 config to a bigger number to match your recording-size.  
  
If your intro is taking up too many resources and starts chugging a bit on your machine, it can be wise to make a version that steps through time lineary by adding a number to your t variable yourself instead of using the time() function.
+
If your intro is taking up too many resources and starts chugging a bit on your machine, it can be wise to make a version that steps through time linearly by adding a number to your t variable yourself instead of using the time() function.
  
 
==== Online version: Metadata and Thumbnail image ====
 
==== Online version: Metadata and Thumbnail image ====
When uploading the intro to the TIC80 website for a playable online version, you will need to build a new TIC file with some added some meta-data and Thumbnail image (You can take this screenshot using the F7 key during the demo playback) and use this as you online version. The screenshot can also be imported from a 240×136 PNG (other size will throw an error) using inside TIC-80 console <code>import screen file[.png]</code>.
+
When uploading the intro to the TIC-80 website for a playable online version, you will need to build a new TIC file with some added some meta-data and Thumbnail image (You can take this screenshot using the F7 key during the demo playback) and use this as you online version. The screenshot can also be imported from a 240×136 PNG (other size will throw an error) using inside TIC-80 console <code>import screen file[.png]</code>.
  
The Meta data is added at the top of your intro as follows
+
The Meta data is added at the top of your source code as follows
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
-- title: My intro
 
-- title: My intro
Line 142: Line 162:
 
-- desc: my first sizecoded TIC-80 intro
 
-- desc: my first sizecoded TIC-80 intro
 
-- script: lua (or moon/wren/js/fennel)
 
-- script: lua (or moon/wren/js/fennel)
 +
-- license: MIT
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Update: As of version 0.9b the TIC80.COM website now also allows you to upload a seperate TIC file with the metadata and keep the uploaded binary TIC file as code only.
+
As of version 0.9b the http://tic80.com website now also allows you to upload a separate TIC file with the metadata and keep the uploaded binary TIC file as code only.
  
 
=== Additional Resources ===
 
=== Additional Resources ===
Line 156: Line 177:
 
* Pactic, fork de TIC-80 Packer https://github.com/phlubby/pactic
 
* Pactic, fork de TIC-80 Packer https://github.com/phlubby/pactic
 
* TIC-Tool https://github.com/exoticorn/tic-tool
 
* TIC-Tool https://github.com/exoticorn/tic-tool
 +
* pakettic, https://github.com/vsariola/pakettic

Latest revision as of 12:35, 3 January 2024

Setting up

The TIC-80 fantasy computer is an all-in-one creation and execution platform.

Setup is easy, simply download an executable from the releases page for your platform of choice (Windows, OSX, Linux and even Raspberry Pi).

Or if you are just curious you can just start doodling online at http://tic80.com/

Getting started

Most TIC-80 programs are coded using the Lua scripting language. However it is possible to use different scripting languages at the cost of a couple of bytes/characters as shown below (respectively for JavaScript, MoonScript, Wren, Fennel, Squirrel)

//script: js
-- script: moon
// script: wren
;; script: fennel
// script: squirrel

The main function used for updating the screen (called 60 times a second) is the TIC() function, and is a requirement for animated graphics. Additionally you can also create a SCN() function that is called once per scanline at the cost of more bytes/characters.

Most animated effects will also need to use some kind of a timer, so you are likely to also use the built-in time() function or keep track of your time yourself. A minimal setup would look something like this:

function TIC()t=time()
-- your effect code
end

See here to learn how TIC() is called in the different languages supported by TIC-80.

A full overview of the TIC-80 memory map and most commonly used functions is available in this TIC-80 cheatsheet, as well as the TIC-80 wiki page.

Video display

The TIC-80 has a 240x136 pixel display with 16 colors which can be accessed via a wide range of graphics functions or by writing directly to VRAM at memory address 0x0000 using the poke4 instruction, setting four bits that represent a pixel's color. Note that the addresses have to be doubled when using poke4 because the memory is 4-bit addressable. Memory address 0x1000 would become 0x02000 (high nibble) and 0x02001 (low nibble).

Draw functions

There are a couple of built-in drawing functions you can use:

cls(color=0)
pix(x,y[color]) [-> color]
circ(x,y,r,color) -- filled circle
circb(x,y,r,color) -- border circle
rect(x,y,w,h,color) -- filled rect
rectb(x,y,w,h,color) -- border rect
line(x0,y0,x1,y1,color)
tri(x1,y1,x2,y2,x3,y3,color)
textri(x1,y1,x2,y2,x3,y3,u1,v1,u2,v2,u3,v3,use_map=false,colorkey=-1)
print(text,x=0,y=0,color=15,fixed=false,scale=1,smallfont=false) -> width

Getting something on screen

Here is a bit of code to get you started:

function TIC() 
t=time()/99
for y=0,136 do for x=0,240 do
pix(x,y,(x>>3~y>>3)+t)
end;end;end

Which will display an animated XOR pattern.

Color Palette

The best way to start is to use the default sweetie16 palette as this palette offers a nice selection of 16 colors arranged in such a way that they are easily accessible. From version 0.9b onward you can initialize the new default sweetie16 palette at startup by adding a 0x11 Chunk to your TIC-80 cartridge.

Normally a chunk would contain 4 bytes of header + data, but as this chunk has no data, it is possible to omit the extra 3 bytes of chunk-header if you place it at the end of your TIC cartridge. Some packers linked below have the option to do this.

Setting your own color palette

Alternatively you can setup your own palette by writing to the palette area located at 0x3fc0 like so:

for i=0,47 do poke(0x3fc0+i,i*5)end

This produces a nice grayscale palette of 16 shades to work with.

Color index shuffling

If you don't want to use the sweetie16 palette you can revert back to the pre-0.8 db16 palette by simply not including a 0x11 chunk in you cartridge. Although the arrangement of color-indices is not as ideal as sweetie16, you can shuffle your color indices a bit to get 'somewhat workable' colors.

A couple of examples for this are

  • (color)&10 - Some grey/blue shade
  • ((color)&6)-3 - A Nice shade of Dark-cyan-white color
  • (color)^2 - A shade of brown/yellowish colors

But feel free to experiment yourself as well and let us know on discord if you find something cool.

Sound

The TIC-80 has sound registers and 32 byte waveforms to access which are located at address 0x0FF9C in memory.

0FF9C SOUND REGS 72 18 byte x 4 ch
0FFE4 WAVEFORMS 256 16 wave/ 32x4b each
100E4 SFX 4224 64 sounds

Make some noise

The easiest way to get 'some' sound going is to bitbang the sound-registers and hope for the best, for example:

TIC=function()for i=0,71 do poke(65436+i,(time()/7.2)%64)end end

A more the "proper" way involves something like : define the waveform yourself (f.e. sawtooth) repeatedly, then write low part of the frequency to one byte, and the high nibble combined with the volume to another.

TIC=function()
for i=0,31 do poke4(2*65438+i,i/2) end -- setup waveform
t=time()/10 
-- write frequencies
poke(65436+0,t%256) 
poke(65437+0,(t/65536)%16+240)
end

But as you can see this costs considerably more bytes to setup.

Final Optimizations

Optimizing with a packer

Packers will shorten variable names, remove excess whitespace, and reorder operators and statements to make the code short and highly-compressible. It is therefore more important to write code that a packer can compress effectively as opposed to simply aiming for the smallest uncompressed code.

Repeating patterns within source code will typically be compressed very well. Thus, it is best to use the same literals and reserved keywords as often as possible, while avoiding introducing new ones. Additionally, if a variable or short function is used in multiple places, replacing each occurrence of the variable or function call with the calculation used to obtain their value will typically reduce cartridge size by a few bytes.

Tic-Tool provides an option for a live compression heatmap (in gzthermal style) to show, how well different parts of the code could be compressed.

With the appearance of a mouse cursor in version 1.0 one common question was, how it could be disabled efficiently. The clean way would be a poke(16379,0), but taking into account code compression, a shorter version might be possible by reusing similar code. Here is an example from "Danger Inbound" of reusing palette setup code:

-- set grey palette
for i=0,47 do
    poke(16320+i,i*5)
end
-- switch off mouse cursor
-- (looks big, but due to compression and reusing palette code, this is just 5-6 bytes)
i=59 poke(16320+i,i*5)

Optimizing without a packer

When you are happy with your intro and want to get it ready for release, it becomes time to look at squeezing those last bytes. As a goal-post, you should always aim to have your uncompressed effect around the target size, and work from there.

Final optimization can be done by stringing as much code together on single lines and removing any extra spaces and newlines. A rule of thumb for this is that of the first or last character of a variable or function isn't a valid hex number (i.e. A-F) you can omit whitespace (so that: x=0 y=0 z=0 can become x=0y=0z=0)

Check the Byte Battle article for more optimization ideas.

Release

For releasing an intro at a demoscene event, a raw TIC cartridge file without any additional graphics/sound/metadata is needed.

Creating a TIC cartridge file adds a 4 byte header + 1 extra byte for a 0x11 sweetie16 chunk. There are various packers that help you convert your (LUA) Script to a empty TIC Cartridge with a single ZLIB compressed code block and optional 0x11 (sweetie16) palette chunk. See the additional links for links to these packers.

Exporting Video as Animated GIF

The TIC-80 environment has a neat feature that lets you export your intro directly as an animated GIF file to converted to video later, by pressing the F9 key to start and stop recording. However, there is a default recording limit capped to a fixed number of frames or seconds. You can change this in the tic80 config to a bigger number to match your recording-size.

If your intro is taking up too many resources and starts chugging a bit on your machine, it can be wise to make a version that steps through time linearly by adding a number to your t variable yourself instead of using the time() function.

Online version: Metadata and Thumbnail image

When uploading the intro to the TIC-80 website for a playable online version, you will need to build a new TIC file with some added some meta-data and Thumbnail image (You can take this screenshot using the F7 key during the demo playback) and use this as you online version. The screenshot can also be imported from a 240×136 PNG (other size will throw an error) using inside TIC-80 console import screen file[.png].

The Meta data is added at the top of your source code as follows

-- title: My intro
-- author: scener
-- desc: my first sizecoded TIC-80 intro
-- script: lua (or moon/wren/js/fennel)
-- license: MIT

As of version 0.9b the http://tic80.com website now also allows you to upload a separate TIC file with the metadata and keep the uploaded binary TIC file as code only.

Additional Resources

Sizecoding on the TIC-80 is still in its infancy, but luckily there is already plenty of information to get you started!