-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
157 lines (125 loc) · 5.23 KB
/
Program.cs
File metadata and controls
157 lines (125 loc) · 5.23 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
using SDL2;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace Chip8
{
class Program
{
static void Main(string[] args)
{
if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) < 0)
{
Console.WriteLine("SDL failed to init.");
return;
}
IntPtr window = SDL.SDL_CreateWindow("Chip-8 Interpreter", 128, 128, 64 * 8, 32 * 8, 0);
if (window == IntPtr.Zero)
{
Console.WriteLine("SDL could not create a window.");
return;
}
IntPtr renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED);
if (renderer == IntPtr.Zero)
{
Console.WriteLine("SDL could not create a valid renderer.");
return;
}
CPU cpu = new CPU();
using (BinaryReader reader = new BinaryReader(new FileStream("../../sample.ch8", FileMode.Open)))
{
List<byte> program = new List<byte>();
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
program.Add(reader.ReadByte());
}
cpu.LoadProgram(program.ToArray());
}
SDL.SDL_Event sdlEvent;
bool running = true;
int sample = 0;
int beepSamples = 0;
SDL.SDL_AudioSpec audioSpec = new SDL.SDL_AudioSpec();
audioSpec.channels = 1;
audioSpec.freq = 44100;
audioSpec.samples = 256;
audioSpec.format = SDL.AUDIO_S8;
audioSpec.callback = new SDL.SDL_AudioCallback((userdata, stream, length) =>
{
if (cpu == null) return;
sbyte[] waveData = new sbyte[length];
for (int i = 0; i < waveData.Length && cpu.SoundTimer > 0; i++, beepSamples++)
{
if (beepSamples == 730)
{
beepSamples = 0;
cpu.SoundTimer--;
}
waveData[i] = (sbyte)(127 * Math.Sin(sample * Math.PI * 2 * 604.1 / 44100));
sample++;
}
byte[] byteData = (byte[])(Array)waveData;
Marshal.Copy(byteData, 0, stream, byteData.Length);
});
SDL.SDL_OpenAudio(ref audioSpec, IntPtr.Zero);
SDL.SDL_PauseAudio(0);
IntPtr sdlSurface, sdlTexture = IntPtr.Zero;
Stopwatch frameTimer = Stopwatch.StartNew();
int ticksPer60hz = (int)(Stopwatch.Frequency * 0.016);
while (running)
{
try
{
if (!cpu.WaitingForKeyPress) cpu.Step();
if (frameTimer.ElapsedTicks > ticksPer60hz)
{
while (SDL.SDL_PollEvent(out sdlEvent) != 0)
{
if (sdlEvent.type == SDL.SDL_EventType.SDL_QUIT)
{
running = false;
}
else if (sdlEvent.type == SDL.SDL_EventType.SDL_KEYDOWN)
{
var key = KeyCodeToKey((int)sdlEvent.key.keysym.sym);
cpu.Keyboard |= (ushort)(1 << key);
if (cpu.WaitingForKeyPress) cpu.KeyPressed((byte)key);
}
else if (sdlEvent.type == SDL.SDL_EventType.SDL_KEYUP)
{
var key = KeyCodeToKey((int)sdlEvent.key.keysym.sym);
cpu.Keyboard &= (ushort)~(1 << key);
}
}
var displayHandle = GCHandle.Alloc(cpu.Display, GCHandleType.Pinned);
if (sdlTexture != IntPtr.Zero) SDL.SDL_DestroyTexture(sdlTexture);
sdlSurface = SDL.SDL_CreateRGBSurfaceFrom(displayHandle.AddrOfPinnedObject(), 64, 32, 32, 64 * 4, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
sdlTexture = SDL.SDL_CreateTextureFromSurface(renderer, sdlSurface);
displayHandle.Free();
SDL.SDL_RenderClear(renderer);
SDL.SDL_RenderCopy(renderer, sdlTexture, IntPtr.Zero, IntPtr.Zero);
SDL.SDL_RenderPresent(renderer);
frameTimer.Restart();
}
Thread.Sleep(1);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
SDL.SDL_DestroyRenderer(renderer);
SDL.SDL_DestroyWindow(window);
}
private static int KeyCodeToKey(int keycode)
{
int keyIndex = 0;
if (keycode < 58) keyIndex = keycode - 48;
else keyIndex = keycode - 87;
return keyIndex;
}
}
}