Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
#9 #19 ppu oam search
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni Hein committed Jul 22, 2019
1 parent 86e44bb commit 0c6d7e0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
76 changes: 50 additions & 26 deletions source/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "main.h"

int main(int argc, char** argv)
int main(int argc, char **argv)
{
int err;

Expand Down Expand Up @@ -31,7 +31,7 @@ int main(int argc, char** argv)

// TEST

FILE* coredump = fopen("Tetris.dump", "r");
FILE *coredump = fopen("Tetris.dump", "r");

fread(MEM, 1, 65536, coredump);

Expand All @@ -54,7 +54,7 @@ int main(int argc, char** argv)
uint8_t extended_opcode;
display_init(WIDTH, HEIGHT, 6);
// todo: fix invalid writes
char* title = malloc(16 + strlen(GAME_NAME) + 1);
char *title = malloc(16 + strlen(GAME_NAME) + 1);
strcpy(title, "Working Title - ");
strcpy(title + 16, GAME_NAME);
display_set_window_title(title);
Expand All @@ -63,7 +63,7 @@ int main(int argc, char** argv)
uint8_t t8[8];
uint16_t t16[8];
uint32_t t32[8];
uint8_t* t8p;
uint8_t *t8p;

int x = 0;

Expand Down Expand Up @@ -1623,38 +1623,62 @@ int main(int argc, char** argv)
}
else // PPU's turn
{
if(ppu_cycle < 80)
{
// OAM search

memset(VISIBLE_SPRITE_ARRAY, 0, 10);
t8[0] = 0; // visible_sprite_array index
for(int i = 0; i < 40; i++)
{
if(SPRITE_Y_COORDINATE(i)>LY && SPRITE_Y_COORDINATE(i) <= LY+8)
{
VISIBLE_SPRITE_ARRAY[t8[0]] = ((uint32_t *) (MEM + 0xFE00))[i];
t8[0]++;
if(t8[0] == 10) break;
}
}

ppu_cycle = 80;
goto loop;
}


if(!LX)
{
t32[0] = (uint32_t) (16 * MEM[0x9800 + (LX / 8) + (LY / 8) * 32]);
t8p = (void*) MEM;
t8p = (void *) MEM;
t8p += 0x8000;
t8p += t32[0];
t8p += 2 * (LY % 8);
for(uint8_t mask = 0x80; mask != 0; mask >>= 1u)
{
(((uint16_t*) (&fifo))[1]) |= (uint16_t) (t8p[1] & mask);
(((uint16_t*) (&fifo))[1]) <<= 1u;
(((uint16_t*) (&fifo))[1]) |= (uint16_t) (t8p[0] & mask);
(((uint16_t *) (&fifo))[1]) |= (uint16_t) (t8p[1] & mask);
(((uint16_t *) (&fifo))[1]) <<= 1u;
(((uint16_t *) (&fifo))[1]) |= (uint16_t) (t8p[0] & mask);
}
}
if(!(LX % 8))
{
t32[0] = (uint32_t) (16 * MEM[0x9800 + (LX / 8) + 1 + (LY / 8) * 32]);
t8p = (void*) MEM;
t8p = (void *) MEM;
t8p += 0x8000;
t8p += t32[0];
t8p += 2 * (LY % 8);
for(uint8_t mask = 0x80; mask != 0; mask >>= 1u)
{
(((uint16_t*) (&fifo))[0]) |= (uint16_t) (t8p[1] & mask);
(((uint16_t*) (&fifo))[0]) <<= 1u;
(((uint16_t*) (&fifo))[0]) |= (uint16_t) (t8p[0] & mask);
(((uint16_t *) (&fifo))[0]) |= (uint16_t) (t8p[1] & mask);
(((uint16_t *) (&fifo))[0]) <<= 1u;
(((uint16_t *) (&fifo))[0]) |= (uint16_t) (t8p[0] & mask);
}
}

// check for Sprites and Window
if(LY - SCY >= 0 && LY - SCY < HEIGHT && (int32_t) LX - SCX >= 0 && LX - SCX < WIDTH)
{
// check for sprite
// load pixel
// todo: #23: pixel mixing
OUTPUT_ARRAY[WIDTH * (LY - SCY) + (LX - SCX)] = (0xC0000000 & fifo) >> 30u;
}
LX++;
Expand Down Expand Up @@ -1689,7 +1713,7 @@ int main(int argc, char** argv)

display_draw(OUTPUT_ARRAY);

sleep(3);
sleep(300);
goto end;

fprintf(stderr, ".");
Expand Down Expand Up @@ -1748,7 +1772,7 @@ void print_mem(uint16_t low, uint16_t high, char mode)
// mode b for binary output
if(mode == 'b')
{
uint8_t* buf = malloc(sizeof(uint8_t) * 8);
uint8_t *buf = malloc(sizeof(uint8_t) * 8);
for(int i = 0; i < n; i++)
{
buf[0] = (uint8_t) (MEM[low + i] & 1u ? 1u : 0u);
Expand All @@ -1769,7 +1793,7 @@ void print_mem(uint16_t low, uint16_t high, char mode)
}
}

void read_header(const uint8_t* buf)
void read_header(const uint8_t *buf)
{
// read GAME_NAME
GAME_NAME = malloc(17 * sizeof(char));
Expand All @@ -1781,7 +1805,7 @@ void read_header(const uint8_t* buf)
GAME_NAME[i] = '\0';

//read cartridgetype
char* cartridgetype = "unreadable";
char *cartridgetype = "unreadable";
if(buf[0x147] == 0x00){cartridgetype = "ROM ONLY";}
if(buf[0x147] == 0x01){cartridgetype = "MBC1";}
if(buf[0x147] == 0x02){cartridgetype = "MBC1+RAM";}
Expand Down Expand Up @@ -1822,10 +1846,10 @@ void read_header(const uint8_t* buf)
printf("Name: %s\nCartridgetype: %s\nBanks:%i\nRAM Size:%i\n", GAME_NAME, cartridgetype, banks, ramsize);
}

int readfff(uint8_t* buffer, const char* filename)
int readfff(uint8_t *buffer, const char *filename)
{
int err;
FILE* cartridge = fopen(filename, "r");
FILE *cartridge = fopen(filename, "r");
if(!cartridge)
{
fprintf(stderr, "%s failed: fopen returned NULL\n", __func__);
Expand Down Expand Up @@ -1870,8 +1894,8 @@ int readfff(uint8_t* buffer, const char* filename)

void create_coredump(uint32_t length, uint16_t coredumpnum)
{
FILE* coredump;
char* path = malloc(sizeof(char) * 64);
FILE *coredump;
char *path = malloc(sizeof(char) * 64);
sprintf(path, "./coredumps/coredump%u.dmp", coredumpnum);
coredump = fopen(path, "w");

Expand All @@ -1884,8 +1908,8 @@ void create_coredump(uint32_t length, uint16_t coredumpnum)

void reset_coredump(uint32_t length, uint16_t coredumpnum)
{
FILE* coredump;
char* path = malloc(sizeof(char) * 64);
FILE *coredump;
char *path = malloc(sizeof(char) * 64);
sprintf(path, "./coredumps/coredump%u.dmp", coredumpnum);
coredump = fopen(path, "r");

Expand All @@ -1900,13 +1924,13 @@ void remove_all_coredumps(uint16_t coredumpnum)
{
for(uint16_t i = 0; i < coredumpnum + 1; i++)
{
char* txt = malloc(sizeof(char) * 64);
char *txt = malloc(sizeof(char) * 64);
sprintf(txt, "./coredumps/coredump%u.dmp", i);
remove(txt);
}
}

void convert_tile(uint8_t* input_ptr, uint32_t* output_ptr)
void convert_tile(uint8_t *input_ptr, uint32_t *output_ptr)
{
for(int i = 0; i < 8; i++)
{
Expand All @@ -1915,7 +1939,7 @@ void convert_tile(uint8_t* input_ptr, uint32_t* output_ptr)

}

void convert_line(const uint8_t* input_ptr, uint32_t* output_ptr)
void convert_line(const uint8_t *input_ptr, uint32_t *output_ptr)
{
for(int i = 0; i < 8; i++)
{
Expand All @@ -1926,7 +1950,7 @@ void convert_line(const uint8_t* input_ptr, uint32_t* output_ptr)

void background_tiles()
{
uint32_t* pixel = malloc(BG_DBG_WINDOW_HEIGHT * BG_DBG_WINDOW_WIDTH * sizeof(uint32_t));
uint32_t *pixel = malloc(BG_DBG_WINDOW_HEIGHT * BG_DBG_WINDOW_WIDTH * sizeof(uint32_t));
size_t i = 0;

for(i = 0; i < BG_DBG_WINDOW_WIDTH * BG_DBG_WINDOW_HEIGHT; ++i)
Expand Down
1 change: 1 addition & 0 deletions source/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void* reg_ptr[12];
uint32_t pixelcounter;

uint32_t fifo;
uint32_t VISIBLE_SPRITE_ARRAY[10];
uint32_t OUTPUT_ARRAY[WIDTH * HEIGHT];
uint8_t* MEM;
uint32_t DEFAULT_PALETTE[4] = {WHITE, LIGHT_GREY, DARK_GREY, BLACK};
Expand Down

0 comments on commit 0c6d7e0

Please sign in to comment.