-
Notifications
You must be signed in to change notification settings - Fork 1
/
PpuOutput.cs
472 lines (415 loc) · 16.5 KB
/
PpuOutput.cs
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Emu6502
{
public partial class PpuOutput : Form
{
public static int[] Palette = new int[]
{
// 0x00
0x757575,
0x271b8f,
0x0000ab,
0x47009f,
0x8f0077,
0xab0013,
0xa70000,
0x7f0b00,
0x432f00,
0x004700,
0x004100,
0x003f17,
0x1b3f5f,
0x000000,
0x000000,
0x000000,
// 0x10
0xbcbcbc,
0x0073ef,
0x233bef,
0x8300f3,
0xbf00bf,
0xe7005b,
0xdb2b00,
0xcb4f0f,
0x8b7300,
0x009700,
0x00ab00,
0x00933b,
0x00838b,
0x000000,
0x000000,
0x000000,
// 0x20
0xffffff,
0x3fbfff,
0x5f97ff,
0xa78bfd,
0xf77bff,
0xff77b7,
0xff7763,
0xff9b3b,
0xf3bf3f,
0x83d313,
0x4fdf4b,
0x58f898,
0x00ebdb,
0x000000,
0x000000,
0x000000,
// 0x30
0xffffff,
0xabe7ff,
0xc7d7ff,
0xd7cbff,
0xffc7ff,
0xffc7db,
0xffbfb3,
0xffdbab,
0xffe7a3,
0xe3ffa3,
0xabf3bf,
0xb3ffcf,
0x9ffff3,
0x000000,
0x000000,
0x000000,
};
private Nes nes;
private Ppu ppu;
private Bitmap bmp = new Bitmap(Ppu.ScreenWidth, Ppu.ScreenHeight);
private GraphicsDevice device;
private Texture2D tex;
private SpriteBatch sb;
public PpuOutput(Nes nes)
{
InitializeComponent();
//this.DoubleBuffered = true;
this.ClientSize = new Size(Ppu.ScreenWidth*3, Ppu.ScreenHeight*3);
this.ppu = nes.Ppu;
this.nes = nes;
PresentationParameters pp = new PresentationParameters();
pp.AutoDepthStencilFormat = DepthFormat.Depth16;
pp.BackBufferCount = 1;
pp.BackBufferFormat = SurfaceFormat.Bgr32;
pp.BackBufferWidth = Ppu.ScreenWidth;
pp.BackBufferHeight = Ppu.ScreenHeight;
pp.DeviceWindowHandle = this.Handle;
pp.EnableAutoDepthStencil = true;
pp.IsFullScreen = false;
pp.MultiSampleType = MultiSampleType.None;
pp.PresentationInterval = PresentInterval.Default; // TODO
pp.RenderTargetUsage = RenderTargetUsage.PreserveContents;
pp.SwapEffect = SwapEffect.Discard;
pp.PresentationInterval = PresentInterval.One;
device = new GraphicsDevice(GraphicsAdapter.DefaultAdapter,
DeviceType.Hardware,
this.Handle,
pp);
tex = new Texture2D(device, Ppu.ScreenWidth, Ppu.ScreenHeight,
1, TextureUsage.None, SurfaceFormat.Bgr32);
sb = new SpriteBatch(device);
}
private Bitmap GetPattern(int addr, int paletteAddr)
{
Bitmap bmp = new Bitmap(8, 8);
for (int y = 0; y < 8; ++y)
for (int x = 0; x < 8; ++x)
{
int addr1 = addr + y;
int addr2 = addr + y + 8;
byte b1 = ppu.PatternTables[Ppu.PatternHigh(addr1)][Ppu.PatternLow(addr1)];
byte b2 = ppu.PatternTables[Ppu.PatternHigh(addr2)][Ppu.PatternLow(addr2)];
byte bit1 = (byte)((b1 >> (7 - x)) & 0x1);
byte bit2 = (byte)((b2 >> (7 - x)) & 0x1);
byte result = (byte)(bit2 << 1 | bit1);
/*byte intensity;
if (result == 3)
intensity = 255;
else if (result == 2)
intensity = 160;
else if (result == 1)
intensity = 80;
else
intensity = 0;
if (result == 0)
bmp.SetPixel(x, y, Color.FromArgb(0,0,0,0));
else
bmp.SetPixel(x, y, Color.FromArgb(intensity, intensity, intensity));*/
if (result == 0)
bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(0, 0, 0, 0));
else
{
int paletteEntry = ppu.Read(paletteAddr + result)%64; // todo: bits
bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(0xFF << 24 | Palette[paletteEntry]));
}
}
return bmp;
}
private void DrawNameTable(Graphics g, int addr, int tx, int ty)
{
for(int y=0; y<30; ++y)
for (int x = 0; x < 32; ++x)
{
byte b = ppu.Read(addr++);;
Bitmap chunk = GetPattern(0x1000 + b*16, 0x3F00);
g.DrawImage(chunk, tx + x*8, ty + y*8);
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//base.OnPaintBackground(e);
}
/*
protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.KeyCode & Keys.KeyCode)
{
case Keys.Z:
nes.Controller1.A = true;
break;
case Keys.X:
nes.Controller1.B = true;
break;
case Keys.Return:
nes.Controller1.Select = true;
break;
case Keys.Space:
nes.Controller1.Start = true;
break;
case Keys.Left:
nes.Controller1.Left = true;
break;
case Keys.Right:
nes.Controller1.Right = true;
break;
case Keys.Up:
nes.Controller1.Up = true;
break;
case Keys.Down:
nes.Controller1.Down = true;
break;
}
base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyEventArgs e)
{
switch (e.KeyCode & Keys.KeyCode)
{
case Keys.Z:
nes.Controller1.A = false;
break;
case Keys.X:
nes.Controller1.B = false;
break;
case Keys.Return:
nes.Controller1.Select = false;
break;
case Keys.Space:
nes.Controller1.Start = false;
break;
case Keys.Left:
nes.Controller1.Left = false;
break;
case Keys.Right:
nes.Controller1.Right = false;
break;
case Keys.Up:
nes.Controller1.Up = false;
break;
case Keys.Down:
nes.Controller1.Down = false;
break;
}
base.OnKeyUp(e);
}
* */
private bool toggle = false;
private unsafe void PpuOutput_Paint(object sender, PaintEventArgs e)
{
//device.Clear(Microsoft.Xna.Framework.Graphics.Color.Blue);
/*int numPixels = Ppu.ScreenWidth * Ppu.ScreenHeight;
byte[] buf = new byte[numPixels * 4];
int j = 0;
for (int i = 0; i < numPixels; ++i)
{
buf[j++] = (byte)(ppu.Framebuffer[i] & 0xFF);
buf[j++] = (byte)((ppu.Framebuffer[i] >> 8) & 0xFF);
buf[j++] = (byte)((ppu.Framebuffer[i] >> 16) & 0xFF);
buf[j++] = 0;
}*/
device.SamplerStates[0].MagFilter = TextureFilter.Linear;
device.SamplerStates[0].MinFilter = TextureFilter.Linear;
device.Textures[0] = null;
tex.SetData(ppu.Framebuffer);
/*toggle = !toggle;
if (toggle)
{*/
sb.Begin();
sb.Draw(tex, new Microsoft.Xna.Framework.Rectangle(0, 0, 256, 240),
Microsoft.Xna.Framework.Graphics.Color.White);
sb.End();
/*}
else
device.Clear(Microsoft.Xna.Framework.Graphics.Color.Blue);*/
device.Present();
//String hud = String.Format("FPS {0:0.0}", nes.FPS);//\n{1}", nes.FPS, ppu.Mirroring);
//e.Graphics.DrawString(hud, new Font("Courier New", 8), new SolidBrush(System.Drawing.Color.White), 0, 0);
}
private unsafe void PpuOutput_Paint_Old(object sender, PaintEventArgs e)
{
// TODO: Now we have to actually make this fancy so we can see what the shit is going on.
/*int baseNametableAddr = 0;
if ((ppu.PpuCtrl & 0x3) == 0)
baseNametableAddr = 0x2000;
else if ((ppu.PpuCtrl & 0x3) == 1)
baseNametableAddr = 0x2400;
else if ((ppu.PpuCtrl & 0x3) == 2)
baseNametableAddr = 0x2800;
else // 3
baseNametableAddr = 0x2C00;*/
/*int spritePatternTableAddr = 0x0000;
if ((ppu.PpuCtrl & 0x08) != 0)
spritePatternTableAddr = 0x1000;
bool eightBySixteen = false;
if ((ppu.PpuCtrl & 0x20) != 0)
eightBySixteen = true;
bool enableBG = (ppu.PpuMask & 0x08) != 0;
bool enableSprites = (ppu.PpuMask & 0x10) != 0;*/
Graphics g = e.Graphics;
//g.SetClip(new Rectangle(0, 0, 256, 262)); // NTSC clipping
//g.Clear(Color.Blue);
/*Bitmap bmp = new Bitmap(Ppu.ScreenWidth, Ppu.ScreenHeight);
for(int y = 0; y < Ppu.ScreenHeight; ++y)
for(int x=0; x<Ppu.ScreenWidth; ++x)
bmp.SetPixel(x,y,Color.FromArgb(ppu.Framebuffer[x + y*Ppu.ScreenWidth]));
g.DrawImage(bmp, 0, 0);*/
BitmapData data =
bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Assume we have zero stride (not exactly safe?)
Marshal.Copy(ppu.Framebuffer, 0, data.Scan0, bmp.Width * bmp.Height);
bmp.UnlockBits(data);
g.DrawImageUnscaledAndClipped(bmp, new System.Drawing.Rectangle(0, 0, ClientSize.Width, ClientSize.Height));
String hud = String.Format("FPS {0:0.0}", nes.FPS);//\n{1}", nes.FPS, ppu.Mirroring);
g.DrawString(hud, new Font("Courier New", 8), new SolidBrush(System.Drawing.Color.White), 0, 0);
#if false
String hud = String.Format("Base Nametable Addr: ${0:X4}\n"+
"8x8 Sprite Pat Table Addr: ${1:X4}\n"+
"Sprite Size: {2}\n"+
"Enable BG: {3}\n"+
"Enable Sprites: {4}\n"+
"BG Scroll X: {5}\n"+
"BG Scroll Y: {6}\n\n",
baseNametableAddr,
spritePatternTableAddr,
eightBySixteen ? "8x16":"8x8",
enableBG,
enableSprites,
ppu.ScrollX,
ppu.ScrollY);
/*for (int i = 0; i < 64; ++i)
{
byte B0 = ppu.SpriteMem[i * 4 + 0];
byte B1 = ppu.SpriteMem[i * 4 + 1];
byte B2 = ppu.SpriteMem[i * 4 + 2];
byte B3 = ppu.SpriteMem[i * 4 + 3];
// int x = B3;
//int y = B0;
//Bitmap chunk = GetPattern((HighBank8x8 ? 0x1000 : 0x0000) + (B1 * 16));
//g.DrawImage(chunk, x, y);
hud += String.Format("Spr{0:00} ${1:X2} ${2:X2} ({3},{4})\n", i, B1, B2, B3, B0);
}*/
/*
Graphics g = e.Graphics;
g.Clear(Color.Black);
DrawNameTable(g, 0x2000, 0, 0);
bool EightBySixteen = (ppu.PpuCtrl & 0x20) != 0;
bool HighBank8x8 = (ppu.PpuCtrl & 0x08) != 0;
*/
#endif
/*DrawNameTable(g, baseNametableAddr, -ppu.ScrollX, -ppu.ScrollY);
DrawNameTable(g, baseNametableAddr == 0x2000 ? 0x2400 : 0x2000, -ppu.ScrollX + 256, -ppu.ScrollY);*/
/*for (int i = 0; i < 64; ++i)
{
byte B0 = ppu.SpriteMem[i * 4 + 0];
byte B1 = ppu.SpriteMem[i * 4 + 1];
byte B2 = ppu.SpriteMem[i * 4 + 2];
byte B3 = ppu.SpriteMem[i * 4 + 3];
int x = B3;
int y = B0;
if (y >= 0xEF)
continue;
int palette = (B2 & 0x3);
bool flipH = (B2 & 0x40) != 0;
bool flipV = (B2 & 0x80) != 0;
Bitmap chunk = GetPattern(spritePatternTableAddr + B1 * 16, 0x3F10 + palette*4);
if (flipH)
chunk.RotateFlip(RotateFlipType.RotateNoneFlipX);
if (flipV)
chunk.RotateFlip(RotateFlipType.RotateNoneFlipY);
g.DrawImage(chunk, x, y);
}*/
/*int tx = 0;
int ty = 0;
for (int i = 0; i < 32; ++i)
{
byte palIdx = ppu.Palette[i];//;.Read(0x3f00 + i);
Color c = Color.FromArgb((int)(0xFF000000 | Palette[palIdx]));
Rectangle r = new Rectangle(tx * 16, ty * 16, 16, 16);
g.FillRectangle(new SolidBrush(c), r);
tx++;
if (tx >= 4)
{
tx = 0;
ty++;
}
}*/
/*int x = 0, y = 0;
for (int addr=0; addr < 0x2000; addr += 16)
{
Bitmap chunk = GetPattern(addr);
g.DrawImage(chunk, x * 8, y * 8);
x++;
if (x > 16)
{
++y;
x = 0;
}
}*/
/*for (int i = 0; i < 64; ++i)
{
byte B0 = ppu.SpriteMem[i * 4 + 0];
byte B1 = ppu.SpriteMem[i * 4 + 1];
byte B2 = ppu.SpriteMem[i * 4 + 2];
byte B3 = ppu.SpriteMem[i * 4 + 3];
// int x = B3;
//int y = B0;
//Bitmap chunk = GetPattern((HighBank8x8 ? 0x1000 : 0x0000) + (B1 * 16));
//g.DrawImage(chunk, x, y);
//hud += String.Format("Spr{0:00} ${1:X2} ${2:X2} ({3},{4})\n", i, B1, B2, B3, B0);
int x = B3;
int y = B0 + 1;
Color color = Color.FromArgb(0,255,0);
if(i == 0 && toggle)
color = Color.Black;
g.DrawRectangle(new Pen(color, 1), new Rectangle(x, y, 8, (ppu.PpuCtrl & 0x20) != 0 ? 16 : 8));
}*/
toggle = !toggle;
//g.DrawString(hud, new Font("Courier New", 8), new SolidBrush(Color.White), 0, 0);
//#endif
return;
}
}
}