-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGbReIgnited.cpp
155 lines (116 loc) · 2.86 KB
/
GbReIgnited.cpp
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
// GbReIgnited.cpp : Este archivo contiene la función "main". La ejecución del programa comienza y termina ahí.
//
#include "pch.h"
#include <iostream>
#include <stack>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
typedef union
{
struct
{
uint8_t lo;
uint8_t hi;
};
uint16_t word;
} Register16_t;
//Our CPU regisgers
typedef struct
{
//Our basic registers, AF, BC, DE, HL
Register16_t AF, BC, DE, HL;
//Special Registers
Register16_t SP, PC;
} regs_t;
unsigned short address;
std::stack <unsigned short> gbStack;
unsigned short tAddress;
//--- LoadFile Module Related
unsigned char memory[0xFFFF];
unsigned char memoryA[0xFFFF];
std::string ROM = "tetris.gb";
char letter;
//-- OpFetcher/OpDecoder Module related
unsigned char engager;
unsigned char _8bitIn;
unsigned char _16bitIn;
unsigned short pc;
std::string vEng; //verbose engager
void PopMode();
void InitGb();
void LoadFile();
void OpDecoder();
void OpFetcher();
class Emulator { // The class
public: // Access specifier
//Variables
regs_t R16;
//Functions
void InitGb() {
R16.PC.word = 0;
R16.SP.word = 0;
R16.AF.word = 0;
R16.BC.word = 0;
R16.DE.word = 0;
R16.HL.word = 0;
} //End InitGb()
void OpFetcher() {
engager = memoryA[R16.PC.word];
std::cout << "opcode fetched - " << std::hex << (int)engager << "\n";
if (engager == 0xcb) {
engager = memoryA[R16.PC.word + 1];
std::cout << "opcode type 0xCB found -" << std::hex << std::to_string(engager) << "\n";
} //End 0xCB type
else
{
_8bitIn = memoryA[R16.PC.word + 1];
_16bitIn = memoryA[R16.PC.word + 2];
std::stringstream stream;
stream << std::hex << (int)engager;
std::string result(stream.str());
//vEng = std::to_string((int)engager);
std::cout << "Engager - C3 - " << result << "\n";
std::cout << "8bit Inm - " << std::hex << (int)_8bitIn << "\n";
std::cout << "16bit Inm - " << std::hex << (int)_16bitIn << "\n";
} //End normal engager
} // End OpFetcher()
void OpDecoder() {
} //End Opdecoder()
void LoadFile() {
std::ifstream fin(ROM, std::ios::binary);
int length = fin.tellg();
if (!fin) { std::cout << "File not found " << std::endl; }
for (int i = 0; i <= 32000; i++) { fin.get(letter); memoryA[i] = letter; }
//did it worked?
//it does :)
} // End LoadFile()
};
void PopMode() {
//get stack size
for (int i = gbStack.size(); i >= 1; i--) {
tAddress = gbStack.top();
std::cout << "[" << i << "] - " << std::hex << tAddress << "\n";
gbStack.pop();
}
}
int main() {
Emulator gb;
/*
while (1) {
std::cout << "assign address to the stack - 0 to exit push mode\n";
std::cin >> std::hex >> address;
if (address == 0) {
PopMode();
}
else
gbStack.push(address);
std::cout << "\n";
} */
gb.InitGb();
gb.LoadFile();
gb.OpFetcher();
gb.OpDecoder();
return 0;
}