Skeetendo

’Cause all games were better on the GBC

You are not logged in.

#1 2016-08-01 20:09:18

pepitopon
Member
Registered: 2016-07-25
Post 17/38

Help a newbie!

Hi everyone! I'm new in this chat.

I was trying to make my own version of pokered. But in this weeks im really confused about scripts and events because i dont know the way of Z80 operate.

I need you help.

I must understand what is this example:

ld a,[wd747]
bit 0,a
ret nz
ld a,[W_YCOORD]
cp 1

I dont know what means "ld a", i know nothing.

So... Where can i learn that code and the way of pokered works? I dont care spend hours reading and practicing to really understand this.

My first challenge: make an event like the prof Oak in the beginning when Ash goes north.

Please tell me where can i find knowledge!

Offline

#2 2016-08-01 21:18:08

Rangi
Member
Registered: 2016-05-09
Post 59/823

Re: Help a newbie!

So, the GameBoy has RAM (memory) and registers. The RAM contains all of the game data, but you can't work with it directly. You have to load data from RAM into a register, process the data, and store any changes back in RAM.

I've been referring to this guide to the opcodes. It's pretty dense, though, and really only makes sense if you already have a mental model of how the GameBoy works.

I'll give you some examples.

ld a, [wd747]

Here "a" is a register and "wd747" is the name of a location in RAM (defined in wram.asm). "[wd747]" dereferences the name and gets the actual value stored at that location. The "ld" opcode takes two arguments, and stores the second argument in the location of the first. So "ld a, [wd747]" reads the value at wd747 in RAM and stores it in register a.

bit 0, a

Registers are 8-bit values, meaning they store a number from 0 to 255. (Bits are binary digits; find a good tutorial for binary and hexadecimal numbers if you don't know them yet.) Here "bit" is checking whether the 0th bit of a is set (1) or not set (0), and it sets the "zero flag" Z accordingly.

ret nz

A "ret" opcode by itself returns from a function call. You may see some code like this:

MainFunction:
    ...
    call Subroutine
    ...

Subroutine:
    ...
    ret

If MainFunction is executing one line at a time, when it gets to the line "call Subroutine" it jumps to the start of Subroutine, and "ret" jumps back to MainFunction and continues executing the rest of the lines. (The GameBoy keeps track of where to jump back to with a stack. There's a special "program counter" register, PC, that keeps on incrementing as you go line by line. If you call function A which calls function B which calls function C, the stack now has three values on it, recording where the PC was when it jumped to a called function. "ret" takes the top value off the stack and sets PC back to it.)

"ret nz" is a convenient way to return only if the Z flag is not set. ("ret z" would return only if it is set.) Keep in mind that the previous instruction, "bit 0, a", set or unset the z flag depending on whether the 0th bit of a was a 0 or a 1.

So taken together, these three lines of a function:

ld a, [wd747]
bit 0, a
ret nz

return from that function if the 0th bit of wd747 is not set.

Once you understand Z80 assembly language, you'll have a much easier time figuring out all of the Pokémon-specific things in the source code, like predefined functions related to battles and trainers and all that. If you can find a good tutorial for any assembly language—x86, MIPS, ARM, whatever—most of the concepts are the same for Z80. You still have a large amount of RAM, a small set of registers, a program counter, a stack. You could probably even pick up these concepts by learning C, a higher-level language that you could learn on a desktop computer while still paying attention to hardware details.

Last edited by Rangi (2016-08-01 21:19:46)


ROM hack: Pokémon Polished Crystal (GitHub) — version 2.2.0 released!

Offline

#3 2016-08-03 17:50:45

pepitopon
Member
Registered: 2016-07-25
Post 18/38

Re: Help a newbie!

Rangi wrote:

So, the GameBoy has RAM (memory) and registers. The RAM contains all of the game data, but you can't work with it directly. You have to load data from RAM into a register, process the data, and store any changes back in RAM.

I've been referring to this guide to the opcodes. It's pretty dense, though, and really only makes sense if you already have a mental model of how the GameBoy works.

I'll give you some examples.

ld a, [wd747]

Here "a" is a register and "wd747" is the name of a location in RAM (defined in wram.asm). "[wd747]" dereferences the name and gets the actual value stored at that location. The "ld" opcode takes two arguments, and stores the second argument in the location of the first. So "ld a, [wd747]" reads the value at wd747 in RAM and stores it in register a.

bit 0, a

Registers are 8-bit values, meaning they store a number from 0 to 255. (Bits are binary digits; find a good tutorial for binary and hexadecimal numbers if you don't know them yet.) Here "bit" is checking whether the 0th bit of a is set (1) or not set (0), and it sets the "zero flag" Z accordingly.

ret nz

A "ret" opcode by itself returns from a function call. You may see some code like this:

MainFunction:
    ...
    call Subroutine
    ...

Subroutine:
    ...
    ret

If MainFunction is executing one line at a time, when it gets to the line "call Subroutine" it jumps to the start of Subroutine, and "ret" jumps back to MainFunction and continues executing the rest of the lines. (The GameBoy keeps track of where to jump back to with a stack. There's a special "program counter" register, PC, that keeps on incrementing as you go line by line. If you call function A which calls function B which calls function C, the stack now has three values on it, recording where the PC was when it jumped to a called function. "ret" takes the top value off the stack and sets PC back to it.)

"ret nz" is a convenient way to return only if the Z flag is not set. ("ret z" would return only if it is set.) Keep in mind that the previous instruction, "bit 0, a", set or unset the z flag depending on whether the 0th bit of a was a 0 or a 1.

So taken together, these three lines of a function:

ld a, [wd747]
bit 0, a
ret nz

return from that function if the 0th bit of wd747 is not set.

Once you understand Z80 assembly language, you'll have a much easier time figuring out all of the Pokémon-specific things in the source code, like predefined functions related to battles and trainers and all that. If you can find a good tutorial for any assembly language—x86, MIPS, ARM, whatever—most of the concepts are the same for Z80. You still have a large amount of RAM, a small set of registers, a program counter, a stack. You could probably even pick up these concepts by learning C, a higher-level language that you could learn on a desktop computer while still paying attention to hardware details.

This is PERFECT! I learned all these concepts just reading this, but i still dont know what these orders mean for a human. Ok, execute routine, flags, registers... For me, these orders are empty because didnt say "if ash goes north Pallettown, Oak appears and guide him to Oak's lab".

Of course, thank you for helping me!

Last edited by pepitopon (2016-08-03 17:51:14)

Offline

Board footer

Powered by FluxBB