Permalink
Browse files

Merge pull request #1 from devn/master

Cleanup and removal of no-op argument to set-memory
  • Loading branch information...
2 parents 9e97c6d + a5990e4 commit 55f21e1c4f7d222c126174c7b7a4e6d872b6195c @jjcomer jjcomer committed Apr 8, 2012
Showing with 33 additions and 35 deletions.
  1. +3 −3 project.clj
  2. +28 −30 src/{cljDCPU → clj_dcpu16}/core.clj
  3. +2 −2 test/{cljDCPU → clj_dcpu16}/core_test.clj
View
@@ -1,6 +1,6 @@
-(defproject cljDCPU "0.1.0-SNAPSHOT"
- :description "FIXME: write description"
- :url "http://example.com/FIXME"
+(defproject clj-dcpu16 "0.1.0-SNAPSHOT"
+ :description "an implementation of notch's dcpu16 in clojure"
+ :url "https://github.com/jjcomer/cljDCPU"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.3.0"]])
@@ -1,12 +1,30 @@
-(ns cljDCPU.core)
+(ns clj-dcpu16.core)
(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})
-(declare follow-memory)
-(declare inc-memory)
-(declare dec-memory)
+(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"
+ [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
"Fetch the provided memory address. Valid addresses are 0x0 to 0x10000.
@@ -21,33 +39,13 @@
:peek (follow-memory :sp)
(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
[address value]
- (let [address (if (not= address :push)
+ (let [address (if-not (= address :push)
address
(do (dec-memory :sp)
(get-memory :sp)))]
- (set-memory address #(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))
+ (set-memory #(assoc % address (bit-and 0xFFFF value)))))
(defn- mask-and-shift
"Generates a function which applies a bit mask to a word and then
@@ -98,7 +96,7 @@
(defmethod execute 0x3 [word]
;; SUB a from b
(let [[a b out] (process word)]
- (if (< 0 (- a b))
+ (if (pos? (- a b))
(change-memory :o 0xFFFF)
(change-memory :o 0))
(change-memory out (bit-and 0xFFFF (- a b)))
@@ -118,7 +116,7 @@
(defmethod execute 0x6 [word]
;; MOD a = a % b
(let [[a b out] (process word)]
- (if (= 0 b)
+ (if (zero? b)
(change-memory out 0)
(change-memory out (bit-and 0xFFFF (mod a b))))
(inc-memory :pc)))
@@ -181,7 +179,7 @@
(defn run!
"Start execution at 0x0000 unless specified"
([pc]
- (set-memory :pc pc)
+ (set-memory pc)
(run!))
([]
(while true
@@ -1,6 +1,6 @@
-(ns cljDCPU.core-test
+(ns clj-dcpu16.core-test
(:use clojure.test
- cljDCPU.core))
+ clj-dcpu16.core))
(deftest word-parsing
(testing "Word Parsing"

0 comments on commit 55f21e1

Please sign in to comment.