Skip to content
Permalink
Browse files

separate status into fields governed by MCU and by SNES

Avoid status getting overwritten by race conditions
  • Loading branch information...
mrehkopf committed Apr 4, 2019
1 parent 0ed0d29 commit 5034c3d06bafa6c9f0a8d43c30f618f13d2594aa
Showing with 43 additions and 32 deletions.
  1. +9 −7 snes/memmap.i65
  2. +2 −2 src/cfg.c
  3. +8 −7 src/main.c
  4. +4 −4 src/memory.c
  5. +2 −1 src/memory.h
  6. +7 −3 src/snes.c
  7. +6 −3 src/snes.h
  8. +5 −5 src/sysinfo.c
@@ -50,7 +50,8 @@

#define FILESEL_CWD $FF0000
#define CFG_ADDR $FF0100
#define ST_ADDR $FF1100
#define ST_MCU_ADDR $FF1100
#define ST_SNES_ADDR $FF1110
#define SYSINFO_BLK $FF1200
#define LAST_GAME $FF1420
#define WRAM_BAK $FF2000
@@ -107,12 +108,13 @@

#define MENU_ENTRY_SIZE 23

#define ST_RTC_VALID ST_ADDR+$0000
#define ST_NUM_RECENT_GAMES ST_ADDR+$0001
#define ST_IS_U16 ST_ADDR+$0002
#define ST_U16_CFG ST_ADDR+$0003
#define ST_PAIRMODE ST_ADDR+$0004
#define ST_HAS_SATELLAVIEW ST_ADDR+$0005
#define ST_RTC_VALID ST_MCU_ADDR+$0000
#define ST_NUM_RECENT_GAMES ST_MCU_ADDR+$0001
#define ST_PAIRMODE ST_MCU_ADDR+$0002

#define ST_IS_U16 ST_SNES_ADDR+$0000
#define ST_U16_CFG ST_SNES_ADDR+$0001
#define ST_HAS_SATELLAVIEW ST_SNES_ADDR+$0002

#define MTYPE_FUNC $01
#define MTYPE_SUBMENU $02
@@ -38,7 +38,7 @@ cfg_t CFG_DEFAULT = {
};

cfg_t CFG;
extern status_t ST;
extern mcu_status_t STM;

int cfg_save() {
int err = 0;
@@ -277,7 +277,7 @@ void cfg_dump_recent_games_for_snes(uint32_t address) {
f_gets(fntmp, 255, &file_handle);
sram_writestrn(strrchr((const char*)fntmp, '/')+1, address+256*index, 256);
}
ST.num_recent_games = index;
STM.num_recent_games = index;
file_close();
}

@@ -47,7 +47,8 @@ extern snes_romprops_t romprops;
extern volatile int reset_changed;

extern volatile cfg_t CFG;
extern volatile status_t ST;
extern volatile mcu_status_t STM;
extern volatile snes_status_t STS;

void menu_cmd_readdir(void) {
uint8_t path[256];
@@ -180,21 +181,21 @@ printf("PCONP=%lx\n", LPC_SC->PCONP);

if((rtc_state = rtc_isvalid()) != RTC_OK) {
printf("RTC invalid!\n");
ST.rtc_valid = 0xff;
STM.rtc_valid = 0xff;
set_bcdtime(0x20120701000000LL);
set_fpga_time(0x20120701000000LL);
invalidate_rtc();
} else {
printf("RTC valid!\n");
ST.rtc_valid = 0;
STM.rtc_valid = 0;
set_fpga_time(get_bcdtime());
}
sram_memset(SRAM_SYSINFO_ADDR, 13*40, 0x20);
printf("SNES GO!\n");
snes_reset(1);
fpga_reset_srtc_state();
if(!firstboot) {
if(ST.is_u16 && (ST.u16_cfg & 0x01)) {
if(STS.is_u16 && (STS.u16_cfg & 0x01)) {
delay_ms(59*SNES_RESET_PULSELEN_MS);
}
}
@@ -204,15 +205,15 @@ printf("PCONP=%lx\n", LPC_SC->PCONP);
enum cicstates cic_state = get_cic_state();
switch(cic_state) {
case CIC_PAIR:
ST.pairmode = 1;
STM.pairmode = 1;
printf("PAIR MODE ENGAGED!\n");
cic_pair(CFG.vidmode_menu, CFG.vidmode_menu);
break;
case CIC_SCIC:
ST.pairmode = 1;
STM.pairmode = 1;
break;
default:
ST.pairmode = 0;
STM.pairmode = 0;
}
fpga_set_dac_boost(CFG.msu_volume_boost);
cfg_load_to_menu();
@@ -57,7 +57,7 @@ extern uint32_t saveram_crc_old;
extern uint8_t sram_crc_valid;
extern uint32_t sram_crc_romsize;
extern cfg_t CFG;
extern status_t ST;
extern snes_status_t STS;

void sram_hexdump(uint32_t addr, uint32_t len) {
static uint8_t buf[16];
@@ -364,7 +364,7 @@ uint32_t load_rom(uint8_t* filename, uint32_t base_addr, uint8_t flags) {
}
printf("done\n");

if(cfg_is_r213f_override_enabled() && (filename != (uint8_t*)MENU_FILENAME) && !ST.is_u16) {
if(cfg_is_r213f_override_enabled() && (filename != (uint8_t*)MENU_FILENAME) && !STS.is_u16) {
romprops.fpga_features |= FEAT_213F; /* e.g. for general consoles */
}
fpga_set_213f(romprops.region);
@@ -390,7 +390,7 @@ uint32_t load_rom(uint8_t* filename, uint32_t base_addr, uint8_t flags) {
romprops.fpga_features |= FEAT_2100_LIMIT(cfg_get_brightness_limit());

/* enable Satellaview Base emulation only if no physical Satellaview Base unit is present */
if(!ST.has_satellaview) {
if(!STS.has_satellaview) {
romprops.fpga_features |= FEAT_SATELLABASE;
}

@@ -405,7 +405,7 @@ uint32_t load_rom(uint8_t* filename, uint32_t base_addr, uint8_t flags) {
printf("resetting SNES\n");
fpga_dspx_reset(1);
snes_reset(1);
if(ST.is_u16 && (ST.u16_cfg & 0x01)) {
if(STS.is_u16 && (STS.u16_cfg & 0x01)) {
delay_ms(60*SNES_RESET_PULSELEN_MS);
} else {
delay_ms(SNES_RESET_PULSELEN_MS);
@@ -50,7 +50,8 @@
#define SRAM_MENU_CFG_ADDR (0xFF0100L)
#define SRAM_CMD_ADDR (0xFF1000L)
#define SRAM_PARAM_ADDR (0xFF1004L)
#define SRAM_STATUS_ADDR (0xFF1100L)
#define SRAM_MCU_STATUS_ADDR (0xFF1100L)
#define SRAM_SNES_STATUS_ADDR (0xFF1110L)
#define SRAM_SYSINFO_ADDR (0xFF1200L)
#define SRAM_LASTGAME_ADDR (0xFF1420L)
#define SRAM_SCRATCHPAD (0xFFFF00L)
@@ -53,9 +53,13 @@ extern cfg_t CFG;
volatile int reset_changed;
volatile int reset_pressed;

status_t ST = {
mcu_status_t STM = {
.rtc_valid = 0xff,
.num_recent_games = 0,
.pairmode = 0
};

snes_status_t STS = {
.is_u16 = 0,
.u16_cfg = 0xff,
.has_satellaview = 0
@@ -480,11 +484,11 @@ void snescmd_prepare_nmihook() {
}

void status_load_to_menu() {
sram_writeblock(&ST, SRAM_STATUS_ADDR, sizeof(status_t));
sram_writeblock(&STM, SRAM_MCU_STATUS_ADDR, sizeof(mcu_status_t));
}

void status_save_from_menu() {
sram_readblock(&ST, SRAM_STATUS_ADDR, sizeof(status_t));
sram_readblock(&STS, SRAM_SNES_STATUS_ADDR, sizeof(snes_status_t));
}

/*
@@ -96,14 +96,17 @@

enum snes_reset_state { SNES_RESET_NONE = 0, SNES_RESET_SHORT, SNES_RESET_LONG };

typedef struct __attribute__ ((__packed__)) _status {
typedef struct __attribute__ ((__packed__)) _mcu_status {
uint8_t rtc_valid;
uint8_t num_recent_games;
uint8_t pairmode;
} mcu_status_t;

typedef struct __attribute__ ((__packed__)) _snes_status {
uint8_t is_u16;
uint8_t u16_cfg;
uint8_t pairmode;
uint8_t has_satellaview;
} status_t;
} snes_status_t;

uint8_t crc_valid;

@@ -15,7 +15,7 @@
#include "sdnative.h"
#include "sysinfo.h"

extern status_t ST;
extern snes_status_t STS;

static uint32_t sd_tacc_max, sd_tacc_avg;

@@ -116,11 +116,11 @@ int write_sysinfo(int sd_measured) {
memset(linebuf+len, 0x20, 40-len);
sram_writeblock(linebuf, sram_addr, 40);
sram_addr += 40;
if(ST.is_u16) {
if(ST.u16_cfg & 0x01) {
len = snprintf(linebuf, sizeof(linebuf), "Ultra16 serial no. %d (Autoboot On)", ST.is_u16);
if(STS.is_u16) {
if(STS.u16_cfg & 0x01) {
len = snprintf(linebuf, sizeof(linebuf), "Ultra16 serial no. %d (Autoboot On)", STS.is_u16);
} else {
len = snprintf(linebuf, sizeof(linebuf), "Ultra16 serial no. %d (Autoboot Off)", ST.is_u16);
len = snprintf(linebuf, sizeof(linebuf), "Ultra16 serial no. %d (Autoboot Off)", STS.is_u16);
}
memset(linebuf+len, 0x20, 40-len);
sram_writeblock(linebuf, sram_addr, 40);

0 comments on commit 5034c3d

Please sign in to comment.
You can’t perform that action at this time.