Skip to content

Commit

Permalink
Merge branch 'master' into wifi_link
Browse files Browse the repository at this point in the history
  • Loading branch information
Gericom committed Feb 17, 2020
2 parents ab6bd36 + c417fd0 commit e9d0372
Show file tree
Hide file tree
Showing 29 changed files with 778 additions and 294 deletions.
95 changes: 80 additions & 15 deletions arm7/source/gbsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,45 @@
//- https://problemkaputt.de/gbatek.htm
//- https://github.com/mgba-emu/mgba

static const u8 sLfsr7Table[] =
{
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7F, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7F, 0x7F, 0x80, 0x80,
0x80, 0x80, 0x7F, 0x80, 0x7F, 0x80, 0x80, 0x80, 0x7F, 0x7F, 0x7F, 0x7F, 0x80, 0x80, 0x7F, 0x80,
0x80, 0x80, 0x7F, 0x80, 0x7F, 0x7F, 0x80, 0x80, 0x7F, 0x7F, 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80,
0x80, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x80, 0x7F, 0x80, 0x80, 0x80, 0x80, 0x7F, 0x7F, 0x7F, 0x80,
0x80, 0x80, 0x7F, 0x80, 0x80, 0x7F, 0x80, 0x80, 0x7F, 0x7F, 0x80, 0x7F, 0x7F, 0x80, 0x7F, 0x80,
0x7F, 0x7F, 0x80, 0x7F, 0x7F, 0x7F, 0x7F, 0x80, 0x7F, 0x7F, 0x80, 0x80, 0x80, 0x7F, 0x7F, 0x80,
0x7F, 0x80, 0x80, 0x7F, 0x80, 0x7F, 0x7F, 0x7F, 0x80, 0x7F, 0x7F, 0x7F, 0x80, 0x80, 0x7F, 0x7F,
0x80, 0x80, 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F,

0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7F, 0x80, 0x80, 0x80, 0x80, 0x80, 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;

static int sChannelLength[4];
Expand Down Expand Up @@ -122,7 +161,7 @@ static void updateChannelFreq(int channel)
div = 4;
div *= 2 << sChannel4ShiftFreq;
int freq = 4194304 / div;
freq *= 8;
//freq *= 8;
if (freq == 0)
{
REG_SOUND[GB_CHANNEL_4_HW_L].TMR = 0;
Expand Down Expand Up @@ -349,20 +388,46 @@ static void startChannel(int channel)
case 3:
REG_SOUND[GB_CHANNEL_4_HW_L].CNT = 0;
REG_SOUND[GB_CHANNEL_4_HW_R].CNT = 0;
REG_SOUND[GB_CHANNEL_4_HW_L].CNT =
REG_SOUNDXCNT_E |
REG_SOUNDXCNT_FORMAT(REG_SOUNDXCNT_FORMAT_PSG_NOISE) |
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) |
REG_SOUNDXCNT_PAN(0) |
REG_SOUNDXCNT_SHIFT(sl) |
REG_SOUNDXCNT_VOLUME(vl);
REG_SOUND[GB_CHANNEL_4_HW_R].CNT =
REG_SOUNDXCNT_E |
REG_SOUNDXCNT_FORMAT(REG_SOUNDXCNT_FORMAT_PSG_NOISE) |
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) |
REG_SOUNDXCNT_PAN(127) |
REG_SOUNDXCNT_SHIFT(sr) |
REG_SOUNDXCNT_VOLUME(vr);
if(sChannel4Is7Bit)
{
REG_SOUND[GB_CHANNEL_4_HW_L].SAD = (u32)&sLfsr7Table[0];
REG_SOUND[GB_CHANNEL_4_HW_R].SAD = (u32)&sLfsr7Table[0];
REG_SOUND[GB_CHANNEL_4_HW_L].LEN = sizeof(sLfsr7Table) >> 2;
REG_SOUND[GB_CHANNEL_4_HW_R].LEN = sizeof(sLfsr7Table) >> 2;
REG_SOUND[GB_CHANNEL_4_HW_L].PNT = 0;
REG_SOUND[GB_CHANNEL_4_HW_L].PNT = 0;
REG_SOUND[GB_CHANNEL_4_HW_L].CNT =
REG_SOUNDXCNT_E |
REG_SOUNDXCNT_FORMAT(REG_SOUNDXCNT_FORMAT_PCM8) |
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) |
REG_SOUNDXCNT_PAN(0) |
REG_SOUNDXCNT_SHIFT(sl) |
REG_SOUNDXCNT_VOLUME(vl);
REG_SOUND[GB_CHANNEL_4_HW_R].CNT =
REG_SOUNDXCNT_E |
REG_SOUNDXCNT_FORMAT(REG_SOUNDXCNT_FORMAT_PCM8) |
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) |
REG_SOUNDXCNT_PAN(127) |
REG_SOUNDXCNT_SHIFT(sr) |
REG_SOUNDXCNT_VOLUME(vr);
}
else
{
REG_SOUND[GB_CHANNEL_4_HW_L].CNT =
REG_SOUNDXCNT_E |
REG_SOUNDXCNT_FORMAT(REG_SOUNDXCNT_FORMAT_PSG_NOISE) |
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) |
REG_SOUNDXCNT_PAN(0) |
REG_SOUNDXCNT_SHIFT(sl) |
REG_SOUNDXCNT_VOLUME(vl);
REG_SOUND[GB_CHANNEL_4_HW_R].CNT =
REG_SOUNDXCNT_E |
REG_SOUNDXCNT_FORMAT(REG_SOUNDXCNT_FORMAT_PSG_NOISE) |
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) |
REG_SOUNDXCNT_PAN(127) |
REG_SOUNDXCNT_SHIFT(sr) |
REG_SOUNDXCNT_VOLUME(vr);
}
break;
}
sChannelPlaying |= 1 << channel;
Expand Down
12 changes: 7 additions & 5 deletions arm7/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ extern "C" void __libnds_exit()

extern "C" void irq_vblank()
{
bool touchDown = (REG_KEYXY & (1 << 6)) == 0;
u16 keyXY = REG_KEYXY;
vram_cd->extKeys = ((keyXY & 3) << 10) | ((keyXY & 0xC0) << 6);
bool touchDown = (keyXY & (1 << 6)) == 0;
if(!sPrevTouchDown && touchDown)
{
//invoke irq on arm9
Expand Down Expand Up @@ -165,10 +167,10 @@ int main()
while (1)
{
while (REG_FIFO_CNT & FIFO_CNT_EMPTY);
{
if (!(REG_KEYXY & 1))
gba_sound_resync();
}
// {
// if (!(REG_KEYXY & 1))
// gba_sound_resync();
// }

u32 cmd = REG_RECV_FIFO;
//if((cmd >> 16) != 0xAA55)
Expand Down
7 changes: 4 additions & 3 deletions arm9/source/bios.s
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ gGbaBios:

.global bios_swiVeneer
bios_swiVeneer:
ldr pc,= (gGbaBios + 0x140)
b (gGbaBios + 0x140)
//b swiPatch

.pool
.space 0x4000 - (. - gGbaBios)

.space 0x4000 - (. - gGbaBios)
.pool
11 changes: 8 additions & 3 deletions arm9/source/bios.vram.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "vram.h"
#include "sd_access.h"
#include "fat/ff.h"
#include "patchUtil.h"
#include "bios.h"

extern "C" void bios_cpuset_cache_patch();
extern "C" void bios_cpufastset_cache_patch();
extern "C" void bios_softResetPatch();
extern "C" void bios_swiPatch();

/**
* \brief Relocates the gba bios so it can be executed from vram
Expand Down Expand Up @@ -89,9 +92,11 @@ static void applyRelocation()
*/
static void applyPatches()
{
//Make bios jump to 02040000
//todo: check if this is even needed anymore, it may be handled by prefetch abort just fine
//gGbaBios[0xCC >> 2] = 0xE3A0E781;
//patch for having the right protected op at boot
gGbaBios[0xDC >> 2] = pcu_makeArmBranch((u32)&gGbaBios[0xDC >> 2], (u32)&bios_softResetPatch);

//patch for having the right protected op after swi
gGbaBios[0x184 >> 2] = pcu_makeArmBranch((u32)&gGbaBios[0x184 >> 2], (u32)&bios_swiPatch);

//fix post boot redirect
//todo: maybe I should correctly implement that register instead
Expand Down
22 changes: 21 additions & 1 deletion arm9/source/bios_patches.s
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,24 @@ bios_cpufastset_cache_patch:
//don't use the armv5 interworking!
ldr r4,= (gGbaBios + 0xBC8)
bx r4
#endif
#endif

.global bios_softResetPatch
bios_softResetPatch:
//set the right protected bios opcode
ldr r0,= gBiosOp
ldr r1,= 0xE129F000
str r1, [r0]
mov r0, #0
mov r1, #0
bx lr

.global bios_swiPatch
bios_swiPatch:
ldr r11,= gBiosOp
ldr r12,= 0xE3A02004
str r12, [r11]
pop {r11, r12, lr}
movs pc, lr

.pool
15 changes: 12 additions & 3 deletions arm9/source/dtcm_data.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.section .dtcm2
.section ".dtcm2","ax"
.altmacro

#include "consts.s"
Expand Down Expand Up @@ -309,6 +309,15 @@ timer_shadow_regs_dtcm:
.short 0 //reload value
.endr

//the current bios opcode that is returned if you do a protected read
//[00DCh+8] = 0xE129F000, after startup and softreset //before this address 0x27C is read
//[0134h+8] = 0xE25EF004, during irq execution
//[013Ch+8] = 0xE55EC002, after irq execution
//[0188h+8] = 0xE3A02004, after swi execution; reads between 0x1C8 and 0x274
.global gBiosOp
gBiosOp:
.word 0xE3A02004

//for some reason the file is ignored without this nop here
nop
nop
@ nop
@ nop
14 changes: 6 additions & 8 deletions arm9/source/emu/handle_address_read.s
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

#include "consts.s"

//bios_op = 0xE129F000 //[00DCh+8] after startup and softreset //before this address 0x27C is read
//bios_op = 0xE25EF004 //[0134h+8] during irq execution
//bios_op = 0xE55EC002 //[013Ch+8] after irq execution
bios_op = 0xE3A02004 //[0188h+8] after swi execution; reads between 0x1C8 and 0x274

.global read_address_from_handler_32bit
read_address_from_handler_32bit:
cmp r9, #0x10000000
Expand Down Expand Up @@ -54,8 +49,9 @@ read_address_from_handler_bios_32:
cmp r10, #0x4000
ldrlt r10, [r9] //if the opcode is in the bios, read the data
bxlt lr
ldr r10,= bios_op
ldr r10,= gBiosOp
and r11, r9, #3
ldr r10, [r10]
mov r11, r11, lsl #3
mov r10, r10, ror r11
bx lr
Expand Down Expand Up @@ -208,7 +204,8 @@ read_address_from_handler_bios_16:
sub r10, #8
cmp r10, #0x4000
ldrlth r10, [r9] //if the opcode is in the bios, read the data
ldrge r10,= (bios_op & 0xFFFF)
ldrge r10,= gBiosOp
ldrgeh r10, [r10]
tst r9, #1
movne r10, r10, ror #8
bx lr
Expand Down Expand Up @@ -368,8 +365,9 @@ read_address_from_handler_bios_8:
cmp r10, #0x4000
ldrltb r10, [r9] //if the opcode is in the bios, read the data
bxlt lr
ldr r10,= bios_op
ldr r10,= gBiosOp
and r11, r9, #3
ldr r10, [r10]
mov r11, r11, lsl #3
mov r10, r10, ror r11
and r10, r10, #0xFF
Expand Down
8 changes: 4 additions & 4 deletions arm9/source/emu/handle_address_read_table.s
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ address_read_table_32bit:
//0x0400012C
.word read_address_undefined_memory_32
//0x04000130
.word read_address_nomod_32
.word inp_readGbaKeyinput32
//0x04000134-0x040001FC
.rept 51
.word read_address_ignore
Expand Down Expand Up @@ -278,7 +278,7 @@ address_read_table_16bit:
.word read_address_undefined_memory_16
.endr
//0x04000130
.word read_address_nomod_16
.word inp_readGbaKeyinput16
//0x04000132
.word read_address_nomod_16
//0x04000134
Expand Down Expand Up @@ -373,9 +373,9 @@ address_read_table_8bit:
.word read_address_ignore
.endr
//0x04000130
.word read_address_nomod_8
.word inp_readGbaKeyinput8lo
//0x04000131
.word read_address_nomod_8
.word inp_readGbaKeyinput8hi
//0x04000132
.word read_address_nomod_8
//0x04000133
Expand Down
Loading

0 comments on commit e9d0372

Please sign in to comment.