-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode-analyser.hpp
186 lines (179 loc) · 7.96 KB
/
opcode-analyser.hpp
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
/*
* MIT License
*
* Copyright(c) 2018 Paul Bernitz
*
* 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.
*/
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "types.hpp"
namespace c8s
{
// Prints opcodes in a readable format with additional information.
void analyse_opcodes(std::vector<u16> opcodes)
{
for (auto& op : opcodes)
{
// Fetch variables.
unsigned nnn = op & 0xFFF; // ----XXXXXXXXXXXX
unsigned n = op & 0xF; // ------------XXXX
unsigned x = (op >> 8) & 0xF; // ----XXXX--------
unsigned y = (op >> 4) & 0xF; // --------XXXX----
unsigned kk = op & 0xFF; // --------XXXXXXXX
switch (op & 0xF000)
{
case 0x0000:
switch (op & 0xFF)
{
case 0xE0: // 00E0 - Clear the screen.
std::cout << std::hex << op << " - 00E0 - Clear the screen\n";
break;
case 0xEE: // 00EE - Return from a subroutine.
std::cout << std::hex << op << " - 00EE - Return from a subroutine\n";
break;
default:
std::cout << "Unknown instruction: " << op << std::endl;
}
break;
case 0x1000: // 1NNN - Jump to location nnn.
std::cout << std::hex << op << " - 1NNN - Jump to location " << nnn << '\n';
break;
case 0x2000: // 2NNN - Call subroutine at nnn.
std::cout << std::hex << op << " - 2NNN - Call subroutine at " << nnn << '\n';
break;
case 0x3000: // 3XNN - Skip next instruction if Vx = kk.
std::cout << std::hex << op << " - 3XNN - Skip next instruction if V[" << x << "] = " << kk << '\n';
break;
case 0x4000: // 4XNN - Skip next instruction if Vx != kk.
std::cout << std::hex << op << " - 4XNN - Skip next instruction if V[" << x << "] != " << kk << '\n';
break;
case 0x5000: // 5XY0 - Skip next instruction if Vx = Vy.
std::cout << std::hex << op << " - 5XY0 - Skip next instruction if V[" << x << "] = V[" << y << "]\n";
break;
case 0x6000: // 6XNN - Set Vx = kk.
std::cout << std::hex << op << " - 6XNN - Set V[" << x << "] = " << kk << '\n';
break;
case 0x7000: // 7XNN - Set Vx = Vx + kk.
std::cout << std::hex << op << " - 7XNN - Set V[" << x << "] += " << kk << '\n';
break;
case 0x8000:
switch (op & 0xF)
{
case 0x0: // 8XY0 - Set Vx = Vy.
std::cout << std::hex << op << " - 8XY0 - Set V[" << x << "] = V[" << y << "]\n";
break;
case 0x1: // 8XY1 - Set Vx = Vx OR Vy.
std::cout << std::hex << op << " - 8XY1 - Set V[" << x << "] |= V[" << y << "]\n";
break;
case 0x2: // 8XY2 - Set Vx = Vx AND Vy.
std::cout << std::hex << op << " - 8XY2 - Set V[" << x << "] &= V[" << y << "]\n";
break;
case 0x3: // 8XY3 - Set Vx = Vx XOR Vy.
std::cout << std::hex << op << " - 8XY3 - Set V[" << x << "] ^= V[" << y << "]\n";
break;
case 0x4: // 8XY4 - Set Vx = Vx + Vy, set VF = carry.
std::cout << std::hex << op << " - 8XY4 - Set V[" << x << "] += V[" << y << "]\n";
break;
case 0x5: // 8XY5 - Set Vx = Vx - Vy, set VF = NOT borrow.
std::cout << std::hex << op << " - 8XY5 - Set V[" << x << "] -= V[" << y << "]\n";
break;
case 0x6: // 8XY6 - Set Vx = Vx SHR 1.
std::cout << std::hex << op << " - 8XY6 - Set V[" << x << "] >>= 1\n";
break;
case 0x7: // 8XY7 - Set Vx = Vy - Vx, set VF = NOT borrow.
std::cout << std::hex << op << " - 8XY7 - Set V[" << x << "] |= V[" << y << "]\n";
break;
case 0xE: // 8XYE - Set Vx = Vx SHL 1.
std::cout << std::hex << op << " - 8XYE - Set V[" << x << "] <<= 1\n";
break;
default:
std::cout << "Unknown instruction: " << op << std::endl;
}
break;
case 0x9000: // 9XY0 - Skip next instruction if Vx != Vy.
std::cout << std::hex << op << " - 9XY0 - Skip next instruction if V[" << x << "] != V[" << y << "]\n";
break;
case 0xA000: // ANNN - Set I = nnn.
std::cout << std::hex << op << " - ANNN - Set I = " << nnn << '\n';
break;
case 0xB000: // BNNN - Jump to location nnn + V0.
std::cout << std::hex << op << " - BNNN - Jump to location " << nnn << " + V[0]\n";
break;
case 0xC000: // CXNN - Set Vx = random byte AND kk.
std::cout << std::hex << op << " - CXNN - Set V[" << x << "] = rand() & " << kk;
break;
case 0xD000: // DXYN - Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision.
std::cout << std::hex << op << " - DXYN - Display " << n << "-byte sprite from location `I` at (V[" << x << "], V[" << y << "]), set V[F] = collision\n";
break;
case 0xE000:
switch (op & 0xFF)
{
case 0x9E: // EX9E - Skip next instruction if key with the value of Vx is pressed.
std::cout << std::hex << op << " - EX9E - Skip next instruction if key with value of V[" << x << "] is pressed\n";
break;
case 0xA1: // EXA1 - Skip next instruction if key with the value of Vx is not pressed.
std::cout << std::hex << op << " - EXA1 - Skip next instruction if key with value of V[" << x << "] is not pressed\n";
break;
default:
std::cout << "Unknown instruction: " << op << std::endl;
}
break;
case 0xF000:
switch (op & 0xFF)
{
case 0x07: // FX07 - Set Vx = delay timer value.
std::cout << std::hex << op << " - FX07 - Set V[" << x << "] = delay timer value\n";
break;
case 0x0A: // FX0A - Wait for a key press, store the value of the key in Vx.
std::cout << std::hex << op << " - FX0A - Wait for a key press, store the value of the key in V[" << x << "]\n";
break;
case 0x15: // FX15 - Set delay timer = Vx.
std::cout << std::hex << op << " - FX15 - Set delay timer = V[" << x << "]\n";
break;
case 0x18: // FX18 - Set sound timer = Vx.
std::cout << std::hex << op << " - FX18 - Set sound timer = V[" << x << "]\n";
break;
case 0x1E: // FX1E - Set I = I + Vx.
std::cout << std::hex << op << " - FX1E - Set I += V[" << x << "]\n";
break;
case 0x29: // FX29 - Set I = location of sprite for digit Vx.
std::cout << std::hex << op << " - FX29 - Set I = Location of sprite for digit V[" << x << "]\n";
break;
case 0x33: // FX33 - Store BCD representation of Vx in memory locations I, I+1, and I+2.
std::cout << std::hex << op << " - FX33 - Store BCD representation of V[" << x << "] in memory locations I, I+1, I+2\n";
break;
case 0x55: // FX55 - Store registers V0 through Vx in memory starting at location I.
std::cout << std::hex << op << " - FX55 - Store registers V[0] -> V[" << x << "] in memory starting at location `I`\n";
break;
case 0x65: // FX65 - Read registers V0 through Vx from memory starting at location I.
std::cout << std::hex << op << " - FX65 - Read registers V[0] -> V[" << x << "] from memory starting at location `I`\n";
break;
default:
std::cout << "Unknown instruction: " << op << std::endl;
}
break;
default:
std::cout << "Unknown instruction: " << op << std::endl;
}
}
}
}