Selaa lähdekoodia

добавил обработку всех оставшихся инструкций, портировал загрузчик модулей, загрузил ядро.

kpmy 10 vuotta sitten
vanhempi
commit
97e5f93a40
10 muutettua tiedostoa jossa 259 lisäystä ja 44 poistoa
  1. 1 0
      lib/machine/app.dart
  2. 1 1
      lib/machine/host.dart
  3. 102 5
      lib/machine/loader.dart
  4. 6 1
      lib/tri/trits.dart
  5. 34 0
      lib/trisc/asm.dart
  6. 5 5
      lib/trisc/mem.dart
  7. 14 3
      lib/trisc/op.dart
  8. 41 8
      lib/trisc/proc.dart
  9. 54 16
      lib/trisc/reg.dart
  10. 1 5
      pubspec.yaml

+ 1 - 0
lib/machine/app.dart

@@ -21,5 +21,6 @@ init() async {
   Host host = new Host();
   Flasher flash = new Flasher(host.mem, mtOrg, 81);
   await flash.flash("bootstrap", adr: bootPC);
+  await flash.flash("Core", boot: true, list: true);
   host.run();
 }

+ 1 - 1
lib/machine/host.dart

@@ -44,7 +44,7 @@ class Host {
   void run(){
     Function step;
     step = () {
-      if (proc.next() != CPUresult.stop && !stop) new Future.delayed(new Duration(milliseconds: 500), step);
+      if (proc.next() != CPUresult.stop && !stop) new Future.delayed(new Duration(milliseconds: 50), step);
     };
     step();
   }

+ 102 - 5
lib/machine/loader.dart

@@ -1,33 +1,130 @@
 part of machine;
 
 class Module{
-  
+  String name;
+  int size;
+  int code;
+  int entry;
+  List<int> ent;
 }
 
 class Flasher{
+  static const descSize = 15;
+  static const firstJump = 1;
+  
   RAM mem;
   int mt;
   int start;
   
   List<Module> ml = new List();
   
-  flash (String name, {int adr: -1, bool boot: false}) async{
+  flash (String name, {int adr: -1, bool boot: false, bool list: false}) async{
+    
+    Mapper mapper = new Mapper(mem);
+    
+    /* снизу вверх исправляем адреса */
+    var fixD = (int adr, int fixorgD){
+      int fadr = adr + fixorgD * 3;
+      while(fadr != adr){
+        int27 inst = mapper[fadr ~/ 3];
+        int27 disp = (inst << 11) >> 11;
+        int27 mno = (inst << 7) >> 23;
+        if(mno == long(0)){
+          int27 _new = ((inst >> 20) << 20) + (long(mt) << 16) + long(ml.length * 3);
+          mapper[fadr ~/ 3] = _new;
+        }
+        fadr = fadr - disp.toInt() * 3;
+      }
+    };
+    
+    var fixP = (int adr, int fixorgP, List<String> imp){
+      int fadr = adr + fixorgP * 3;
+      while(fadr != adr){
+        int27 inst = mapper[fadr ~/ 3];
+        int27 mno = (inst << 9) >> 21;
+        int27 pno = (inst << 15) >> 21;
+        int27 disp = (inst << 21) >> 21;
+        halt.on(condition: (mno.toInt() - 1) < imp.length);
+        String im = imp[mno.toInt() - 1];
+        Module impmod = ml.firstWhere((m){return m.name == im;});
+        int dest = impmod.ent[pno.toInt() - 1] + impmod.code;
+        int offset = (dest - fadr - 3) ~/ 3;
+        int27 _new = ((inst >> 18) << 18) + long(offset);
+        mapper[fadr ~/ 3]=_new;
+        fadr = fadr - disp.toInt()*3;
+      }
+    };
+    
     try{
       var code = await HttpRequest.getString("tc/$name.tc");
       Map tc;
        tc = JSON.decode(code);
-       halt.on(condition: adr != -1); //куча модулей будет позже
-       fmt.fine(tc);
+       if (adr == -1){
+         adr = start;
+         ml.forEach((m){
+           adr += m.size + descSize + 3;
+         });
+       }
+       Module mod = new Module();
+       mod.name = name;
+       int entry = tc.containsKey("entry") ? tc["entry"] : 0;
+       int _var = tc.containsKey("varsize") ? tc["varsize"] : 0;
+       List<String> imp = tc.containsKey("imports") ? tc["imports"] : new List();
+       mod.ent = tc.containsKey("entries") ? tc["entries"] : new List();
+       int typ = 0;
+       if(tc.containsKey("types")){
+         List<String> types = tc["types"];
+         for(int i = 0; i<types.length; i++){
+           mapper[(adr ~/ 3) + i] = Nons.parse(types[i]);
+           typ += 3;
+         }
+       }
+       for(int i = 0; i<_var; i++){
+         mem[adr + typ + i] = short(0);
+       }
+       int str = tc.containsKey("strings") ? (tc["strings"] as List).length : 0;
+       if(tc.containsKey("strings")){
+         List<int> strings = tc["strings"];
+         for(int i=0; i<strings.length; i++){
+           mem[adr + typ + _var + i] = short(strings[i]);
+         }
+       }
+       int fixorgD = 0;
+       int fixorgP = 0;
        if(tc.containsKey("code")){
          Map nons = tc["code"];
+         fixorgD = nons.containsKey("fixorgD") ? nons["fixorgD"] : 0;
+         fixorgP = nons.containsKey("fixorgP") ? nons["fixorgP"] : 0;
          List nonits = nons["nons"];
          int i = 0;
+         int c = adr + _var + typ + str; mod.code = c; 
          nonits.forEach((String n){
-           new Mapper(mem)[(adr ~/ 3) + i] = Nons.parse(n);
+           mapper[(c ~/ 3) + i] = Nons.parse(n);
            i++;
          });
+         
+         if(list){
+           ml.add(mod);
+           mod.size = _var + str + typ + (nonits.length * 3);
+           mapper[(adr - descSize + 3) ~/ 3] = long(mod.size);
+           mapper[(adr - descSize + 6) ~/ 3] = long(mod.code);
+           mapper[(adr - descSize + 9) ~/ 3] = long(mod.code + entry);
+           /* значение SB каждый раз грузится из MT+mod.num ячейки памяти */
+           mapper[(mt ~/ 3) + ml.length] = long(adr);
+           for(int i=0; i<descSize; i++){
+             mem[adr+mod.size+i] = short(0);
+           }
+         }
+         fixD(mod.code, fixorgD);
+         fixP(mod.code, fixorgP, imp);
        }
+      if(boot){
+        mapper[firstJump] = asg.imov(org.SB, long(adr));
+        mapper[firstJump + 1] = asg.iadd(org.TMP, org.SB, long(_var + typ + str + entry));
+        mapper[firstJump + 2] = asg.brr(org.TMP, new Condition(false, TRUE, NULL, NULL).compile(), long(0));
+      }
     }catch(e){
+      fmt.fine(e);
       return -1;
     }
   }

+ 6 - 1
lib/tri/trits.dart

@@ -115,7 +115,12 @@ class Trits {
     if (x < 0) ret = ~ret;
     return ret;
   }
-
+  
+  operator []=(int i, Tril x){
+    if (i == 0 || i.abs() > 27) throw new ArgumentError("$i");
+    _trits[i.abs() - 1] = i > 0 ? x : ~x;
+  }
+  
   void _fill(int x, int mx) {
     clear();
     List<int> m = new List(mx);

+ 34 - 0
lib/trisc/asm.dart

@@ -30,4 +30,38 @@ class asm{
   static final tryte jmp = new tryte(5);
   static final tryte nz = new tryte(4);
   static final tryte eq = new tryte(3);
+}
+
+/* предопределенные регистры */
+class org{
+  static final tryte MT = short(23); 
+  static final tryte SB = short(24); 
+  static final tryte SP = short(25); 
+  static final tryte LNK = short(26);
+  static final tryte TMP = short(8);   
+}
+
+class asg{
+
+  static int27 _reg2(tryte op, tryte ra, tryte rb, int27 im){
+    int27 max = (int27.max << 15) >> 15;
+    int27 min = (int27.min << 15) >> 15;
+    if(im > max || im < min){
+      halt.on(code: 126);
+    }else{
+      return (asm.reg2 << 24) + (ra << 20) + (rb << 16) + (op << 12) + ((im << 15) >> 15);
+    }
+  }
+  
+  static int27 brr(tryte rc, tryte cond, int27 data){
+    return (asm.brr << 24) + (cond << 18) + ((data << 13) >> 9) + long(rc);
+  }
+  
+  static int27 imov(tryte ra, int27 im){
+    return _reg2(asm.mov, ra, short(0), im);
+  }
+  
+  static int27 iadd(tryte ra, tryte rb, int27 im){
+     return _reg2(asm.add, ra, rb, im);
+   }
 }

+ 5 - 5
lib/trisc/mem.dart

@@ -16,7 +16,7 @@ class MemFactory {
   static RAM newRAM(int length) {
     return new _stdRam(length);
   }
-  
+
   static MMU newMMU(int neg, int pos){
     return new _stdMmu(neg, pos);
   }
@@ -51,17 +51,17 @@ class Mapper {
 }
 
 class _stdMmu extends MMU{
-  
+
   RAM _n;
   RAM _p;
-  
+
   int get neg => _n.length;
   int get pos => _p.length;
   int get length => _n.length + _p.length;
-  
+
   tryte operator [](int adr){
     if (adr >= 0){
-      halt.on(condition: adr<_p.length);
+      halt.on(condition: adr<_p.length, msg: adr);
       return _p[adr];
     }else{
       halt.on(condition: adr.abs()-1<_n.length);

+ 14 - 3
lib/trisc/op.dart

@@ -17,7 +17,7 @@ abstract class MemoryOperation extends Operation{
   MemoryOperation(this._ir){
     a = short((_ir << 3) >> 23);
     b = short((_ir << 7) >> 23);
-    offset = (_ir << -11) >> 11;
+    offset = (_ir << 11) >> 11;
   }
 }
 
@@ -27,13 +27,24 @@ class Condition{
   Tril nz;
   Tril eq;
 
-  Condition(tryte c){
+  Condition.parse(tryte c){
     Trits cond = new Trits(c);
     link = cond[asm.lnk.toInt()].True;
     jump = cond[asm.jmp.toInt()];
     nz = cond[asm.nz.toInt()];
     eq = cond[asm.eq.toInt()];
   }
+
+  tryte compile(){
+    Trits cond = new Trits(short(0));
+    cond[asm.lnk.toInt()] = new Tril(link);
+    cond[asm.jmp.toInt()] = jump;
+    cond[asm.nz.toInt()] = nz;
+    cond[asm.eq.toInt()] = eq;
+    return short(cond.toInt27());
+  }
+
+  Condition(this.link, this.jump, this.nz, this.eq);
 }
 
 abstract class BranchOperation extends Operation{
@@ -53,7 +64,7 @@ abstract class BranchOperation extends Operation{
     offset = (_ir << 9) >> 9;
     data = (_ir << 9) >> 13;
     tryte cnd = short((_ir << 3) >> 21);
-    cond = new Condition(cnd);
+    cond = new Condition.parse(cnd);
   }
 }
 

+ 41 - 8
lib/trisc/proc.dart

@@ -58,24 +58,56 @@ class _cpu extends CPU{
 
     var jump = (int27 to, bool link){
       if(link)
-        reg[short(reg.length-1)] = to * long(3); /* регистры запоминают адрес в трайтах */
+        reg[short(reg.length-1)] = pc * long(3); /* регистры запоминают адрес в трайтах */
       pc = to; /* сдвиг в словах */
     };
 
     var calc = (BranchOperation op, int27 adr){
-      if(op.cond.jump.True)
+      if(op.cond.jump.True){
         jump(adr, op.cond.link);
+      }else if(op.cond.jump.Null){
+        var nz = op.cond.nz;
+        var eq = op.cond.eq;
+        if(nz == TRUE && eq == NULL){
+          if(reg.nz == TRUE) jump(adr, op.cond.link); /* > */
+        }else if(nz == NULL && eq == NULL){
+          if(reg.nz == NULL) jump(adr, op.cond.link); /* = */
+        }else if(nz == FALSE && eq == NULL){
+          if(reg.nz == FALSE) jump(adr, op.cond.link); /* < */
+        }else if(nz == TRUE && eq == TRUE){
+          if(reg.nz != FALSE) jump(adr, op.cond.link); /* >= */
+        }else if(nz == NULL && eq == FALSE){
+          if(reg.nz != NULL) jump(adr, op.cond.link); /* # */
+        }else if(nz == FALSE && eq == NULL){
+          if(reg.nz != TRUE) jump(adr, op.cond.link); /* <= */
+        }else halt.on(code: 215);
+      }
     };
 
     var def = (Operation op){
-      if(op is Reg)
+      if(op is Reg){
         return rh(op);
-      if(op is BranchReg){
+      }else if(op is BranchReg){
         calc(op, reg[op.c] ~/ long(3));
         return CPUresult.ok;
-      }
-      else
-        halt.on(msg: op.runtimeType);
+      }else if(op is BranchConst){
+        calc(op, pc + op.offset);
+        return CPUresult.ok;
+      }else if (op is GetWord){
+        reg[op.a] = new Mapper(mem)[(reg[op.b] + op.offset).toInt()];
+        reg.updateNZ(op.a);
+        return CPUresult.ok;
+      }else if (op is SetWord){
+        new Mapper(mem)[(reg[op.b] + op.offset).toInt()] = reg[op.a];
+        return CPUresult.ok;
+      }else if(op is GetTryte){
+        reg[op.a] = long(mem[(reg[op.b] + op.offset).toInt()]);
+        reg.updateNZ(op.a);
+        return CPUresult.ok;
+      }else if(op is SetTryte){
+        mem[(reg[op.b] + op.offset).toInt()] = short(reg[op.a]);
+        return CPUresult.ok;
+      }else halt.on(msg: op.runtimeType);
     };
     return def;
   }
@@ -112,7 +144,8 @@ class _cpu extends CPU{
     fmt.fine(new Trits(ir));
 
     CPUresult ret = parse(ir, handler());
-    fmt.fine("step $step: $ret");
+    fmt.fine(reg);
+    fmt.fine("");
     return ret;
   }
 

+ 54 - 16
lib/trisc/reg.dart

@@ -2,7 +2,7 @@ part of trisc;
 
 typedef void Computation(tryte ra, tryte rb, int27 im);
 
-class Registers{
+class Registers {
   Map<tryte, Computation> _cache = new Map();
 
   List<int27> _r = new List(27);
@@ -11,46 +11,84 @@ class Registers{
   Tril _nz = NULL;
   Tril get nz => _nz;
 
-  void updateNZ(tryte adr){
+  void updateNZ(tryte adr) {
     int27 x = _r[adr.toInt()];
     _nz = new Tril(x == long(0) ? null : (x > long(0)));
   }
 
-  int27 operator[](tryte adr){
+  int27 operator [](tryte adr) {
     return _r[adr.toInt()];
   }
 
-  operator[]=(tryte adr, int27 val){
+  operator []=(tryte adr, int27 val) {
     _r[adr.toInt()] = val;
   }
 
-  IRhandler handler(){
-    var def = (Operation op){
-      if(op is Reg3){
+  IRhandler handler() {
+    var def = (Operation op) {
+      if (op is Reg3) {
         var comp = _cache[op.op];
         halt.on(condition: comp != null, code: 126, msg: op.op);
         comp(op.a, op.b, this[op.c]);
+        updateNZ(op.a);
         return CPUresult.ok;
-      }else if(op is Reg2){
+      } else if (op is Reg2) {
         var comp = _cache[op.op];
         halt.on(condition: comp != null, code: 126, msg: op.op);
         comp(op.a, op.b, op.im);
+        updateNZ(op.a);
         return CPUresult.ok;
-      }else
-        halt.on(msg: op.runtimeType);
+      } else halt.on(msg: op.runtimeType);
     };
     return def;
   }
 
-  void _init(){
-    _cache[asm.mov] = (tryte a, tryte b, int27 c){
+  void _init() {
+    _cache[asm.mov] = (tryte a, tryte b, int27 c) {
       this[a] = c;
     };
+    _cache[asm.add] = (tryte a, tryte b, int27 c) {
+      this[a] = this[b] + c;
+    };
+    _cache[asm.sub] = (tryte a, tryte b, int27 c) {
+      this[a] = this[b] - c;
+    };
+    _cache[asm.lsl] = (tryte a, tryte b, int27 c) {
+      this[a] = c.toInt() == 0
+          ? this[b]
+          : (c.toInt() > 0 ? this[b] << c.toInt() : this[b] >> c.toInt());
+    };
+    _cache[asm.mul] = (tryte a, tryte b, int27 c) {
+      this[a] = this[b] * c;
+    };
+    _cache[asm.div] = (tryte a, tryte b, int27 c) {
+      this[a] = this[b] ~/ c;
+    };
+    _cache[asm.mod] = (tryte a, tryte b, int27 c) {
+      this[a] = this[b] % c;
+    };
+    _cache[asm.and] = (tryte a, tryte b, int27 c) {
+      this[a] = (new Trits(this[b]) * new Trits(c)).toInt27();
+    };
+    _cache[asm.ann] = (tryte a, tryte b, int27 c) {
+      this[a] = (new Trits(this[b]) - new Trits(c)).toInt27();
+    };
+    _cache[asm.xor] = (tryte a, tryte b, int27 c) {
+      this[a] = (new Trits(this[b]) / new Trits(c)).toInt27();
+    };
+    _cache[asm.ior] = (tryte a, tryte b, int27 c) {
+      this[a] = (new Trits(this[b]) + new Trits(c)).toInt27();
+    };
+  }
+
+  @override
+  String toString(){
+    String ret = _r.toString();
+    return "registers: $ret";
   }
 
-  Registers(){
+  Registers() {
     _init();
-    for(int i = 0; i<_r.length; i++)
-      _r[i] = new int27.zero();
+    for (int i = 0; i < _r.length; i++) _r[i] = new int27.zero();
   }
-}
+}

+ 1 - 5
pubspec.yaml

@@ -7,8 +7,4 @@ dependencies:
   browser: any
   libpen: any
   logging_handlers: any
-  unittest: any
-  async_await:
-    git: https://github.com/dart-lang/async_await.git
-transformers:
-- async_await
+  unittest: any