ソースを参照

Add encodings for ORI and SW

Matt Coles 9 年 前
コミット
1a59bddd14
共有2 個のファイルを変更した31 個の追加2 個の削除を含む
  1. 17 1
      js/encoders.js
  2. 14 1
      js/utils.js

+ 17 - 1
js/encoders.js

@@ -20,9 +20,25 @@ const encoders = {
20 20
   'LUI': (rt, immediate) => {
21 21
     let encoded = '001111'
22 22
     encoded = encoded + '0'.repeat(5)
23
-    encoded = encoded + utils.dec2binu(getReg(rt))
23
+    encoded = encoded + utils.dec2binu(getReg(rt), 5)
24 24
     encoded = encoded + utils.hex2bin(immediate, 16)
25 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
+  },
34
+  'SW': (rt, offsetBase) => {
35
+    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)
26 42
   }
27 43
 }
28 44
 

+ 14 - 1
js/utils.js

@@ -10,6 +10,15 @@ const dec2binu = (dec, length) => {
10 10
   }
11 11
 }
12 12
 
13
+const int16_2bin = (int16) => {
14
+  const signed32 = (int16 >>> 0).toString(2)
15
+  if (signed32.length > 16) {
16
+    return signed32.slice(16)
17
+  } else {
18
+    return '0'.repeat(16 - signed32.length).concat(signed32)
19
+  }
20
+}
21
+
13 22
 const hex2bin = (hex, length) => {
14 23
   if (hex.startsWith('0x')) {
15 24
     return dec2binu(parseInt(hex.slice(2), 16), length)
@@ -27,9 +36,13 @@ const bin2hex = (bin, length) => {
27 36
   }
28 37
 }
29 38
 
39
+const splitOffsetBase = (offsetBase) => offsetBase.trim().slice(0, -1).split('(').map(s => s.trim())
40
+
30 41
 module.exports = {
31 42
   'error': error,
32 43
   'dec2binu': dec2binu,
33 44
   'hex2bin': hex2bin,
34
-  'bin2hex': bin2hex
45
+  'bin2hex': bin2hex,
46
+  'int16_2bin': int16_2bin,
47
+  'splitOffsetBase': splitOffsetBase
35 48
 }