Skip to content

Commit

Permalink
Added support for hookless executable patch region @ $2C00-$2FFF.
Browse files Browse the repository at this point in the history
Removed (temporarily) the snescast code to fit other changes.
  • Loading branch information
RedGuyyyy committed Dec 3, 2017
1 parent 81ce26e commit b66e7c2
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 98 deletions.
71 changes: 25 additions & 46 deletions patch/save_state/save.asm
Original file line number Diff line number Diff line change
Expand Up @@ -243,34 +243,22 @@ start:
lda.l !SS_FULL, x
beq +
cmp.l $00FFDE
beq +
beq ++
inx #6
bra -
+ inx #2
++ inx #2

; set save
lda.l !SS_FULL, x
sta.l .save_state_save_input+1
tay
inx #2
lda.l !SS_FULL, x : inx #2
sta.l .CS_SAVE_INPUT

; set load
lda.l !SS_FULL, x
sta.l .save_state_load_input+1
; set both
tya
ora.l !SS_FULL, x
sta.l .save_state_saveload_input_1+1
sta.l .save_state_saveload_input_2+1
; set same
tya
and.l !SS_FULL, x
sta.l .save_state_compare_input+1
lda.l !SS_FULL, x : inx #2
sta.l .CS_LOAD_INPUT

lda #$0000
sta.l .CS_SAVE_REQ
%ai16()
+ %ai16()
%load_registers()
.ss_start
Expand Down Expand Up @@ -314,32 +302,18 @@ start:
;beq .save_state_code ; temp workaround for reading no input
sta.l .CS_INPUT_CUR
; Savestate code starts here -- no more customization should be needed below here
.save_state_code
; byetUDLR axlr0000
;lda !SRAM_CS_INPUT_CUR
.save_state_compare_input
bit !SS_INPUT_COMPARE
beq +
and.l .CS_SAVE_INPUT
cmp.l .CS_SAVE_INPUT
beq .save_state

eor.l .CS_INPUT_PREV ; changed buttons
and.l .CS_INPUT_CUR ; changed buttons that are newly pressed
.save_state_saveload_input_1
bit !SS_INPUT_SAVE|!SS_INPUT_LOAD ; only look at select + L/R
beq +
lda.l .CS_INPUT_CUR
.save_state_saveload_input_2
and !SS_INPUT_SAVE|!SS_INPUT_LOAD
.save_state_save_input
cmp !SS_INPUT_SAVE
beq .save_state
.save_state_load_input
cmp !SS_INPUT_LOAD
;eor.l .CS_INPUT_PREV ; changed buttons
;and.l .CS_INPUT_CUR ; changed buttons that are newly pressed
and.l .CS_LOAD_INPUT
cmp.l .CS_LOAD_INPUT
bne +
jmp .load_state

; check programmable trigger
+ lda.l .CS_SAVE_REQ ; loads both
beq .save_state_jump_exit
Expand Down Expand Up @@ -697,6 +671,7 @@ start:
dw $614A, $000A,$00, $2142; mickey's magical quest (US)
dw $24DD, $0176,$7E, $2140; nosferatu (US) ; Something w/ interrupts going on in this game.

; FIXME add a common fix location that can be checked
dw $0000, $0000,$00, $0000
.load_dma_regs_start
Expand Down Expand Up @@ -772,8 +747,12 @@ print "Savestate Bank Ending at: ", pc
org !SS_DATA
print "Savestate Data Bank Starting at: ", pc
.data
.CS_SAVE_REQ db $00
.CS_LOAD_REQ db $00
; default to nonzero so we get through init
.CS_SAVE_REQ db $01 ; $FC2000
.CS_LOAD_REQ db $01 ; $FC2001
.CS_SAVE_INPUT dw $2010 ; $FC2002
.CS_LOAD_INPUT dw $2020 ; $FC2004

.CS_INPUT_CUR dw $0000
.CS_INPUT_PREV dw $0000
.CS_STATE dw $0000
Expand Down
6 changes: 3 additions & 3 deletions patch/save_state/save_cfg.asm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
; Savestate code variables defaults
!SS_INPUT_SAVE = #$2010 ; select + r
!SS_INPUT_LOAD = #$2020 ; select + l
!SS_INPUT_COMPARE = #$2000 ; select
;!SS_INPUT_SAVE = #$2010 ; select + r
;!SS_INPUT_LOAD = #$2020 ; select + l
;!SS_INPUT_COMPARE = #$2000 ; select

; temporary state machine variable
;!CS_STATE = $002AD4
Expand Down
Binary file modified patch/save_state/save_state_v0.ips
Binary file not shown.
4 changes: 2 additions & 2 deletions src/config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CONFIG_VERSION="0.1.7e-usb-v4"
CONFIG_FWVER=0x80000004
CONFIG_VERSION="0.1.7e-usb-v5"
CONFIG_FWVER=0x80000005
#CONFIG_FWVER=0x44534E53
CONFIG_MCU_FOSC=12000000
16 changes: 10 additions & 6 deletions src/snes.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void get_selected_name(uint8_t* fn) {
uint32_t fdaddr;
char *dot;
cwdaddr = snes_get_mcu_param();
fdaddr = snescmd_readlong(0x08);
fdaddr = snescmd_readlong(0x2A08);
printf("cwd addr=%lx fdaddr=%lx\n", cwdaddr, fdaddr);
uint16_t count = sram_readstrn(fn, cwdaddr, 256);
if(count && fn[count-1] != '/') {
Expand Down Expand Up @@ -407,19 +407,23 @@ void snes_get_filepath(uint8_t *buffer, uint16_t length) {
printf("%s\n", buffer);
}

void snescmd_writeblock(void *buf, uint16_t addr, uint16_t size) {
uint16_t snescmd_writeblock(void *buf, uint16_t addr, uint16_t size) {
fpga_set_snescmd_addr(addr);
while(size--) {
uint16_t count=size;
while(count--) {
fpga_write_snescmd(*(uint8_t*)buf++);
}
return size;
}

void snescmd_readblock(void *buf, uint16_t addr, uint16_t size) {
uint16_t snescmd_readblock(void *buf, uint16_t addr, uint16_t size) {
fpga_set_snescmd_addr(addr);
uint16_t count=size;
uint16_t i = 0;
while(size--) {
while(count--) {
((uint8_t*)buf)[i++] = fpga_read_snescmd();
}
return size;
}

uint64_t snescmd_gettime(void) {
Expand Down Expand Up @@ -455,7 +459,7 @@ void snescmd_prepare_nmihook() {
}

printf("bram count: %d\n", count);
snescmd_writeblock(bram, 0x4, count ? count : sizeof(bram));
snescmd_writeblock(bram, 0x2A04, count ? count : sizeof(bram));
}

void status_load_to_menu() {
Expand Down
4 changes: 2 additions & 2 deletions src/snes.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ void echo_mcu_cmd(void);
uint32_t snes_get_mcu_param(void);
void snescmd_writeshort(uint16_t val, uint16_t addr);
void snescmd_writebyte(uint8_t val, uint16_t addr);
void snescmd_writeblock(void *buf, uint16_t addr, uint16_t size);
uint16_t snescmd_writeblock(void *buf, uint16_t addr, uint16_t size);
uint16_t snescmd_readshort(uint16_t addr);
uint8_t snescmd_readbyte(uint16_t addr);
uint32_t snescmd_readlong(uint16_t addr);
void snescmd_readblock(void *buf, uint16_t addr, uint16_t size);
uint16_t snescmd_readblock(void *buf, uint16_t addr, uint16_t size);
uint64_t snescmd_gettime(void);
void snescmd_prepare_nmihook(void);
void snes_get_filepath(uint8_t *buffer, uint16_t length);
Expand Down
14 changes: 11 additions & 3 deletions src/usbinterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ static inline void __DMB2(void) { asm volatile ("dmb" ::: "memory"); }

#if CONFIG_FWVER == 0x44534E53
#define PRINT_FUNCTION() printf("%-20s ", __FUNCTION__);
#define PRINT_CMD(buf) printf("header=%c%c%c%c op=%s space=%s flags=%d cmd_size=%d block_size=%d size=%d" \
#define PRINT_CMD(buf) printf("header=%c%c%c%c op=%s space=%s flags=%d cmd_size=%d block_size=%d offset=%6x size=%d"\
, buf[0], buf[1], buf[2], buf[3] \
, usbint_server_opcode_s[buf[4]] \
, usbint_server_space_s[buf[5]] \
, (int)buf[6] \
, (int)server_info.cmd_size \
, (int)server_info.block_size \
, (int)server_info.offset \
, (int)server_info.size \
);
#define PRINT_DAT(num, total) printf("%d/%d ", num, total);
Expand Down Expand Up @@ -157,6 +158,7 @@ static const char *usbint_server_opcode_s[] = { FOREACH_SERVER_OPCODE(GENERATE_S
OP(USBINT_SERVER_SPACE_FILE) \
OP(USBINT_SERVER_SPACE_SNES) \
OP(USBINT_SERVER_SPACE_MSU) \
OP(USBINT_SERVER_SPACE_CMD) \
OP(USBINT_SERVER_SPACE_CONFIG)
enum usbint_server_space_e { FOREACH_SERVER_SPACE(GENERATE_ENUM) };
#if CONFIG_FWVER == 0x44534E53
Expand Down Expand Up @@ -327,6 +329,10 @@ void usbint_recv_block(void) {
UINT remainingBytes = min(server_info.block_size - blockBytesWritten, server_info.size - count);
bytesWritten = sram_writeblock(recv_buffer + blockBytesWritten, server_info.offset + count, remainingBytes);
}
else if (server_info.space == USBINT_SERVER_SPACE_CMD) {
UINT remainingBytes = min(server_info.block_size - blockBytesWritten, server_info.size - count);
bytesWritten = snescmd_writeblock(recv_buffer + blockBytesWritten, server_info.offset + count, remainingBytes);
}
else {
uint8_t group = server_info.size & 0xFF;
uint8_t index = server_info.offset & 0xFF;
Expand Down Expand Up @@ -507,8 +513,7 @@ int usbint_handler_cmd(void) {
case USBINT_SERVER_OPCODE_VGET:
case USBINT_SERVER_OPCODE_VPUT: {
// don't support MSU for now
server_info.error = (server_info.space != USBINT_SERVER_SPACE_SNES)
&& (server_info.space != USBINT_SERVER_SPACE_CONFIG);
server_info.error = (server_info.space == USBINT_SERVER_SPACE_FILE);

if (!server_info.error) {
// get total size
Expand Down Expand Up @@ -788,6 +793,9 @@ int usbint_handler_dat(void) {
else if (server_info.space == USBINT_SERVER_SPACE_MSU) {
bytesRead = msu_readblock((uint8_t *)send_buffer[send_buffer_index] + bytesSent, server_info.offset + count, remainingBytes);
}
else if (server_info.space == USBINT_SERVER_SPACE_CMD) {
bytesRead = snescmd_readblock((uint8_t *)send_buffer[send_buffer_index] + bytesSent, server_info.offset + count, remainingBytes);
}
else {
// config
uint8_t group = server_info.size & 0xFF;
Expand Down
5 changes: 4 additions & 1 deletion verilog/sd2snes/address.v
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module address(
output return_vector_enable,
output branch1_enable,
output branch2_enable,
output exe_enable,
input [8:0] bs_page_offset,
input [9:0] bs_page,
input bs_page_enable
Expand Down Expand Up @@ -248,6 +249,7 @@ assign usb_enable = featurebits[FEAT_USB1] & (!SNES_ADDR[22] && ((SNES_ADDR[15:0
assign dma_enable = featurebits[FEAT_DMA1] & (!SNES_ADDR[22] && ((SNES_ADDR[15:0] & 16'hfff0) == 16'h2020));
assign use_bsx = (MAPPER_DEC[3'b011]);
assign srtc_enable = featurebits[FEAT_SRTC] & (!SNES_ADDR[22] && ((SNES_ADDR[15:0] & 16'hfffe) == 16'h2800));
assign exe_enable = (!SNES_ADDR[22] && ((SNES_ADDR[15:0] & 16'hffff) == 16'h2C00)); // requires USB write to enable

// DSP1 LoROM: DR=30-3f:8000-bfff; SR=30-3f:c000-ffff
// or DR=60-6f:0000-3fff; SR=60-6f:4000-7fff
Expand Down Expand Up @@ -281,7 +283,8 @@ assign dspx_a0 = featurebits[FEAT_DSPX]

assign r213f_enable = featurebits[FEAT_213F] & (SNES_PA == 8'h3f);

assign snescmd_enable = ({SNES_ADDR[22], SNES_ADDR[15:9]} == 8'b0_0010101);
// snescmd covers $2A00-$2FFF. This overlaps with at least one hardware cheat device address range.
assign snescmd_enable = ({SNES_ADDR[22], SNES_ADDR[15:11]} == 6'b0_00101) && (SNES_ADDR[10:9] != 2'b00);
assign nmicmd_enable = (SNES_ADDR == 24'h002BF2);
assign return_vector_enable = (SNES_ADDR == 24'h002A5A);
assign branch1_enable = (SNES_ADDR == 24'h002A13);
Expand Down
Loading

0 comments on commit b66e7c2

Please sign in to comment.