[SC64][FW][SW] Added escape mechanism in USB, changed N64 boot procedure, added "fake SD card through USB" feature, rewritten PC communication software (#13)

This commit is contained in:
Mateusz Faderewski 2021-12-10 17:36:30 +01:00 committed by GitHub
parent 29aca8aea6
commit 71f134178a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
53 changed files with 26021 additions and 815 deletions

4
.gitignore vendored
View File

@ -1,2 +1,4 @@
**/.DS_Store
**/.vscode **/.vscode
*.zip **/*.bak
**/*.zip

View File

@ -21,13 +21,15 @@ BUILT_RELEASE=false
FORCE_CLEAN=false FORCE_CLEAN=false
SKIP_FPGA_REBUILD=false SKIP_FPGA_REBUILD=false
DEBUG_ENABLED=false
USER_FLAGS+=" -D__SC64_VERSION=\"$__SC64_VERSION\""
build_cic () { build_cic () {
if [ "$BUILT_CIC" = true ]; then return; fi if [ "$BUILT_CIC" = true ]; then return; fi
pushd sw/cic pushd sw/cic > /dev/null
avra UltraCIC-III.asm -D attiny45 avra UltraCIC-III.asm -D attiny45
popd popd > /dev/null
BUILT_CIC=true BUILT_CIC=true
} }
@ -35,12 +37,12 @@ build_cic () {
build_n64 () { build_n64 () {
if [ "$BUILT_N64" = true ]; then return; fi if [ "$BUILT_N64" = true ]; then return; fi
pushd sw/n64 pushd sw/n64 > /dev/null
if [ "$FORCE_CLEAN" = true ]; then if [ "$FORCE_CLEAN" = true ]; then
make clean make clean
fi fi
make all make all -j USER_FLAGS="$USER_FLAGS"
popd popd > /dev/null
BUILT_N64=true BUILT_N64=true
} }
@ -48,12 +50,12 @@ build_n64 () {
build_riscv () { build_riscv () {
if [ "$BUILT_RISCV" = true ]; then return; fi if [ "$BUILT_RISCV" = true ]; then return; fi
pushd sw/riscv pushd sw/riscv > /dev/null
if [ "$FORCE_CLEAN" = true ]; then if [ "$FORCE_CLEAN" = true ]; then
make clean make clean
fi fi
make all make all -j USER_FLAGS="$USER_FLAGS"
popd popd > /dev/null
BUILT_RISCV=true BUILT_RISCV=true
} }
@ -64,13 +66,17 @@ build_fpga () {
build_n64 build_n64
build_riscv build_riscv
pushd fw pushd fw > /dev/null
if [ "$SKIP_FPGA_REBUILD" = true ] && [ -f output_files/SummerCart64.sof ]; then if [ "$SKIP_FPGA_REBUILD" = true ] && [ -f output_files/SummerCart64.sof ]; then
quartus_cpf -c SummerCart64.cof quartus_cpf -c SummerCart64.cof
else else
quartus_sh --flow compile ./SummerCart64.qpf if [ "$DEBUG_ENABLED" = true ]; then
quartus_sh --set VERILOG_MACRO="DEBUG" ./SummerCart64.qpf
fi fi
popd quartus_sh --flow compile ./SummerCart64.qpf
quartus_sh --set -remove VERILOG_MACRO="DEBUG" ./SummerCart64.qpf
fi
popd > /dev/null
BUILT_FPGA=true BUILT_FPGA=true
} }
@ -80,11 +86,11 @@ build_update () {
build_fpga build_fpga
pushd fw/output_files pushd fw/output_files > /dev/null
cat sc64_firmware_ufm_auto.rpd sc64_firmware_cfm0_auto.rpd > SC64_update_tmp.bin cat sc64_firmware_ufm_auto.rpd sc64_firmware_cfm0_auto.rpd > SC64_update_tmp.bin
objcopy -I binary -O binary --reverse-bytes=4 SC64_update_tmp.bin SC64_update.bin objcopy -I binary -O binary --reverse-bytes=4 SC64_update_tmp.bin SC64_update.bin
rm SC64_update_tmp.bin rm SC64_update_tmp.bin
popd popd > /dev/null
BUILT_UPDATE=true BUILT_UPDATE=true
} }
@ -105,7 +111,7 @@ build_release () {
print_usage () { print_usage () {
echo "builder script for SummerCart64" echo "builder script for SummerCart64"
echo "usage: ./build.sh [cic] [n64] [riscv] [fpga] [update] [release] [-c] [-s] [--help]" echo "usage: ./build.sh [cic] [n64] [riscv] [fpga] [update] [release] [-c] [-s] [-d] [--help]"
echo "parameters:" echo "parameters:"
echo " cic - assemble UltraCIC-III software" echo " cic - assemble UltraCIC-III software"
echo " n64 - compile N64 bootloader software" echo " n64 - compile N64 bootloader software"
@ -117,6 +123,8 @@ print_usage () {
echo " - clean software compilation result directories before build" echo " - clean software compilation result directories before build"
echo " -s | --skip-fpga-rebuild" echo " -s | --skip-fpga-rebuild"
echo " - do not recompile whole FPGA design if it's already done, just update software binaries" echo " - do not recompile whole FPGA design if it's already done, just update software binaries"
echo " -d | --debug"
echo " - enable debug features"
echo " --help - print this guide" echo " --help - print this guide"
} }
@ -160,6 +168,9 @@ while test $# -gt 0; do
-s|--skip-fpga-rebuild) -s|--skip-fpga-rebuild)
SKIP_FPGA_REBUILD=true SKIP_FPGA_REBUILD=true
;; ;;
-d|--debug)
DEBUG_ENABLED=true
;;
--help) --help)
print_usage print_usage
exit 0 exit 0
@ -174,6 +185,7 @@ while test $# -gt 0; do
shift shift
done done
if [ "$DEBUG_ENABLED" = true ]; then USER_FLAGS+=" -DDEBUG"; fi
if [ "$TRIGGER_CIC" = true ]; then build_cic; fi if [ "$TRIGGER_CIC" = true ]; then build_cic; fi
if [ "$TRIGGER_N64" = true ]; then build_n64; fi if [ "$TRIGGER_N64" = true ]; then build_n64; fi
if [ "$TRIGGER_RISCV" = true ]; then build_riscv; fi if [ "$TRIGGER_RISCV" = true ]; then build_riscv; fi

View File

@ -1,7 +1,18 @@
#!/bin/bash #!/bin/bash
GIT_SHA=$(git rev-parse --short HEAD)
GIT_TAG=$(git describe --tags --exact-match 2> /dev/null)
if [ ! -z $GIT_TAG ]; then
__SC64_VERSION=$(printf "%.7q\ %.7q" $GIT_SHA $GIT_TAG)
else
__SC64_VERSION=$(printf "%.7q\ develop" $GIT_SHA)
fi
docker run \ docker run \
--rm \ --rm \
--user $(id -u):$(id -g) \
--mount type=bind,src="$(pwd)",target="/workdir" \ --mount type=bind,src="$(pwd)",target="/workdir" \
-e __SC64_VERSION="$__SC64_VERSION" \
ghcr.io/polprzewodnikowy/sc64env:v1.2 \ ghcr.io/polprzewodnikowy/sc64env:v1.2 \
./build.sh $@ ./build.sh $@

View File

@ -27,7 +27,7 @@
<verify_protect>0</verify_protect> <verify_protect>0</verify_protect>
<epof>0</epof> <epof>0</epof>
<ufm_source>2</ufm_source> <ufm_source>2</ufm_source>
<ufm_filepath>../sw/n64/build/SummerLoader64.hex</ufm_filepath> <ufm_filepath>../sw/n64/build/n64boot.hex</ufm_filepath>
<cfm0_filepath>../sw/riscv/build/governor.hex</cfm0_filepath> <cfm0_filepath>../sw/riscv/build/governor.hex</cfm0_filepath>
<cfm0_file_start_addr>305152</cfm0_file_start_addr> <cfm0_file_start_addr>305152</cfm0_file_start_addr>
</MAX10_device_options> </MAX10_device_options>

View File

@ -9,7 +9,7 @@ module cpu_cfg (
typedef enum bit [2:0] { typedef enum bit [2:0] {
R_SCR, R_SCR,
R_DD_OFFSET, R_DDIPL_OFFSET,
R_SAVE_OFFSET, R_SAVE_OFFSET,
R_COMMAND, R_COMMAND,
R_DATA_0, R_DATA_0,
@ -45,7 +45,7 @@ module cpu_cfg (
cfg.sdram_writable, cfg.sdram_writable,
cfg.sdram_switch cfg.sdram_switch
}; };
R_DD_OFFSET: bus.rdata = {6'd0, cfg.dd_offset}; R_DDIPL_OFFSET: bus.rdata = {6'd0, cfg.ddipl_offset};
R_SAVE_OFFSET: bus.rdata = {6'd0, cfg.save_offset}; R_SAVE_OFFSET: bus.rdata = {6'd0, cfg.save_offset};
R_COMMAND: bus.rdata = {24'd0, cfg.cmd}; R_COMMAND: bus.rdata = {24'd0, cfg.cmd};
R_DATA_0: bus.rdata = cfg.data[0]; R_DATA_0: bus.rdata = cfg.data[0];
@ -77,7 +77,7 @@ module cpu_cfg (
cfg.sram_enabled <= 1'b0; cfg.sram_enabled <= 1'b0;
cfg.sram_banked <= 1'b0; cfg.sram_banked <= 1'b0;
cfg.flashram_enabled <= 1'b0; cfg.flashram_enabled <= 1'b0;
cfg.dd_offset <= 26'h3BE_0000; cfg.ddipl_offset <= 26'h3BE_0000;
cfg.save_offset <= 26'h3FE_0000; cfg.save_offset <= 26'h3FE_0000;
skip_bootloader <= 1'b0; skip_bootloader <= 1'b0;
trigger_reconfiguration <= 1'b0; trigger_reconfiguration <= 1'b0;
@ -112,9 +112,9 @@ module cpu_cfg (
end end
end end
R_DD_OFFSET: begin R_DDIPL_OFFSET: begin
if (&bus.wmask) begin if (&bus.wmask) begin
cfg.dd_offset <= bus.wdata[25:0]; cfg.ddipl_offset <= bus.wdata[25:0];
end end
end end

View File

@ -20,6 +20,10 @@ module cpu_usb (
logic tx_write; logic tx_write;
logic [7:0] tx_wdata; logic [7:0] tx_wdata;
logic rx_escape_valid;
logic rx_escape_ack;
logic [7:0] rx_escape;
logic cpu_rx_read; logic cpu_rx_read;
logic cpu_tx_write; logic cpu_tx_write;
@ -45,9 +49,10 @@ module cpu_usb (
always_comb begin always_comb begin
bus.rdata = 32'd0; bus.rdata = 32'd0;
if (bus.ack) begin if (bus.ack) begin
case (bus.address[2:2]) case (bus.address[3:2])
0: bus.rdata = {26'd0, usb_pwren, usb_enabled, 2'b00, ~tx_full, ~rx_empty}; 0: bus.rdata = {25'd0, rx_escape_valid, usb_pwren, usb_enabled, 2'b00, ~tx_full, ~rx_empty};
1: bus.rdata = {24'd0, rx_rdata}; 1: bus.rdata = {24'd0, rx_rdata};
2: bus.rdata = {24'd0, rx_escape};
default: bus.rdata = 32'd0; default: bus.rdata = 32'd0;
endcase endcase
end end
@ -60,6 +65,8 @@ module cpu_usb (
tx_flush <= 1'b0; tx_flush <= 1'b0;
cpu_tx_write <= 1'b0; cpu_tx_write <= 1'b0;
rx_escape_ack <= 1'b0;
if (sys.reset) begin if (sys.reset) begin
usb_enabled <= 1'b0; usb_enabled <= 1'b0;
end else begin end else begin
@ -67,6 +74,7 @@ module cpu_usb (
case (bus.address[2:2]) case (bus.address[2:2])
2'd0: begin 2'd0: begin
if (bus.wmask[0]) begin if (bus.wmask[0]) begin
rx_escape_ack <= bus.wdata[7];
{usb_enabled, tx_flush, rx_flush} <= bus.wdata[4:2]; {usb_enabled, tx_flush, rx_flush} <= bus.wdata[4:2];
end end
end end
@ -101,7 +109,11 @@ module cpu_usb (
.tx_flush(tx_flush), .tx_flush(tx_flush),
.tx_full(tx_full), .tx_full(tx_full),
.tx_write(tx_write), .tx_write(tx_write),
.tx_wdata(tx_wdata) .tx_wdata(tx_wdata),
.rx_escape_valid(rx_escape_valid),
.rx_escape_ack(rx_escape_ack),
.rx_escape(rx_escape)
); );
endmodule endmodule

View File

@ -169,13 +169,12 @@ module n64_flashram (
end end
endcase endcase
end end
end else begin
if (bus.address[1] && flashram_state == FS_STATUS) begin
flashram_status[B_ERASE_BUSY] <= bus.wdata[B_ERASE_BUSY];
flashram_status[B_WRITE_BUSY] <= bus.wdata[B_WRITE_BUSY];
end
end end
// else begin
// if (bus.address[1] && flashram_state == FS_STATUS) begin
// flashram_status[B_ERASE_DONE] <= bus.wdata[B_ERASE_DONE];
// flashram_status[B_WRITE_DONE] <= bus.wdata[B_WRITE_DONE];
// end
// end
end end
end end
end end

View File

@ -212,7 +212,7 @@ module n64_pi (
if (n64_pi_ad_input >= 16'h0600 && n64_pi_ad_input < 16'h0640) begin if (n64_pi_ad_input >= 16'h0600 && n64_pi_ad_input < 16'h0640) begin
n64_pi_address_valid <= 1'b1; n64_pi_address_valid <= 1'b1;
next_id <= sc64::ID_N64_SDRAM; next_id <= sc64::ID_N64_SDRAM;
next_offset <= cfg.dd_offset + 32'h0A00_0000; next_offset <= cfg.ddipl_offset + 32'h0A00_0000;
end end
end end
if (cfg.flashram_enabled) begin if (cfg.flashram_enabled) begin
@ -297,7 +297,7 @@ module n64_pi (
first_write_op <= 1'b1; first_write_op <= 1'b1;
load_starting_address <= 1'b1; load_starting_address <= 1'b1;
starting_id <= next_id; starting_id <= next_id;
starting_address <= {starting_address[31:16], n64_pi_ad_input[15:1], 1'b0} + next_offset; starting_address <= {starting_address[31:16], n64_pi_ad_input[15:1], 1'b0};
end end
if (write_op) begin if (write_op) begin
@ -314,7 +314,10 @@ module n64_pi (
bus.write <= 1'b1; bus.write <= 1'b1;
if (load_starting_address) begin if (load_starting_address) begin
bus.id <= starting_id; bus.id <= starting_id;
bus.address <= starting_address + next_offset;
if (starting_id == sc64::ID_N64_FLASHRAM) begin
bus.address <= starting_address; bus.address <= starting_address;
end
load_starting_address <= 1'b0; load_starting_address <= 1'b0;
end end
bus.wdata <= write_fifo_rdata; bus.wdata <= write_fifo_rdata;
@ -324,19 +327,17 @@ module n64_pi (
bus.write <= 1'b0; bus.write <= 1'b0;
if (load_starting_address) begin if (load_starting_address) begin
bus.id <= starting_id; bus.id <= starting_id;
bus.address <= starting_address + next_offset;
if (starting_id == sc64::ID_N64_FLASHRAM && cfg.flashram_read_mode) begin if (starting_id == sc64::ID_N64_FLASHRAM && cfg.flashram_read_mode) begin
bus.id <= sc64::ID_N64_SDRAM; bus.id <= sc64::ID_N64_SDRAM;
end end
bus.address <= starting_address;
load_starting_address <= 1'b0; load_starting_address <= 1'b0;
end end
end end
end else begin end else if (bus.ack) begin
if (bus.ack) begin
bus.request <= 1'b0; bus.request <= 1'b0;
bus.address <= bus.address + 2'd2; bus.address <= bus.address + 2'd2;
end end
end
if (end_op) begin if (end_op) begin
can_read <= 1'b0; can_read <= 1'b0;

View File

@ -15,7 +15,7 @@ interface if_config ();
logic sram_banked; logic sram_banked;
logic flashram_enabled; logic flashram_enabled;
logic flashram_read_mode; logic flashram_read_mode;
logic [25:0] dd_offset; logic [25:0] ddipl_offset;
logic [25:0] save_offset; logic [25:0] save_offset;
modport pi ( modport pi (
@ -26,7 +26,7 @@ interface if_config ();
input sram_banked, input sram_banked,
input flashram_enabled, input flashram_enabled,
input flashram_read_mode, input flashram_read_mode,
input dd_offset, input ddipl_offset,
input save_offset input save_offset
); );
@ -60,7 +60,7 @@ interface if_config ();
output sram_enabled, output sram_enabled,
output sram_banked, output sram_banked,
output flashram_enabled, output flashram_enabled,
output dd_offset, output ddipl_offset,
output save_offset output save_offset
); );

View File

@ -33,7 +33,12 @@ package sc64;
parameter bit [31:0] SC64_VER = 32'h53437632; parameter bit [31:0] SC64_VER = 32'h53437632;
parameter int CLOCK_FREQUENCY = 32'd100_000_000; parameter int CLOCK_FREQUENCY = 32'd100_000_000;
parameter bit [31:0] CPU_RESET_VECTOR = {4'(ID_CPU_FLASH), 28'h0035800}; parameter bit [31:0] CPU_RESET_VECTOR = {4'(ID_CPU_FLASH), 28'h0035800};
parameter bit CPU_HAS_UART = 1'b0;
parameter int UART_BAUD_RATE = 32'd1_000_000; parameter int UART_BAUD_RATE = 32'd1_000_000;
`ifdef DEBUG
parameter bit CPU_HAS_UART = 1'b1;
`else
parameter bit CPU_HAS_UART = 1'b0;
`endif
endpackage endpackage

View File

@ -16,9 +16,15 @@ module usb_ft1248 (
input tx_flush, input tx_flush,
output tx_full, output tx_full,
input tx_write, input tx_write,
input [7:0] tx_wdata input [7:0] tx_wdata,
output rx_escape_valid,
input rx_escape_ack,
output [7:0] rx_escape
); );
parameter bit [7:0] ESCAPE_CHARACTER = 8'h1B;
// FIFOs // FIFOs
logic rx_full; logic rx_full;
@ -29,6 +35,9 @@ module usb_ft1248 (
logic tx_read; logic tx_read;
logic [7:0] tx_rdata; logic [7:0] tx_rdata;
logic rx_wdata_valid;
logic rx_escape_active;
intel_fifo_8 fifo_8_rx_inst ( intel_fifo_8 fifo_8_rx_inst (
.clock(sys.clk), .clock(sys.clk),
.sclr(rx_flush || !usb_enabled), .sclr(rx_flush || !usb_enabled),
@ -56,6 +65,41 @@ module usb_ft1248 (
); );
// Escape character detection
always_comb begin
rx_write = 1'b0;
if (rx_wdata_valid) begin
rx_write = rx_escape_active ? rx_wdata == ESCAPE_CHARACTER : rx_wdata != ESCAPE_CHARACTER;
end
end
always_ff @(posedge sys.clk) begin
if (sys.reset || !usb_enabled) begin
rx_escape_valid <= 1'b0;
rx_escape_active <= 1'b0;
end else begin
if (rx_escape_ack) begin
rx_escape_valid <= 1'b0;
end
if (rx_wdata_valid) begin
if (!rx_escape_active) begin
if (rx_wdata == ESCAPE_CHARACTER) begin
rx_escape_active <= 1'b1;
end
end else begin
rx_escape_active <= 1'b0;
rx_escape <= rx_wdata;
if (rx_wdata != ESCAPE_CHARACTER) begin
rx_escape_valid <= 1'b1;
end
end
end
end
end
// FT1248 interface controller // FT1248 interface controller
typedef enum bit [1:0] { typedef enum bit [1:0] {
@ -149,7 +193,7 @@ module usb_ft1248 (
end end
always_ff @(posedge sys.clk) begin always_ff @(posedge sys.clk) begin
rx_write <= 1'b0; rx_wdata_valid <= 1'b0;
tx_read <= 1'b0; tx_read <= 1'b0;
if (clock_phase[P_RISING]) begin if (clock_phase[P_RISING]) begin
@ -161,7 +205,7 @@ module usb_ft1248 (
end else begin end else begin
case (state) case (state)
S_TRY_RX: begin S_TRY_RX: begin
if (!rx_full) begin if (!rx_full && !rx_escape_valid) begin
state <= S_COMMAND; state <= S_COMMAND;
is_cmd_write <= 1'b0; is_cmd_write <= 1'b0;
nibble_counter <= 2'b11; nibble_counter <= 2'b11;
@ -202,9 +246,9 @@ module usb_ft1248 (
if (clock_phase[P_RISING]) begin if (clock_phase[P_RISING]) begin
rx_wdata <= {usb_miosi_input, rx_wdata[7:4]}; rx_wdata <= {usb_miosi_input, rx_wdata[7:4]};
if (nibble_counter[0]) begin if (nibble_counter[0]) begin
rx_write <= !is_cmd_write; rx_wdata_valid <= !is_cmd_write;
end end
if (usb_miso_input || (!is_cmd_write && rx_full) || (is_cmd_write && tx_empty)) begin if (usb_miso_input || (!is_cmd_write && (rx_full || rx_escape_valid)) || (is_cmd_write && tx_empty)) begin
state <= is_cmd_write ? S_TRY_RX : S_TRY_TX; state <= is_cmd_write ? S_TRY_RX : S_TRY_TX;
end end
end end

1
sw/n64/.gitignore vendored
View File

@ -1,2 +1 @@
/build /build
*.z64

View File

@ -1,28 +1,70 @@
TOOLCHAIN = mips64-elf-
CC = $(TOOLCHAIN)gcc
CXX = $(TOOLCHAIN)g++
OBJCOPY = $(TOOLCHAIN)objcopy
OBJDUMP = $(TOOLCHAIN)objdump
SIZE = $(TOOLCHAIN)size
FLAGS = -march=vr4300 -mtune=vr4300 -falign-functions=32 $(USER_FLAGS)
CFLAGS = -Os -Wall -ffunction-sections -fdata-sections -ffreestanding -MMD -MP
ASFLAGS = -Wa,-I$(N64_INST)/mips64-elf/lib
LDFLAGS = -lc -nostartfiles -Wl,--gc-sections
SRC_DIR = src
BUILD_DIR = build BUILD_DIR = build
SOURCE_DIR = src
PROGRAM_NAME = SummerLoader64
include $(N64_INST)/include/n64.mk SRC_FILES = \
startup.S \
boot.c \
crc32.c \
init.c \
main.c \
sc64.c \
storage.c \
sys.c \
syscalls.c \
fatfs/diskio.c \
fatfs/ff.c \
fatfs/ffsystem.c \
fatfs/ffunicode.c
src = main.c sc64.c boot.c crc32.c SRCS = $(addprefix $(SRC_DIR)/, $(SRC_FILES))
OBJS = $(addprefix $(BUILD_DIR)/, $(notdir $(patsubst %,%.o,$(SRCS))))
DEPS = $(OBJS:.o=.d)
all: $(BUILD_DIR)/$(PROGRAM_NAME).hex VPATH = $(SRC_DIR) $(SRC_DIR)/fatfs
$(BUILD_DIR)/$(PROGRAM_NAME).elf: $(src:%.c=$(BUILD_DIR)/%.o) $(@info $(shell mkdir -p ./$(BUILD_DIR) &> /dev/null))
$(PROGRAM_NAME).z64: N64_ROM_TITLE="$(PROGRAM_NAME)" $(BUILD_DIR)/%.S.o: %.S
$(CC) -x assembler-with-cpp $(FLAGS) $(ASFLAGS) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/$(PROGRAM_NAME).hex: $(PROGRAM_NAME).z64 $(BUILD_DIR)/%.c.o: %.c
sed '$$ s/\x00*$$//' $(PROGRAM_NAME).z64 > $(BUILD_DIR)/$(PROGRAM_NAME)_stripped.z64 $(CC) $(FLAGS) $(CFLAGS) -c $< -o $@
@if [ $$(stat -L -c %s $(BUILD_DIR)/$(PROGRAM_NAME)_stripped.z64) -gt 92160 ]; then\
echo "\n Error: stripped file size is larger than 90kB thus cannot fit inside FPGA flash.\n"; exit 1;\ $(BUILD_DIR)/n64boot.elf: $(OBJS) N64.ld
fi $(CXX) $(FLAGS) $(LDFLAGS) -TN64.ld $(OBJS) -o $@
truncate --size=90k $(BUILD_DIR)/$(PROGRAM_NAME)_stripped.z64 @$(OBJDUMP) -S $@ > $(BUILD_DIR)/n64boot.lst
$(N64_OBJCOPY) -I binary -O ihex $(BUILD_DIR)/$(PROGRAM_NAME)_stripped.z64 $(BUILD_DIR)/$(PROGRAM_NAME).hex
$(BUILD_DIR)/n64boot.bin: $(BUILD_DIR)/n64boot.elf
@$(OBJCOPY) -O binary $< $@
@chksum64 $@ > /dev/null
@truncate --size=90k $@
$(BUILD_DIR)/n64boot.hex: $(BUILD_DIR)/n64boot.bin
@$(OBJCOPY) -I binary -O ihex $< $@
print_size: $(BUILD_DIR)/n64boot.elf
@echo 'Size of modules:'
@$(SIZE) -B -d -t --common $(OBJS)
@echo 'Size of n64boot:'
@$(SIZE) -B -d $<
all: $(BUILD_DIR)/n64boot.hex print_size
clean: clean:
rm -rf ./$(BUILD_DIR) ./$(PROGRAM_NAME).z64 @rm -rf ./$(BUILD_DIR)/*
-include $(wildcard $(BUILD_DIR)/*.d) .PHONY: all clean print_size
.PHONY: all clean -include $(DEPS)

46
sw/n64/N64.ld Normal file
View File

@ -0,0 +1,46 @@
MEMORY {
rdram (rwx) : org = 0x80000400, len = 4M - 0x400
flash (r) : org = 0xB0000000, len = 90k
}
ENTRY(entry_handler)
__stack_size = 64k;
SECTIONS {
.flash : {
KEEP(*(.text.rom_header));
KEEP(*(.text.ipl3));
__ipl3_font = LOADADDR(.flash) + 0xB70;
} > flash
.text : {
*(.text.entry_handler)
*(.text .text.* .gnu.linkonce.t.*)
*(.rodata .rodata.* .gnu.linkonce.r.*)
*(.data .data.* .gnu.linkonce.d.*)
_gp = . + 0x8000;
*(.sdata .sdata.* .gnu.linkonce.s.*)
*(.lit8 .lit4)
. = ALIGN(4);
} > rdram AT > flash
.bss : {
*(.sbss .sbss.* .gnu.linkonce.sb.*)
*(.scommon .scommon.*)
*(.bss .bss.* .gnu.linkonce.b.*)
*(COMMON)
. = ALIGN(4);
} > rdram
_sheap = .;
. = ORIGIN(rdram) + LENGTH(rdram) - __stack_size;
_eheap = .;
. += __stack_size;
_sp = .;
/DISCARD/ : {
*(.MIPS.*)
}
}

View File

@ -1,53 +1,48 @@
#include "boot.h" #include "boot.h"
#include "crc32.h" #include "crc32.h"
#include "n64_regs.h" #include "sc64.h"
// #include "sc64.h" #include "sys.h"
static const struct crc32_to_cic_seed { extern uint32_t ipl2 __attribute__((section(".data")));
uint32_t ipl3_crc32;
uint16_t cic_seed;
} crc32_to_cic_seed[] = { typedef struct {
{ .ipl3_crc32 = 0x587BD543, .cic_seed = 0x00AC }, // CIC5101 const uint32_t crc32;
{ .ipl3_crc32 = 0x6170A4A1, .cic_seed = 0x013F }, // CIC6101 const uint8_t seed;
{ .ipl3_crc32 = 0x009E9EA3, .cic_seed = 0x013F }, // CIC7102 const uint8_t version;
{ .ipl3_crc32 = 0x90BB6CB5, .cic_seed = 0x003F }, // CICx102 } ipl3_crc32_t;
{ .ipl3_crc32 = 0x0B050EE0, .cic_seed = 0x0078 }, // CICx103
{ .ipl3_crc32 = 0x98BC2C86, .cic_seed = 0x0091 }, // CICx105 static const ipl3_crc32_t ipl3_crc32[] = {
{ .ipl3_crc32 = 0xACC8580A, .cic_seed = 0x0085 }, // CICx106 { .crc32 = 0x587BD543, .seed = 0xAC, .version = 0 }, // CIC5101
{ .ipl3_crc32 = 0x10C68B18, .cic_seed = 0x00DD }, // JP 64DD dev { .crc32 = 0x6170A4A1, .seed = 0x3F, .version = 1 }, // CIC6101
{ .ipl3_crc32 = 0x0E018159, .cic_seed = 0x00DD }, // JP 64DD retail { .crc32 = 0x009E9EA3, .seed = 0x3F, .version = 1 }, // CIC7102
{ .ipl3_crc32 = 0x8FEBA21E, .cic_seed = 0x00DE }, // US 64DD retail { .crc32 = 0x90BB6CB5, .seed = 0x3F, .version = 0 }, // CICx102
{ .crc32 = 0x0B050EE0, .seed = 0x78, .version = 0 }, // CICx103
{ .crc32 = 0x98BC2C86, .seed = 0x91, .version = 0 }, // CICx105
{ .crc32 = 0xACC8580A, .seed = 0x85, .version = 0 }, // CICx106
{ .crc32 = 0x10C68B18, .seed = 0xDD, .version = 0 }, // NDXJ0
{ .crc32 = 0xBC605D0A, .seed = 0xDD, .version = 0 }, // NDDJ0
{ .crc32 = 0x502C4466, .seed = 0xDD, .version = 0 }, // NDDJ1
{ .crc32 = 0x0C965795, .seed = 0xDD, .version = 0 }, // NDDJ2
{ .crc32 = 0x8FEBA21E, .seed = 0xDE, .version = 0 }, // NDDE0
}; };
static cart_header_t global_cart_header __attribute__((aligned(16))); static io32_t *boot_get_device_base (boot_info_t *info) {
io32_t *device_base_address = ROM_CART;
if (info->device_type == BOOT_DEVICE_TYPE_DD) {
cart_header_t *boot_load_cart_header(void) { device_base_address = ROM_DDIPL;
cart_header_t *cart_header_pointer = &global_cart_header; }
return device_base_address;
platform_pi_dma_read(cart_header_pointer, CART_BASE, sizeof(cart_header_t));
platform_cache_invalidate(cart_header_pointer, sizeof(cart_header_t));
return cart_header_pointer;
} }
uint16_t boot_get_cic_seed(cart_header_t *cart_header) { bool boot_get_tv_type (boot_info_t *info) {
uint16_t cic_seed = crc32_to_cic_seed[7].cic_seed; io32_t *base = boot_get_device_base(info);
uint32_t ipl3_crc32 = crc32_calculate(cart_header->boot_code, sizeof(cart_header->boot_code));
for (size_t i = 0; i < ARRAY_ITEMS(crc32_to_cic_seed); i++) { char region = ((pi_io_read(&base[15]) >> 8) & 0xFF);
if (crc32_to_cic_seed[i].ipl3_crc32 == ipl3_crc32) {
cic_seed = crc32_to_cic_seed[i].cic_seed;
}
}
return cic_seed; switch (region) {
}
tv_type_t boot_get_tv_type(cart_header_t *cart_header) {
switch (cart_header->country_code) {
case 'D': case 'D':
case 'F': case 'F':
case 'H': case 'H':
@ -57,7 +52,9 @@ tv_type_t boot_get_tv_type(cart_header_t *cart_header) {
case 'W': case 'W':
case 'X': case 'X':
case 'Y': case 'Y':
return TV_PAL; info->tv_type = BOOT_TV_TYPE_PAL;
break;
case '7': case '7':
case 'A': case 'A':
case 'C': case 'C':
@ -66,152 +63,119 @@ tv_type_t boot_get_tv_type(cart_header_t *cart_header) {
case 'K': case 'K':
case 'N': case 'N':
case 'U': case 'U':
return TV_NTSC; info->tv_type = BOOT_TV_TYPE_NTSC;
break;
case 'B': case 'B':
return TV_MPAL; info->tv_type = BOOT_TV_TYPE_MPAL;
break;
default: default:
return -1; LOG_E("Unknown region: [0x%02X]\r\n", region);
return false;
}
return true;
}
bool boot_get_cic_seed_version (boot_info_t *info) {
io32_t *base = boot_get_device_base(info);
uint32_t ipl3[1008];
io32_t *ipl3_src = &base[16];
uint32_t *ipl3_dst = ipl3;
for (int i = 0; i < sizeof(ipl3); i += sizeof(uint32_t)) {
*ipl3_dst++ = pi_io_read(ipl3_src++);
}
uint32_t crc32 = crc32_calculate(ipl3, sizeof(ipl3));
for (int i = 0; i < sizeof(ipl3_crc32) / sizeof(ipl3_crc32_t); i++) {
if (ipl3_crc32[i].crc32 == crc32) {
info->cic_seed = ipl3_crc32[i].seed;
info->version = ipl3_crc32[i].version;
return true;
} }
} }
void boot(cart_header_t *cart_header, uint16_t cic_seed, tv_type_t tv_type) { LOG_E("Unknown IPL3 CRC32: [0x%08lX]\r\n", crc32);
uint32_t is_x105_boot = (cic_seed == crc32_to_cic_seed[5].cic_seed);
uint32_t is_ddipl_boot = (
(cic_seed == crc32_to_cic_seed[7].cic_seed) ||
(cic_seed == crc32_to_cic_seed[8].cic_seed) ||
(cic_seed == crc32_to_cic_seed[9].cic_seed)
);
tv_type_t os_tv_type = tv_type == -1 ? OS_BOOT_CONFIG->tv_type : tv_type;
volatile uint64_t gpr_regs[32]; return false;
MI->interrupt_mask = MI_INT_CLEAR_SP | MI_INT_CLEAR_SI | MI_INT_CLEAR_AI | MI_INT_CLEAR_VI | MI_INT_CLEAR_PI | MI_INT_CLEAR_DP;
while (!(SP->status & SP_STATUS_HALT));
SP->status = SP_STATUS_CLEAR_INTERRUPT | SP_STATUS_SET_HALT;
while (SP->dma_busy & SP_DMA_BUSY);
PI->status = PI_STATUS_CLEAR_INTERRUPT | PI_STATUS_RESET_CONTROLLER;
VI->v_intr = 0x3FF;
VI->h_limits = 0;
VI->current_line = 0;
AI->dram_addr = 0;
AI->len = 0;
PI->dom1_lat = cart_header->pi_conf & 0xFF;
PI->dom1_pwd = cart_header->pi_conf >> 8;
PI->dom1_pgs = cart_header->pi_conf >> 16;
PI->dom1_rls = cart_header->pi_conf >> 20;
if (DP_CMD->status & DP_CMD_STATUS_XBUS_DMEM_DMA) {
while (DP_CMD->status & DP_CMD_STATUS_PIPE_BUSY);
} }
for (size_t i = 0; i < ARRAY_ITEMS(cart_header->boot_code); i++) { void boot (boot_info_t *info) {
SP_MEM->dmem[16 + i] = cart_header->boot_code[i]; c0_set_status(C0_SR_CU1 | C0_SR_CU0 | C0_SR_FR);
OS_INFO->mem_size_6105 = OS_INFO->mem_size;
while (!(io_read(&SP->SR) & SP_SR_HALT));
io_write(&SP->SR, SP_SR_CLR_INTR | SP_SR_SET_HALT);
while (io_read(&SP->DMA_BUSY));
io_write(&PI->SR, PI_SR_CLR_INTR | PI_SR_RESET);
io_write(&VI->V_INTR, 0x3FF);
io_write(&VI->H_LIMITS, 0);
io_write(&VI->CURR_LINE, 0);
io_write(&AI->MADDR, 0);
io_write(&AI->LEN, 0);
io32_t *base = boot_get_device_base(info);
uint32_t pi_config = pi_io_read(base);
io_write(&PI->DOM[0].LAT, pi_config & 0xFF);
io_write(&PI->DOM[0].PWD, pi_config >> 8);
io_write(&PI->DOM[0].PGS, pi_config >> 16);
io_write(&PI->DOM[0].RLS, pi_config >> 20);
if (io_read(&DPC->SR) & DPC_SR_XBUS_DMEM_DMA) {
while (io_read(&DPC->SR) & DPC_SR_PIPE_BUSY);
} }
// CIC X105 based games checks if start of IPL2 code exists in SP IMEM uint32_t *ipl2_src = &ipl2;
io32_t *ipl2_dst = SP_MEM->IMEM;
SP_MEM->imem[0] = 0x3C0DBFC0; // lui t5, 0xBFC0 for (int i = 0; i < 8; i++) {
SP_MEM->imem[1] = 0x8DA807FC; // lw t0, 0x07FC(t5) io_write(&ipl2_dst[i], ipl2_src[i]);
SP_MEM->imem[2] = 0x25AD07C0; // addiu t5, t5, 0x07C0
SP_MEM->imem[3] = 0x31080080; // andi t0, t0, 0x0080
SP_MEM->imem[4] = 0x5500FFFC; // bnel t0, zero, &SP_MEM->imem[1]
SP_MEM->imem[5] = 0x3C0DBFC0; // lui t5, 0xBFC0
SP_MEM->imem[6] = 0x8DA80024; // lw t0, 0x0024(t5)
SP_MEM->imem[7] = 0x3C0BB000; // lui t3, 0xB000
if (is_x105_boot) {
OS_BOOT_CONFIG->mem_size_6105 = OS_BOOT_CONFIG->mem_size;
} }
for (size_t i = 0; i < ARRAY_ITEMS(gpr_regs); i++) { io32_t *ipl3_src = base;
gpr_regs[i] = 0; io32_t *ipl3_dst = SP_MEM->DMEM;
for (int i = 16; i < 1024; i++) {
io_write(&ipl3_dst[i], pi_io_read(&ipl3_src[i]));
} }
gpr_regs[CPU_REG_T3] = CPU_ADDRESS_IN_REG(SP_MEM->dmem[16]); register void (*entry_point)(void) asm ("t3");
gpr_regs[CPU_REG_S3] = is_ddipl_boot ? OS_BOOT_ROM_TYPE_DD : OS_BOOT_ROM_TYPE_GAME_PAK; register uint32_t boot_device asm ("s3");
gpr_regs[CPU_REG_S4] = os_tv_type; register uint32_t tv_type asm ("s4");
gpr_regs[CPU_REG_S5] = OS_BOOT_RESET_TYPE_COLD; register uint32_t reset_type asm ("s5");
gpr_regs[CPU_REG_S6] = BOOT_SEED_IPL3(cic_seed); register uint32_t cic_seed asm ("s6");
gpr_regs[CPU_REG_S7] = BOOT_SEED_OS_VERSION(cic_seed); register uint32_t version asm ("s7");
gpr_regs[CPU_REG_SP] = CPU_ADDRESS_IN_REG(SP_MEM->imem[ARRAY_ITEMS(SP_MEM->imem) - 4]); void *stack_pointer;
gpr_regs[CPU_REG_RA] = CPU_ADDRESS_IN_REG(SP_MEM->imem[(os_tv_type == TV_PAL) ? 341 : 340]);
// sc64_print_debug((uint32_t) gpr_regs[CPU_REG_T3], (uint32_t) gpr_regs[CPU_REG_S3], (uint32_t) gpr_regs[CPU_REG_S4]); entry_point = (void (*)(void)) UNCACHED(&SP_MEM->DMEM[16]);
// sc64_print_debug((uint32_t) gpr_regs[CPU_REG_S5], (uint32_t) gpr_regs[CPU_REG_S6], (uint32_t) gpr_regs[CPU_REG_S7]); boot_device = (info->device_type & 0x01);
// sc64_print_debug((uint32_t) gpr_regs[CPU_REG_SP], (uint32_t) gpr_regs[CPU_REG_RA], 0); tv_type = (info->tv_type & 0x03);
reset_type = (info->reset_type & 0x01);
cic_seed = (info->cic_seed & 0xFF);
version = (info->version & 0x01);
stack_pointer = (void *) UNCACHED(&SP_MEM->IMEM[1020]);
__asm__ ( asm volatile (
".set noat \n\t" "move $sp, %[stack_pointer] \n"
".set noreorder \n\t" "jr %[entry_point] \n" ::
"li $t0, 0x34000000 \n\t" [entry_point] "r" (entry_point),
"mtc0 $t0, $12 \n\t" [boot_device] "r" (boot_device),
"nop \n\t" [tv_type] "r" (tv_type),
"li $t0, 0x0006E463 \n\t" [reset_type] "r" (reset_type),
"mtc0 $t0, $16 \n\t" [cic_seed] "r" (cic_seed),
"nop \n\t" [version] "r" (version),
"li $t0, 0x00005000 \n\t" [stack_pointer] "r" (stack_pointer)
"mtc0 $t0, $9 \n\t"
"nop \n\t"
"li $t0, 0x0000005C \n\t"
"mtc0 $t0, $13 \n\t"
"nop \n\t"
"li $t0, 0x007FFFF0 \n\t"
"mtc0 $t0, $4 \n\t"
"nop \n\t"
"li $t0, 0xFFFFFFFF \n\t"
"mtc0 $t0, $14 \n\t"
"nop \n\t"
"mtc0 $t0, $30 \n\t"
"nop \n\t"
"lui $t0, 0x0000 \n\t"
"mthi $t0 \n\t"
"nop \n\t"
"mtlo $t0 \n\t"
"nop \n\t"
"ctc1 $t0, $31 \n\t"
"nop \n\t"
"add $ra, $zero, %[gpr_regs] \n\t"
"ld $at, 0x08($ra) \n\t"
"ld $v0, 0x10($ra) \n\t"
"ld $v1, 0x18($ra) \n\t"
"ld $a0, 0x20($ra) \n\t"
"ld $a1, 0x28($ra) \n\t"
"ld $a2, 0x30($ra) \n\t"
"ld $a3, 0x38($ra) \n\t"
"ld $t0, 0x40($ra) \n\t"
"ld $t1, 0x48($ra) \n\t"
"ld $t2, 0x50($ra) \n\t"
"ld $t3, 0x58($ra) \n\t"
"ld $t4, 0x60($ra) \n\t"
"ld $t5, 0x68($ra) \n\t"
"ld $t6, 0x70($ra) \n\t"
"ld $t7, 0x78($ra) \n\t"
"ld $s0, 0x80($ra) \n\t"
"ld $s1, 0x88($ra) \n\t"
"ld $s2, 0x90($ra) \n\t"
"ld $s3, 0x98($ra) \n\t"
"ld $s4, 0xA0($ra) \n\t"
"ld $s5, 0xA8($ra) \n\t"
"ld $s6, 0xB0($ra) \n\t"
"ld $s7, 0xB8($ra) \n\t"
"ld $t8, 0xC0($ra) \n\t"
"ld $t9, 0xC8($ra) \n\t"
"ld $k0, 0xD0($ra) \n\t"
"ld $k1, 0xD8($ra) \n\t"
"ld $gp, 0xE0($ra) \n\t"
"ld $sp, 0xE8($ra) \n\t"
"ld $fp, 0xF0($ra) \n\t"
"ld $ra, 0xF8($ra) \n\t"
"jr $t3 \n\t"
"nop"
:
: [gpr_regs] "r" (gpr_regs)
: "t0", "ra"
); );
while (1); while (1);

View File

@ -2,62 +2,56 @@
#define BOOT_H__ #define BOOT_H__
#include "platform.h" #include <stdbool.h>
#include <stdint.h>
struct cart_header_s { typedef enum {
uint32_t pi_conf; BOOT_DEVICE_TYPE_ROM = 0,
uint32_t clock_rate; BOOT_DEVICE_TYPE_DD = 1,
uint32_t boot_addr; } boot_device_type_t;
uint32_t release_addr;
uint32_t crc_1; typedef enum {
uint32_t crc_2; BOOT_RESET_TYPE_COLD = 0,
uint32_t ___unused_1[2]; BOOT_RESET_TYPE_NMI = 1,
char name[20]; } boot_reset_type_t;
uint32_t ___unused_2;
uint32_t format; typedef enum {
char id[2]; BOOT_TV_TYPE_PAL = 0,
char country_code; BOOT_TV_TYPE_NTSC = 1,
BOOT_TV_TYPE_MPAL = 2,
} boot_tv_type_t;
typedef struct {
boot_device_type_t device_type;
boot_reset_type_t reset_type;
boot_tv_type_t tv_type;
uint8_t cic_seed;
uint8_t version; uint8_t version;
uint32_t boot_code[1008]; } boot_info_t;
} __attribute__((packed, aligned(1)));
typedef struct cart_header_s cart_header_t;
struct os_boot_config_s { typedef struct {
uint32_t tv_type; uint32_t tv_type;
uint32_t rom_type; uint32_t device_type;
uint32_t rom_base; uint32_t device_base;
uint32_t reset_type; uint32_t reset_type;
uint32_t cic_id; uint32_t cic_id;
uint32_t version; uint32_t version;
uint32_t mem_size; uint32_t mem_size;
uint8_t app_nmi_buffer[64]; uint8_t app_nmi_buffer[64];
uint32_t ___unused[37]; uint32_t __reserved_1[37];
uint32_t mem_size_6105; uint32_t mem_size_6105;
} __attribute__((packed, aligned(1))); } os_info_t;
typedef struct os_boot_config_s os_boot_config_t; #define OS_INFO_BASE (0x80000300UL)
#define OS_INFO ((os_info_t *) OS_INFO_BASE)
#define OS_BOOT_CONFIG_BASE (0xA0000300) bool boot_get_tv_type (boot_info_t *info);
#define OS_BOOT_CONFIG ((os_boot_config_t *) OS_BOOT_CONFIG_BASE) bool boot_get_cic_seed_version (boot_info_t *info);
void boot (boot_info_t *info);
#define OS_BOOT_ROM_TYPE_GAME_PAK (0)
#define OS_BOOT_ROM_TYPE_DD (1)
#define OS_BOOT_RESET_TYPE_COLD (0)
#define OS_BOOT_RESET_TYPE_NMI (1)
#define OS_BOOT_RESET_TYPE_DISK (2)
#define BOOT_SEED_IPL3(x) (((x) & 0x000000FF) >> 0)
#define BOOT_SEED_OS_VERSION(x) (((x) & 0x00000100) >> 8)
cart_header_t *boot_load_cart_header(void);
uint16_t boot_get_cic_seed(cart_header_t *cart_header);
tv_type_t boot_get_tv_type(cart_header_t *cart_header);
void boot(cart_header_t *cart_header, uint16_t cic_seed, tv_type_t tv_type);
#endif #endif

View File

@ -1,7 +1,7 @@
#include "crc32.h" #include "crc32.h"
static const uint32_t crc_table[256] = { static const uint32_t crc32_table[256] = {
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
@ -38,13 +38,13 @@ static const uint32_t crc_table[256] = {
uint32_t crc32_calculate (void *buffer, size_t length) { uint32_t crc32_calculate (void *buffer, size_t length) {
uint32_t crc32 = 0xFFFFFFFF; uint32_t crc32 = 0xFFFFFFFFUL;
uint8_t *byte_pointer = (uint8_t *) buffer; uint8_t *byte_pointer = (uint8_t *) (buffer);
while (length--) { while (length--) {
crc32 = (crc32 >> 8) ^ crc_table[(crc32 & 0xFF) ^ (*byte_pointer++)]; crc32 = (crc32 >> 8) ^ crc32_table[(crc32 & 0xFF) ^ (*byte_pointer++)];
} }
return ~crc32; return ~(crc32);
} }

View File

@ -2,7 +2,8 @@
#define CRC32_H__ #define CRC32_H__
#include "platform.h" #include <stddef.h>
#include <stdint.h>
uint32_t crc32_calculate (void *buffer, size_t length); uint32_t crc32_calculate (void *buffer, size_t length);

View File

@ -0,0 +1,359 @@
----------------------------------------------------------------------------
Revision history of FatFs module
----------------------------------------------------------------------------
R0.00 (February 26, 2006)
Prototype.
R0.01 (April 29, 2006)
The first release.
R0.02 (June 01, 2006)
Added FAT12 support.
Removed unbuffered mode.
Fixed a problem on small (<32M) partition.
R0.02a (June 10, 2006)
Added a configuration option (_FS_MINIMUM).
R0.03 (September 22, 2006)
Added f_rename().
Changed option _FS_MINIMUM to _FS_MINIMIZE.
R0.03a (December 11, 2006)
Improved cluster scan algorithm to write files fast.
Fixed f_mkdir() creates incorrect directory on FAT32.
R0.04 (February 04, 2007)
Added f_mkfs().
Supported multiple drive system.
Changed some interfaces for multiple drive system.
Changed f_mountdrv() to f_mount().
R0.04a (April 01, 2007)
Supported multiple partitions on a physical drive.
Added a capability of extending file size to f_lseek().
Added minimization level 3.
Fixed an endian sensitive code in f_mkfs().
R0.04b (May 05, 2007)
Added a configuration option _USE_NTFLAG.
Added FSINFO support.
Fixed DBCS name can result FR_INVALID_NAME.
Fixed short seek (<= csize) collapses the file object.
R0.05 (August 25, 2007)
Changed arguments of f_read(), f_write() and f_mkfs().
Fixed f_mkfs() on FAT32 creates incorrect FSINFO.
Fixed f_mkdir() on FAT32 creates incorrect directory.
R0.05a (February 03, 2008)
Added f_truncate() and f_utime().
Fixed off by one error at FAT sub-type determination.
Fixed btr in f_read() can be mistruncated.
Fixed cached sector is not flushed when create and close without write.
R0.06 (April 01, 2008)
Added fputc(), fputs(), fprintf() and fgets().
Improved performance of f_lseek() on moving to the same or following cluster.
R0.07 (April 01, 2009)
Merged Tiny-FatFs as a configuration option. (_FS_TINY)
Added long file name feature. (_USE_LFN)
Added multiple code page feature. (_CODE_PAGE)
Added re-entrancy for multitask operation. (_FS_REENTRANT)
Added auto cluster size selection to f_mkfs().
Added rewind option to f_readdir().
Changed result code of critical errors.
Renamed string functions to avoid name collision.
R0.07a (April 14, 2009)
Septemberarated out OS dependent code on reentrant cfg.
Added multiple sector size feature.
R0.07c (June 21, 2009)
Fixed f_unlink() can return FR_OK on error.
Fixed wrong cache control in f_lseek().
Added relative path feature.
Added f_chdir() and f_chdrive().
Added proper case conversion to extended character.
R0.07e (November 03, 2009)
Septemberarated out configuration options from ff.h to ffconf.h.
Fixed f_unlink() fails to remove a sub-directory on _FS_RPATH.
Fixed name matching error on the 13 character boundary.
Added a configuration option, _LFN_UNICODE.
Changed f_readdir() to return the SFN with always upper case on non-LFN cfg.
R0.08 (May 15, 2010)
Added a memory configuration option. (_USE_LFN = 3)
Added file lock feature. (_FS_SHARE)
Added fast seek feature. (_USE_FASTSEEK)
Changed some types on the API, XCHAR->TCHAR.
Changed .fname in the FILINFO structure on Unicode cfg.
String functions support UTF-8 encoding files on Unicode cfg.
R0.08a (August 16, 2010)
Added f_getcwd(). (_FS_RPATH = 2)
Added sector erase feature. (_USE_ERASE)
Moved file lock semaphore table from fs object to the bss.
Fixed f_mkfs() creates wrong FAT32 volume.
R0.08b (January 15, 2011)
Fast seek feature is also applied to f_read() and f_write().
f_lseek() reports required table size on creating CLMP.
Extended format syntax of f_printf().
Ignores duplicated directory separators in given path name.
R0.09 (September 06, 2011)
f_mkfs() supports multiple partition to complete the multiple partition feature.
Added f_fdisk().
R0.09a (August 27, 2012)
Changed f_open() and f_opendir() reject null object pointer to avoid crash.
Changed option name _FS_SHARE to _FS_LOCK.
Fixed assertion failure due to OS/2 EA on FAT12/16 volume.
R0.09b (January 24, 2013)
Added f_setlabel() and f_getlabel().
R0.10 (October 02, 2013)
Added selection of character encoding on the file. (_STRF_ENCODE)
Added f_closedir().
Added forced full FAT scan for f_getfree(). (_FS_NOFSINFO)
Added forced mount feature with changes of f_mount().
Improved behavior of volume auto detection.
Improved write throughput of f_puts() and f_printf().
Changed argument of f_chdrive(), f_mkfs(), disk_read() and disk_write().
Fixed f_write() can be truncated when the file size is close to 4GB.
Fixed f_open(), f_mkdir() and f_setlabel() can return incorrect value on error.
R0.10a (January 15, 2014)
Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID)
Added a configuration option of minimum sector size. (_MIN_SS)
2nd argument of f_rename() can have a drive number and it will be ignored.
Fixed f_mount() with forced mount fails when drive number is >= 1. (appeared at R0.10)
Fixed f_close() invalidates the file object without volume lock.
Fixed f_closedir() returns but the volume lock is left acquired. (appeared at R0.10)
Fixed creation of an entry with LFN fails on too many SFN collisions. (appeared at R0.07)
R0.10b (May 19, 2014)
Fixed a hard error in the disk I/O layer can collapse the directory entry.
Fixed LFN entry is not deleted when delete/rename an object with lossy converted SFN. (appeared at R0.07)
R0.10c (November 09, 2014)
Added a configuration option for the platforms without RTC. (_FS_NORTC)
Changed option name _USE_ERASE to _USE_TRIM.
Fixed volume label created by Mac OS X cannot be retrieved with f_getlabel(). (appeared at R0.09b)
Fixed a potential problem of FAT access that can appear on disk error.
Fixed null pointer dereference on attempting to delete the root direcotry. (appeared at R0.08)
R0.11 (February 09, 2015)
Added f_findfirst(), f_findnext() and f_findclose(). (_USE_FIND)
Fixed f_unlink() does not remove cluster chain of the file. (appeared at R0.10c)
Fixed _FS_NORTC option does not work properly. (appeared at R0.10c)
R0.11a (September 05, 2015)
Fixed wrong media change can lead a deadlock at thread-safe configuration.
Added code page 771, 860, 861, 863, 864, 865 and 869. (_CODE_PAGE)
Removed some code pages actually not exist on the standard systems. (_CODE_PAGE)
Fixed errors in the case conversion teble of code page 437 and 850 (ff.c).
Fixed errors in the case conversion teble of Unicode (cc*.c).
R0.12 (April 12, 2016)
Added support for exFAT file system. (_FS_EXFAT)
Added f_expand(). (_USE_EXPAND)
Changed some members in FINFO structure and behavior of f_readdir().
Added an option _USE_CHMOD.
Removed an option _WORD_ACCESS.
Fixed errors in the case conversion table of Unicode (cc*.c).
R0.12a (July 10, 2016)
Added support for creating exFAT volume with some changes of f_mkfs().
Added a file open method FA_OPEN_APPEND. An f_lseek() following f_open() is no longer needed.
f_forward() is available regardless of _FS_TINY.
Fixed f_mkfs() creates wrong volume. (appeared at R0.12)
Fixed wrong memory read in create_name(). (appeared at R0.12)
Fixed compilation fails at some configurations, _USE_FASTSEEK and _USE_FORWARD.
R0.12b (September 04, 2016)
Made f_rename() be able to rename objects with the same name but case.
Fixed an error in the case conversion teble of code page 866. (ff.c)
Fixed writing data is truncated at the file offset 4GiB on the exFAT volume. (appeared at R0.12)
Fixed creating a file in the root directory of exFAT volume can fail. (appeared at R0.12)
Fixed f_mkfs() creating exFAT volume with too small cluster size can collapse unallocated memory. (appeared at R0.12)
Fixed wrong object name can be returned when read directory at Unicode cfg. (appeared at R0.12)
Fixed large file allocation/removing on the exFAT volume collapses allocation bitmap. (appeared at R0.12)
Fixed some internal errors in f_expand() and f_lseek(). (appeared at R0.12)
R0.12c (March 04, 2017)
Improved write throughput at the fragmented file on the exFAT volume.
Made memory usage for exFAT be able to be reduced as decreasing _MAX_LFN.
Fixed successive f_getfree() can return wrong count on the FAT12/16 volume. (appeared at R0.12)
Fixed configuration option _VOLUMES cannot be set 10. (appeared at R0.10c)
R0.13 (May 21, 2017)
Changed heading character of configuration keywords "_" to "FF_".
Removed ASCII-only configuration, FF_CODE_PAGE = 1. Use FF_CODE_PAGE = 437 instead.
Added f_setcp(), run-time code page configuration. (FF_CODE_PAGE = 0)
Improved cluster allocation time on stretch a deep buried cluster chain.
Improved processing time of f_mkdir() with large cluster size by using FF_USE_LFN = 3.
Improved NoFatChain flag of the fragmented file to be set after it is truncated and got contiguous.
Fixed archive attribute is left not set when a file on the exFAT volume is renamed. (appeared at R0.12)
Fixed exFAT FAT entry can be collapsed when write or lseek operation to the existing file is done. (appeared at R0.12c)
Fixed creating a file can fail when a new cluster allocation to the exFAT directory occures. (appeared at R0.12c)
R0.13a (October 14, 2017)
Added support for UTF-8 encoding on the API. (FF_LFN_UNICODE = 2)
Added options for file name output buffer. (FF_LFN_BUF, FF_SFN_BUF).
Added dynamic memory allocation option for working buffer of f_mkfs() and f_fdisk().
Fixed f_fdisk() and f_mkfs() create the partition table with wrong CHS parameters. (appeared at R0.09)
Fixed f_unlink() can cause lost clusters at fragmented file on the exFAT volume. (appeared at R0.12c)
Fixed f_setlabel() rejects some valid characters for exFAT volume. (appeared at R0.12)
R0.13b (April 07, 2018)
Added support for UTF-32 encoding on the API. (FF_LFN_UNICODE = 3)
Added support for Unix style volume ID. (FF_STR_VOLUME_ID = 2)
Fixed accesing any object on the exFAT root directory beyond the cluster boundary can fail. (appeared at R0.12c)
Fixed f_setlabel() does not reject some invalid characters. (appeared at R0.09b)
R0.13c (October 14, 2018)
Supported stdint.h for C99 and later. (integer.h was included in ff.h)
Fixed reading a directory gets infinite loop when the last directory entry is not empty. (appeared at R0.12)
Fixed creating a sub-directory in the fragmented sub-directory on the exFAT volume collapses FAT chain of the parent directory. (appeared at R0.12)
Fixed f_getcwd() cause output buffer overrun when the buffer has a valid drive number. (appeared at R0.13b)
R0.14 (October 14, 2019)
Added support for 64-bit LBA and GUID partition table (FF_LBA64 = 1)
Changed some API functions, f_mkfs() and f_fdisk().
Fixed f_open() function cannot find the file with file name in length of FF_MAX_LFN characters.
Fixed f_readdir() function cannot retrieve long file names in length of FF_MAX_LFN - 1 characters.
Fixed f_readdir() function returns file names with wrong case conversion. (appeared at R0.12)
Fixed f_mkfs() function can fail to create exFAT volume in the second partition. (appeared at R0.12)
R0.14a (December 5, 2020)
Limited number of recursive calls in f_findnext().
Fixed old floppy disks formatted with MS-DOS 2.x and 3.x cannot be mounted.
Fixed some compiler warnings.
R0.14b (April 17, 2021)
Made FatFs uses standard library <string.h> for copy, compare and search instead of built-in string functions.
Added support for long long integer and floating point to f_printf(). (FF_STRF_LLI and FF_STRF_FP)
Made path name parser ignore the terminating separator to allow "dir/".
Improved the compatibility in Unix style path name feature.
Fixed the file gets dead-locked when f_open() failed with some conditions. (appeared at R0.12a)
Fixed f_mkfs() can create wrong exFAT volume due to a timing dependent error. (appeared at R0.12)
Fixed code page 855 cannot be set by f_setcp().
Fixed some compiler warnings.

View File

@ -0,0 +1,21 @@
FatFs Module Source Files R0.14b
FILES
00readme.txt This file.
00history.txt Revision history.
ff.c FatFs module.
ffconf.h Configuration file of FatFs module.
ff.h Common include file for FatFs and application module.
diskio.h Common include file for FatFs and disk I/O module.
diskio.c An example of glue function to attach existing disk I/O module to FatFs.
ffunicode.c Optional Unicode utility functions.
ffsystem.c An example of optional O/S related functions.
Low level disk I/O module is not included in this archive because the FatFs
module is only a generic file system layer and it does not depend on any specific
storage device. You need to provide a low level disk I/O module written to
control the storage device that attached to the target system.

37
sw/n64/src/fatfs/diskio.c Normal file
View File

@ -0,0 +1,37 @@
#include "ff.h"
#include "diskio.h"
#include "../sc64.h"
DSTATUS disk_status (BYTE pdrv) {
return 0;
}
DSTATUS disk_initialize (BYTE pdrv) {
return 0;
}
DRESULT disk_read (BYTE pdrv, BYTE *buff, LBA_t sector, UINT count) {
if (pdrv == 0) {
return RES_NOTRDY;
} else if (pdrv == 1) {
sc64_debug_fsd_read(buff, sector, count);
return RES_OK;
}
return RES_PARERR;
}
#if FF_FS_READONLY == 0
DRESULT disk_write (BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count) {
if (pdrv == 0) {
return RES_NOTRDY;
} else if (pdrv == 1) {
sc64_debug_fsd_write(buff, sector, count);
return RES_OK;
}
return RES_PARERR;
}
#endif
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void *buff) {
return RES_PARERR;
}

77
sw/n64/src/fatfs/diskio.h Normal file
View File

@ -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

6982
sw/n64/src/fatfs/ff.c Normal file

File diff suppressed because it is too large Load Diff

422
sw/n64/src/fatfs/ff.h Normal file
View File

@ -0,0 +1,422 @@
/*----------------------------------------------------------------------------/
/ FatFs - Generic FAT Filesystem module R0.14b /
/-----------------------------------------------------------------------------/
/
/ Copyright (C) 2021, ChaN, all right reserved.
/
/ FatFs module is an open source software. Redistribution and use of FatFs in
/ source and binary forms, with or without modification, are permitted provided
/ that the following condition is met:
/ 1. Redistributions of source code must retain the above copyright notice,
/ this condition and the following disclaimer.
/
/ This software is provided by the copyright holder and contributors "AS IS"
/ and any warranties related to this software are DISCLAIMED.
/ The copyright owner or contributors be NOT LIABLE for any damages caused
/ by use of this software.
/
/----------------------------------------------------------------------------*/
#ifndef FF_DEFINED
#define FF_DEFINED 86631 /* Revision ID */
#ifdef __cplusplus
extern "C" {
#endif
#include "ffconf.h" /* FatFs configuration options */
#if FF_DEFINED != FFCONF_DEF
#error Wrong configuration file (ffconf.h).
#endif
/* Integer types used for FatFs API */
#if defined(_WIN32) /* Windows VC++ (for development only) */
#define FF_INTDEF 2
#include <windows.h>
typedef unsigned __int64 QWORD;
#include <float.h>
#define isnan(v) _isnan(v)
#define isinf(v) (!_finite(v))
#elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) /* C99 or later */
#define FF_INTDEF 2
#include <stdint.h>
typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
typedef unsigned char BYTE; /* char must be 8-bit */
typedef uint16_t WORD; /* 16-bit unsigned integer */
typedef uint32_t DWORD; /* 32-bit unsigned integer */
typedef uint64_t QWORD; /* 64-bit unsigned integer */
typedef WORD WCHAR; /* UTF-16 character type */
#else /* Earlier than C99 */
#define FF_INTDEF 1
typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
typedef unsigned char BYTE; /* char must be 8-bit */
typedef unsigned short WORD; /* 16-bit unsigned integer */
typedef unsigned long DWORD; /* 32-bit unsigned integer */
typedef WORD WCHAR; /* UTF-16 character type */
#endif
/* Type of file size and LBA variables */
#if FF_FS_EXFAT
#if FF_INTDEF != 2
#error exFAT feature wants C99 or later
#endif
typedef QWORD FSIZE_t;
#if FF_LBA64
typedef QWORD LBA_t;
#else
typedef DWORD LBA_t;
#endif
#else
#if FF_LBA64
#error exFAT needs to be enabled when enable 64-bit LBA
#endif
typedef DWORD FSIZE_t;
typedef DWORD LBA_t;
#endif
/* Type of path name strings on FatFs API (TCHAR) */
#if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */
typedef WCHAR TCHAR;
#define _T(x) L ## x
#define _TEXT(x) L ## x
#elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */
typedef char TCHAR;
#define _T(x) u8 ## x
#define _TEXT(x) u8 ## x
#elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */
typedef DWORD TCHAR;
#define _T(x) U ## x
#define _TEXT(x) U ## x
#elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3)
#error Wrong FF_LFN_UNICODE setting
#else /* ANSI/OEM code in SBCS/DBCS */
typedef char TCHAR;
#define _T(x) x
#define _TEXT(x) x
#endif
/* Definitions of volume management */
#if FF_MULTI_PARTITION /* Multiple partition configuration */
typedef struct {
BYTE pd; /* Physical drive number */
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
} PARTITION;
extern PARTITION VolToPart[]; /* Volume - Partition mapping table */
#endif
#if FF_STR_VOLUME_ID
#ifndef FF_VOLUME_STRS
extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */
#endif
#endif
/* Filesystem object structure (FATFS) */
typedef struct {
BYTE fs_type; /* Filesystem type (0:not mounted) */
BYTE pdrv; /* Associated physical drive */
BYTE n_fats; /* Number of FATs (1 or 2) */
BYTE wflag; /* win[] flag (b0:dirty) */
BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */
WORD id; /* Volume mount ID */
WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
WORD csize; /* Cluster size [sectors] */
#if FF_MAX_SS != FF_MIN_SS
WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */
#endif
#if FF_USE_LFN
WCHAR* lfnbuf; /* LFN working buffer */
#endif
#if FF_FS_EXFAT
BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */
#endif
#if FF_FS_REENTRANT
FF_SYNC_t sobj; /* Identifier of sync object */
#endif
#if !FF_FS_READONLY
DWORD last_clst; /* Last allocated cluster */
DWORD free_clst; /* Number of free clusters */
#endif
#if FF_FS_RPATH
DWORD cdir; /* Current directory start cluster (0:root) */
#if FF_FS_EXFAT
DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */
DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */
DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */
#endif
#endif
DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */
DWORD fsize; /* Size of an FAT [sectors] */
LBA_t volbase; /* Volume base sector */
LBA_t fatbase; /* FAT base sector */
LBA_t dirbase; /* Root directory base sector/cluster */
LBA_t database; /* Data base sector */
#if FF_FS_EXFAT
LBA_t bitbase; /* Allocation bitmap base sector */
#endif
LBA_t winsect; /* Current sector appearing in the win[] */
BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
} FATFS;
/* Object ID and allocation information (FFOBJID) */
typedef struct {
FATFS* fs; /* Pointer to the hosting volume of this object */
WORD id; /* Hosting volume mount ID */
BYTE attr; /* Object attribute */
BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */
DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */
FSIZE_t objsize; /* Object size (valid when sclust != 0) */
#if FF_FS_EXFAT
DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */
DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */
DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */
DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */
DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */
#endif
#if FF_FS_LOCK
UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
#endif
} FFOBJID;
/* File object structure (FIL) */
typedef struct {
FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */
BYTE flag; /* File status flags */
BYTE err; /* Abort flag (error code) */
FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */
DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */
LBA_t sect; /* Sector number appearing in buf[] (0:invalid) */
#if !FF_FS_READONLY
LBA_t dir_sect; /* Sector number containing the directory entry (not used at exFAT) */
BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */
#endif
#if FF_USE_FASTSEEK
DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */
#endif
#if !FF_FS_TINY
BYTE buf[FF_MAX_SS]; /* File private data read/write window */
#endif
} FIL;
/* Directory object structure (DIR) */
typedef struct {
FFOBJID obj; /* Object identifier */
DWORD dptr; /* Current read/write offset */
DWORD clust; /* Current cluster */
LBA_t sect; /* Current sector (0:Read operation has terminated) */
BYTE* dir; /* Pointer to the directory item in the win[] */
BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */
#if FF_USE_LFN
DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */
#endif
#if FF_USE_FIND
const TCHAR* pat; /* Pointer to the name matching pattern */
#endif
} DIR;
/* File information structure (FILINFO) */
typedef struct {
FSIZE_t fsize; /* File size */
WORD fdate; /* Modified date */
WORD ftime; /* Modified time */
BYTE fattrib; /* File attribute */
#if FF_USE_LFN
TCHAR altname[FF_SFN_BUF + 1];/* Altenative file name */
TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */
#else
TCHAR fname[12 + 1]; /* File name */
#endif
} FILINFO;
/* Format parameter structure (MKFS_PARM) */
typedef struct {
BYTE fmt; /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */
BYTE n_fat; /* Number of FATs */
UINT align; /* Data area alignment (sector) */
UINT n_root; /* Number of root directory entries */
DWORD au_size; /* Cluster size (byte) */
} MKFS_PARM;
/* File function return code (FRESULT) */
typedef enum {
FR_OK = 0, /* (0) Succeeded */
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
FR_INT_ERR, /* (2) Assertion failed */
FR_NOT_READY, /* (3) The physical drive cannot work */
FR_NO_FILE, /* (4) Could not find the file */
FR_NO_PATH, /* (5) Could not find the path */
FR_INVALID_NAME, /* (6) The path name format is invalid */
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
FR_EXIST, /* (8) Access denied due to prohibited access */
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
FR_NOT_ENABLED, /* (12) The volume has no work area */
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
} FRESULT;
/*--------------------------------------------------------------*/
/* FatFs module application interface */
FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
FRESULT f_close (FIL* fp); /* Close an open file object */
FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */
FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */
FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */
FRESULT f_truncate (FIL* fp); /* Truncate the file */
FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */
FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */
FRESULT f_closedir (DIR* dp); /* Close an open directory */
FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */
FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */
FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */
FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */
FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */
FRESULT f_chdir (const TCHAR* path); /* Change current directory */
FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
FRESULT f_expand (FIL* fp, FSIZE_t fsz, BYTE opt); /* Allocate a contiguous block to the file */
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
FRESULT f_mkfs (const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len); /* Create a FAT volume */
FRESULT f_fdisk (BYTE pdrv, const LBA_t ptbl[], void* work); /* Divide a physical drive into some partitions */
FRESULT f_setcp (WORD cp); /* Set current code page */
int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */
int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
#define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
#define f_error(fp) ((fp)->err)
#define f_tell(fp) ((fp)->fptr)
#define f_size(fp) ((fp)->obj.objsize)
#define f_rewind(fp) f_lseek((fp), 0)
#define f_rewinddir(dp) f_readdir((dp), 0)
#define f_rmdir(path) f_unlink(path)
#define f_unmount(path) f_mount(0, path, 0)
/*--------------------------------------------------------------*/
/* Additional user defined functions */
/* RTC function */
#if !FF_FS_READONLY && !FF_FS_NORTC
DWORD get_fattime (void);
#endif
/* LFN support functions */
#if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */
WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */
WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */
DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */
#endif
#if FF_USE_LFN == 3 /* Dynamic memory allocation */
void* ff_memalloc (UINT msize); /* Allocate memory block */
void ff_memfree (void* mblock); /* Free memory block */
#endif
/* Sync functions */
#if FF_FS_REENTRANT
int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */
int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */
void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */
int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */
#endif
/*--------------------------------------------------------------*/
/* Flags and offset address */
/* File access mode and open method flags (3rd argument of f_open) */
#define FA_READ 0x01
#define FA_WRITE 0x02
#define FA_OPEN_EXISTING 0x00
#define FA_CREATE_NEW 0x04
#define FA_CREATE_ALWAYS 0x08
#define FA_OPEN_ALWAYS 0x10
#define FA_OPEN_APPEND 0x30
/* Fast seek controls (2nd argument of f_lseek) */
#define CREATE_LINKMAP ((FSIZE_t)0 - 1)
/* Format options (2nd argument of f_mkfs) */
#define FM_FAT 0x01
#define FM_FAT32 0x02
#define FM_EXFAT 0x04
#define FM_ANY 0x07
#define FM_SFD 0x08
/* Filesystem type (FATFS.fs_type) */
#define FS_FAT12 1
#define FS_FAT16 2
#define FS_FAT32 3
#define FS_EXFAT 4
/* File attribute bits for directory entry (FILINFO.fattrib) */
#define AM_RDO 0x01 /* Read only */
#define AM_HID 0x02 /* Hidden */
#define AM_SYS 0x04 /* System */
#define AM_DIR 0x10 /* Directory */
#define AM_ARC 0x20 /* Archive */
#ifdef __cplusplus
}
#endif
#endif /* FF_DEFINED */

301
sw/n64/src/fatfs/ffconf.h Normal file
View File

@ -0,0 +1,301 @@
/*---------------------------------------------------------------------------/
/ FatFs Functional Configurations
/---------------------------------------------------------------------------*/
#define FFCONF_DEF 86631 /* Revision ID */
/*---------------------------------------------------------------------------/
/ Function Configurations
/---------------------------------------------------------------------------*/
#define FF_FS_READONLY 1
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
/ Read-only configuration removes writing API functions, f_write(), f_sync(),
/ f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
/ and optional writing functions as well. */
#define FF_FS_MINIMIZE 1
/* This option defines minimization level to remove some basic API functions.
/
/ 0: Basic functions are fully enabled.
/ 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
/ are removed.
/ 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
/ 3: f_lseek() function is removed in addition to 2. */
#define FF_USE_FIND 0
/* This option switches filtered directory read functions, f_findfirst() and
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
#define FF_USE_MKFS 0
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
#define FF_USE_FASTSEEK 1
/* This option switches fast seek function. (0:Disable or 1:Enable) */
#define FF_USE_EXPAND 0
/* This option switches f_expand function. (0:Disable or 1:Enable) */
#define FF_USE_CHMOD 0
/* This option switches attribute manipulation functions, f_chmod() and f_utime().
/ (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */
#define FF_USE_LABEL 0
/* This option switches volume label functions, f_getlabel() and f_setlabel().
/ (0:Disable or 1:Enable) */
#define FF_USE_FORWARD 0
/* This option switches f_forward() function. (0:Disable or 1:Enable) */
#define FF_USE_STRFUNC 0
#define FF_PRINT_LLI 0
#define FF_PRINT_FLOAT 0
#define FF_STRF_ENCODE 0
/* FF_USE_STRFUNC switches string functions, f_gets(), f_putc(), f_puts() and
/ f_printf().
/
/ 0: Disable. FF_PRINT_LLI, FF_PRINT_FLOAT and FF_STRF_ENCODE have no effect.
/ 1: Enable without LF-CRLF conversion.
/ 2: Enable with LF-CRLF conversion.
/
/ FF_PRINT_LLI = 1 makes f_printf() support long long argument and FF_PRINT_FLOAT = 1/2
makes f_printf() support floating point argument. These features want C99 or later.
/ When FF_LFN_UNICODE >= 1 with LFN enabled, string functions convert the character
/ encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE
/ to be read/written via those functions.
/
/ 0: ANSI/OEM in current CP
/ 1: Unicode in UTF-16LE
/ 2: Unicode in UTF-16BE
/ 3: Unicode in UTF-8
*/
/*---------------------------------------------------------------------------/
/ Locale and Namespace Configurations
/---------------------------------------------------------------------------*/
#define FF_CODE_PAGE 437
/* This option specifies the OEM code page to be used on the target system.
/ Incorrect code page setting can cause a file open failure.
/
/ 437 - U.S.
/ 720 - Arabic
/ 737 - Greek
/ 771 - KBL
/ 775 - Baltic
/ 850 - Latin 1
/ 852 - Latin 2
/ 855 - Cyrillic
/ 857 - Turkish
/ 860 - Portuguese
/ 861 - Icelandic
/ 862 - Hebrew
/ 863 - Canadian French
/ 864 - Arabic
/ 865 - Nordic
/ 866 - Russian
/ 869 - Greek 2
/ 932 - Japanese (DBCS)
/ 936 - Simplified Chinese (DBCS)
/ 949 - Korean (DBCS)
/ 950 - Traditional Chinese (DBCS)
/ 0 - Include all code pages above and configured by f_setcp()
*/
#define FF_USE_LFN 2
#define FF_MAX_LFN 255
/* The FF_USE_LFN switches the support for LFN (long file name).
/
/ 0: Disable LFN. FF_MAX_LFN has no effect.
/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
/ 2: Enable LFN with dynamic working buffer on the STACK.
/ 3: Enable LFN with dynamic working buffer on the HEAP.
/
/ To enable the LFN, ffunicode.c needs to be added to the project. The LFN function
/ requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
/ additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.
/ The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can
/ be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN
/ specification.
/ When use stack for the working buffer, take care on stack overflow. When use heap
/ memory for the working buffer, memory management functions, ff_memalloc() and
/ ff_memfree() exemplified in ffsystem.c, need to be added to the project. */
#define FF_LFN_UNICODE 0
/* This option switches the character encoding on the API when LFN is enabled.
/
/ 0: ANSI/OEM in current CP (TCHAR = char)
/ 1: Unicode in UTF-16 (TCHAR = WCHAR)
/ 2: Unicode in UTF-8 (TCHAR = char)
/ 3: Unicode in UTF-32 (TCHAR = DWORD)
/
/ Also behavior of string I/O functions will be affected by this option.
/ When LFN is not enabled, this option has no effect. */
#define FF_LFN_BUF 255
#define FF_SFN_BUF 12
/* This set of options defines size of file name members in the FILINFO structure
/ which is used to read out directory items. These values should be suffcient for
/ the file names to read. The maximum possible length of the read file name depends
/ on character encoding. When LFN is not enabled, these options have no effect. */
#define FF_FS_RPATH 2
/* This option configures support for relative path.
/
/ 0: Disable relative path and remove related functions.
/ 1: Enable relative path. f_chdir() and f_chdrive() are available.
/ 2: f_getcwd() function is available in addition to 1.
*/
/*---------------------------------------------------------------------------/
/ Drive/Volume Configurations
/---------------------------------------------------------------------------*/
#define FF_VOLUMES 2
/* Number of volumes (logical drives) to be used. (1-10) */
#define FF_STR_VOLUME_ID 0
#define FF_VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3"
/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
/ When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
/ number in the path name. FF_VOLUME_STRS defines the volume ID strings for each
/ logical drives. Number of items must not be less than FF_VOLUMES. Valid
/ characters for the volume ID strings are A-Z, a-z and 0-9, however, they are
/ compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is
/ not defined, a user defined volume string table needs to be defined as:
/
/ const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",...
*/
#define FF_MULTI_PARTITION 0
/* This option switches support for multiple volumes on the physical drive.
/ By default (0), each logical drive number is bound to the same physical drive
/ number and only an FAT volume found on the physical drive will be mounted.
/ When this function is enabled (1), each logical drive number can be bound to
/ arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
/ funciton will be available. */
#define FF_MIN_SS 512
#define FF_MAX_SS 512
/* This set of options configures the range of sector size to be supported. (512,
/ 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and
/ harddisk, but a larger value may be required for on-board flash memory and some
/ type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured
/ for variable sector size mode and disk_ioctl() function needs to implement
/ GET_SECTOR_SIZE command. */
#define FF_LBA64 0
/* This option switches support for 64-bit LBA. (0:Disable or 1:Enable)
/ To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */
#define FF_MIN_GPT 0x10000000
/* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and
/ f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */
#define FF_USE_TRIM 0
/* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)
/ To enable Trim function, also CTRL_TRIM command should be implemented to the
/ disk_ioctl() function. */
/*---------------------------------------------------------------------------/
/ System Configurations
/---------------------------------------------------------------------------*/
#define FF_FS_TINY 0
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
/ At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes.
/ Instead of private sector buffer eliminated from the file object, common sector
/ buffer in the filesystem object (FATFS) is used for the file data transfer. */
#define FF_FS_EXFAT 1
/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
/ To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1)
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */
#define FF_FS_NORTC 1
#define FF_NORTC_MON 1
#define FF_NORTC_MDAY 1
#define FF_NORTC_YEAR 2020
/* The option FF_FS_NORTC switches timestamp functiton. If the system does not have
/ any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable
/ the timestamp function. Every object modified by FatFs will have a fixed timestamp
/ defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time.
/ To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be
/ added to the project to read current time form real-time clock. FF_NORTC_MON,
/ FF_NORTC_MDAY and FF_NORTC_YEAR have no effect.
/ These options have no effect in read-only configuration (FF_FS_READONLY = 1). */
#define FF_FS_NOFSINFO 0
/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
/ option, and f_getfree() function at first time after volume mount will force
/ a full FAT scan. Bit 1 controls the use of last allocated cluster number.
/
/ bit0=0: Use free cluster count in the FSINFO if available.
/ bit0=1: Do not trust free cluster count in the FSINFO.
/ bit1=0: Use last allocated cluster number in the FSINFO if available.
/ bit1=1: Do not trust last allocated cluster number in the FSINFO.
*/
#define FF_FS_LOCK 0
/* The option FF_FS_LOCK switches file lock function to control duplicated file open
/ and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
/ is 1.
/
/ 0: Disable file lock function. To avoid volume corruption, application program
/ should avoid illegal open, remove and rename to the open objects.
/ >0: Enable file lock function. The value defines how many files/sub-directories
/ can be opened simultaneously under file lock control. Note that the file
/ lock control is independent of re-entrancy. */
/* #include <somertos.h> // O/S definitions */
#define FF_FS_REENTRANT 0
#define FF_FS_TIMEOUT 1000
#define FF_SYNC_t HANDLE
/* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
/ module itself. Note that regardless of this option, file access to different
/ volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
/ and f_fdisk() function, are always not re-entrant. Only file/directory access
/ to the same volume is under control of this function.
/
/ 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect.
/ 1: Enable re-entrancy. Also user provided synchronization handlers,
/ ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
/ function, must be added to the project. Samples are available in
/ option/syscall.c.
/
/ The FF_FS_TIMEOUT defines timeout period in unit of time tick.
/ The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
/ SemaphoreHandle_t and etc. A header file for O/S definitions needs to be
/ included somewhere in the scope of ff.h. */
/*--- End of configuration options ---*/

170
sw/n64/src/fatfs/ffsystem.c Normal file
View File

@ -0,0 +1,170 @@
/*------------------------------------------------------------------------*/
/* Sample Code of OS Dependent Functions for FatFs */
/* (C)ChaN, 2018 */
/*------------------------------------------------------------------------*/
#include "ff.h"
#if FF_USE_LFN == 3 /* Dynamic memory allocation */
/*------------------------------------------------------------------------*/
/* Allocate a memory block */
/*------------------------------------------------------------------------*/
void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
UINT msize /* Number of bytes to allocate */
)
{
return malloc(msize); /* Allocate a new memory block with POSIX API */
}
/*------------------------------------------------------------------------*/
/* Free a memory block */
/*------------------------------------------------------------------------*/
void ff_memfree (
void* mblock /* Pointer to the memory block to free (nothing to do if null) */
)
{
free(mblock); /* Free the memory block with POSIX API */
}
#endif
#if FF_FS_REENTRANT /* Mutal exclusion */
/*------------------------------------------------------------------------*/
/* Create a Synchronization Object */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to create a new
/ synchronization object for the volume, such as semaphore and mutex.
/ When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
*/
//const osMutexDef_t Mutex[FF_VOLUMES]; /* Table of CMSIS-RTOS mutex */
int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
BYTE vol, /* Corresponding volume (logical drive number) */
FF_SYNC_t* sobj /* Pointer to return the created sync object */
)
{
/* Win32 */
*sobj = CreateMutex(NULL, FALSE, NULL);
return (int)(*sobj != INVALID_HANDLE_VALUE);
/* uITRON */
// T_CSEM csem = {TA_TPRI,1,1};
// *sobj = acre_sem(&csem);
// return (int)(*sobj > 0);
/* uC/OS-II */
// OS_ERR err;
// *sobj = OSMutexCreate(0, &err);
// return (int)(err == OS_NO_ERR);
/* FreeRTOS */
// *sobj = xSemaphoreCreateMutex();
// return (int)(*sobj != NULL);
/* CMSIS-RTOS */
// *sobj = osMutexCreate(&Mutex[vol]);
// return (int)(*sobj != NULL);
}
/*------------------------------------------------------------------------*/
/* Delete a Synchronization Object */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to delete a synchronization
/ object that created with ff_cre_syncobj() function. When a 0 is returned,
/ the f_mount() function fails with FR_INT_ERR.
*/
int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */
FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
)
{
/* Win32 */
return (int)CloseHandle(sobj);
/* uITRON */
// return (int)(del_sem(sobj) == E_OK);
/* uC/OS-II */
// OS_ERR err;
// OSMutexDel(sobj, OS_DEL_ALWAYS, &err);
// return (int)(err == OS_NO_ERR);
/* FreeRTOS */
// vSemaphoreDelete(sobj);
// return 1;
/* CMSIS-RTOS */
// return (int)(osMutexDelete(sobj) == osOK);
}
/*------------------------------------------------------------------------*/
/* Request Grant to Access the Volume */
/*------------------------------------------------------------------------*/
/* This function is called on entering file functions to lock the volume.
/ When a 0 is returned, the file function fails with FR_TIMEOUT.
*/
int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
FF_SYNC_t sobj /* Sync object to wait */
)
{
/* Win32 */
return (int)(WaitForSingleObject(sobj, FF_FS_TIMEOUT) == WAIT_OBJECT_0);
/* uITRON */
// return (int)(wai_sem(sobj) == E_OK);
/* uC/OS-II */
// OS_ERR err;
// OSMutexPend(sobj, FF_FS_TIMEOUT, &err));
// return (int)(err == OS_NO_ERR);
/* FreeRTOS */
// return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE);
/* CMSIS-RTOS */
// return (int)(osMutexWait(sobj, FF_FS_TIMEOUT) == osOK);
}
/*------------------------------------------------------------------------*/
/* Release Grant to Access the Volume */
/*------------------------------------------------------------------------*/
/* This function is called on leaving file functions to unlock the volume.
*/
void ff_rel_grant (
FF_SYNC_t sobj /* Sync object to be signaled */
)
{
/* Win32 */
ReleaseMutex(sobj);
/* uITRON */
// sig_sem(sobj);
/* uC/OS-II */
// OSMutexPost(sobj);
/* FreeRTOS */
// xSemaphoreGive(sobj);
/* CMSIS-RTOS */
// osMutexRelease(sobj);
}
#endif

15593
sw/n64/src/fatfs/ffunicode.c Normal file

File diff suppressed because it is too large Load Diff

12
sw/n64/src/init.c Normal file
View File

@ -0,0 +1,12 @@
#include "sys.h"
#include "sc64.h"
void init (void) {
uint32_t pifram = si_io_read((io32_t *) (&PIFRAM[0x3C]));
si_io_write((io32_t *) (&PIFRAM[0x3C]), pifram | 0x08);
sc64_init();
LOG_I("Initialized\r\n");
}

View File

@ -1,19 +1,71 @@
#include "sc64.h"
#include "boot.h" #include "boot.h"
#include "sc64.h"
#include "storage.h"
int main(void) { void main (void) {
OS_BOOT_CONFIG->tv_type = TV_NTSC; boot_info_t boot_info;
sc64_info_t sc64_info;
while (!sc64_check_presence()); boot_info.reset_type = OS_INFO->reset_type;
sc64_wait_cpu_ready(); sc64_get_info(&sc64_info);
sc64_set_config(CFG_ID_SDRAM_SWITCH, true); LOG_I("Bootloader version: %.32s\r\n", sc64_info.bootloader_version);
cart_header_t *cart_header = boot_load_cart_header(); switch (sc64_info.boot_mode) {
uint16_t cic_seed = boot_get_cic_seed(cart_header); case BOOT_MODE_MENU:
tv_type_t tv_type = boot_get_tv_type(cart_header); LOG_I("Running menu from SD card\r\n");
storage_run_menu(STORAGE_BACKEND_SD, &boot_info, &sc64_info);
break;
boot(cart_header, cic_seed, tv_type); case BOOT_MODE_ROM:
LOG_I("Running ROM from SDRAM\r\n");
boot_info.device_type = BOOT_DEVICE_TYPE_ROM;
break;
case BOOT_MODE_DDIPL:
LOG_I("Running DDIPL from SDRAM\r\n");
boot_info.device_type = BOOT_DEVICE_TYPE_DD;
break;
case BOOT_MODE_DIRECT:
LOG_I("Running bootloader from SDRAM - running menu from FSD\r\n");
storage_run_menu(STORAGE_BACKEND_USB, &boot_info, &sc64_info);
break;
default:
LOG_E("Unknown boot mode! - %d\r\n", sc64_info.boot_mode);
while (1);
}
if (sc64_info.tv_type != TV_TYPE_UNKNOWN) {
boot_info.tv_type = sc64_info.tv_type;
LOG_I("Using provided TV type: %d\r\n", boot_info.tv_type);
} else {
if (boot_get_tv_type(&boot_info)) {
LOG_I("Using TV type guessed from ROM header: %d\r\n", boot_info.tv_type);
} else {
boot_info.tv_type = OS_INFO->tv_type;
LOG_I("Using console TV type: %d\r\n", boot_info.tv_type);
}
}
if (sc64_info.cic_seed != 0xFFFF) {
boot_info.cic_seed = sc64_info.cic_seed & 0xFF;
boot_info.version = (sc64_info.cic_seed >> 8) & 0x01;
LOG_I("Using provided CIC seed and version: 0x%02X.%d\r\n", boot_info.cic_seed, boot_info.version);
} else {
if (boot_get_cic_seed_version(&boot_info)) {
LOG_I("Using CIC seed and version guessed from IPL3: 0x%02X.%d\r\n", boot_info.cic_seed, boot_info.version);
} else {
boot_info.cic_seed = 0x3F;
boot_info.version = 0;
LOG_I("Using 6102/7101 CIC seed and version: 0x%02X.%d\r\n", boot_info.cic_seed, boot_info.version);
}
}
LOG_I("Booting IPL3\r\n\r\n");
boot(&boot_info);
} }

View File

@ -1,170 +0,0 @@
#ifndef N64_REGS_H__
#define N64_REGS_H__
#include <inttypes.h>
#define ARRAY_ITEMS(x) (sizeof(x) / sizeof(x[0]))
#define CPU_ADDRESS_IN_REG(x) ((0xFFFFFFFFULL << 32) | ((uint32_t) (&(x))))
#define CPU_REG_Z0 (0)
#define CPU_REG_AT (1)
#define CPU_REG_V0 (2)
#define CPU_REG_V1 (3)
#define CPU_REG_A0 (4)
#define CPU_REG_A1 (5)
#define CPU_REG_A2 (6)
#define CPU_REG_A3 (7)
#define CPU_REG_T0 (8)
#define CPU_REG_T1 (9)
#define CPU_REG_T2 (10)
#define CPU_REG_T3 (11)
#define CPU_REG_T4 (12)
#define CPU_REG_T5 (13)
#define CPU_REG_T6 (14)
#define CPU_REG_T7 (15)
#define CPU_REG_S0 (16)
#define CPU_REG_S1 (17)
#define CPU_REG_S2 (18)
#define CPU_REG_S3 (19)
#define CPU_REG_S4 (20)
#define CPU_REG_S5 (21)
#define CPU_REG_S6 (22)
#define CPU_REG_S7 (23)
#define CPU_REG_T8 (24)
#define CPU_REG_T9 (25)
#define CPU_REG_K0 (26)
#define CPU_REG_K1 (27)
#define CPU_REG_GP (28)
#define CPU_REG_SP (29)
#define CPU_REG_FP (30)
#define CPU_REG_RA (31)
typedef struct SP_MEM_s {
volatile uint32_t dmem[1024];
volatile uint32_t imem[1024];
} SP_MEM_t;
typedef struct SP_regs_s {
volatile void *mem_addr;
volatile void *dram_addr;
volatile uint32_t rd_len;
volatile uint32_t wr_len;
volatile uint32_t status;
volatile uint32_t dma_full;
volatile uint32_t dma_busy;
volatile uint32_t semaphore;
} SP_regs_t;
typedef struct DP_CMD_regs_s {
volatile void *start;
volatile void *end;
volatile void *current;
volatile uint32_t status;
volatile uint32_t clock;
volatile uint32_t buf_busy;
volatile uint32_t pipe_busy;
volatile uint32_t tmem;
} DP_CMD_regs_t;
typedef struct MI_regs_s {
volatile uint32_t mode;
volatile uint32_t version;
volatile uint32_t interrupt;
volatile uint32_t interrupt_mask;
} MI_regs_t;
typedef struct VI_regs_s {
volatile uint32_t control;
volatile void *dram_addr;
volatile uint32_t h_width;
volatile uint32_t v_intr;
volatile uint32_t current_line;
volatile uint32_t timing;
volatile uint32_t v_sync;
volatile uint32_t h_sync;
volatile uint32_t h_sync_leap;
volatile uint32_t h_limits;
volatile uint32_t v_limits;
volatile uint32_t color_burst;
volatile uint32_t h_scale;
volatile uint32_t v_scale;
} VI_regs_t;
typedef struct AI_regs_s {
volatile void *dram_addr;
volatile uint32_t len;
volatile uint32_t control;
volatile uint32_t status;
volatile uint32_t dacrate;
volatile uint32_t bitrate;
} AI_regs_t;
typedef struct PI_regs_s {
volatile void *dram_addr;
volatile uint32_t cart_addr;
volatile uint32_t rd_len;
volatile uint32_t wr_len;
volatile uint32_t status;
volatile uint32_t dom1_lat;
volatile uint32_t dom1_pwd;
volatile uint32_t dom1_pgs;
volatile uint32_t dom1_rls;
volatile uint32_t dom2_lat;
volatile uint32_t dom2_pwd;
volatile uint32_t dom2_pgs;
volatile uint32_t dom2_rls;
} PI_regs_t;
#define SP_MEM_BASE (0xA4000000)
#define SP_REGS_BASE (0xA4040000)
#define DP_CMD_REGS_BASE (0xA4100000)
#define MI_REGS_BASE (0xA4300000)
#define VI_REGS_BASE (0xA4400000)
#define AI_REGS_BASE (0xA4500000)
#define PI_REGS_BASE (0xA4600000)
#define DDIPL_BASE (0xA6000000)
#define CART_BASE (0xB0000000)
#define SP_MEM ((volatile SP_MEM_t *) SP_MEM_BASE)
#define SP ((volatile SP_regs_t *) SP_REGS_BASE)
#define DP_CMD ((volatile DP_CMD_regs_t *) DP_CMD_REGS_BASE)
#define MI ((volatile MI_regs_t *) MI_REGS_BASE)
#define VI ((volatile VI_regs_t *) VI_REGS_BASE)
#define AI ((volatile AI_regs_t *) AI_REGS_BASE)
#define PI ((volatile PI_regs_t *) PI_REGS_BASE)
#define DDIPL ((volatile uint32_t *) DDIPL_BASE)
#define CART ((volatile uint32_t *) CART_BASE)
#define SP_STATUS_HALT (1 << 0)
#define SP_STATUS_DMA_BUSY (1 << 2)
#define SP_STATUS_SET_HALT (1 << 1)
#define SP_STATUS_CLEAR_INTERRUPT (1 << 3)
#define SP_DMA_BUSY (1 << 0)
#define DP_CMD_STATUS_XBUS_DMEM_DMA (1 << 0)
#define DP_CMD_STATUS_PIPE_BUSY (1 << 5)
#define DP_CMD_STATUS_CLEAR_XBUS_DMEM_DMA (1 << 0)
#define DP_CMD_STATUS_CLEAR_FREEZE (1 << 2)
#define DP_CMD_STATUS_CLEAR_FLUSH (1 << 4)
#define MI_INT_CLEAR_SP (1 << 0)
#define MI_INT_CLEAR_SI (1 << 2)
#define MI_INT_CLEAR_AI (1 << 4)
#define MI_INT_CLEAR_VI (1 << 6)
#define MI_INT_CLEAR_PI (1 << 8)
#define MI_INT_CLEAR_DP (1 << 10)
#define PI_STATUS_DMA_BUSY (1 << 0)
#define PI_STATUS_IO_BUSY (1 << 1)
#define PI_STATUS_RESET_CONTROLLER (1 << 0)
#define PI_STATUS_CLEAR_INTERRUPT (1 << 1)
#endif

View File

@ -1,26 +0,0 @@
#ifndef PLATFORM_H__
#define PLATFORM_H__
#include <stddef.h>
#include <stdint.h>
#include <libdragon.h>
#define BOOTLOADER_VERSION_MAJOR (1)
#define BOOTLOADER_VERSION_MINOR (0)
#define __IO volatile
typedef uint32_t reg_t;
#define platform_cache_writeback(rdram, length) data_cache_hit_writeback_invalidate(rdram, length)
#define platform_cache_invalidate(rdram, length) data_cache_hit_invalidate(rdram, length)
#define platform_pi_io_write(pi, value) io_write((uint32_t) (pi), value)
#define platform_pi_io_read(pi) io_read((uint32_t) (pi))
#define platform_pi_dma_write(rdram, pi, length) dma_write(rdram, (uint32_t) (pi), length)
#define platform_pi_dma_read(rdram, pi, length) dma_read(rdram, (uint32_t) (pi), length)
#endif

View File

@ -1,48 +1,203 @@
#include "sc64.h" #include "sc64.h"
typedef struct sc64_cart_registers { extern char header_text_info __attribute__((section(".data")));
__IO reg_t SR_CMD;
__IO reg_t DATA[2];
__IO reg_t VERSION;
} sc64_cfg_registers_t;
#define SC64_CFG_BASE (0x1FFF0000)
#define SC64_CFG ((__IO sc64_cfg_registers_t *) SC64_CFG_BASE)
bool sc64_check_presence (void) { bool sc64_check_presence (void) {
uint32_t version = platform_pi_io_read(&SC64_CFG->VERSION); uint32_t version = pi_io_read(&SC64->VERSION);
return (version == 0x53437632); return (version == SC64_VERSION_2);
} }
void sc64_wait_cpu_ready (void) { void sc64_wait_cpu_ready (void) {
uint32_t sr; uint32_t sr;
do { do {
sr = platform_pi_io_read(&SC64_CFG->SR_CMD); sr = pi_io_read(&SC64->SR_CMD);
} while (!(sr & SC64_CFG_SCR_CPU_READY)); } while (!(sr & SC64_SR_CPU_READY));
} }
void sc64_wait_cpu_busy(void) { bool sc64_wait_cpu_busy (void) {
uint32_t sr; uint32_t sr;
do { do {
sr = platform_pi_io_read(&SC64_CFG->SR_CMD); sr = pi_io_read(&SC64->SR_CMD);
} while (sr & SC64_CFG_SCR_CPU_BUSY); } while (sr & SC64_SR_CPU_BUSY);
return sr & SC64_SR_CMD_ERROR;
} }
void sc64_perform_cmd(uint8_t cmd, uint32_t *args) { bool sc64_perform_cmd (uint8_t cmd, uint32_t *args, uint32_t *result) {
for (int i = 0; i < 2; i++) { if (args != NULL) {
platform_pi_io_write(&SC64_CFG->DATA[i], args[i]); pi_io_write(&SC64->DATA[0], args[0]);
pi_io_write(&SC64->DATA[1], args[1]);
} }
platform_pi_io_write(&SC64_CFG->SR_CMD, (uint32_t) cmd); pi_io_write(&SC64->SR_CMD, ((uint32_t) (cmd)) & 0xFF);
sc64_wait_cpu_busy(); bool error = sc64_wait_cpu_busy();
for (int i = 0; i < 2; i++) { if (result != NULL) {
args[i] = platform_pi_io_read(&SC64_CFG->DATA[i]); result[0] = pi_io_read(&SC64->DATA[0]);
result[1] = pi_io_read(&SC64->DATA[1]);
}
return error;
}
uint32_t sc64_get_config (cfg_id_t id) {
uint32_t args[2] = { id, 0 };
uint32_t result[2];
sc64_perform_cmd(SC64_CMD_QUERY, args, result);
return result[1];
}
void sc64_set_config (cfg_id_t id, uint32_t value) {
uint32_t args[2] = { id, value };
sc64_perform_cmd(SC64_CMD_CONFIG, args, NULL);
}
void sc64_get_info (sc64_info_t *info) {
io32_t *src = (io32_t *) UNCACHED(&header_text_info);
uint32_t *dst = (uint32_t *) info->bootloader_version;
bool sdram_switched = sc64_get_config(CFG_ID_SDRAM_SWITCH);
if (sdram_switched) {
sc64_set_config(CFG_ID_SDRAM_SWITCH, false);
}
for (int i = 0; i < sizeof(info->bootloader_version); i += sizeof(uint32_t)) {
*dst++ = pi_io_read(src++);
}
if (sdram_switched) {
sc64_set_config(CFG_ID_SDRAM_SWITCH, true);
}
info->dd_enabled = (bool) sc64_get_config(CFG_ID_DD_ENABLE);
info->save_type = (save_type_t) sc64_get_config(CFG_ID_SAVE_TYPE);
info->cic_seed = (uint16_t) sc64_get_config(CFG_ID_CIC_SEED);
info->tv_type = (tv_type_t) sc64_get_config(CFG_ID_TV_TYPE);
info->save_location = (io32_t *) (0x10000000 | sc64_get_config(CFG_ID_SAVE_OFFEST));
info->ddipl_location = (io32_t *) (0x10000000 | sc64_get_config(CFG_ID_DDIPL_OFFEST));
info->boot_mode = (boot_mode_t) sc64_get_config(CFG_ID_BOOT_MODE);
}
void sc64_wait_usb_rx_ready (uint32_t *type, uint32_t *length) {
uint32_t result[2];
do {
sc64_perform_cmd(SC64_CMD_DEBUG_RX_READY, NULL, result);
} while (result[0] == 0 && result[1] == 0);
*type = result[0];
*length = result[1];
}
void sc64_wait_usb_rx_busy (void) {
uint32_t result[2];
do {
sc64_perform_cmd(SC64_CMD_DEBUG_RX_BUSY, NULL, result);
} while (result[0]);
}
void sc64_usb_rx_data (io32_t *address, uint32_t length) {
uint32_t args[2] = { (uint32_t) (address), ALIGN(length, 2) };
sc64_perform_cmd(SC64_CMD_DEBUG_RX_DATA, args, NULL);
}
void sc64_wait_usb_tx_ready (void) {
uint32_t result[2];
do {
sc64_perform_cmd(SC64_CMD_DEBUG_TX_READY, NULL, result);
} while (!result[0]);
}
void sc64_usb_tx_data (io32_t *address, uint32_t length) {
uint32_t args[2] = { (uint32_t) (address), ALIGN(length, 2) };
sc64_perform_cmd(SC64_CMD_DEBUG_TX_DATA, args, NULL);
}
void sc64_debug_write (uint8_t type, const void *data, uint32_t len) {
char *dma = "DMA@";
char *cmp = "CMPH";
io32_t *sdram = (io32_t *) (SC64_DEBUG_WRITE_ADDRESS);
uint32_t *src = (uint32_t *) (data);
io32_t *dst = sdram;
uint32_t copy_length = ALIGN(len, 4);
sc64_wait_usb_tx_ready();
bool writable = sc64_get_config(CFG_ID_SDRAM_WRITABLE);
bool sdram_switched = sc64_get_config(CFG_ID_SDRAM_SWITCH);
if (!writable) {
sc64_set_config(CFG_ID_SDRAM_WRITABLE, true);
}
if (!sdram_switched) {
sc64_set_config(CFG_ID_SDRAM_SWITCH, true);
}
pi_io_write(dst++, *((uint32_t *) (dma)));
pi_io_write(dst++, (type << 24) | len);
while (src < ((uint32_t *) (data + copy_length))) {
pi_io_write(dst++, *src++);
if (dst >= (io32_t *) ((void *) (sdram) + SC64_DEBUG_MAX_SIZE)) {
sc64_usb_tx_data(sdram, (dst - sdram) * sizeof(uint32_t));
sc64_wait_usb_tx_ready();
dst = sdram;
} }
} }
void sc64_set_config(uint32_t type, uint32_t value) { pi_io_write(dst++, *((uint32_t *) (cmp)));
uint32_t args[2] = { type, value };
sc64_perform_cmd(SC64_CMD_CONFIG, args); if (!writable) {
sc64_set_config(CFG_ID_SDRAM_WRITABLE, false);
}
if (!sdram_switched) {
sc64_set_config(CFG_ID_SDRAM_SWITCH, false);
}
sc64_usb_tx_data(sdram, (dst - sdram) * sizeof(uint32_t));
}
void sc64_debug_fsd_read (const void *data, uint32_t sector, uint32_t count) {
uint32_t type;
uint32_t length;
io32_t *sdram = (io32_t *) (SC64_DEBUG_READ_ADDRESS);
io32_t *src = sdram;
uint32_t *dst = (uint32_t *) (data);
uint32_t read_length = count * 512;
sc64_debug_write(SC64_DEBUG_ID_FSD_SECTOR, &sector, 4);
sc64_debug_write(SC64_DEBUG_ID_FSD_READ, &read_length, 4);
sc64_wait_usb_rx_ready(&type, &length);
sc64_usb_rx_data(sdram, length);
sc64_wait_usb_rx_busy();
uint32_t copy_length = ALIGN(length, 4);
bool sdram_switched = sc64_get_config(CFG_ID_SDRAM_SWITCH);
if (!sdram_switched) {
sc64_set_config(CFG_ID_SDRAM_SWITCH, true);
}
for (int i = 0; i < copy_length; i += 4) {
*dst++ = pi_io_read(src++);
}
if (!sdram_switched) {
sc64_set_config(CFG_ID_SDRAM_SWITCH, false);
}
}
void sc64_debug_fsd_write (const void *data, uint32_t sector, uint32_t count) {
sc64_debug_write(SC64_DEBUG_ID_FSD_SECTOR, &sector, 4);
sc64_debug_write(SC64_DEBUG_ID_FSD_WRITE, data, count * 512);
}
void sc64_init (void) {
while (!sc64_check_presence());
sc64_wait_cpu_ready();
sc64_set_config(CFG_ID_SDRAM_SWITCH, true);
} }

View File

@ -2,16 +2,57 @@
#define SC64_H__ #define SC64_H__
#include "platform.h" #include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "sys.h"
#define SC64_CFG_SCR_CPU_READY (1 << 31) #ifdef DEBUG
#define SC64_CFG_SCR_CPU_BUSY (1 << 30) #include <assert.h>
#define LOG_I(args...) {iprintf("\033[32m" args);}
#define LOG_E(args...) {iprintf("\033[31m" args);}
#else
#define LOG_I(args...)
#define LOG_E(args...)
#define assert(expr)
#endif
#define SC64_CMD_CONFIG 'C'
#define SC64_CMD_QUERY 'Q'
enum cfg_id { typedef struct {
io32_t SR_CMD;
io32_t DATA[2];
io32_t VERSION;
} sc64_regs_t;
#define SC64_BASE (0x1FFF0000)
#define SC64 ((sc64_regs_t *) SC64_BASE)
#define SC64_SR_CMD_ERROR (1 << 28)
#define SC64_SR_CPU_BUSY (1 << 30)
#define SC64_SR_CPU_READY (1 << 31)
#define SC64_CMD_CONFIG ('C')
#define SC64_CMD_QUERY ('Q')
#define SC64_CMD_DEBUG_RX_DATA ('E')
#define SC64_CMD_DEBUG_RX_READY ('A')
#define SC64_CMD_DEBUG_RX_BUSY ('F')
#define SC64_CMD_DEBUG_TX_DATA ('D')
#define SC64_CMD_DEBUG_TX_READY ('S')
#define SC64_VERSION_2 (0x53437632)
#define SC64_DEBUG_WRITE_ADDRESS (0x13BD8000UL)
#define SC64_DEBUG_READ_ADDRESS (0x13BD0000UL)
#define SC64_DEBUG_MAX_SIZE (32 * 1024)
#define SC64_DEBUG_ID_TEXT (0x01)
#define SC64_DEBUG_ID_FSD_READ (0xF1)
#define SC64_DEBUG_ID_FSD_WRITE (0xF2)
#define SC64_DEBUG_ID_FSD_SECTOR (0xF3)
typedef enum {
CFG_ID_SCR, CFG_ID_SCR,
CFG_ID_SDRAM_SWITCH, CFG_ID_SDRAM_SWITCH,
CFG_ID_SDRAM_WRITABLE, CFG_ID_SDRAM_WRITABLE,
@ -20,34 +61,66 @@ enum cfg_id {
CFG_ID_CIC_SEED, CFG_ID_CIC_SEED,
CFG_ID_TV_TYPE, CFG_ID_TV_TYPE,
CFG_ID_SAVE_OFFEST, CFG_ID_SAVE_OFFEST,
CFG_ID_DD_OFFEST, CFG_ID_DDIPL_OFFEST,
CFG_ID_SKIP_BOOTLOADER, CFG_ID_BOOT_MODE,
}; CFG_ID_FLASH_SIZE,
CFG_ID_FLASH_READ,
CFG_ID_FLASH_PROGRAM,
CFG_ID_RECONFIGURE,
} cfg_id_t;
typedef enum {
SAVE_TYPE_NONE = 0,
SAVE_TYPE_EEPROM_4K = 1,
SAVE_TYPE_EEPROM_16K = 2,
SAVE_TYPE_SRAM = 3,
SAVE_TYPE_FLASHRAM = 4,
SAVE_TYPE_SRAM_BANKED = 5,
SAVE_TYPE_FLASHRAM_PKST2 = 6,
} save_type_t;
typedef struct sc64_config { typedef enum {
union { TV_TYPE_PAL = 0,
uint32_t ___raw_data[2]; TV_TYPE_NTSC = 1,
struct { TV_TYPE_MPAL = 2,
uint8_t ___unused_1[2]; TV_TYPE_UNKNOWN = 3,
uint8_t save_type; } tv_type_t;
uint8_t ___unused_2: 1;
uint8_t dd_enable: 1; typedef enum {
uint8_t sdram_writable: 1; BOOT_MODE_MENU = 0,
uint8_t sdram_switch: 1; BOOT_MODE_ROM = 1,
uint8_t ___unused_3; BOOT_MODE_DDIPL = 2,
uint8_t tv_type; BOOT_MODE_DIRECT = 3,
uint16_t cic_type; } boot_mode_t;
};
}; typedef struct {
} sc64_config_t; bool dd_enabled;
save_type_t save_type;
uint16_t cic_seed;
tv_type_t tv_type;
io32_t *save_location;
io32_t *ddipl_location;
boot_mode_t boot_mode;
char bootloader_version[32];
} sc64_info_t;
bool sc64_check_presence (void); bool sc64_check_presence (void);
void sc64_wait_cpu_ready (void); void sc64_wait_cpu_ready (void);
void sc64_wait_cpu_busy(void); bool sc64_wait_cpu_busy (void);
void sc64_perform_cmd(uint8_t cmd, uint32_t *args); bool sc64_perform_cmd (uint8_t cmd, uint32_t *args, uint32_t *result);
void sc64_set_config(uint32_t type, uint32_t value); uint32_t sc64_get_config (cfg_id_t id);
void sc64_set_config (cfg_id_t id, uint32_t value);
void sc64_get_info (sc64_info_t *info);
void sc64_wait_usb_rx_ready (uint32_t *type, uint32_t *length);
void sc64_wait_usb_rx_busy (void);
void sc64_usb_rx_data (io32_t *address, uint32_t length);
void sc64_wait_usb_tx_ready (void);
void sc64_usb_tx_data (io32_t *address, uint32_t length);
void sc64_debug_write (uint8_t type, const void *data, uint32_t len);
void sc64_debug_fsd_read (const void *data, uint32_t sector, uint32_t count);
void sc64_debug_fsd_write (const void *data, uint32_t sector, uint32_t count);
void sc64_init (void);
#endif #endif

66
sw/n64/src/startup.S Normal file
View File

@ -0,0 +1,66 @@
#define STR(x) #x
#define XSTR(s) STR(s)
#define VERSION XSTR(__SC64_VERSION)
.section .text.rom_header
header_pi_config:
.word 0x80371240
header_clock_rate:
.word 0x0000000F
header_load_addr:
.word entry_handler
header_sdk_version:
.word 0x00000000
header_crc:
.fill 2, 4, 0
.org 0x20, 0x00
header_text_info:
.global header_text_info
.ascii "SummerLoader64 "
.ascii VERSION
.org 0x40, 0x00
.section .text.ipl3
ipl3:
.incbin "header", 0x40
.section .text.entry_handler
entry_handler:
.global entry_handler
la $gp, _gp
la $sp, _sp
run:
la $t0, init
jalr $t0
la $t0, main
jalr $t0
loop:
j loop
.section .text.ipl2
ipl2:
.global ipl2
.set noat
.set noreorder
lui $t5, 0xBFC0
1:
lw $t0, 0x7FC($t5)
addiu $t5, $t5, 0x7C0
andi $t0, $t0, 0x80
bnel $t0, $zero, 1b
lui $t5, 0xBFC0
lw $t0, 0x24($t5)
lui $t3, 0xB000

64
sw/n64/src/storage.c Normal file
View File

@ -0,0 +1,64 @@
#include "storage.h"
#include "sc64.h"
#include "fatfs/ff.h"
static const char *fatfs_error_codes[] = {
"(0) Succeeded",
"(1) A hard error occurred in the low level disk I/O layer",
"(2) Assertion failed",
"(3) The physical drive cannot work",
"(4) Could not find the file",
"(5) Could not find the path",
"(6) The path name format is invalid",
"(7) Access denied due to prohibited access or directory full",
"(8) Access denied due to prohibited access",
"(9) The file/directory object is invalid",
"(10) The physical drive is write protected",
"(11) The logical drive number is invalid",
"(12) The volume has no work area",
"(13) There is no valid FAT volume",
"(14) The f_mkfs() aborted due to any problem",
"(15) Could not get a grant to access the volume within defined period",
"(16) The operation is rejected according to the file sharing policy",
"(17) LFN working buffer could not be allocated",
"(18) Number of open files > FF_FS_LOCK",
"(19) Given parameter is invalid",
};
#define FF_CHECK(x) { \
FRESULT fatfs_result = x; \
if (fatfs_result) { \
LOG_E("fatfs error \"%s\" at [%s:%d] in expr: %s\r\n", fatfs_error_codes[fatfs_result], __FILE__, __LINE__, #x); \
while (1); \
} \
}
void storage_run_menu (storage_backend_t storage_backend, boot_info_t *boot_info, sc64_info_t *sc64_info) {
FATFS fs;
FIL fil;
if (storage_backend == STORAGE_BACKEND_SD) {
FF_CHECK(f_mount(&fs, "0:", 1));
FF_CHECK(f_chdrive("0:"));
} else if (storage_backend == STORAGE_BACKEND_USB) {
FF_CHECK(f_mount(&fs, "1:", 1));
FF_CHECK(f_chdrive("1:"));
} else {
LOG_E("Unknown storage backend %d\r\n", storage_backend);
while (1);
}
FF_CHECK(f_open(&fil, "sc64menu.elf", FA_READ));
// TODO: Implement ELF loader here
FF_CHECK(f_close(&fil));
// TODO: Execute ELF here
// menu(&boot_info, &sc64_info);
boot_info->device_type = BOOT_DEVICE_TYPE_ROM;
}

18
sw/n64/src/storage.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef STORAGE_H__
#define STORAGE_H__
#include "boot.h"
#include "sc64.h"
typedef enum {
STORAGE_BACKEND_SD = 0,
STORAGE_BACKEND_USB = 1,
} storage_backend_t;
void storage_run_menu (storage_backend_t storage_backend, boot_info_t *boot_info, sc64_info_t *sc64_info);
#endif

71
sw/n64/src/sys.c Normal file
View File

@ -0,0 +1,71 @@
#include "sys.h"
void c0_set_status (uint32_t status) {
asm volatile (
".set noat \n"
".set noreorder \n"
"mtc0 %[status], $12 \n"
"nop \n"
:
: [status] "r" (status)
);
}
uint32_t c0_get_count (void) {
uint32_t count;
asm volatile (
".set noat \n"
".set noreorder \n"
"mfc0 %[count], $9 \n"
"nop \n"
: [count] "=r" (count)
);
return count;
}
void wait_ms (uint32_t ms) {
uint32_t start = c0_get_count();
while (c0_get_count() - start < (ms * ((CPU_FREQUENCY / 2) / 1000)));
}
uint32_t io_read (io32_t *address) {
io32_t *uncached = UNCACHED(address);
asm volatile ("" : : : "memory");
uint32_t value = *uncached;
asm volatile ("" : : : "memory");
return value;
}
void io_write (io32_t *address, uint32_t value) {
io32_t *uncached = UNCACHED(address);
asm volatile ("" : : : "memory");
*uncached = value;
asm volatile ("" : : : "memory");
}
uint32_t pi_busy (void) {
return (io_read(&PI->SR) & (PI_SR_IO_BUSY | PI_SR_DMA_BUSY));
}
uint32_t pi_io_read (io32_t *address) {
return io_read(address);
}
void pi_io_write (io32_t *address, uint32_t value) {
io_write(address, value);
while (pi_busy());
}
uint32_t si_busy (void) {
return (io_read(&SI->SR) & (SI_SR_IO_BUSY | SI_SR_DMA_BUSY));
}
uint32_t si_io_read (io32_t *address) {
return io_read(address);
}
void si_io_write (io32_t *address, uint32_t value) {
io_write(address, value);
while (si_busy());
}

269
sw/n64/src/sys.h Normal file
View File

@ -0,0 +1,269 @@
#ifndef SYS_H__
#define SYS_H__
#include <stddef.h>
#include <stdint.h>
typedef volatile uint8_t io8_t;
typedef volatile uint32_t io32_t;
#define ALIGN(value, align) (((value) + ((typeof(value))(align) - 1)) & ~((typeof(value))(align) - 1))
#define UNCACHED(address) ((typeof(address)) (((io32_t) (address)) | (0xA0000000UL)))
#define CPU_FREQUENCY (93750000UL)
#define C0_SR_CU3 (1 << 31)
#define C0_SR_CU2 (1 << 30)
#define C0_SR_CU1 (1 << 29)
#define C0_SR_CU0 (1 << 28)
#define C0_SR_RP (1 << 27)
#define C0_SR_FR (1 << 26)
#define C0_SR_RE (1 << 25)
#define C0_SR_DS_ITS (1 << 24)
#define C0_SR_DS_BEV (1 << 22)
#define C0_SR_DS_TS (1 << 21)
#define C0_SR_DS_SR (1 << 20)
#define C0_SR_DS_CH (1 << 18)
#define C0_SR_DS_CE (1 << 17)
#define C0_SR_DS_DE (1 << 16)
#define C0_SR_IM7 (1 << 15)
#define C0_SR_IM6 (1 << 14)
#define C0_SR_IM5 (1 << 13)
#define C0_SR_IM4 (1 << 12)
#define C0_SR_IM3 (1 << 11)
#define C0_SR_IM2 (1 << 10)
#define C0_SR_IM1 (1 << 9)
#define C0_SR_IM0 (1 << 8)
#define C0_SR_KX (1 << 7)
#define C0_SR_SX (1 << 6)
#define C0_SR_UX (1 << 5)
#define C0_SR_KSU1 (1 << 4)
#define C0_SR_KSU0 (1 << 3)
#define C0_SR_EXR (1 << 2)
#define C0_SR_EXL (1 << 1)
#define C0_SR_IE (1 << 0)
typedef struct {
io32_t DMEM[1024];
io32_t IMEM[1024];
} sp_mem_t;
#define SP_MEM_BASE (0x04000000UL)
#define SP_MEM ((sp_mem_t *) SP_MEM_BASE)
typedef struct {
io32_t PADDR;
io32_t MADDR;
io32_t RD_LEN;
io32_t WR_LEN;
io32_t SR;
io32_t DMA_FULL;
io32_t DMA_BUSY;
io32_t SEMAPHORE;
} sp_regs_t;
#define SP_BASE (0x04040000UL)
#define SP ((sp_regs_t *) SP_BASE)
#define SP_SR_HALT (1 << 0)
#define SP_SR_BROKE (1 << 1)
#define SP_SR_DMA_BUSY (1 << 2)
#define SP_SR_DMA_FULL (1 << 3)
#define SP_SR_IO_FULL (1 << 4)
#define SP_SR_SSTEP (1 << 5)
#define SP_SR_INTR_BREAK (1 << 6)
#define SP_SR_SIG0 (1 << 7)
#define SP_SR_SIG1 (1 << 8)
#define SP_SR_SIG2 (1 << 9)
#define SP_SR_SIG3 (1 << 10)
#define SP_SR_SIG4 (1 << 11)
#define SP_SR_SIG5 (1 << 12)
#define SP_SR_SIG6 (1 << 13)
#define SP_SR_SIG7 (1 << 14)
#define SP_SR_CLR_HALT (1 << 0)
#define SP_SR_SET_HALT (1 << 1)
#define SP_SR_CLR_BROKE (1 << 2)
#define SP_SR_CLR_INTR (1 << 3)
#define SP_SR_SET_INTR (1 << 4)
#define SP_SR_CLR_SSTEP (1 << 5)
#define SP_SR_SET_SSTEP (1 << 6)
#define SP_SR_CLR_INTR_BREAK (1 << 7)
#define SP_SR_SET_INTR_BREAK (1 << 8)
#define SP_SR_CLR_SIG0 (1 << 9)
#define SP_SR_SET_SIG0 (1 << 10)
#define SP_SR_CLR_SIG1 (1 << 11)
#define SP_SR_SET_SIG1 (1 << 12)
#define SP_SR_CLR_SIG2 (1 << 13)
#define SP_SR_SET_SIG2 (1 << 14)
#define SP_SR_CLR_SIG3 (1 << 15)
#define SP_SR_SET_SIG3 (1 << 16)
#define SP_SR_CLR_SIG4 (1 << 17)
#define SP_SR_SET_SIG4 (1 << 18)
#define SP_SR_CLR_SIG5 (1 << 19)
#define SP_SR_SET_SIG5 (1 << 20)
#define SP_SR_CLR_SIG6 (1 << 21)
#define SP_SR_SET_SIG6 (1 << 22)
#define SP_SR_CLR_SIG7 (1 << 23)
#define SP_SR_SET_SIG7 (1 << 24)
typedef struct {
io32_t START;
io32_t END;
io32_t CURRENT;
io32_t SR;
io32_t CLOCK;
io32_t BUF_BUSY;
io32_t PIPE_BUSY;
io32_t TMEM;
} dpc_regs_t;
#define DPC_BASE (0x04100000UL)
#define DPC ((dpc_regs_t *) DPC_BASE)
#define DPC_SR_XBUS_DMEM_DMA (1 << 0)
#define DPC_SR_FREEZE (1 << 1)
#define DPC_SR_FLUSH (1 << 2)
#define DPC_SR_START_GCLK (1 << 3)
#define DPC_SR_TMEM_BUSY (1 << 4)
#define DPC_SR_PIPE_BUSY (1 << 5)
#define DPC_SR_CMD_BUSY (1 << 6)
#define DPC_SR_CBUF_READY (1 << 7)
#define DPC_SR_DMA_BUSY (1 << 8)
#define DPC_SR_END_VALID (1 << 9)
#define DPC_SR_START_VALID (1 << 10)
#define DPC_SR_CLR_XBUS_DMEM_DMA (1 << 0)
#define DPC_SR_SET_XBUS_DMEM_DMA (1 << 1)
#define DPC_SR_CLR_FREEZE (1 << 2)
#define DPC_SR_SET_FREEZE (1 << 3)
#define DPC_SR_CLR_FLUSH (1 << 4)
#define DPC_SR_SET_FLUSH (1 << 5)
#define DPC_SR_CLR_TMEM_CTR (1 << 6)
#define DPC_SR_CLR_PIPE_CTR (1 << 7)
#define DPC_SR_CLR_CMD_CTR (1 << 8)
#define DPC_SR_CLR_CLOCK_CTR (1 << 9)
typedef struct {
io32_t CR;
io32_t MADDR;
io32_t H_WIDTH;
io32_t V_INTR;
io32_t CURR_LINE;
io32_t TIMING;
io32_t V_SYNC;
io32_t H_SYNC;
io32_t H_SYNC_LEAP;
io32_t H_LIMITS;
io32_t V_LIMITS;
io32_t COLOR_BURST;
io32_t H_SCALE;
io32_t V_SCALE;
} vi_regs_t;
#define VI_BASE (0x04400000UL)
#define VI ((vi_regs_t *) VI_BASE)
#define VI_CR_TYPE_16 (2 << 0)
#define VI_CR_TYPE_32 (3 << 0)
#define VI_CR_GAMMA_DITHER_ON (1 << 2)
#define VI_CR_GAMMA_ON (1 << 3)
#define VI_CR_DIVOT_ON (1 << 4)
#define VI_CR_SERRATE_ON (1 << 6)
#define VI_CR_ANTIALIAS_0 (1 << 8)
#define VI_CR_ANTIALIAS_1 (1 << 9)
#define VI_CR_DITHER_FILTER_ON (1 << 16)
typedef struct {
io32_t MADDR;
io32_t LEN;
io32_t CR;
io32_t SR;
io32_t DACRATE;
io32_t BITRATE;
} ai_regs_t;
#define AI_BASE (0x04500000UL)
#define AI ((ai_regs_t *) AI_BASE)
#define AI_SR_DMA_BUSY (1 << 30)
#define AI_SR_FIFO_FULL (1 << 31)
#define AI_CR_DMA_ON (1 << 0)
typedef struct {
io32_t MADDR;
io32_t PADDR;
io32_t RDMA;
io32_t WDMA;
io32_t SR;
struct {
io32_t LAT;
io32_t PWD;
io32_t PGS;
io32_t RLS;
} DOM[2];
} pi_regs_t;
#define PI_BASE (0x04600000UL)
#define PI ((pi_regs_t *) PI_BASE)
#define PI_SR_DMA_BUSY (1 << 0)
#define PI_SR_IO_BUSY (1 << 1)
#define PI_SR_DMA_ERROR (1 << 2)
#define PI_SR_RESET (1 << 0)
#define PI_SR_CLR_INTR (1 << 1)
typedef struct {
io32_t MADDR;
io32_t RDMA;
io32_t __reserved_1;
io32_t __reserved_2;
io32_t WDMA;
io32_t __reserved_3;
io32_t SR;
} si_regs_t;
#define SI_BASE (0x04800000UL)
#define SI ((si_regs_t *) SI_BASE)
#define SI_SR_DMA_BUSY (1 << 0)
#define SI_SR_IO_BUSY (1 << 1)
#define SI_SR_DMA_ERROR (1 << 3)
#define SI_SR_INTERRUPT (1 << 12)
#define SI_SR_CLEAR_INTERRUPT (0)
#define ROM_DDIPL_BASE (0x06000000UL)
#define ROM_DDIPL ((io32_t *) ROM_DDIPL_BASE)
#define ROM_CART_BASE (0x10000000UL)
#define ROM_CART ((io32_t *) ROM_CART_BASE)
#define PIFRAM_BASE (0x1FC007C0UL)
#define PIFRAM ((io8_t *) PIFRAM_BASE)
void c0_set_status (uint32_t status);
uint32_t c0_get_count (void);
void wait_ms (uint32_t ms);
uint32_t io_read (io32_t *address);
void io_write (io32_t *address, uint32_t value);
uint32_t pi_busy (void);
uint32_t pi_io_read (io32_t *address);
void pi_io_write (io32_t *address, uint32_t value);
uint32_t si_busy (void);
uint32_t si_io_read (io32_t *address);
void si_io_write (io32_t *address, uint32_t value);
#endif

71
sw/n64/src/syscalls.c Normal file
View File

@ -0,0 +1,71 @@
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "sc64.h"
extern char _sheap __attribute__((section(".data")));
extern char _eheap __attribute__((section(".data")));
int _close_r (struct _reent *prt, int fd) {
errno = ENOSYS;
return -1;
}
int _fstat_r (struct _reent *prt, int fd, struct stat *pstat) {
errno = ENOSYS;
return -1;
}
int _isatty_r (struct _reent *prt, int fd) {
if (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO){
return 1;
}
errno = EBADF;
return 0;
}
off_t _lseek_r (struct _reent *prt, int fd, off_t pos, int whence) {
errno = ENOSYS;
return -1;
}
ssize_t _read_r (struct _reent *prt, int fd, void *buf, size_t cnt) {
errno = ENOSYS;
return -1;
}
caddr_t _sbrk_r (struct _reent *prt, ptrdiff_t incr) {
static char *curr_heap_end = &_sheap;
char *prev_heap_end;
prev_heap_end = curr_heap_end;
curr_heap_end += incr;
if (curr_heap_end > &_eheap) {
errno = ENOMEM;
return (caddr_t) -1;
}
return (caddr_t) prev_heap_end;
}
ssize_t _write_r (struct _reent *prt, int fd, const void *buf, size_t cnt) {
if (fd == STDIN_FILENO) {
errno = EBADF;
return -1;
} else if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
sc64_debug_write(SC64_DEBUG_ID_TEXT, buf, cnt);
return cnt;
}
errno = ENOSYS;
return -1;
}
void __assert_func (const char *file, int line, const char *func, const char *failedexpr) {
LOG_E("\r\nassertion \"%s\" failed: file \"%s\", line %d%s%s\r\n", failedexpr, file, line, func ? ", function: " : "", func ? func : "");
while (1);
}

3
sw/pc/.gitignore vendored
View File

@ -1,4 +1,6 @@
/__pycache__
/backup /backup
/roms
/saves /saves
*.bak *.bak
*.bin *.bin
@ -6,6 +8,7 @@
*.data *.data
*.eep *.eep
*.fla *.fla
*.img
*.n64 *.n64
*.srm *.srm
*.v64 *.v64

View File

@ -1,108 +0,0 @@
import os
import serial
import sys
class SC64:
__SDRAM_SIZE = 64 * 1024 * 1024
__CONFIG_QUERY_SAVE_TYPE = 4
__CONFIG_QUERY_SAVE_OFFSET = 7
def __init__(self, port):
self.__serial = serial.Serial(port)
self.__save_type = self.__query_config(self.__CONFIG_QUERY_SAVE_TYPE)
self.__save_offset = self.__query_config(self.__CONFIG_QUERY_SAVE_OFFSET)
print('{:08X}'.format(self.__save_type))
print('{:08X}'.format(self.__save_offset))
def __query_config(self, query):
self.__serial.write(b'CMDQ')
self.__serial.write(query.to_bytes(4, byteorder='big'))
self.__serial.write(bytes(4))
value = self.__serial.read(4)
if (self.__serial.read(4).decode() != 'CMPQ'):
raise Exception('Bad query response')
return int.from_bytes(value, byteorder='big')
def __save_length(self):
return {
0: 0,
1: 512,
2: 2048,
3: (32 * 1024),
4: (128 * 1024),
5: (3 * 32 * 1024),
6: (128 * 1024),
7: 0
}[self.__save_type]
def dump_save(self, file):
length = self.__save_length()
if (length == 0):
raise Exception('No save type is selected')
self.__serial.write(b'CMDR')
self.__serial.write(self.__save_offset.to_bytes(4, byteorder='big'))
self.__serial.write(length.to_bytes(4, byteorder='big'))
save = self.__serial.read(length)
response = self.__serial.read(4)
if (response.decode() == 'CMPR'):
with open(file, 'wb') as f:
f.write(save)
else:
raise Exception('There was a problem while dumping save data')
def redump_save(self, file):
length = self.__save_length()
if (not length):
raise Exception('No save type is selected')
if (os.path.getsize(file) != length):
raise Exception('Wrong save file size')
with open(file, 'rb') as f:
self.__serial.write(b'CMDW')
self.__serial.write(self.__save_offset.to_bytes(4, byteorder='big'))
self.__serial.write(length.to_bytes(4, byteorder='big'))
self.__serial.write(f.read())
response = self.__serial.read(4)
if (response.decode() != 'CMPW'):
raise Exception('There was a problem while redumping save data')
def dump_all(self, file):
self.__serial.write(b'CMDR')
self.__serial.write((0).to_bytes(4, byteorder='big'))
self.__serial.write((self.__SDRAM_SIZE).to_bytes(4, byteorder='big'))
save = self.__serial.read(self.__SDRAM_SIZE)
response = self.__serial.read(4)
if (response.decode() == 'CMPR'):
with open(file, 'wb') as f:
f.write(save)
else:
raise Exception('There was a problem while dumping all data')
mode = 'r'
file = 'save.dat'
port = 'COM7'
if (len(sys.argv) >= 2):
mode = sys.argv[1]
if (len(sys.argv) >= 3):
file = sys.argv[2]
if (len(sys.argv) >= 4):
port = sys.argv[3]
sc64 = SC64(port)
if (mode == 'r'):
sc64.dump_save(file)
elif (mode == 'w'):
sc64.redump_save(file)
elif (mode == 'a'):
sc64.dump_all(file)

2
sw/pc/requirements.txt Normal file
View File

@ -0,0 +1,2 @@
progressbar2==3.55.0
pyserial==3.5

593
sw/pc/sc64.py Normal file
View File

@ -0,0 +1,593 @@
#!/usr/bin/env python3
from serial import Serial, SerialException
from serial.tools import list_ports
import argparse
import filecmp
import os
import progressbar
import re
import sys
import time
class SC64Exception(Exception):
pass
class SC64:
__CFG_ID_SCR = 0
__CFG_ID_SDRAM_SWITCH = 1
__CFG_ID_SDRAM_WRITABLE = 2
__CFG_ID_DD_ENABLE = 3
__CFG_ID_SAVE_TYPE = 4
__CFG_ID_CIC_SEED = 5
__CFG_ID_TV_TYPE = 6
__CFG_ID_SAVE_OFFEST = 7
__CFG_ID_DDIPL_OFFEST = 8
__CFG_ID_BOOT_MODE = 9
__CFG_ID_FLASH_SIZE = 10
__CFG_ID_FLASH_READ = 11
__CFG_ID_FLASH_PROGRAM = 12
__CFG_ID_RECONFIGURE = 13
__SC64_VERSION_V2 = 0x53437632
__UPDATE_OFFSET = 0x03B60000
__CHUNK_SIZE = 256 * 1024
__MIN_ROM_LENGTH = 0x101000
__DDIPL_ROM_LENGTH = 0x400000
__DEBUG_ID_TEXT = 0x01
__DEBUG_ID_FSD_READ = 0xF1
__DEBUG_ID_FSD_WRITE = 0xF2
__DEBUG_ID_FSD_SECTOR = 0xF3
def __init__(self) -> None:
self.__serial = None
self.__progress_init = None
self.__progress_value = None
self.__progress_finish = None
self.__fsd_file = None
self.__find_sc64()
def __del__(self) -> None:
if (self.__serial):
self.__serial.close()
if (self.__fsd_file):
self.__fsd_file.close()
def __set_progress_init(self, value: int, label: str) -> None:
if (self.__progress_init != None):
self.__progress_init(value, label)
def __set_progress_value(self, value: int) -> None:
if (self.__progress_value != None):
self.__progress_value(value)
def __set_progress_finish(self) -> None:
if (self.__progress_finish != None):
self.__progress_finish()
def __align(self, value: int, align: int) -> int:
return (value + (align - 1)) & ~(align - 1)
def __escape(self, data: bytes) -> bytes:
return re.sub(b"\x1B", b"\x1B\x1B", data)
def __reset_link(self) -> None:
self.__serial.write(b"\x1BR")
def __read(self, bytes: int) -> bytes:
return self.__serial.read(bytes)
def __write(self, data: bytes) -> None:
self.__serial.write(self.__escape(data))
def __read_long(self, length: int) -> bytes:
data = bytearray()
while (len(data) < length):
data += self.__read(length - len(data))
return bytes(data)
def __write_dummy(self, length: int) -> None:
self.__write(bytes(length))
def __read_int(self) -> int:
return int.from_bytes(self.__read(4), byteorder="big")
def __write_int(self, value: int) -> None:
self.__write(value.to_bytes(4, byteorder="big"))
def __read_cmd_status(self, cmd: str) -> None:
if (self.__read(4) != f"CMP{cmd[0]}".encode(encoding="ascii")):
raise SC64Exception("Wrong command response")
def __write_cmd(self, cmd: str, arg1: int, arg2: int) -> None:
self.__write(f"CMD{cmd[0]}".encode())
self.__write_int(arg1)
self.__write_int(arg2)
def __find_sc64(self) -> None:
ports = list_ports.comports()
device_found = False
if (self.__serial != None and self.__serial.isOpen()):
self.__serial.close()
for p in ports:
if (p.vid == 0x0403 and p.pid == 0x6014 and p.serial_number.startswith("SC64")):
try:
self.__serial = Serial(p.device, timeout=1.0, write_timeout=1.0)
self.__serial.flushOutput()
self.__reset_link()
while (self.__serial.in_waiting):
self.__serial.read_all()
time.sleep(0.1)
self.__probe_device()
except (SerialException, SC64Exception):
if (self.__serial):
self.__serial.close()
continue
device_found = True
break
if (not device_found):
raise SC64Exception("No SummerCart64 device was found")
def __probe_device(self) -> None:
self.__write_cmd("V", 0, 0)
version = self.__read_int()
self.__read_cmd_status("V")
if (version != self.__SC64_VERSION_V2):
raise SC64Exception(f"Unknown hardware version: {hex(version)}")
def __query_config(self, id: int) -> int:
self.__write_cmd("Q", id, 0)
value = self.__read_int()
self.__read_cmd_status("Q")
return value
def __change_config(self, id: int, arg: int = 0, ignore_response: bool = False) -> None:
self.__write_cmd("C", id, arg)
if (not ignore_response):
self.__read_cmd_status("C")
def __debug_write(self, type: int, data: bytes) -> None:
self.__write_cmd("D", type, len(data))
self.__write(data)
def __read_file_from_sdram(self, file: str, offset: int, length: int, align: int = 2) -> None:
with open(file, "wb") as f:
transfer_size = self.__align(length, align)
self.__set_progress_init(transfer_size, os.path.basename(f.name))
self.__write_cmd("R", offset, transfer_size)
while (f.tell() < length):
chunk_size = min(self.__CHUNK_SIZE, length - f.tell())
f.write(self.__read(chunk_size))
self.__set_progress_value(f.tell())
if (transfer_size != length):
self.__read(transfer_size - length)
self.__read_cmd_status("R")
self.__set_progress_finish()
def __write_file_to_sdram(self, file: str, offset: int, align: int = 2, min_length: int = 0) -> None:
with open(file, "rb") as f:
length = os.fstat(f.fileno()).st_size
transfer_size = self.__align(max(length, min_length), align)
self.__set_progress_init(transfer_size, os.path.basename(f.name))
self.__write_cmd("W", offset, transfer_size)
while (f.tell() < length):
self.__write(f.read(min(self.__CHUNK_SIZE, length - f.tell())))
self.__set_progress_value(f.tell())
if (transfer_size != length):
self.__write_dummy(transfer_size - length)
self.__read_cmd_status("W")
self.__set_progress_finish()
def __get_save_length(self) -> None:
save_type = self.__query_config(self.__CFG_ID_SAVE_TYPE)
return {
0: 0,
1: 512,
2: 2048,
3: (32 * 1024),
4: (128 * 1024),
5: (3 * 32 * 1024),
6: (128 * 1024)
}[save_type]
def __reconfigure(self) -> None:
magic = self.__query_config(self.__CFG_ID_RECONFIGURE)
self.__change_config(self.__CFG_ID_RECONFIGURE, magic, ignore_response=True)
time.sleep(0.2)
def backup_firmware(self, file: str) -> None:
length = self.__query_config(self.__CFG_ID_FLASH_SIZE)
self.__change_config(self.__CFG_ID_FLASH_READ, self.__UPDATE_OFFSET)
self.__read_file_from_sdram(file, self.__UPDATE_OFFSET, length)
def update_firmware(self, file: str) -> None:
self.__write_file_to_sdram(file, self.__UPDATE_OFFSET)
saved_timeout = self.__serial.timeout
self.__serial.timeout = 20.0
self.__change_config(self.__CFG_ID_FLASH_PROGRAM, self.__UPDATE_OFFSET)
self.__serial.timeout = saved_timeout
self.__reconfigure()
self.__find_sc64()
def set_boot_mode(self, mode: int) -> None:
if (mode >= 0 and mode <= 3):
self.__change_config(self.__CFG_ID_BOOT_MODE, mode)
else:
raise SC64Exception("Boot mode outside of supported values")
def get_boot_mode_label(self, mode: int) -> None:
if (mode < 0 or mode > 3):
return "Unknown"
return {
0: "Load menu from SD card",
1: "Load ROM from SDRAM through bootloader",
2: "Load DDIPL from SDRAM",
3: "Load ROM from SDRAM directly without bootloader"
}[mode]
def set_tv_type(self, type: int = None) -> None:
if (type == None or type == 3):
self.__change_config(self.__CFG_ID_TV_TYPE, 3)
elif (type >= 0 and type <= 2):
self.__change_config(self.__CFG_ID_TV_TYPE, type)
else:
raise SC64Exception("TV type outside of supported values")
def get_tv_type_label(self, type: int) -> None:
if (type < 0 or type > 2):
return "Unknown"
return {
0: "PAL",
1: "NTSC",
2: "MPAL"
}[type]
def set_cic_seed(self, seed: int = None) -> None:
if (seed == None or seed == 0xFFFF):
self.__change_config(self.__CFG_ID_CIC_SEED, 0xFFFF)
elif (seed >= 0 and seed <= 0x1FF):
self.__change_config(self.__CFG_ID_CIC_SEED, seed)
else:
raise SC64Exception("CIC seed outside of supported values")
def download_rom(self, file: str, length: int, offset: int = 0) -> None:
self.__read_file_from_sdram(file, offset, length)
def upload_rom(self, file: str, offset: int = 0) -> None:
self.__write_file_to_sdram(file, offset, min_length=self.__MIN_ROM_LENGTH)
def set_save_type(self, type: int) -> None:
if (type >= 0 and type <= 6):
self.__change_config(self.__CFG_ID_SAVE_TYPE, type)
else:
raise SC64Exception("Save type outside of supported values")
def get_save_type_label(self, type: int) -> None:
if (type < 0 or type > 6):
return "Unknown"
return {
0: "No save",
1: "EEPROM 4Kb",
2: "EEPROM 16Kb",
3: "SRAM 256Kb",
4: "FlashRAM 1Mb",
5: "SRAM 768Kb",
6: "FlashRAM 1Mb (Pokemon Stadium 2 special case)"
}[type]
def download_save(self, file: str) -> None:
length = self.__get_save_length()
if (length > 0):
offset = self.__query_config(self.__CFG_ID_SAVE_OFFEST)
self.__read_file_from_sdram(file, offset, length)
else:
raise SC64Exception("Can't read save data - no save type is set")
def upload_save(self, file: str) -> None:
length = self.__get_save_length()
save_length = os.path.getsize(file)
if (length <= 0):
raise SC64Exception("Can't write save data - no save type is set")
elif (length != save_length):
raise SC64Exception("Can't write save data - save file size is different than expected")
else:
offset = self.__query_config(self.__CFG_ID_SAVE_OFFEST)
self.__write_file_to_sdram(file, offset)
def set_dd_enable(self, enable: bool) -> None:
self.__change_config(self.__CFG_ID_DD_ENABLE, 1 if enable else 0)
def download_dd_ipl(self, file: str) -> None:
dd_ipl_offset = self.__query_config(self.__CFG_ID_DDIPL_OFFEST)
self.__read_file_from_sdram(file, dd_ipl_offset, length=self.__DDIPL_ROM_LENGTH)
def upload_dd_ipl(self, file: str, offset: int = None) -> None:
if (offset != None):
self.__change_config(self.__CFG_ID_DDIPL_OFFEST, offset)
dd_ipl_offset = self.__query_config(self.__CFG_ID_DDIPL_OFFEST)
self.__write_file_to_sdram(file, dd_ipl_offset, min_length=self.__DDIPL_ROM_LENGTH)
def __debug_process_fsd_set_sector(self, data: bytes) -> None:
sector = int.from_bytes(data[0:4], byteorder='big')
if (self.__fsd_file):
self.__fsd_file.seek(sector * 512)
def __debug_process_fsd_read(self, data: bytes) -> None:
length = int.from_bytes(data[0:4], byteorder='big')
if (self.__fsd_file):
self.__debug_write(self.__DEBUG_ID_FSD_READ, self.__fsd_file.read(length))
else:
self.__debug_write(self.__DEBUG_ID_FSD_READ, bytes(length))
def __debug_process_fsd_write(self, data: bytes) -> None:
if (self.__fsd_file):
self.__fsd_file.write(data)
def debug_loop(self, file: str = None) -> None:
print("\r\n\033[34m --- Debug server started --- \033[0m\r\n")
self.__serial.timeout = 0.01
self.__serial.write_timeout = 1
if (file):
self.__fsd_file = open(file, "rb+")
start_indicator = bytearray()
dropped_bytes = 0
while (True):
while (start_indicator != b"DMA@"):
start_indicator.append(self.__read_long(1)[0])
if (len(start_indicator) > 4):
dropped_bytes += 1
start_indicator.pop(0)
start_indicator.clear()
if (dropped_bytes):
print(f"\033[35mWarning - dropped {dropped_bytes} bytes from stream\033[0m", file=sys.stderr)
dropped_bytes = 0
header = self.__read_long(4)
id = int(header[0])
length = int.from_bytes(header[1:4], byteorder="big")
if (length > 0):
data = self.__read_long(length)
if (id == self.__DEBUG_ID_TEXT):
print(data.decode(encoding="ascii", errors="backslashreplace"), end="")
elif (id == self.__DEBUG_ID_FSD_READ):
self.__debug_process_fsd_read(data)
elif (id == self.__DEBUG_ID_FSD_WRITE):
self.__debug_process_fsd_write(data)
elif (id == self.__DEBUG_ID_FSD_SECTOR):
self.__debug_process_fsd_set_sector(data)
else:
print(f"\033[35mGot unknown id: {id}, length: {length}\033[0m", file=sys.stderr)
self.__read_long(self.__align(length, 4) - length)
end_indicator = self.__read_long(4)
if (end_indicator != b"CMPH"):
print(f"\033[35mGot unknown end indicator: {end_indicator.decode(encoding='ascii', errors='backslashreplace')}\033[0m", file=sys.stderr)
class SC64ProgressBar:
__LABEL_LENGTH = 30
def __init__(self, sc64: SC64) -> None:
self.__sc64 = sc64
self.__widgets = [
"[ ",
progressbar.FormatLabel("{variables.label}", new_style=True),
" | ",
progressbar.Bar(left="", right=""),
" ",
progressbar.Percentage(),
" | ",
progressbar.DataSize(prefixes=(" ", "Ki", "Mi")),
" | ",
progressbar.AdaptiveTransferSpeed(),
" ]"
]
self.__variables = {"label": ""}
self.__bar = None
def __enter__(self) -> None:
if (self.__sc64):
self.__sc64._SC64__progress_init = self.__progress_init
self.__sc64._SC64__progress_value = self.__progress_value
self.__sc64._SC64__progress_finish = self.__progress_finish
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
if (self.__sc64):
self.__sc64._SC64__progress_init = None
self.__sc64._SC64__progress_value = None
self.__sc64._SC64__progress_finish = None
def __progress_init(self, value: int, label: str) -> None:
if (self.__bar == None):
self.__bar = progressbar.ProgressBar(widgets=self.__widgets, variables=self.__variables)
justified_label = label.ljust(self.__LABEL_LENGTH)
if (len(justified_label) > self.__LABEL_LENGTH):
justified_label = f"{justified_label[:self.__LABEL_LENGTH - 3]}..."
self.__bar.variables.label = justified_label
self.__bar.start(max_value=value)
def __progress_value(self, value: int) -> None:
self.__bar.update(value)
def __progress_finish(self) -> None:
self.__bar.finish(end="")
print()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="SummerCart64 one stop control center")
parser.add_argument("-b", metavar="boot_mode", default="1", required=False, help="set boot mode (0 - 3)")
parser.add_argument("-t", metavar="tv_type", default="3", required=False, help="set TV type (0 - 2)")
parser.add_argument("-c", metavar="cic_seed", default="0xFFFF", required=False, help="set CIC seed")
parser.add_argument("-s", metavar="save_type", default="0", required=False, help="set save type (0 - 6)")
parser.add_argument("-d", default=False, action="store_true", required=False, help="enable 64DD emulation")
parser.add_argument("-r", default=False, action="store_true", required=False, help="perform reading operation instead of writing")
parser.add_argument("-l", metavar="length", default="0x101000", required=False, help="specify ROM length to read")
parser.add_argument("-u", metavar="update_path", default=None, required=False, help="path to update file")
parser.add_argument("-e", metavar="save_path", default=None, required=False, help="path to save file")
parser.add_argument("-i", metavar="ddipl_path", default=None, required=False, help="path to DDIPL file")
parser.add_argument("-q", default=None, action="store_true", required=False, help="start debug server")
parser.add_argument("-f", metavar="sd_path", default=None, required=False, help="path to disk or file for fake SD card emulation")
parser.add_argument("rom", metavar="rom_path", default=None, help="path to ROM file", nargs="?")
if (len(sys.argv) <= 1):
parser.print_help()
parser.exit()
args = parser.parse_args()
try:
sc64 = SC64()
boot_mode = int(args.b)
save_type = int(args.s)
tv_type = int(args.t)
cic_seed = int(args.c, 0)
dd_enable = args.d
is_read = args.r
rom_length = int(args.l, 0)
update_file = args.u
save_file = args.e
dd_ipl_file = args.i
rom_file = args.rom
debug_server = args.q
sd_file = args.f
firmware_backup_file = "sc64firmware.bin.bak"
with SC64ProgressBar(sc64):
if (update_file):
if (is_read):
sc64.backup_firmware(update_file)
else:
sc64.backup_firmware(firmware_backup_file)
if (not filecmp.cmp(update_file, firmware_backup_file)):
print("Update file different than contents of flash - updating SummerCart64")
sc64.update_firmware(update_file)
else:
print("SummerCart64 is already updated to latest version")
os.remove(firmware_backup_file)
if (not is_read):
if (boot_mode != 1):
print(f"Setting boot mode to [{sc64.get_boot_mode_label(boot_mode)}]")
sc64.set_boot_mode(boot_mode)
if (save_type):
print(f"Setting save type to [{sc64.get_save_type_label(save_type)}]")
sc64.set_save_type(save_type)
if (tv_type != 3):
print(f"Setting TV type to [{sc64.get_tv_type_label(tv_type)}]")
sc64.set_tv_type(tv_type)
if (cic_seed != 0xFFFF):
print(f"Setting CIC seed to [{hex(cic_seed) if cic_seed != 0xFFFF else 'Unknown'}]")
sc64.set_cic_seed(cic_seed)
if (dd_enable):
print(f"Setting 64DD emulation to [{'Enabled' if dd_enable else 'Disabled'}]")
sc64.set_dd_enable(dd_enable)
if (rom_file):
if (is_read):
if (rom_length > 0):
sc64.download_rom(rom_file, rom_length)
else:
raise SC64Exception("ROM length should be larger than 0")
else:
sc64.upload_rom(rom_file)
if (dd_ipl_file):
if (is_read):
sc64.download_dd_ipl(dd_ipl_file)
else:
sc64.upload_dd_ipl(dd_ipl_file)
if (save_file):
if (is_read):
sc64.download_save(save_file)
else:
sc64.upload_save(save_file)
if (debug_server):
sc64.debug_loop(sd_file)
except SC64Exception as e:
print(f"Error: {e}")
parser.exit(1)
except KeyboardInterrupt:
pass
finally:
sys.stdout.write("\033[0m")

View File

@ -1,97 +0,0 @@
import os
import serial
import time
import filecmp
class SC64:
__CFG_ID_FLASH_OPERATION = 10
__CFG_ID_RECONFIGURE = 11
def __init__(self, port):
self.__serial = serial.Serial(port, timeout=10.0, write_timeout=10.0)
def __query_config(self, query, arg=0):
self.__serial.write(b'CMDQ')
self.__serial.write(query.to_bytes(4, byteorder='big'))
self.__serial.write(arg.to_bytes(4, byteorder='big'))
value = self.__serial.read(4)
if (self.__serial.read(4).decode() != 'CMPQ'):
raise Exception('Bad query response')
return int.from_bytes(value, byteorder='big')
def __change_config(self, change, arg=0, ignore_response=False):
self.__serial.write(b'CMDC')
self.__serial.write(change.to_bytes(4, byteorder='big'))
self.__serial.write(arg.to_bytes(4, byteorder='big'))
if (not ignore_response and self.__serial.read(4).decode() != 'CMPC'):
raise Exception('Bad change response')
def reconfigure(self):
magic = self.__query_config(self.__CFG_ID_RECONFIGURE)
self.__change_config(self.__CFG_ID_RECONFIGURE, magic, ignore_response=True)
time.sleep(0.2)
def read_flash(self, file):
size = self.__query_config(self.__CFG_ID_FLASH_OPERATION)
print(f'Flash size: {(size / 1024.0):1.1f} kB')
self.__serial.write(b'CMDR')
self.__serial.write((0).to_bytes(4, byteorder='big'))
self.__serial.write((size).to_bytes(4, byteorder='big'))
flash = self.__serial.read(size)
response = self.__serial.read(4)
if (response.decode() == 'CMPR'):
with open(file, 'wb') as f:
f.write(flash)
else:
raise Exception('There was a problem while reading flash data')
def program_flash(self, file):
length = os.path.getsize(file)
offset = 0
with open(file, 'rb') as f:
self.__serial.write(b'CMDW')
self.__serial.write(offset.to_bytes(4, byteorder='big'))
self.__serial.write(length.to_bytes(4, byteorder='big'))
self.__serial.write(f.read())
response = self.__serial.read(4)
if (response.decode() != 'CMPW'):
raise Exception('There was a problem while sending flash data')
self.__change_config(self.__CFG_ID_FLASH_OPERATION)
file = '../../fw/output_files/SC64_update.bin'
backup_file = 'SC64_backup.bin'
verify_file = 'SC64_update_verify.bin'
port = 'COM7'
sc64 = SC64(port)
print('Making backup...')
sc64.read_flash(backup_file)
print('done\n')
print('Flashing... ')
sc64.program_flash(file)
print('done\n')
print('Reconfiguring... ')
sc64.reconfigure()
print('done\n')
print('Verifying... ')
sc64.read_flash(verify_file)
if (filecmp.cmp(file, verify_file)):
print('success!\n')
else:
print('failure.\n')
print('Update done!')

View File

@ -1,11 +1,11 @@
TOOLCHAIN = riscv32-unknown-elf- TOOLCHAIN = riscv32-unknown-elf-
CC = $(TOOLCHAIN)gcc CC = $(TOOLCHAIN)gcc
AS = $(TOOLCHAIN)as CXX = $(TOOLCHAIN)g++
OBJCOPY = $(TOOLCHAIN)objcopy OBJCOPY = $(TOOLCHAIN)objcopy
OBJDUMP = $(TOOLCHAIN)objdump OBJDUMP = $(TOOLCHAIN)objdump
SIZE = $(TOOLCHAIN)size SIZE = $(TOOLCHAIN)size
FLAGS = -mabi=ilp32 -march=rv32i FLAGS = -mabi=ilp32 -march=rv32i $(USER_FLAGS)
CFLAGS = -Os -Wall -ffunction-sections -fdata-sections -ffreestanding -MMD -MP CFLAGS = -Os -Wall -ffunction-sections -fdata-sections -ffreestanding -MMD -MP
LDFLAGS = -nostartfiles -Wl,--gc-sections LDFLAGS = -nostartfiles -Wl,--gc-sections
@ -22,27 +22,33 @@ VPATH = $(SRC_DIR)
$(@info $(shell mkdir -p ./$(BUILD_DIR) &> /dev/null)) $(@info $(shell mkdir -p ./$(BUILD_DIR) &> /dev/null))
all: $(BUILD_DIR)/governor.hex $(BUILD_DIR)/%.S.o: %.S
$(CC) -x assembler-with-cpp $(FLAGS) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/%.c.o: %.c $(BUILD_DIR)/%.c.o: %.c
$(CC) $(FLAGS) $(CFLAGS) $(USER_FLAGS) -c $< -o $@ $(CC) $(FLAGS) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/%.S.o: %.S $(BUILD_DIR)/governor.elf: $(OBJS) SC64.ld
$(AS) $(FLAGS) $(ASFLAGS) -c $< -o $@ $(CXX) $(FLAGS) $(LDFLAGS) -TSC64.ld $(OBJS) -o $@
@$(OBJDUMP) -D $@ > $(BUILD_DIR)/governor.lst
$(BUILD_DIR)/governor.hex: $(OBJS) SC64.ld $(BUILD_DIR)/governor.bin: $(BUILD_DIR)/governor.elf
$(CC) $(FLAGS) $(LDFLAGS) -TSC64.ld $(OBJS) -o $(BUILD_DIR)/governor.elf @$(OBJCOPY) -O binary $< $@
$(OBJDUMP) -D $(BUILD_DIR)/governor.elf > $(BUILD_DIR)/governor.map
$(OBJCOPY) -O binary $(BUILD_DIR)/governor.elf $(BUILD_DIR)/governor.bin $(BUILD_DIR)/governor.hex: $(BUILD_DIR)/governor.bin
$(OBJCOPY) -I binary -O ihex $(BUILD_DIR)/governor.bin $@ @$(OBJCOPY) -I binary -O ihex $< $@
print_size: $(BUILD_DIR)/governor.elf
@echo 'Size of modules:' @echo 'Size of modules:'
@$(SIZE) -B -t --common $(OBJS) @$(SIZE) -B -d -t --common $(OBJS)
@echo 'Size of governor:' @echo 'Size of governor:'
@$(SIZE) -B $(BUILD_DIR)/governor.elf @$(SIZE) -B -d $<
all: $(BUILD_DIR)/governor.hex print_size
clean: clean:
rm -rf ./$(BUILD_DIR)/* @rm -rf ./$(BUILD_DIR)/*
.PHONY: all clean .PHONY: all clean print_size
-include $(DEPS) -include $(DEPS)

View File

@ -37,7 +37,7 @@ SECTIONS {
. = ALIGN(4); . = ALIGN(4);
_sbss = .; _sbss = .;
*(.sbss .sbss.* .gnu.linkonce.sb.*) *(.sbss .sbss.* .gnu.linkonce.sb.*)
*(.scommon) *(.scommon .scommon.*)
*(.bss .bss.* .gnu.linkonce.b.*) *(.bss .bss.* .gnu.linkonce.b.*)
*(COMMON) *(COMMON)
. = ALIGN(4); . = ALIGN(4);

View File

@ -13,7 +13,7 @@
#define SAVE_OFFSET_PKST2 (0x01608000UL) #define SAVE_OFFSET_PKST2 (0x01608000UL)
#define DEFAULT_SAVE_OFFSET (0x03FE0000UL) #define DEFAULT_SAVE_OFFSET (0x03FE0000UL)
#define DEFAULT_DD_OFFSET (0x03BE0000UL) #define DEFAULT_DDIPL_OFFSET (0x03BE0000UL)
enum cfg_id { enum cfg_id {
@ -25,9 +25,11 @@ enum cfg_id {
CFG_ID_CIC_SEED, CFG_ID_CIC_SEED,
CFG_ID_TV_TYPE, CFG_ID_TV_TYPE,
CFG_ID_SAVE_OFFEST, CFG_ID_SAVE_OFFEST,
CFG_ID_DD_OFFEST, CFG_ID_DDIPL_OFFEST,
CFG_ID_SKIP_BOOTLOADER, CFG_ID_BOOT_MODE,
CFG_ID_FLASH_OPERATION, CFG_ID_FLASH_SIZE,
CFG_ID_FLASH_READ,
CFG_ID_FLASH_PROGRAM,
CFG_ID_RECONFIGURE, CFG_ID_RECONFIGURE,
}; };
@ -41,11 +43,19 @@ enum save_type {
SAVE_TYPE_FLASHRAM_PKST2 = 6, SAVE_TYPE_FLASHRAM_PKST2 = 6,
}; };
enum boot_mode {
BOOT_MODE_MENU = 0,
BOOT_MODE_ROM = 1,
BOOT_MODE_DD = 2,
BOOT_MODE_DIRECT = 3,
};
struct process { struct process {
enum save_type save_type; enum save_type save_type;
uint16_t cic_seed; uint16_t cic_seed;
uint8_t tv_type; uint8_t tv_type;
enum boot_mode boot_mode;
}; };
static struct process p; static struct process p;
@ -103,6 +113,10 @@ static void set_save_type (enum save_type save_type) {
} }
uint32_t cfg_get_version (void) {
return CFG->VERSION;
}
void cfg_update (uint32_t *args) { void cfg_update (uint32_t *args) {
switch (args[0]) { switch (args[0]) {
case CFG_ID_SCR: case CFG_ID_SCR:
@ -129,13 +143,17 @@ void cfg_update (uint32_t *args) {
case CFG_ID_SAVE_OFFEST: case CFG_ID_SAVE_OFFEST:
CFG->SAVE_OFFSET = args[1]; CFG->SAVE_OFFSET = args[1];
break; break;
case CFG_ID_DD_OFFEST: case CFG_ID_DDIPL_OFFEST:
CFG->DD_OFFSET = args[1]; CFG->DDIPL_OFFSET = args[1];
break; break;
case CFG_ID_SKIP_BOOTLOADER: case CFG_ID_BOOT_MODE:
change_scr_bits(CFG_SCR_SKIP_BOOTLOADER, args[1]); p.boot_mode = args[1];
change_scr_bits(CFG_SCR_SKIP_BOOTLOADER, args[1] == BOOT_MODE_DIRECT);
break; break;
case CFG_ID_FLASH_OPERATION: case CFG_ID_FLASH_READ:
flash_read(args[1]);
break;
case CFG_ID_FLASH_PROGRAM:
flash_program(args[1]); flash_program(args[1]);
break; break;
case CFG_ID_RECONFIGURE: case CFG_ID_RECONFIGURE:
@ -175,14 +193,14 @@ void cfg_query (uint32_t *args) {
case CFG_ID_SAVE_OFFEST: case CFG_ID_SAVE_OFFEST:
args[1] = CFG->SAVE_OFFSET; args[1] = CFG->SAVE_OFFSET;
break; break;
case CFG_ID_DD_OFFEST: case CFG_ID_DDIPL_OFFEST:
args[1] = CFG->DD_OFFSET; args[1] = CFG->DDIPL_OFFSET;
break; break;
case CFG_ID_SKIP_BOOTLOADER: case CFG_ID_BOOT_MODE:
args[1] = CFG->SCR & CFG_SCR_SKIP_BOOTLOADER; args[1] = p.boot_mode;
break; break;
case CFG_ID_FLASH_OPERATION: case CFG_ID_FLASH_SIZE:
args[1] = flash_read(args[1]); args[1] = flash_size();
break; break;
case CFG_ID_RECONFIGURE: case CFG_ID_RECONFIGURE:
args[1] = CFG->RECONFIGURE; args[1] = CFG->RECONFIGURE;
@ -194,11 +212,12 @@ void cfg_query (uint32_t *args) {
void cfg_init (void) { void cfg_init (void) {
set_save_type(SAVE_TYPE_NONE); set_save_type(SAVE_TYPE_NONE);
CFG->DD_OFFSET = DEFAULT_DD_OFFSET; CFG->DDIPL_OFFSET = DEFAULT_DDIPL_OFFSET;
CFG->SCR = CFG_SCR_CPU_READY | CFG_SCR_SDRAM_SWITCH; CFG->SCR = CFG_SCR_CPU_READY | CFG_SCR_SDRAM_SWITCH;
p.cic_seed = 0xFFFF; p.cic_seed = 0xFFFF;
p.tv_type = 0x03; p.tv_type = 0x03;
p.boot_mode = BOOT_MODE_MENU;
} }

View File

@ -5,6 +5,7 @@
#include "sys.h" #include "sys.h"
uint32_t cfg_get_version (void);
void cfg_update (uint32_t *args); void cfg_update (uint32_t *args);
void cfg_query (uint32_t *args); void cfg_query (uint32_t *args);
void cfg_init (void); void cfg_init (void);

View File

@ -1,15 +1,17 @@
#include "flash.h" #include "flash.h"
uint32_t flash_read (uint32_t sdram_offset) { uint32_t flash_size (void) {
return FLASH_SIZE;
}
void flash_read (uint32_t sdram_offset) {
io32_t *flash = (io32_t *) (FLASH_BASE); io32_t *flash = (io32_t *) (FLASH_BASE);
io32_t *sdram = (io32_t *) (SDRAM_BASE + sdram_offset); io32_t *sdram = (io32_t *) (SDRAM_BASE + sdram_offset);
for (size_t i = 0; i < FLASH_SIZE; i += 4) { for (size_t i = 0; i < FLASH_SIZE; i += 4) {
*sdram++ = *flash++; *sdram++ = *flash++;
} }
return FLASH_SIZE;
} }
void flash_program (uint32_t sdram_offset) { void flash_program (uint32_t sdram_offset) {

View File

@ -5,7 +5,8 @@
#include "sys.h" #include "sys.h"
uint32_t flash_read (uint32_t sdram_offset); uint32_t flash_size(void);
void flash_read (uint32_t sdram_offset);
void flash_program (uint32_t sdram_offset); void flash_program (uint32_t sdram_offset);

View File

@ -1,5 +1,4 @@
.section .text.reset_handler .section .text.reset_handler
reset_handler: reset_handler:
.global reset_handler .global reset_handler

View File

@ -76,7 +76,9 @@ typedef volatile struct i2c_regs {
typedef volatile struct usb_regs { typedef volatile struct usb_regs {
io32_t SCR; io32_t SCR;
io8_t DR; io8_t DR;
io8_t __padding[3]; io8_t __padding_1[3];
io8_t ESCAPE;
io8_t __padding_2[3];
} usb_regs_t; } usb_regs_t;
#define USB_BASE (0x40000000UL) #define USB_BASE (0x40000000UL)
@ -88,6 +90,8 @@ typedef volatile struct usb_regs {
#define USB_SCR_FLUSH_TX (1 << 3) #define USB_SCR_FLUSH_TX (1 << 3)
#define USB_SCR_ENABLED (1 << 4) #define USB_SCR_ENABLED (1 << 4)
#define USB_SCR_PWREN (1 << 5) #define USB_SCR_PWREN (1 << 5)
#define USB_SCR_ESCAPE_PENDING (1 << 6)
#define USB_SCR_ESCAPE_ACK (1 << 7)
typedef volatile struct uart_regs { typedef volatile struct uart_regs {
@ -120,7 +124,7 @@ typedef volatile struct dma_regs {
typedef volatile struct cfg_regs { typedef volatile struct cfg_regs {
io32_t SCR; io32_t SCR;
io32_t DD_OFFSET; io32_t DDIPL_OFFSET;
io32_t SAVE_OFFSET; io32_t SAVE_OFFSET;
io8_t CMD; io8_t CMD;
io8_t __padding[3]; io8_t __padding[3];
@ -185,4 +189,7 @@ typedef volatile struct joybus_regs {
#define JOYBUS_SCR_TX_LENGTH_BIT (16) #define JOYBUS_SCR_TX_LENGTH_BIT (16)
void reset_handler(void);
#endif #endif

View File

@ -13,18 +13,19 @@ static bool rx_byte (uint8_t *data) {
return true; return true;
} }
static uint8_t rx_word_current_byte = 0;
static uint32_t rx_word_buffer = 0;
static bool rx_word (uint32_t *data) { static bool rx_word (uint32_t *data) {
static uint8_t current_byte = 0;
static uint32_t buffer = 0;
uint8_t tmp; uint8_t tmp;
while (rx_byte(&tmp)) { while (rx_byte(&tmp)) {
buffer = (buffer << 8) | tmp; rx_word_buffer = (rx_word_buffer << 8) | tmp;
current_byte += 1; rx_word_current_byte += 1;
if (current_byte == 4) { if (rx_word_current_byte == 4) {
current_byte = 0; rx_word_current_byte = 0;
*data = buffer; *data = rx_word_buffer;
buffer = 0; rx_word_buffer = 0;
return true; return true;
} }
@ -43,12 +44,13 @@ static bool tx_byte (uint8_t data) {
return true; return true;
} }
static uint8_t tx_word_current_byte = 0;
static bool tx_word (uint32_t data) { static bool tx_word (uint32_t data) {
static uint8_t current_byte = 0; while (tx_byte(data >> ((3 - tx_word_current_byte) * 8))) {
while (tx_byte(data >> ((3 - current_byte) * 8))) { tx_word_current_byte += 1;
current_byte += 1; if (tx_word_current_byte == 4) {
if (current_byte == 4) { tx_word_current_byte = 0;
current_byte = 0;
return true; return true;
} }
@ -152,25 +154,25 @@ void usb_debug_reset (void) {
USB->SCR = USB_SCR_FLUSH_TX | USB_SCR_FLUSH_RX; USB->SCR = USB_SCR_FLUSH_TX | USB_SCR_FLUSH_RX;
} }
static uint8_t rx_cmd_current_byte = 0;
static uint32_t rx_cmd_buffer = 0;
static bool rx_cmd (uint32_t *data) { static bool rx_cmd (uint32_t *data) {
static uint8_t current_byte = 0;
static uint32_t buffer = 0;
uint8_t tmp; uint8_t tmp;
while (rx_byte(&tmp)) { while (rx_byte(&tmp)) {
current_byte += 1; rx_cmd_current_byte += 1;
if ((current_byte != 4) && (tmp != (USB_CMD_TOKEN >> (8 * (4 - current_byte)) & 0xFF))) { if ((rx_cmd_current_byte != 4) && (tmp != (USB_CMD_TOKEN >> (8 * (4 - rx_cmd_current_byte)) & 0xFF))) {
current_byte = 0; rx_cmd_current_byte = 0;
buffer = 0; rx_cmd_buffer = 0;
return false; return false;
} }
buffer = (buffer << 8) | tmp; rx_cmd_buffer = (rx_cmd_buffer << 8) | tmp;
if (current_byte == 4) { if (rx_cmd_current_byte == 4) {
current_byte = 0; rx_cmd_current_byte = 0;
*data = buffer; *data = rx_cmd_buffer;
buffer = 0; rx_cmd_buffer = 0;
return true; return true;
} }
@ -179,6 +181,19 @@ static bool rx_cmd (uint32_t *data) {
return false; return false;
} }
static void handle_escape (void) {
if (USB->SCR & USB_SCR_ESCAPE_PENDING) {
if (USB->ESCAPE == 'R') {
if (p.dma_in_progress) {
dma_stop();
while (dma_busy());
}
usb_init();
}
USB->SCR |= USB_SCR_ESCAPE_ACK;
}
}
void usb_init (void) { void usb_init (void) {
USB->SCR = USB_SCR_ENABLED | USB_SCR_FLUSH_TX | USB_SCR_FLUSH_RX; USB->SCR = USB_SCR_ENABLED | USB_SCR_FLUSH_TX | USB_SCR_FLUSH_RX;
@ -186,10 +201,18 @@ void usb_init (void) {
p.state = STATE_IDLE; p.state = STATE_IDLE;
p.debug_rx_busy = false; p.debug_rx_busy = false;
p.debug_tx_busy = false; p.debug_tx_busy = false;
rx_word_current_byte = 0;
rx_word_buffer = 0;
tx_word_current_byte = 0;
rx_cmd_current_byte = 0;
rx_cmd_buffer = 0;
} }
void process_usb (void) { void process_usb (void) {
handle_escape();
switch (p.state) { switch (p.state) {
case STATE_IDLE: case STATE_IDLE:
if (p.debug_tx_busy) { if (p.debug_tx_busy) {
@ -222,6 +245,12 @@ void process_usb (void) {
case STATE_DATA: case STATE_DATA:
switch (p.cmd) { switch (p.cmd) {
case 'V':
if (tx_word(cfg_get_version())) {
p.state = STATE_RESPONSE;
}
break;
case 'C': case 'C':
cfg_update(p.args); cfg_update(p.args);
p.state = STATE_RESPONSE; p.state = STATE_RESPONSE;