Ok I'm not insane just foolish
1 files changed, 20 insertions(+), 8 deletions(-)

M src/main.fnl
M src/main.fnl +20 -8
@@ 49,6 49,8 @@ 
 ;
 ; enum OpMode {iABC, iABx, iAsBx, iAx, isJ};  /* basic instruction formats */
 
+(local fennel (require :fennel))
+
 ;; Opcodes with metadata, as listed in lopcodes.h
 ;;
 ;; `code` is the integer ID of the opcode from the Lua OpCode struct

          
@@ 174,11 176,17 @@ 
 
 (fn opcode-idx [op]
   "Returns the opcode number of the given opcode name"
-  (- (. OpcodeNames op) 1))
+   (let [v (- (. OpcodeNames op) 1)]
+     (if v
+       v
+       (error (string.format "Invalid opcode name %s" op)))))
 
 (fn idx-opcode [i]
   "Returns the opcode struct for the opcode with the given number"
-  (. Opcodes (+ i 1)))
+  (let [v (. Opcodes (+ i 1))]
+     (if v
+       v
+       (error (string.format "Invalid opcode index %s" i)))))
 
 (fn extract [i offset len]
   "Extract `len` bits of integer `i`, starting from bit `offset`

          
@@ 191,7 199,11 @@ 
 
 (fn decode-abc [i]
   "Decode an instruction in the :iABC format"
-  0)
+  (let [a (extract i 7 8)
+        k (extract i 15 1)
+        b (extract i 16 8)
+        c (extract i 24 8)]
+    {: a : b : c : k}))
 
 (fn decode-abx [i]
   "Decode an instruction in the :iABx format"

          
@@ 215,12 227,12 @@ 
         OpPos  0   ; Starting bit number of the opcode field
         opcode (extract i OpPos OpSize)
         opcode-format (. (idx-opcode opcode) :format)]
-    (print (string.format "Instruction %d is opcode %i named %s with format %s"
+    (print (string.format "Instruction 0x%X is opcode %i named %s with format %s %s"
                           i
                           opcode
                           (. (idx-opcode opcode) :name)
-                          opcode-format))))
+                          opcode-format
+                          (fennel.view (decode-abc i))))))
 
-(print "Opcode" 3 "is" (. (idx-opcode 3) :name))
-(print "Opcode :OpGetI is" (opcode-idx :OpGetI))
-(decode 0x03)
+(decode 0x11223303)
+