Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
134 lines (111 loc) · 3.44 KB
/
Copy pathProgram.cs
File metadata and controls
134 lines (111 loc) · 3.44 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
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using Chip_8_Emulator.System;
if (args.Length == 0)
{
Console.WriteLine("Usage: Chip8Emulator <rom-file>");
return;
}
// initialize system components
var memory = new Memory();
var display = new Display();
var keyboard = new Keyboard();
var cpu = new CPU(memory, display, keyboard);
byte[] rom = File.ReadAllBytes(args[0]);
memory.LoadRom(rom);
Console.WriteLine($"Loaded ROM: {args[0]} ({rom.Length} bytes)");
Console.CursorVisible = false;
Console.Clear();
// 16 input keys
int[] keymap = new int[16]
{
(int)ConsoleKey.Q,
(int)ConsoleKey.W,
(int)ConsoleKey.E,
(int)ConsoleKey.R,
(int)ConsoleKey.A,
(int)ConsoleKey.S,
(int)ConsoleKey.D,
(int)ConsoleKey.F,
(int)ConsoleKey.Z,
(int)ConsoleKey.X,
(int)ConsoleKey.C,
(int)ConsoleKey.V,
(int)ConsoleKey.D1,
(int)ConsoleKey.D2,
(int)ConsoleKey.D3,
(int)ConsoleKey.D4,
};
const int instructionsPerSecond = 700;
const int timerHz = 60;
// how many milliseconds each tick should take
// one tick = one pass of the Fetch-Decode-Execute cycle
double cpuIntervalMs = 1000.0 / instructionsPerSecond; // ~1.428ms
// this one is for the CPU timers(Delay and Sound)
// we decrement those timers at a rate of 60Hz
double timerIntervalMs = 1000.0 / timerHz; // ~16.66ms
var cpuTimer = Stopwatch.StartNew();
var tickTimer = Stopwatch.StartNew();
// Accumulator for the cpu
// basically keeps 'spare change' of milliseconds
// Every loop, it checks the time elapsed by the cpuTimer
// and adds it to itself. This is the spare change
double cpuAcc = 0;
double timerAcc = 0;
bool wasSoundOn = false;
var sb = new StringBuilder();
while (true)
{
for (byte i = 0; i < 16; i++)
{
// GetAsyncKeyState gives a 16 bit value
// if the most significant bit (15th bit) is 1, the key is pressed
// 0x8000 => 1000 0000 0000 in binary
// which is why it is used as a mask
bool isDown = (GetAsyncKeyState(keymap[i]) & 0x8000) != 0;
keyboard.SetKey(i, isDown);
}
// check if 'Esc' key is pressed
if ((GetAsyncKeyState((int)ConsoleKey.Escape) & 0x8000) != 0)
break;
// Accumulator for the CPU
// this is essentially the 'Delta time' or the time between each frame
cpuAcc += cpuTimer.Elapsed.TotalMilliseconds;
cpuTimer.Restart(); // the 'Delta time' is measured from this line onwards until it loops back to itself
int cycles = 0;
// if we have atleast 'cpuIntervalMs'(1.428ms) amount of milliseconds, we execute a tick
// this allows the cpu to 'catch up'
while (cpuAcc >= cpuIntervalMs && cycles < 20)
{
cpu.Cycle();
cpuAcc -= cpuIntervalMs;
cycles++;
}
timerAcc += tickTimer.Elapsed.TotalMilliseconds;
tickTimer.Restart();
while (timerAcc >= timerIntervalMs)
{
cpu.UpdateTimers();
timerAcc -= timerIntervalMs;
}
bool isSoundOn = cpu.SoundTimer > 0;
if (isSoundOn && !wasSoundOn)
{
Task.Run(() => Console.Beep(440, 200));
}
wasSoundOn = isSoundOn;
sb.Clear();
for (int y = 0; y < Display.Height; y++)
{
for (int x = 0; x < Display.Width; x++)
{
sb.Append(display.GetPixel(x, y) ? '█' : ' ');
}
sb.AppendLine();
}
Console.SetCursorPosition(0, 0);
Console.Write(sb.ToString());
}
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(int key);