EarthBound/ASM/Memory And String Util Routines

From Data Crystal
< EarthBound
Revision as of 19:17, 30 May 2023 by Esorth (talk | contribs) (Describe more routines)
Jump to navigation Jump to search

This is a sub-page of EarthBound/ASM.

$C08ED2-$C08EEC: 16-bit memcpy

memcpy(a, $0e..$10, x)

Copy memory similarly to the C memcpy function. Memory is copied in 16-bit words, ignoring the final byte if an odd length is requested. Implemented using trivial repeated LDA and STA calls.

Note: Unclear why EarthBound uses this routine instead of using the typically faster MVN or MVP CPU instructions.

Inputs

  • x: Num bytes to copy. Because routine only copies in 16-bit words, only copies x - 1 if x % 2 == 1
  • accumulator: Destination immediate pointer
  • $0e..$10: Source long pointer

$C08EED-$C08EFB: 8-bit memcpy

memcpy($0e..$10, $12..$15, a)

Copy memory similarly to the C memcpy function. Memory is copied byte-by-byte. Implemented using trivial repeated LDA and STA calls with the accumulator set to 8-bit mode.

Note: Unclear why EarthBound uses this routine instead of using the typically faster MVN or MVP CPU instructions.

Inputs

  • accumulator: Num bytes to copy
  • $0e..$10: Destination long pointer
  • $12..$15: Source long pointer

$C08EFC-$C08F14: 16-bit memset

memset(a, $0e, x)

Fill a block of memory with a given 8-bit value, similarly to the C memset function. Memory is set in 16-bit words, ignoring the final byte if an odd length is requested. Implemented by filling both bytes of the accumulator with the input byte value and then using trivial repeated STA calls.

Note: Unclear why EarthBound uses this routine instead of applying the typically faster MVN or MVP CPU instructions to overlapping memory regions.

Inputs

  • x: Num bytes to fill. Because routine only fills in 16-bit words, only fills x - 1 if x % 2 == 1
  • accumulator: Memory start immediate pointer
  • $0e: 8-bit value

$C08F15-$C08F21: 8-bit memset

memset($0e..$10, a, x)

Fill a block of memory with a given 8-bit value, similarly to the C memset function. Memory is set byte-by-byte. Implemented using trivial repeated STA calls with the accumulator set to 8-bit mode.

Note: Unclear why EarthBound uses this routine instead of applying the typically faster MVN or MVP CPU instructions to overlapping memory regions.

Inputs

  • x: Num bytes to fill
  • $0e..$10: Memory start long pointer
  • accumulator[0]: 8-bit value

$C08F22-$C08F2E: strlen

strlen($0e..$10)

Count number of bytes in a memory block before a #$00 value, not including the #$00 byte. Used to find the length of a null-terminated string, similarly to the C strlen function. Implemented trivially by incrementing an index register until LDA instructions using the index loads zero.

Warning: Because an 8-bit register is used to count string bytes, will loop and hang if the input string does not contain a zero byte within the first 256 bytes.

Inputs

  • $0e..$10: Memory start long pointer

Outputs

  • accumulator[0]: 8-bit length (num bytes)

$C08F2F-$C08F41: strcmp(ish)

strcmpish($0e..$10, $12..$14)

Determine if one string (s1) is the same as the start to another string (s2). Implementing by comparing the strings byte-by-byte until the end (#$00 byte) of s1 is found or the compared bytes of s1 and s2 are not equal.

Note: Behaves quite differently from the C strcmp function. Only compares input strings for the length of s1 and has different return values.

Inputs

  • $0e..$10: s1 long pointer
  • $12..$14: s2 long pointer

Outputs

  • accumulator[0]: #$00 if s2 begins with s1, otherwise #$01

$C08F42-$C08F67: Copy CPU state to memory

$C08F68-$C08F8A: Load CPU state from memory

So very stubbly.
This page is rather stubbly and could use some expansion.
Are you a bad enough dude to rescue this article?