pico9918-core 1.3.0
TMS9918A / F18A video display processor emulation in C99
Loading...
Searching...
No Matches
pico9918_util.h
Go to the documentation of this file.
1/**
2 * \file
3 * \brief pico9918-core - utility / helper functions
4 *
5 * Copyright (c) 2022 Troy Schrapel
6 *
7 * This code is licensed under the MIT license
8 *
9 * https://github.com/visrealm/pico9918-core
10 */
11
12#ifndef _PICO9918_UTIL_H
13#define _PICO9918_UTIL_H
14
15#include "pico9918.h"
16
17#include <stddef.h>
18#include <string.h>
19
20/** \brief the VRAM table layout pico9918_initialise_gfx_i() and GfxII() program */
21#define TMS_DEFAULT_VRAM_NAME_ADDRESS 0x3800 /**< name table (R2) */
22#define TMS_DEFAULT_VRAM_COLOR_ADDRESS 0x0000 /**< colour table (R3) */
23#define TMS_DEFAULT_VRAM_PATT_ADDRESS 0x2000 /**< pattern table (R4) */
24#define TMS_DEFAULT_VRAM_SPRITE_ATTR_ADDRESS 0x3B00 /**< sprite attribute table (R5) */
25#define TMS_DEFAULT_VRAM_SPRITE_PATT_ADDRESS 0x1800 /**< sprite pattern table (R6) */
26
27/** \brief the sixteen TMS9918 colours, each packed as 0xrrggbbaa */
28PICO9918_DLLEXPORT_CONST uint32_t pico9918_palette[];
29
30/** \brief write a VDP register as a host would, value byte first */
32{
34 pico9918_write_addr(PICO9918_INST 0x80 | (uint8_t)reg);
35}
36
37/** \brief point the VRAM address register at \p addr for reading */
38inline static void pico9918_set_address_read(PICO9918_INST_ARG uint16_t addr)
39{
41 pico9918_write_addr(PICO9918_INST((addr & 0xff00) >> 8));
42}
43
44/** \brief point the VRAM address register at \p addr for writing, ie. with bit 14 set */
45inline static void pico9918_set_address_write(PICO9918_INST_ARG uint16_t addr)
46{
48}
49
50/** \brief write a block of bytes to VRAM from the current address */
51inline static void pico9918_write_bytes(PICO9918_INST_ARG const uint8_t* bytes, size_t numBytes)
52{
53 for (size_t i = 0; i < numBytes; ++i)
54 {
56 }
57}
58
59/** \brief write \p byte to VRAM \p rpt times from the current address */
60inline static void pico9918_write_byte_rpt(PICO9918_INST_ARG uint8_t byte, size_t rpt)
61{
62 for (size_t i = 0; i < rpt; ++i)
63 {
65 }
66}
67
68
69/** \brief write a string to VRAM, its terminator excluded */
70inline static void pico9918_write_string(PICO9918_INST_ARG const char* str)
71{
72 size_t len = strlen(str);
73 for (size_t i = 0; i < len; ++i)
74 {
76 }
77}
78
79/** \brief write a string to VRAM with \p offset added to each character's pattern index */
80inline static void pico9918_write_string_offset(PICO9918_INST_ARG const char* str, uint8_t offset)
81{
82 size_t len = strlen(str);
83 for (size_t i = 0; i < len; ++i)
84 {
85 pico9918_write_data(PICO9918_INST str[i] + offset);
86 }
87}
88
89/** \brief pack \p fg into the high nibble and \p bg into the low nibble of one colour byte */
91{
92 return (uint8_t)((uint8_t)fg << 4) | (uint8_t)bg;
93}
94
95/** \brief set the name table address, which register 2 holds in 1KB units */
96inline static void pico9918_set_name_table_addr(PICO9918_INST_ARG uint16_t addr)
97{
98 pico9918_write_register_value(PICO9918_INST TMS_REG_NAME_TABLE, addr >> 10);
99}
100
101/** \brief set the colour table address, which register 3 holds in 64-byte units */
102inline static void pico9918_set_color_table_addr(PICO9918_INST_ARG uint16_t addr)
103{
104 pico9918_write_register_value(PICO9918_INST TMS_REG_COLOR_TABLE, (uint8_t)(addr >> 6));
105}
106
107/** \brief set the pattern table address, which register 4 holds in 2KB units */
108inline static void pico9918_set_pattern_table_addr(PICO9918_INST_ARG uint16_t addr)
109{
110 pico9918_write_register_value(PICO9918_INST TMS_REG_PATTERN_TABLE, addr >> 11);
111}
112
113/** \brief set the sprite attribute table address, which register 5 holds in 128-byte units */
115{
116 pico9918_write_register_value(PICO9918_INST TMS_REG_SPRITE_ATTR_TABLE, (uint8_t)(addr >> 7));
117}
118
119/** \brief set the sprite pattern table address, which register 6 holds in 2KB units */
121{
122 pico9918_write_register_value(PICO9918_INST TMS_REG_SPRITE_PATT_TABLE, addr >> 11);
123}
124
125/** \brief set register 7, the text-mode foreground colour and the backdrop */
130
131
132/** \brief set up Graphics I with the default table addresses and a cleared VRAM */
135
136/** \brief set up Graphics II with the default table addresses and a cleared VRAM */
139
140#endif // _PICO9918_UTIL_H
void pico9918_write_addr(pico9918_t *tms9918, uint8_t data)
write an address (mode = 1) to the tms9918
Definition pico9918.c:373
void pico9918_write_data(pico9918_t *tms9918, uint8_t data)
write data (mode = 0) to the tms9918
Definition pico9918.c:395
pico9918-core - core interface
pico9918_color_t
the sixteen TMS9918 colours, in palette-index order
Definition pico9918.h:177
#define PICO9918_INST_ARG
declare the instance ahead of other parameters
Definition pico9918.h:71
pico9918_register_t
the eight TMS9918 registers, by number and by what each one holds
Definition pico9918.h:198
#define PICO9918_INST_ONLY_ARG
declare the instance as the only parameter
Definition pico9918.h:72
#define PICO9918_INST
pass the instance ahead of other arguments
Definition pico9918.h:73
#define PICO9918_DLLEXPORT
the linkage every public entry point carries - see LINKAGE MODES above
Definition pico9918.h:41
static void pico9918_write_string(pico9918_t *tms9918, const char *str)
write a string to VRAM, its terminator excluded
static void pico9918_set_address_write(pico9918_t *tms9918, uint16_t addr)
point the VRAM address register at addr for writing, ie.
static void pico9918_write_string_offset(pico9918_t *tms9918, const char *str, uint8_t offset)
write a string to VRAM with offset added to each character's pattern index
static void pico9918_set_fg_bg_color(pico9918_t *tms9918, pico9918_color_t fg, pico9918_color_t bg)
set register 7, the text-mode foreground colour and the backdrop
static void pico9918_set_color_table_addr(pico9918_t *tms9918, uint16_t addr)
set the colour table address, which register 3 holds in 64-byte units
static void pico9918_set_address_read(pico9918_t *tms9918, uint16_t addr)
point the VRAM address register at addr for reading
static void pico9918_set_sprite_patt_table_addr(pico9918_t *tms9918, uint16_t addr)
set the sprite pattern table address, which register 6 holds in 2KB units
static void pico9918_set_name_table_addr(pico9918_t *tms9918, uint16_t addr)
set the name table address, which register 2 holds in 1KB units
static void pico9918_write_bytes(pico9918_t *tms9918, const uint8_t *bytes, size_t numBytes)
write a block of bytes to VRAM from the current address
static void pico9918_set_sprite_attr_table_addr(pico9918_t *tms9918, uint16_t addr)
set the sprite attribute table address, which register 5 holds in 128-byte units
static void pico9918_write_byte_rpt(pico9918_t *tms9918, uint8_t byte, size_t rpt)
write byte to VRAM rpt times from the current address
static void pico9918_write_register_value(pico9918_t *tms9918, pico9918_register_t reg, uint8_t value)
write a VDP register as a host would, value byte first
void pico9918_initialise_gfx_ii(pico9918_t *tms9918)
set up Graphics II with the default table addresses and a cleared VRAM
uint32_t pico9918_palette[]
the sixteen TMS9918 colours, each packed as 0xrrggbbaa
static uint8_t pico9918_fg_bg_color(pico9918_color_t fg, pico9918_color_t bg)
pack fg into the high nibble and bg into the low nibble of one colour byte
void pico9918_initialise_gfx_i(pico9918_t *tms9918)
set up Graphics I with the default table addresses and a cleared VRAM
static void pico9918_set_pattern_table_addr(pico9918_t *tms9918, uint16_t addr)
set the pattern table address, which register 4 holds in 2KB units