add memory get/set
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 4 Apr 2020 18:41:34 +0000 (19:41 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 4 Apr 2020 18:41:34 +0000 (19:41 +0100)
src/soc/decoder/isa/caller.py
src/soc/decoder/power_pseudo.py
src/soc/decoder/pseudo/parser.py
src/soc/decoder/pseudo/pywriter.py

index cc78a3f968f42097fc9f6d47b6e28247413cf6c0..89917d9c715265c92878a61aa63ddc72bcdea920 100644 (file)
@@ -4,7 +4,13 @@ class ISACaller:
     def __init__(self):
         self.gpr = {} # TODO
         self.mem = {} # TODO
-        self.namespace = {'GPR': self.gpr, 'MEM': self.mem}
+        self.namespace = {'GPR': self.gpr,
+                          'MEM': self.mem,
+                          'memassign': self.memassign
+                         }
+
+    def memassign(self, ea, sz, val):
+        pass
 
 def inject(context):
     """ Decorator factory. """
index 9ed8295126fe49c83cdeb835f258ebf6191fb7d7..c4f962a28c7f1fea10e38c9eb4ff45ae33d32604 100644 (file)
@@ -117,10 +117,20 @@ RT <- (load_data[56:63] || load_data[48:55]
 testgpr = """
 GPR(5) <- x
 """
+testmem = """
+a <- (RA|0)
+b <- (RB|0)
+RA <- MEM(RB, 2)
+EA <- a + 1
+MEM(EA, 1) <- (RS)[56:63]
+RB <- RA
+RA <- EA
+"""
 #code = testmul
 #code = testgetzero
 #code = testcat
-code = testgpr
+#code = testgpr
+code = testmem
 #code = testreg
 #code = cnttzd
 #code = cmpi
@@ -140,6 +150,26 @@ def tolist(num):
 def get_reg_hex(reg):
     return hex(reg.value)
 
+class Mem:
+
+    def __init__(self):
+        self.mem = []
+        for i in range(128):
+            self.mem.append(i)
+
+    def __call__(self, addr, sz):
+        res = []
+        for s in range(sz): # TODO: big/little-end
+            res.append(SelectableInt(self.mem[addr.value + s], 8))
+        print ("memread", addr, sz, res)
+        return selectconcat(*res)
+
+    def memassign(self, addr, sz, val):
+        print ("memassign", addr, sz, val)
+        for s in range(sz):
+            byte = (val.value) >> (s*8) & 0xff # TODO: big/little-end
+            self.mem[addr.value + s] = byte
+
 
 class GPR(dict):
     def __init__(self, sd, regfile):
@@ -185,11 +215,11 @@ def test():
 
     gsc = GardenSnakeCompiler()
 
-    # XXX unused!  see GPR instead
     gsc.regfile = {}
     for i in range(32):
         gsc.regfile[i] = i
     gsc.gpr = GPR(gsc.parser.sd, gsc.regfile)
+    gsc.mem = Mem()
 
     _compile = gsc.compile
 
@@ -219,6 +249,8 @@ def test():
     d["SelectableInt"] = SelectableInt
     d["concat"] = selectconcat
     d["GPR"] = gsc.gpr
+    d["MEM"] = gsc.mem
+    d["memassign"] = gsc.mem.memassign
 
     form = 'X'
     gsc.gpr.set_form(form)
@@ -269,7 +301,9 @@ def test():
             print(decode.sigforms['X'])
             x = yield decode.sigforms['X'].RS
             ra = yield decode.sigforms['X'].RA
+            rb = yield decode.sigforms['X'].RB
             print("RA", ra, d['RA'])
+            print("RB", rb, d['RB'])
             print("RS", x)
 
             for wname in gsc.parser.write_regs:
@@ -286,6 +320,12 @@ def test():
     for i in range(len(gsc.gpr)):
         print("regfile", i, get_reg_hex(gsc.gpr[i]))
 
+    for i in range(0, len(gsc.mem.mem), 16):
+        hexstr = []
+        for j in range(16):
+            hexstr.append("%02x" % gsc.mem.mem[i+j])
+        hexstr = ' '.join(hexstr)
+        print ("mem %4x" % i, hexstr)
 
 if __name__ == '__main__':
     test()
index 437310ef4aa619308fd9f59d8dc836d1f00bd7bf..9384ca213d5c23a47edf63e5ed58990e8a59909e 100644 (file)
@@ -318,6 +318,15 @@ class PowerParser:
                 # replace GPR(x) with GPR[x]
                 idx = p[1].args[0]
                 p[1] = ast.Subscript(p[1].func, idx)
+            elif isinstance(p[1], ast.Call) and p[1].func.id == 'MEM':
+                print ("mem assign")
+                print(astor.dump_tree(p[1]))
+                p[1].func.id = "memassign" # change function name to set
+                p[1].args.append(p[3])
+                p[0] = p[1]
+                print ("mem rewrite")
+                print(astor.dump_tree(p[0]))
+                return
             else:
                 print ("help, help")
                 print(astor.dump_tree(p[1]))
index 36948940b4db74100f64b6f8c7be162034c8c956..d26074cf31ff2d7299e1a87e153779132af4b528 100644 (file)
@@ -80,7 +80,8 @@ class PyISAWriter(ISA):
 
 if __name__ == '__main__':
     isa = PyISAWriter()
-    isa.write_pysource('fixedload')
+    isa.write_pysource('fixedstore')
     exit(0)
+    isa.write_pysource('fixedload')
     isa.write_pysource('comparefixed')
     isa.write_pysource('fixedarith')