aboutsummaryrefslogtreecommitdiffstats
path: root/format.py
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2021-07-09 10:58:14 +0000
committers-ol <s+removethis@s-ol.nu>2021-07-09 12:39:12 +0000
commit6368fb586f3d5d70528915369397281ccfd00dbe (patch)
treedc60783429b8f1c831649c5aa7804161883bc023 /format.py
parentremove outdated disasm.sh (diff)
downloadsubv-6368fb586f3d5d70528915369397281ccfd00dbe.tar.gz
subv-6368fb586f3d5d70528915369397281ccfd00dbe.zip
wip more validation logic
Diffstat (limited to 'format.py')
-rwxr-xr-xformat.py38
1 files changed, 27 insertions, 11 deletions
diff --git a/format.py b/format.py
index 87e3c51..64eff01 100755
--- a/format.py
+++ b/format.py
@@ -18,22 +18,22 @@ For example the "j" instruction-format can take an "imm20" pre-sliced immediate,
... 37/u 5/rd 10010/imm20
... # store 0x48 (H) in UART0+0
... 13/i 6/rd 0/funct3 0/rs 48/imm12
-... 23/s 5/rs1 0/imm12 2/funct3 6/rs2
+... 23/s 2/funct3 5/rs1 0/imm12 6/rs2
... # store 0x65 (e) in UART0+0
... 13/i 6/rd 0/funct3 0/rs 65/imm12
-... 23/s 5/rs1 0/imm12 2/funct3 6/rs2
+... 23/s 2/funct3 5/rs1 0/imm12 6/rs2
... # store 0x6c (l) in UART0+0
... 13/i 6/rd 0/funct3 0/rs 6c/imm12
-... 23/s 5/rs1 0/imm12 2/funct3 6/rs2
+... 23/s 2/funct3 5/rs1 0/imm12 6/rs2
... # store 0x6c (l) in UART0+0
... 13/i 6/rd 0/funct3 0/rs 6c/imm12
-... 23/s 5/rs1 0/imm12 2/funct3 6/rs2
+... 23/s 2/funct3 5/rs1 0/imm12 6/rs2
... # store 0x6f (o) in UART0+0
... 13/i 6/rd 0/funct3 0/rs 6f/imm12
-... 23/s 5/rs1 0/imm12 2/funct3 6/rs2
+... 23/s 2/funct3 5/rs1 0/imm12 6/rs2
... # store 0x0a (\\\\n) in UART0+0
... 13/i 6/rd 0/funct3 0/rs a/imm12
-... 23/s 5/rs1 0/imm12 2/funct3 6/rs2
+... 23/s 2/funct3 5/rs1 0/imm12 6/rs2
... # jump back up to the top
... 6f/j 0/rd -34/imm21
... '''[1:-1]))))
@@ -71,6 +71,23 @@ def _test_format(words):
return " ".join(map(str, words))
+def pack_r(instr):
+ """verify & pack R-type instructions.
+
+ >>> _test_format(pack_r([(0x33, 'r'), (5, 'rd'), (0, 'funct3'), (0xa, 'rs1'), (0xb, 'rs2'), (0, 'funct7')]))
+ '33/7 05/5 0/3 0a/5 0b/5 00/7'
+ """
+ (op, rd, funct3, rs1, rs2, funct7) = instr
+ op = bits.u(subv.untag(op), 7)
+ rd = bits.u(subv.untag(rd, "rd"), 5)
+ funct3 = bits.u(subv.untag(funct3, "funct3"), 3)
+ rs1 = bits.u(subv.untag(rs1, "rs1"), 5)
+ rs2 = bits.u(subv.untag(rs2, "rs2"), 5)
+ funct7 = bits.u(subv.untag(funct7, "funct7"), 7)
+
+ return [op, rd, funct3, rs1, rs2, funct7]
+
+
def pack_i(instr):
"""verify & pack I-type instructions.
@@ -78,7 +95,6 @@ def pack_i(instr):
'13/7 06/5 0/3 00/5 065/12'
"""
(op, rd, funct, rs, imm) = instr
- op_tag = op[1]
op = bits.u(subv.untag(op), 7)
rd = bits.u(subv.untag(rd, "rd"), 5)
funct = bits.u(subv.untag(funct, "funct3"), 3)
@@ -91,14 +107,14 @@ def pack_i(instr):
def pack_s(instr):
"""verify & pack S-type instructions.
- >>> _test_format(pack_s([(0x23, 's'), (5, 'rs1'), (0, 'imm12'), (2, 'funct3'), (6, 'rs2')]))
+ >>> _test_format(pack_s([(0x23, 's'), (2, 'funct3'), (5, 'rs1'), (0, 'imm12'), (6, 'rs2')]))
'23/7 00/5 2/3 05/5 06/5 00/7'
"""
- (op, rs1, imm, funct, rs2) = instr
+ (op, funct, rs1, imm, rs2) = instr
op = bits.u(subv.untag(op), 7)
+ funct = bits.u(subv.untag(funct, "funct3"), 3)
rs1 = bits.u(subv.untag(rs1, "rs1"), 5)
imm = bits.i(subv.untag(imm, "imm12"), 12)
- funct = bits.u(subv.untag(funct, "funct3"), 3)
rs2 = bits.u(subv.untag(rs2, "rs2"), 5)
imm_lo = imm[4:0]
@@ -180,7 +196,7 @@ def pack_j(instr):
format_map = {
- # "r": pack_r,
+ "r": pack_r,
"i": pack_i,
"s": pack_s,
"b": pack_b,