-
Notifications
You must be signed in to change notification settings - Fork 34
Home
Welcome to the devkitSMS wiki! How devkitSMS/SMSlib works. A small journey into functions and functionalities.
The SEGA Master System and its portable brother, the SEGA Game Gear, are 8-bit machines powered by two hearts. One heart is the Zilog Z80 processor, while the other is a custom graphic chip (derived from the Texas Instruments TMS9918) that handles everything that's on-screen: it's called the 'Video Display Processor', or VDP for short. Different Master System models can have different VDP revisions with different features, and the Game Gear one also has a few differences, but as long as we're concerned we can consider them all equal. There will be a special note when distinctions would be unavoidable.
The VDP works by redrawing the screen top to bottom 50 or 60 times a second, according to television standards. 192 lines of pixels will be on screen in any case, and each line (a scanline) will be 256 pixel wide, making the image resolution 256x192 pixel. The pixels will use colors from two 16-color palettes, one reserved for background tiles and one that can be used both for sprites and background tiles. This also means that all the sprites will share the same 16-color palette.
The background is made up of 16-color 8x8 pixel tiles. Both tiles and tilemap (which describes how tiles should be placed on-screen) need to be loaded into Video RAM (VRAM). The tilemap is a single 32x28 table where each entry contains information about which tile should be used, which palette, and if sprites are supposed to appear behind that tile or in front of it. VRAM size is 16 KB and it also contains the Sprite Attribute Table (SAT).
Sprites are made of 16-color 8x8 pixel tiles too, but they can be placed anywhere on screen. The VDP supports up to 64 of them at the same time, even if won't draw more than 8 of them on any given scanline. Their positions and images (the tiles used) are stored into SAT, which is again allocated into VRAM. VRAM is big enough to theoretically contain up to 512 tiles, but only tiles defined into either the 1st or the 2nd half of VRAM can be used.
SMSlib is substantially a wrapper that will hide most of the hardware inner workings, making it easier for anyone to write programs on the Master System platform. It's meant to be used with devkitSMS, and simply using devkitSMS and including SMSlib.h in your code you'll automatically initialize the library. This means the whole hardware will be ready straight from the beginning. The ROM mappers prepared, the VDP internal registers initialized to common values, the vBlank and pause handlers both ready.
In detail, the VDP will be initialized to 'Mode 4', which is Master System own mode, with the features described in the Introduction. Background scrolling will be reset to 0 both X & Y direction, the SAT (256 bytes) allocated at end of VRAM, the tilemap (1.75 KB, also called 'Pattern Names Table', PNT) just before the SAT, so to have a contiguous 14 KB free space in VRAM, enough for 448 tiles, from the VRAM beginning on. The display (and the vBlank interrupt) will be off, since the VRAM needs first to be loaded with some content before displaying anything meaningful.
To load tiles into VRAM, you can use
SMS_loadTiles (void *src, unsigned int Tilefrom, unsigned int len)
and to load a tilemap you can use
SMS_loadTileMap (unsigned char x, unsigned char y, void *src, unsigned int len)
(if you've got an STM compressed tilemap, generated by Maxim's Bmp2Tile, you can use)
SMS_loadSTMcompressedTileMap (unsigned char x, unsigned char y, unsigned char *src)
Then of course you need to load the palettes for your background and for your sprites eventually
SMS_loadBGPalette (void *palette)
SMS_loadSpritePalette (void *palette)
done that, you can finally turn on the screen, using the macro
SMS_displayOn()