Skip to content

Commit

Permalink
bootloader/cardenginei: remove libnds7 dependency (#1747)
Browse files Browse the repository at this point in the history
* cardenginei: remove libnds7 dependency

- Needed calls are now inlined

* cardengine: arm7: remove unused swiDivMod

* bootloader: remove libnds7 dependency
  • Loading branch information
lifehackerhansol authored Nov 18, 2024
1 parent 9bbe833 commit a9f8b4b
Show file tree
Hide file tree
Showing 39 changed files with 1,563 additions and 42 deletions.
4 changes: 2 additions & 2 deletions retail/bootloader/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ endif
#---------------------------------------------------------------------------------
TARGET := load0
BUILD := build
SOURCES := source source/arm7 source/arm7/save_patches source/arm9 ../common/source ../common/source_general
SOURCES := source source/arm7 source/arm7/libnds source/arm7/save_patches source/arm9 ../common/source ../common/source_general
INCLUDES := include build ../common/include ../common/include_fat
DATA := ../data
SPECS := specs
Expand All @@ -42,7 +42,7 @@ CFLAGS += $(INCLUDE) -DARM7 -D_NO_SDMMC -DB4DS -DLOADERTYPE0 -std=gnu99
ASFLAGS := -g $(ARCH) $(INCLUDE)
LDFLAGS = -nostartfiles -T $(TOPDIR)/load.ld -g $(ARCH) -Wl,--nmagic -Wl,-Map,$(TARGET).map

LIBS := -lnds7
LIBS :=

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
Expand Down
2 changes: 1 addition & 1 deletion retail/bootloader/source/arm7/hook_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <string.h> // memcpy
#include <stdio.h>
#include <nds/system.h>
#include <nds/debug.h>

//#include "my_fat.h"
#include "debug_file.h"
Expand All @@ -32,6 +31,7 @@
#include "find.h"
#include "hook.h"
#include "tonccpy.h"
#include "nocashMessage.h"

#define b_a9IrqHooked BIT(7)
#define b_sleepMode BIT(17)
Expand Down
2 changes: 1 addition & 1 deletion retail/bootloader/source/arm7/hook_arm9.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <stdio.h>
#include <string.h>
#include <nds/ndstypes.h>
#include <nds/debug.h>
#include "patch.h"
#include "find.h"
#include "hook.h"
#include "common.h"
#include "cardengine_header_arm9.h"
#include "debug_file.h"
#include "nds_header.h"
#include "nocashMessage.h"

#define b_expansionPakFound BIT(0)
#define b_extendedMemory BIT(1)
Expand Down
49 changes: 49 additions & 0 deletions retail/bootloader/source/arm7/libnds/biosCalls.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*---------------------------------------------------------------------------------
Copyright (C) 2005
Michael Noland (joat)
Jason Rogers (dovoto)
Dave Murphy (WinterMute)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
---------------------------------------------------------------------------------*/
#include <nds/asminc.h>

.text
.align 4

.thumb

@---------------------------------------------------------------------------------
BEGIN_ASM_FUNC swiDelay
@---------------------------------------------------------------------------------
swi 0x03
bx lr

@---------------------------------------------------------------------------------
BEGIN_ASM_FUNC swiSleep
@---------------------------------------------------------------------------------
swi 0x07
bx lr

@---------------------------------------------------------------------------------
BEGIN_ASM_FUNC swiCRC16
@---------------------------------------------------------------------------------
swi 0x0E
bx lr
204 changes: 204 additions & 0 deletions retail/bootloader/source/arm7/libnds/codec.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*---------------------------------------------------------------------------------
DSi "codec" Touchscreen/Sound Controller control for ARM7
Copyright (C) 2017
fincs
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
---------------------------------------------------------------------------------*/

#include <nds/arm7/codec.h>

//---------------------------------------------------------------------------------
static u8 readTSC(u8 reg) {
//---------------------------------------------------------------------------------

while (REG_SPICNT & 0x80);

REG_SPICNT = SPI_ENABLE | SPI_BAUD_4MHz | SPI_DEVICE_TOUCH | SPI_CONTINUOUS;
REG_SPIDATA = 1 | (reg << 1);

while (REG_SPICNT & 0x80);

REG_SPICNT = SPI_ENABLE | SPI_BAUD_4MHz | SPI_DEVICE_TOUCH;
REG_SPIDATA = 0;

while (REG_SPICNT & 0x80);
return REG_SPIDATA;
}

//---------------------------------------------------------------------------------
static void writeTSC(u8 reg, u8 value) {
//---------------------------------------------------------------------------------

while (REG_SPICNT & 0x80);

REG_SPICNT = SPI_ENABLE | SPI_BAUD_4MHz | SPI_DEVICE_TOUCH | SPI_CONTINUOUS;
REG_SPIDATA = reg << 1;

while (REG_SPICNT & 0x80);

REG_SPICNT = SPI_ENABLE | SPI_BAUD_4MHz | SPI_DEVICE_TOUCH;
REG_SPIDATA = value;
}

//---------------------------------------------------------------------------------
static void bankSwitchTSC(u8 bank) {
//---------------------------------------------------------------------------------

static u8 curBank = 0x63;
if (bank != curBank) {
writeTSC(curBank == 0xFF ? 0x7F : 0x00, bank);
curBank = bank;
}
}

//---------------------------------------------------------------------------------
u8 cdcReadReg(u8 bank, u8 reg) {
//---------------------------------------------------------------------------------

bankSwitchTSC(bank);
return readTSC(reg);
}

//---------------------------------------------------------------------------------
void cdcReadRegArray(u8 bank, u8 reg, void* data, u8 size) {
//---------------------------------------------------------------------------------

u8* out = (u8*)data;
bankSwitchTSC(bank);

while (REG_SPICNT & 0x80);

REG_SPICNT = SPI_ENABLE | SPI_BAUD_4MHz | SPI_DEVICE_TOUCH | SPI_CONTINUOUS;
REG_SPIDATA = 1 | (reg << 1);

while (REG_SPICNT & 0x80);

for (; size > 1; size--) {
REG_SPIDATA = 0;
while (REG_SPICNT & 0x80);
*out++ = REG_SPIDATA;
}

REG_SPICNT = SPI_ENABLE | SPI_BAUD_4MHz | SPI_DEVICE_TOUCH;
REG_SPIDATA = 0;

while (REG_SPICNT & 0x80);

*out++ = REG_SPIDATA;
}

//---------------------------------------------------------------------------------
void cdcWriteReg(u8 bank, u8 reg, u8 value) {
//---------------------------------------------------------------------------------

bankSwitchTSC(bank);
writeTSC(reg, value);
}

//---------------------------------------------------------------------------------
void cdcWriteRegMask(u8 bank, u8 reg, u8 mask, u8 value) {
//---------------------------------------------------------------------------------

bankSwitchTSC(bank);
writeTSC(reg, (readTSC(reg) &~ mask) | (value & mask));
}

//---------------------------------------------------------------------------------
void cdcWriteRegArray(u8 bank, u8 reg, const void* data, u8 size) {
//---------------------------------------------------------------------------------

const u8* in = (u8*)data;
bankSwitchTSC(bank);

while (REG_SPICNT & 0x80);

REG_SPICNT = SPI_ENABLE | SPI_BAUD_4MHz | SPI_DEVICE_TOUCH | SPI_CONTINUOUS;
REG_SPIDATA = reg << 1;

while (REG_SPICNT & 0x80);

for (; size > 1; size--) {
REG_SPIDATA = *in++;
while (REG_SPICNT & 0x80);
}

REG_SPICNT = SPI_ENABLE | SPI_BAUD_4MHz | SPI_DEVICE_TOUCH;
REG_SPIDATA = *in++;
}

//---------------------------------------------------------------------------------
void cdcTouchInit(void) {
//---------------------------------------------------------------------------------

cdcWriteRegMask(CDC_TOUCHCNT, 0x0E, 0x80, 0<<7);
cdcWriteRegMask(CDC_TOUCHCNT, 0x02, 0x18, 3<<3);
cdcWriteReg (CDC_TOUCHCNT, 0x0F, 0xA0);
cdcWriteRegMask(CDC_TOUCHCNT, 0x0E, 0x38, 5<<3);
cdcWriteRegMask(CDC_TOUCHCNT, 0x0E, 0x40, 0<<6);
cdcWriteReg (CDC_TOUCHCNT, 0x03, 0x87);
cdcWriteRegMask(CDC_TOUCHCNT, 0x05, 0x07, 4<<0);
cdcWriteRegMask(CDC_TOUCHCNT, 0x04, 0x07, 6<<0);
cdcWriteRegMask(CDC_TOUCHCNT, 0x04, 0x70, 4<<4);
cdcWriteRegMask(CDC_TOUCHCNT, 0x12, 0x07, 0<<0);
cdcWriteRegMask(CDC_TOUCHCNT, 0x0E, 0x80, 1<<7);
}

//---------------------------------------------------------------------------------
bool cdcTouchPenDown(void) {
//---------------------------------------------------------------------------------

return (cdcReadReg(CDC_TOUCHCNT, 0x09) & 0xC0) != 0x40 && !(cdcReadReg(CDC_TOUCHCNT, 0x0E) & 0x02);
}

//---------------------------------------------------------------------------------
bool cdcTouchRead(touchPosition* pos) {
//---------------------------------------------------------------------------------

u8 raw[2*2*5];
u16 arrayX[5], arrayY[5];
u32 sumX, sumY;
int i;

cdcReadRegArray(CDC_TOUCHDATA, 0x01, raw, sizeof(raw));

for (i = 0; i < 5; i ++) {
arrayX[i] = (raw[i*2+ 0]<<8) | raw[i*2+ 1];
arrayY[i] = (raw[i*2+10]<<8) | raw[i*2+11];
if ((arrayX[i] & 0xF000) || (arrayY[i] & 0xF000)) {
pos->rawx = 0;
pos->rawy = 0;
return false;
}
}

// TODO: For now we just average all values without removing inaccurate values
sumX = 0;
sumY = 0;
for (i = 0; i < 5; i ++) {
sumX += arrayX[i];
sumY += arrayY[i];
}

pos->rawx = sumX / 5;
pos->rawy = sumY / 5;
return true;
}
59 changes: 59 additions & 0 deletions retail/bootloader/source/arm7/libnds/firmware.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*---------------------------------------------------------------------------------
Copyright (C) 2014
Dave Murphy (WinterMute)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
---------------------------------------------------------------------------------*/
#include <nds/arm7/serial.h>
#include <nds/interrupts.h>
#include <nds/system.h>
#include <string.h>

static u8 readwriteSPI(u8 data) {
REG_SPIDATA = data;
SerialWaitBusy();
return REG_SPIDATA;
}

//---------------------------------------------------------------------------------
void readFirmware(u32 address, void * destination, u32 size) {
//---------------------------------------------------------------------------------
int oldIME=enterCriticalSection();
u8 *buffer = destination;

// Read command
REG_SPICNT = SPI_ENABLE | SPI_BYTE_MODE | SPI_CONTINUOUS | SPI_DEVICE_FIRMWARE;
readwriteSPI(FIRMWARE_READ);

// Set the address
readwriteSPI((address>>16) & 0xFF);
readwriteSPI((address>> 8) & 0xFF);
readwriteSPI((address) & 0xFF);

u32 i;

// Read the data
for(i=0;i<size;i++) {
buffer[i] = readwriteSPI(0);
}

REG_SPICNT = 0;
leaveCriticalSection(oldIME);
}
Loading

0 comments on commit a9f8b4b

Please sign in to comment.