Mercurial > vba-clojure
diff clojure/com/aurellem/gb/mem_util.clj @ 346:5639312a393f
Saving progress. Cleaned up documentation a little; found more ROM stuff, including names of status ailments and amount of HP restored by potions.
author | Dylan Holmes <ocsenave@gmail.com> |
---|---|
date | Sun, 08 Apr 2012 01:28:39 -0500 |
parents | 3d4f60b4a4af |
children |
line wrap: on
line diff
1.1 --- a/clojure/com/aurellem/gb/mem_util.clj Sun Apr 08 08:43:52 2012 -0500 1.2 +++ b/clojure/com/aurellem/gb/mem_util.clj Sun Apr 08 01:28:39 2012 -0500 1.3 @@ -190,3 +190,43 @@ 1.4 1.5 (defn restore-rom! [] (write-rom! original-rom)) 1.6 1.7 + 1.8 +(defn endian-flip 1.9 + "Flip the bytes of the two-byte number." 1.10 + [n] 1.11 + (assert (< n 0xFFFF)) 1.12 + (+ (* 0x100 (rem n 0x100)) 1.13 + (int (/ n 0x100)))) 1.14 + 1.15 + 1.16 +(defn offset->ptr 1.17 + "Convert an offset into a little-endian pointer." 1.18 + [n] 1.19 + (-> 1.20 + n 1.21 + (rem 0x10000) ;; take last four bytes 1.22 + (rem 0x4000) ;; get relative offset from the start of the bank 1.23 + (+ 0x4000) 1.24 + endian-flip)) 1.25 + 1.26 +(defn offset->bank 1.27 + "Get the bank of the offset." 1.28 + [n] 1.29 + (int (/ n 0x4000))) 1.30 + 1.31 +(defn ptr->offset 1.32 + "Convert a two-byte little-endian pointer into an offset." 1.33 + [bank ptr] 1.34 + (-> 1.35 + ptr 1.36 + endian-flip 1.37 + (- 0x4000) 1.38 + (+ (* 0x4000 bank)) 1.39 + )) 1.40 + 1.41 +(defn same-bank-offset 1.42 + "Convert a ptr into an absolute offset by using the bank of the reference." 1.43 + [reference ptr] 1.44 + (ptr->offset 1.45 + (offset->bank reference) 1.46 + ptr)) 1.47 \ No newline at end of file