diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/copy2.asm | 134 | ||||
-rw-r--r-- | common/math.asm | 76 |
2 files changed, 210 insertions, 0 deletions
diff --git a/common/copy2.asm b/common/copy2.asm new file mode 100644 index 000000000..bdfbe9fdf --- /dev/null +++ b/common/copy2.asm @@ -0,0 +1,134 @@ +CopyBytes: ; 0x3026 +; copy bc bytes from hl to de + inc b ; we bail the moment b hits 0, so include the last run + inc c ; same thing; include last byte + jr .HandleLoop +.CopyByte + ld a, [hli] + ld [de], a + inc de +.HandleLoop + dec c + jr nz, .CopyByte + dec b + jr nz, .CopyByte + ret + +SwapBytes: ; 0x3034 +; swap bc bytes between hl and de +.Loop + ; stash [hl] away on the stack + ld a, [hl] + push af + + ; copy a byte from [de] to [hl] + ld a, [de] + ld [hli], a + + ; retrieve the previous value of [hl]; put it in [de] + pop af + ld [de], a + inc de + + ; handle loop stuff + dec bc + ld a, b + or c + jr nz, .Loop + ret + +ByteFill: ; 0x3041 +; fill bc bytes with the value of a, starting at hl + inc b ; we bail the moment b hits 0, so include the last run + inc c ; same thing; include last byte + jr .HandleLoop +.PutByte + ld [hli], a +.HandleLoop + dec c + jr nz, .PutByte + dec b + jr nz, .PutByte + ret + +GetFarByte: ; 0x304d +; retrieve a single byte from a:hl, and return it in a. + ; bankswitch to new bank + ld [hBuffer], a + ld a, [hROMBank] + push af + ld a, [hBuffer] + rst Bankswitch + + ; get byte from new bank + ld a, [hl] + ld [hBuffer], a + + ; bankswitch to previous bank + pop af + rst Bankswitch + + ; return retrieved value in a + ld a, [hBuffer] + ret + +GetFarHalfword: ; 0x305d +; retrieve a halfword from a:hl, and return it in hl. + ; bankswitch to new bank + ld [hBuffer], a + ld a, [hROMBank] + push af + ld a, [hBuffer] + rst Bankswitch + + ; get halfword from new bank, put it in hl + ld a, [hli] + ld h, [hl] + ld l, a + + ; bankswitch to previous bank and return + pop af + rst Bankswitch + ret +; 0x306b + +FarCopyWRAM: ; 306b + ld [hBuffer], a + ld a, [rSVBK] + push af + ld a, [hBuffer] + ld [rSVBK], a + call CopyBytes + pop af + ld [rSVBK], a + ret +; 307b + +GetFarWRAMByte: ; 307b + ld [hBuffer], a + ld a, [rSVBK] + push af + ld a, [hBuffer] + ld [rSVBK], a + ld a, [hl] + ld [hBuffer], a + pop af + ld [rSVBK], a + ld a, [hBuffer] + ret +; 308d + +GetFarWRAMWord: ; 308d + ld [hBuffer], a + ld a, [rSVBK] + push af + ld a, [hBuffer] + ld [rSVBK], a + ld a, [hli] + ld h, [hl] + ld l, a + pop af + ld [rSVBK], a + ret +; 309d + diff --git a/common/math.asm b/common/math.asm new file mode 100644 index 000000000..87a00c412 --- /dev/null +++ b/common/math.asm @@ -0,0 +1,76 @@ +SimpleMultiply: ; 3105 +; Return a * c. + and a + ret z + + push bc + ld b, a + xor a +.loop + add c + dec b + jr nz, .loop + pop bc + ret +; 3110 + + +SimpleDivide: ; 3110 +; Divide a by c. Return quotient b and remainder a. + ld b, 0 +.loop + inc b + sub c + jr nc, .loop + dec b + add c + ret +; 3119 + + +Multiply: ; 3119 +; Multiply hMultiplicand (3 bytes) by hMultiplier. Result in hProduct. +; All values are big endian. + push hl + push bc + + callab _Multiply + + pop bc + pop hl + ret +; 3124 + + +Divide: ; 3124 +; Divide hDividend length b (max 4 bytes) by hDivisor. Result in hQuotient. +; All values are big endian. + push hl + push de + push bc + ld a, [hROMBank] + push af + ld a, BANK(_Divide) + rst Bankswitch + + call _Divide + + pop af + rst Bankswitch + pop bc + pop de + pop hl + ret +; 3136 + + +SubtractSigned: ; 3136 +; Return a - b, sign in carry. + sub b + ret nc + cpl + add 1 + scf + ret +; 313d + |