forked from buricco/marduk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tms_util.h
150 lines (131 loc) · 4.51 KB
/
tms_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (c) 2022 Troy Schrapel.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following condition: The
* above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef _VR_EMU_TMS9918_UTIL_H_
#define _VR_EMU_TMS9918_UTIL_H_
#include "tms9918.h"
#include <stddef.h>
#define TMS_R0_MODE_GRAPHICS_I 0x00
#define TMS_R0_MODE_GRAPHICS_II 0x02
#define TMS_R0_MODE_MULTICOLOR 0x00
#define TMS_R0_MODE_TEXT 0x00
#define TMS_R0_EXT_VDP_ENABLE 0x01
#define TMS_R0_EXT_VDP_DISABLE 0x00
#define TMS_R1_RAM_16K 0x80
#define TMS_R1_RAM_4K 0x00
#define TMS_R1_DISP_BLANK 0x00
#define TMS_R1_DISP_ACTIVE 0x40
#define TMS_R1_INT_ENABLE 0x20
#define TMS_R1_INT_DISABLE 0x00
#define TMS_R1_MODE_GRAPHICS_I 0x00
#define TMS_R1_MODE_GRAPHICS_II 0x00
#define TMS_R1_MODE_MULTICOLOR 0x08
#define TMS_R1_MODE_TEXT 0x10
#define TMS_R1_SPRITE_8 0x00
#define TMS_R1_SPRITE_16 0x02
#define TMS_R1_SPRITE_MAG1 0x00
#define TMS_R1_SPRITE_MAG2 0x01
/*
* TMS9918 palette (RGBA)
*/
extern const uint32_t vrEmuTms9918Palette[];
/*
* Write a register value
*/
inline static void vrEmuTms9918WriteRegisterValue(VrEmuTms9918 *tms9918, vrEmuTms9918Register reg, uint8_t value)
{
vrEmuTms9918WriteAddr(tms9918, value);
vrEmuTms9918WriteAddr(tms9918, 0x80 | (uint8_t)reg);
}
/*
* Set current VRAM address for reading
*/
inline static void vrEmuTms9918SetAddressRead(VrEmuTms9918* tms9918, uint16_t addr)
{
vrEmuTms9918WriteAddr(tms9918, addr & 0x00ff);
vrEmuTms9918WriteAddr(tms9918, ((addr & 0xff00) >> 8));
}
/*
* Set current VRAM address for writing
*/
inline static void vrEmuTms9918SetAddressWrite(VrEmuTms9918* tms9918, uint16_t addr)
{
vrEmuTms9918SetAddressRead(tms9918, addr | 0x4000);
}
/*
* Write a series of bytes to the VRAM
*/
inline static void vrEmuTms9918WriteBytes(VrEmuTms9918* tms9918, uint8_t *bytes, size_t numBytes)
{
for (int i = 0; i < numBytes; ++i)
{
vrEmuTms9918WriteData(tms9918, bytes[i]);
}
}
/*
* Return a colur byte consisting of foreground and background colors
*/
inline static uint8_t vrEmuTms9918FgBgColor(vrEmuTms9918Color fg, vrEmuTms9918Color bg)
{
return (uint8_t)((uint8_t)fg << 4) | (uint8_t)bg;
}
/*
* Set name table address
*/
inline static void vrEmuTms9918SetNameTableAddr(VrEmuTms9918* tms9918, uint16_t addr)
{
vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_NAME_TABLE, addr >> 10);
}
/*
* Set color table address
*/
inline static void vrEmuTms9918SetColorTableAddr(VrEmuTms9918* tms9918, uint16_t addr)
{
vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_COLOR_TABLE, (uint8_t)(addr >> 6));
}
/*
* Set pattern table address
*/
inline static void vrEmuTms9918SetPatternTableAddr(VrEmuTms9918* tms9918, uint16_t addr)
{
vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_PATTERN_TABLE, addr >> 11);
}
/*
* Set sprite attribute table address
*/
inline static void vrEmuTms9918SetSpriteAttrTableAddr(VrEmuTms9918* tms9918, uint16_t addr)
{
vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_SPRITE_ATTR_TABLE, (uint8_t)(addr >> 7));
}
/*
* Set sprite pattern table address
*/
inline static void vrEmuTms9918SetSpritePattTableAddr(VrEmuTms9918* tms9918, uint16_t addr)
{
vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_SPRITE_PATT_TABLE, addr >> 11);
}
/*
* Set foreground (text mode) and background colors
*/
inline static void vrEmuTms9918SetFgBgColor(VrEmuTms9918* tms9918, vrEmuTms9918Color fg, vrEmuTms9918Color bg)
{
vrEmuTms9918WriteRegisterValue(tms9918, TMS_REG_FG_BG_COLOR, vrEmuTms9918FgBgColor(fg, bg));
}
#endif // _VR_EMU_TMS9918_UTIL_H_