libws libws
WSwan hardware library for the Wonderful toolchain
Loading...
Searching...
No Matches
display.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022, 2023 Adrian "asie" Siekierka
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 *
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 *
20 * 3. This notice may not be removed or altered from any source distribution.
21 */
22
23#ifndef LIBWS_DISPLAY_H_
24#define LIBWS_DISPLAY_H_
25
26#ifndef __ASSEMBLER__
27#include <assert.h>
28#include <stdbool.h>
29#include <stdint.h>
30#include <string.h>
31#endif
32#include <wonderful.h>
33#include "memory.h"
34#include "ports.h"
35
39
44
45#define WS_DISPLAY_WIDTH_TILES 28
46#define WS_DISPLAY_HEIGHT_TILES 18
47#define WS_DISPLAY_WIDTH_PIXELS (WS_DISPLAY_WIDTH_TILES * WS_DISPLAY_TILE_WIDTH)
48#define WS_DISPLAY_HEIGHT_PIXELS (WS_DISPLAY_HEIGHT_TILES * WS_DISPLAY_TILE_HEIGHT)
49
50#define WS_DISPLAY_TILE_WIDTH 8
51#define WS_DISPLAY_TILE_HEIGHT 8
55#define WS_DISPLAY_TILE_SIZE 16
59#define WS_DISPLAY_TILE_SIZE_4BPP 32
60
61#ifndef WS_DISPLAY_VTOTAL
65#define WS_DISPLAY_VTOTAL 159
66#endif
67
68#ifndef __ASSEMBLER__
72typedef struct {
73 union {
76 };
78
82typedef struct {
83 union {
86 };
88#endif
89
93#define WS_SCREEN_ATTR_TILE(x) (x)
94#define WS_SCREEN_ATTR_TILE_MASK (0x1FF)
98#define WS_SCREEN_ATTR_PALETTE(x) ((x) << 9)
99#define WS_SCREEN_ATTR_PALETTE_MASK (0xF << 9)
103#define WS_SCREEN_ATTR_BANK(x) ((x) << 13)
104#define WS_SCREEN_ATTR_BANK_MASK (0x2000)
108#define WS_SCREEN_ATTR_TILE_EX(x) (((x) & 0x1FF) | (((x) >> 13) << 13))
109#define WS_SCREEN_ATTR_TILE_EX_MASK (0x21FF)
110
114#define WS_SCREEN_ATTR_FLIP_H 0x4000
118#define WS_SCREEN_ATTR_FLIP_V 0x8000
122#define WS_SCREEN_ATTR_FLIP 0xC000
123#define WS_SCREEN_ATTR_FLIP_MASK 0xC000
124
125#define WS_SCREEN_WIDTH_TILES 32
126#define WS_SCREEN_HEIGHT_TILES 32
127#define WS_SCREEN_WIDTH_PIXELS (WS_SCREEN_WIDTH_TILES * WS_DISPLAY_TILE_WIDTH)
128#define WS_SCREEN_HEIGHT_PIXELS (WS_SCREEN_HEIGHT_TILES * WS_DISPLAY_TILE_HEIGHT)
129
133#define WS_SPRITE_ATTR_TILE(x) (x)
134#define WS_SPRITE_ATTR_TILE_MASK (0x1FF)
138#define WS_SPRITE_ATTR_PALETTE(x) ((x) << 9)
139#define WS_SPRITE_ATTR_PALETTE_MASK (0x7 << 9)
143#define WS_SPRITE_ATTR_OUTSIDE 0x1000
147#define WS_SPRITE_ATTR_PRIORITY 0x2000
151#define WS_SPRITE_ATTR_FLIP_H 0x4000
155#define WS_SPRITE_ATTR_FLIP_V 0x8000
159#define WS_SPRITE_ATTR_FLIP 0xC000
160#define WS_SPRITE_ATTR_FLIP_MASK 0xC000
164#define WS_SPRITE_MAX_COUNT 128
168#define WS_SPRITE_MAX_LINE_COUNT 32
169
170#ifndef __ASSEMBLER__
171typedef struct {
172 union {
173 struct {
177 };
179_Static_assert(sizeof(ws_screen_t) == WS_SCREEN_WIDTH_TILES * WS_SCREEN_HEIGHT_TILES * 2, "invalid ws_screen_t size");
180
181typedef struct {
182 uint16_t attr;
183 uint8_t y;
184 uint8_t x;
186_Static_assert(sizeof(ws_sprite_t) == 4, "invalid ws_sprite_t size");
187
191_Static_assert(sizeof(ws_sprite_table_t) == WS_SPRITE_MAX_COUNT * 4, "invalid ws_sprite_table_t size");
192#endif
193
201#define WS_RGB(r, g, b) (((r) << 8) | ((g) << 4) | (b))
202
206#define WS_DISPLAY_MONO_PALETTE(c0, c1, c2, c3) ((c0) | ((c1) << 4) | ((c2) << 8) | ((c3) << 12))
207
208#ifndef __ASSEMBLER__
209
215#define WS_TILE_MEM(i) ((ws_display_tile_t ws_iram *) (0x2000 + ((i) << 4)))
216
222#define WS_TILE_4BPP_MEM(i) ((ws_display_tile_4bpp_t ws_iram *) (0x4000 + ((i) << 5)))
223
230#define WS_TILE_BANKED_MEM(b, i) ((ws_display_tile_t ws_iram *) (0x2000 + (!!(b) << 13) + ((i) << 4)))
231
238#define WS_TILE_4BPP_BANKED_MEM(b, i) ((ws_display_tile_4bpp_t ws_iram *) (0x4000 + (!!(b) << 14) + ((i) << 5)))
239
245#define WS_DISPLAY_COLOR_MEM(i) ((uint16_t ws_iram *) (0xFE00 + ((i) << 5)))
246
252#define WS_SCREEN_COLOR_MEM WS_DISPLAY_COLOR_MEM
253
259#define WS_SPRITE_COLOR_MEM(i) ((uint16_t ws_iram *) (0xFF00 + ((i) << 5)))
260
261#define WS_DISPLAY_SHADE_LUT(c0, c1, c2, c3, c4, c5, c6, c7) \
262 (((uint32_t)(c0)) | (((uint32_t)(c1)) << 4) | (((uint32_t)(c2)) << 8) | (((uint32_t)(c3)) << 12) | \
263 (((uint32_t)(c4)) << 16) | (((uint32_t)(c5)) << 20) | (((uint32_t)(c6)) << 24) | (((uint32_t)(c7)) << 28))
264#define WS_DISPLAY_SHADE_LUT_DEFAULT WS_DISPLAY_SHADE_LUT(0, 2, 4, 6, 9, 11, 13, 15)
265
266// TODO: Add ws_display_set_backdrop (has to consider mono/color modes have different values)
267
275static inline void ws_display_load_palette_mono(const void __far* data, int first, int count) {
276 ws_portcpy(WS_SCR_PAL_PORT(first), data, count * 2);
277}
278
286void ws_display_load_palette_color_2bpp(const void __far* data, int first, int count);
287
295static inline void ws_display_load_palette_color_4bpp(const void __far* data, int first, int count) {
296 memcpy(WS_DISPLAY_COLOR_MEM(first), data, count * 32);
297}
298
299#define ws_sprite_load_palette_mono(data, first, count) ws_display_load_palette_mono((data), (first) + 8, (count))
300#define ws_sprite_load_palette_color_2bpp(data, first, count) ws_display_load_palette_color_2bpp((data), (first) + 8, (count))
301#define ws_sprite_load_palette_color_4bpp(data, first, count) ws_display_load_palette_color_4bpp((data), (first) + 8, (count))
302
314static inline void ws_display_set_control(uint8_t value) {
315 outportb(WS_DISPLAY_CTRL_PORT, value);
316}
317
321static inline uint8_t ws_display_get_control(void) {
322 return inportb(WS_DISPLAY_CTRL_PORT);
323}
324
331static inline void ws_display_set_screen_addresses(const void __wf_iram* scr1_addr, const void __wf_iram* scr2_addr) {
332 outportb(WS_SCR_BASE_PORT, WS_SCR_BASE_ADDR1(scr1_addr) | WS_SCR_BASE_ADDR2(scr2_addr));
333}
334
341static inline void ws_display_set_screen_address(uint8_t screen, const void __wf_iram* address) {
342 outportb(WS_SCR_BASE_PORT, (inportb(WS_SCR_BASE_PORT) & (0xF0 >> (screen * 4))) | (WS_SCR_BASE_ADDR1(address) << (screen * 4)));
343}
344
350#define ws_display_set_screen1_address(address) ws_display_set_screen_address(0, (address))
351
357#define ws_display_set_screen2_address(address) ws_display_set_screen_address(1, (address))
358
364static inline void ws_display_set_sprite_address(const void __wf_iram* address) {
365 outportb(WS_SPR_BASE_PORT, WS_SPR_BASE_ADDR(address));
366}
367
376static inline void ws_display_set_screen2_window(uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
377 outportw(WS_SCR2_WIN_X1_PORT, x | (y << 8));
378 outportw(WS_SCR2_WIN_X2_PORT, ((x + width - 1) & 0xFF) | ((y + height - 1) << 8));
379}
380
389static inline void ws_display_set_screen2_window_corners(uint8_t left, uint8_t top, uint8_t right, uint8_t bottom) {
390 outportw(WS_SCR2_WIN_X1_PORT, left | (top << 8));
391 outportw(WS_SCR2_WIN_X2_PORT, right | (bottom << 8));
392}
393
402static inline void ws_display_set_sprite_window(uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
403 outportw(WS_SPR_WIN_X1_PORT, x | (y << 8));
404 outportw(WS_SPR_WIN_X2_PORT, ((x + width - 1) & 0xFF) | ((y + height - 1) << 8));
405}
406
415static inline void ws_display_set_sprite_window_corners(uint8_t left, uint8_t top, uint8_t right, uint8_t bottom) {
416 outportw(WS_SPR_WIN_X1_PORT, left | (top << 8));
417 outportw(WS_SPR_WIN_X2_PORT, right | (bottom << 8));
418}
419
427static inline void ws_display_scroll_screen_to(uint8_t screen, uint8_t x, uint8_t y) {
428 outportw(WS_SCR1_SCRL_X_PORT + (screen * 2), x | (y << 8));
429}
430
437#define ws_display_scroll_screen1_to(x, y) ws_display_scroll_screen_to(0, (x), (y))
438
445#define ws_display_scroll_screen2_to(x, y) ws_display_scroll_screen_to(1, (x), (y))
446
454void ws_display_scroll_screen_by(uint8_t screen, int16_t x, int16_t y);
455
463static inline uint8_t ws_display_get_current_line(void) {
464 return inportb(WS_DISPLAY_LINE_PORT);
465}
466
473static inline void ws_display_scroll_screen1_by(uint8_t x, uint8_t y) {
474 __asm (
475 "in $0x10, %%ax\nadd %0, %%al\nadd %1, %%ah\nout %%ax, $0x10"
476 : : "g"(x), "g"(y) : "a", "cc"
477 );
478}
479
486static inline void ws_display_scroll_screen2_by(uint8_t x, uint8_t y) {
487 __asm (
488 "in $0x12, %%ax\nadd %0, %%al\nadd %1, %%ah\nout %%ax, $0x12"
489 : : "g"(x), "g"(y) : "a", "cc"
490 );
491}
492
504static inline void ws_display_set_icons(uint8_t mask) {
505 outportb(WS_LCD_ICON_PORT, mask);
506}
507
519static inline void ws_display_show_icons(uint8_t mask) {
520 outportb(WS_LCD_ICON_PORT, inportb(WS_LCD_ICON_PORT) | mask);
521}
522
534static inline void ws_display_hide_icons(uint8_t mask) {
535 outportb(WS_LCD_ICON_PORT, inportb(WS_LCD_ICON_PORT) & ~mask);
536}
537
549static inline void ws_display_toggle_icons(uint8_t mask) {
550 outportb(WS_LCD_ICON_PORT, inportb(WS_LCD_ICON_PORT) ^ mask);
551}
552
560__attribute__((no_assume_ds_data, no_assume_ss_data, save_all))
561void ws_display_set_shade_lut(uint32_t lut);
562
566#define ws_display_set_shade_lut_default() ws_display_set_shade_lut(WS_DISPLAY_SHADE_LUT_DEFAULT)
567
573static inline void ws_lcd_control_enable(void) {
575}
576
580static inline void ws_lcd_control_disable(void) {
582}
583
594static inline void ws_screen_put_tiles(void ws_iram *dest, const void __far *src, uint16_t x, uint16_t y, uint16_t width, uint16_t height) {
595 void __libws_screen_put_tiles(void ws_iram *dest, const void __far *src, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t pitch);
596 __libws_screen_put_tiles(dest, src, x, y, width, height, width);
597}
598
613static inline void ws_screen_put_tiles_ex(void ws_iram *dest, const void __far *src, uint16_t sx, uint16_t sy, uint16_t pitch, uint16_t dx, uint16_t dy, uint16_t width, uint16_t height) {
614 void __libws_screen_put_tiles(void ws_iram *dest, const void __far *src, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t pitch);
615 __libws_screen_put_tiles(dest, ((const uint16_t __far*) src) + (sy * pitch) + sx, dx, dy, width, height, pitch);
616}
617
628void ws_screen_get_tiles(void __far *dest, const void ws_iram *src, uint16_t x, uint16_t y, uint16_t width, uint16_t height);
629
640void ws_screen_fill_tiles(void ws_iram *dest, uint16_t src, uint16_t x, uint16_t y, uint16_t width, uint16_t height);
641
653void ws_screen_modify_tiles(void ws_iram *dest, uint16_t mask, uint16_t value, uint16_t x, uint16_t y, uint16_t width, uint16_t height);
654
663static inline void ws_screen_put_tile(void ws_iram *dest, uint16_t src, uint16_t x, uint16_t y) {
664 ((uint16_t ws_iram*) dest)[((y & 0x1F) << 5) | (x & 0x1F)] = src;
665}
666
674static inline uint16_t ws_screen_get_tile(void ws_iram *src, uint16_t x, uint16_t y) {
675 return ((uint16_t ws_iram*) src)[((y & 0x1F) << 5) | (x & 0x1F)];
676}
677
678#endif
679
681
682#endif /* LIBWS_DISPLAY_H_ */
static void ws_display_scroll_screen1_by(uint8_t x, uint8_t y)
Scroll screen 1 by a specified number of pixels.
Definition display.h:473
static void ws_display_load_palette_mono(const void __far *data, int first, int count)
Load monochrome palette data (2 bytes per palette).
Definition display.h:275
#define WS_SCREEN_HEIGHT_TILES
Definition display.h:126
static void ws_display_set_screen2_window(uint8_t x, uint8_t y, uint8_t width, uint8_t height)
Set the position and size of the screen 2 window.
Definition display.h:376
static void ws_screen_put_tiles_ex(void ws_iram *dest, const void __far *src, uint16_t sx, uint16_t sy, uint16_t pitch, uint16_t dx, uint16_t dy, uint16_t width, uint16_t height)
Place a map of tiles on the screen. This varianta llows specifying the source X position,...
Definition display.h:613
static void ws_display_toggle_icons(uint8_t mask)
Toggle specified LCD icons.
Definition display.h:549
static void ws_display_set_sprite_address(const void __wf_iram *address)
Set the base addresses of the sprite table.
Definition display.h:364
static void ws_display_scroll_screen2_by(uint8_t x, uint8_t y)
Scroll screen 2 by a specified number of pixels.
Definition display.h:486
void ws_display_scroll_screen_by(uint8_t screen, int16_t x, int16_t y)
Scroll the specified screen by a specified number of pixels.
static void ws_screen_put_tile(void ws_iram *dest, uint16_t src, uint16_t x, uint16_t y)
Put a tile on the screen.
Definition display.h:663
static void ws_display_set_control(uint8_t value)
Set which layers and windows are visible on the display.
Definition display.h:314
static void ws_display_set_icons(uint8_t mask)
Set the list of displayed LCD icons, clearing any unspecified icons.
Definition display.h:504
void ws_display_load_palette_color_2bpp(const void __far *data, int first, int count)
Load 2BPP color palette data (4 words per palette).
static void ws_display_set_screen_address(uint8_t screen, const void __wf_iram *address)
Set the base addresses of the specified screen.
Definition display.h:341
void ws_screen_fill_tiles(void ws_iram *dest, uint16_t src, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Fill an area on the screen with a given tile.
static void ws_display_scroll_screen_to(uint8_t screen, uint8_t x, uint8_t y)
Scroll the specified screen to a specified location.
Definition display.h:427
static void ws_display_set_screen2_window_corners(uint8_t left, uint8_t top, uint8_t right, uint8_t bottom)
Set the corners of the screen 2 window.
Definition display.h:389
static void ws_display_hide_icons(uint8_t mask)
Hide specified LCD icons.
Definition display.h:534
static void ws_display_load_palette_color_4bpp(const void __far *data, int first, int count)
Load 4BPP color palette data (16 words per palette).
Definition display.h:295
static uint16_t ws_screen_get_tile(void ws_iram *src, uint16_t x, uint16_t y)
Get a tile on the screen.
Definition display.h:674
void ws_screen_get_tiles(void __far *dest, const void ws_iram *src, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Copy a map of tiles from the screen.
static void ws_display_show_icons(uint8_t mask)
Show specified LCD icons.
Definition display.h:519
static void ws_lcd_control_enable(void)
Enable the LCD panel.
Definition display.h:573
static void ws_lcd_control_disable(void)
Disable the LCD panel.
Definition display.h:580
static void ws_screen_put_tiles(void ws_iram *dest, const void __far *src, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Place a map of tiles on the screen.
Definition display.h:594
static uint8_t ws_display_get_control(void)
Query which layers and windows are visible on the display.
Definition display.h:321
static void ws_display_set_sprite_window(uint8_t x, uint8_t y, uint8_t width, uint8_t height)
Set the position and size of the sprite window.
Definition display.h:402
void ws_screen_modify_tiles(void ws_iram *dest, uint16_t mask, uint16_t value, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Modify an area on the screen with given data.
static void ws_display_set_screen_addresses(const void __wf_iram *scr1_addr, const void __wf_iram *scr2_addr)
Set the base addresses of screens 1 and 2.
Definition display.h:331
void ws_display_set_shade_lut(uint32_t lut)
Configure the shade LUT.
#define WS_SPRITE_MAX_COUNT
Sprite: maximum number of sprites in a sprite table.
Definition display.h:164
static uint8_t ws_display_get_current_line(void)
Get the line currently being drawn to the line buffer.
Definition display.h:463
#define WS_DISPLAY_TILE_HEIGHT
Definition display.h:51
static void ws_display_set_sprite_window_corners(uint8_t left, uint8_t top, uint8_t right, uint8_t bottom)
Set the corners of the sprite window.
Definition display.h:415
#define WS_SCREEN_WIDTH_TILES
Definition display.h:125
#define WS_DISPLAY_COLOR_MEM(i)
Pointer to color palette.
Definition display.h:245
#define WS_SCR2_WIN_X2_PORT
Definition ports.h:242
#define WS_SCR_PAL_PORT(i)
Definition ports.h:403
#define WS_DISPLAY_CTRL_PORT
Definition ports.h:159
#define WS_SPR_BASE_PORT
Definition ports.h:209
#define WS_SPR_BASE_ADDR(n)
Definition ports.h:210
#define WS_LCD_CTRL_PORT
Definition ports.h:292
#define WS_SPR_WIN_X1_PORT
Definition ports.h:252
#define WS_DISPLAY_LINE_PORT
Definition ports.h:199
#define WS_SCR1_SCRL_X_PORT
Definition ports.h:272
#define WS_SCR_BASE_ADDR1(n)
Definition ports.h:226
#define WS_SCR_BASE_ADDR2(n)
Definition ports.h:227
#define WS_LCD_CTRL_DISPLAY_ENABLE
Definition ports.h:315
#define WS_SPR_WIN_X2_PORT
Definition ports.h:262
#define WS_LCD_ICON_PORT
Definition ports.h:325
#define WS_SCR2_WIN_X1_PORT
Definition ports.h:232
#define WS_SCR_BASE_PORT
Definition ports.h:225
#define ws_iram
Definition memory.h:55
void ws_portcpy(uint16_t port, const void __far *src, uint16_t count)
Copy memory data to I/O ports, in order.
A structure representing a 4 bit per pixel tile.
Definition display.h:82
uint8_t plane[WS_DISPLAY_TILE_HEIGHT][4]
Per-byte access: 8 rows of 4 planes.
Definition display.h:85
uint32_t row[WS_DISPLAY_TILE_HEIGHT]
Per-row access: 32 bits per row.
Definition display.h:84
A structure representing a 2 bit per pixel tile.
Definition display.h:72
uint8_t plane[WS_DISPLAY_TILE_HEIGHT][2]
Per-byte access: 8 rows of 2 planes.
Definition display.h:75
uint16_t row[WS_DISPLAY_TILE_HEIGHT]
Per-row access: 16 bits per row.
Definition display.h:74
uint16_t cell[WS_SCREEN_WIDTH_TILES]
Definition display.h:174
uint8_t x
Definition display.h:184
uint8_t y
Definition display.h:183
uint16_t attr
Definition display.h:182
ws_sprite_t entry[WS_SPRITE_MAX_COUNT]
Definition display.h:189