Advertisement
mhughson

6502 Math Macros

May 8th, 2024
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; params:   addr_l - address of low byte of 16 bit value
  2. ;           addr_h - address of high byte of 16 bit value
  3. ;           val - literal to add
  4. ;           x reg - assumes x is index into addresses
  5. ; note:     stomps A register
  6. .macro add16x8const    addr_l, addr_h, val
  7.     clc
  8.     lda addr_l,x
  9.     adc val
  10.     sta addr_l,x
  11.     bcc :+
  12.         inc addr_h,x
  13.     :
  14. .endmacro
  15.  
  16. ; params:   addr_left_l/h - address of low/high byte that is being changing
  17. ;           addr_right_l/h - address of the low/high byte being added to l-value.
  18. ;           x reg - assumes x is index into addresses
  19. ; note:     stomps A register
  20. .macro add16x16    addr_left_l, addr_left_h, addr_right_l, addr_right_h
  21.     ; low byte
  22.     clc
  23.     lda addr_left_l,x
  24.     adc addr_right_l,x
  25.     sta addr_left_l,x
  26.     ; high byte
  27.     lda addr_left_h,x
  28.     adc addr_right_h,x
  29.     sta addr_left_h,x
  30. .endmacro
  31.  
  32. ; params:   addr_left_l/h/hh - address of low/high/xtrahigh byte that is being changing
  33. ;           addr_right_l/h - address of the low/high byte being added to l-value.
  34. ;           x reg - assumes x is index into addresses
  35. ; note:     stomps A register
  36. .macro add24x16    addr_left_l, addr_left_h, addr_left_hh, addr_right_l, addr_right_h
  37.     ; low byte
  38.     clc
  39.     lda addr_left_l,x
  40.     adc addr_right_l,x
  41.     sta addr_left_l,x
  42.     ; high byte
  43.     lda addr_left_h,x
  44.     adc addr_right_h,x
  45.     sta addr_left_h,x
  46.     ; extra high byte
  47.     bcc :+
  48.         inc addr_left_hh,x
  49.     :
  50. .endmacro
  51.  
  52. ; Adds an signed 16 bit number to an unsigned 24 bit number
  53. ; params:   addr_left_l/h/hh - address of low/high/xtrahigh byte that is being changing
  54. ;           addr_right_l/h - address of the low/high byte being added to l-value.
  55. ;           x reg - assumes x is index into addresses
  56. ; note:     stomps A register
  57. .macro add24xs16 addr_left_l, addr_left_h, addr_left_hh, addr_right_l, addr_right_h
  58.     clc
  59.     lda addr_right_l,x
  60.     adc addr_left_l,x
  61.     sta addr_left_l,x
  62.     lda addr_right_h,x
  63.     bpl :+
  64.         dec addr_left_hh,x  ; adjust if r-value is negative
  65.     :
  66.     adc addr_left_h,x
  67.     sta addr_left_h,x
  68.     bcc :+
  69.         inc addr_left_hh,x  ; adjust hh if carry
  70.     :
  71. .endmacro
Tags: nesev
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement