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 Dec 23, 2019
2 parents 67bb27d + 9bb1e61 commit ab6bd36
Show file tree
Hide file tree
Showing 40 changed files with 1,021 additions and 495 deletions.
2 changes: 1 addition & 1 deletion arm7/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ endif

LDFLAGS = -specs=../ds_arm7_gbarunner.specs -g $(ARCH) -Wl,-Map,$(notdir $*).map

LIBS := -lnds7
LIBS := # -lnds7

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
Expand Down
22 changes: 16 additions & 6 deletions arm7/source/irq.s
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,32 @@ my_irq_handler:

//timer used for sound
ands r2, r1, #(1 << 6)
bne my_irq_handler_timer3
bne timer3

//timer used for gb sound
ands r2, r1, #(1 << 3)
bne my_irq_handler_timer0
bne timer0

//vblank
ands r2, r1, #1
bne vblank

//timer used for saving
ands r2, r1, #(1 << 4)
bne my_irq_handler_timer1
bne timer1

ands r2, r1, #(1 << 24)
bne my_irq_handler_wifi

pop {lr}
bx lr

vblank:
str r2, [r0, #0x214]
bl irq_vblank
pop {lr}
bx lr

//my_irq_handler_hblank:
// str r2, [r0, #0x214]
// bl hblank_irq
Expand All @@ -45,19 +55,19 @@ my_irq_handler:
// pop {lr}
// bx lr

my_irq_handler_timer0:
timer0:
str r2, [r0, #0x214]
bl gbs_frameSeqTick
pop {lr}
bx lr

my_irq_handler_timer1:
timer1:
str r2, [r0, #0x214]
bl timer1_overflow_irq
pop {lr}
bx lr

my_irq_handler_timer3:
timer3:
str r2, [r0, #0x214]
bl timer3_overflow_irq
pop {lr}
Expand Down
37 changes: 33 additions & 4 deletions arm7/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,25 @@
#include "wifi/wifi.h"
#include "wifi/wifi_tx.h"
#include "sio/sio.h"
#include "rtc.h"

static void vblank_handler()
static bool sPrevTouchDown = false;

extern "C" void __libnds_exit()
{

}

extern "C" void irq_vblank()
{
bool touchDown = (REG_KEYXY & (1 << 6)) == 0;
if(!sPrevTouchDown && touchDown)
{
//invoke irq on arm9
vram_cd->openMenuIrqFlag = 1;
REG_IPC_SYNC |= IPC_SYNC_IRQ_REQUEST;
}
sPrevTouchDown = touchDown;
}

extern "C" void my_irq_handler();
Expand Down Expand Up @@ -112,6 +128,9 @@ int main()
gbs_init();
gba_save_init();

//set vblank irq
REG_DISPSTAT |= DISP_VBLANK_IRQ;
REG_IE |= IRQ_VBLANK;

//irqSet(IRQ_VBLANK, vblank_irq_handler);
//irqEnable(IRQ_VBLANK);
Expand Down Expand Up @@ -147,7 +166,7 @@ int main()
{
while (REG_FIFO_CNT & FIFO_CNT_EMPTY);
{
if (!(*((vu32*)0x04000136) & 1))
if (!(REG_KEYXY & 1))
gba_sound_resync();
}

Expand Down Expand Up @@ -229,16 +248,26 @@ int main()
}
break;
}
case 0xAA5500FF://set master volume
{
while (REG_FIFO_CNT & FIFO_CNT_EMPTY);
val = REG_RECV_FIFO;
if(val & 0x80000000)
gba_sound_resync();
int volume = val & 0x7F;
REG_SOUNDCNT = (REG_SOUNDCNT & ~0x7F) | volume;
break;
}
case 0xAA560000: //sio reinit
sio_init();
break;
case 0xAA550100: //get rtc data
{
u8 dateTime[8];
u8 cmd = READ_TIME_AND_DATE;
rtcTransaction(&cmd, 1, dateTime, 7);
rtc_doTransfer(&cmd, 1, dateTime, 7);
cmd = READ_STATUS_REG1;
rtcTransaction(&cmd, 1, &dateTime[7], 1);
rtc_doTransfer(&cmd, 1, &dateTime[7], 1);
REG_SEND_FIFO = *(u32*)&dateTime[0];
REG_SEND_FIFO = *(u32*)&dateTime[4];
break;
Expand Down
75 changes: 75 additions & 0 deletions arm7/source/rtc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <nds.h>
#include "swi.h"
#include "rtc.h"

// Delay (in swiDelay units) for each bit transfer
#define RTC_DELAY 48

// Pin defines on RTC_CR
#define CS_0 (1<<6)
#define CS_1 ((1<<6) | (1<<2))
#define SCK_0 (1<<5)
#define SCK_1 ((1<<5) | (1<<1))
#define SIO_0 (1<<4)
#define SIO_1 ((1<<4) | (1<<0))
#define SIO_out (1<<4)
#define SIO_in (1)

void rtc_doTransfer(uint8 * command, uint32 commandLength, uint8 * result, uint32 resultLength)
{
uint32 bit;
uint8 data;

// Raise CS
RTC_CR8 = CS_0 | SCK_1 | SIO_1;
swi_delay(RTC_DELAY);
RTC_CR8 = CS_1 | SCK_1 | SIO_1;
swi_delay(RTC_DELAY);

// Write command byte (high bit first)
data = *command++;

for (bit = 0; bit < 8; bit++) {
RTC_CR8 = CS_1 | SCK_0 | SIO_out | (data>>7);
swi_delay(RTC_DELAY);

RTC_CR8 = CS_1 | SCK_1 | SIO_out | (data>>7);
swi_delay(RTC_DELAY);

data = data << 1;
}
// Write parameter bytes (low bit first)
for ( ; commandLength > 1; commandLength--) {
data = *command++;

for (bit = 0; bit < 8; bit++) {
RTC_CR8 = CS_1 | SCK_0 | SIO_out | (data & 1);
swi_delay(RTC_DELAY);

RTC_CR8 = CS_1 | SCK_1 | SIO_out | (data & 1);
swi_delay(RTC_DELAY);

data = data >> 1;
}
}

// Read result bytes (low bit first)
for ( ; resultLength > 0; resultLength--) {
data = 0;

for (bit = 0; bit < 8; bit++) {
RTC_CR8 = CS_1 | SCK_0;
swi_delay(RTC_DELAY);

RTC_CR8 = CS_1 | SCK_1;
swi_delay(RTC_DELAY);

if (RTC_CR8 & SIO_in) data |= (1 << bit);
}
*result++ = data;
}

// Finish up by dropping CS low
RTC_CR8 = CS_0 | SCK_1;
swi_delay(RTC_DELAY);
}
3 changes: 3 additions & 0 deletions arm7/source/rtc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void rtc_doTransfer(uint8 * command, uint32 commandLength, uint8 * result, uint32 resultLength);
4 changes: 2 additions & 2 deletions arm7/source/save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern "C" void timer1_overflow_irq()
case SAVE_WORK_STATE_WAIT:
vram_cd->save_work.save_state = SAVE_WORK_STATE_SDSAVE;
//invoke an irq on arm9
*((vu32*)0x04000180) |= (1 << 13);
REG_IPC_SYNC |= IPC_SYNC_IRQ_REQUEST;
break;
case SAVE_WORK_STATE_SDSAVE:
break;
Expand All @@ -31,5 +31,5 @@ void gba_save_init()
REG_TM[1].CNT_H = 0;
REG_TM[1].CNT_L = TIMER_FREQ(2 * 1024);
REG_TM[1].CNT_H = REG_TMXCNT_H_E | REG_TMXCNT_H_I | REG_TMXCNT_H_PS_1024;
REG_IE |= (1 << 4);
REG_IE |= IRQ_TIMER(1);
}
84 changes: 76 additions & 8 deletions arm7/source/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ uint32_t srcAddress = 0;

u8 sChannelATimer = 0;
u8 sChannelAVolume = 1;
u8 sChannelAEnables = 0;

u16 sTimerReloadVals[4];
u16 sTimerControlVals[4];
Expand All @@ -44,13 +45,46 @@ static int sampcnter = 0;
}
}*/

static void calcChannelVolume(int channel, int& volumeL, int& volumeR)
{
if(channel == 1)
{
volumeL = 0;
volumeR = 0;
return;
}
if(sampleFreq <= 0 || sampleFreq >= 1000000)
{
volumeL = 0;
volumeR = 0;
}
int vol = sChannelAVolume == 1 ? 0x7F : 0x40;
if((sChannelAEnables & 1) || (sChannelAEnables & 2))
{
volumeL = vol;
volumeR = vol;
}
else
{
volumeL = 0;
volumeR = 0;
}
//todo: enable this when both channels are implemented
//volumeL = (sChannelAEnables & 1) ? vol : 0;
//volumeR = (sChannelAEnables & 2) ? vol : 0;
}

void gba_sound_init()
{
REG_SOUNDCNT = REG_SOUNDCNT_E | 0x7F;
REG_SOUNDBIAS = 0x200;
soundStarted = 0;
soundBufferWriteOffset = 0;
sampleFreq = 0;
vram_cd->sound_emu_work.req_read_ptr = 0;
vram_cd->sound_emu_work.req_write_ptr = 0;
vram_cd->sound_emu_work.resp_read_ptr = 0;
vram_cd->sound_emu_work.resp_write_ptr = 0;
//soundBufferVirtualWriteOffset = 0;
//soundBufferVirtualReadOffset = 0;
//REG_TM[2].CNT_H = 0;
Expand All @@ -60,9 +94,17 @@ void gba_sound_init()

void gba_sound_resync()
{
soundStarted = 0;
soundBufferWriteOffset = 0;
//REG_SOUND[0].CNT = 0;
int oldIrq = enterCriticalSection();
{
soundStarted = 0;
soundBufferWriteOffset = 0;
srcAddress = 0;
vram_cd->sound_emu_work.req_write_ptr = vram_cd->sound_emu_work.req_read_ptr;
vram_cd->sound_emu_work.resp_read_ptr = vram_cd->sound_emu_work.resp_write_ptr;
REG_SOUND[0].CNT = 0;
REG_SOUND[1].CNT = 0;
}
leaveCriticalSection(oldIrq);
}

static void gba_sound_update_ds_channels()
Expand All @@ -86,12 +128,13 @@ static void gba_sound_update_ds_channels()
REG_SOUND[1].LEN = SOUND_BUFFER_SIZE >> 2; //396 * 10;

//REG_TM[2].CNT_L = TIMER_FREQ(13378);

int volumeL, volumeR;
calcChannelVolume(0, volumeL, volumeR);
REG_SOUND[0].CNT = REG_SOUNDXCNT_E | REG_SOUNDXCNT_FORMAT(REG_SOUNDXCNT_FORMAT_PCM8) |
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) | REG_SOUNDXCNT_PAN(0) | REG_SOUNDXCNT_VOLUME(0x7F);
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) | REG_SOUNDXCNT_PAN(0) | REG_SOUNDXCNT_VOLUME(volumeL);
//SOUND_CHANNEL_0_SETTINGS;
REG_SOUND[1].CNT = REG_SOUNDXCNT_E | REG_SOUNDXCNT_FORMAT(REG_SOUNDXCNT_FORMAT_PCM8) |
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) | REG_SOUNDXCNT_PAN(127) | REG_SOUNDXCNT_VOLUME(0x7F);
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) | REG_SOUNDXCNT_PAN(127) | REG_SOUNDXCNT_VOLUME(volumeR);
//SOUND_CHANNEL_0_SETTINGS;
soundStarted = 1;
//REG_TM[2].CNT_H = REG_TMXCNT_H_E | REG_TMXCNT_H_I | REG_TMXCNT_H_PS_256;
Expand Down Expand Up @@ -135,6 +178,10 @@ extern "C" void timer3_overflow_irq()
}
else
{
for (int i = 0; i < FIFO_BLOCK_SIZE; i++)
soundBuffer[(soundBufferWriteOffset + i) & (SOUND_BUFFER_SIZE - 1)] = soundBuffer[(soundBufferWriteOffset - FIFO_BLOCK_SIZE + i) & (SOUND_BUFFER_SIZE - 1)];
soundBufferWriteOffset = (soundBufferWriteOffset + FIFO_BLOCK_SIZE) & (SOUND_BUFFER_SIZE - 1);
gba_sound_update_ds_channels();
/*vram_cd->sound_emu_work.req_queue[vram_cd->sound_emu_work.req_write_ptr] = srcAddress;
vram_cd->sound_emu_work.req_read_ptr++;
if (vram_cd->sound_emu_work.req_read_ptr >= SOUND_EMU_QUEUE_LEN)
Expand All @@ -160,6 +207,17 @@ extern "C" void timer3_overflow_irq()
sampcnter = 0;
}

static void updateChannelVolume(int channel)
{
if(channel == 0)
{
int volumeL, volumeR;
calcChannelVolume(0, volumeL, volumeR);
REG_SOUND[0].CNT = (REG_SOUND[0].CNT & ~REG_SOUNDXCNT_VOLUME(0x7F)) | REG_SOUNDXCNT_VOLUME(volumeL);
REG_SOUND[1].CNT = (REG_SOUND[1].CNT & ~REG_SOUNDXCNT_VOLUME(0x7F)) | REG_SOUNDXCNT_VOLUME(volumeR);
}
}

void gbas_updateChannelATimer()
{
int freq = 0;
Expand All @@ -176,10 +234,12 @@ void gbas_updateChannelATimer()
REG_IE |= (1 << 6);
REG_SOUND[0].TMR = sTimerReloadVals[sChannelATimer];
REG_SOUND[1].TMR = sTimerReloadVals[sChannelATimer];
int volumeL, volumeR;
calcChannelVolume(0, volumeL, volumeR);
REG_SOUND[0].CNT = REG_SOUNDXCNT_E | REG_SOUNDXCNT_FORMAT(REG_SOUNDXCNT_FORMAT_PCM8) |
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) | REG_SOUNDXCNT_PAN(0) | REG_SOUNDXCNT_VOLUME(0x7F);
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) | REG_SOUNDXCNT_PAN(0) | REG_SOUNDXCNT_VOLUME(volumeL);
REG_SOUND[1].CNT = REG_SOUNDXCNT_E | REG_SOUNDXCNT_FORMAT(REG_SOUNDXCNT_FORMAT_PCM8) |
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) | REG_SOUNDXCNT_PAN(127) | REG_SOUNDXCNT_VOLUME(0x7F);
REG_SOUNDXCNT_REPEAT(REG_SOUNDXCNT_REPEAT_LOOP) | REG_SOUNDXCNT_PAN(127) | REG_SOUNDXCNT_VOLUME(volumeR);
}
else
{
Expand Down Expand Up @@ -249,6 +309,11 @@ void gba_sound_fifo_write16(uint8_t* samps)
void gbas_updateVolume(u8 vol)
{
sChannelAVolume = (vol >> 2) & 1;
if(soundStarted)
{
updateChannelVolume(0);
updateChannelVolume(1);
}
}

void gbas_updateMixConfig(u8 mixConfig)
Expand All @@ -259,4 +324,7 @@ void gbas_updateMixConfig(u8 mixConfig)
sChannelATimer = newATimer;
gbas_updateChannelATimer();
}
sChannelAEnables = mixConfig & 3;
updateChannelVolume(0);
updateChannelVolume(1);
}
Loading

0 comments on commit ab6bd36

Please sign in to comment.