Procházet zdrojové kódy

Refactor encoders to use a neater style

Matt Coles před 9 roky
rodič
revize
e697f89292
1 změnil soubory, kde provedl 17 přidání a 20 odebrání
  1. 17 20
      js/encoders.js

+ 17 - 20
js/encoders.js

@@ -17,28 +17,25 @@ const getReg = (id) => {
17 17
 }
18 18
 
19 19
 const encoders = {
20
-  'LUI': (rt, immediate) => {
21
-    let encoded = '001111'
22
-    encoded = encoded + '0'.repeat(5)
23
-    encoded = encoded + utils.dec2binu(getReg(rt), 5)
24
-    encoded = encoded + utils.hex2bin(immediate, 16)
25
-    return '0x' + utils.bin2hex(encoded, 8)
26
-  },
27
-  'ORI': (rt, rs, immediate) => {
28
-    let encoded = '001101'
29
-    encoded = encoded + utils.dec2binu(getReg(rs), 5)
30
-    encoded = encoded + utils.dec2binu(getReg(rt), 5)
31
-    encoded = encoded + utils.hex2bin(immediate, 16)
32
-    return '0x' + utils.bin2hex(encoded, 8)
33
-  },
20
+  'LUI': (rt, immediate) => 
21
+  '0x'.concat(utils.bin2hex(
22
+    '001111'.concat(
23
+      '0'.repeat(5),
24
+      utils.dec2binu(getReg(rt), 5),
25
+      utils.hex2bin(immediate, 16)), 8)),
26
+  'ORI': (rt, rs, immediate) => 
27
+  '0x'.concat(utils.bin2hex(
28
+    '001101'.concat(
29
+      utils.dec2binu(getReg(rs), 5),
30
+      utils.dec2binu(getReg(rt), 5),
31
+      utils.hex2bin(immediate, 16)), 8)),
34 32
   'SW': (rt, offsetBase) => {
35 33
     const [offset, base] = utils.splitOffsetBase(offsetBase)
36
-    let encoded = '101011'
37
-    encoded = encoded + utils.dec2binu(getReg(base), 5)
38
-    encoded = encoded + utils.dec2binu(getReg(rt), 5)
39
-    encoded = encoded + utils.int16_2bin(offset, 16)
40
-    console.log(encoded)
41
-    return '0x' + utils.bin2hex(encoded, 8)
34
+    return '0x'.concat(utils.bin2hex(
35
+      '101011'.concat(
36
+        utils.dec2binu(getReg(base), 5),
37
+        utils.dec2binu(getReg(rt), 5),
38
+        utils.int16_2bin(offset, 16)), 8))
42 39
   }
43 40
 }
44 41