Glitch City Laboratories Archives

Glitch City Laboratories closed on 1 September 2020 (announcement). This is an archived copy of a thread from Glitch City Laboratories Forums.

You can join Glitch City Research Institute to ask questions or discuss current developments.

You may also download the archive of this forum in .tar.gz, .sql.gz, or .sqlite.gz formats.

Generation I Glitch Discussion

Pokemon Blue plays itself (TPP) - Page 1

Pokemon Blue plays itself (TPP)

Posted by: luckytyphlosion
Date: 2015-01-01 12:53:27
It took me so long to figure out how I could do this, but I finally figured out!

Every frame, the OAM DMA is called (or something like that I don't know it very well), but if you redirect the code somewhere else in HRAM, you can have the game perform instructions every frame!

The way I did it:

At FF86, write "jr FFF9".
At FFF9, write "dec a"
At FFFA, write "jr nz, FFF9"
At FFFC, write "jp D34A"

D34A is the rival name, so it can be never changed through normal gameplay.

At D34A, write "ld a,(FFD3)"
At D34D, write "ld (FFF8),a"
At D350, write "ret"

This will write a random value to FFF8 every frame (I think, correct me if I'm wrong). FFF8 is the joypad, so the game is essentially pressing random buttons every single frame.

Now, the only problem is that I'm not exactly sure if the game is doing "OAM DMA Bad Accesses", since Sanqui says BGB doesn't emulate that part correctly, but the OAM DMA is called at the correct time, so I don't think anything bad is happening.

Re: Pokemon Blue plays itself (TPP)

Posted by: Torchickens
Date: 2015-01-01 13:12:05
Awesome. Is that possible with 8F, or only a custom map script? Will the effects be 'permanent'?

Re: Pokemon Blue plays itself (TPP)

Posted by: luckytyphlosion
Date: 2015-01-01 13:25:00
As long as you don't reset, then the effects "should" be permanent. I made sure the portion of HRAM the jump relative sends you to in order to complete the OAM DMA is unused. You can probably structure the code with 8F, but it would need to be done in one go.

Re: Pokemon Blue plays itself (TPP)

Posted by: Torchickens
Date: 2015-01-01 13:29:36
It white screened for me on both BGB and VBA. Do you know why? I entered the right ASM/opcodes. I'll try Pokémon Blue instead of Red.

Edit: I accidentally entered code in the wrong addresses, sorry.
Edit 2: This is hilarious on turbo mode, ha ha :). One time Red used 8F (I just edited the memory, haven't set up code yet), freezing the game.
Edit 3: Why don't you just use this?

At FF86, write "jr FFF9".
At FFF9, write "jp D34A"

At D34A, write "ld a,(FFD3)"
At D34D, write "ld (FFF8),a"
At D350, write "ret"

Does your code add some delay before the next button of some sort, because the game only jumps to D34A in yours if a is 0?

Re: Pokemon Blue plays itself (TPP)

Posted by: luckytyphlosion
Date: 2015-01-01 14:11:45
I need to complete the OAM DMA before I can jump to D34A; the extra code in my post was to complete it because I removed some code in the OAM DMA, so I needed to add it somewhere else.

Re: Pokemon Blue plays itself (TPP)

Posted by: Torchickens
Date: 2015-01-01 14:16:58

I need to complete the OAM DMA before I can jump to D34A; the extra code in my post was to complete it because I removed some code in the OAM DMA, so I needed to add it somewhere else.


I don't really get that, but it seems to work perfectly without dec a and jr nz, FFF9. Well maybe because of an emulation error. Dunno. Would it not work on a real Game Boy?

Re: Pokemon Blue plays itself (TPP)

Posted by: luckytyphlosion
Date: 2015-01-01 14:31:33
I'm not so sure. From FF80-FF89, code is executed there every vblank, so I want to make sure that it's successfully executed.

Re: Pokemon Blue plays itself (TPP)

Posted by: Torchickens
Date: 2015-01-01 14:35:30

I'm not so sure. From FF80-FF89, code is executed there every vblank, so I want to make sure that it's successfully executed.


OK. Thanks then. Going to try this on an SP using my Xploder GB :).

Edit: The original code works. Will test other one.
Edit 2: The other one doesn't work on SP. It was an emulation error. You were right.

By the way, for people who don't know how to find them, the GameShark codes for all this are:

Part 1, rival's name (Enable this first. Preferably save with it)
01FA4AD3
01D34BD3
01FF4CD3
01EA4DD3
01F84ED3
01FF4FD3
01C950D3

Part 2, jump/complete OAM DMA (Enable this when you load your save with the rival's name)

011886FF
017187FF
013DF9FF
0120FAFF
01FDFBFF
01C3FCFF
014AFDFF
01D3FEFF

These don't work on my VBA, only on my actual Game Boy Advance SP using Xploder, and BGB.

Re: Pokemon Blue plays itself (TPP)

Posted by: luckytyphlosion
Date: 2015-01-01 15:25:40
I guess you need to use BGB then. VBA apparently incorrectly emulates "Modify Timing" (the other inaccuracies are just soundstuff and things that all other emulators fail on, which are OAM bugs) according to this, but I'm not sure what it is.

Re: Pokemon Blue plays itself (TPP)

Posted by: Torchickens
Date: 2015-01-01 15:35:24
Oh, so that may be the problem.

Most of the time I'm not going to switch to BGB (only when I need to, for this and for debugging), because VBA is not that inaccurate (although I imagine accuracy is much more important for speedrunning). Even though it has sound glitches, the echo RAM glitch that apparently affected earlier versions is gone in later versions, and I like its in-built recorder; which I think is more convenient than BGB's (which takes many screenshots and audio, and you have to join the two together).

I'll test if this works on Bizhawk, because that has an in-built recorder like VBA's.

Re: Pokemon Blue plays itself (TPP)

Posted by: Dabomstew
Date: 2015-01-01 15:45:40
Well, I read over some gameboy docs to figure out if lucky's code working was just a fluke or perfectly fine. And the result is, it's perfectly fine:

(chat logs quoted)


[10:47:08] dabomstew okay lucky i researched oam dma a bit and i know why your code works now lol
[10:47:20] dabomstew if you wanted more info still
[10:47:50] dabomstew ok so
[10:48:05] dabomstew "oam dma" is only up to the bit where you're deccing a until it's 0
[10:48:27] dabomstew basically the "dec a until it's 0" bit is forcing the cpu to wait in hram long enough for the DMA to complete
[10:48:38] dabomstew then after it's done, the game normally returns straight away
[10:48:49] dabomstew basically you just hijacked that so it doesn't return straight away
[10:48:57] dabomstew and you're not in the DMA time period anymore, so you can do whatever you want
[10:49:08] dabomstew provided you let the "dec a" bit happen
[10:49:14] dabomstew removing that is not allowed

Re: Pokemon Blue plays itself (TPP)

Posted by: Torchickens
Date: 2015-01-01 16:00:39
This works on BizHawk too.

I wasn't paying attention, but at 00:09 in-game time the game reset so the random buttons effect no longer occurred. Would it be viable to disable soft-resetting? You'd have to make the game not set FFF8 to 0F. I don't know for sure if it was a soft-reset though.

Edit: In other news, I did this on my real Game Boy again. Red hasn't left the house yet xD. I got a soft reset eventually like what happened on Bizhawk.

Re: Pokemon Blue plays itself (TPP)

Posted by: luckytyphlosion
Date: 2015-01-01 16:45:26
The easiest way to prevent a soft reset is to "res 2,a" so that the select button will never be put into joypad.

Re: Pokemon Blue plays itself (TPP)

Posted by: Torchickens
Date: 2015-01-01 16:52:52

The easiest way to prevent a soft reset is to "res 2,a" so that the select button will never be put into joypad.


Wow, that's nice and easy.

Re: Pokemon Blue plays itself (TPP)

Posted by: luckytyphlosion
Date: 2015-01-01 17:55:05
I'm trying to make the game pick random buttons on a bias (modified version of RNGPlaysPokemon's bias), but I'm not exactly sure how I could create a bias on each button. My current plan was:

16/256 = Start
40/256 = A
24/256 = B
44/256 = Up
44/256 = Down
44/256 = Left
44/256 = Right

However, making each button have that chance of being called won't make each button have that bias. Does anyone know how you could create a bias like that?

EDIT: I've decided to just put the chances of each button being called as:

80/256 = Start
100/256 = A
88/256 = B
108/256 = Up
108/256 = Down
108/256 = Left
108/256 = Right

Now all I need to do is write the ASM. I'm writing it in the Pokemon Daycare data, and adding code to always alternate between Down and B if the player ever enters the daycare. (incase you get the very slim chance of a text box appearing, like repel text or poison text)

EDIT2: So my plan of adding Bias to buttons failed, because I ran out of space before I reached into box addresses. Maybe I could use SRAM, though, but apparently on BGB control panel, there's "Inaccessible VRAM", so maybe I could use that? (as long as it doesn't mean the VRAM won't be displayed on screen, but it can still be used).

Otherwise, I could just shorten the code to divide the chance of start button being called up by 1/2.
(code if anyone wants to know)

wFlags_DA48: ; really just W_DAYCARE_IN_USE, used to alternate the Down and B button presses when in the daycare
db 00
IsInDaycare_DA49:
ld a,(D35E) ; w_curmap
sub a,48 ; are we in the daycare?
or a ; clears carry flag, but keeps the result of a for the incoming nz check
jr nz,.InitRandomButtons ; if not, start random buttons
ld hl,wFlags
inc (hl)
bit 0,(hl)
jr nz,.asm_pressb
ld a,80
jr .scf
.asm_pressb_DA5D
ld a,02
.scf_DA5F
scf
.InitRandomButtons_DA60
ld hl,FFF8
ld (hl),a
res 2,(hl)
ret c

.AButton_DA67
call GetRandom
cp a,9B
jr c,BButton
set 0,(hl)

.BButton_DA6F
call GetRandom
cp a,A7
jr c,.StartButton
set 1,(hl)

.StartButton_DA77
call GetRandom
cp a,AF
jr c,.RightButton
set 3,(hl)

.RightButton_DA7F
call GetRandom
cp a,93
jr c,LeftButton
set 4,(hl)

.LeftButton_DA
call GetRandom
cp a,93
jr c,UpButton
set 5,(hl)

.UpButton
call GetRandom
cp a,93
jr c,DownButton
set 6,(hl)

.DownButton
call GetRandom
cp a,93
ret c
set 7,(hl)
;fallthrough
GetRandom:
ld a,($FF00+04) ; rDiv
ret