Subversion Repositories Shiroi

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 nishi 1
/*
2
 * Troy's TMS9918 Emulator - Utility / helper functions
3
 *
4
 * Copyright (c) 2022 Troy Schrapel
5
 *
6
 * This code is licensed under the MIT license
7
 *
8
 * https://github.com/visrealm/vrEmuTms9918
9
 *
10
 */
11
 
12
#ifndef _VR_EMU_TMS9918_UTIL_H_
13
#define _VR_EMU_TMS9918_UTIL_H_
14
 
15
#include "tms9918.h"
16
 
17
#include <stddef.h>
18
#include <string.h>
19
 
20
#define TMS_R0_MODE_GRAPHICS_I 0x00
21
#define TMS_R0_MODE_GRAPHICS_II 0x02
22
#define TMS_R0_MODE_MULTICOLOR 0x00
23
#define TMS_R0_MODE_TEXT 0x00
24
#define TMS_R0_EXT_VDP_ENABLE 0x01
25
#define TMS_R0_EXT_VDP_DISABLE 0x00
26
 
27
#define TMS_R1_RAM_16K 0x80
28
#define TMS_R1_RAM_4K 0x00
29
#define TMS_R1_DISP_BLANK 0x00
30
#define TMS_R1_DISP_ACTIVE 0x40
31
#define TMS_R1_INT_ENABLE 0x20
32
#define TMS_R1_INT_DISABLE 0x00
33
#define TMS_R1_MODE_GRAPHICS_I 0x00
34
#define TMS_R1_MODE_GRAPHICS_II 0x00
35
#define TMS_R1_MODE_MULTICOLOR 0x08
36
#define TMS_R1_MODE_TEXT 0x10
37
#define TMS_R1_SPRITE_8 0x00
38
#define TMS_R1_SPRITE_16 0x02
39
#define TMS_R1_SPRITE_MAG1 0x00
40
#define TMS_R1_SPRITE_MAG2 0x01
41
 
42
#define TMS_DEFAULT_VRAM_NAME_ADDRESS 0x3800
43
#define TMS_DEFAULT_VRAM_COLOR_ADDRESS 0x0000
44
#define TMS_DEFAULT_VRAM_PATT_ADDRESS 0x2000
45
#define TMS_DEFAULT_VRAM_SPRITE_ATTR_ADDRESS 0x3B00
46
#define TMS_DEFAULT_VRAM_SPRITE_PATT_ADDRESS 0x1800
47
 
48
/*
49
 * TMS9918 palette (RGBA)
50
 */
51
extern uint32_t vrEmuTms9918Palette[];
52
 
53
/*
54
 * Write a register value
55
 */
56
inline static void vrEmuTms9918WriteRegisterValue(VrEmuTms9918* tms9918, vrEmuTms9918Register reg, uint8_t value) {
57
	vrEmuTms9918WriteAddr(tms9918, value);
58
	vrEmuTms9918WriteAddr(tms9918, 0x80 | (uint8_t)reg);
59
}
60
 
61
/*
62
 * Set current VRAM address for reading
63
 */
64
inline static void vrEmuTms9918SetAddressRead(VrEmuTms9918* tms9918, uint16_t addr) {
65
	vrEmuTms9918WriteAddr(tms9918, addr & 0x00ff);
66
	vrEmuTms9918WriteAddr(tms9918, ((addr & 0xff00) >> 8));
67
}
68
 
69
/*
70
 * Set current VRAM address for writing
71
 */
72
inline static void vrEmuTms9918SetAddressWrite(VrEmuTms9918* tms9918, uint16_t addr) { vrEmuTms9918SetAddressRead(tms9918, addr | 0x4000); }
73
 
74
/*
75
 * Write a series of bytes to the VRAM
76
 */
77
inline static void vrEmuTms9918WriteBytes(VrEmuTms9918* tms9918, const uint8_t* bytes, size_t numBytes) {
78
	for(size_t i = 0; i < numBytes; ++i) {
79
		vrEmuTms9918WriteData(tms9918, bytes[i]);
80
	}
81
}
82
 
83
/*
84
 * Write a series of bytes to the VRAM
85
 */
86
inline static void vrEmuTms9918WriteByteRpt(VrEmuTms9918* tms9918, uint8_t byte, size_t rpt) {
87
	for(size_t i = 0; i < rpt; ++i) {
88
		vrEmuTms9918WriteData(tms9918, byte);
89
	}
90
}
91
 
92
/*
93
 * Write a series of chars to the VRAM
94
 */
95
inline static void vrEmuTms9918WriteString(VrEmuTms9918* tms9918, const char* str) {
96
	size_t len = strlen(str);
97
	for(size_t i = 0; i < len; ++i) {
98
		vrEmuTms9918WriteData(tms9918, str[i]);
99
	}
100
}
101
 
102
/*
103
 * Write a series of chars to the VRAM with offset
104
 */
105
inline static void vrEmuTms9918WriteStringOffset(VrEmuTms9918* tms9918, const char* str, uint8_t offset) {
106
	size_t len = strlen(str);
107
	for(size_t i = 0; i < len; ++i) {
108
		vrEmuTms9918WriteData(tms9918, str[i] + offset);
109
	}
110
}
111
 
112
/*
113
 * Return a colur byte consisting of foreground and background colors
114
 */
115
inline static uint8_t vrEmuTms9918FgBgColor(vrEmuTms9918Color fg, vrEmuTms9918Color bg) { return (uint8_t)((uint8_t)fg << 4) | (uint8_t)bg; }
116
 
117
/*
118
 * Set name table address
119
 */
120
inline static void vrEmuTms9918SetNameTableAddr(VrEmuTms9918* tms9918, uint16_t addr) { vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_NAME_TABLE, addr >> 10); }
121
 
122
/*
123
 * Set color table address
124
 */
125
inline static void vrEmuTms9918SetColorTableAddr(VrEmuTms9918* tms9918, uint16_t addr) { vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_COLOR_TABLE, (uint8_t)(addr >> 6)); }
126
 
127
/*
128
 * Set pattern table address
129
 */
130
inline static void vrEmuTms9918SetPatternTableAddr(VrEmuTms9918* tms9918, uint16_t addr) { vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_PATTERN_TABLE, addr >> 11); }
131
 
132
/*
133
 * Set sprite attribute table address
134
 */
135
inline static void vrEmuTms9918SetSpriteAttrTableAddr(VrEmuTms9918* tms9918, uint16_t addr) { vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_SPRITE_ATTR_TABLE, (uint8_t)(addr >> 7)); }
136
 
137
/*
138
 * Set sprite pattern table address
139
 */
140
inline static void vrEmuTms9918SetSpritePattTableAddr(VrEmuTms9918* tms9918, uint16_t addr) { vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_SPRITE_PATT_TABLE, addr >> 11); }
141
 
142
/*
143
 * Set foreground (text mode) and background colors
144
 */
145
inline static void vrEmuTms9918SetFgBgColor(VrEmuTms9918* tms9918, vrEmuTms9918Color fg, vrEmuTms9918Color bg) { vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_FG_BG_COLOR, vrEmuTms9918FgBgColor(fg, bg)); }
146
 
147
/*
148
 * Initialise for Graphics I mode
149
 */
150
VR_EMU_TMS9918_DLLEXPORT
151
void vrEmuTms9918InitialiseGfxI(VrEmuTms9918* tms9918);
152
 
153
/*
154
 * Initialise for Graphics II mode
155
 */
156
VR_EMU_TMS9918_DLLEXPORT
157
void vrEmuTms9918InitialiseGfxII(VrEmuTms9918* tms9918);
158
 
159
#endif // _VR_EMU_TMS9918_UTIL_H_