Namespaces

Namespace

Global (S65)

Macros
Vars
Shallan65 macro toolkit for the MEGA65.

- Uses NCM and FCM mode and the raster rewrite buffer to provide a powerful layer and sprite framework
- DMA job data macros
- SDCard loading

The S65 namespace is mostly used internally by the engine, however some methods and vars may be of use.

Macros
AddToMemoryReport
Measures the byte size of a block of assembly and records it in the memory report output by S65_MemoryReport

Called at the start and the end of the code block you wish to measure by passing the same name in both
Usage:
S65_AddToMemoryReport(name)
Parameters:
{string} name - The StringID you wish to appear in the report
MemoryReport
Called at the very end of your program code this macro will produce a report of the memory used in each of the framework calls
Usage:
S65_MemoryReport()
RestoreBasePage
registers:
A B
flags:
n z
Restores the base page from S65_LastBasePage
Usage:
S65_RestoreBasePage()
RestoreRegisters
Pulls the AXYZ registers off the stack
Usage:
S65_RestoreRegisters()
SaveRegisters
Pushes the AXYZ registers onto the stack
Usage:
S65_SaveRegisters()
SetBasePage
registers:
A B
flags:
n z
Saves the current base page in S65_LastBasePage and sets the base page to the S65 Base page area
Usage:
S65_SetBasePage()
Text16
Generates a string of 16 bit words based on the text input. The upper nybblwe of each word is $00 and the lower nybble is the normal 8 bit .screencode encoded value. terminates the string with $ffff
Usage:
S65_Text16(str)
Parameters:
{byte} str - The string to convert
Trace
Outputs a string to the kick assembler console at build time
Usage:
S65_Trace(str)
Parameters:
{string} str - The string to output in the console

Vars
COLOR_RAM
Defaults to $ff80000
Usage:
S65_COLOR_RAM
ColorRamPointer
{dword} S65 BasePage pointer into color ram. It is guaranteed to have the upper two bytes set at all times so can be used to access the color ram using 32bit indirect z addressing. DO NOT change bytes 2 and 3!

Note: Requires S65_SetBasePage or Layer_SetScreenPointersXY to correctly set up the base page before using indirect indexed adressing modes.
Usage:
S65_ColorRamPointer
LastImportPtr
Contains the address at which the last Asset_Import macro finished importing. Useful for chaining imports together
Usage:
S65_LastImportPtr
LastLayerIOPointer
{word} S65 BasePage pointer into to the IO data for the last layer that was fetched using Layer_Get
Usage:
S65_LastLayerIOPointer
LastSpriteIOPointer
{word} S65 BasePage pointer into to the IO data for the last sprite that was fetched using Sprite_Get
Usage:
S65_LastSpriteIOPointer
ReturnValue
{word} S65 BasePage value that is used as a return for many commands when the result is a word rather than a byte.
Usage:
S65_ReturnValue
SCREEN_LOGICAL_ROW_WIDTH
Number of bytes that make up an entire screen row
Usage:
S65_SCREEN_LOGICAL_ROW_WIDTH
SCREEN_RAM
Defaults to $0800
Usage:
S65_SCREEN_RAM
SCREEN_ROW_WIDTH
Number of characters that make up an entire screen row
Usage:
S65_SCREEN_ROW_WIDTH
SCREEN_TERMINATOR_OFFSET
Screen row offset for the row terminator bytes
Usage:
S65_SCREEN_TERMINATOR_OFFSET
ScreenPixelHeight
Contains the height of the visible screen in pixels
Usage:
S65_ScreenPixelHeight
ScreenPixelWidth
Contains the width of the visible screen in pixels
Usage:
S65_ScreenPixelWidth
ScreenRamPointer
{dword} S65 BasePage pointer into screen ram. It is guaranteed to have the upper two bytes set at all times so can be used to access the screen ram using 32bit indirect z addressing. DO NOT change bytes 2 and 3!

For this reason you MUST use a Screen RAM location that does not cross a 64kb boundary and aligned to page boundarys as the engine assumes this for speed

Note: Requires S65_SetBasePage or Layer_SetScreenPointersXY to correctly set up the base page before using indirect indexed adressing modes.
Usage:
S65_ScreenRamPointer
SpareBasePage
16 bytes of spare storage free for use when the S65 base page is active to assist with using commands that expect to be in S65 base page
Usage:
S65_SpareBasePage
VISIBLE_SCREEN_CHAR_HEIGHT
Height of the visible screen background layer in characters
Usage:
S65_VISIBLE_SCREEN_CHAR_HEIGHT
VISIBLE_SCREEN_CHAR_WIDTH
Width of the visible screen background layer in characters
Usage:
S65_VISIBLE_SCREEN_CHAR_WIDTH


Namespace

Anim

Functions
Data
API for controlling the screen ram and color ram. Using the raster rewrite buffer to create and manipulate layers

Functions
Define
Defines an animation sequence
Usage:
Anim_Define(name, spriteSet, start, end)
Parameters:
{byte} name - The name of this animation sequence
{byte} spriteSet - The Spriteset this animation is from
{byte} start - The start frame
{byte} end - The end frame
Get
Returns the animation sequence object
Usage:
Anim_Get(name)
Parameters:
{string} name - The name of the animation to fetch

Data
Sequence
The object returned by Anim_Get / truct Anim_Sequence { id, name, address, spriteSet, startFrame, endFrame, color} *
Usage:
Anim_Sequence
{byte} id - The numerical id of the animation
{string} name - The name of the aniamtion
{word} address - The start address for the animation data
{Asset_Spriteset} spriteSet - The struct for the spriteset
{byte} startFrame - The starting frame in the spriteset
{byte} endFrame - The end frame in the spriteset
{byte} color - The color for this animnation


Namespace

Asset

Macros
Functions
Data
Vars
API for the assets pipeline. Provides methods for importing assets and their metadata files

Macros
AddExternal
Adds a new external asset file to the SDCard containing the binary data providewd. It can then be loaded later using the name provided
Usage:
Asset_AddExternal(name, data)
Parameters:
{string} name - The name to reference this file
{binary} data - The binary data, either from previous Asset_Imports (eg charset.palette) or from a kick ass LoadBinary()
EndExternal
Ends the data/code block that was previously started with an Asset_StartExternal and returns the program counter to where it was prior to starting the block.
Usage:
Asset_EndExternal()
ImportCharset
Helper function to preload the _chars file generated by png65 into a given address and load the _ncm file
When loading using these imports you only need the first part of the path and file name. eg

Asset_ImportCharset("map","assets/bin/tileset1", $8000)

Will load both the "assets/bin/tileset1_chars.bin" and the "assets/bin/tileset1_ncm.s" files. NOTE: MUST be called before the Layer_InitScreen
Usage:
Asset_ImportCharset(name, path, address)
Parameters:
{string} name - The name to refer to this charset
{string} path - The path to the _chars and _ncm file generated by png65
{word} address - The address to laod the char data into
ImportCharsetPalette
Data generation macro that imports the full 256 color palette for the given charset name. The charset must be imported already otherwise this will fail.
Usage:
Asset_ImportCharsetPalette(name)
Parameters:
{string} name - The name of the charset to import the palette for
ImportSpriteset
Helper function to preload the sprites _chars file generated by png65 into a given address and load the _meta file
S65 provides space for up to 16 sprite set imports at any one time
When loading using these imports you only need the first part of the path and file name. eg

Asset_ImportSpriteset("player","assets/bin/sprites1", $8000)

Will load both the "assets/bin/sprites1_chars.bin" and the "assets/bin/sprites1_meta.s" files. NCM or FCM is determined from the metafile

NOTE: MUST be called before the Layer_InitScreen
Usage:
Asset_ImportSpriteset(name, charpath, address)
Parameters:
{string} name - The name to refer to this spriteset
{string} charpath - The path to the _chars and _meta file generated by png65
{word} address - The address to laod the sprites char data into
ImportSpritesetPalette
Data generation macro that imports the full 256 color palette for the given spriteset name. The spriteset must be imported already otherwise this will fail.
Usage:
Asset_ImportSpritesetPalette(name)
Parameters:
{string} name - The name of the spriteset to import the palette for
ImportTilemap
Helper function to preload the _tiles and _map file generated by ldtk65 into a given address
When loading using these imports you only need the first part of the path and file name. eg

Asset_ImportTilemap("map","assets/bin/map", $8000)

Will load both the "assets/bin/map_tiles.bin" and the "assets/bin/map_map.s" files. NOTE: MUST be called before the Layer_InitScreen
Usage:
Asset_ImportTilemap(name, path, charset, address)
Parameters:
{string} name - The name to refer to this tilemap
{string} path - The path to the _tiles and _map file generated by ldtk65
{Asset_Charset} charset - The charset to link to this tilemap
{word} address - The address to laod the data into
LoadFromExternal
registers: flags:
c n z
Loads an external asset previosuly defined using Asset_AddExternal or Asset_StartExternal from SDcard into the memory address provided
Usage:
Asset_LoadFromExternal(name, address)
Parameters:
{string} name - The Asset name defined in the Asset_AddExternal or Asset_StartExternal macro call
{dword} address - The address to load this asset into
Preload
Internal macro used to preload assets when imports have been done at or above $f000. Before initialisation any preloaded assets get loaded here
Usage:
Asset_Preload()
StartExternal
Signals the start of a blcok of data/code that will be saved to the SDCard for importing at runtime.
NOTE: If you are using this to save code make sure to use .pseudopc directive to set your intended load address as the program counter will be reset to $0000 for this
Usage:
Asset_StartExternal(name)
Parameters:
{string} name - The name to reference this file

Functions
GetCharset
This method returns the charset object containing all the vars needed to perform many operations.

id - The numerical id of the charset assigned by Asset_ImportCharset
name - The name of the charset
address - The start address for the charset char data
colorAddress - The start address for the color table if present
palette - A {List} of bytes containing the palette data
colors - A {List} of bytes containing the ncm color mapping data
Usage:
Asset_GetCharset(name)
Parameters:
{string} name - The name of charset to retrieve
GetSpriteset
Sprite sets are assigned numerical values from 0 to 15 in order as they are imported. This method returns the spriteset object containing all the vars needed to perform many operations, .id is used whenever referencing a spriteset in commands e.g. Sprite_SetSpriteMeta

id - The numerical id of the spriteset assigned by Asset_ImportSpriteset
name - The name of the spriteset
address - The start address for the spriteset char data
metaAddress - The start address for the spriteset metadata
meta - A {List} of bytes containing the metadata
palette - A {List} of bytes containing the palette data
Usage:
Asset_GetSpriteset(name)
Parameters:
{string} name - The name of sprite set to retrieve
GetTilemap
This method returns the tilemap object containing all the vars needed to perform many operations.

NOTE: The first four bytes of the tilemap data are thew width and height of the map in tiles (16 bit word values)

id - The numerical id of the tilemap assigned by Asset_ImportTilemap
name - The name of the tilemap
tilemapAddress - The start address for the tilemap data
tiledefAddress - The start address for the tile definition data
tilemap - A {List} of bytes containing the tilemap data
tiles - A {List} of bytes containing the tile definiton data
Usage:
Asset_GetTilemap(name)
Parameters:
{string} name - The name of tilemap to retrieve

Data
Charset
The object returned by GetCharset
Usage:
Asset_Charset
{byte} id - The numerical id of the charset assigned by Asset_ImportCharset
{string} name - The name of the charset
{word} address - The start address for the charset data
{word} colorAddress - The start address for the charset color data
{List} palette - A List of bytes containing the palette data
{List} colors - A list representing the color table fopr an ncm charset
{List} indices - A list representing the in memory indices for charset
Spriteset
The object returned by GetSpriteset
Usage:
Asset_Spriteset
{byte} id - The numerical id of the spriteset assigned by Asset_ImportSpriteset
{string} name - The name of the spriteset
{word} address - The start address for the spriteset char data
{word} metaAddress - The start address for the spriteset metadata
{List} meta - A List of bytes containing the metadata
{List} palette - A List of bytes containing the palette data
{List} indices - A List of character indexes for the sprites
Tilemap
The object returned by GetTilemap
Usage:
Asset_Tilemap
{byte} id - The numerical id of the tilemap assigned by Asset_ImportTilemap
{string} name - The name of the tilemap
{dword} tilemapAddress - The start address for the tile definitions data
{dword} tiledefAddress - The start address for the tile definitions data
{dword} colorAddress - The start address for the ncm color lookups
{word} width - The width of the tilemap in tiles
{word} height - The height of the tilemap in tiles
{byte} tilewidth - The width of a tile in chars
{byte} tileheight - The height of a tile in chars
{List} tilemap - A List of bytes containing the tilemap data
{List} tiles - A List of tiles containing a list for each tile definition data

Vars
Asset_SpriteListMetaTable
Pointer to the lookup table for the spritesets meta data tables
Usage:
Asset_Asset_SpriteListMetaTable


Namespace

DMA

Pseudocommands
Data
API for controlling the configuration and execution of DMagic jobs. Uses the F018 11 byte data format. throughout.

Pseudocommands
CopyJob
Data generator that copys a defined number of bytes from one location in memory to another using the DMagic chip @ 20mb/s
This is NOT executable code, only data and should be used as such

Usage:
DMA_CopyJob source : destination : length : chain : backwards
Parameters:
{dword} {ABS} source - The source data pointer
{dword} {ABS} destination - The destination data pointer
{word} {IMM} length - The number of bytes to copy
{bool} {IMM} chain - Job chains to another
{bool} {IMM} backwards - pointer progresses backwards
DisableTransparency
Data generator that disables the transparent byte masking. This is the default state.
This is NOT executable code, only data and should be used as such

Usage:
DMA_DisableTransparency
EnableTransparency
Data generator that enables transparent byte masking. This will ignore any source bytes that match the given byte and leave the destination byte untouched.
This is NOT executable code, only data and should be used as such

Usage:
DMA_EnableTransparency transparentByte
Parameters:
{byte} {IMM} transparentByte - The byte value to match from source for transparency.
Execute
registers:
A
flags:
n z
Executes the DMagic job at the given address.
Usage:
DMA_Execute address
Parameters:
{byte} {ABS} address - Pointer to a DMagic job to execute
FillJob
Data generator that fills a defined number of bytes from one location in memory to another using the DMagic chip @ 40mb/s
This is NOT executable code, only data and should be used as such

Usage:
DMA_FillJob sourceByte : destination : length : chain
Parameters:
{byte} {IMM} sourceByte - The source data byte value
{dword} {ABS} destination - The destination data pointer
{word} {IMM} length - The number of bytes to fill
{bool} {IMM} chain - Job chains to another
Header
Data generator that sets the DMagic header bytes defining the source and destination banks.
This is NOT executable code, only data and should be used as such

Note: The bank number of a memory adress is its 5th and 6th nybble. e.g. $ff80000 is bank number $ff
Usage:
DMA_Header sourceBank : destBank
Parameters:
optional{byte} {IMM} sourceBank - The source bank number Defaults to 0
optional{byte} {IMM} destBank - The destination bank number Defaults to 0
Step
Data generator that sets the source and/or destination stepping values. The DMA will use a fixed point step for each increment on the source and destination by default they are both set to the fixed point 8:8 value $0100 (or 1.0 in decimal)
This is NOT executable code, only data and should be used as such

Usage:
DMA_Step sourceStep : destStep
Parameters:
optional{word} {IMM} sourceStep - Source data stepping value in 8:8 fixed point format
optional{word} {IMM} destStep - Destination data stepping value in 8:8 fixed point format

Data
F018_DMA_11_byte_format
Offsets into the DMagic job for the F018 11 byte data format.
Usage:
DMA_F018_DMA_11_byte_format
{byte} $00 - End of options
{byte} $01 - Command
{word} $02 - Count
{word} $04 - Source
{byte} $06 - Source bank + flags
{word} $07 - Destination
{byte} $09 - Destination bank + flags
{word} $0a - Modulo


Namespace

Debug

Functions
Simple debug time namespace for displaying live memory contents via watchers

Functions
AddWatcher
Creates a watcher that is then displayed in the debug overlay. 4 bytes at a time are watcehd at each address. Best declared directly after your S65 library import. Requires NODEBUG to NOT be defined
Usage:
Debug_AddWatcher(label, address)
Parameters:
{string} label - The label to give the watcher
{dword} address - The address from which to watch data


Namespace

Layer

Pseudocommands
Macros
Functions
Data
Vars
Lists
Hashtables
API for controlling the screen ram and color ram. Using the raster rewrite buffer to create and manipulate layers

Pseudocommands
AddText
registers: flags:
c n z
Writes a string of bytes to the currently active layer at the provide co-ordinates. Optionally allows the use of color, setting Color RAM Byte 1 all bits (so includes bit4-blink, bit5-reverse, bit6-bold and bit7-underline), this will only work on non NCM layers with char indices less than $100
This is a conveinience function it is better to use Layer_WriteToScreen as it is more efficient

Note: As layer screen rows are interlaced in memory, its important to not let the string extend off the right edge of the layer as it can break the RRB on other layers. There is an upper limit string length of 128
Usage:
Layer_AddText xpos : ypos : textPtr : color
Parameters:
{word} {REG|IMM} xpos - Layer char X position
{word} {REG|IMM} ypos - Layer char Y position
{word} {ABS} textPtr - The address to fetch char data from
{byte} {REG|IMM|ABSXY} color - Color to write to color ram
AdvanceScreenPointers
registers:
A
flags:
c n z
Advances the S65 Basepage dword values for ColorRamPointer and ScreenRamPointer by the given byte offset.

Note: This method assumes you are already in the S65 base page, this is true after a
Layer_Get command, be careful not to use this command if base page is not set, otherwise it will likely write to unintended locations
Usage:
Layer_AdvanceScreenPointers Optional
Parameters:
optional{byte} {REG|IMM} Optional - byte offset (defaults to S65_SCREEN_LOGICAL_ROW_WIDTH)
ClearAllLayers
registers: flags:
n z
Fills the screen RAM area for ALL layers with a given 16bit value. Note this will overwrite any RRB GotoX markers also
NOTE: Absolute addressing will fetch 2 bytes from the target address (aka ABS16)
Usage:
Layer_ClearAllLayers clearChar
Parameters:
optional{word} {REG|IMM|ABS16|ABX16|ABY16} clearChar - The 16bit char value to clear with, defaults to $0000
ClearLayer
registers: flags:
c n z
Fills the screen RAM area for the currently selected layer with a given 16bit value. Optionally clearing the color too
Usage:
Layer_ClearLayer clearChar : clearColor
Parameters:
optional{word} {REG|IMM|ABS16|ABX16|ABY16} clearChar - The 16bit char value to clear with, defaults to $0000
optional{word} {REG|IMM|ABS|ABX|ABY} clearColor - The 8bit char value to clear with, defaults to $00
Get
registers:
B
flags:
c n z
This method sets the currently active layer for all Layer commands.

Note: This method will also call S65_SetBasePage which is required for the Layer functions
Usage:
Layer_Get layerNum
Parameters:
{byte} {IMM|REG|ABSXY} layerNum - The layer to fetch
GetGotoX
registers: flags:
n z
Returns the gotox value for the current selected layer into S65_ReturnValue
Usage:
Layer_GetGotoX
SetGotoX
registers: flags:
n z
Sets the gotox value for the current selected layer so that it is rendered in a Layer_Update with a new shifted X position
NOTE: Absolute addressing will fetch 2 bytes from the target address (aka ABS16)
Usage:
Layer_SetGotoX gotox
Parameters:
{word} {IMM|REG|ABS16|ABX16|ABY16} gotox - The gotox value to set
Shift
registers: flags:
c n z
Shift the cahrs on this layer horizontally
Usage:
Layer_Shift xshift
Parameters:
{byte} {IMM|REG|ABS|ABX|ABY} xshift - The number of chars to shift (Currently values other than -1 or 1 may cause issues)
SortSprites
registers: flags:
c n z
Y sorts the sprite render order for this layer, using the sprite y pos + its height as a base
Usage:
Layer_SortSprites start
Parameters:
{byte} {IMM} start - start of sprites to sort, allows you to keep some always on top in the higher areas
Update
Updates ALL the layers. This is basically the render method, it sets all the GOTOX markers for the various layers and calls the Sprite_Update method

Note: this is an expensive operation in both memory and cpu, it should be called once only per frame and put it in a subroutine if you need to call it from more than one place
Usage:
Layer_Update

Macros
DefineRRBSpriteLayer
registers:
A
flags:
n z
Defines a new RRB Sprite layer in Screen RAM. RRB Sprite layers are always NCM mode (16x8px chars). IO for this layer is assigned to the Layer IO dynamic memory area
Note that chars per line is NOT the max sprites per line as a sprite can be any multiple of 16 chars wide

RRB Sprite space is a buffer limited by a set amount of chars per line. During an update each new Sprite uses a GOTOX marker and however many RRB chars wide it is. So, for example, a 32x32 RRB sprite is 2 chars wide + a GOTOX marker so will take 3 chars of space.

Note: There is a RRB Sprite hard limit of 256 per RRB Sprite layer. The majority of the memory and cpu time consumed by the RRB Sprite system is a result of the number of maxSprites. Reducing this number will have the best impact on SpriteIO area memory usage as each sprite slot (regardless if its enabled or not in Sprite_IOflagswill use at least 10 bytes each. And every sprite that IS enabled will use some cpu time to update.
Usage:
Layer_DefineRRBSpriteLayer(charsPerLine, maxSprites)
Parameters:
{byte} charsPerLine - The number of RRB chars reserved in the buffer for this layer, lower numbers improve performance but reduce the number of visible sprites per line
{byte} maxSprites - The maximum number of RRB Sprites for this layer, lower numbers improve performance
Sets Registers:
A {byte} - The layer number this layer was created at
DefineResolution
Defines the screen resolution in 8x8 charachters (regardless if NCM is being used) and if it should be stretched horizontally to fit the width of the display. Layer_InitScreen will configure the borders and TEXTXPOS and TEXTYPOS to center the screen on the display.

NOTE:
- charHeight above 32 will use V400 mode and disable any RRB double buffering and double rate RRB
- charWidth above 42 will use H640 mode
Usage:
Layer_DefineResolution(charWidth, charHeight, stretchWide)
Parameters:
{byte} charWidth - The screen base visible width in chars, range 1-84
{byte} charHeight - The screen base visible height in chars, range 1-50
{bool} stretchWide - should this screen be stetched horizontally to fit the screen
DefineScreenLayer
registers:
A
flags:
n z
Defines a new screen layer in Screen RAM optionally shifting its RRB GOTOX offset.
Note: the maximum charWidth for a layer is 126
Usage:
Layer_DefineScreenLayer(charWidth, offsetX, ncm)
Parameters:
{byte} charWidth - The screen layers width in chars
{word} offsetX - The new RRB GotoX position to set for this layer
{bool} ncm - NCM enabled for this layer, defaults to FALSE
Sets Registers:
A {byte} - The layer number this layer was created at
InitScreen
registers:
A X Y Z
flags:
c n z
Initialises the MEGA65 and VIC-IV and parses the Layer definitions into a Screen RAM layout
Usage:
Layer_InitScreen(screenBaseAddress)
Parameters:
{word} screenBaseAddress - The base address of the Screen RAM

Functions
GetColorAddress
Returns the address of the color RAM at the given position on this screen layer
Usage:
Layer_GetColorAddress(layerNumber, xpos, ypos)
Parameters:
{byte} layerNumber - The layer number to fetch
{byte} xpos - The character x position on the screen layer
{byte} ypos - The character y position on the screen layer
GetLayerCount
Returns the current number of layers, useful for assigning to constants to name each layer for use in your code.
Usage:
Layer_GetLayerCount()
GetScreenAddress
Returns the address of the char at the given position on this screen layer
Usage:
Layer_GetScreenAddress(layerNumber, xpos, ypos)
Parameters:
{byte} layerNumber - The layer number to fetch
{byte} xpos - The character x position on the screen layer
{byte} ypos - The character y position on the screen layer

Data
GotoXColorPositions
Table of GOTOX Attribute in color ram for each layer
Usage:
Layer_GotoXColorPositions
{word} $00 - Layer X position
GotoXPositions
Table of current GOTOX positions for all the layers
Usage:
Layer_GotoXPositions
{word} $00 - Layer X position
LayerWidth
Table of start address offsets for each layer
Usage:
Layer_LayerWidth
{word} $00 - Layer width in chars
LogicalWidth
Contains the current Screen Row Logical Width in bytes
Usage:
Layer_LogicalWidth
{word} $00 - Screen Row Logical Width

Vars
AddrOffsets
Pointer to Table of start address offsets for each layer
Usage:
Layer_AddrOffsets
DMAClear
Pointer to a dma subroutine that clears the screen ram layer, A=layer, X=charLo, Y=charHi
Usage:
Layer_DMAClear
DMAClearColor
Pointer to a dma subroutine that clears the color ram layer, A=layer, X=color
Usage:
Layer_DMAClearColor
DMAClearColorRRB
Pointer to a dma subroutine that clears the color ram layer for RRB sprites, A=layer
Usage:
Layer_DMAClearColorRRB
DynamicDataTable
A pointer to the table containing the address of each layers dynamic data, this memory is initialised on a Layer_InitScreen it's size is dependant on the screen layer structure, RRB sprite layers use the most memory
Usage:
Layer_DynamicDataTable
IOgotoX
The IO register index for this layers GOTOX value. Retrieve the absolute address by passing it to the Layer_GetIO function.
Usage:
Layer_IOgotoX
IOmaxCharsRRB
RRB SPRITE LAYER ONLY. The IO register index for this layers maximum chars per line value.
Retrieve the absolute address by passing it to the Layer_GetIO function.
Usage:
Layer_IOmaxCharsRRB
IOmaxSpritesRRB
RRB SPRITE LAYER ONLY. The IO register index for this layers maximum sprites value.
Retrieve the absolute address by passing it to the Layer_GetIO function.
Usage:
Layer_IOmaxSpritesRRB
IOrowCountTableRRB
RRB SPRITE LAYER ONLY. The IO register index for this layers RRB sprite rowCharCount table, used internally by the RRB sprite system.
Retrieve the absolute address by passing it to the Layer_GetIO function.
Usage:
Layer_IOrowCountTableRRB
IsRRBSprite
Pointer to the table of flags indicating if layers are RRB sprite layer
Usage:
Layer_IsRRBSprite
RowAddressBaseLSB
Pointer to the table of LSB values for the address of the start of each screen row directly
Usage:
Layer_RowAddressBaseLSB
RowAddressBaseMSB
Pointer to the table of MSB values for the address of the start of each screen row directly
Usage:
Layer_RowAddressBaseMSB
RowAddressLSB
Pointer to the table of LSB values for the address of the start of each screen row directly after the first GOTOX
Usage:
Layer_RowAddressLSB
RowAddressMSB
Pointer to the table of MSB values for the address of the start of each screen row directly after the first GOTOX
Usage:
Layer_RowAddressMSB
SpriteCount
How many sprites there are max in this layer
Usage:
Layer_SpriteCount
SpriteIOAddrLSB
Pointer to the table of LSB values for the address of the start of the layers sprite IO area
Usage:
Layer_SpriteIOAddrLSB
SpriteIOAddrMSB
Pointer to the table of MSB values for the address of the start of the layers sprite IO area
Usage:
Layer_SpriteIOAddrMSB
SpriteSortListLSB
Pointer to the LSB of table of sorted sprite indices
Usage:
Layer_SpriteSortListLSB
SpriteSortListMSB
Pointer to the MSB of table of sorted sprite indices
Usage:
Layer_SpriteSortListMSB

Lists
LayerList
The list of defined layers used to create the Screen RAM layout
Usage:
Layer_LayerList
Items:
{hashtable} LayerListTable - Layer data hashtable

Hashtables
LayerListTable
A layer definition used to create the Screen RAM layout
Usage:
Layer_LayerListTable
Keys:
{bool} rrbSprites - Is this layer a RRB Sprite layer?
{word} startAddr - The offset in bytes from the start of the screen row for this layer
{byte} charWidth - The width of this layer in chars
{word} offsetX - The GotoX offset for this layer, RRB Sprite layers cannot be offset
{byte} gotoX - The offset in bytes from the start of the screen row for this layers GOTOX marker
{word} dynamicDataAddr - The memory address for this layers dynamic data area


Namespace

Palette

Pseudocommands
Macros
Data
Vars
API for manipulating the MEGA65 Palettes

Pseudocommands
Assign
registers: flags:
c n z
Assigns palettes to the MEGA65 palette banks.
Usage:
Palette_Assign palleteChar : paletteHWSprite : paletteRRBSprite
Parameters:
optional{byte} {IMM} palleteChar - The Palette for Character/Bitmap
optional{byte} {IMM} paletteHWSprite - The Palette for HW Sprites
optional{byte} {IMM} paletteRRBSprite - The Palette for RRB Sprites
Cycle
registers: flags:
c n z
Cycles a range of colors in the current palette shifting them in a loop by 1 backwards
Usage:
Palette_Cycle from : count
Parameters:
{byte} {ABS|IMM|REG} from - The start position to cycle from
{byte} {ABS|IMM|REG} count - the number of colors in the cycle
LoadFromMem
registers: flags:
c n z
Copys palette data from a given address into the currently active palette.
Usage:
Palette_LoadFromMem addr : size
Parameters:
{word} {ABS} addr - The location of the source palette data in memory
optional{byte} {IMM} size - The number of colors in the palette data, defaults to 256
LoadFromSD
registers: flags:
c n z
Loads a full 256 color palette from SD card into the currently active palette
Usage:
Palette_LoadFromSD addr
Parameters:
{word} {ABS} addr - Pointer to the filename in CAPITALS and zero terminated
Set
registers: flags:
n z
Sets the currently active palette in IO memory.
Usage:
Palette_Set paletteNum
Parameters:
{byte} {REG|IMM} paletteNum - The MEGA palette number to map

Macros
LoadFromExternal
registers: flags:
c n z
Loads a full 256 color palette from SD card into the currently active palette using the name referenced in a Asset_AddExternal
Usage:
Palette_LoadFromExternal(name)
Parameters:
{string} name - The Asset name defined in the Asset_AddExternal macro call

Data
MEGA65_Palette
The MEGA65 has up to 4 palettes that can be assigned to the various graphic types Sprite and Character. At any one time one of the palettes will be banked into IO memory at $d100-$d3ff. Note: While the palette format is RGB788 the nybbles of each byte are switched e.g. $F0 becomes $0F, to maintain compatibility with the C65
Usage:
Palette_MEGA65_Palette
{byte} $000-$0ff - Red channel data
{byte} $100-$1ff - Green channel data
{byte} $200-$2ff - Blue channel data

Vars
0
MEGA65 palette 0
Usage:
Palette_0
1
MEGA65 palette 1
Usage:
Palette_1
2
MEGA65 palette 2
Usage:
Palette_2
3
MEGA65 palette 3
Usage:
Palette_3


Namespace

SDCard

Pseudocommands
Macros
API for using the SD card to load assets to memory using mostly macros

Pseudocommands
LoadToAtticRam
registers: flags:
c i z
Loads a file from the SDCard into attic RAM
Usage:
SDCard_LoadToAtticRam addr : filePtr
Parameters:
{word} {ABS} addr - Pointer to the memory load location
{string} {ABS} filePtr - Pointer to the filename (zero terminated) on the SDCard to load
LoadToChipRam
registers: flags:
c i z
Loads a file from the SDCard into chip RAM
Usage:
SDCard_LoadToChipRam addr : filePtr
Parameters:
{dword} {ABS} addr - Pointer to the memory load location
{word} {ABS} filePtr - Pointer to the filename (zero terminated) on the SDCard to load

Macros
LoadExternalToAtticRam
registers: flags:
c n z
Loads a binary from SD card into the attic ram location provided
Usage:
SDCard_LoadExternalToAtticRam(name, name)
Parameters:
{string} name - The Asset name defined in the Asset_AddExternal macro call
{dword} name - The address to load the file into
LoadExternalToChipRam
registers: flags:
c n z
Loads a binary from SD card into the chip ram location provided
Usage:
SDCard_LoadExternalToChipRam(name, name)
Parameters:
{string} name - The Asset name defined in the Asset_AddExternal macro call
{dword} name - The address to load the file into


Namespace

Sprite

Pseudocommands
Macros
Functions
Data
Vars
Contains the API for managing RRB sprites

Pseudocommands
Get
registers:
B
flags:
c n z
This method is a prerequisite for getting or setting any sprites IO registers it sets the "current active" sprite used by the Sprite Get and Set methods by storing the pointer to that sprites IO area in S65_LastSpriteIOPointer

Note: This method will also call S65_SetBasePage which is required for the subsequent Sprite functions
Usage:
Sprite_Get layerNum : sprNum
Parameters:
{byte} {IMM} layerNum - The layer to get sprite from, note if this is NOT an RRB layer writing to addresses pointed to by S65_LastSpriteIOPointer can cause crashes and corruption
{byte} {IMM|REG|ABSXY} sprNum - The sprite number to enable
GetColor
registers: flags:
n z
Returns the color of the currently selected sprite in S65_ReturnValue and the accumulator Lo byte is color
Note that colors are in the upper nybble in NCM mode, so palette slice $02 is represented as $20
Usage:
Sprite_GetColor
GetDimensions
registers: flags:
n z
Returns the dimensions of the currently selected sprite into S65_ReturnValue
Lo byte is width, Hi byte is height
Usage:
Sprite_GetDimensions
GetEnabled
registers: flags:
n z
Returns the enabled state of the current selected sprite into S65_ReturnValue
Usage:
Sprite_GetEnabled
GetFlags
registers: flags:
n z
Returns the flags of the current selected sprite into S65_ReturnValue
Usage:
Sprite_GetFlags
GetPointer
registers: flags:
n z
Returns the current pointer of the currently selected sprite into S65_ReturnValue
Usage:
Sprite_GetPointer
GetPositionX
registers: flags:
n z
Returns the X position of the current selected sprite into S65_ReturnValue
Usage:
Sprite_GetPositionX
GetPositionY
registers: flags:
n z
Returns the Y position of the current selected sprite into S65_ReturnValue
Usage:
Sprite_GetPositionY
ResetFlags
registers: flags:
n z
Resets one or more of the currently selected sprites flags. You can use the flag constants provided by S65 to abstract the values. The other flags are left untouched
Usage:
Sprite_ResetFlags flags
Parameters:
{byte} {IMM|REG|ABS} flags - Sprite flags
SetAnim
registers: flags:
n z
Sets the sprites animation id and resets its timer and frame counter. A value of 0 turns off the animation and returns control of the sprites pointer to Sprite_IOptr.
Usage:
Sprite_SetAnim animId : speed
Parameters:
{byte} {IMM|REG|ABS|ABSX} animId - The id of the animation to assign
optional{byte} {IMM|REG|ABS|ABSX} speed - The speed of the animation, lower=faster, minimum 1 defaults to 4
SetColor
registers: flags:
n z
Sets the currently selected sprites color. Note that colors are in the upper nybble in NCM mode, so palette slice $02 is represented as $20
Usage:
Sprite_SetColor color
Parameters:
{byte} {IMM|REG|ABS} color - Sprites color
SetDimensions
registers: flags:
n z
Sets the currently selected sprites width and height. There is a hard limit of 255 chars to make up any one sprite therefore width * height MUST be less than 256
Usage:
Sprite_SetDimensions width : height
Parameters:
{byte} {IMM|REG|ABS} width - Sprite width in chars
{byte} {IMM|REG|ABS} height - Sprite height in chars
SetEnabled
registers: flags:
n z
Enables or disables the current selected sprite so that it is rendered in a Layer_Update
Sets S65_LastSpriteIOPointer

Usage:
Sprite_SetEnabled enabled
Parameters:
{bool} {IMM|REG} enabled - Sprite enabled flag in the sprites IO
SetFlags
registers: flags:
n z
Sets one or more of the currently selected sprites flags. You can use the flag constants provided by S65 to abstract the values. The other flags are left untouched
Usage:
Sprite_SetFlags flags
Parameters:
{byte} {IMM|REG|ABS} flags - Sprite flags
SetPointer
registers: flags:
n z
Sets the current selected sprite pointer. If pointer is passed as a register it sets ONLY the LSB, MSB sets to 0. If ABS mode is used then two bytes are read from that address and used to set the value
Usage:
Sprite_SetPointer pointer
Parameters:
{word} {IMM|REG|ABS} pointer - The sprite pointer to set
SetPositionX
registers: flags:
n z
Sets the current selected sprite X position. If position is passed as a register it sets ONLY the LSB, MSB sets to 0. If ABS mode is used then two bytes are read from that address and used to set the value
Usage:
Sprite_SetPositionX xpos
Parameters:
{word} {IMM|REG|ABS} xpos - The x position
SetPositionY
registers: flags:
n z
Sets the current selected sprite Y position. If position is passed as a register it sets ONLY the LSB, MSB sets to 0. If ABS mode is used then two bytes are read from that address and used to set the value
Usage:
Sprite_SetPositionY ypos
Parameters:
{word} {IMM|REG|ABS} ypos - The y position
SetSpriteMeta
registers: flags:
c n z
Enables the currently selected sprite and popualtes its IO registers with the meta data for the given spriteset index. Affects:
- IOflags (Enabled, NCM)
- IOwidth
- IOheight
- IOptr
- IOcolor
Usage:
Sprite_SetSpriteMeta spritesId : spriteNum
Parameters:
{byte} {IMM|REG} spritesId - The spriteset index retrieved from Sprite_GetSprites
optional{byte} {IMM|REG|ABSXY} spriteNum - The sprite index from the spriteset (ordered left to right, top to bottom)
Update
Called internally by Layer_Update
Usage:
Sprite_Update

Macros
GenerateLayerData
Internally used macro for assigning sprite IO area during a Layer_InitScreen
Usage:
Sprite_GenerateLayerData()
GenerateMetaData
Internally used to generate the runtime lookup tables for sprite meta data
Usage:
Sprite_GenerateMetaData()

Functions

Data
MetaData
This is a byte array used by the engine when Asset_ImportSpriteset and Asset_ImportSpritesetMeta is used to store all the meta data for the sprites such as mappings from sprite to character numbers, colors etc.
Usage:
Sprite_MetaData
{byte} $00 - Metafile version num
{byte} $01 - NCM mode enable ($00 fcm, >$00 ncm)
{word} $02 - Number of sprites
{byte} $04 - Sprite width in chars
{byte} $05 - Sprite height in chars
{byte} $06-$1f - Reserved for future use
{table} $20+ - character index LSB table
{table} $xx+ - character index MSB table
{table} $xx+ - sprite color table
SequenceData
This is a byte array used by the engine for storing animation sequences. Animations are not stored in a table but in a sequential list, specific aniamtion data addresses can be retrievd using Anim_Get().address
Usage:
Sprite_SequenceData
{byte} $00 - type (for now always 0)
{byte} $01 - spritesetId
{byte} $02 - startFrame
{byte} $03 - endFrame

Vars
IOanim
Sprites animation id if non zero takes over control of the sprites pointer from Sprite_IOptr
Usage:
Sprite_IOanim
IOanimFrame
Used internally to keep track of the current frame for the sprites animation
Usage:
Sprite_IOanimFrame
IOanimSpeed
The speed of the aniamtion, lower=faster, minimum 1
Usage:
Sprite_IOanimSpeed
IOanimTimer
Used internally to keep track of the time per frame for the sprites animation
Usage:
Sprite_IOanimTimer
IOcolor
The color to apply to all the chars in this sprite.
Usage:
Sprite_IOcolor
IOflagEnabled
The value to set the enabled bit in Sprite_IOflags
Usage:
Sprite_IOflagEnabled
IOflagFlipH
The value to set the flip H bit in Sprite_IOflags
Usage:
Sprite_IOflagFlipH
IOflagFlipV
The value to set the flip V bit in Sprite_IOflags

NOTE: Currently due to HW limitations (missing ROWMASK functionality) this flag will cause rendering issues and should NOT be used
Usage:
Sprite_IOflagFlipV
IOflagNCM
Turns on NCM for this sprite

NOTE: It is best to use NCM where possible as it takes half the processing time per visible sprite on a Layer_Update, less chars on a line, and less memory for the image data at the cost of dropping to 16 colors
Usage:
Sprite_IOflagNCM
IOflagOneShot
Starts as zero sets to 1 when the first animation loop has passed
Usage:
Sprite_IOflagOneShot
IOflags
Flags for the state of this sprite bit 7 = Flip V (Not yet implemented) bit 6 = Flip H bit 5 = Enabled bit 0 = One shot flag
Usage:
Sprite_IOflags
IOflipHoffset
This registers is set internally on creation of a sprite and should NOT be manually changed
Usage:
Sprite_IOflipHoffset
IOflipVoffset
This registers is set internally on creation of a sprite and should NOT be manually changed
Usage:
Sprite_IOflipVoffset
IOheight
The height in chars for this sprite, sprites will automatically advance through the char set as they are drawn in pieces, top to bottom, left to right.
Usage:
Sprite_IOheight
IOptr
The IO register offset for looking up the sprite pointer location in memory. Pass this to the Sprite_GetIO function, to retrieve the address.
Usage:
Sprite_IOptr
IOwidth
The width in chars for this sprite, sprites will automatically advance through the char set as they are drawn in pieces, top to bottom, left to right.
Usage:
Sprite_IOwidth
IOx
The IO register offset for looking up the a sprites x position location in memory. Pass this to the Sprite_GetIO function, to retrieve the address.
Usage:
Sprite_IOx
IOy
The IO register offset for looking up the a sprites y position location in memory. Pass this to the Sprite_GetIO function, to retrieve the address.
Usage:
Sprite_IOy
SpriteIOLength
The size in bytes of the IO registers for a single sprite
Usage:
Sprite_SpriteIOLength


Namespace

System

Pseudocommands
Macros
Data
Tools and utilities for configuring the processor and launching programs.

Pseudocommands
Compare16
registers: flags:
c n z
peforms a 16bit compare between value A and valueB, setting flags accordingly
Usage:
System_Compare16 valueA : valueB
Parameters:
{byte} {IMM|ABS16|ABX16|ABY16} valueA - Vlaue to compare against valueB
{byte} {IMM|ABS16|ABX16|ABY16} valueB - Value to compare against valueA
GetRandom16
registers:
A
flags:
c n z
Returns a random 16 bit number in S65_ReturnValue
Accumulator will have the hi byte
Usage:
System_GetRandom16
GetRandom8
registers:
A
flags:
c n z
Returns a random 8 bit number in S65_ReturnValue and the accumulator
Usage:
System_GetRandom8
HideScreen
registers: flags:
n z
Hides the display by setting illegal values in $d011. Useful for blacking out screen when loading from SDCard
Usage:
System_HideScreen
SeedRandom16
registers: flags:
n z
Seeds the random number generator using a 16bit non zero value. Passing a register will use that byute for both LSB and MSB
Usage:
System_SeedRandom16 seed
Parameters:
{byte} {REG|IMM|ABS16|ABX16|ABY16} seed - The 16bit value to use as a seed
ShowScreen
registers: flags:
n z
Shows the display after a HideScreen by setting valid values in $d011. Useful for blacking out screen when loading from SDCard
Usage:
System_ShowScreen

Macros
BasicUpstart65
Creates the Basic Upstart for the MEGA65 at location $2001 pointing to the given SYS entry address
Usage:
System_BasicUpstart65(addr)
Parameters:
{word} addr - Pointer to the program entry point
BorderDebug
registers:
A
flags:
n z
If the preprocessor #define NODEBUG is not defined this will set the border color to the give value. Useful for debugging
Usage:
System_BorderDebug(color)
Parameters:
{byte} color - The color to set the border
DisableC65ROM
registers:
A
flags:
i z
Disables the C65 rom protection making it writable
Usage:
System_DisableC65ROM()
Enable40Mhz
registers:
A
flags:
i z
Switches the MEGA65 45GS_02 processor to use 40.5Mhz mode
Usage:
System_Enable40Mhz()
EnableFastRRB
registers:
A
flags:
n z
Enables rewrite double buffering to prevent clipping on the left side of the screen and enable 2 raster scans per line for double pixel clock in V200 mode.
Usage:
System_EnableFastRRB()
EnableVIC4
registers:
A X Y Z
flags:
i z
Switches the VIC chip in the MEGA65 into VIC4 mode.
Usage:
System_EnableVIC4()
WaitForRaster
registers:
A
flags:
c n z
Halts execution and waits for the given raster line
Usage:
System_WaitForRaster(raster)
Parameters:
{byte} raster - The line to wait for

Data
BasicUpstart65_Format
Usage:
System_BasicUpstart65_Format
{word} $2001 - Pointer to next end of command marker $2009
{word} $2003 - Basic Line number 10
{dword} $2005 - Basic "BANK 0"
{word} $2009 - Pointer to next snd of command marker after screencode string
{word} $200b - Basic Line number 20
{byte} $200d - Basic "SYS"
{string} $200e - Entry point as screencode string
{byte} ???? - Terminating Zero
{word} ???? - End of Basic terminating zeros


Namespace

Tilemap

Pseudocommands
Data
API for the tilemap system

Pseudocommands
Draw
registers: flags:
c n z
Draws a rectangle from the currently active tilemap to the currently active layer using the current screenpointers
NOTE: that this routine will NOT wrap at the edge of the later so make sure to not ewxceed the layers right edge or you may cause RRB corruption
Usage:
Tilemap_Draw srcX : srcY : srcWidth : srcHeight
Parameters:
{byte} {IMM|REG|ABS|ABSX|ABSY} srcX - The source rectangle X
{byte} {IMM|REG|ABS|ABSX|ABSY} srcY - The source rectangle Y
{byte} {IMM|REG|ABS|ABSX|ABSY} srcWidth - The source rectangle width
{byte} {IMM|REG|ABS|ABSX|ABSY} srcHeight - The source rectangel height
DrawTile
Draw a single tile at the current location
Usage:
Tilemap_DrawTile tileId
Parameters:
{byte} {IMM|REG|ABS} tileId - The tile id to draw at the current location
Get
registers:
B
flags:
c n z
This method sets the currently active tilemap for all tilemap commands.

Note: This method will also call S65_SetBasePage which is required for the Tilemap functions
Usage:
Tilemap_Get tilemapId
Parameters:
{byte} {IMM} tilemapId - The id of the tilemap to fetch

Data
TilemapData
Start of the tables for the Tilemap meta data
Usage:
Tilemap_TilemapData
{byte} $00 - widthLSB
{byte} $XX - widthMSB
{byte} $XX - heightLSB
{byte} $XX - heightMSB
{byte} $XX - tilewidth
{byte} $XX - tileheight
{byte} $XX - tilesize, total char count per tile
{byte} $XX - colorlookup byte 0
{byte} $XX - colorlookup byte 1
{byte} $XX - colorlookup byte 2