-
Notifications
You must be signed in to change notification settings - Fork 1
/
chip8.h
165 lines (144 loc) · 10.8 KB
/
chip8.h
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
// MIT License
//
// Copyright (c) 2018 - 2022 Les Farrell
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE
#ifndef CHIP8_HEADER
#define CHIP8_HEADER
#define BACKGROUND tigrRGB( 0,160, 60 )
#define FOREGROUND tigrRGB( 50, 50, 50 )
int CHIP8_SUPER = 0; // Flag which mode the Virtual Machine is in.
int CHIP8_SCREENWIDTH = 64; // Default CHIP8 Screen Width.
int CHIP8_SCREENHEIGHT = 32; // Default CHIP8 Screen Height.
enum CHIP8_KEYSTATES
{
CHIP8_KEYUP = 0,
CHIP8_KEYDOWN = 1
};
// Current Chip8_OpCode.
unsigned short Chip8_OpCode; // Current Chip8_OpCode.
// Program Memory.
unsigned char Chip8_ProgramMemory[4096]; // Chip 8's Main Memory.
// Display Memory.
unsigned char Chip8_DisplayMemory[8192]; // Chip 8 Display 2048 = (64 * 32) 8192 = (128 * 64)
// Various Registers.
unsigned char Chip8_VRegister[16]; // Chip8's 16 Registers.
unsigned char Chip8_HP48Registers[16]; // Addition registers for SuperChip instructions.
unsigned short Chip8_IndexRegister; // Chip8's Index register.
unsigned short Chip8_ProgramCounter; // Program counter.
// Timers.
unsigned char Chip8_DelayTimer; // Delay Timer.
unsigned char Chip8_SoundTimer; // Sound Timer.
// Chip8 Call Stack and Pointer.
unsigned short Chip8_Stack[16]; // Chip8's stack.
unsigned short Chip8_StackPointer; // Stack pointer.
// Chip8 Key States.
unsigned char Chip8_KeyStates[16]; // Store key states.
// Screen Update Flag.
unsigned char Chip8_DrawFlag; // Okay to redraw screen.
// Chip 8 Default Fonts.
unsigned char Chip8_FontSet[80] = {
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
};
// high-res mode font sprites ('0'-'F')
unsigned char Chip8_SuperFontSet[160] = {
0xFF, 0xFF, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xFF, // 0
0x18, 0x78, 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0xFF, 0xFF, // 1
0xFF, 0xFF, 0x03, 0x03, 0xFF, 0xFF, 0xC0, 0xC0, 0xFF, 0xFF, // 2
0xFF, 0xFF, 0x03, 0x03, 0xFF, 0xFF, 0x03, 0x03, 0xFF, 0xFF, // 3
0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xFF, 0x03, 0x03, 0x03, 0x03, // 4
0xFF, 0xFF, 0xC0, 0xC0, 0xFF, 0xFF, 0x03, 0x03, 0xFF, 0xFF, // 5
0xFF, 0xFF, 0xC0, 0xC0, 0xFF, 0xFF, 0xC3, 0xC3, 0xFF, 0xFF, // 6
0xFF, 0xFF, 0x03, 0x03, 0x06, 0x0C, 0x18, 0x18, 0x18, 0x18, // 7
0xFF, 0xFF, 0xC3, 0xC3, 0xFF, 0xFF, 0xC3, 0xC3, 0xFF, 0xFF, // 8
0xFF, 0xFF, 0xC3, 0xC3, 0xFF, 0xFF, 0x03, 0x03, 0xFF, 0xFF, // 9
0x7E, 0xFF, 0xC3, 0xC3, 0xC3, 0xFF, 0xFF, 0xC3, 0xC3, 0xC3, // A
0xFC, 0xFC, 0xC3, 0xC3, 0xFC, 0xFC, 0xC3, 0xC3, 0xFC, 0xFC, // B
0x3C, 0xFF, 0xC3, 0xC0, 0xC0, 0xC0, 0xC0, 0xC3, 0xFF, 0x3C, // C
0xFC, 0xFE, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xFE, 0xFC, // D
0xFF, 0xFF, 0xC0, 0xC0, 0xFF, 0xFF, 0xC0, 0xC0, 0xFF, 0xFF, // E
0xFF, 0xFF, 0xC0, 0xC0, 0xFF, 0xFF, 0xC0, 0xC0, 0xC0, 0xC0 // F
};
// Our Default Chip8 ROM.
unsigned char Chip8_LogoRom[] = {
0x00,0xFF,0xA2,0xE4,0x60,0x00,0x61,0x00,0xD0,0x10,0xA3,0x04,0x60,0x10,0x61,0x00,0xD0,0x10,0xA3,0x24,0x60,0x20,0x61,0x00,0xD0,0x10,0xA3,0x44,0x60,0x30,
0x61,0x00,0xD0,0x10,0xA3,0x64,0x60,0x40,0x61,0x00,0xD0,0x10,0xA3,0x84,0x60,0x50,0x61,0x00,0xD0,0x10,0xA3,0xA4,0x60,0x60,0x61,0x00,0xD0,0x10,0xA3,0xC4,
0x60,0x70,0x61,0x00,0xD0,0x10,0xA3,0xE4,0x60,0x10,0x61,0x10,0xD0,0x10,0xA4,0x04,0x60,0x20,0x61,0x10,0xD0,0x10,0xA4,0x24,0x60,0x30,0x61,0x10,0xD0,0x10,
0xA4,0x44,0x60,0x40,0x61,0x10,0xD0,0x10,0xA4,0x64,0x60,0x50,0x61,0x10,0xD0,0x10,0xA4,0x84,0x60,0x60,0x61,0x10,0xD0,0x10,0xA4,0xA4,0x60,0x10,0x61,0x20,
0xD0,0x10,0xA4,0xC4,0x60,0x20,0x61,0x20,0xD0,0x10,0xA4,0xE4,0x60,0x30,0x61,0x20,0xD0,0x10,0xA5,0x04,0x60,0x40,0x61,0x20,0xD0,0x10,0xA5,0x24,0x60,0x50,
0x61,0x20,0xD0,0x10,0xA5,0x44,0x60,0x60,0x61,0x20,0xD0,0x10,0xA5,0x64,0x60,0x00,0x61,0x30,0xD0,0x10,0xA5,0x84,0x60,0x10,0x61,0x30,0xD0,0x10,0xA5,0xA4,
0x60,0x20,0x61,0x30,0xD0,0x10,0xA5,0xC4,0x60,0x30,0x61,0x30,0xD0,0x10,0xA5,0xE4,0x60,0x40,0x61,0x30,0xD0,0x10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00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xF0,0x89,0xF8,0xCB,0xFC,
0xCF,0x9C,0xCF,0xF8,0xCF,0xF0,0xCF,0xF8,0xCF,0xBC,0x8F,0x9C,0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x61,0x00,0x31,
0x01,0xE7,0x01,0xC7,0x03,0xC7,0x03,0xC7,0x01,0xC7,0x01,0xE7,0x01,0xF7,0x00,0x61,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x60,
0xFE,0xB0,0xC0,0x7C,0xC0,0x3C,0xC0,0x3C,0xC0,0x3C,0xC0,0x3C,0xFC,0xB0,0xFE,0xF0,0xFC,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x3F,0x00,0x4F,0x00,0x5F,0x00,0x01,0x00,0x5F,0x00,0x7C,0x00,0x7F,0x00,0x7F,0x00,0x7F,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x83,0xF0,0xC3,0xF9,0xEF,0x9D,0xEF,0x9C,0xAF,0x9D,0x0F,0x9D,0xCF,0x9D,0xEF,0xFD,0xEF,0xFD,0xC3,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xFE,0x0F,0x3F,0x13,0x7F,0x97,0x07,0x80,0x7E,0x97,0xF0,0x1F,0xFF,0x1F,0xFF,0x9F,0xFF,0x9F,0xFF,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0xF0,0x00,0xF8,0x00,0x78,0x00,0xE8,0x00,0x00,0x00,0xF0,0x00,0xF8,0x00,0xF8,0x00,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x82,0x04,0xC2,0x05,0xC3,0x07,0xC3,0x07,0xC3,0x07,0xC3,0x07,0xC3,0x07,0xFB,0x07,0xFF,0x03,0xF9,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x1F,0xFF,0x1F,0xE0,0x7F,0xE1,0x7C,0xFF,0x3F,0xE1,0x01,0xE0,0x3F,0xFE,0x7F,0xFF,0x7F,0xFE,0x3F,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x01,0xE0,0x01,0xC0,0x01,0x00,0x01,0xC0,0x01,0xE0,0x01,0xE0,0x01,0xE0,0x01,0xE0,0x01,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x8F,0x7F,0x8F,0xF0,0x3E,0xF0,0xBE,0xFF,0xBF,0xFF,0x3E,0xF0,0x3E,0xF0,0x3E,0xF0,0x3E,
0xE0,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC5,0xFC,0xC5,0xFE,0x77,0xCE,0x77,0xCE,0xF7,0xFC,0x77,0xF8,0x77,0xFC,0x77,0xDE,
0x77,0xCE,0x23,0x84,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xBF,0x8B,0xBF,0xCB,0xF9,0xCF,0xF9,0xCF,0xFF,0x8F,0xFF,0x0F,0xFF,0x8F,
0xFB,0xCF,0xF9,0xCF,0x70,0x87,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x70,0xFC,0x98,0x80,0xB8,0x84,0xF8,0xFC,0xF8,0x84,0xF8,
0x80,0xF8,0xF8,0xFF,0xFC,0xFF,0xF8,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x13,0x00,0x17,0x00,0x1F,0x00,0x1F,0x00,
0x1F,0x00,0x1F,0x00,0x1F,0xE0,0x9F,0xF0,0x0F,0xE0,0x00,0x00,0x00,0x00
};
#include "tigr.h"
// Function prototypes.
int Chip8_LoadROM(char *ROM_FileName);
void Chip8_Initialise(void);
void Chip8_EmulateCPU(void);
void Chip8_GetKeyStates(Tigr *screen);
void Chip8_DrawScreen(Tigr *screen);
void Chip8_ShowProgramState(void);
void Chip8_ProcessDroppedFiles(void);
void Chip8_Disassemble(void);
#endif