ALT-N change it to some other keys (How)

Second part of the best game ever made.
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

ALT-N change it to some other keys (How)

Post by Thenasty »

In the past, David had help me on how to change the controls of Pop2 (viewtopic.php?f=76&t=4250)

I learned a lot from this and I thank David for helping me.

I learned how to convert the Scancode key to replace the Key assigned to the game.

I now need to know how to convert ALT-N for the game to some other key like SHIFT-P

I don't know how to convert this KEY combination using ALT & SHIFT (it only show in Scancode ALT = 38 & SHIFT = A2)
Here is what David showed me: http://kbdlayout.info/KBDUS/scancodes

This is what I see in the code:

seg000:030A dw offset key_alt_n


eg000:03CD ; ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
seg000:03CF align 2
seg000:03D0
seg000:03D0 key_alt_n: ; CODE XREF: check_keys+A7j
seg000:03D0 ; DATA XREF: check_keys+C8o
seg000:03D0 cmp current_level, 3
seg000:03D5 jle loc_3DE
seg000:03D7
seg000:03D7 loc_3D7:
seg000:03D7 cmp cheat_mode, 0
seg000:03DC jz loc_436
seg000:03DE


How to convert (ALT-????) or (SHIFT-????) ?

I never asked David how he came to finding out how to search this listed below.
(I am wondering if it's the same 80 3E ALT-VALUE 1D 00 (if I know the CONVERTED VALUE) & replaced it with a SHIFT-P (if I know the CONVERTED VALUE).

CONTROL Up
Search: 80 3E 55 1D 00

CONTROL Down
Search: 80 3E 5D 1D 00

CONTROL Left
Search: 80 3E 58 1D 00

CONTROL Right
Search: 80 3E 5A 1D 00



I am hoping that it's something simple to find out.

Thanks a lot again for the help (David)
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

Re: ALT-N change it to some other keys (How)

Post by Thenasty »

I guess I was loking at an old source this I download a different one this is what i see in the same lines


seg000:03D0 key_alt_n: ; CODE XREF: check_keys+A7j
seg000:03D0 ; DATA XREF: check_keys+C8o
seg000:03D0 80 3E 18 0A 03 cmp current_level, 3
seg000:03D5 7E 07 jle loc_3DE
seg000:03D7 83 3E 42 11 00 cmp cheat_mode, 0
seg000:03DC 74 58 jz loc_436





From what I understand from the last request that David help me out on, I can see something similar to what I might be searching for

80 3E 18 0A 03 I think is what I might be searching for and replace the 18 with SOMETHING I need I am assuming that 18 is ALT-N


Now, if I am correct, how can I calculate SHIFT-anyletter or ALT-anyletter or even CTRL-anyletter ?
Is this Formula still the same ? 0xScancode+0x0D

Thanks for any help that anyone can provides me.
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

Re: ALT-N change it to some other keys (How)

Post by Thenasty »

I guess I was ahead of myself.

I tried searching for 80 3E 18 0A 03 and 83 3E 42 11 00 and came up empty.

I search only 80 3E and tons shows....



Hopefully someone can shed a small or a big light for me. Thanks
David
The Prince of Persia
The Prince of Persia
Posts: 2846
Joined: December 11th, 2008, 9:48 pm
Location: Hungary

Re: ALT-N change it to some other keys (How)

Post by David »

Sorry, I haven't posted the PoP2 1.1 disassembly yet.
This topic has only the disassemblies of PoP2 IR (Initial Release).

First of all, this is the code which checks for Alt+N along with some other keys is the following: (PoP2 v1.1)

Code: Select all

seg000:034E                         loc_34E:                                ; CODE XREF: process_key+5F↑j
seg000:034E 2D 00 23                                sub     ax, 2300h ; The smallest key code checked in this section. (Alt+N: 0x3100 -> 0xE00)
seg000:0351 A8 FF                                   test    al, 0FFh ; Check lower byte.
seg000:0353 74 03                                   jz      loc_358 ; If it's zero then continue below.
seg000:0355 E9 74 01                                jmp     end_switch ; Otherwise it's not a key code we care about.
seg000:0358                         ; ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
seg000:0358
seg000:0358                         loc_358:                                ; CODE XREF: process_key+95↑j
seg000:0358 B1 07                                   mov     cl, 7 ; If the lower byte is zero then shifting right by 7 bits
seg000:035A D3 E8                                   shr     ax, cl ; is the same as shifting right by 8 bits then left by 1 bit. (Alt+N: 0xE00 -> 0x1C)
seg000:035C 3D 1E 00                                cmp     ax, 1Eh ; Number of bytes in the jump table below, minus 2.
seg000:035F 76 03                                   jbe     loc_364 ; If the code is within range then continue below.
seg000:0361 E9 68 01                                jmp     end_switch ; Otherwise it's not a key code we care about.
seg000:0364                         ; ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
seg000:0364
seg000:0364                         loc_364:                                ; CODE XREF: process_key+A1↑j
seg000:0364 93                                      xchg    ax, bx
seg000:0365 2E FF A7 6A 03                          jmp     cs:off_36A[bx] ; Indirect jump using the jump table below. (Alt+N: 0x1C+0x36A=0x386)
seg000:0365                         ; ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
seg000:036A 12 04                   off_36A         dw offset key_alt_h  ; 0x23
seg000:036C 2A 04                                   dw offset key_alt_j  ; 0x24
seg000:036E 30 04                                   dw offset key_alt_k  ; 0x25
seg000:0370 3E 04                                   dw offset key_alt_l  ; 0x26
seg000:0372 CC 04                                   dw offset end_switch ; 0x27
seg000:0374 CC 04                                   dw offset end_switch ; 0x28
seg000:0376 CC 04                                   dw offset end_switch ; 0x29
seg000:0378 CC 04                                   dw offset end_switch ; 0x2A
seg000:037A CC 04                                   dw offset end_switch ; 0x2B
seg000:037C CC 04                                   dw offset end_switch ; 0x2C
seg000:037E CC 04                                   dw offset end_switch ; 0x2D
seg000:0380 CC 04                                   dw offset end_switch ; 0x2E
seg000:0382 5E 04                                   dw offset key_alt_v  ; 0x2F
seg000:0384 CC 04                                   dw offset end_switch ; 0x30
seg000:0386 66 04                                   dw offset key_alt_n  ; 0x31
seg000:0388 B6 04                                   dw offset key_alt_m  ; 0x32
seg000:038A                         ; ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
The Alt+N key is checked in a rather different way than the arrow keys.
Instead of using an array of key states, the program reads a key code from the OS, and then branches around based on it.
This key code is not the same as the scancode, but for Alt+letter keys it's based on the scancode.
The code of Alt+N is 0x3100, where 0x31 is the scancode of N.
For letter keys by themselves or with shift, the key code is simply the ASCII code of the letter. (example: p = 0x70, Shift+P = 0x50)

The thing is, instead of explicitly comparing the key code with the code of each supported key,
the code is optimized, for example is subtracts values from the key code, shifts its bits, and uses jump tables.
So you can't just change 0x3100 or 0x31 to something else, because that number is not even in the EXE file!
(The numbers after the semicolons are not in the code, they are just comments.)

(The part in the spoiler is not important, I was just pondering what can we do with less-radical edits before I found the better solution.)
Spoiler: show
The only thing you can do by editing this section is to disable the effect of Alt+N (change 66 04 to CC 04),
or move it to some other Alt+letter key whose scancode is in the range 0x23..0x32 (change one of the CC 04 bytes to 66 04).

Or, it's possible to change the 2300h at the top, to change what range of key codes will this code look for, but then that will move the effects of Alt+H,J,K,L,V,M to other keys as well!

But to add the next level effect to Shift+P or some other key not in this range, while keeping Alt+H,J,K,L,V,M intact, we would need to add a new check somewhere before this section of code.
But for that we need to overwrite some code which checks for some other key.
Those other keys are: Alt+G, Alt+R, Esc, Space, Alt+O, Alt+A, Alt+S.

But there is a solution!
We overwrite all of the key-checking code (more than what I've shown above) with something that does the same but can be edited more easily.

At 0x2315 write: 3D 00 22 0F 84 D6 00 3D 00 13 0F 84 85 00 3D 1B 00 74 62 3D 20 00 74 67 3D 00 18 0F 84 82 00 3D 00 1E 0F 84 83 01 3D 00 1F 0F 84 98 00 3D 00 23 0F 84 C9 00 3D 00 24 0F 84 DA 00 3D 00 25 0F 84 D9 00 3D 00 26 0F 84 E0 00 3D 00 2F 0F 84 F9 00 3D 00 31 0F 84 FA 00 3D 00 32 0F 84 43 01 E9 56 01

This is the meaning of the new code:
(listing from NASM)

Code: Select all

     1                                  cpu 386
     2
     3                                  org 0
     4 00000000 90<rept>                times 0315h nop
     5
     6 00000315 3D0022                  cmp ax,2200h ; Alt+G
     7 00000318 0F84D600                je $$+03F2h
     8
     9 0000031C 3D0013                  cmp ax,1300h ; Alt+R
    10 0000031F 0F848500                je $$+03A8h
    11
    12 00000323 3D1B00                  cmp ax,001Bh ; Esc
    13 00000326 7462                    je $$+038Ah
    14
    15 00000328 3D2000                  cmp ax,0020h ; Space
    16 0000032B 7467                    je $$+0394h
    17
    18 0000032D 3D0018                  cmp ax,1800h ; Alt+O
    19 00000330 0F848200                je $$+03B6h
    20
    21 00000334 3D001E                  cmp ax,1E00h ; Alt+A
    22 00000337 0F848301                je $$+04BEh
    23
    24 0000033B 3D001F                  cmp ax,1F00h ; Alt+S
    25 0000033E 0F849800                je $$+03DAh
    26
    27 00000342 3D0023                  cmp ax,2300h ; Alt+H
    28 00000345 0F84C900                je $$+0412h
    29
    30 00000349 3D0024                  cmp ax,2400h ; Alt+J
    31 0000034C 0F84DA00                je $$+042Ah
    32
    33 00000350 3D0025                  cmp ax,2500h ; Alt+K
    34 00000353 0F84D900                je $$+0430h
    35
    36 00000357 3D0026                  cmp ax,2600h ; Alt+L
    37 0000035A 0F84E000                je $$+043Eh
    38
    39 0000035E 3D002F                  cmp ax,2F00h ; Alt+V
    40 00000361 0F84F900                je $$+045Eh
    41
    42 00000365 3D0031                  cmp ax,3100h ; Alt+N
    43 00000368 0F84FA00                je $$+0466h
    44
    45 0000036C 3D0032                  cmp ax,3200h ; Alt+M
    46 0000036F 0F844301                je $$+04B6h
    47
    48 00000373 E95601                  jmp $$+04CCh
    49
    50 00000376 90<rept>                times ($$+038Ah-$) nop
This does not yet change any key assignments.
To change them, you need to change the two bytes after one of the 0x3D bytes.

You can find a table of DOS key codes here for example: http://microvga.com/ansi-keycodes
Notes:
That table has the codes in decimal, while the code above contains them in hexadecimal, so you need to convert them appropriately.
If the code of the new key is a single number then write it into the first byte after 0x3D, and write 0x00 into the second byte. (3D nn 00)
If the code of the new key is two numbers (the first is always 0), then write them into two separate bytes after 0x3D. (3D 00 nn)

To change Alt+N to Shift+P for example, change 3D 00 31 to 3D 50 00.
(The table of DOS key codes contains these codes as 0;49 and 80.)
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

Re: ALT-N change it to some other keys (How)

Post by Thenasty »

Wow, thanks a million again David....

I would never had figure out :)


One of the reason why I am trying to learn how to change control of a, is this other game that same author of POP had made "Karateka"

I am gonna try to see if I can figure out how to change those default control to fit my Control Panel (Arcade Cabinet, Mame)

I love this 2 80's game and still play it.


I am going to try now what you have posted. I am sure it's gonna be 10000000000000% correct.

Ed
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

Re: ALT-N change it to some other keys (How)

Post by Thenasty »

Thanks a million David again.. It works

any yes, now the ALT-N had become SHIFT-P

In regular GAme mode, it will skip up to the 3rd level (i think) and then it stops but in activating the cheat yippeeyahoo, it will continue all the way to the end and cycles.

How can I achieve this in regular game mode ? (no cheat enabled).

Thanks,
Ed
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

Re: ALT-N change it to some other keys (How)

Post by Thenasty »

I had a thought and again I'm wrong... replace K with F9.. but no go :(.

I look for the K (cause this is the button in cheat to kill all guard) 80 3E 32 1D 00 and replace with and F9 key (chance 32 to 50) so that way it won't be press accidentally on one of the Control Panel Button (only regular letters are used in my Control Panel and no F-Keys used at all).

I am now guessing that when CHEAT is enabled, it is "checked in a rather different way" like ALT SHIFT CTRL.

Thanks again for any help.
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

Re: ALT-N change it to some other keys (How)

Post by Thenasty »

if it's just easier to make the ALT-N (which is now SHIFT-P) to skip all levels without enabling yippeeyahoo, then I don't have to worry about the K kill guard with cheat on.

Thanks.
David
The Prince of Persia
The Prince of Persia
Posts: 2846
Joined: December 11th, 2008, 9:48 pm
Location: Hungary

Re: ALT-N change it to some other keys (How)

Post by David »

Thenasty wrote: August 8th, 2020, 9:58 pm In regular GAme mode, it will skip up to the 3rd level (i think) and then it stops but in activating the cheat yippeeyahoo, it will continue all the way to the end and cycles.

How can I achieve this in regular game mode ? (no cheat enabled).
Enable unlimited level skip outside cheat mode: (PoP2 v1.1)

Search: 83 3E C2 10 00 74 58
Change: 74 58 to 90 90

(Needed for wrap around)
Search: 83 3E C2 10 00 74 08
Change: 74 08 to 90 90

(Don't reduce time to 15 minutes)
Search: 83 3E C2 10 00 75 13
Change: 75 13 to EB 13

This hack changes the following part of the code:

Code: Select all

seg000:0466                         key_alt_n:
seg000:0466
seg000:0466 80 3E 98 09 03                          cmp     current_level, 3
seg000:046B 7E 07                                   jle     loc_474
seg000:046D 83 3E C2 10 00                          cmp     cheat_mode, 0
seg000:0472 74 58                                   jz      end_switch ; <-- change to NOP
seg000:0474
seg000:0474                         loc_474:
seg000:0474 80 3E 98 09 0E                          cmp     current_level, 14
seg000:0479 75 0F                                   jnz     loc_48A
seg000:047B 83 3E C2 10 00                          cmp     cheat_mode, 0
seg000:0480 74 08                                   jz      loc_48A ; <-- change to NOP
seg000:0482 C7 06 EC 5C 01 00                       mov     next_level, 1
seg000:0488 EB 22                                   jmp     short loc_4AC
seg000:048A                         ; ───────────────────────────────────────────────────────────────────────────
seg000:048A
seg000:048A                         loc_48A:
seg000:048A
seg000:048A A0 98 09                                mov     al, current_level
seg000:048D 98                                      cbw
seg000:048E 40                                      inc     ax
seg000:048F A3 EC 5C                                mov     next_level, ax
seg000:0492 83 3E C2 10 00                          cmp     cheat_mode, 0
seg000:0497 75 13                                   jnz     loc_4AC ; <-- change to JMP
seg000:0499 83 3E D2 5C 0F                          cmp     minutes_left, 15
seg000:049E 7E 0C                                   jle     loc_4AC
seg000:04A0 C7 06 D2 5C 0F 00                       mov     minutes_left, 15
seg000:04A6 C7 06 EA 5C CF 02                       mov     ticks_left, 719
seg000:04AC
seg000:04AC                         loc_4AC:
seg000:04AC
seg000:04AC 2B C0                                   sub     ax, ax
seg000:04AE 50                                      push    ax
seg000:04AF 9A D2 83 29 11                          call    sub_19662
seg000:04B4 EB 16                                   jmp     short end_switch
seg000:04B6                         ; ───────────────────────────────────────────────────────────────────────────
David
The Prince of Persia
The Prince of Persia
Posts: 2846
Joined: December 11th, 2008, 9:48 pm
Location: Hungary

Re: ALT-N change it to some other keys (How)

Post by David »

Thenasty wrote: August 8th, 2020, 9:57 pm One of the reason why I am trying to learn how to change control of a, is this other game that same author of POP had made "Karateka"

I am gonna try to see if I can figure out how to change those default control to fit my Control Panel (Arcade Cabinet, Mame)
You can find some keys used by Karateka in KARATEKA.EXE starting at offset 0x71C2 (assuming we have the same version of the game).

The keys are, in order:
q = punch high
a = punch mid
z = punch low
w = kick high
s = kick mid
x = kick low
4 = left
6 = right
space = toggle fighting mode
b = bow
0 = (unknown)

Do you also want to change some keys not listed here?
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

Re: ALT-N change it to some other keys (How)

Post by Thenasty »

Thanks for the update on Pop2. I going to try it after I type this.


For Karateka
The 4 & 6 are basically ARROWS left right (don't need. I'll use the Arrows Left/Right)

the only thing needs changing are:

b = bow
q = punch high
a = punch mid
z = punch low
w = kick high
s = kick mid
x = kick low

spacebar change into Enter
CTRL Q (Quit) changing it to ESC


Thanks a million.
With this Pandemic, I been out of work since March and nothing to do. This learning hacking (changin controls to a game) is keeping me busy.

Thank again.
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

Re: ALT-N change it to some other keys (How)

Post by Thenasty »

0x71c2 - 71D5 showS
71 61 7A 77 73 78 34 36 20 62 30 2E 69 6E 64 00 2E 64 61 74

then bunch of 00000000000
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

Re: ALT-N change it to some other keys (How)

Post by Thenasty »

Hi David,

The wrap around code, Thanks for giving me that for learning.
I actually did not use but it's there for my learning, Thanks
83 3E C2 10 00 74 08


How can I make this code to stop at the Last level where the Prince is Standing on top of the Tower? This way I still have to play the Last level and beat Jaffar.

83 3E C2 10 00 74 58


Thanks.
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

Re: ALT-N change it to some other keys (How)

Post by Thenasty »

I think i got Karateka figure out...


using what you gave me here http://microvga.com/ansi-keycodes


I going to try all and see if I'm right
Thenasty
Sheikh
Sheikh
Posts: 34
Joined: February 27th, 2020, 10:19 pm

Re: ALT-N change it to some other keys (How)

Post by Thenasty »

one thing i notice in the Chart http://microvga.com/ansi-keycodes
is that it did not include the Spacebar but I figure it is 32 into hex is 0x20


I don't see CTRL-Q (0x11) in the code so i can change it to Esc (0x1B)

Thanks David.
Post Reply