Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ ifeq ($(UNF),1)
SRC_DIRS += src/usb
endif

# libcart - Flashcart SD Card Interfacing (Compatible with select emulators)
# TO CONTRIBUTORS: -Run `git subrepo pull --force src/libcart` to update.
# -Delete sdcrc16.c and cartbuf.c upon update
LIBCART ?= 0
$(eval $(call validate-option,LIBCART,0 1))
ifeq ($(LIBCART),1)
LIBCART_SRC += src/libcart/ff src/libcart/src
INCLUDE_DIRS += src/libcart/include
DEFINES += _ULTRA64=1
endif

# ISVPRINT - whether to fake IS-Viewer presence,
# allowing for usage of CEN64 (and possibly Project64) to print messages to terminal.
# 1 - includes code in ROM
Expand Down Expand Up @@ -358,7 +369,7 @@ ACTOR_DIR := actors
LEVEL_DIRS := $(patsubst levels/%,%,$(dir $(wildcard levels/*/header.h)))

# Directories containing source files
SRC_DIRS += src src/boot src/game src/engine src/audio src/menu src/buffers actors levels bin data assets asm lib sound
SRC_DIRS += src $(LIBCART_SRC) src/boot src/game src/engine src/audio src/menu src/buffers actors levels bin data assets asm lib sound
LIBZ_SRC_DIRS := src/libz
GODDARD_SRC_DIRS := src/goddard src/goddard/dynlists
BIN_DIRS := bin bin/$(VERSION)
Expand Down
5 changes: 5 additions & 0 deletions include/libc/stdint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// most stdint types are in ultratypes.h, add any necessary extras here

#pragma once

#include <PR/ultratypes.h>
8 changes: 8 additions & 0 deletions sm64.ld
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ SECTIONS
BUILD_DIR/src/boot*.o(.text*);
BUILD_DIR/src/hvqm*.o(.text*);
BUILD_DIR/src/usb*.o(.text*);
BUILD_DIR/src/libcart/ff*.o(.text*);
BUILD_DIR/src/libcart/src*.o(.text*);
BUILD_DIR/src/audio*.o(.text*);
#ifdef S2DEX_TEXT_ENGINE
BUILD_DIR/src/s2d_engine*.o(.text*);
Expand All @@ -188,6 +190,8 @@ SECTIONS
/* data */
BUILD_DIR/asm/n64_assert.o(.*data*);
BUILD_DIR/src/boot*.o(.*data*);
BUILD_DIR/src/libcart/ff*.o(.*data*);
BUILD_DIR/src/libcart/src*.o(.*data*);
BUILD_DIR/src/audio*.o(.*data*);
#ifdef S2DEX_TEXT_ENGINE
BUILD_DIR/src/s2d_engine*.o(.*data*);
Expand All @@ -204,6 +208,8 @@ SECTIONS
/* rodata */
BUILD_DIR/src/boot*.o(.rodata*);
BUILD_DIR/src/usb*.o(.rodata*);
BUILD_DIR/src/libcart/ff*.o(.rodata*);
BUILD_DIR/src/libcart/src*.o(.rodata*);
BUILD_DIR/src/audio*.o(.rodata*);
#ifdef S2DEX_TEXT_ENGINE
BUILD_DIR/src/s2d_engine*.o(.rodata*);
Expand All @@ -221,6 +227,8 @@ SECTIONS
BUILD_DIR/src/boot*.o(.*bss*);
BUILD_DIR/src/hvqm*.o(.*bss*);
BUILD_DIR/src/usb*.o(.*bss*);
BUILD_DIR/src/libcart/ff*.o(.*bss*);
BUILD_DIR/src/libcart/src*.o(.*bss*);
BUILD_DIR/src/audio*.o(.*bss*);
#ifdef S2DEX_TEXT_ENGINE
BUILD_DIR/src/s2d_engine*.o(.*bss*);
Expand Down
2 changes: 2 additions & 0 deletions src/libcart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build/
/lib/
12 changes: 12 additions & 0 deletions src/libcart/.gitrepo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git "subrepo", and this file is maintained by the
; git-subrepo command. See https://github.com/ingydotnet/git-subrepo#readme
;
[subrepo]
remote = https://github.com/devwizard64/libcart
branch = main
commit = 948358b8320f49e75b4a9e3b4e792ccaec866e0b
parent = 543d7add024fea312a8991b29a029a0ac0ed7bef
method = merge
cmdver = 0.4.6
Comment on lines +1 to +12
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the advantage of subrepos over submodules exactly, and is it necessary here? Seems like subrepos aren't an actual part of git, so I'm hesitant to use this unless submodules/subtrees are no good here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

submodules are an extra command for the end user which is Bad™ (think changing the required baserom name bad)

maintaining the subrepo is on the contributors with zero work for the end user

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is? Cloning a repo with a submodule doesn't also clone the submodule?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an end user would have to either run git clone --recursive or git submodule init --update after a clone to grab those submodules

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welp...that sucks. What about with subtrees?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those work but i found them harder to maintain (s2dex text engine used to be a subtree here but i just made it part of the main repo eventually)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, whatever then. Could you update the libpl PR to use subrepos then since it sounds like submodules are bad

14 changes: 14 additions & 0 deletions src/libcart/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (C) 2022 - 2023 devwizard

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 changes: 24 additions & 0 deletions src/libcart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# libcart - Nintendo 64 flash cartridge library
Copyright (C) 2022 - 2023 devwizard

## About
This library provides an interface to several Nintendo 64 flash cartridges. It
automatically detects the hardware in use, and uses the appropriate protocol for
the hardware.

## Supported hardware
* 64Drive HW1 and HW2
* EverDrive-64 V1, V2, V2.5, V3, X7 and X5
* ED64Plus / Super 64
* SummerCart64

[Testing result spreadsheet](https://docs.google.com/spreadsheets/d/19opOv4e_4ABqgOmqRBSREtXLdGy08VzMsymtxWky0NU/edit?usp=sharing)

## Features
* Unlocks the cartridge ROM, making it rewritable.
* Uses optimised bus timing for faster DMA (EverDrive-64).
* Memory card access, with I/O between RDRAM and cartridge ROM.

## Credits
Special thanks to Reonu and Meeq for their extensive testing on ED64Plus and
EverDrive-64 V2. Without them, this project would not be possible.
170 changes: 170 additions & 0 deletions src/libcart/ff/diskio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*-----------------------------------------------------------------------*/
/* Low level disk I/O module for FatFs (libcart) (C)ChaN, 2019 */
/* (C)devwizard, 2022-2023 */
/*-----------------------------------------------------------------------*/

#include "ff.h" /* Obtains integer types */
#include "diskio.h" /* Declarations of disk functions */
#ifdef _ULTRA64
#include <ultra64.h>
#else
#include <libdragon.h>
#endif
#include <cart.h>

/* Definitions of physical drive number for each drive */
#define DEV_CARD 0 /* Cartridge memory card */


static DSTATUS card_status;

/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
switch (pdrv) {
case DEV_CARD :
return card_status;
}
return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
switch (pdrv) {
case DEV_CARD :
return card_status = cart_card_init() ? STA_NOINIT : 0;
}
return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
)
{
u32 addr;

switch (pdrv) {
case DEV_CARD :
#ifdef _ULTRA64
addr = osVirtualToPhysical(buff);
#else
addr = PhysicalAddr(buff);
#endif
if (addr < 0x800000) return cart_card_rd_dram(buff, sector, count) ? RES_ERROR : RES_OK;
else return cart_card_rd_cart(addr, sector, count) ? RES_ERROR : RES_OK;
}

return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
)
{
u32 addr;

switch (pdrv) {
case DEV_CARD :
#ifdef _ULTRA64
addr = osVirtualToPhysical((void *)buff);
#else
addr = PhysicalAddr(buff);
#endif
if (addr < 0x800000) return cart_card_wr_dram(buff, sector, count) ? RES_ERROR : RES_OK;
else return cart_card_wr_cart(addr, sector, count) ? RES_ERROR : RES_OK;
}

return RES_PARERR;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
(void)buff;

switch (pdrv) {
case DEV_CARD :
switch (cmd) {
#if !FF_FS_READONLY
case CTRL_SYNC:
return RES_OK;
#endif

#if !FF_FS_READONLY && FF_USE_MKFS
#if 0
case GET_SECTOR_COUNT:
*(LBA_t *)buff = 0;
break;
#endif
#endif

#if FF_MAX_SS != FF_MIN_SS
case GET_SECTOR_SIZE:
*(WORD *)buff = 512;
return RES_OK;
#endif

#if !FF_FS_READONLY && FF_USE_MKFS
case GET_BLOCK_SIZE:
*(DWORD *)buff = 512;
return RES_OK;
#endif
}
}

return RES_PARERR;
}


#if !FF_FS_READONLY && !FF_FS_NORTC

DWORD get_fattime (void)
{
return 0;
}

#endif

77 changes: 77 additions & 0 deletions src/libcart/ff/diskio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*-----------------------------------------------------------------------/
/ Low level disk interface modlue include file (C)ChaN, 2019 /
/-----------------------------------------------------------------------*/

#ifndef _DISKIO_DEFINED
#define _DISKIO_DEFINED

#ifdef __cplusplus
extern "C" {
#endif

/* Status of Disk Functions */
typedef BYTE DSTATUS;

/* Results of Disk Functions */
typedef enum {
RES_OK = 0, /* 0: Successful */
RES_ERROR, /* 1: R/W Error */
RES_WRPRT, /* 2: Write Protected */
RES_NOTRDY, /* 3: Not Ready */
RES_PARERR /* 4: Invalid Parameter */
} DRESULT;


/*---------------------------------------*/
/* Prototypes for disk control functions */


DSTATUS disk_initialize (BYTE pdrv);
DSTATUS disk_status (BYTE pdrv);
DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count);
DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count);
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);


/* Disk Status Bits (DSTATUS) */

#define STA_NOINIT 0x01 /* Drive not initialized */
#define STA_NODISK 0x02 /* No medium in the drive */
#define STA_PROTECT 0x04 /* Write protected */


/* Command code for disk_ioctrl fucntion */

/* Generic command (Used by FatFs) */
#define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */
#define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */
#define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */
#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */
#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */

/* Generic command (Not used by FatFs) */
#define CTRL_POWER 5 /* Get/Set power status */
#define CTRL_LOCK 6 /* Lock/Unlock media removal */
#define CTRL_EJECT 7 /* Eject media */
#define CTRL_FORMAT 8 /* Create physical format on the media */

/* MMC/SDC specific ioctl command */
#define MMC_GET_TYPE 10 /* Get card type */
#define MMC_GET_CSD 11 /* Get CSD */
#define MMC_GET_CID 12 /* Get CID */
#define MMC_GET_OCR 13 /* Get OCR */
#define MMC_GET_SDSTAT 14 /* Get SD status */
#define ISDIO_READ 55 /* Read data form SD iSDIO register */
#define ISDIO_WRITE 56 /* Write data to SD iSDIO register */
#define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */

/* ATA/CF specific ioctl command */
#define ATA_GET_REV 20 /* Get F/W revision */
#define ATA_GET_MODEL 21 /* Get model name */
#define ATA_GET_SN 22 /* Get serial number */

#ifdef __cplusplus
}
#endif

#endif
Loading