øAslickproductions.org/forum/index.php?PHPSESSID=5f0fck550j2m4m2fpbtkj2vkm1&topic=2293.msg26341e:/My Web Sites/Slick Productions - FFIV Message Board/slickproductions.org/forum/indexf97f-2.htmllayedslickproductions.org/forum/index.php?PHPSESSID=5f0fck550j2m4m2fpbtkj2vkm1&action=profile;area=showposts;sa=attach;u=262e:/My Web Sites/Slick Productions - FFIV Message Board/slickproductions.org/forum/indexf97f-2.html.zx–h^ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÈ ,/ü/OKtext/htmlISO-8859-1gzip@øÕü/ÿÿÿÿÿÿÿÿÑHTue, 10 Mar 2020 22:45:01 GMT0ó°° ®0®P®€§²ð®•h^ÿÿÿÿÿÿÿÿ>*ü/ Possible FF6 event bug on game reloading

Author Topic: Possible FF6 event bug on game reloading  (Read 2734 times)

Madsiur

  • Tunnel Armor
  • *
  • Posts: 149
  • Gender: Male
  • FF6AE coder
    • View Profile
    • Madsiur's Lair
Possible FF6 event bug on game reloading
« on: October 08, 2016, 10:40:36 AM »
The game can freeze at wounder returner scene on emulator reload (a game reload) if you replay the Terra / Banon scene in Returners Hideout and choose 3 time "No". It seems objects $01, $04 and $05 last positions on that map is not changed (or cleared). Here is a video showing the freeze: https://youtu.be/BRpmoZQ4YVU?t=3m25s

Could that be related to $1FD3-$1FF2 that are not reset to 00 00 for characters 01, 04 and 05? Eventwise their objects are not deleted (only hidden) after the scene (except Locke). I proposed as workaround to move the call to CB/0072 further down the event right before object $01, $04 and $05 position are set (long description: http://www.insanedifficulty.com/board/index.php?/topic/8008-softlock-tell-astral-no-3-times-and-cutscene-breaks/page__p__143004#entry143004).

Was this freeze or possible bug something known?

Lenophis

  • Forum Overlord
  • *
  • Posts: 1,688
  • Gender: Male
  • I sad
    • View Profile
    • Slick Productions
Re: Possible FF6 event bug on game reloading
« Reply #1 on: October 09, 2016, 10:45:57 AM »
I am genuinely surprised FF6 isn't clearing this ram on reset. Not clearing event ram can lead to all kinds of problems. Granted, this is a very specific edge-case, but at the same time Square should've taken some kind of general precaution.

119 bugs fixed and counting.

Madsiur

  • Tunnel Armor
  • *
  • Posts: 149
  • Gender: Male
  • FF6AE coder
    • View Profile
    • Madsiur's Lair
Re: Possible FF6 event bug on game reloading
« Reply #2 on: October 09, 2016, 12:04:22 PM »
Since it's part of the save RAM I was thinking maybe there is at least one case in the game where you load a game and character objects are already present on map thus requiring their position. Can't think of any example though. 

:edit: (5:00PM): Or did you meant clearing $0869-$086E for objects $00-$0F on game reset? I'm not sure when the game update into $1FD3-$1FF2 but if at game load $1FD3-$1FF2 is used to fill $0869-$086E the problem would maybe be there. I'll try to get a better grasp on all this with a debugger. An ASM solution would be more elegant than an event edit.


 :edit: (2:00PM): I came out with an event fix requiring 21 bytes in free space and modifying 12 bytes in original event. Basically I call a variance of CB/0072 at the same spot that only created objects. Then I use object $04 queue further down to call the second subroutine that show object $04 in-between its two queues. I separated its queue into two to respect the original event flow  (command E0 when object is already shown). Then I show objects $01 and $05. No FD command(s) used as filler!

Code: [Select]
CA/FDE4: B2    Call subroutine $D1F9D0

CA/FE62: B2    Call subroutine $D1F9D8
CA/FE66: 41    Show object $05
CA/FE68: 41    Show object $01

D1/F9D0: 3D    Create object $04
D1/F9D2: 3D    Create object $05
D1/F9D4: 3D    Create object $01
D1/F9D6: 45    Refresh objects
D1/F9D7: FE    Return

D1/F9D8: 04    Begin action queue for character $04 (Actor in stot 4), 4 bytes long
D1/F9DA: D5        Set vehicle/entity's position to (20, 28)
D1/F9DD: FF        End queue
D1/F9DE: 41    Show object $04
D1/F9E0: 04    Begin action queue for character $04 (Actor in stot 4), 3 bytes long (wait until completed)
D1/F9E2: E0        Pause for 4 * 1 (4) frames
D1/F9E4: FF        End queue
D1/F9E5: FE    Return
« Last Edit: October 16, 2016, 04:38:54 PM by Madsiur »

Lenophis

  • Forum Overlord
  • *
  • Posts: 1,688
  • Gender: Male
  • I sad
    • View Profile
    • Slick Productions
Re: Possible FF6 event bug on game reloading
« Reply #3 on: October 09, 2016, 06:40:13 PM »
:edit: (5:00PM): Or did you meant clearing $0869-$086E for objects $00-$0F on game reset?
All of it. I wonder if a routine already exists to clear the necessary ram. For some reason, a soft-reset isn't clearing everything that the game needs to.

119 bugs fixed and counting.

Madsiur

  • Tunnel Armor
  • *
  • Posts: 149
  • Gender: Male
  • FF6AE coder
    • View Profile
    • Madsiur's Lair
Re: Possible FF6 event bug on game reloading
« Reply #4 on: October 14, 2016, 04:48:34 PM »
Well I did a few tests and the save file is loaded correctly in RAM. However, $1FD3-$1FF2 get overwritten before map load by what was left in $086A. I am unsure which call to C0/6E88 is the culprit but I re-initialized the objects data ($0867-$1068) right before loading a save file and the bug is gone. The fix takes 18 bytes at end of bank $C3. Doing an init on objects data before save load shouldn't affect anything (I think) since all that is blank when starting the game (without resetting).

Code: [Select]
org $C31572
JSR fix


org $C3FFEE
fix:
TAX
SEP #$20       
LDY $00
TDC
branch:
STA $0867,Y 
INY
CPY #$0801 ;$0867-$1068
BNE branch
LDY $00 
RTS

 :edit: Patch removed
« Last Edit: October 16, 2016, 09:32:44 AM by Madsiur »

13375K31C43R

  • Ultros
  • *
  • Posts: 675
  • Gender: Male
    • View Profile
    • Leet Sketcher's Final Fantasy III Patches
Re: Possible FF6 event bug on game reloading
« Reply #5 on: October 15, 2016, 05:10:38 AM »
Not bad! Unfortunately, this ASM fix overlaps with my "No X in Fight" patch.
"Do not meddle in the affairs of wizards, for they are subtle and quick to anger." -Gildor from The Lord of the Rings

Madsiur

  • Tunnel Armor
  • *
  • Posts: 149
  • Gender: Male
  • FF6AE coder
    • View Profile
    • Madsiur's Lair
Re: Possible FF6 event bug on game reloading
« Reply #6 on: October 15, 2016, 09:01:03 AM »
Not bad! Unfortunately, this ASM fix overlaps with my "No X in Fight" patch.

Is there any space left in bank $C3 for fixes? I could try moving it to bank $C0 if there is more room there.

Lenophis

  • Forum Overlord
  • *
  • Posts: 1,688
  • Gender: Male
  • I sad
    • View Profile
    • Slick Productions
Re: Possible FF6 event bug on game reloading
« Reply #7 on: October 15, 2016, 11:41:25 AM »
C0 has plenty of room. A big block of free space starts at C0/D613 and ends at C0/DF9F. I do have a couple of patches towards the beginning of that space, but they are patches that need serious re-working.

As for your fix, how about something like this?
Code: [Select]
org $C31566
XBA
LDA $00
XBA
ASL A
REP #$20
LDX #$07FE  ; start from the end of the buffer, and work our way down
branch:
STZ $0867,X
DEX
DEX  ; since we're doing 2 bytes at a time, this will use fewer cycles, and less space
BPL branch
TAX  ; back to our original code to load from SRAM to working SRAM
LDA $C3155E,X
TAX
SEP #$20
LDY $00
branch2:
LDA $306000,X
STA $1600,Y
INX
INY
CPY #$0A00
BNE branch2
RTS
That would use only 11 bytes of free space. I'm not entirely sure I clear the last byte of the buffer, though.
« Last Edit: October 15, 2016, 12:12:27 PM by Lenophis »

119 bugs fixed and counting.

13375K31C43R

  • Ultros
  • *
  • Posts: 675
  • Gender: Male
    • View Profile
    • Leet Sketcher's Final Fantasy III Patches
Re: Possible FF6 event bug on game reloading
« Reply #8 on: October 15, 2016, 05:07:18 PM »
I have patches that take up C3/FFBB-C3/FFFF. There should be plenty of space before that block.
"Do not meddle in the affairs of wizards, for they are subtle and quick to anger." -Gildor from The Lord of the Rings

13375K31C43R

  • Ultros
  • *
  • Posts: 675
  • Gender: Male
    • View Profile
    • Leet Sketcher's Final Fantasy III Patches
Re: Possible FF6 event bug on game reloading
« Reply #9 on: October 16, 2016, 04:42:10 AM »
By the way, the data-clearing function you devised also gets called when trying to save the game. I just tested this, in the Returners' Hideout, at the point where Terra is wandering around before telling Banon her decision. I went to the Save Point in Banon's room, saved the game, and then when I exited the Main Menu, Terra had turned green and both Edgar and the Save Point had disappeared.
"Do not meddle in the affairs of wizards, for they are subtle and quick to anger." -Gildor from The Lord of the Rings

Madsiur

  • Tunnel Armor
  • *
  • Posts: 149
  • Gender: Male
  • FF6AE coder
    • View Profile
    • Madsiur's Lair
Re: Possible FF6 event bug on game reloading
« Reply #10 on: October 16, 2016, 09:31:50 AM »
By the way, the data-clearing function you devised also gets called when trying to save the game.

Yes, $C31566 is called from multiple points.  :blush:

Hooking the fix on the $C329C2 subroutine which is not called on game save seems to solve the problem. However I have an efficiency question... At which point should one sacrifice free space in a limited free space bank to save cycles? The 16-bit version of the modified code takes 18 bytes of free space instead of optimal 14 bytes (because A is 8-bit where I hook my code) and the 8-bit version would take 13 bytes. However the 16-bit loop save 8750 cycles compared to its 8-bit version. I'm unsure what to do...

Code: [Select]
; Sustain Load menu subroutine
org $C329EB
JSR fix

org $C3FFA9     ; code ends at $C3FFBA
fix:
REP #$20
LDX #$0801      ; covers $0867 to $1068
branch:
STZ $0867,X
DEX
DEX
BPL branch
SEP #$20
JSR $1566       ; previously at $C329EB
RTS

13375K31C43R

  • Ultros
  • *
  • Posts: 675
  • Gender: Male
    • View Profile
    • Leet Sketcher's Final Fantasy III Patches
Re: Possible FF6 event bug on game reloading
« Reply #11 on: October 16, 2016, 12:40:37 PM »
However I have an efficiency question... At which point should one sacrifice free space in a limited free space bank to save cycles? The 16-bit version of the modified code takes 18 bytes of free space instead of optimal 14 bytes (because A is 8-bit where I hook my code) and the 8-bit version would take 13 bytes. However the 16-bit loop save 8750 cycles compared to its 8-bit version. I'm unsure what to do...

Personally, saving 8750 cycles is worth an extra four bytes, especially considering how much free space there still is in bank C3.

Also, a couple of things. First, if you change the JSR at the end of your data-clearing function to a JMP, you can remove the RTS. Second, exactly what RAM needs to be cleared? I'm seeing different versions of this all over the place. It seems $0867 needs to be cleared, $1068 needs to be cleared, and everything in between needs to be cleared. Is this correct? If that's the case, you should set X to start at #$0800 because otherwise you're also clearing $1069 and neglecting $0867.
"Do not meddle in the affairs of wizards, for they are subtle and quick to anger." -Gildor from The Lord of the Rings

Madsiur

  • Tunnel Armor
  • *
  • Posts: 149
  • Gender: Male
  • FF6AE coder
    • View Profile
    • Madsiur's Lair
Re: Possible FF6 event bug on game reloading
« Reply #12 on: October 16, 2016, 02:19:33 PM »
Also, a couple of things. First, if you change the JSR at the end of your data-clearing function to a JMP, you can remove the RTS. Second, exactly what RAM needs to be cleared? I'm seeing different versions of this all over the place. It seems $0867 needs to be cleared, $1068 needs to be cleared, and everything in between needs to be cleared. Is this correct? If that's the case, you should set X to start at #$0800 because otherwise you're also clearing $1069 and neglecting $0867.

Thanks for the optimization tip!

In fact, for this specific bug, $0869,X to $086E,X for objects $00-$2F should be cleared but might as well clear all objects data ($0867-$1069). The specific approach would make the code longer and I think it's logic to reset everything that should be reset. So yes setting X to #$0800 is right, I somewhat calculated and came up with #$0801. It was a mistake.

 :edit: Patch removed!
« Last Edit: January 23, 2017, 09:13:48 PM by Madsiur »

C. V. Reynolds

  • Tunnel Armor
  • *
  • Posts: 153
  • RATED TEM OUTTA TEM.
    • View Profile
    • cvreynolds.net
Re: Possible FF6 event bug on game reloading
« Reply #13 on: October 16, 2016, 04:02:35 PM »
Hoi. Congratulations on the discovery and fix, Madsiur. :3

I'm interesting in using this fix. I see two patches here, though. What is the difference between Free Moving Terra and Frozen Terra Fix? Do they fix the same bug in different ways? Does each fix different aspects of the bug? Is one an obsolete mule that should be deleted?
I'm the former Hollywood Narrator from GameFAQs.

I'm a published novelist. My novels "Universe Eleven Series - Blue Ruin" Part 1, Part 2, and Part 3 are for sale on Amazon (check my website) in print and ebook formats.

Madsiur

  • Tunnel Armor
  • *
  • Posts: 149
  • Gender: Male
  • FF6AE coder
    • View Profile
    • Madsiur's Lair
Re: Possible FF6 event bug on game reloading
« Reply #14 on: October 16, 2016, 04:44:52 PM »
I'm interesting in using this fix. I see two patches here, though. What is the difference between Free Moving Terra and Frozen Terra Fix?

You should use Frozen Terra Fix. The first patch (Free Moving Terra Fix) was an event modification that fixed the bug instance but did not addressed the root cause of the problem. Frozen Terra Fix address the root cause and it is an ASM fix. I made sure with LeetSketcher that it doesn't conflict with one of his fix and it probably doesn't with other bank $C3 fixes though I'm not 100% sure.

I removed the attachment with the first patch to clarify the question.