Skip to content

Commit 8689f2b

Browse files
committed
Added the op-size function. Only process left to go
1 parent 55f21e1 commit 8689f2b

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/clj_dcpu16/core.clj

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,21 @@
6969
"retrieves the b parameter from the word"
7070
(mask-and-shift 0xFC00 10))
7171

72+
(defn- between
73+
"Determine if n is x <= n <= y"
74+
[n x y]
75+
(and (<= x n) (>= y n)))
76+
7277
(defn process
7378
[word])
7479

75-
(defn op-size [pc])
80+
(defn op-size
81+
"Given a word, calculate how many words the
82+
next instruction will consume and return the jump distance"
83+
[pc]
84+
(let [params [(get-a pc) (get-b pc)]]
85+
(apply + 1 (map #(if (or (between % 0x10 0x17)
86+
(between % 0x1e 0x1f)) 1 0) params))))
7687

7788
(defmulti execute get-o)
7889
(defmethod execute 0x0 [word]
@@ -153,33 +164,33 @@
153164
pc (get-memory :pc)]
154165
(if (= a b)
155166
(inc-memory :pc)
156-
(change-memory :pc (+ pc 1 (op-size (inc pc)))))))
167+
(change-memory :pc (+ pc 1 (op-size (follow-memory (inc pc))))))))
157168
(defmethod execute 0xd [word]
158169
;; IFN execute next instruction iff a!=b
159170
(let [[a b out] (process word)
160171
pc (get-memory :pc)]
161172
(if (not= a b)
162173
(inc-memory :pc)
163-
(change-memory :pc (+ pc 1 (op-size (inc pc)))))))
174+
(change-memory :pc (+ pc 1 (op-size (follow-memory (inc pc))))))))
164175
(defmethod execute 0xe [word]
165176
;; IFG execute next instruction iff a>b
166177
(let [[a b out] (process word)
167178
pc (get-memory :pc)]
168179
(if (> a b)
169180
(inc-memory :pc)
170-
(change-memory :pc (+ pc 1 (op-size (inc pc)))))))
181+
(change-memory :pc (+ pc 1 (op-size (follow-memory (inc pc))))))))
171182
(defmethod execute 0xf [word]
172183
;; IFB execute next instruction iff (a&b)!= 0
173184
(let [[a b out] (process word)
174185
pc (get-memory :pc)]
175186
(if (not= 0 (bit-and a b))
176187
(inc-memory :pc)
177-
(change-memory :pc (+ pc 1 (op-size (inc pc)))))))
188+
(change-memory :pc (+ pc 1 (op-size (follow-memory (inc pc))))))))
178189

179190
(defn run!
180191
"Start execution at 0x0000 unless specified"
181192
([pc]
182-
(set-memory pc)
193+
(change-memory :pc pc)
183194
(run!))
184195
([]
185196
(while true

0 commit comments

Comments
 (0)