Skip to content

Commit

Permalink
First graphical window
Browse files Browse the repository at this point in the history
  • Loading branch information
Abde5 committed May 19, 2021
1 parent 5a6ad80 commit 9d62b83
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LIBS =
LIBS = -lmingw32 -lSDL2main -lSDL2
CFLAGS = -Wall

# Should be equivalent to your list of C files, if you don't build selectively
Expand Down
Binary file modified debug.exe
Binary file not shown.
Binary file modified gbt.exe
Binary file not shown.
13 changes: 11 additions & 2 deletions src/groups/aritmetic.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,11 @@ void Scf37(struct CPU* cpu, struct MMU* mmu)
printf("SCF");
#endif
// Cycles: 4, (Z N H C): - 0 0 1
printf("Not implemented! (Scf37)");

// update timer
(*cpu).tick+=4;

(*cpu).F = ((*cpu).F & Z_flag) | C_flag;
}

void Ccf3F(struct CPU* cpu, struct MMU* mmu)
Expand All @@ -577,7 +581,12 @@ void Ccf3F(struct CPU* cpu, struct MMU* mmu)
printf("CCF");
#endif
// Cycles: 4, (Z N H C): - 0 0 C
printf("Not implemented! (Ccf3F)");

// update timer
(*cpu).tick+=4;

unsigned char inv_Cflag = (~((*cpu).F & C_flag) & C_flag);
(*cpu).F = ((*cpu).F & Z_flag) | inv_Cflag;
}

void Adc_8(unsigned char* A, unsigned char* B, struct CPU* cpu) {
Expand Down
2 changes: 1 addition & 1 deletion src/groups/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void Nop00(struct CPU* cpu, struct MMU* mmu)
printf("NOP");
#endif
// Cycles: 4, (Z N H C): - - - -
printf("Not implemented! (Nop00)");
(*cpu).tick += 4;
}

void Rlca07(struct CPU* cpu, struct MMU* mmu)
Expand Down
7 changes: 6 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "tools.h"
#include "cpu.h"
#include "mmu.h"
#include "ppu.h"


#define TETRIS_SIZE 32768
Expand Down Expand Up @@ -45,7 +47,7 @@ int main(int argc, char *argv[]) {
cpu.tick= 0;

//////////////////////////////

/*
char* var;
int instruction_nb = 0;
// TESTING instructions
Expand All @@ -69,5 +71,8 @@ int main(int argc, char *argv[]) {
//printf("\tB register: %x\n", cpu.B);
patches(&cpu,&mmu);
}
*/

create_window();
return 0;
}
63 changes: 63 additions & 0 deletions src/ppu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "ppu.h"

#define SCALE 5

SDL_Window* create_window(){

SDL_Window *window;

SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2

// Create an application window with the following settings:
window =SDL_CreateWindow(
"GBT", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
160*SCALE, // width, in pixels
144*SCALE, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
unsigned int palette[4] = {0xffe0f8d0, 0xff88c070, 0xff346856, 0xff081820};

unsigned int* pixels = calloc(160*144,sizeof(unsigned int));

int i, j; for (i = 0; i < 144; ++i)
for (j = 0; j < 160; ++j)
if (j < 40) pixels[i*160+j] = palette[0];
else if (j < 80) pixels[i*160+j] = palette[1];
else if (j < 120) pixels[i*160+j] = palette[2];
else pixels[i*160+j] = palette[3];

SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);

SDL_RenderSetScale(renderer,
SCALE, SCALE);

SDL_Texture * texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC,
160,
144);

if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return NULL;
}

SDL_UpdateTexture(texture, NULL, pixels, 160 * sizeof(Uint32));

SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);

SDL_Delay(30000); // Pause execution for 3000 milliseconds, for example

// Close and destroy the window
SDL_DestroyWindow(window);

// Clean up
SDL_Quit();

return window;
}
4 changes: 4 additions & 0 deletions src/ppu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>

SDL_Window* create_window();

0 comments on commit 9d62b83

Please sign in to comment.