From 765dc3ab3d79bdca696a0d651385af3020c895c6 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sun, 28 May 2023 18:54:03 +0200 Subject: Add initial link cable clock sync --- Makefile | 5 +++++ src/gba/gba.h | 18 ++++++++++++++++++ src/sequencer.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/Makefile b/Makefile index 90b0a0d..5ccc725 100644 --- a/Makefile +++ b/Makefile @@ -81,6 +81,11 @@ $(BUILD_DIR): run: main mgba-qt $(BIN) +deploy: + mount /dev/sdf1 /mnt + cp $(BIN) /mnt + umount /mnt + # Remove build directory. clean: rm -rf $(BUILD_DIR) diff --git a/src/gba/gba.h b/src/gba/gba.h index be7a305..f726b72 100644 --- a/src/gba/gba.h +++ b/src/gba/gba.h @@ -451,6 +451,24 @@ void irq_init(void); int bios_vblank_wait(); int bios_div(int num, int denom); +// +// SIO Link Cable +// + +#define SIO_MODE *((vu16*)(MEM_IO + 0x0134)) +#define SIO_CNT *((vu16*)(MEM_IO + 0x0128)) + +#define SIO_MODE_GP (2 << 14) +#define SIO_SC(X) ((X) << 0) +#define SIO_SD(X) ((X) << 1) +#define SIO_SI(X) ((X) << 2) +#define SIO_SO(X) ((X) << 3) +#define SIO_SC_OUT(X) ((X) << 4) +#define SIO_SD_OUT(X) ((X) << 5) +#define SIO_SI_OUT(X) ((X) << 6) +#define SIO_SO_OUT(X) ((X) << 7) +#define SIO_IRQ_ENABLE (1 << 8) + // // Sound. // diff --git a/src/sequencer.c b/src/sequencer.c index 867e686..7c989f2 100644 --- a/src/sequencer.c +++ b/src/sequencer.c @@ -19,6 +19,19 @@ bool redraw_bpm = true; bool redraw_piano_note = true; bool update_bpm = false; +void +gate_off(void) { + SIO_MODE = SIO_MODE_GP + | SIO_SC_OUT(1) + | SIO_SD_OUT(1) + | SIO_SI_OUT(0) + | SIO_SO_OUT(1) + | SIO_SC(0) + | SIO_SD(1) + | SIO_SO(0); + TIMER_CTRL_1 = 0; +} + void play_step(void) { Pattern *pat = &patterns[current_pattern]; @@ -135,6 +148,22 @@ play_step(void) { SOUND_NOISE_CTRL = 0; SOUND_NOISE_FREQ = 0; } + // TODO: This should be configurable e.g. po needs a 1 each 2 to sync + // exactly, but we can also just want a pseudo clock divider. + if (step_counter % 2 == 0) { + SIO_MODE = SIO_MODE_GP + | SIO_SC_OUT(1) + | SIO_SD_OUT(1) + | SIO_SI_OUT(0) + | SIO_SO_OUT(1) + | SIO_SC(1) + | SIO_SD(1) + | SIO_SO(0); + int n_ticks = -244181 / 600; + irs_set(IRQ_TIMER_1, gate_off); + TIMER_DATA_1 = n_ticks; + TIMER_CTRL_1 = TIMER_CTRL_IRQ | TIMER_CTRL_ENABLE | TIMER_CTRL_FREQ_3; + } step_counter = (step_counter + 1) % 16; redraw_piano_note = true; } -- cgit v1.2.1