Skip to content

Instantly share code, notes, and snippets.

@fujidig
Last active January 9, 2022 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujidig/4624244e0af04392ba08bebb56d33619 to your computer and use it in GitHub Desktop.
Save fujidig/4624244e0af04392ba08bebb56d33619 to your computer and use it in GitHub Desktop.
が ん ぜ ョ 空 Q
ぼ ダ ぞ づ 空 Q
ぼ ボ ク づ 空 Q
ぼ ノ ぞ づ 空 Q
が ん ぜ は 空 Q
ぼ ケ ぞ づ 空 Q
が れ ぜ 空 ョ Q
ヅ ー て カ 空 Q
へ る ん ョ カ Q
へ ん ん ボ ご Q
ざ へ る ん ご Q
ざ へ ん ん 空 Q
へ 空 空 空 う Q
ひ ぺ と ギ っ Q
ひ 空 と ゲ っ Q
ダ ひ ぺ ぐ ジ Q
ご に ヂ ざ 空 Q
空 て よ ん ダ Q
ひ ぺ と ザ っ Q
て ス ん ダ ダ Q
ダ ダ ギ ダ メ Q
ギ ザ く だ ザ Q
に ぽ に ュ ぺ Q
ョ ぐ ダ に ざ Q
に ざ ぺ 空 空 Q
空 空 空 空 空 Q
ョ ア て ゼ っ Q
ど の ざ の む Q
の ョ ア ギ て Q
デ ボ の
26 de 2e af 7f 50 3e 0f 2f 32 7f 50 3e 1c 87 32
7f 50 3e 98 2f 32 7f 50 26 de 2e ca 7f 50 3e 88
2f 32 7f 50 26 da 2e 7f af 50 11 e3 c3 85 7f 50
cd d9 de af 85 50 cd de de 1c 2a 50 2b cd d9 de
2a 50 2b cd de de 7f 50 cd 7f 7f 7f b3 50 cb 47
c4 06 df 50 cb 7f c4 08 df 50 0f cb 47 28 0b 50
2a c6 10 2b 7f 50 7f c3 d6 de 0f 50 cb 47 c4 0a
df 50 c3 8c de 0f 0f 50 0f 0f 06 0f a0 50 06 0a
b8 30 0a 50 c6 48 c6 ae 47 50 af 28 0f c6 2b 50
c6 2b 47 7f 7f 50 7f 7f 7f 7f 7f 50 af 80 c3 0d
df 50 34 c9 2b c9 d1 50 c9 af 80 06 c3 50 12 1c
c9
177 bytes
29.5 pokes.
[org(0xde64)]
start:
ld h, 0xde # call_joypad
ld l, 0xaf
ld a, 0x0f
cpl
ld [hl-], a
ld a, 0x1c
add a, a
ld [hl-], a
ld a, 0x98
cpl
ld [hl-], a
ld h, 0xde # ld_hl_a
ld l, 0xca
ld a, 0x88
cpl
ld [hl-], a
ld h, 0xda
ld l, 0x7f
mainloop:
xor a
ld de, 0xc3e3
add a, l
call print_hex_with_swap
xor a
add a, l
call print_hex
inc e
ld a, [hl+]
dec hl
call print_hex_with_swap
ld a, [hl+]
dec hl
call print_hex
ld a, a
call_joypad:
call 0x7f7f
db 0x7f, 0xb3
bit 0, a
call nz, process_a_button
bit 7, a
call nz, process_down_button
rrca
bit 0, a
jr z, skip_processing_b_button
ld a, [hl+]
add a, 0x10
dec hl
ld a, a
ld_hl_a:
db 0x7f
jp mainloop_end
skip_processing_b_button:
rrca
bit 0, a
call nz, process_start_button
mainloop_end:
jp mainloop
print_hex_with_swap:
rrca
rrca
rrca
rrca
print_hex:
ld b, 0x0f
and b
ld b, 0xa
cp b
jr nc, print_hex_alphabet
add a, 0x48
add a, 0xae
ld b, a
xor a
jr z, print_hex_join
print_hex_alphabet:
add a, 0x2b
add a, 0x2b
ld b, a
ld a, a
ld a, a
ld a, a
ld a, a
ld a, a
ld a, a
ld a, a
print_hex_join:
xor a
add a, b
jp print_hex_end
process_a_button:
inc [hl]
ret
process_down_button:
dec hl
ret
process_start_button:
pop de
ret
print_hex_end:
xor a
add a, b
ld b, 0xc3
# ここでld d, bが挟まることが必要
ld [de], a
inc e
ret
require "csv"
Insn = Struct.new(:opcodes, :next, :jump, :index)
chars = CSV.read("pokemoncode.csv").map{|x| x[2] }
chars[0x7f] = "空"
chars[0x50] = "Q"
numbytes = {
0x06 => 2,
0x08 => 3,
0x0a => 2,
0x0e => 2,
0x11 => 3,
0x26 => 2,
0x28 => 2,
0x2e => 2,
0x30 => 2,
0x31 => 3,
0x3e => 2,
0xc2 => 3,
0xc3 => 3,
0xc4 => 3,
0xc6 => 2,
0xca => 3,
0xcb => 2,
0xcc => 3,
0xcd => 3,
0xce => 2,
0xd2 => 3,
0xd4 => 3,
0xd6 => 2,
0xda => 3,
0xdc => 3,
0xde => 2,
0xe0 => 2
}
numbytes.default = 1
offset = ARGV[1].to_i(16)
bin = File.binread(ARGV[0]).bytes.to_a[offset..-1]
# 連結リストを作る
first_insn = prev_insn = Insn.new([], nil, nil, nil)
insn_hash = []
i = 0
while i < bin.size
op = bin[i]
len = numbytes[op]
ops = bin[i, len]
insn = Insn.new(ops, nil, nil, nil)
insn_hash[offset + i] = insn
i += len
prev_insn.next = insn
prev_insn = insn
end
# ジャンプ先を設定
insn = first_insn.next
index = 0
while insn != nil
if [0xc4, 0xcc, 0xcd, 0xd4, 0xdc, 0xc2, 0xc3, 0xca, 0xd2, 0xda].include?(insn.opcodes[0])
insn.jump = insn_hash[insn.opcodes[2] << 8 | insn.opcodes[1]]
end
if [0x28, 0x30].include?(insn.opcodes[0])
off = insn.opcodes[1]
off -= 0x100 if off >= 0x80
insn.jump = insn_hash[offset + index + 2 + off]
end
index += insn.opcodes.size
insn = insn.next
end
# 6バイトに1回0x50を挟む
prev_insn = first_insn
insn = first_insn.next
index = 0
outindex = 0
while insn != nil
len = insn.opcodes.size
if outindex % 6 + len >= 6
(5 - outindex % 6).times {
new_insn = Insn.new([0x7f], insn, nil)
prev_insn.next = new_insn
prev_insn = new_insn
outindex += 1
}
new_insn = Insn.new([0x50], insn, nil)
prev_insn.next = new_insn
prev_insn = new_insn
outindex += 1
end
index += len
outindex += len
insn, prev_insn = insn.next, insn
end
# indexを設定
insn = first_insn.next
index = 0
while insn != nil
len = insn.opcodes.size
insn.index = index
index += len
insn = insn.next
end
# ジャンプ先の情報をopcodesに反映
insn = first_insn.next
index = 0
while insn != nil
len = insn.opcodes.size
if [0xc4, 0xcc, 0xcd, 0xd4, 0xdc, 0xc2, 0xc3, 0xca, 0xd2, 0xda].include?(insn.opcodes[0]) and insn.jump
insn.opcodes[1] = (offset + insn.jump.index) & 0xff
insn.opcodes[2] = (offset + insn.jump.index) >> 8
end
if [0x28, 0x30].include?(insn.opcodes[0])
off = insn.jump.index - index - 2
off += 0x100 if off < 0
if !(0 <= off && off < 0x100)
raise "offset error"
end
insn.opcodes[1] = off
end
index += insn.opcodes.length
insn = insn.next
end
# 新しいバイナリを作る
newbin = []
insn = first_insn.next
while insn != nil
len = insn.opcodes.size
len.times {|i| newbin << insn.opcodes[i] }
insn = insn.next
end
File.binwrite(ARGV[0] + ".patched", newbin.map{|x| x.chr }.join(""))
newbin.each_with_index do |insn, i|
print chars[insn] || ("<<%02x>>" % insn)
print " "
puts if insn == 0x50
end
puts
puts newbin.map{|x| "%02x" % x }.each_slice(16).map {|x| x.join(" ") }.join("\n")
puts "#{newbin.length} bytes"
puts "#{newbin.length / 6.0} pokes."
0x00 NOP
0x01 LD BC,d16
0x02 LD (BC),A
0x03 INC BC
0x04 INC B
0x05 DEC B
0x06 LD B,d8
0x07 RLCA
0x08 LD (a16),SP
0x09 ADD HL,BC
0x0a LD A,(BC)
0x0b DEC BC
0x0c INC C
0x0d DEC C
0x0e LD C,d8
0x0f RRCA
0x10 STOP 0
0x11 LD DE,d16
0x12 LD (DE),A
0x13 INC DE
0x14 INC D
0x15 DEC D
0x16 LD D,d8
0x17 RLA
0x18 JR r8
0x19 ADD HL,DE
0x1a LD A,(DE)
0x1b DEC DE
0x1c INC E
0x1d DEC E
0x1e LD E,d8
0x1f RRA
0x20 JR NZ,r8
0x21 LD HL,d16
0x22 LD (HL+),A
0x23 INC HL
0x24 INC H
0x25 DEC H
0x26 LD H,d8
0x27 DAA
0x28 JR Z,r8
0x29 ADD HL,HL
0x2a LD A,(HL+)
0x2b DEC HL
0x2c INC L
0x2d DEC L
0x2e LD L,d8
0x2f CPL
0x30 JR NC,r8
0x31 LD SP,d16
0x32 LD (HL-),A
0x33 INC SP
0x34 INC (HL)
0x35 DEC (HL)
0x36 LD (HL),d8
0x37 SCF
0x38 JR C,r8
0x39 ADD HL,SP
0x3a LD A,(HL-)
0x3b DEC SP
0x3c INC A
0x3d DEC A
0x3e LD A,d8
0x3f CCF
0x40 LD B,B
0x41 LD B,C
0x42 LD B,D
0x43 LD B,E
0x44 LD B,H
0x45 LD B,L
0x46 LD B,(HL)
0x47 LD B,A
0x48 LD C,B
0x49 LD C,C
0x4a LD C,D
0x4b LD C,E
0x4c LD C,H
0x4d LD C,L
0x4e LD C,(HL)
0x4f LD C,A
0x50 LD D,B 終端文字
0x51 LD D,C
0x52 LD D,D
0x53 LD D,E
0x54 LD D,H
0x55 LD D,L
0x56 LD D,(HL)
0x57 LD D,A
0x58 LD E,B
0x59 LD E,C
0x5a LD E,D
0x5b LD E,E
0x5c LD E,H
0x5d LD E,L
0x5e LD E,(HL)
0x5f LD E,A
0x60 LD H,B
0x61 LD H,C
0x62 LD H,D
0x63 LD H,E
0x64 LD H,H
0x65 LD H,L
0x66 LD H,(HL)
0x67 LD H,A
0x68 LD L,B
0x69 LD L,C
0x6a LD L,D
0x6b LD L,E
0x6c LD L,H
0x6d LD L,L
0x6e LD L,(HL)
0x6f LD L,A
0x70 LD (HL),B
0x71 LD (HL),C
0x72 LD (HL),D
0x73 LD (HL),E
0x74 LD (HL),H
0x75 LD (HL),L
0x76 HALT
0x77 LD (HL),A
0x78 LD A,B
0x79 LD A,C
0x7a LD A,D
0x7b LD A,E
0x7c LD A,H
0x7d LD A,L
0x7e LD A,(HL)
0x7f LD A,A (空白)
0x80 ADD A,B
0x81 ADD A,C
0x82 ADD A,D
0x83 ADD A,E
0x84 ADD A,H
0x85 ADD A,L
0x86 ADD A,(HL)
0x87 ADD A,A
0x88 ADC A,B
0x89 ADC A,C
0x8a ADC A,D
0x8b ADC A,E
0x8c ADC A,H
0x8d ADC A,L
0x8e ADC A,(HL)
0x8f ADC A,A
0x90 SUB B
0x91 SUB C
0x92 SUB D
0x93 SUB E
0x94 SUB H
0x95 SUB L
0x96 SUB (HL)
0x97 SUB A
0x98 SBC A,B
0x99 SBC A,C
0x9a SBC A,D
0x9b SBC A,E
0x9c SBC A,H
0x9d SBC A,L
0x9e SBC A,(HL)
0x9f SBC A,A
0xa0 AND B
0xa1 AND C
0xa2 AND D
0xa3 AND E
0xa4 AND H
0xa5 AND L
0xa6 AND (HL)
0xa7 AND A
0xa8 XOR B
0xa9 XOR C
0xaa XOR D
0xab XOR E
0xac XOR H
0xad XOR L
0xae XOR (HL)
0xaf XOR A
0xb0 OR B
0xb1 OR C
0xb2 OR D
0xb3 OR E
0xb4 OR H
0xb5 OR L
0xb6 OR (HL)
0xb7 OR A
0xb8 CP B
0xb9 CP C
0xba CP D
0xbb CP E
0xbc CP H
0xbd CP L
0xbe CP (HL)
0xbf CP A
0xc0 RET NZ
0xc1 POP BC
0xc2 JP NZ,a16
0xc3 JP a16
0xc4 CALL NZ,a16
0xc5 PUSH BC
0xc6 ADD A,d8
0xc7 RST 00H
0xc8 RET Z
0xc9 RET
0xca JP Z,a16
0xcb PREFIX CB
0xcc CALL Z,a16
0xcd CALL a16
0xce ADC A,d8
0xcf RST 08H
0xd0 RET NC
0xd1 POP DE
0xd2 JP NC,a16
0xd3
0xd4 CALL NC,a16
0xd5 PUSH DE
0xd6 SUB d8
0xd7 RST 10H
0xd8 RET C
0xd9 RETI
0xda JP C,a16
0xdb
0xdc CALL C,a16
0xdd
0xde SBC A,d8
0xdf RST 18H
0xe0 LDH (a8),A
0xe1 POP HL
0xe2 LD (C),A
0xe3
0xe4
0xe5 PUSH HL
0xe6 AND d8
0xe7 RST 20H
0xe8 ADD SP,r8
0xe9 JP (HL)
0xea LD (a16),A
0xeb
0xec
0xed
0xee XOR d8
0xef RST 28H
0xf0 LDH A,(a8)
0xf1 POP AF
0xf2 LD A,(C)
0xf3 DI
0xf4
0xf5 PUSH AF
0xf6 OR d8
0xf7 RST 30H
0xf8 LD HL,SP+r8
0xf9 LD SP,HL
0xfa LD A,(a16)
0xfb EI
0xfc
0xfd
0xfe CP d8
0xff RST 38H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment