view clojure/com/aurellem/gb/dylan_assembly.clj @ 605:54644b08da1a

rendered final video.
author Robert McIntyre <rlm@mit.edu>
date Sun, 02 Sep 2012 14:28:53 -0500
parents 1f14c1b8af7e
children
line wrap: on
line source
1 (ns com.aurellem.gb.dylan-assembly
2 "A much more compact version of write-memory-assembly"
3 {:author "Dylan Holmes"}
4 (:use (com.aurellem.gb gb-driver assembly util vbm))
5 (:import [com.aurellem.gb.gb_driver SaveState]))
7 ;; Specs for main bootstrap program
9 ;; Number-Input
10 ;; Number input works using all eight buttons to
11 ;; spell out an 8 bit number. The order of buttons is
12 ;; [:d :u :l :r :start :select :b :a] --> 11111111
13 ;; [ :l :start :a] --> 00101001
15 ;;; MODE-SELECT
16 ;; The bootstrap program starts in MODE-SELECT mode.
17 ;; MODE-SELECT transitions to one of three modes depending
18 ;; on which buttons are pressed:
19 ;; 0 (no-buttons) : MODE-SELECT
20 ;; 8 [:start] : WRITE-BYTES
21 ;; 0xFF (all-buttons) : JUMP
24 ;;; WRITE-BYTES
26 ;; WRITE-BYTES mode writes sequences of arbitray values to
27 ;; arbitray memory locations. It expects you to enter a
28 ;; header of three bytes describing what to write:
30 ;; Byte 0 : Number of Bytes to Write
31 ;; Byte 1 : Start Address High Byte
32 ;; Byte 1 : Start Address Low Byte
34 ;; Then, you enter the number of bytes specified in Byte 0
35 ;; they are written to the start address in
36 ;; sequence. After the last byte is written control
37 ;; returns to MODE-SELECT mode.
39 ;; Example: to write the sequence [1 2 3 4] starting at
40 ;; address 0xC01F enter
41 ;; Byte 0 : 4 (will write four bytes)
42 ;; Byte 1 : 0xC0 (high byte of 0xC01F)
43 ;; Byte 2 : 0x1F (low byte of 0xC01F)
44 ;; Byte 3 : 1 (write 1 to 0xC01F)
45 ;; Byte 4 : 2 (write 2 to 0xC020)
46 ;; Byte 5 : 3 (write 3 to 0xC021)
47 ;; Byte 6 : 4 (write 4 to 0xC022)
49 ;;; JUMP
50 ;; JUMP mode jumps program control to any arbitray
51 ;; location. It expects you to enter two bytes which
52 ;; correspond to the high and low bytes of the memory
53 ;; address to which you want to jump.
54 ;; Byte 0 : Jump Address High Byte
55 ;; Byte 1 : Jump Address Low Byte
57 ;; Example: to jump to address 0x1234 enter
58 ;; Byte 0 : 0x12 (high byte of 0x1234)
59 ;; Byte 1 : 0x34 (low byte of 0x1234)
62 (defn write-memory-assembly-compact
63 "Currently, grabs input from the user each frame."
64 []
65 [
66 ;; --------- FRAME METRONOME
67 0x18 ;; jump ahead to cleanup. first time only.
68 0x40 ;; v-blank-prev [D31E]
70 0xFA ;; load modes into A [D31F]
71 0x41
72 0xFF
74 0x47 ;; A -> B
75 0xCB ;; rotate A
76 0x2F
77 0x2F ;; invert A
79 0xA0
80 0x47 ;; now B_0 contains (VB==1)
82 0xFA ;; load v-blank-prev
83 0x1E
84 0xD3
86 0x2F ;; complement v-blank-prev
88 0xA0 ;; A & B --> A
89 0x4F ;; now C_0 contains increment?
92 0x78 ;; B->A
93 0xEA ;; spit A --> vbprev
94 0x1E
95 0xD3
97 0xCB ;test C_0
98 0x41
99 0x20 ; JUMP ahead to button input if nonzero
100 0x02
101 0x18 ; JUMP back to frame metronome (D31F)
102 0xE7
104 ;; -------- GET BUTTON INPUT
106 ;; btw, C_0 is now 1
107 ;; prepare to select bits
109 0x06 ;; load 0x00 into B
110 0x00 ;; to initialize for "OR" loop
112 0x3E ;; load 0x20 into A, to measure dpad
113 0x20
116 0xE0 ;; load A into [FF00] ;; start of OR loop [D33C]
117 0x00
119 0xF0 ;; load A from [FF00]
120 0x00
122 0xE6 ;; bitmask 00001111
123 0x0F
125 0xB0 ;; A or B --> A
126 0xCB
127 0x41 ;; test bit 0 of C
128 0x28 ;; JUMP forward if 0
129 0x08
131 0x47 ;; A -> B
132 0xCB ;; swap B nybbles
133 0x30
134 0x0C ;; increment C
135 0x3E ;; load 0x10 into A, to measure btns
136 0x10
137 0x18 ;; JUMP back to "load A into [FF00]" [20 steps?]
138 0xED
141 ;; ------ TAKE ACTION BASED ON USER INPUT
143 ;; "input mode"
144 ;; mode 0x00 : select mode
145 ;; mode 0x08 : select bytes-to-write
146 ;; mode 0x10 : select hi-bit
147 ;; mode 0x18 : select lo-bit
149 ;; "output mode"
150 ;; mode 0x20 : write bytes
151 ;; mode 0xFF : jump PC
154 ;; registers
155 ;; D : mode select
156 ;; E : count of bytes to write
157 ;; H : address-high
158 ;; L : address-low
160 ;; now A contains the pressed keys
161 0x2F ; complement A, by request. [D34F]
163 0x47 ; A->B ;; now B contains the pressed keys
164 0x7B ; E->A ;; now A contains the count.
166 0xCB ; test bit 5 of D (are we in o/p mode?)
167 0x6A
168 0x28 ; if test == 0, skip this o/p section
169 0x13 ; JUMP
171 0xCB ; else, test bit 0 of D (fragile; are we in pc mode?)
172 0x42
173 0x28 ; if test == 0, skip the following command
174 0x01
176 ;; output mode I: moving the program counter
177 0xE9 ; ** move PC to (HL)
179 ;; output mode II: writing bytes
180 0xFE ; A compare 0. finished writing?
181 0x00
182 0x20 ; if we are not finished, skip cleanup
183 0x04 ; JUMP
185 ;; CLEANUP
186 ;; btw, A is already zero.
187 0xAF ; zero A [D35F]
188 0x57 ; A->D; makes D=0.
189 0x18 ; end of frame
190 0xBC
192 ;; ---- end of cleanup
195 ;; continue writing bytes
196 0x1D ;; decrement E, the number of bytes to write [D363]
197 0x78 ;; B->A; now A contains the pressed keys
198 0x77 ;; copy A to (HL)
199 0x23 ;; increment HL
200 0x18 ;; end frame. [goto D31F]
201 0xB6 ;; TODO: set skip length backwards
204 ;; ---- end of o/p section
206 ;; i/p mode
207 ;; adhere to the mode discipline:
208 ;; D must be one of 0x00 0x08 0x10 0x18.
210 0x3E ;; load the constant 57 into A. [D369]
211 0x57
212 0x82 ;; add the mode to A
213 0xEA ;; store A into "thing to execute"
214 0x74
215 0xD3
217 0x3E ;; load the constant 8 into A
218 0x08
219 0x82 ;; add the mode to A
221 0x57 ;; store the incremented mode into D
222 0x78 ;; B->A; now A contains the pressed keys
224 0x00 ;; var: thing to execute [D374]
226 0x18 ;; end frame
227 0xA8])
229 (defn write-mem-compact []
230 (-> (tick (mid-game))
231 (IE! 0)
232 (inject-item-assembly (write-memory-assembly-compact))))
234 (defn test-write-bytes-mode []
235 (let [target-address 0xD135
236 [target-high target-low] (disect-bytes-2 target-address)
237 assembly [0xF3 0x18 0xFE 0x12]
238 get-mem-region #(subvec (vec (memory %))
239 target-address (+ target-address 20))
240 before (write-mem-compact)
241 after
242 (-> before
243 (step []) ; make sure it can handle blanks
244 (step []) ; at the beginning.
245 (step [])
246 (step [:start]) ; select WRITE-BYTES mode
247 (step (buttons 4)) ; write 4 bytes
248 (step (buttons target-high))
249 (step (buttons target-low))
250 (step (buttons (nth assembly 0)))
251 (step (buttons (nth assembly 1)))
252 (step (buttons (nth assembly 2)))
253 (step (buttons (nth assembly 3)))
254 (step [])
255 (step [])
256 (step []))]
257 (println "before :" (get-mem-region before))
258 (println "after :" (get-mem-region after))
259 (assert (= assembly (take 4 (get-mem-region after))))
260 after))
262 (defn test-jump-mode []
263 (let [target-address 0xC01F
264 [target-high target-low] (disect-bytes-2 target-address)
265 post-jump
266 (-> (test-write-bytes-mode)
267 (step [])
268 (step [])
269 (step [])
270 (step (buttons 0xFF)) ; Select JUMP mode.
271 (step (buttons target-high))
272 (step (buttons target-low)))
273 program-counters
274 (capture-program-counter
275 post-jump
276 10000)]
277 (println program-counters)
278 (assert (contains? (set program-counters) target-address))
279 post-jump))
282 (defn test-loop []
283 (contains?
284 (set
285 (capture-program-counter
286 (-> (mid-game)
287 ;; (IE! 0)
288 (set-memory-range 0xD135 [0xF3 0x18 0xFE])
289 (PC! 0xD135)) 10000))
290 0xD136))