Skip to content
Browse files

Added first part of assembler (iml -> machine code)

  • Loading branch information...
1 parent 904b7fd commit ec9628917bca036c9c1681dd5b1fe99d6c2bb171 @johnwayner committed Apr 12, 2012
Showing with 49 additions and 5 deletions.
  1. +44 −0 src/hermit/assembler.clj
  2. +2 −2 src/hermit/cpu.clj
  3. +3 −3 src/hermit/disassembler.clj
View
44 src/hermit/assembler.clj
@@ -0,0 +1,44 @@
+(ns hermit.assembler
+ (:use (hermit core disassembler)))
+
+(comment bit-range indexes are from least to most significant bit and zero indexed.)
+(defn basic-op-props
+ [val] {:offsets {:op 0
+ :a 4
+ :b 10}
+ :val val})
+(defn ext-op-props
+ [val] {:offsets {:op 4
+ :a 10
+ :b 0}
+ :val val})
+
+(def op-map (merge {:jsr (ext-op-props 0x1)}
+ (apply merge (map #(hash-map % (basic-op-props %2))
+ ops
+ (iterate inc 0)))))
+
+(defn encode-operand
+ [{:keys [val literal]}]
+ (if (= :literal-20 val)
+ (+ literal 0x20)
+ (let [idx (.indexOf op-vals val)]
+ (if (< idx 0)
+ 0
+ idx))))
+
+(defn encode-iml
+ "Renders the intermediate instruction format to an actual word.
+
+ The iml format is the same outputted by hermit.disassembler/instruction."
+ [iml]
+ (let [omap ((:op iml) op-map)]
+ (reduce #(bit-or %1
+ (bit-shift-left (case (%2 0)
+ :op (:val omap)
+ (:a
+ :b) (encode-operand ((%2 0) iml)))
+ (%2 1)))
+ 0x0000
+ (:offsets omap))))
+
View
4 src/hermit/cpu.clj
@@ -90,7 +90,7 @@
(case val
:ptr-nxt (mem-val m (:nxt op-val))
:nxt (:nxt op-val)
- :lit (:literal op-val)
+ :literal-20 (:literal op-val)
(:a-ptr-nxt
:b-ptr-nxt
:c-ptr-nxt
@@ -120,7 +120,7 @@
(let [val (:val op-val)]
(case val
:ptr-nxt {:type :mem :val (:nxt op-val)}
- (:nxt :lit) {:type :nop} ;fail silently as per spec
+ (:nxt :literal-20) {:type :nop} ;fail silently as per spec
(:a-ptr-nxt
:b-ptr-nxt
:c-ptr-nxt
View
6 src/hermit/disassembler.clj
@@ -74,14 +74,14 @@
[w offset] (let [idx (bit-and 0x3f (bit-shift-right w (- 10 offset)))
val (op-vals idx)]
(if (= val :literal-20)
- {:val :lit :literal (- idx 0x20)}
+ {:val :literal-20 :literal (- idx 0x20)}
{:val val})))
(defn instruction
"Returns a map of the instruction defined by w with :op :a :b"
[w] (let [o (op w)
a (if (= :unk o)
- {:val :lit :literal w}
+ {:val :literal-20 :literal w}
(op-val w (if (= :jsr o) 0 6)))
b (if (or (= :unk o) (= :jsr o)) nil (op-val w 0))]
{:op o, :a a, :b b, :w w}))
@@ -124,7 +124,7 @@ Note: Assumes branches succeed."
(cond
(= :ptr-nxt val) (str "[0x" (Integer/toHexString (:nxt v)) "]")
(= :nxt val) (str "0x" (Integer/toHexString (:nxt v)))
- (= :lit val) (str "0x" (Integer/toHexString (:literal v)))
+ (= :literal-20 val) (str "0x" (Integer/toHexString (:literal v)))
(re-find #":.-ptr-nxt" (str val)) (str "[0x"
(if (:nxt v)
(Integer/toHexString (:nxt v))

0 comments on commit ec96289

Please sign in to comment.
Something went wrong with that request. Please try again.