Author Topic: Donkey Kong Junior Code Analysis  (Read 16935 times)

0 Members and 2 Guests are viewing this topic.

Offline Monstabonza

  • Senior Member
  • *
  • Posts: 237
    • Awards
Re: Donkey Kong Junior Code Analysis
« Reply #15 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

Dk PB 391800
Dk jr PB 42000
CK 481400 lvl 19-1
CK On CC 567100 KS

Twitch =
Member for 11 Years IGBY 2014 DKF Team Member CK Killscreener Blogger Twitch Streamer

Donkey Kong Genius

  • Guest
Re: Donkey Kong Junior Code Analysis
« Reply #16 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.
« Last Edit: July 16, 2014, 10:05:38 am by Donkey Kong Genius »

Donkey Kong Genius

  • Guest
Re: Donkey Kong Junior Code Analysis
« Reply #17 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.
« Last Edit: July 16, 2014, 11:56:40 am by Donkey Kong Genius »

Offline JCHarrist

  • Spring Jumper
  • *
  • Posts: 643
    • Donkey Kong Forum
    • Awards
Re: Donkey Kong Junior Code Analysis
« Reply #18 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.
Member for 11 Years DKF Founder Former CK World Record Holder - Arcade CK Killscreener DK Killscreener

WCopeland

  • Guest
Re: Donkey Kong Junior Code Analysis
« Reply #19 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.

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

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/

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.

Donkey Kong Genius

  • Guest
Re: Donkey Kong Junior Code Analysis
« Reply #20 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

Donkey Kong Genius

  • Guest
Re: Donkey Kong Junior Code Analysis
« Reply #21 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.

Offline Monstabonza

  • Senior Member
  • *
  • Posts: 237
    • Awards
Re: Donkey Kong Junior Code Analysis
« Reply #22 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.
Dk PB 391800
Dk jr PB 42000
CK 481400 lvl 19-1
CK On CC 567100 KS

Twitch =
Member for 11 Years IGBY 2014 DKF Team Member CK Killscreener Blogger Twitch Streamer

WCopeland

  • Guest
Re: Donkey Kong Junior Code Analysis
« Reply #23 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.

Offline xelnia

  • Administrator
  • Spring Jumper
  • *
  • Posts: 2815
  • Stop using 0.106
    • Twitch
    • Awards
Re: Donkey Kong Junior Code Analysis
« Reply #24 on: July 17, 2014, 07:16:00 am »
I would recommend checking out this post 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.
"Do not criticize, question, suggest or opine anything about an upcoming CAG event, no matter how constructive or positive your intent may be. You will find nothing but pain and frustration, trust me. Just go, or don't go, and :-X either way!" -ChrisP, 3/29/15
Member for 11 Years snek CK Killscreener IGBY 2016 DKF Team Member IGBY 2015 DKF Team Member IGBY 2014 DKF Team Member DK Killscreener Blogger Twitch Streamer