EarthBound/ASM/Memory And String Util Routines: Difference between revisions

From Data Crystal
Jump to navigation Jump to search
(Start creating page)
 
(Describe more routines)
Line 15: Line 15:
==<tt>$C08EED-$C08EFB</tt>: 8-bit memcpy==
==<tt>$C08EED-$C08EFB</tt>: 8-bit memcpy==
<code>memcpy($0e..$10, $12..$15, a)</code>
<code>memcpy($0e..$10, $12..$15, a)</code>
Copy memory similarly to the [https://cplusplus.com/reference/cstring/memcpy/ C memcpy function]. Memory is copied byte-by-byte. Implemented using trivial repeated <tt>LDA</tt> and <tt>STA</tt> calls with the accumulator set to 8-bit mode.
''Note: Unclear why EarthBound uses this routine instead of using the typically faster <tt>MVN</tt> or <tt>MVP</tt> CPU instructions.''
===Inputs===
* <tt>accumulator</tt>: Num bytes to copy
* <tt>$0e..$10</tt>: Destination long pointer
* <tt>$12..$15</tt>: Source long pointer


==<tt>$C08EFC-$C08F14</tt>: 16-bit memset==
==<tt>$C08EFC-$C08F14</tt>: 16-bit memset==
<code>memset(a, $0e, x)</code>
<code>memset(a, $0e, x)</code>
Fill a block of memory with a given 8-bit value, similarly to the [https://cplusplus.com/reference/cstring/memset/ 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 <tt>STA</tt> calls.
''Note: Unclear why EarthBound uses this routine instead of applying the typically faster <tt>MVN</tt> or <tt>MVP</tt> CPU instructions to overlapping memory regions.''
===Inputs===
* <tt>x</tt>: Num bytes to fill. Because routine only fills in 16-bit words, only fills <tt>x - 1</tt> if <tt>x % 2 == 1</tt>
* <tt>accumulator</tt>: Memory start immediate pointer
* <tt>$0e</tt>: 8-bit value


==<tt>$C08F15-$C08F21</tt>: 8-bit memset==
==<tt>$C08F15-$C08F21</tt>: 8-bit memset==
<code>memset($0e..$10, a, x)</code>
<code>memset($0e..$10, a, x)</code>
Fill a block of memory with a given 8-bit value, similarly to the [https://cplusplus.com/reference/cstring/memset/ C memset function]. Memory is set byte-by-byte. Implemented using trivial repeated <tt>STA</tt> calls with the accumulator set to 8-bit mode.
''Note: Unclear why EarthBound uses this routine instead of applying the typically faster <tt>MVN</tt> or <tt>MVP</tt> CPU instructions to overlapping memory regions.''
===Inputs===
* <tt>x</tt>: Num bytes to fill
* <tt>$0e..$10</tt>: Memory start long pointer
* <tt>accumulator[0]</tt>: 8-bit value


==<tt>$C08F22-$C08F2E</tt>: strlen==
==<tt>$C08F22-$C08F2E</tt>: strlen==
<code>strlen($0e..$10)</code>
<code>strlen($0e..$10)</code>
Count number of bytes in a memory block before a <tt>#$00</tt> value, not including the <tt>#$00</tt> byte. Used to find the length of a null-terminated string, similarly to the [https://cplusplus.com/reference/cstring/strlen/ C strlen function]. Implemented trivially by incrementing an index register until <tt>LDA</tt> 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===
* <tt>$0e..$10</tt>: Memory start long pointer
===Outputs===
* <tt>accumulator[0]</tt>: 8-bit length (num bytes)


==<tt>$C08F2F-$C08F41</tt>: strcmp(ish)==
==<tt>$C08F2F-$C08F41</tt>: strcmp(ish)==
<code>strcmpish($0e..$10, $12..$14)</code>
<code>strcmpish($0e..$10, $12..$14)</code>
Determine if one string (<tt>s1</tt>) is the same as the start to another string (<tt>s2</tt>). Implementing by comparing the strings byte-by-byte until the end (<tt>#$00</tt> byte) of <tt>s1</tt> is found or the compared bytes of <tt>s1</tt> and <tt>s2</tt> are not equal.
''Note: Behaves quite differently from the [https://cplusplus.com/reference/cstring/strcmp/ C strcmp function]. Only compares input strings for the length of <tt>s1</tt> and has different return values.''
===Inputs===
* <tt>$0e..$10</tt>: <tt>s1</tt> long pointer
* <tt>$12..$14</tt>: <tt>s2</tt> long pointer
===Outputs===
* <tt>accumulator[0]</tt>: <tt>#$00</tt> if <tt>s2</tt> begins with <tt>s1</tt>, otherwise <tt>#$01</tt>


==<tt>$C08F42-$C08F67</tt>: Copy CPU state to memory==
==<tt>$C08F42-$C08F67</tt>: Copy CPU state to memory==

Revision as of 19:17, 30 May 2023

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?