Advertisement
NovaYoshi

mbyt.s

Jul 3rd, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; mbyt.s
  2. ; Multibyte constant macro for ca65
  3. ;
  4. ; Copyright 2013 Damian Yerrick
  5. ; Copying and distribution of this file, with or without
  6. ; modification, are permitted in any medium without royalty provided
  7. ; the copyright notice and this notice are preserved in all source
  8. ; code copies.  This file is offered as-is, without any warranty.
  9. ;
  10. .macro mbyt_hex2nibs highnib, lownib
  11. .local highdig, lowdig
  12.   ; "dec0de" the hex nibbles
  13.   .if highnib >= 'A' && highnib <= 'F'
  14.     highdig = highnib - 'A' + 10
  15.   .elseif highnib >= 'a' && highnib <= 'f'
  16.     highdig = highnib - 'a' + 10
  17.   .elseif highnib >= '0' && highnib <= '9'
  18.     highdig = highnib - '0'
  19.   .endif
  20.   .if lownib >= 'A' && lownib <= 'F'
  21.     lowdig = lownib - 'A' + 10
  22.   .elseif lownib >= 'a' && lownib <= 'f'
  23.     lowdig = lownib - 'a' + 10
  24.   .elseif lownib >= '0' && lownib <= '9'
  25.     lowdig = lownib - '0'
  26.   .endif
  27.   .byte highdig * $10 + lowdig
  28.   ;.out .sprintf(".byte %02x", highdig * $10 + lowdig)
  29. .endmacro
  30.  
  31. .macro mbyt inbytes
  32.   ; thanks to thefox who recommended .set
  33.   .local pos, nib
  34.   pos .set 0
  35.   .repeat .strlen(inbytes)
  36.     .if pos < .strlen(inbytes)
  37.       nib .set .strat(inbytes, pos)
  38.       ; these characters can be used as separators
  39.       .if (nib = ' ' || nib = ',' || nib = '$' || nib = '_')
  40.         pos .set pos + 1
  41.       .else
  42.         mbyt_hex2nibs nib, {.strat(inbytes, pos + 1)}
  43.         pos .set pos + 2
  44.       .endif
  45.     .endif
  46.   .endrepeat
  47. .endmacro
  48.  
  49. ; use it like this:
  50. ; mbyt "09F91102 9D74E35B D84156C5 635688C0"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement