Skip to content

Commit

Permalink
Merge branch 'v0.2' of https://github.com/PSI-Rockin/CorgiDS into v0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon committed Jan 19, 2018
2 parents 0f37e7d + 450024d commit 47e3d56
Show file tree
Hide file tree
Showing 16 changed files with 752 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
build-CorgiDS-Desktop_Qt_5_9_1_clang_64bit-Debug/*
build-CorgiDS-Desktop_Qt_5_9_1_clang_64bit-Profile/*
build-CorgiDS-Desktop_Qt_5_9_1_clang_64bit-Release/*
*.gba
7 changes: 5 additions & 2 deletions CorgiDS/CorgiDS.pro
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ SOURCES += \
../src/disasm_thumb.cpp \
../src/debugger.cpp \
../src/slot2.cpp \
../src/gba/gbarw.cpp
../src/gba/gbarw.cpp \
../src/gba/gbadma.cpp \
../src/gba/gbagpu.cpp

DEFINES += QT_DEPRECATED_WARNINGS
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
Expand Down Expand Up @@ -83,7 +85,8 @@ HEADERS += \
../src/bios.hpp \
../src/qt/audiodevice.hpp \
../src/debugger.hpp \
../src/slot2.hpp
../src/slot2.hpp \
../src/gba/gbadma.hpp

FORMS += \
../src/qt/configwindow.ui \
Expand Down
15 changes: 13 additions & 2 deletions src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ void ARM_CPU::direct_boot(uint32_t entry_point)
}
}

void ARM_CPU::gba_boot(bool direct)
{
if (direct)
jp(GBA_ROM_START, true);
else
jp(0, true);
regs[13] = 0x03007F00;
SP_svc = 0x03007FE0;
SP_irq = 0x03007FA0;
CPSR.mode = PSR_MODE::SYSTEM;
}

void ARM_CPU::set_cp15(CP15 *cp)
{
cp15 = cp;
Expand All @@ -243,8 +255,7 @@ void ARM_CPU::execute()
//TODO: replace these comparisons with a generic "halt state" variable
if (halted)
{
//Wait until next event
timestamp = e->get_timestamp() << (1 - cpu_id);
timestamp += 4;
if (e->requesting_interrupt(cpu_id))
{
halted = false;
Expand Down
1 change: 1 addition & 0 deletions src/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class ARM_CPU
void set_cp15(CP15* cp);
void power_on();
void direct_boot(uint32_t entry_point);
void gba_boot(bool direct);
void run();
void execute();
void jp(uint32_t new_addr, bool change_thumb_state);
Expand Down
6 changes: 4 additions & 2 deletions src/disasm_thumb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ string Disassembler::disasm_thumb_load_store_imm(ARM_CPU &cpu, uint16_t instruct
{
stringstream output;
bool is_loading = instruction & (1 << 11);
bool is_byte = (instruction & (1 << 10));
bool is_byte = (instruction & (1 << 12));

uint32_t base = (instruction >> 3) & 0x7;
uint32_t source_dest = instruction & 0x7;
uint32_t offset = (instruction >> 6) & 0x7;
uint32_t offset = (instruction >> 6) & 0x1F;

if (is_loading)
output << "ldr";
Expand All @@ -298,6 +298,8 @@ string Disassembler::disasm_thumb_load_store_imm(ARM_CPU &cpu, uint16_t instruct

if (is_byte)
output << "b";
else
offset <<= 2;

output << " " << ARM_CPU::get_reg_name(source_dest) << ", [" << ARM_CPU::get_reg_name(base);
if (offset)
Expand Down
17 changes: 13 additions & 4 deletions src/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void POWCNT2_REG::set(uint8_t byte)
}

Emulator::Emulator() : arm7(this, 1), arm9(this, 0), arm9_cp15(this), cart(this), debugger(this), dma(this),
gpu(this), spi(this), spu(this), timers(this) {}
gba_dma(this), gpu(this), spi(this), spu(this), timers(this) {}

int Emulator::init()
{
Expand Down Expand Up @@ -341,6 +341,9 @@ void Emulator::run_gba()
while (!gpu.is_frame_complete())
{
arm7.execute();
uint64_t cycle_count = arm7.cycles_ran();

gpu.gba_run(cycle_count);
}
}

Expand All @@ -353,11 +356,12 @@ bool Emulator::is_gba()
void Emulator::start_gba_mode(bool throw_exception)
{
gba_mode = true;
arm7.jp(0, true);
debug();
arm7.gba_boot(true);
gba_dma.power_on();
//debug();
//Allocate VRAM C and D as 256 KB work RAM
gpu.set_VRAMCNT_C(0x82);
gpu.set_VRAMCNT_D(0x86);
gpu.set_VRAMCNT_D(0x8A);
if (throw_exception)
{
//Signal to the emulation thread that emulation has switched from NDS to GBA
Expand Down Expand Up @@ -451,6 +455,11 @@ void Emulator::request_interrupt9(INTERRUPT id)
int9_reg.IF |= 1 << static_cast<int>(id);
}

void Emulator::request_interrupt_gba(int id)
{
int7_reg.IF |= 1 << id;
}

void Emulator::get_upper_frame(uint32_t* buffer)
{
gpu.get_upper_frame(buffer);
Expand Down
3 changes: 3 additions & 0 deletions src/emulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#ifndef emulator_hpp
#define emulator_hpp
#include "gba/gbadma.hpp"
#include "bios.hpp"
#include "cartridge.hpp"
#include "cpu.hpp"
Expand Down Expand Up @@ -83,6 +84,7 @@ class Emulator
WiFi wifi;

bool gba_mode;
GBA_DMA gba_dma;

uint8_t main_RAM[1024 * 1024 * 4]; //4 MB
uint8_t shared_WRAM[1024 * 32]; //32 KB
Expand Down Expand Up @@ -212,6 +214,7 @@ class Emulator

void request_interrupt7(INTERRUPT id);
void request_interrupt9(INTERRUPT id);
void request_interrupt_gba(int id);

bool arm7_has_cart_rights();

Expand Down
154 changes: 154 additions & 0 deletions src/gba/gbadma.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include "../emulator.hpp"
#include "gbadma.hpp"

GBA_DMA::GBA_DMA(Emulator* e) : e(e)
{

}

void GBA_DMA::power_on()
{
active_DMAs = 0;
for (unsigned int i = 0; i < 4; i++)
dmas[i].CNT.set(0);
}

void GBA_DMA::handle_DMA(int index)
{
DMA* active_DMA = &dmas[index];
for (;;)
{
active_DMA->internal_len++;
if (active_DMA->internal_len > active_DMA->length)
{
/*if (active_DMA->CNT.IRQ_after_transfer)
{
e->request_interrupt7(static_cast<INTERRUPT>(4 + active_DMA->index));
}*/
active_DMA->internal_len = 0;
active_DMAs &= ~(1 << index);
//Reload dest
if (active_DMA->CNT.dest_control == 3)
active_DMA->internal_dest = active_DMA->destination;
if (!active_DMA->CNT.repeat)
active_DMA->CNT.enabled = false;
return;
}

uint32_t value;
int offset;
if (active_DMA->CNT.word_transfer)
{
offset = 4;
value = e->gba_read_word(active_DMA->internal_source);
e->gba_write_word(active_DMA->internal_dest, value);
}
else
{
offset = 2;
value = e->gba_read_halfword(active_DMA->internal_source);
e->gba_write_halfword(active_DMA->internal_dest, value);
}

switch (active_DMA->CNT.dest_control)
{
case 0:
case 3:
active_DMA->internal_dest += offset;
break;
case 1:
active_DMA->internal_dest -= offset;
break;
case 2:
//Fixed
break;
default:
printf("\nUnrecognized DMA dest control %d", active_DMA->CNT.dest_control);
}
switch (active_DMA->CNT.source_control)
{
case 0:
active_DMA->internal_source += offset;
break;
case 1:
active_DMA->internal_source -= offset;
break;
case 2:
//Fixed
break;
default:
printf("\nUnrecognized DMA source control %d", active_DMA->CNT.source_control);
}
}
}

uint16_t GBA_DMA::read_CNT(int index)
{
return dmas[index].CNT.get();
}

uint32_t GBA_DMA::read_len_CNT(int index)
{
return dmas[index].length | (dmas[index].CNT.get() << 16);
}

void GBA_DMA::write_source(int index, uint32_t source)
{
dmas[index].source = source;
}

void GBA_DMA::write_dest(int index, uint32_t dest)
{
dmas[index].destination = dest;
}

void GBA_DMA::write_len(int index, uint32_t len)
{
if (index == 3)
{
if (!len)
dmas[index].length = 0x10000;
else
dmas[index].length = len & 0xFFFF;
}
else
{
if (!len)
dmas[index].length = 0x4000;
else
dmas[index].length = len & 0x3FFF;
}
}

void GBA_DMA::write_CNT(int index, uint16_t CNT)
{
bool old_enabled = dmas[index].CNT.enabled;
dmas[index].CNT.set(CNT);
if (!old_enabled && (CNT & (1 << 15)))
{
dmas[index].internal_source = dmas[index].source;
dmas[index].internal_dest = dmas[index].destination;
dmas[index].internal_len = 0;
if (dmas[index].CNT.timing)
{
printf("\nDMA%d activated", index);
printf("\nSource: $%08X", dmas[index].source);
printf("\nDest: $%08X", dmas[index].destination);
printf("\nLen: %d", dmas[index].length);
printf("\nCNT: $%04X", dmas[index].CNT.get());
printf("\nTiming: %d", dmas[index].CNT.timing);
}

if (dmas[index].CNT.timing == 0)
{
//printf("\nRunning immediately");
handle_DMA(index);
}
}
}

void GBA_DMA::write_len_CNT(int index, uint32_t word)
{
write_len(index, word & 0xFFFF);
write_CNT(index, word >> 16);
}
27 changes: 27 additions & 0 deletions src/gba/gbadma.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef DMA_HPP
#define DMA_HPP
#include "../dma.hpp"

class GBA_DMA
{
private:
Emulator* e;
uint8_t active_DMAs;
DMA dmas[4];
public:
GBA_DMA(Emulator* e);

void power_on();
void handle_DMA(int index);

uint16_t read_CNT(int index);
uint32_t read_len_CNT(int index);

void write_source(int index, uint32_t source);
void write_dest(int index, uint32_t dest);
void write_len(int index, uint32_t len);
void write_CNT(int index, uint16_t CNT);
void write_len_CNT(int index, uint32_t word);
};

#endif // GBADMA_HPP
Loading

0 comments on commit 47e3d56

Please sign in to comment.