Skip to content

Commit

Permalink
Merge pull request jjcomer#1 from devn/master
Browse files Browse the repository at this point in the history
Cleanup and removal of no-op argument to set-memory
  • Loading branch information
jjcomer committed Apr 8, 2012
2 parents 9e97c6d + a5990e4 commit 55f21e1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
6 changes: 3 additions & 3 deletions project.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
(defproject cljDCPU "0.1.0-SNAPSHOT" (defproject clj-dcpu16 "0.1.0-SNAPSHOT"
:description "FIXME: write description" :description "an implementation of notch's dcpu16 in clojure"
:url "http://example.com/FIXME" :url "https://github.com/jjcomer/cljDCPU"
:license {:name "Eclipse Public License" :license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"} :url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.3.0"]]) :dependencies [[org.clojure/clojure "1.3.0"]])
58 changes: 28 additions & 30 deletions src/cljDCPU/core.clj → src/clj_dcpu16/core.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,12 +1,30 @@
(ns cljDCPU.core) (ns clj-dcpu16.core)


(def memory (ref {})) (def memory (ref {}))
(def register-conversion {0 :a, 1 :b, 2 :c, 3 :x, 4 :y, 5 :z, 6 :i, 7 :j, 0x1B :sp, 0x1C :pc, 0x1D :o (def register-conversion {0 :a, 1 :b, 2 :c, 3 :x, 4 :y, 5 :z,
6 :i, 7 :j, 0x1B :sp, 0x1C :pc, 0x1D :o,
0x18 :pop, 0x19 :peek, 0x1a :push}) 0x18 :pop, 0x19 :peek, 0x1a :push})


(declare follow-memory) (defn set-memory
(declare inc-memory) "Set the memory address with value. Valid addresses are 0x0 to 0x10000.
(declare dec-memory) As each word is 16 bits, the max value is 0xFFFF. If the provided value
is greater than 0xFFFF the value saved will be truncated"
[f]
(dosync
(alter memory f)))

(defn inc-memory
[address]
(set-memory inc))

(defn dec-memory
[address]
(set-memory dec))

(declare get-memory)
(def follow-memory
"Fetch the value of the memory location which is stored in another memory location"
(comp get-memory get-memory))


(defn get-memory (defn get-memory
"Fetch the provided memory address. Valid addresses are 0x0 to 0x10000. "Fetch the provided memory address. Valid addresses are 0x0 to 0x10000.
Expand All @@ -21,33 +39,13 @@
:peek (follow-memory :sp) :peek (follow-memory :sp)
(get @memory address 0))) (get @memory address 0)))


(defn set-memory
"Set the memory address with value. Valid addresses are 0x0 to 0x10000.
As each word is 16 bits, the max value is 0xFFFF. If the provided value
is greater than 0xFFFF the value saved will be truncated"
[address f]
(dosync
(alter memory f)))

(defn change-memory (defn change-memory
[address value] [address value]
(let [address (if (not= address :push) (let [address (if-not (= address :push)
address address
(do (dec-memory :sp) (do (dec-memory :sp)
(get-memory :sp)))] (get-memory :sp)))]
(set-memory address #(assoc % address (bit-and 0xFFFF value))))) (set-memory #(assoc % address (bit-and 0xFFFF value)))))

(defn inc-memory
[address]
(set-memory address inc))

(defn dec-memory
[address]
(set-memory address dec))

(def follow-memory
"Fetch the value of the memory location which is stored in another memory location"
(comp get-memory get-memory))


(defn- mask-and-shift (defn- mask-and-shift
"Generates a function which applies a bit mask to a word and then "Generates a function which applies a bit mask to a word and then
Expand Down Expand Up @@ -98,7 +96,7 @@
(defmethod execute 0x3 [word] (defmethod execute 0x3 [word]
;; SUB a from b ;; SUB a from b
(let [[a b out] (process word)] (let [[a b out] (process word)]
(if (< 0 (- a b)) (if (pos? (- a b))
(change-memory :o 0xFFFF) (change-memory :o 0xFFFF)
(change-memory :o 0)) (change-memory :o 0))
(change-memory out (bit-and 0xFFFF (- a b))) (change-memory out (bit-and 0xFFFF (- a b)))
Expand All @@ -118,7 +116,7 @@
(defmethod execute 0x6 [word] (defmethod execute 0x6 [word]
;; MOD a = a % b ;; MOD a = a % b
(let [[a b out] (process word)] (let [[a b out] (process word)]
(if (= 0 b) (if (zero? b)
(change-memory out 0) (change-memory out 0)
(change-memory out (bit-and 0xFFFF (mod a b)))) (change-memory out (bit-and 0xFFFF (mod a b))))
(inc-memory :pc))) (inc-memory :pc)))
Expand Down Expand Up @@ -181,7 +179,7 @@
(defn run! (defn run!
"Start execution at 0x0000 unless specified" "Start execution at 0x0000 unless specified"
([pc] ([pc]
(set-memory :pc pc) (set-memory pc)
(run!)) (run!))
([] ([]
(while true (while true
Expand Down
4 changes: 2 additions & 2 deletions test/cljDCPU/core_test.clj → test/clj_dcpu16/core_test.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns cljDCPU.core-test (ns clj-dcpu16.core-test
(:use clojure.test (:use clojure.test
cljDCPU.core)) clj-dcpu16.core))


(deftest word-parsing (deftest word-parsing
(testing "Word Parsing" (testing "Word Parsing"
Expand Down

0 comments on commit 55f21e1

Please sign in to comment.