What Game Mechanics would you like to know?

Any information you would like to share or ask in regards to the hacking of Legend of Legaia
User avatar
Nightshade
Level 60
Posts: 1797
Joined: Tue Jul 26, 2011 9:07 pm
Location: the the the the

Re: What Game Mechanics would you like to know?

Post by Nightshade » Mon Dec 05, 2016 10:37 pm

meth962 wrote:
Prokion way:
(((agility << 3 - agility) * 0x66666667) >> 33) + 8
which if we get rid of compiler functions like shifting and the hex numbers, it would be...
(((agility * 8 - agility) * 1717986919) / 8589934592) + 8

Reduces to:
AGL * 1.4 + 8
That was a lucky guess on my part.

I wasn't sure what the arrows meant, so it was difficult to understand.

I'm guessing that they are the left bit-shift and right bit-shift; i.e.

x << n is shorthand for x * 2^n

and

x >> n is shorthand for x / (2^n) then?
Do not question yourself with the why or the how. I simply am, and that is all you need to know.

zkshd
Level 5
Posts: 10
Joined: Sun Nov 20, 2016 2:22 pm

Re: What Game Mechanics would you like to know?

Post by zkshd » Tue Dec 06, 2016 6:31 am

Nightshade wrote: I'm guessing that they are the left bit-shift and right bit-shift; i.e.
x << n is shorthand for x * 2^n
and
x >> n is shorthand for x / (2^n) then?
Yes. That's cuz << or >> are number shifts, so when you do a shift in base 2 (binary), you multiple or divide by 2 for each shift (left or right), like:
0111b (7d) << 1 --> 1110b (14d)
0111b (7d) >> 1 --> 0011b (3d, yes it is rounded down)

However if you do a shift in base 8 (octal), then it will by 8, in base 10 (decimal) by 10 and in base 16 (hexadecimal) by 16, example:
077o (63d) << 1 --> 770o (504d = 63 by 8)
099d (99d) << 1 --> 990d (990d = 99 by 10)
0FFh (255d) << 1 --> FF0h (4080d = 255 by 16)

Since machines are mostly hardware binary based, you won't see a shift (left or right) in other base than 2.
Last edited by zkshd on Tue Dec 06, 2016 3:21 pm, edited 1 time in total.

User avatar
meth962
Level 23
Posts: 266
Joined: Tue Apr 03, 2012 8:37 am
Location: USA
Contact:

Re: What Game Mechanics would you like to know?

Post by meth962 » Tue Dec 06, 2016 11:51 am

I should try to give some saving grace to Prokion. I'm assuming they developed the game in C or C++ or other such language that interprets their code and makes it more efficient by bit shifting instead of multiplication. However, when I see something like this:

AGL * 8 - AGL

I'm pretty sure that's not the compiler and just a programming inefficiency for not doing AGL * 7 haha. That is NOT faster in assembly language so yeah, still funny stuff to find in the assembly code of the game :)
まさかつあがつ

User avatar
Nightshade
Level 60
Posts: 1797
Joined: Tue Jul 26, 2011 9:07 pm
Location: the the the the

Re: What Game Mechanics would you like to know?

Post by Nightshade » Tue Dec 06, 2016 2:44 pm

meth962 wrote:I should try to give some saving grace to Prokion. I'm assuming they developed the game in C or C++ or other such language that interprets their code and makes it more efficient by bit shifting instead of multiplication. However, when I see something like this:

AGL * 8 - AGL

I'm pretty sure that's not the compiler and just a programming inefficiency for not doing AGL * 7 haha. That is NOT faster in assembly language so yeah, still funny stuff to find in the assembly code of the game :)
Does what they did in the rest of the formula make sense, to multiply by 7 then by 1,717,986,919 then shift 33 bits, in order to avoid doing floating-point calculations?
Do not question yourself with the why or the how. I simply am, and that is all you need to know.

User avatar
meth962
Level 23
Posts: 266
Joined: Tue Apr 03, 2012 8:37 am
Location: USA
Contact:

Re: What Game Mechanics would you like to know?

Post by meth962 » Tue Dec 06, 2016 5:53 pm

Yeah, it's faster for the Playstation CPU to do that since it doesn't have a dedicated FPU that's quicker.
まさかつあがつ

User avatar
K73SK
Level 89
Posts: 3953
Joined: Tue Feb 01, 2011 7:00 am
Location: USA
Contact:

Re: What Game Mechanics would you like to know?

Post by K73SK » Sat Dec 10, 2016 6:15 pm

meth962 wrote:I should try to give some saving grace to Prokion. I'm assuming they developed the game in C or C++ or other such language that interprets their code and makes it more efficient by bit shifting instead of multiplication. However, when I see something like this:

AGL * 8 - AGL

I'm pretty sure that's not the compiler and just a programming inefficiency for not doing AGL * 7 haha. That is NOT faster in assembly language so yeah, still funny stuff to find in the assembly code of the game :)
It's going to depend on the CPU architecture. I'm not sure what the PS1 architecture of the CPU was designed with in regards to the multiplication and addition stuff; but typically the compiler will automatically convert your code to the fastest operation based on the CPU (at least for C++).

AGL * 8 - AGL might actually be be very much faster if it converts to (AGL << 3) - AGL because of the bit-shift + the very cheap subtraction operation. In many cases, multiplication can take 4x longer than a normal add/subtract operation.

(All assuming I remember my CPU architecture class from 2 years ago well enough, lol)
Donate to legendoflegaia.net if you are one who wants to keep it alive! http://www.legendoflegaia.net/donate.html

User avatar
meth962
Level 23
Posts: 266
Joined: Tue Apr 03, 2012 8:37 am
Location: USA
Contact:

Re: What Game Mechanics would you like to know?

Post by meth962 » Mon Dec 12, 2016 10:21 am

Eh whoops. I totally forgot that was * 8 - AGL and that would make sense since you can't do a bit shift equivalent to x7. I figured bit shifting was faster but now that I look at it, doing multiplication is like two instructions in assembly for the PSX. That can't be faster could it? I mean, I'm pretty sure since consoles run at 100% speed, all instructions (say one line of assembly) take the same amount of time. But maybe not...hrmmmmmmm wish I had a class on this 2 years ago lol. It took a long time to learn this crap myself and follow a CPU emulator!

Here's what I'm talking about, K73SK.
Multiplication:
lw r2, 0x84140 (load AGL from memory into r2)
multu r2,7 (multiply r2 by 7; result is stored in 'hi' and 'lo' registers)
mflo r3 (move 'lo' result of multiplication into r3)

Bit-shift:
lw r2, 0x84140 (load AGL from memory into r2)
sll r3,r2,3 (shift r2 left by 3 which is equivalent to x8 and store in r3)
subu r3,r3,r2 (subtract r3 by r2 and store in r3)

This is the same amount of steps to get r3 to equal something x7. Right? or no? Teach me Senpai!!
まさかつあがつ

User avatar
K73SK
Level 89
Posts: 3953
Joined: Tue Feb 01, 2011 7:00 am
Location: USA
Contact:

Re: What Game Mechanics would you like to know?

Post by K73SK » Tue Dec 13, 2016 7:18 pm

The assembly instruction count may be the same, but the actual CPU time itself will be longer. It's the execution time inside of the CPU that's different - which are determined by the number of 'clock cycles' it takes to run that calculation.

So again, I don't know the CPU architecture of the PS1 so the results in cycles will be completely different. It also depends on how it handles prediction algorithms. To get an idea of what I mean by cycle count per instruction, Intel has lots of manuals on their CPU's. Here's one I found when trying to look around online:

https://software.intel.com/sites/defaul ... manual.pdf

Go to page 661 and you can see the cycle count for ADD,SUB and IMUL. On a CPU containing a Skylake microarchitecture (labeled as 06_4E,06_5E in the latency column) results in 1 cycle count for a normal ADD,SUB instruction while it takes 5 cycles to do an IMUL instruction (view attached image). Assuming it's a 32-bit, of course. For 64-bit, it's only 3 cycles.

Go down to the next page, you can see a bit-shift is only 1 cycle for an immediate value and 1.5 cycles for the CL register.
You do not have the required permissions to view the files attached to this post.
Donate to legendoflegaia.net if you are one who wants to keep it alive! http://www.legendoflegaia.net/donate.html

User avatar
meth962
Level 23
Posts: 266
Joined: Tue Apr 03, 2012 8:37 am
Location: USA
Contact:

Re: What Game Mechanics would you like to know?

Post by meth962 » Wed Dec 14, 2016 12:57 pm

Cool, I didn't know that kind of information was out there. It's readily available for creating nes and snes emulators and such but didn't think Intel would have it.

Makes me think about starting to code that way so the compiler doesn't have to change it and anyone looking at my code will go "huh??". Pretty sure everyone I know that codes doesn't know bit shifting haha.
まさかつあがつ

User avatar
Nightshade
Level 60
Posts: 1797
Joined: Tue Jul 26, 2011 9:07 pm
Location: the the the the

Re: What Game Mechanics would you like to know?

Post by Nightshade » Thu Jan 19, 2017 12:30 pm

Question:

What exactly does the War Soul do internally?

Is it just a flat +50% to Art Attack damage, which is what I sort of suspect that it is?
Do not question yourself with the why or the how. I simply am, and that is all you need to know.

Post Reply