-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameboy.cpp
More file actions
143 lines (116 loc) · 4.29 KB
/
Copy pathgameboy.cpp
File metadata and controls
143 lines (116 loc) · 4.29 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
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <fstream>
//#include <vector>
#include "Z80.h"
#include "gameboy.pro"
#include "screen.h"
#include "screen.cpp"
// Global ROM array - Will be loaded from a file
unsigned char* rom = nullptr; // Use unsigned char* for binary data
int romSize = 0;
unsigned char graphicsRAM[8192];
int palette[4];
int tileset, tilemap, scrollx, scrolly;
// Memory access functions
unsigned char memoryRead(int address) {
if(address < romSize) {
return rom[address];
}
return 0; // Return 0 for addresses beyond the ROM size
}
void memoryWrite(int address, unsigned char b) {
// For this project, writing to ROM is not handled
}
void renderScreen() {
for (int y = 0; y < 144; y++) {
for (int x = 0; x < 160; x++) {
// Apply scroll
int scrolledX = (x + scrollx) & 255;
int scrolledY = (y + scrolly) & 255;
// Determine tile coordinates
int tileX = scrolledX / 8;
int tileY = scrolledY / 8;
// Find tile's position in the tile map
int tilePosition = tileY * 32 + tileX;
// Get the tile index from the appropriate tile map
int tileIndex = (tilemap == 0) ? graphicsRAM[0x1800 + tilePosition] : graphicsRAM[0x1c00 + tilePosition];
// Address calculation differs for tileset 0
int tileAddress = (tileset == 1) ? tileIndex * 16 : (tileIndex >= 128 ? tileIndex - 256 : tileIndex) * 16 + 0x1000;
// Get the pixel within the tile
int xoffset = scrolledX % 8;
int yoffset = scrolledY % 8;
// Retrieve the color bytes for the tile row
unsigned char row0 = graphicsRAM[tileAddress + (yoffset * 2)];
unsigned char row1 = graphicsRAM[tileAddress + (yoffset * 2) + 1];
// Extract the color bits for the specific pixel
int colorBit0 = (row0 >> (7 - xoffset)) & 1;
int colorBit1 = (row1 >> (7 - xoffset)) & 1;
// Combine the color bits to get the color index
int colorIndex = (colorBit1 << 1) | colorBit0;
// Map the color index to the actual color using the palette
int color = palette[colorIndex];
// Update the screen with the calculated color
updateSquare(x, y, color);
}
}
// Refresh the screen after all updates are done
onFrame();
}
extern QApplication* app;
extern Screen* screen;
int main(int argc, char** argv)
{
setup(argc,argv);
//part 1 code here
// Load ROM from file
std::ifstream romfile("testrom.gb", std::ios::in | std::ios::binary | std::ios::ate);
if (!romfile.is_open()) {
std::cerr << "Failed to open ROM file." << std::endl;
return 1;
}
std::streampos size = romfile.tellg();
rom = new unsigned char[size]; // Allocate memory for the ROM
romSize = size;
romfile.seekg(0, std::ios::beg);
romfile.read(reinterpret_cast<char*>(rom), size); // Cast to char* for read function
romfile.close();
// Initialize the Z80 CPU with memory access functions
Z80* z80 = new Z80(memoryRead, memoryWrite);
// Reset the CPU
z80->reset();
// Execute instructions until the CPU halts
while(!z80->halted) {
z80->doInstruction();
std::cout << "PC: " << z80->PC << ", A: " << (int)z80->A << ", B: " << (int)z80->B << std::endl;
}
// Output the final value in register A (expected: 21)
std::cout << "Final A: " << (int)z80->A << std::endl;
//return 0;
//part 2 code here
// read the first 8192 integers from screendump into graphicsRAM.
if (!vidfile.is_open()) {
std::cerr << "Unable to open screendump.txt" << std::endl;
return 1;
}
int n;
ifstream vidfile("screendump.txt",ios::in);
for(int i=0; i<8192; i++){
int n;
vidfile>>n;
graphicsRAM[i]=(unsigned char)n;
}
// Then read the other variables:
vidfile >> tileset;
vidfile >> tilemap;
vidfile >> scrollx;
vidfile >> scrolly;
vidfile >> palette[0];
vidfile >> palette[1];
vidfile >> palette[2];
vidfile >> palette[3];
// Now that graphicsRAM and other variables are initialized, call renderScreen
renderScreen();
// Enter the Qt application loop
return app.exec();
}