Skip to content

Commit

Permalink
Return bit map result code when executing a clock (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
kremi151 committed Aug 12, 2020
1 parent 02f35ae commit d8731eb
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 23 deletions.
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ set(HEADERS
source/util/registers.h
source/util/flags.h
source/util/gpumode.h
source/util/return_codes.h
)

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DFB_DEBUG")
Expand Down
21 changes: 11 additions & 10 deletions core/source/emulator/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <utility>
#include <util/typedefs.h>
#include <util/registers.h>
#include <util/return_codes.h>
#include <emulator/io_registers.h>

using namespace FunkyBoy;
Expand Down Expand Up @@ -144,9 +145,9 @@ void CPU::powerUpInit() {
ioRegisters.updateJoypad();
}

bool CPU::doMachineCycle() {
ret_code CPU::doMachineCycle() {
doJoypad();
bool result = doCycle();
auto result = doCycle();

// TODO: Handle timer here or earlier?
doTimers(4);
Expand All @@ -167,9 +168,9 @@ bool CPU::doMachineCycle() {
return result;
}

bool CPU::doCycle() {
ret_code CPU::doCycle() {
if (instrContext.cpuState == CPUState::STOPPED) {
return true;
return FB_RET_SUCCESS | FB_RET_SKIPPED;
}

bool shouldDoInterrupts = instrContext.cpuState == CPUState::HALTED;
Expand Down Expand Up @@ -207,17 +208,17 @@ bool CPU::doCycle() {

if (isDMA) {
memory->doDMA();
return true;
return FB_RET_SUCCESS;
} else if (!shouldFetch) {
return true;
return FB_RET_SUCCESS;
}

bool result = doFetchAndDecode();
auto result = doFetchAndDecode();
operandIndex = 0;
return result;
}

bool CPU::doFetchAndDecode() {
ret_code CPU::doFetchAndDecode() {
if (!instrContext.haltBugRequested) {
instrContext.instr = memory->read8BitsAt(instrContext.progCounter++);
} else {
Expand All @@ -232,9 +233,9 @@ bool CPU::doFetchAndDecode() {

if (!Operands::decodeOpcode(instrContext.instr, operands)) {
fprintf(stderr, "Illegal instruction 0x%02X at 0x%04X\n", instrContext.instr, instrContext.progCounter - 1);
return false;
return 0;
}
return true;
return FB_RET_SUCCESS;
}

inline memory_address getInterruptStartAddress(InterruptType type) {
Expand Down
6 changes: 3 additions & 3 deletions core/source/emulator/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ namespace FunkyBoy {

void powerUpInit();

bool doCycle();
bool doFetchAndDecode();
ret_code doCycle();
ret_code doFetchAndDecode();

void doJoypad();
bool doInterrupts();
Expand Down Expand Up @@ -100,7 +100,7 @@ namespace FunkyBoy {
void setProgramCounter(u16 offset);
void requestInterrupt(InterruptType type);

bool doMachineCycle();
ret_code doMachineCycle();
};

typedef std::shared_ptr<CPU> CPUPtr;
Expand Down
11 changes: 6 additions & 5 deletions core/source/emulator/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ CartridgeStatus Emulator::loadGame(const fs::path &romPath) {
return cartridge->getStatus();
}

bool Emulator::doTick() {
if (!cpu->doMachineCycle()) {
return false;
ret_code Emulator::doTick() {
auto result = cpu->doMachineCycle();
if (!result) {
return 0;
}
ppu.doClocks(4);
return true;
result |= ppu.doClocks(4);
return result;
}

Cartridge & Emulator::getCartridge() {
Expand Down
3 changes: 2 additions & 1 deletion core/source/emulator/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define CORE_EMULATOR_H

#include <util/fs.h>
#include <util/return_codes.h>
#include <memory>
#include <emulator/cpu.h>
#include <emulator/ppu.h>
Expand Down Expand Up @@ -47,7 +48,7 @@ namespace FunkyBoy {
CartridgeStatus loadGame(const fs::path &romPath);
Cartridge &getCartridge();

bool doTick();
ret_code doTick();
};

}
Expand Down
10 changes: 7 additions & 3 deletions core/source/emulator/ppu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "ppu.h"

#include <util/typedefs.h>
#include <util/return_codes.h>
#include <emulator/io_registers.h>

#define FB_TILE_DATA_OBJ 0x8000
Expand Down Expand Up @@ -61,17 +61,18 @@ PPU::~PPU() {
//
// In total for 155 scanlines => 70224 clocks

void PPU::doClocks(u8 clocks) {
ret_code PPU::doClocks(u8 clocks) {
// TODO: Finish implementation
// See https://gbdev.gg8.se/wiki/articles/Video_Display#VRAM_Tile_Data
// See http://marc.rawer.de/Gameboy/Docs/GBCPUman.pdf (pages 22-27)

auto lcdc = memory->read8BitsAt(FB_REG_LCDC);
if (!__fb_lcdc_isOn(lcdc)) {
ioRegisters.updateLCD(false, gpuMode /* TODO: Correct* */, 0x00);
return; // TODO: Correct?
return FB_RET_SUCCESS; // TODO: Correct?
}

ret_code result = FB_RET_SUCCESS;
auto ly = memory->read8BitsAt(FB_REG_LY);

modeClocks += clocks;
Expand All @@ -83,6 +84,7 @@ void PPU::doClocks(u8 clocks) {
gpuMode = GPUMode::GPUMode_1;
controllers->getDisplay()->drawScreen();
cpu->requestInterrupt(InterruptType::VBLANK);
result |= FB_RET_NEW_FRAME;
} else {
gpuMode = GPUMode::GPUMode_2;
}
Expand Down Expand Up @@ -119,6 +121,8 @@ void PPU::doClocks(u8 clocks) {
// TODO: Compare LY with LYC and trigger interrupt

ioRegisters.updateLCD(true, gpuMode, ly);

return result;
}

void PPU::renderScanline(u8 ly) {
Expand Down
3 changes: 2 additions & 1 deletion core/source/emulator/ppu.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <emulator/cpu.h>
#include <emulator/io_registers.h>
#include <util/gpumode.h>
#include <util/typedefs.h>

namespace FunkyBoy {

Expand All @@ -43,7 +44,7 @@ namespace FunkyBoy {
PPU(MemoryPtr memory, CPUPtr cpu, Controller::ControllersPtr controllers, const io_registers& ioRegisters);
~PPU();

void doClocks(u8 clocks);
ret_code doClocks(u8 clocks);
};

}
Expand Down
24 changes: 24 additions & 0 deletions core/source/util/return_codes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright 2020 Michel Kremer (kremi151)
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FB_CORE_RETURN_CODES_H
#define FB_CORE_RETURN_CODES_H

#define FB_RET_SUCCESS 1
#define FB_RET_SKIPPED (1 << 1)
#define FB_RET_NEW_FRAME (1 << 2)

#endif //FB_CORE_RETURN_CODES_H
2 changes: 2 additions & 0 deletions core/source/util/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ namespace FunkyBoy {
typedef int32_t i32;
typedef int64_t i64;

typedef int ret_code;

}

#endif //FUNKYBOY_CORE_TYPEDEFS_H

0 comments on commit d8731eb

Please sign in to comment.