Skip to content

Commit

Permalink
Merge branch 'main' of github.com:nand2mario/snestang
Browse files Browse the repository at this point in the history
  • Loading branch information
nand2mario committed Jun 9, 2024
2 parents 898bbe5 + 7a2223e commit 36ff902
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 46 deletions.
59 changes: 59 additions & 0 deletions firmware/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#
# The xPack GNU RISC-V Embedded GCC
# https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack
#
TARGET_ELF = firmware.elf
TARGET_BIN = firmware.bin
RISCV = /opt/xpack-riscv-none-elf-gcc-13.2.0-2/bin/riscv-none-elf
CC = $(RISCV)-gcc
OBJCOPY = $(RISCV)-objcopy
OBJDUMP = $(RISCV)-objdump
CFLAGS = -Wall -O2 -g -mabi=ilp32 -march=rv32i -ffreestanding
LFLAGS = -mabi=ilp32 -march=rv32i -Wl,--build-id=none,-Bstatic,-T,baremetal.ld -nostdlib
LIBS = -lgcc

SRCS := start.S firmware.c picorv32.c spi_sd.c spiflash.c \
fatfs/diskio.c fatfs/ff.c fatfs/ffunicode.c
OBJS := $(SRCS:.c=.o)
OBJS := $(OBJS:.S=.o)
HDRS := $(wildcard *.h)

ifeq ($(VERBOSE),1)
Q =
else
Q = @
endif

default: $(TARGET_BIN)
all: default

%.o: %.c $(HDRS)
ifneq ($(VERBOSE),1)
@echo CC $@
endif
$(Q)$(CC) $(CFLAGS) -c $< -o $@

%.o: %.S $(HDRS)
ifneq ($(VERBOSE),1)
@echo CC $@
endif
$(Q)$(CC) $(CFLAGS) -c $< -o $@

.PRECIOUS: $(TARGET_BIN) $(TARGET_ELF) $(OBJS)

$(TARGET_ELF): $(OBJS)
ifneq ($(VERBOSE),1)
@echo CC $@
endif
$(Q)$(CC) $(LFLAGS) $(OBJS) $(LIBS) -o $@

$(TARGET_BIN): $(TARGET_ELF)
ifneq ($(VERBOSE),1)
@echo OBJCOPY $@
endif
$(Q)$(OBJCOPY) $(TARGET_ELF) $(TARGET_BIN) -O binary

clean:
$(Q)rm -f $(OBJS) $(TARGET_ELF) $(TARGET_BIN)

.PHONY: default all clean
9 changes: 5 additions & 4 deletions firmware/baremetal.ld
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/
MEMORY
{
RAM (RWX) : ORIGIN = 0x00000, LENGTH = 0x200000
FLASH (RX): ORIGIN = 0x00000, LENGTH = 100K
RAM (RWX): ORIGIN = 100K, LENGTH = 0x1e7000
}
SECTIONS
{
Expand All @@ -14,7 +15,7 @@ SECTIONS
start.o (.text)
*(.text)
_end = .; /* as expected by syscalls.c */
}
} >FLASH

/* global and static variable with initial values */
.data :
Expand All @@ -26,7 +27,7 @@ SECTIONS
*(.data*) /* .data* sections */
*(.sdata) /* .sdata sections */
*(.sdata*) /* .sdata* sections */
. = ALIGN(4);
. = ALIGN(4);
_edata = .;
}

Expand All @@ -48,5 +49,5 @@ SECTIONS
{
. = ALIGN(4);
_heap_start = .; /* define a global symbol at heap start */
}
}
}
15 changes: 0 additions & 15 deletions firmware/fatfs/diskio.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
int result;

switch (pdrv) {
case DEV_SD :
if (sd_initialized)
Expand All @@ -48,9 +45,6 @@ DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
int result;

// print("disk_initialize\n");

switch (pdrv) {
Expand Down Expand Up @@ -80,9 +74,6 @@ DRESULT disk_read (
UINT count /* Number of sectors to read */
)
{
DRESULT res;
int result;

switch (pdrv) {
case DEV_SD :
if (!sd_initialized)
Expand Down Expand Up @@ -110,9 +101,6 @@ DRESULT disk_write (
UINT count /* Number of sectors to write */
)
{
DRESULT res;
int result;

switch (pdrv) {
case DEV_SD :
if (!sd_initialized)
Expand All @@ -139,9 +127,6 @@ DRESULT disk_ioctl (
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res;
int result;

switch (pdrv) {
case DEV_SD :
if (cmd == GET_SECTOR_SIZE)
Expand Down
38 changes: 18 additions & 20 deletions firmware/firmware.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ int file_len; // number of files on this page
int load_dir(char *dir, int start, int len, int *count) {
DEBUG("load_dir: %s, start=%d, len=%d\n", dir, start, len);
int cnt = 0;
int r = 0;
DIR d;
file_len = 0;
// initiaze sd again to be sure
Expand Down Expand Up @@ -388,13 +387,12 @@ bool verify_flash(uint8_t *corebuf, uint32_t addr, int cnt) {
return true;
}

static int load_buf_off; // next available pos in load_buf
static int load_buf_len; // length of data in load_buf
static unsigned int load_buf_off; // next available pos in load_buf
static unsigned int load_buf_len; // length of data in load_buf

// load a line into buf (max length *len), *len is updated to actual length of string
// this uses load_buf[] internally
void read_line(FIL *fp, char *buf, int *len) {
int br;
int i = 0;
bool done = false;
while (!done) {
Expand Down Expand Up @@ -438,9 +436,7 @@ void load_core(char *fname, int verify) {
}
int addr = 0;
char *s = load_buf;
int cnt = 0;
int bol = 1;
int br;
unsigned int cnt = 0;
while (!f_eof(&f) && (binfile || addr < 32*1024)) { // write only 32KB for .fs
if (binfile) {
// uart_print("Checking cycles\n");
Expand Down Expand Up @@ -636,8 +632,10 @@ int in_game;

// return 0 if snes header is successfully parsed at off
// typ 0: LoROM, 1: HiROM, 2: ExHiROM
int parse_snes_header(FIL *fp, int pos, int file_size, int typ, uint8_t *hdr, int *map_ctrl, int *rom_type_header, int *rom_size, int *ram_size, int *company) {
int br;
int parse_snes_header(FIL *fp, int pos, int file_size, int typ, char *hdr,
int *map_ctrl, int *rom_type_header, int *rom_size,
int *ram_size, int *company) {
unsigned int br;
if (f_lseek(fp, pos))
return 1;
f_read(fp, hdr, 64, &br);
Expand Down Expand Up @@ -687,6 +685,7 @@ int parse_snes_header(FIL *fp, int pos, int file_size, int typ, uint8_t *hdr, in
// return 0 if successful
int loadsnes(int rom) {
FIL f;
int r = 1;
strncpy(load_fname, pwd, 1024);
strncat(load_fname, "/", 1024);
strncat(load_fname, file_names[rom], 1024);
Expand All @@ -707,12 +706,12 @@ int loadsnes(int rom) {
// initiaze sd again to be sure
if (sd_init() != 0) return 99;

int r = f_open(&f, load_fname, FA_READ);
r = f_open(&f, load_fname, FA_READ);
if (r) {
status("Cannot open file");
goto loadsnes_end;
}
int br, total = 0;
unsigned int br, total = 0;
int size = file_sizes[rom];
int map_ctrl, rom_type_header, rom_size, ram_size, company;
// parse SNES header from ROM file
Expand Down Expand Up @@ -792,6 +791,7 @@ int loadsnes(int rom) {
// return 0 if successful
int loadnes(int rom) {
FIL f;
int r = 1;
strncpy(load_fname, pwd, 1024);
strncat(load_fname, "/", 1024);
strncat(load_fname, file_names[rom], 1024);
Expand All @@ -808,13 +808,13 @@ int loadnes(int rom) {
// initiaze sd again to be sure
if (sd_init() != 0) return 99;

int r = f_open(&f, load_fname, FA_READ);
r = f_open(&f, load_fname, FA_READ);
if (r) {
status("Cannot open file");
goto loadnes_end;
}
int off = 0, br, total = 0;
int size = file_sizes[rom];
unsigned int off = 0, br, total = 0;
unsigned int size = file_sizes[rom];

// load actual ROM
snes_ctrl(1); // enable game loading, this resets SNES
Expand Down Expand Up @@ -846,8 +846,7 @@ int loadnes(int rom) {
overlay(0); // turn off OSD

loadnes_snes_end:
snes_ctrl(0); // turn off game loading, this starts the core
loadnes_close_file:
snes_ctrl(0); // turn off game loading, this starts the core
f_close(&f);
loadnes_end:
return r;
Expand Down Expand Up @@ -876,9 +875,9 @@ void backup_load(char *name, int size) {
goto backup_load_crc;
}
uint8_t *p = bsram;
int load = 0;
unsigned int load = 0;
while (load < size) {
int br;
unsigned int br;
if (f_read(&f, p, 1024, &br) != FR_OK || br < 1024)
break;
p += br;
Expand Down Expand Up @@ -915,7 +914,7 @@ int backup_save(char *name, int size) {
uart_printf("Cannot write save file");
return 2;
}
int bw;
unsigned int bw;
// for (int off = 0; off < size; off += bw) {
// if (f_write(&f, bsram, 1024, &bw) != FR_OK) {
if (f_write(&f, bsram, size, &bw) != FR_OK || bw != size) {
Expand Down Expand Up @@ -1081,4 +1080,3 @@ int main() {
}
}
}

11 changes: 8 additions & 3 deletions firmware/picorv32.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ int _putchar(int c, int uart) {
uart_putchar(c);
else
putchar(c);

return c;
}

int print(const char *p)
Expand All @@ -59,6 +61,8 @@ int _print(const char *p, int uart) {
uart_print(p);
else
print(p);

return 0;
}

void _print_hex_digits(uint32_t val, int nbdigits, int uart) {
Expand Down Expand Up @@ -364,7 +368,7 @@ char *strcat(char *dest, const char *src) {

while (*dest)
dest++;
while (*dest++ = *src++)
while ((*dest++ = *src++))
;
return rdest;
}
Expand All @@ -383,7 +387,8 @@ char* strncat(char* destination, const char* source, size_t num)
char * strcpy(char *strDest, const char *strSrc) {
// assert(strDest!=NULL && strSrc!=NULL);
char *temp = strDest;
while(*strDest++ = *strSrc++);
while ((*strDest++ = *strSrc++))
;
return temp;
}

Expand Down Expand Up @@ -472,4 +477,4 @@ int atoi(const char *str) {
base = 10 * base + (str[i++] - '0');
}
return sign == -1 ? -base : base;
}
}
6 changes: 2 additions & 4 deletions firmware/spi_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,8 @@ void debug_print_buf(uint8_t *buf, int len) {

int sd_readsector(uint32_t start_block, uint8_t *buffer, uint32_t sector_count) {
uint8_t response;
uint32_t ctrl;
int retries = 0;
int i;

// DEBUG("sd_readsector: %d %d\n", start_block, sector_count);
if (sector_count == 0)
return 0;
Expand Down Expand Up @@ -300,7 +299,6 @@ int sd_readsector(uint32_t start_block, uint8_t *buffer, uint32_t sector_count)
int sd_writesector(uint32_t start_block, const uint8_t *buffer, uint32_t sector_count) {
uint8_t response;
int retries = 0;
int i;

DEBUG("sd_writesector: %d %d\n", start_block, sector_count);
while (sector_count--) {
Expand Down Expand Up @@ -356,4 +354,4 @@ int sd_writesector(uint32_t start_block, const uint8_t *buffer, uint32_t sector_
}
}
return 1;
}
}

0 comments on commit 36ff902

Please sign in to comment.