Mercurial > vba-clojure
view clojure/com/aurellem/assembly.clj @ 108:d3c0b990e163
fix typo
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 15 Mar 2012 03:40:40 -0500 |
parents | ad96e9464d6a |
children | c8b48102b17c |
line wrap: on
line source
1 (ns com.aurellem.assembly2 (:use (com.aurellem gb-driver vbm title items))3 (:import [com.aurellem.gb_driver SaveState]))5 (defn mid-game []6 (read-state "mid-game"))8 (defn inject-assembly9 ([^SaveState state10 program-counter registers11 assembly-code]12 (let [scratch-memory (memory state)]13 ;; inject assembly code14 (dorun (map (fn [index val]15 (aset scratch-memory index val))16 (range program-counter17 (+ program-counter (count assembly-code)))18 assembly-code))19 (-> state20 (write-memory! scratch-memory)21 (write-registers! registers)22 (PC! program-counter))))23 ;;([program-counter]24 ;; (fn [^SaveState state registers assembly-code]25 ;; (inject-assembly state program-counter registers assembly-code)))26 )29 ;;(def inject-assembly-item30 ;; (inject-assembly (inc item-list-start)))32 (defn inject-item-assembly33 ([^SaveState state assembly-code]34 (inject-assembly state (inc item-list-start)35 (registers state)36 assembly-code))37 ([assembly-code]38 (inject-item-assembly @current-state assembly-code)))40 (defn info41 ([^SaveState state]42 (println (format "PC: 0x%04X" (PC state)))43 (println "Instruction:"44 (format "0x%02X" (aget (memory state) (PC state))))45 state))47 (defn print-interrupt48 [^SaveState state]49 (println (format "IE: %d" (IE state)))50 state)52 (defn run-assembly53 ([info-fn assembly n]54 (let [final-state55 (reduce (fn [state _]56 (tick (info-fn state)))57 (inject-item-assembly58 (mid-game) assembly)59 (range n))]60 final-state))61 ([assembly n]62 (run-assembly info assembly n)))65 (def buttons-port 0xFF00)67 (defn view-register [state name reg-fn]68 (println (format "%s : 0x%02X" name (reg-fn state)))69 state)71 (defn A [state]72 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))74 (defn view-memory [state mem]75 (println (format "mem 0x%04X = 0x%02X" mem (aget (memory state) mem)))76 state)78 (defn read-buttons []79 (let [button-pressed (tick (step (mid-game) [:d]))]80 (-> button-pressed81 (IE! 0)82 (inject-item-assembly83 (concat84 ;; write 00010000 to 0xFF00 to select joypad85 [0x18 ;D31D ; jump over86 0x01 ;D31E ; the next 8 bits87 (Integer/parseInt "00010000" 2) ;D31F data section88 0x00 ;D320 ; take a break89 0xFA ;D321 ; put DC15 into A90 0x1F ;D322 --]91 0xD3 ;D323 --] DC1F ; data section92 0x01 ;D324 ; load 0xFF00 into BC93 0x00 ;D32594 0xFF ;D32695 0x02 ;D327 ; load (00010000) into FF0096 ; to select directional97 ; buttons99 ]101 []))102 (info)103 (tick) ;; skip over data section104 (info)105 (tick) ;; no-op106 (info)107 (view-register "Register A" A)108 (tick) ;; load-data into A109 (view-register "Register A" A)110 (info)111 (view-register "Register BC" BC)112 (tick) ;; store 0xFFOO into BC113 (view-register "Register BC" BC)114 (info)115 (view-memory 0xFF00)116 (tick) ;; load A into 0xFF00117 (view-memory 0xFF00)118 )))121 (defn trace [state]122 (loop [program-counters []123 opcodes []]124 (let [frame-boundary?125 (com.aurellem.gb.Gb/tick)]126 (println (count opcodes))127 (if frame-boundary?128 [program-counters opcodes]129 (recur130 (conj program-counters131 (first (registers @current-state)))132 (conj opcodes133 (aget (memory @current-state)134 (PC @current-state))))))))137 (defn good-trace []138 (-> (mid-game) (tick) (IE! 0)139 (set-inv-mem [0x00 0x00 0X00 0x00])140 (PC! item-list-start)(print-interrupt)141 (info) (tick) (info) (tick) (info)))