added SDL

This commit is contained in:
Lauchmelder 2021-10-23 20:36:38 +02:00
parent 7c0957325f
commit 2b71e3e0d2
8 changed files with 85 additions and 22 deletions

View file

@ -1,9 +1,6 @@
# CMakeList.txt: CMake-Projektdatei der obersten Ebene. Führen Sie hier die globale Konfiguration aus, cmake_minimum_required (VERSION 3.8)
# und schließen Sie Unterprojekte ein.
#
cmake_minimum_required (VERSION 3.8)
project ("NES Emulator") project ("NES Emulator")
# Schließen Sie Unterprojekte ein. add_subdirectory ("vendor/SDL")
add_subdirectory ("NES Emulator") add_subdirectory ("NES Emulator")

View file

@ -2,6 +2,9 @@
add_executable (nesemu "main.c" "cpu.h" "types.h" "bus.h" "bus.c" "cartridge.h" "cpu.c" "cartridge.c" "opcodes.c" "log.c" "ppu.h" "ppu.c") add_executable (nesemu "main.c" "cpu.h" "types.h" "bus.h" "bus.c" "cartridge.h" "cpu.c" "cartridge.c" "opcodes.c" "log.c" "ppu.h" "ppu.c")
target_include_directories(nesemu PUBLIC SDL2-static)
target_link_libraries(nesemu PUBLIC SDL2-static SDL2main)
if(MSVC) if(MSVC)
target_compile_definitions(nesemu PUBLIC _CRT_SECURE_NO_WARNINGS) target_compile_definitions(nesemu PUBLIC _CRT_SECURE_NO_WARNINGS)
endif() endif()

View file

@ -42,7 +42,7 @@ struct Bus* createBus()
// Create PPU and attack it // Create PPU and attack it
bus->ppu = createPPU(bus); bus->ppu = createPPU(bus);
printf("Reset vector: $%x\n", ((Word)readCartridge(bus->cartridge, 0xFFFD) << 8) | (readCartridge(bus->cartridge, 0xFFFC))); bus->masterClockTimer = 0;
return bus; return bus;
} }
@ -115,10 +115,26 @@ void writeBus(struct Bus* bus, Word addr, Byte val)
} }
void tick(struct Bus* bus) int tick(struct Bus* bus)
{ {
for (int i = 0; i < 3; i++) bus->masterClockTimer++;
tickPPU(bus->ppu);
tickCPU(bus->cpu); tickPPU(bus->ppu);
if (bus->masterClockTimer == 3)
{
tickCPU(bus->cpu);
bus->masterClockTimer = 0;
}
}
int doInstruction(struct Bus* bus)
{
while(bus->cpu->remainingCycles > 0)
tick(bus);
tick(bus);
}
int doFrame(struct Bus* bus)
{
do tick(bus); while (bus->ppu->x != 0 || bus->ppu->y != 0);
} }

View file

@ -16,6 +16,8 @@ struct Bus
struct CPU* cpu; struct CPU* cpu;
struct PPU* ppu; struct PPU* ppu;
struct Cartridge* cartridge; struct Cartridge* cartridge;
Byte masterClockTimer;
}; };
// Sets up the Bus, allocates memory and creates devices // Sets up the Bus, allocates memory and creates devices
@ -30,6 +32,8 @@ void writeBus(struct Bus* bus, Word addr, Byte val);
// Ticks the master clock 12 times (i.e. 1 CPU tick & 3 PPU dots) // Ticks the master clock 12 times (i.e. 1 CPU tick & 3 PPU dots)
void tick(struct Bus* bus); int tick(struct Bus* bus);
int doInstruction(struct Bus* bus);
int doFrame(struct Bus* bus);
#endif // _BUS_H_ #endif // _BUS_H_

View file

@ -25,7 +25,7 @@ struct CPU* createCPU(struct Bus* parent)
} }
// TODO: THIS IS JUST FOR THE TEST ROM // TODO: THIS IS JUST FOR THE TEST ROM
// cpu->pc.word = 0xC000; cpu->pc.word = 0xC000;
cpu->pc.word = ((Word)readBus(parent, 0xFFFD) << 8) | readBus(parent, 0xFFFC); cpu->pc.word = ((Word)readBus(parent, 0xFFFD) << 8) | readBus(parent, 0xFFFC);
cpu->status.raw = 0x34; cpu->status.raw = 0x34;
@ -189,7 +189,7 @@ void fetch(struct CPU* cpu)
void execute(struct CPU* cpu) void execute(struct CPU* cpu)
{ {
LOG_BUS(cpu->bus); // LOG_BUS(cpu->bus);
switch (cpu->currentOpcode->op) switch (cpu->currentOpcode->op)
{ {

View file

@ -1,15 +1,55 @@
#include "bus.h" #include "bus.h"
int main() #include <stdio.h>
#include <SDL.h>
int main(int argc, char** argv)
{ {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("NES Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 800, SDL_WINDOW_SHOWN);
if (window == NULL)
{
fprintf(stderr, "Failed to create SDL_Window.\n");
exit(1);
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
fprintf(stderr, "Failed to create SDL_Renderer\n");
exit(1);
}
struct Bus* bus = createBus(); struct Bus* bus = createBus();
for (;;) SDL_Event event;
int running = 1;
while(running)
{ {
tick(bus); while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_WINDOWEVENT:
{
switch (event.window.event)
{
case SDL_WINDOWEVENT_CLOSE:
running = 0;
break;
}
} break;
}
}
doFrame(bus);
} }
destroyBus(bus); destroyBus(bus);
SDL_Quit();
return 0; return 0;
} }

View file

@ -76,8 +76,10 @@ void destroyPPU(struct PPU* ppu)
{ {
free(ppu->oam); free(ppu->oam);
free(ppu->paletteIndexes); free(ppu->paletteIndexes);
free(ppu->nametables); for(int i = 0; i < 4; i++)
free(ppu->patternTables); free(ppu->nametables[i]);
for (int i = 0; i < 2; i++)
free(ppu->patternTables[i]);
free(ppu); free(ppu);
} }
@ -152,7 +154,7 @@ void ppuWrite(struct PPU* ppu, Word addr, Byte val)
} }
} }
void tickPPU(struct PPU* ppu) int tickPPU(struct PPU* ppu)
{ {
ppu->x++; ppu->x++;
@ -164,4 +166,6 @@ void tickPPU(struct PPU* ppu)
if (ppu->y == 261) if (ppu->y == 261)
ppu->y = 0; ppu->y = 0;
} }
return (ppu->x == 0 && ppu->y == 0);
} }

View file

@ -2,6 +2,7 @@
#define _PPU_H_ #define _PPU_H_
#include "types.h" #include "types.h"
#include "SDL.h"
struct Bus; struct Bus;
@ -94,8 +95,6 @@ struct PPU
Word x, y; Word x, y;
struct Bus* bus; struct Bus* bus;
}; };
@ -105,6 +104,6 @@ void destroyPPU(struct PPU* ppu);
Byte ppuRead(struct PPU* ppu, Word addr); Byte ppuRead(struct PPU* ppu, Word addr);
void ppuWrite(struct PPU* ppu, Word addr, Byte val); void ppuWrite(struct PPU* ppu, Word addr, Byte val);
void tickPPU(struct PPU* ppu); int tickPPU(struct PPU* ppu);
#endif // _PPU_H_ #endif // _PPU_H_