-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNtsc.cpp
More file actions
339 lines (318 loc) · 11.2 KB
/
Ntsc.cpp
File metadata and controls
339 lines (318 loc) · 11.2 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <memory.h>
#include <pico/stdlib.h>
#include <hardware/clocks.h>
#include <hardware/pwm.h>
#include <hardware/dma.h>
#include <tusb.h>
#include "Ntsc.h"
#include "Color.h"
#include "Sound.h"
#include "Timer.h"
static constexpr auto XResolution = TileWidth * VramWidth;
static constexpr auto YResolution = TileHeight * VramHeight;
static constexpr auto DmaChannelCount = 2;
static constexpr auto SamplesPerRaster = 908; // 227 * 4
static constexpr auto PwmCycle = Config::SystemClock * 1000 / 14318181;
static constexpr auto RasterCount = 262;
static constexpr auto VSyncRasterCount = 10;
static constexpr auto BlankingRasterCount = 24 + (26 - VramHeight) * TileHeight;
static constexpr auto HSyncLength = 68; // Approximately 4.7μsec
static constexpr auto HStartPosition = HSyncLength + 120;
static Color colors[ColorCount];
static int dmaChannels[DmaChannelCount];
static uint16_t dmaBuffer[DmaChannelCount][SamplesPerRaster] __attribute__ ((aligned (4)));
static volatile uint16_t currentRaster;
static volatile int currentY;
static volatile uint8_t* pTileRow;
static volatile int yMod;
uint8_t Vram[VramWidth * VramHeight * PageCount];
uint8_t* pVramTop = Vram;
uint8_t TilePattern[TilePatternSize * 256];
uint8_t SpritePattern[SpritePatternSize * 64];
SpriteAttribute SpriteAttributes[MaxSpriteCount];
static void MakeDmaBuffer(uint16_t* pBuffer, uint16_t raster)
{
auto p = pBuffer;
if (raster < 2) {
// VSync
for (auto i = 0; i < SamplesPerRaster - HSyncLength; ++i) {
*p++ = 0;
}
while (p < pBuffer + SamplesPerRaster) {
*p++ = 2;
}
}
else if(raster == VSyncRasterCount || raster == VSyncRasterCount + 1) {
// HSync & Color Burst
for (auto i = 0; i < HSyncLength; ++i) {
*p++ = 0;
}
for (auto i = 0; i < 8; ++i) {
*p++ = 2;
}
for (auto i = 0; i < 9; ++i) {
*p++ = 2;
*p++ = 1;
*p++ = 2;
*p++ = 3;
}
while (p < pBuffer + SamplesPerRaster) {
*p++ = 2;
}
}
else if (
raster >= VSyncRasterCount + BlankingRasterCount &&
raster < VSyncRasterCount + BlankingRasterCount + YResolution
) {
static uint8_t lineBuffer[XResolution];
if (raster == VSyncRasterCount + BlankingRasterCount) {
currentY = 0;
pTileRow = pVramTop;
yMod = 0;
}
{
auto pLine = lineBuffer;
auto pTile = pTileRow;
for (auto tileX = 0; tileX < VramWidth; ++tileX) {
auto pPattern = TilePattern + (static_cast<uint16_t>(*pTile++) << 4);
pPattern += ((currentY & 7) << 1);
auto b = *pPattern++;
*pLine++ = b >> 4;
*pLine++ = b & 0x0f;
b = *pPattern++;
*pLine++ = b >> 4;
*pLine++ = b & 0x0f;
}
}
{
auto horizontalCount = 0;
auto pSprite = SpriteAttributes + MaxSpriteCount;
for (auto i = 0; i < MaxSpriteCount; ++i) {
--pSprite;
uint8_t yOffset = currentY - pSprite->y;
if (yOffset < SpriteHeight) {
uint8_t x = pSprite->x;
auto pPattern = SpritePattern +
(static_cast<uint16_t>(pSprite->pattern) << 6);
pPattern += yOffset << 2;
for (auto j = 0; j < SpriteWidth / 2; ++j) {
auto b = *pPattern; //0x44; //
if (x < XResolution) {
auto dot = b >> 4;
if (dot != 0) {
lineBuffer[x] = dot;
}
}
++x;
if (x < XResolution) {
auto dot = b & 0x0f;
if (dot != 0) {
lineBuffer[x] = dot;
}
}
++x;
++pPattern;
}
if (++horizontalCount >= MaxHorizontalSpriteCount) {
break;
}
}
}
}
{
auto pLine = lineBuffer;
p += HStartPosition;
for (auto i = 0; i < XResolution; ++i) {
const auto& color = colors[*pLine++];
*p++ = color.Values()[0];
*p++ = color.Values()[1];
*p++ = color.Values()[2];
*p++ = color.Values()[3];
}
}
++currentY;
if (++yMod >= TileHeight) {
pTileRow += VramWidth;
yMod = 0;
}
}
else if (
raster == VSyncRasterCount + BlankingRasterCount + YResolution ||
raster == VSyncRasterCount + BlankingRasterCount + YResolution + 1
) {
p += HStartPosition;
for (auto i = 0; i < XResolution * 4; ++i) {
*p++ = 2;
}
}
}
inline uint32_t InterruptBit(int channel)
{
return 1u << channel;
}
static void Handler()
{
volatile auto status = dma_hw->ints0;
dma_hw->ints0 = status; // Clear IRQ
for (auto i = 0; i < DmaChannelCount; ++i) {
if ((status & InterruptBit(dmaChannels[i])) != 0) {
MakeDmaBuffer(dmaBuffer[i], currentRaster);
dma_channel_set_read_addr(dmaChannels[i], dmaBuffer[i], false);
}
}
if (++currentRaster == RasterCount) {
currentRaster = 0;
++TimerCount;
SoundHandler();
}
}
static void InitializePwmDma()
{
gpio_set_function(Config::Gpio::Video, GPIO_FUNC_PWM);
auto pwmSlice = pwm_gpio_to_slice_num(Config::Gpio::Video);
auto pwmConfig = pwm_get_default_config();
pwm_config_set_clkdiv(&pwmConfig, 1);
pwm_init(pwmSlice, &pwmConfig, true);
pwm_set_wrap(pwmSlice, PwmCycle - 1);
auto pOut = reinterpret_cast<io_rw_16 *>(&pwm_hw->slice[pwmSlice].cc) + 1;
for (auto i = 0; i < DmaChannelCount; ++i)
{
dmaChannels[i] = dma_claim_unused_channel(true);
}
uint32_t mask = 0;
for (auto i = 0; i < DmaChannelCount; ++i)
{
auto dmaChannelConfig = dma_channel_get_default_config(dmaChannels[i]);
channel_config_set_transfer_data_size(&dmaChannelConfig, DMA_SIZE_16);
channel_config_set_read_increment(&dmaChannelConfig, true);
channel_config_set_write_increment(&dmaChannelConfig, false);
channel_config_set_dreq(&dmaChannelConfig, DREQ_PWM_WRAP0 + pwmSlice);
auto nextChannel = dmaChannels[(i + 1) % DmaChannelCount];
channel_config_set_chain_to(&dmaChannelConfig, nextChannel);
dma_channel_configure(
dmaChannels[i],
&dmaChannelConfig,
pOut,
dmaBuffer[i],
SamplesPerRaster,
false);
memset(dmaBuffer[i], SamplesPerRaster * sizeof(uint16_t), 0);
MakeDmaBuffer(dmaBuffer[i], i);
mask |= InterruptBit(dmaChannels[i]);
}
dma_set_irq0_channel_mask_enabled(mask ,true);
irq_set_exclusive_handler(DMA_IRQ_0, Handler);
irq_set_enabled(DMA_IRQ_0, true);
currentRaster = 0;
dma_channel_start(dmaChannels[0]);
}
void InitNtsc()
{
for (auto& sprite : SpriteAttributes) {
sprite.y = YResolution;
}
set_sys_clock_khz(Config::SystemClock, true);
InitializePwmDma();
}
void InitializeColors(const uint8_t* pPaletteValues)
{
auto pSource = pPaletteValues;
for (auto i = 0; i < ColorCount; ++i) {
auto r = *pSource++;
auto g = *pSource++;
auto b = *pSource++;
colors[i].SetRgb(r, g, b);
}
}
uint8_t MakeTileMono(uint8_t c, const uint8_t* pSource, uint8_t count, uint8_t color)
{
auto foregroundColor = color & 0x0f;
auto backgroundColor = (color >> 4) & 0x0f;
auto pDestination = TilePattern + TilePatternSize * c;
while (count-- > 0) {
for (auto y0 = 0; y0 < TileHeight; y0 += 2) {
auto source = *pSource++;
byte bit = 0x80;
for (auto y1 = 0; y1 < 2; ++y1) {
for (auto x0 = 0; x0 < TileWidth; x0 += 2) {
byte destination = 0;
for (auto x1 = 0; x1 < 2; ++x1) {
destination <<=4;
if ((source & bit) != 0) {
destination |= foregroundColor;
}
else {
destination |= backgroundColor;
}
bit >>= 1;
}
*pDestination++ = destination;
}
}
}
++c;
}
return c;
}
uint8_t MakeTileMono(uint8_t c, const byte* pSource, const ColorElement* pColorElements)
{
while (true)
{
if (pColorElements->count == 0) break;
c = MakeTileMono(c, pSource, pColorElements->count, pColorElements->color);
pSource += TileHeight / 2 * pColorElements->count;
++pColorElements;
}
return c;
}
uint8_t MakeTileColor(uint8_t c, const byte* pSource, uint8_t count)
{
memcpy(TilePattern + TilePatternSize * c, pSource, TilePatternSize * count);
return c + count;
}
const uint8_t AsciiPattern[] = {
// ascii
0x00, 0x00, 0x00, 0x00, 0x44, 0x44, 0x04, 0x40,
0xaa, 0x00, 0x00, 0x00, 0x44, 0xe4, 0xe4, 0x40,
0x4e, 0xce, 0x6e, 0x40, 0x8a, 0x24, 0x8a, 0x20,
0x4a, 0x48, 0xaa, 0x40, 0x44, 0x40, 0x00, 0x00,
0x48, 0x88, 0x88, 0x40, 0x42, 0x22, 0x22, 0x40,
0x0a, 0xa4, 0xaa, 0x00, 0x04, 0x4e, 0x44, 0x00,
0x00, 0x00, 0x44, 0x80, 0x00, 0x0e, 0x00, 0x00,
0x00, 0x00, 0x04, 0x40, 0x02, 0x24, 0x88, 0x00,
0x4a, 0xaa, 0xaa, 0x40, 0x22, 0x22, 0x22, 0x20,
0x4a, 0x22, 0x48, 0xe0, 0x4a, 0x24, 0x2a, 0x40,
0x2a, 0xae, 0x22, 0x20, 0xe8, 0x8c, 0x22, 0xc0,
0x4a, 0x8c, 0xaa, 0x40, 0xea, 0x24, 0x44, 0x40,
0x4a, 0xa4, 0xaa, 0x40, 0x4a, 0xa6, 0x2a, 0x40,
0x04, 0x40, 0x44, 0x00, 0x04, 0x40, 0x44, 0x80,
0x02, 0x48, 0x42, 0x00, 0x00, 0xe0, 0xe0, 0x00,
0x08, 0x42, 0x48, 0x00, 0x4a, 0x24, 0x40, 0x40,
0x4a, 0xaa, 0x88, 0x60, 0x4a, 0xaa, 0xea, 0xa0,
0xca, 0xac, 0xaa, 0xc0, 0x4a, 0x88, 0x8a, 0x40,
0xca, 0xaa, 0xaa, 0xc0, 0xe8, 0x8c, 0x88, 0xe0,
0xe8, 0x8c, 0x88, 0x80, 0x68, 0x8a, 0xaa, 0x40,
0xaa, 0xae, 0xaa, 0xa0, 0xe4, 0x44, 0x44, 0xe0,
0x62, 0x22, 0x2a, 0x40, 0xae, 0xc8, 0xce, 0xa0,
0x88, 0x88, 0x88, 0xe0, 0xae, 0xee, 0xaa, 0xa0,
0xca, 0xaa, 0xaa, 0xa0, 0x4a, 0xaa, 0xaa, 0x40,
0xca, 0xaa, 0xc8, 0x80, 0x4a, 0xaa, 0xac, 0x60,
0xca, 0xaa, 0xca, 0xa0, 0x4a, 0x84, 0x2a, 0x40,
0xe4, 0x44, 0x44, 0x40, 0xaa, 0xaa, 0xaa, 0xe0,
0xaa, 0xaa, 0xa4, 0x40, 0xaa, 0xae, 0xee, 0xa0,
0xaa, 0x44, 0x4a, 0xa0, 0xaa, 0xa4, 0x44, 0x40,
0xe2, 0x24, 0x88, 0xe0, 0x64, 0x44, 0x44, 0x60,
0x08, 0x84, 0x22, 0x00, 0xc4, 0x44, 0x44, 0xc0,
0x4a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
};
const uint8_t LogoPattern[] = {
// logo
0x00, 0x00, 0x00, 0x00, 0xcc, 0xcc, 0x00, 0x00,
0x00, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
0x33, 0x33, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0x33, 0x33, 0xcc, 0xcc, 0xff, 0xff, 0xcc, 0xcc,
0x00, 0x00, 0x33, 0x33, 0xcc, 0xcc, 0x33, 0x33,
0x00, 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xff, 0xff,
0x33, 0x33, 0x33, 0x33, 0xff, 0xff, 0x33, 0x33,
0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};