-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.cs
More file actions
71 lines (56 loc) · 1.66 KB
/
Copy pathState.cs
File metadata and controls
71 lines (56 loc) · 1.66 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
using System;
using System.Runtime.InteropServices;
namespace Chip8
{
public class State
{
public readonly byte[] Memory = new byte[4096];
public readonly byte[] Registers = new byte[16];
public ushort ProgramCounter { get; set; }
public byte StackPointer { get; set; } = 0;
public ushort Index { get; set; }
public byte DelayTimer { get; set; }
public byte SoundTimer { get; set; }
public Span<ulong> ScreenBuffer
{
get
{
return MemoryMarshal.Cast<byte, ulong>(Memory.AsSpan()[0x0F00..0x1000]);
}
}
public Span<ushort> Stack
{
get
{
return MemoryMarshal.Cast<byte, ushort>(Memory.AsSpan()[0xEA0..0xF00]);
}
}
/// <summary>
/// 1-bit for each input 1 if pressed, otherwise 0.
/// </summary>
public ushort Keys { get; set; }
public void KeyPress(int key)
{
if (key < 0 || key > 0xF)
throw new InvalidOperationException($"Invalid key pressed, '{key}'.");
Keys |= (ushort)(1 << key);
}
public void KeyRelease(int key)
{
if (key < 0 || key > 0xF)
throw new InvalidOperationException($"Invalid key released, '{key}'.");
Keys &= (ushort)~(1 << key);
}
public int GetKey()
{
for (var i = 0; i <= 0xF; i++)
{
if ((Keys & (1 << i)) > 0)
{
return i;
}
}
return 0;
}
}
}