Mesen Label Extractor: This should make debugging easier.

jorotroid

Member
gxQoIFp.png


Intro
During the recent drama, I learned about the fact that you can import/export labels in Mesen's Debugger. Rather than looking to swap out ASM6 with ASM6f and risk introducing unknown bugs, I elected take a different route and write a script that scrapes the label data from the file demo.txt. demo.txt is a file that gets created each time you export and contains the entire assembly code of your game along with which address each line of code is located at. I have been using this file to manually look up what each variable and label were as I was debugging, but now you can use this script to automatically fill in (most of) those labels!

Instructions
There are three files in the zip file: TestBat.bat, ExtractLabels.bat and le.ps1. le.ps1 is the actually script, while ExtractLabels.bat and TestBat.bat are two different ways of running this script.
-First extract and place le.ps1 in your GameEngineData folder (where the demo.txt file is located).
-Now you have the option to either put ExtractLabels.bat or TestBat.bat in that folder. Replace the TestBat.bat that us already in the folder with my copy to automatically create the game.mlb file every time you export your game (this will add a few seconds to the export process). Or you can just bring over ExtractLabels.bat and whenever you feel like creating a label file, you can double click on this and it will create the game.mlb file.
-Once you are in Mesen, you can open up the Debugger and go to File>Workspace>Import Labels and import the game.mlb file. Now the code should get populated with the appropriate labels.
-If you want Mesen to automatically load up the game.mlb file, go to File>Workspace and make sure "Auto-load DGB/MLB files" is checked.

Known Limitations
There are some imperfections, however. The script has trouble with 2 types of labels and will skip them:
1. If the label didn't have a ':' at the end of it. There are only a few of these in the code, and I did't want to spend more time to take them into account.
2. If the label was made inside of a Macro.

Finally, one more limitation: the way Mesen sees addresses changes depending on which bank is loaded. So if you are browsing through the code in the debugger, you might run into some addresses that are either not labeled or mislabeled. But if you pause the code at those locations, the appropriate label should appear at that time. That said, this is probably the most likely cause for error on my part, so let me know if you come across a label that is wrong (as long as it's not one of the 3 types mentioned above).

Other Updates
It used to be that any "label+offest" wouldn't be recognized. I came up with sort of a solution to this. The '+' character is not allowed, so instead I used the '@'' character. So where you would see something like "temp16+1" in your code files, that will show up as a "temp16@1" in the debugger.
The original version missed the variables related to sound and maybe more. They are now included.


And that's it. I hope you find this useful. Let me know if you have any questions. Also if anyone can improve upon this, that is welcomed, too.

UPDATE for 4.1
This came up in the Facebook group, so I thought I would share the most up to date version of my label extractor. I haven't been keeping notes on what I changes I have been making to it, but it should be largely the same as previous versions. I don't remember now, but I think that maybe there were some changes between 4.0.11 and 4.1 that made the previous version of the extractor obselete for use with 4.1. For that reason I will keep the old version attached here for anyone still using older versions of NESmaker. One thing that I do know I added to this new version is support for extracting labels from more banks. The addressing of labels based on banks follows a pattern I haven't figured out yet, so I have just been adding support for banks once I start working in a bank only to discover the labels didn't import properly. Here is a list of the banks that I pretty sure should work fine: 14, 16, 17, 18, 1B, 1C, 1D, 1E, 1F

I also currently made it so labels will add a space and the bank number next to them, but label from Bank 1F will get a 1E next to it instead.

UPDATE Jan 14, 2020 - 4.1.1
Just checking to see if what I have in this thread is my most up to date version. Only a slight change: When adding a bank suffix to a label, it not puts an underscore between the label and the suffix instead of a space. This was due to an update with Mesen that seem to drop support for spaces in labels.
 

Attachments

  • ExtractLabels 2.5.zip
    1.5 KB · Views: 175
  • ExtractLabels 4.1.zip
    1.8 KB · Views: 157
  • ExtractLabels 4.1.1.7z
    1.7 KB · Views: 122

jorotroid

Member
I just updated my script and the above post. The main updates are that now the script catches a few more things that it missed before and I also updated the instruction to explain how to get thing to automatically run if that is your preference.

Also not exactly related to my script, but an observation of how code appears in the debugger:
So as you might already know, memory on an nes rom can be described as several addresses of 8-bit numbers. These numbers are interpreted to either be code operations or data values by the debugger and it doesn't always guess right. To be on the safe side, it appears the debugger assumes these memory values are code instead of data most of the time. What this means to you, is that if you were to peruse the code through the debugger, you will come across sections that look like code, but are actually just gibberish because it's not really code, it just data. At this point, I can't see a way of telling Mesen which addresses should be seen as code, and which should be seen as data. You can manually tell it which addresses are what in the debugger, but I can't find a way to save that at the moment.

Other than big issues with the script, I might do one more update. I think it will be possible to bring comments along from the original code, so I'll be looking into that.
 

dale_coop

Moderator
Staff member
Looks nice. Never tried debugging with Mesen. But I will take a look, because sounds very usefull with the labels!
 

jorotroid

Member
dale_coop said:
Looks nice. Never tried debugging with Mesen. But I will take a look, because sounds very usefull with the labels!

I have definitely found the debugger very useful. I'm thinking of maybe making a video tutorial on it once I get all the bugs out of my label extractor. Speaking of which:


So 2.0 still has some bugs that I just noticed. It should still be usable, but it has two problems at the moment. It keeps appending to the mlb file which means the more you run it, the file will just keep getting longer and longer. This won't affect the functionality of the mlb file because Mesen just reads the imports the last label listed for an address. I already fixed it on my computer, but there is something else I want to fix first. There are still some labels missing. I'm getting about 5000 labels when the mlb file is created, but only 3000 are showing up in Mesen. So I need to track down what is going on. One possible reason for a missing label is like I said a moment ago where if two labels have the same address, then Mesen will go with the last one listed. But I doubt that will account for all 2000 missing labels.
 

jorotroid

Member
Ok, just updated the script again to deal with those issues. It turns out that many of those missing labels are a non-issue. Like I have said before, Mesen will only register the last label assigned to a specific address (actually it the last alphabetical label), and as it turns out there are a number of ways multiple labels being assigned to the same address can legitimately happen:
  • Two labels are next to each other because the code between them has been commented out, or there wasn't any code between them for whatever reason.
  • There is a label right before the include of other file, then the first line in that file is also a label.
  • There is a label right before a series of blocks of data.
  • There is no information between labels because that information hasn't been created yet by you in your NESmaker project. So labels associated with things like Text and Monster Objects will point to the same address if you haven't "filled" them up with your own creations.
  • Labels around the chr file imports.
  • Labels for graphical data that have the same address, but in different banks. (I wish, I knew how to distinguish these, but it's probably not going to get in the way of you debugging your game)

Honestly, the only one that might cause some confusion while debugging is that first one, but I am also pretty sure there is nothing I can do about it. As for the rest of them, they probably won't cause you any troubles. There were one or two labels that were legitimately missing, but I got them included now.
 

dale_coop

Moderator
Staff member
I really need to take a look at mesen debugger... currently I just... "rubber duck" my code :p
 

jorotroid

Member
I would really like to make a tutorial on using the Mesen debugger, but gah, not enough time in a day. There is so much useful stuff there. I've even started writing lua scripts for it to help visualize things that are happening in the code.
 
Top Bottom