First post! Work done until now
Introduction
Hi everyone! This is the first time I make a post (and a blog!) to talk about my projects. This time, I’m trying disassemble Harvest Moon for the original Game Boy.
Harvest Moon is one of the games I usually go when I need to disconnect from long story adventures. The structure of the game is usually the same: starts in a farm, make money and meet NPCs. I don’t exactly know if this specific game is Harvest Moon 1, 2 or 3 (please help me with this!). I saw the first Harvest Moon was released for N64, but my first time playing it was on Harvest Moon DS.
Now it’s time to talk about the process!
Reverse Engineering
Learning about the architecture from Pandocs and playing with assembly gave me an initial idea of how ROMs work. Another useful source is this article by Rodrigo Copetti.
I’ll try to summarize all the discoveries.
Time cycle
I’m using Emulicious as a tool for looking at RAM values, palettes, etc. Using the RAM search functionality in the middle of the farm, I started comparing values that were changing without doing anything. As you might know, in these games being in the farm updates the time while being in buildings freeze it.
1
2
3
4
5
6
00:B880 sTimerSeconds
00:B881 sTimerMinutes
00:B882 sCurrentHour
00:B883 sCurrentDay
00:B884 sCurrentSeason
00:B885 sCurrentYear
I found these variables that matches with the in-game time. the s at the start of each variable is for SRAM. These are variables stored in the cartridge, keeping their values thanks to the battery.
Replacing them in the code reveals new routines:
1
2
3
4
00:0432 NextHour
00:0448 NextDay
00:0461 NextSeason
00:047A NextYear
If we look at the hour change subroutine, it has a very simple logic:
1
2
3
4
5
6
7
8
NextHour::
xor a ; Sets a to 0
ld [sTimerMinutes], a ; Reset minutes
ld a, [sCurrentHour]
inc a
ld [sCurrentHour], a ; Increment the hour and store again
cp $18 ; 24 in decimal
jr nc, NextDay ; If the clock has reached 24 hours, go to the next day
Day, season and year change looks the same, but the value to compare is different.
Money
Now let’s talk about something every farmer cares about: money.
Using the same RAM search approach, I found these SRAM/WRAM addresses:
1
2
3
4
5
6
00:B8EF sCurrentMoneyL
00:B8F0 sCurrentMoneyM
00:B8F1 sCurrentMoneyH
00:CCCA wCurrentMoneyL
00:CCCB wCurrentMoneyM
00:CCCC wCurrentMoneyH
Money is stored across 3 bytes (L/M/H = Low/Mid/High), so it’s likely handled as a 24-bit value rather than a single byte — makes sense, since a game like this can easily have players saving up thousands of coins.
You’ll also notice there are two copies of the same value: one in SRAM (sCurrentMoney*) and one in WRAM (wCurrentMoney*). This is a pretty typical pattern — SRAM keeps the value safe across power-offs thanks to the cartridge battery, while WRAM holds a working copy the game reads/writes during actual gameplay, syncing back to SRAM at some point (probably on save, or maybe every transaction).
On the code side, I found a handful of related routines:
1
2
3
4
5
6
00:10A0 CheckCurrentMoney
00:10C6 LimitMoney
00:10D7 ResetMoney
00:10E1 LoadMoney
1B:46A8 CompareMoney
1B:46B5 DiscountMoney
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
CheckCurrentMoney::
ld a, [sCurrentMoneyH]
cp $ff
jr z, ResetMoney ; if the high byte is $FF, something went wrong -> reset to 0
ld a, [sCurrentMoneyH]
cp $02
jr nc, LimitMoney ; if high byte >= 2, money is over the cap -> clamp it
cp $01
jr c, LoadMoney ; if high byte < 1 (i.e. 0), we're well under the cap -> just load
ld a, [sCurrentMoneyM]
cp $87
jr nc, LimitMoney ; high byte == 1, mid byte >= $87 -> over the cap -> clamp
cp $86
jr c, LoadMoney ; mid byte < $86 -> still under the cap -> load
ld a, [sCurrentMoneyL]
cp $a0
jr nc, LimitMoney ; mid byte == $86, low byte >= $A0 -> over the cap -> clamp
jr LoadMoney ; otherwise we're exactly at the boundary -> load
This is a classic 24-bit bounds check, done byte by byte from most-significant (H) to least-significant (L). And LimitMoney reveals exactly what the cap is:
1
2
3
4
5
6
7
8
LimitMoney::
ld a, $9f
ld [sCurrentMoneyL], a
ld a, $86
ld [sCurrentMoneyM], a
ld a, $01
ld [sCurrentMoneyH], a
jr LoadMoney
Putting those three bytes together — H=$01, M=$86, L=$9F — gives $01869F, which in decimal is exactly 99999. So the money cap is the classic five-nines limit.
There’s also a safety net for the opposite problem (underflow): if the high byte is $FF, CheckCurrentMoney jumps straight to ResetMoney instead of clamping — my guess is this catches the case where a subtraction elsewhere in the code makes the balance go negative and wrap around, so instead of showing garbage the game just resets your money to 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
ResetMoney::
xor a
ld [sCurrentMoneyL], a
ld [sCurrentMoneyM], a
ld [sCurrentMoneyH], a
LoadMoney::
ld a, [sCurrentMoneyH]
ld [wCurrentMoneyH], a
ld a, [sCurrentMoneyM]
ld [wCurrentMoneyM], a
ld a, [sCurrentMoneyL]
ld [wCurrentMoneyL], a
ResetMoney zeroes out all three SRAM bytes and falls through into LoadMoney, which confirms the theory from before: LoadMoney is the sync step that copies the “safe” SRAM value into the WRAM working copy the rest of the game actually reads from during gameplay.
So the full picture for CheckCurrentMoney is: validate the SRAM balance is within [0, 99999], reset to 0 on underflow, clamp to 99999 on overflow, then sync it into WRAM.
After that, I found two routines with the shop logic:
1
2
3
4
5
6
7
8
9
10
11
CompareMoney::
push bc
ld hl, sCurrentMoneyL
ld a, [hl+] ; a = money low byte, hl -> mid byte
sub e ; a = moneyL - priceL
ld a, [hl+] ; a = money mid byte, hl -> high byte
sbc d ; a = moneyM - priceM - borrow
ld a, [hl+] ; a = money high byte
sbc $00 ; a = moneyH - 0 - borrow
pop bc
ret
CompareMoney doesn’t write anything back — it just runs a chained 24-bit subtraction (money minus a 16-bit value passed in de) purely to set the flags. Since the price only needs de (2 bytes), it looks like item prices in this game never exceed 65535, even though the money cap itself is 99999 and needs all 3 bytes. The final sbc $00 only propagates the borrow from the lower bytes into the high byte — it’s not subtracting anything from the price side there. After this call, the carry flag tells you whether money < price (not enough funds), which is exactly what you’d want to check before letting a purchase go through.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DiscountMoney::
push bc
ld b, $00
ld hl, sCurrentMoneyL
ld a, [hl] ; a = money low byte
sub e ; a = moneyL - priceL
ld [hl+], a ; store back, hl -> mid byte
ld a, [hl] ; a = money mid byte
sbc d ; a = moneyM - priceM - borrow
ld [hl+], a ; store back, hl -> high byte
ld a, [hl] ; a = money high byte
sbc b ; a = moneyH - 0 - borrow (b is always 0)
ld [hl], a ; store back
pop bc
ret
DiscountMoney is basically the same 24-bit subtraction as CompareMoney, except this time each intermediate result is written straight back into sCurrentMoneyL/M/H. So this is the routine that actually takes the money away once a purchase is confirmed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
TryToBuyFixedItem::
ld h, b
ld l, c
ld a, [hl+]
ld e, a ; e = price low byte (from the item's data entry, pointed by bc)
ld a, [hl+]
ld d, a ; d = price high byte; hl now points 2 bytes into the entry
push hl ; save that position for later
call CompareMoney
jr nc, BuyFixedItem ; carry clear -> money >= price -> can afford it
pop hl
ld a, $51
call SpawnDialogOrEvent ; $51 = "not enough money" dialog
ld a, [hl+]
ld h, [hl]
ld l, a ; hl = 16-bit value read from the entry, right after the price
ret
1
2
3
4
5
6
7
8
BuyFixedItem::
call DiscountMoney ; de still holds the price -> subtract it
ld a, $42
call SpawnDialogOrEvent ; $42 = "purchase complete" dialog
pop hl
inc hl
inc hl ; skip the same 2-byte field, advance to the next table entry
ret
There is a lot more I could talk about these routines and other discoverments in this post, but I think it’s enough for a first entry. I hope you like it!