Skip to content

Commit

Permalink
Begin work on the gpu window
Browse files Browse the repository at this point in the history
  • Loading branch information
Leslie Chisholm committed Oct 1, 2019
1 parent 4096e61 commit 224f0dc
Show file tree
Hide file tree
Showing 5 changed files with 7,935 additions and 96 deletions.
Binary file removed ROMs/Hangman (PD).gb
Binary file not shown.
100 changes: 40 additions & 60 deletions gpu.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <unistd.h>
#include "gpu.h"
#include "defs.h"
#include "types.h"
Expand All @@ -23,7 +24,6 @@ void gpu_init(){
gpu->tile_data_select = 0x8800;
gpu->background_tile_map_display = 0x9800;
gpu->background_display = 0;
gpu->window_display_enable = 0;
gpu->sprite_display_enable = 0;

for(y=0;y<HEIGHT;y++){
Expand All @@ -39,6 +39,16 @@ void gpu_init(){
u8 gpu_get_line(){
return gpu->line & 0xFF;
}
/* Writing to the gpu line register will reset it */
void gpu_set_line(){
gpu->line = 0;
}
u8 gpu_get_line_compare(){
return gpu->line_compare & 0xFF;
}
void gpu_set_line_compare(const u8 value){
gpu->line_compare = value;
}

u8 gpu_get_palette(const PaletteType palette_type){
switch(palette_type){
Expand Down Expand Up @@ -88,23 +98,38 @@ void gpu_set_palette(const u8 value, const PaletteType palette_type){
}
}

void set_scroll_x(const u8 value){
void gpu_set_scroll_x(const u8 value){
gpu->scroll_x = value;
}

u8 get_scroll_x(){
u8 gpu_get_scroll_x(){
return gpu->scroll_x;
}

void set_scroll_y(const u8 value){
void gpu_set_scroll_y(const u8 value){
gpu->scroll_y = value;
}

u8 get_scroll_y(){
u8 gpu_get_scroll_y(){
return gpu->scroll_y;
}

void set_lcd_control_register(const u8 value){
void gpu_set_window_x(const u8 value){
printf("This game is using the window\n");
gpu->window_x = value;
}
u8 gpu_get_window_x(){
return gpu->window_x;
}
void gpu_set_window_y(const u8 value){
printf("This game is using the window\n");
gpu->window_y = value;
}
u8 gpu_get_window_y(){
return gpu->window_y;
}

void gpu_set_lcd_control_register(const u8 value){
gpu->lcd_control_register = value;
gpu->lcd_display_enable = gpu->lcd_control_register & 0x80 ? 1:0;
gpu->window_tile_map_display_select = gpu->lcd_control_register & 0x40 ?
Expand All @@ -118,11 +143,11 @@ void set_lcd_control_register(const u8 value){
gpu->background_display = gpu->lcd_control_register & 0x01 ? 1 : 0;
}

u8 get_lcd_control_register(){
u8 gpu_get_lcd_control_register(){
return gpu->lcd_control_register;
}

void update_tile(const u16 address, const u8 value){
void gpu_update_tile(const u16 address, const u8 value){
// Takes 2 bytes at a time
// first byte is LSB of a row
// second byte is MSB of a row
Expand All @@ -142,7 +167,7 @@ void update_tile(const u16 address, const u8 value){

}

void update_sprite(const u16 address, const u8 value){
void gpu_update_sprite(const u16 address, const u8 value){
unsigned int sprite_num = (address - 0xFE00) >> 2;
Sprite *sprite;
/* printf("Updating sprite %d with value %d at address %X \n", */
Expand Down Expand Up @@ -259,14 +284,14 @@ static void swap_buffers(){
display_redraw();
/* Sleep until the frame time is finished */
struct timespec frame_end_time;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &frame_end_time);
clock_gettime(CLOCK_MONOTONIC, &frame_end_time);
long timedelta = FULL_FRAME_TIME_US -
((frame_end_time.tv_nsec - gpu->frame_start_time.tv_nsec) / 1000);
if(timedelta > 0 && timedelta < FULL_FRAME_TIME_US)
usleep(timedelta);

/* Set new frame start time */
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &gpu->frame_start_time);
clock_gettime(CLOCK_MONOTONIC, &gpu->frame_start_time);

}

Expand Down Expand Up @@ -313,55 +338,10 @@ void gpu_step(int op_time){
break;
}
}

u8 gpu_get_status_register(){
return gpu->mode;
void gpu_set_status_register(const u8 value){
printf("gpu_set_status_register %X\n", value);
}

static void print_screen(){
unsigned int x,y;
for(y=0;y<HEIGHT;y++){
for(x=0;x<WIDTH;x++){
printf("%s",gpu->screen[y][x]==0?" ":"a");
}
printf("\n");
}
}

static void print_tiles(){
unsigned int i,j,k;
for(i=0;i<27;i++){
printf("Tile : %d\n",i);
for(j=0;j<8;j++){
printf("row %d:",j);
for(k=0;k<8;k++){
printf("%s",gpu->tiles[i][j][k]==1? "a" :" ");
}
printf("\n");
}
}
}

static void print_tile_ram(){
unsigned int i,j;
for(i=0;i<26;i++){
printf("Tile %X:\n",i);
for(j=0;j<16;j++){
printf("memory %d : %X\n",j,get_mem(0x8000 + i*16 + j));
}
}
}

static void print_background_data_ram(){
unsigned int i;
for(i=0;i<=0x2f;i++){
printf("memory %X : %d\n",0x9900+i,get_mem(0x9900 + i));
}
}

void gpu_test(){
print_screen();
print_tiles();
print_tile_ram();
print_background_data_ram();
u8 gpu_get_status_register(){
return gpu->mode | (gpu->line == gpu->line_compare ? 0x04 : 0);
}
30 changes: 20 additions & 10 deletions gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ typedef struct{
//scroll registers
u8 scroll_x;
u8 scroll_y;
u8 window_x;
u8 window_y;

int line;
int line_compare;
int mode;
int curscan;

Expand Down Expand Up @@ -83,18 +86,25 @@ typedef struct{
extern GPU *gpu;
//extern int gpu_clock;
extern void gpu_init();
extern u8 gpu_get_line();
extern void gpu_set_line();
extern u8 gpu_get_line_compare();
extern void gpu_set_line_compare(u8 value);
extern void gpu_step(const int op_time);
extern u8 gpu_get_status_register();
extern void gpu_set_status_register(const u8 value);
extern u8 gpu_get_palette(const PaletteType palette_type);
extern void gpu_set_palette(const u8 value, const PaletteType palette_type);
extern void set_scroll_y(const u8 value);
extern u8 get_scroll_y();
extern void set_scroll_x(const u8 value);
extern u8 get_scroll_x();
extern void set_lcd_control_register(const u8 value);
extern u8 get_lcd_control_register();
extern void update_tile(const u16 address, const u8 value);
extern void update_sprite(const u16 address, const u8 value);
extern void gpu_test();
extern u8 gpu_get_line();
extern void gpu_set_scroll_y(const u8 value);
extern u8 gpu_get_scroll_y();
extern void gpu_set_scroll_x(const u8 value);
extern u8 gpu_get_scroll_x();
extern void gpu_set_window_y(const u8 value);
extern u8 gpu_get_window_y();
extern void gpu_set_window_x(const u8 value);
extern u8 gpu_get_window_x();
extern void gpu_set_lcd_control_register(const u8 value);
extern u8 gpu_get_lcd_control_register();
extern void gpu_update_tile(const u16 address, const u8 value);
extern void gpu_update_sprite(const u16 address, const u8 value);
#endif
50 changes: 24 additions & 26 deletions mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,17 @@ u8 get_mem(u16 address){
return 0; //TODO

case LCD_CONTROL_REGISTER: // FF40
return get_lcd_control_register();
return gpu_get_lcd_control_register();
case LCD_STATUS_REGISTER:
return gpu_get_status_register();
case LCD_SCROLL_Y_REGISTER:
return get_scroll_y();
return gpu_get_scroll_y();
case LCD_SCROLL_X_REGISTER://lcd scroll X register
return get_scroll_x();
return gpu_get_scroll_x();
case CURRENT_FRAME_LINE://get the current frame line
return gpu_get_line();
case FRAME_LINE_COMPARE:
printf("Need to implement FLC\n");
return 0;
return gpu_get_line_compare();
case DMA_TRANSFER:
printf("Shouldn't be reading DMA_TRANSFER\n");
return 0;
Expand All @@ -229,12 +228,10 @@ u8 get_mem(u16 address){
return gpu_get_palette(OBJECT_PALETTE0);
case OBJECT_PALETTE1_MEMORY:
return gpu_get_palette(OBJECT_PALETTE1);
case WINDOW_X_POS:
printf("Implement read window x %d\n");
return 0;
case WINDOW_Y_POS:
printf("Implement read window y %d\n");
return 0;
return gpu_get_window_y();
case WINDOW_X_POS:
return gpu_get_window_x();
case DISABLE_BOOT_ROM://disable boot rom
fprintf(stderr,"Why would you read disabling the boot rom?\n");
return 0;
Expand Down Expand Up @@ -325,7 +322,7 @@ void set_mem(u16 address, u8 value){
case 0x8000: case 0x9000:
memory->vram[address & 0x1FFF] = value;
if(address <= TILE_DATA_END)
update_tile(address, value);
gpu_update_tile(address, value);
return;
//Swtichable RAM 2KB
case 0xA000: case 0xB000:
Expand All @@ -349,7 +346,7 @@ void set_mem(u16 address, u8 value){
case 0xE00:
if(address < 0xFEA0){
memory->oam[address & 0xFF] = value;
update_sprite(address, value);
gpu_update_sprite(address, value);
return;
}
case 0xF00:
Expand Down Expand Up @@ -392,22 +389,23 @@ void set_mem(u16 address, u8 value){
case SOUND_OUTPUT_SELECTION_TERMINAL:
case SOUND_ON_OFF:
return; //TODO
case LCD_CONTROL_REGISTER://lcd control register
set_lcd_control_register(value);
return;
case LCD_STATUS_REGISTER://lcd status register
case LCD_CONTROL_REGISTER:
gpu_set_lcd_control_register(value);
return;
case LCD_SCROLL_Y_REGISTER://lcd scroll Y register
set_scroll_y(value);
case LCD_STATUS_REGISTER:
gpu_set_status_register(value);
return;
case LCD_SCROLL_Y_REGISTER:
gpu_set_scroll_y(value);
return;
case LCD_SCROLL_X_REGISTER://lcd scroll X register
set_scroll_x(value);
case LCD_SCROLL_X_REGISTER:
gpu_set_scroll_x(value);
return;
case CURRENT_FRAME_LINE:
printf("Shouldn't change frame line\n");
gpu_set_line();
return;
case FRAME_LINE_COMPARE:
printf("Shouldn't compare frame line??\n");
gpu_set_line_compare(value);
return;
case DMA_TRANSFER:
{
Expand All @@ -425,11 +423,11 @@ void set_mem(u16 address, u8 value){
case OBJECT_PALETTE1_MEMORY:
gpu_set_palette(value, OBJECT_PALETTE1);
return;
case WINDOW_X_POS:
printf("Implement window x %d\n", value);
return;
case WINDOW_Y_POS:
printf("Implement window y %d\n", value);
gpu_set_window_y(value);
return;
case WINDOW_X_POS:
gpu_set_window_x(value);
return;
case DISABLE_BOOT_ROM://disable boot rom
if(value == 1)
Expand Down
Loading

0 comments on commit 224f0dc

Please sign in to comment.