view clojure/com/aurellem/gb/rlm_assembly.clj @ 382:3c24216e0080

decreased frame-metronome by one opcode
author Robert McIntyre <rlm@mit.edu>
date Wed, 11 Apr 2012 13:17:08 -0500
parents 1bfa43d35403
children 9eae7e914bf0
line wrap: on
line source
1 (ns com.aurellem.gb.rlm-assembly
2 "Version of main bootstrap program that is valid output for the
3 item-writer program."
4 (:use (com.aurellem.gb gb-driver assembly util vbm constants))
5 (:use (com.aurellem.run bootstrap-1))
6 (:import [com.aurellem.gb.gb_driver SaveState]))
8 ;; Specs for Main Bootstrap Program
10 ;; Number-Input
11 ;; Number input works using all eight buttons to
12 ;; spell out an 8 bit number. The order of buttons is
13 ;; [:d :u :l :r :start :select :b :a] --> 11111111
14 ;; [ :l :start :a] --> 00101001
16 ;;; MODE-SELECT
17 ;; The bootstrap program starts in MODE-SELECT mode.
18 ;; MODE-SELECT transitions to one of three modes depending
19 ;; on which buttons are pressed:
20 ;; 0 (no-buttons) : MODE-SELECT
21 ;; 8 [:start] : WRITE-BYTES
22 ;; 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 ->signed-8-bit [n]
63 (if (< n 0)
64 (+ 256 n) n))
66 0x01
67 0x44
68 0xFF
69 inc C
70 inc D
72 (defn frame-metronome []
73 (let [timing-loop
74 [0x01
75 0x43
76 0xFE ;; load 0xFF44 into BC without repeats
77 0x0C
78 0x04
80 0x0A] ;; (BC) -> A, now A = LY (vertical line coord)
81 continue-if-144
82 [0xFE
83 144 ;; compare LY (in A) with 144
84 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)
85 (->signed-8-bit
86 (+ -4 (- (count timing-loop))))]
87 spin-loop
88 [0x05 ;; dec B, which is 0xFF
89 0x20 ;; spin until B==0
90 0xFD]]
91 (concat timing-loop continue-if-144 spin-loop)))
94 (defn test-frame-metronome
95 "Ensure that frame-metronome ticks exactly once every frame."
96 ([] (test-frame-metronome 151))
97 ([steps]
98 (let [inc-D [0x14 0x18
99 (->signed-8-bit
100 (+ -3 (- (count (frame-metronome)))))]
101 program (concat (frame-metronome) inc-D)
102 count-frames
103 (-> (tick (mid-game))
104 (IE! 0)
105 (DE! 0)
106 (set-memory-range pokemon-list-start program)
107 (PC! pokemon-list-start))
108 D-after-moves (D (run-moves count-frames (repeat steps [])))]
109 (println "D:" D-after-moves)
110 (assert (= steps D-after-moves))
112 (println "D =" D-after-moves "after" steps "steps")
113 count-frames)))
115 (defn main-bootstrap-program [start-address]
116 (let [[start-high start-low] (disect-bytes-2 start-address)
117 ]
118 ))
129 ;;;;;; TESTS ;;;;;;
131 (defn bootstrap-base []
132 (let [program (main-bootstrap-program pokemon-list-start)]
133 ;; make sure program is valid output for item-writer
134 (bootstrap-pattern program)
135 (-> (tick (mid-game))
136 (set-memory-range pokemon-list-start program)
137 (PC! pokemon-list-start))))
139 (defn test-write-bytes-mode []
140 (let [target-address 0xC00F
141 [target-high target-low] (disect-bytes-2 target-address)
142 assembly [0xF3 0x18 0xFE 0x12]
143 get-mem-region #(subvec (vec (memory %))
144 target-address (+ target-address 20))
145 before (bootstrap-base)
146 after
147 (-> before
148 (step []) ; make sure it can handle blanks
149 (step []) ; at the beginning.
150 (step [])
151 (step [:start]) ; select WRITE-BYTES mode
152 (step (buttons 4)) ; write 4 bytes
153 (step (buttons target-high))
154 (step (buttons target-low))
155 (step (buttons (nth assembly 0)))
156 (step (buttons (nth assembly 1)))
157 (step (buttons (nth assembly 2)))
158 (step (buttons (nth assembly 3)))
159 (step [])
160 (step [])
161 (step []))]
162 (println "before :" (get-mem-region before))
163 (println "after :" (get-mem-region after))
164 (assert (= assembly (take 4 (get-mem-region after))))
165 after))
167 (defn test-jump-mode []
168 (let [target-address 0xC00F
169 [target-high target-low] (disect-bytes-2 target-address)
170 post-jump
171 (-> (test-write-bytes-mode)
172 (step [])
173 (step [])
174 (step [])
175 (step (buttons 0xFF)) ; Select JUMP mode.
176 (step (buttons target-high))
177 (step (buttons target-low)))
178 program-counters
179 (capture-program-counter
180 post-jump
181 10000)]
182 (println program-counters)
183 (assert (contains? (set program-counters) target-address))
184 post-jump))