Multibyte constant

From NESdev Wiki
Revision as of 23:57, 16 November 2013 by Tepples (talk | contribs) (0xBEE52 is hex. 0xACTION52 is not unless it equals 0xFECE5.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

This macro allows including long hexadecimal constants on one line, like an arbitrary extension of the .dbyt (2 byte big endian constant). <source>

mbyt.s
Multibyte constant macro for ca65
Copyright 2013 Damian Yerrick
Copying and distribution of this file, with or without
modification, are permitted in any medium without royalty provided
the copyright notice and this notice are preserved in any source
code copies. This file is offered as-is, without any warranty.

.macro mbyt_hex2nibs highnib, lownib .local highdig, lowdig

 ; "dec0de" the hex nibbles
 .if highnib >= 'A' && highnib <= 'F'
   highdig = highnib - 'A' + 10
 .elseif highnib >= 'a' && highnib <= 'f'
   highdig = highnib - 'a' + 10
 .elseif highnib >= '0' && highnib <= '9'
   highdig = highnib - '0'
 .endif
 .if lownib >= 'A' && lownib <= 'F'
   lowdig = lownib - 'A' + 10
 .elseif lownib >= 'a' && lownib <= 'f'
   lowdig = lownib - 'a' + 10
 .elseif lownib >= '0' && lownib <= '9'
   lowdig = lownib - '0'
 .endif
 .byte highdig * $10 + lowdig

.endmacro

.macro mbyt_rgn inbytes, ifrom, ito

 ; Subdivide region into two, for O(log n) nested macros
 ; to make it more "5ca1ab1e"
 .if (ito - ifrom) > 2
   .local imid
   imid = (ifrom + (ito - ifrom) / 4 * 2)
   mbyt_rgn inbytes, ifrom, imid
   mbyt_rgn inbytes, imid, ito
 .elseif ito > ifrom
   mbyt_hex2nibs {.strat(inbytes, ifrom)}, {.strat(inbytes, ifrom + 1)}
 .endif

.endmacro

.macro mbyt inbytes

 .local len
 len = .strlen(inbytes)
 mbyt_rgn inbytes, 0, len

.endmacro

use it like this
mbyt "ba5eba11"

</source>

External links