# HG changeset patch # User Simon Heath # Date 1664859776 14400 # Tue Oct 04 01:02:56 2022 -0400 # Node ID 3e06b7fe0e3ae228494f478b1c9fe486284cb953 # Parent 1ad37d61b1c070ea60c87c695d11e69156fcd5a0 Ok I'm not insane just foolish diff --git a/src/main.fnl b/src/main.fnl --- a/src/main.fnl +++ b/src/main.fnl @@ -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) +