-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgb.cpp
90 lines (83 loc) · 3.38 KB
/
gb.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
#include <iostream>
#include <csignal>
#include <chrono>
#include <thread>
#include "gb.h"
gb myGB;
gb::gb(){
reset();
}
void gb::reset(){
ime = false;
cycles = 0;
getMemory().putByte(0xFF47, 0xE4); //Initialize our color palette.
//FIXME: We are temporarily using 0x100 as our starting PC because we have not yet implemented the GB bootstrap rom.
pc = PC_START;
myGB.readBootRom();
//ppu.addMem(&memory);
}
void sigint_handler(int signum){
printf("Force Quitting...\n");
myGB.trace();
exit(signum);
}
int main(int argc, char** argv){
signal(SIGINT, sigint_handler);
if(argc == 1){
printf("No rom specified, loading asm/DMG_ROM.bin (if it exists).\n");
//To test with the bootROM lets just pass thru true.
myGB.setStatus(true);
//myGB.setStatus(myGB.load(""));
}
else{
printf("Loading %s\n", argv[1]);
myGB.setStatus(myGB.load(argv[1]));
}
// printf("File loaded successfully: %d\n", myGB.getStatus());
// uint8_t smileyTile[16] = {0x00, 0xFF, 0x00,0xFF,
// 0x24, 0xFF, 0x00,0xFF,
// 0x42, 0xFF, 0x7E,0xFF,
// 0x00, 0xFF, 0x00,0xFF};
// myGB.getMemory().putByte(LCD_CONTROL, 0x81); //Sets the bits 0 and 7 (LCD_ENABLE and BG_DISPLAY)
//myGB.getPPU().render_tiles();
//Main loop
// while(myGB.getStatus()){
// int frameCount = 0;
// //While GB is still running, run every 16ms (60 FPS).
// while(frameCount < FRAMES_PER_SECOND){
// myGB.run();
// std::this_thread::sleep_for(std::chrono::milliseconds(16));
// frameCount++;
// }
// }
}
void gb::run(){
//Primary Event Loop.
while(((myGB.getCycles() % CYCLES_PER_FRAME) != 0 || myGB.getCycles() == 0) && myGB.getStatus()){
//myGB.getMemory().putByte(0x0100, 0x76);
if(myGB.getMemory().getByte(pc) == 0x50) printf("last instruction in bootrom!\n");
myGB.setStatus(myGB.decode(myGB.getMemory().getByte(myGB.pc++)));
myGB.trace();
//myGB.getMemory().dump(0x0134, 0x014D);
//update timers
//update graphics
//do interrupts
}
//Update graphics.
}
/*
* trace
* Trace shows the contents of our 8- and 16-bit Program Registers, as well as the last utilized location in memory and it's contents.
*/
void gb::trace(){
printf("-----------------------------------------------------------\n");
printf("Current cycle: %d Status is: %d\n", myGB.getCycles(), myGB.getStatus());
printf("Program Counter is at: 0x%04x, which contains: 0x%02x\n", pc, getMemory().getByte(pc));
printf("The contents of the 8- and 16-bit registers are:\n");
printf("A: 0x%02x F: 0x%02x\nB: 0x%02x C: 0x%02x\nD: 0x%02x E: 0x%02x\nH: 0x%02x L: 0x%02x\n", getRegisters().getReg8(A), getRegisters().getReg8(F), getRegisters().getReg8(B), getRegisters().getReg8(C) , getRegisters().getReg8(D), getRegisters().getReg8(E), getRegisters().getReg8(H), getRegisters().getReg8(L));
printf("AF: 0x%04x, BC: 0x%04x, DE: 0x%04x, HL: 0x%04x, SP: 0x%04x\n", getRegisters().getReg16(AF), getRegisters().getReg16(BC), getRegisters().getReg16(DE), getRegisters().getReg16(HL), getRegisters().getReg16(SP));
printf("Flag state: H: %d, C: %d, N: %d, Z: %d\n", getRegisters().getFlag(FLAG_H), getRegisters().getFlag(FLAG_C), getRegisters().getFlag(FLAG_N), getRegisters().getFlag(FLAG_Z));
printf("Memory Error: %d\n", getMemory().isMemError());
printf("Last used Memory location: 0x%04x, which contains: %02x\n", getMemory().trace(), getMemory().getByte(getMemory().trace()));
printf("-----------------------------------------------------------\n");
}