Donkey Kong Forum

Related Games => Donkey Kong Junior => Topic started by: Donkey Kong Genius on July 15, 2014, 02:41:12 pm

Title: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 15, 2014, 02:41:12 pm
I am making this post because I wanted to compile some information concerning the Junior code. I do not have the code nor do I know how to obtain it. I have windows 8, and MAME .106. What programs, and files will I need to even see the code. If there are others who have links and files, and programs that could assist me in looking at the code, please post it here. I know that some of the code for Junior was borrowed from DK, that that helps a bit. Ultimately, I want to fully understand the bird patterns on the springboard. After looking at Chris' initial post on the DK code, it seems too complicated for me to analyze on my own. Maybe there is a way for me to play with the code and try to narrow down its use. Scott, you were using another program that gave you values as well. Please help me in this regard when you have time.
Title: Re: Donkey Kong Junior Code Analysis
Post by: Monstabonza on July 15, 2014, 03:57:29 pm
Start with the mame debugger Corey,
Research how to use that properly then you can start playing with memory locations.
Title: Re: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 15, 2014, 03:59:30 pm
Excellent! Now. I just downloaded the newest version of MAME. How does one use the debugger? And by memory locations do you mean the specific location in the code that is being referenced?
Title: Re: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 15, 2014, 04:03:07 pm
If it looks anything like this, I have got nothing to look like that yet: http://www.jeffsromhack.com/toolbox/mamedebug.htm (http://www.jeffsromhack.com/toolbox/mamedebug.htm)
Title: Re: Donkey Kong Junior Code Analysis
Post by: JCHarrist on July 15, 2014, 04:34:05 pm
I did a breakdown of the springboard code in this thread: https://donkeykongforum.net/index.php?topic=495.msg9418#msg9418 (https://donkeykongforum.net/index.php?topic=495.msg9418#msg9418)

As far as I know that is the only analysis of the Jr. code that's been done.

There are some memory locations and commented code to get you started at least.

Title: Re: Donkey Kong Junior Code Analysis
Post by: WCopeland on July 15, 2014, 04:41:00 pm
Do you have any experience with programming?

Here is a guide on how to use the MAME debugger: http://www.dorkbotpdx.org/blog/skinny/use_mames_debugger_to_reverse_engineer_and_extend_old_games (http://www.dorkbotpdx.org/blog/skinny/use_mames_debugger_to_reverse_engineer_and_extend_old_games)
It's probably worth a read.
Title: Re: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 15, 2014, 06:21:15 pm
Thanks Jeff, I am sure that info will be helpful in finding the code that relates to the birds on the springboard. Do you have the code or how to get access to it?

Wes, I have no programming experience. I am an idiot when it comes to this stuff. But I do have good logical skills. If there is sufficient info, then I can reason out a conclusion even if it is obscure. I have your link open from earlier to read, your support of the article will come in handy. I have glanced through it but still can find out how to use the information.

Right now I want to learn how to use the debugger for mame such as it is shown in the link I shared: http://www.jeffsromhack.com/toolbox/mamedebug.htm (http://www.jeffsromhack.com/toolbox/mamedebug.htm)

Still have not yet accessed this debugger, not sure how to use it either.

Next I want to be able to get the code for the game. Does anyone have it? If not, how do I get the code from the roms, and how to do this in Z80 assembler/disassembler.
Title: Re: Donkey Kong Junior Code Analysis
Post by: JCHarrist on July 15, 2014, 06:53:04 pm


Next I want to be able to get the code for the game. Does anyone have it? If not, how do I get the code from the roms, and how to do this in Z80 assembler/disassembler.

Here's the complete disassembled code.
Title: Re: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 15, 2014, 06:59:49 pm
Thanks, Jeff!

A lot of the lines say: 6A8B: 00            nop

I thought that if I found the 6A8B that correlates to your locations that I would find the same infor but all I found was this empty line.
Title: Re: Donkey Kong Junior Code Analysis
Post by: JCHarrist on July 15, 2014, 07:11:44 pm
Thanks, Jeff!

A lot of the lines say: 6A8B: 00            nop

I thought that if I found the 6A8B that correlates to your locations that I would find the same infor but all I found was this empty line.

Those memory locations are used as storage for variables, so initially they will have no value. If you monitor those locations with the MAME debugger while stepping through the program , you will see the values changing.

The actual code for the game ends at $5FD9. Everything above that is variable storage.
Title: Re: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 15, 2014, 07:21:56 pm
Great. Hey, how do I use the MAME Debugger? I have the newest version of MAME downloaded. Just wasn't sure how to use that feature.
Title: Re: Donkey Kong Junior Code Analysis
Post by: Monstabonza on July 15, 2014, 07:28:15 pm
mame -window -debug dkjnr


Should run the debugger for you Corey
Swap the dkjnr for whatever roms you are using.
Title: Re: Donkey Kong Junior Code Analysis
Post by: JCHarrist on July 15, 2014, 07:30:49 pm
You need a version of MAME that supports debugging and then from the command line use:

mame dkongjr -window -debug

You are familiar with command line , right? :P
Title: Re: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 15, 2014, 07:32:18 pm
Yup, thanks guys.
Title: Re: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 15, 2014, 11:26:08 pm
mame dkongjr -window -debug does not work in the command prompt

More instructions please. Pretend that I am an idoit, only pretend right, lol
Title: Re: Donkey Kong Junior Code Analysis
Post by: Monstabonza on July 16, 2014, 12:14:57 am
you have to be in the same directory as mame.
first do
cd\
at the command prompt to get to the root directory.
then use
cd <Directory>
to get to the directory mame is in so for me it would be.
cd wolf149
then the command to start it all
mame -window - debug dkongjr

let us know more information on what exactly isn't working

Title: Re: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 16, 2014, 01:36:29 am
Thanks Nick. Got it working, created a batch file so I am always a double click away. Now, a few questions about the debugger.

1) I assume the yellow highlighted area of the code that keeps moving around is the part of the code that is being referenced. So I could in theory just jot down all the highlighted areas, correlate them with something happening on the board, for example a "falling bird" sound occurs every time a bird reaches the falling point on the spring board level. In theory then there should be a piece of the code that is highlighted every time the bird reaches that point.  Correct?

2) Is there a way to have the program compile the code accessed during the board? For example, I do the save state to get the board running and then don't do anything with Junior, just let the level progress but the program would track all the locations on a log so I can observe it and start creating correlations.

Also, Nick, do realize that this will be challenging and take some time but I am also only analyzing one part of a board. I am hoping that being able to observe the birds, correlate some code, and then add junior to a certain vertical height and see if anything in the code executes here and if so what does it do particular. Also, I want to see if there is any particular randomness that exists in the code for what sequences of birds will be released. For example, if I do nothing with Junior, then I see that the birds will run through what appears to be the exact same sequences over and over. If this is so, then I want to know what that is, but I also want to know if and when Junior influences the sequences or randomness. Given that I am only looking at a set number of variables I should be able to create the necessary correlations, and begin to narrow down what the code is doing just to run the board, that is a start. I see that the time bonus ticks every time Mario whips so there should be a common element controlling both. Once I know what these are I will be able to rule them out as potential code areas that controls the birds.  In theory, that is my idea.  I just don't know what I am doing.
Title: Re: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 16, 2014, 11:50:32 am
I had the chance to use the mame debugger today. Here are the areas that were highlighted:

0008: 3A 07 60      ld   a,($6007)

000C: D0            ret  nc


02BD: 26 60         ld   h,$60
02BF: 3A B1 60      ld   a,($60B1)
02C2: 6F            ld   l,a
02C3: 7E            ld   a,(hl)
02C4: 87            add  a,a
02C5: 30 1C         jr   nc,$02E3
02C7: CD 15 03      call $0315
02CA: CD 50 03      call $0350
02CD: 21 19 60      ld   hl,$6019
02D0: 34            inc  (hl)
02D1: 21 83 63      ld   hl,$6383
02D4: 3A 1A 60      ld   a,($601A)
02D7: BE            cp   (hl)
02D8: 28 E3         jr   z,$02BD


0318: 47            ld   b,a
0319: E6 0F         and  $0F
031B: C0            ret  nz
031C: CF            rst  $08
031D: 3A 0D 60      ld   a,($600D)
0320: CD 47 03      call $0347

0328: 28 14         jr   z,$033E


0343: 19            add  hl,de

034B: C8            ret  z

0350: 3A 2D 62      ld   a,($622D)
0353: A7            and  a
0354: C0            ret  nz

I basically allowed the level to run by itself with no inputs. These were the active locations within the code. 02D0 may have something to do with the egg about to drop but I am not certain. It was not as easy to find correlations as I thought.

Some interesting observations:

1) The debugger showed 0315 location consistently, and it is in the code at one location as $0315 but there is no such line in the code at all.
2) 02C5 did not execute until 600 on the bonus timer, 034B did not execute until 500 on the bonus timer, and the only part of the code that was referenced during around the time of death was 033E. There was no observable code references unique to restarting the level or through the death sequence other than these few areas.

I do not think I can progress further without more information. What is "add", "call" "what does it mean to have an $ before a number on the right side of the code", "jr", "Id", etc?

Any one want to chime in and help me take this a little further?

This list of code references is accurate as far as I know unless there is strange code that was executed in rare places through out the board other than the end. I went through a large sampling of the board but I do not want to assume automatically that I have compiled it perfectly. With that said, this list is a good representation of what is going on in the code during the Springboard level. I am more interested in narrowing in on the pattern of the birds.
Title: Re: Donkey Kong Junior Code Analysis
Post by: JCHarrist on July 16, 2014, 02:13:54 pm
The code you have listed there is for credit handling. The game is constantly monitoring the coin switch to see if credits have been added.

There are literally thousands of lines of code executing every frame and 60 frames every second. If you are running the game full speed , the yellow highlight line isn't really going to mean much. You will just tend to see it at the routines that run the most , like the credit handler.

You need to learn to use watchpoints and breakpoints and step through the program to examine specific memory locations to see how they correlate to actions that are happening in the game.

The $ means the number is hexadecimal. If it's in parentheses, it is referencing the value at that address. If it's not in parentheses , then it means the literal value.

add, call, jr, ld are all assembly language instructions. See the attached Z80 instruction set for more info.
Title: Re: Donkey Kong Junior Code Analysis
Post by: WCopeland on July 16, 2014, 03:24:40 pm
Corey I mean no offense but I really feel like it might be worth your while to learn some basic programming concepts. What you are trying to do here with the Jr. code is so difficult it might even qualify as a university-level capstone project.  There is a reason I started the DKCore project in the first place --

I would highly recommend taking a short free course on a modern language (link provided below). Though modern code will look nothing like ASM, the concepts are extremely applicable. You can also learn some basic debugging principles, which you absolutely will need for what you are trying to do. My personal opinion is everyone should have a little coding experience, and a lot of places tend to agree (http://www.coca-colacompany.com/stories/program-or-perish-why-everyone-should-learn-to-code).

JavaScript is the most widely-used modern programming language in the world, and it's a great place to start. Here's a wonderful interactive course that will teach you everything you need to know in a reasonable amount of time: http://www.codecademy.com/en/tracks/javascript (http://www.codecademy.com/en/tracks/javascript)

Once you have a handle on basic programming concepts you can start diving into assembly. Many non-programmers do not actually realize "assembly" is a blanket term referring to hundreds of programming languages. Each different CPU architecture that exists has its own assembly language. Donkey Kong Jr uses a Z80 processor, so you will be want to learn the basics of Z80 assembly. You can find a guide here: http://sgate.emt.bme.hu/patai/publications/z80guide/ (http://sgate.emt.bme.hu/patai/publications/z80guide/)

You will be tempted to look at the above guide before taking a look at the JavaScript course (it's what I would do). Your life would be much easier if you did the JS course first, as you would have concepts/ideas you could relate the much more complex assembly language back to.

I truly believe if you don't do something along the lines of the two things I listed above you're going to only end up frustrated.
Title: Re: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 16, 2014, 03:29:13 pm
Jeff, just as you were posting I was completing this post, which may not mean anything anymore. I will follow up with your post in a few. This is the same code that I had shown before but wanted to add the references to it so I can see it all together. So, congrats Corey! You succussfully compiled code concerning credit monitoring. Time well spent, I will be putting the plaque on my wall this evening. What the HECK!

0008: 3A 07 60      ld   a,($6007)
                                          6007: 00            nop

000C: D0            ret  nc

02BD: 26 60         ld   h,$60
                                     $60 is unknown
02BF: 3A B1 60      ld   a,($60B1)
                                          60B1: 00            nop
02C2: 6F            ld   l,a
02C3: 7E            ld   a,(hl)
02C4: 87            add  a,a
02C5: 30 1C         jr   nc,$02E3
                                        02E3: E6 1F         and  $1F
                                                                             001F: C9            ret
02C7: CD 15 03      call $0315
                                     $0315 is unknown
02CA: CD 50 03      call $0350
                                       0350: 3A 2D 62      ld   a,($622D)
                                                                                  622D: 00            nop
02CD: 21 19 60      ld   hl,$6019
                                          6019: 00            nop
02D0: 34            inc  (hl)
02D1: 21 83 63      ld   hl,$6383
                                          6383: 00            nop
02D4: 3A 1A 60      ld   a,($601A)
                                           601A: 00            nop
02D7: BE            cp   (hl)
02D8: 28 E3         jr   z,$02BD
                                       02BD: 26 60         ld   h,$60
                                                                            $60 is unknown
0318: 47            ld   b,a
0319: E6 0F         and  $0F
                                    000F: C9            ret
031B: C0            ret  nz
031C: CF            rst  $08
                                 0008: 3A 07 60      ld   a,($6007)
                                                                           6007: 00            nop
031D: 3A 0D 60      ld   a,($600D)
                                          600D: 00            nop
0320: CD 47 03      call $0347
                                      0347: 21 40 77      ld   hl,$7740
                                                                               7740: 00            nop
0328: 28 14         jr   z,$033E
                                     033E: 3C            inc  a

0343: 19            add  hl,de

034B: C8            ret  z

0350: 3A 2D 62      ld   a,($622D)
                                          622D: 00            nop
0353: A7            and  a
0354: C0            ret  nz
Title: Re: Donkey Kong Junior Code Analysis
Post by: Donkey Kong Genius on July 16, 2014, 03:45:54 pm
Jeff thanks for your ideas. My mame debugger only has a set breakpoint at cursor option. I don't see any way to run it frame by frame to get a closer look at what is going on. What version do you use? I am using the newest debug version.

"You need to learn to use watchpoints and breakpoints and step through the program to examine specific memory locations to see how they correlate to actions that are happening in the game."
How do I begin to learn this?

Wes, if I have to learn all that just to be able to understand how the birds pattern themselves on the springboard then it may not be worth it to me. I could just observe it all and write it all out. But I thought it would be easier just to look at the code. Thanks for the suggestions.
Title: Re: Donkey Kong Junior Code Analysis
Post by: Monstabonza on July 16, 2014, 04:25:57 pm
Wes, is there anyway to Use visual studio to debug mame games,
I'm sure it must be possible but I'm not exactly sure how to do it.

Corey don't you worry about this just yet, it won't really help you at the moment.
Title: Re: Donkey Kong Junior Code Analysis
Post by: WCopeland on July 17, 2014, 06:43:51 am
Wes, is there anyway to Use visual studio to debug mame games,
I'm sure it must be possible but I'm not exactly sure how to do it.

Probably not ... as far as I know, the VS debugger is only built to debug programs built with native C++ and/or the .NET framework.
Title: Re: Donkey Kong Junior Code Analysis
Post by: xelnia on July 17, 2014, 07:16:00 am
I would recommend checking out this post (https://donkeykongforum.net/index.php?topic=383.msg12975#msg12975) by Jeff Willms. I've heard it mentioned before that DKJR and DK share a lot of the same code, so any commented DK code would be a useful tool for you. Every line of DKJR code you posted above can be found in DK, verbatim. Obviously there are major differences between the two games, but certain things like timers and enemy behavior might be easier to pick out. At the very least you might be able to use a process of elimination to help save you some time.