Przeglądaj źródła

исправил ошибки

p.kushnir 10 lat temu
rodzic
commit
3a823c1eb2

BIN
doc/trinary-0.pdf


+ 13 - 0
lib/halt.dart

@@ -0,0 +1,13 @@
+library halt;
+
+class halt implements Exception{
+  var msg;
+  int code;
+  halt._new(this.code, [this.msg]);
+  
+  static on({bool condition: false, int code: 100, msg}){
+    if(!condition){
+      throw new halt._new(code, msg);
+    }
+  }
+}

+ 13 - 0
lib/machine/app.dart

@@ -0,0 +1,13 @@
+library machine;
+
+import 'dart:async';
+import 'package:tri/trisc/core.dart';
+
+void init(){
+  RAM r = MemFactory.newRAM(9841);
+  CPU p = ProcFactory.newCPU(r);
+  p.reset();
+  Future.doWhile((){
+    return (p.next() == CPU.ok);
+  });
+}

+ 4 - 4
lib/tri/int.dart

@@ -101,11 +101,11 @@ class tryte extends tri_num {
   }
   }
 
 
   int27 operator <<(int x) {
   int27 operator <<(int x) {
-    return (new Bits(this) << x).toInt27();
+    return (new Trits(this) << x).toInt27();
   }
   }
 
 
   int27 operator >>(int x) {
   int27 operator >>(int x) {
-    return (new Bits(this) >> x).toInt27();
+    return (new Trits(this) >> x).toInt27();
   }
   }
 }
 }
 
 
@@ -194,10 +194,10 @@ class int27 extends tri_num {
   }
   }
 
 
   int27 operator <<(int x) {
   int27 operator <<(int x) {
-    return (new Bits(this) << x).toInt27();
+    return (new Trits(this) << x).toInt27();
   }
   }
 
 
   int27 operator >>(int x) {
   int27 operator >>(int x) {
-    return (new Bits(this) >> x).toInt27();
+    return (new Trits(this) >> x).toInt27();
   }
   }
 }
 }

+ 1 - 1
lib/tri/mathe.dart

@@ -83,7 +83,7 @@ Tril mod(Tril x) {
 }
 }
 
 
 tryte short(int27 i) {
 tryte short(int27 i) {
-  Bits from = new Bits(i).extract(0, 9);
+  Trits from = new Trits(i).extract(0, 9);
   return new tryte(from.toInt27().toInt());
   return new tryte(from.toInt27().toInt());
 }
 }
 
 

+ 9 - 16
lib/tri/test.dart

@@ -4,8 +4,6 @@ import 'package:unittest/unittest.dart';
 import 'package:tri/tri/mathe.dart';
 import 'package:tri/tri/mathe.dart';
 
 
 void testTri(){
 void testTri(){
-  print("$TRUE, $NULL, $FALSE");
-
     test("basics", () {
     test("basics", () {
       expect(TRUE, equals(TRUE));
       expect(TRUE, equals(TRUE));
       expect(FALSE, equals(FALSE));
       expect(FALSE, equals(FALSE));
@@ -20,27 +18,22 @@ void testTri(){
       expect(TRUE | TRUE, equals(TRUE));
       expect(TRUE | TRUE, equals(TRUE));
       expect(FALSE & FALSE, equals(FALSE));
       expect(FALSE & FALSE, equals(FALSE));
     });
     });
-
-    print(tryte.min);
-    print(tryte.max);
-    print(int27.min);
-    print(int27.max);
-
+    
     test("arithmetics", () {
     test("arithmetics", () {
       expect(new tryte(5) + new tryte.one(), equals(new tryte(6)));
       expect(new tryte(5) + new tryte.one(), equals(new tryte(6)));
       expect(short(new int27(40)), equals(new tryte(40)));
       expect(short(new int27(40)), equals(new tryte(40)));
     });
     });
 
 
-    test("bits", () {
-      expect(new Bits(int27.max).toString(), equals("+++++++++++++++++++++++++++"));
-      expect(new Bits(tryte.max).toString(), equals("000000000000000000+++++++++"));
-      expect(tryte.max, equals(short(new Bits(tryte.max).toInt27())));
-      expect(int27.max, equals(new Bits(int27.max).toInt27()));
-      expect(new Bits(new tryte(1)) << 5, equals((new Bits(new tryte(1)) << 18) >> 13));
+    test("Trits", () {
+      expect(new Trits(int27.max).toString(), equals("+++++++++++++++++++++++++++"));
+      expect(new Trits(tryte.max).toString(), equals("000000000000000000+++++++++"));
+      expect(tryte.max, equals(short(new Trits(tryte.max).toInt27())));
+      expect(int27.max, equals(new Trits(int27.max).toInt27()));
+      expect(new Trits(new tryte(1)) << 5, equals((new Trits(new tryte(1)) << 18) >> 13));
       expect(new int27(1) << 5, equals((new int27(1) << 18) >> 13));
       expect(new int27(1) << 5, equals((new int27(1) << 18) >> 13));
       expect(new tryte(1) << 5, isNot(equals(short(new tryte(1) << 18) >> 13)));
       expect(new tryte(1) << 5, isNot(equals(short(new tryte(1) << 18) >> 13)));
-      expect(new Bits(new tryte(5))[1], equals(~(new Bits(new tryte(-5))[1])));
-      expect(new Bits(new tryte(0)).incl(5).incl(8).incl(-1).excl(-1), equals(new Bits(new tryte(0)).incl(5).join(new Bits(new tryte(0)).incl(8))));
+      expect(new Trits(new tryte(5))[1], equals(~(new Trits(new tryte(-5))[1])));
+      expect(new Trits(new tryte(0)).incl(5).incl(8).incl(-1).excl(-1), equals(new Trits(new tryte(0)).incl(5).join(new Trits(new tryte(0)).incl(8))));
     });
     });
 
 
     test("conv", () {
     test("conv", () {

+ 28 - 28
lib/tri/trits.dart

@@ -1,6 +1,6 @@
 part of tri;
 part of tri;
 
 
-class Bits {
+class Trits {
 
 
   List<Tril> _trits;
   List<Tril> _trits;
 
 
@@ -12,29 +12,29 @@ class Bits {
   @override
   @override
   int get hashCode => _toInt();
   int get hashCode => _toInt();
 
 
-  bool operator ==(Bits that) {
+  bool operator ==(Trits that) {
     return this._toInt() == that._toInt();
     return this._toInt() == that._toInt();
   }
   }
 
 
-  Bits operator <<(int x) {
-    Bits ret = new Bits._new();
+  Trits operator <<(int x) {
+    Trits ret = new Trits._new();
     for (int i = x; i < _trits.length; i++) {
     for (int i = x; i < _trits.length; i++) {
       ret._trits[i] = this._trits[i - x];
       ret._trits[i] = this._trits[i - x];
     }
     }
     return ret;
     return ret;
   }
   }
 
 
-  Bits operator >>(int _x) {
-    Bits ret = new Bits._new();
+  Trits operator >>(int _x) {
+    Trits ret = new Trits._new();
     int x = -_x;
     int x = -_x;
-    for (int i = _trits.length - 2 + x; i >= 0; i--) {
+    for (int i = _trits.length - 1 + x; i >= 0; i--) {
       ret._trits[i] = this._trits[i + x.abs()];
       ret._trits[i] = this._trits[i + x.abs()];
     }
     }
     return ret;
     return ret;
   }
   }
 
 
-  Bits operator +(Bits that) {
-    Bits ret = new Bits._new();
+  Trits operator +(Trits that) {
+    Trits ret = new Trits._new();
     for (int i = 1; i < 28; i++) {
     for (int i = 1; i < 28; i++) {
       Tril x = this[i] | that[i];
       Tril x = this[i] | that[i];
       if (x != NULL) {
       if (x != NULL) {
@@ -44,8 +44,8 @@ class Bits {
     return ret;
     return ret;
   }
   }
 
 
-  Bits operator *(Bits that) {
-    Bits ret = new Bits._new();
+  Trits operator *(Trits that) {
+    Trits ret = new Trits._new();
     for (int i = 1; i < 28; i++) {
     for (int i = 1; i < 28; i++) {
       Tril x = this[i] & that[i];
       Tril x = this[i] & that[i];
       if (x != NULL) {
       if (x != NULL) {
@@ -55,8 +55,8 @@ class Bits {
     return ret;
     return ret;
   }
   }
 
 
-  Bits operator -(Bits that) {
-    Bits ret = new Bits._new();
+  Trits operator -(Trits that) {
+    Trits ret = new Trits._new();
     for (int i = 1; i < 28; i++) {
     for (int i = 1; i < 28; i++) {
       Tril x = this[i] & ~that[i];
       Tril x = this[i] & ~that[i];
       if (x != NULL) {
       if (x != NULL) {
@@ -66,8 +66,8 @@ class Bits {
     return ret;
     return ret;
   }
   }
 
 
-  Bits operator /(Bits that) {
-    Bits ret = new Bits._new();
+  Trits operator /(Trits that) {
+    Trits ret = new Trits._new();
     for (int i = 1; i < 28; i++) {
     for (int i = 1; i < 28; i++) {
       Tril x = ~eq(this[i], that[i]);
       Tril x = ~eq(this[i], that[i]);
       if (x != NULL) {
       if (x != NULL) {
@@ -77,8 +77,8 @@ class Bits {
     return ret;
     return ret;
   }
   }
 
 
-  Bits extract(int from, int to) {
-    Bits ret = new Bits._new();
+  Trits extract(int from, int to) {
+    Trits ret = new Trits._new();
     if (from >= _trits.length || to > _trits.length) throw new ArgumentError("$from, $to");
     if (from >= _trits.length || to > _trits.length) throw new ArgumentError("$from, $to");
     for (int i = from; i < to; i++) {
     for (int i = from; i < to; i++) {
       ret._trits[i] = this._trits[i];
       ret._trits[i] = this._trits[i];
@@ -86,8 +86,8 @@ class Bits {
     return ret;
     return ret;
   }
   }
 
 
-  Bits join(Bits that) {
-    Bits ret = new Bits._new();
+  Trits join(Trits that) {
+    Trits ret = new Trits._new();
     for (int i = 1; i < 28; i++) {
     for (int i = 1; i < 28; i++) {
       if (this[i] != NULL) ret = ret.incl(i * this[i].toInt);
       if (this[i] != NULL) ret = ret.incl(i * this[i].toInt);
       if (that[i] != NULL) ret = ret.incl(i * that[i].toInt);
       if (that[i] != NULL) ret = ret.incl(i * that[i].toInt);
@@ -95,16 +95,16 @@ class Bits {
     return ret;
     return ret;
   }
   }
 
 
-  Bits incl(int x) {
+  Trits incl(int x) {
     if (x == 0 || x.abs() > 27) throw new ArgumentError("$x");
     if (x == 0 || x.abs() > 27) throw new ArgumentError("$x");
-    Bits ret = this.extract(0, 27);
+    Trits ret = this.extract(0, 27);
     ret._trits[x.abs() - 1] = new Tril.fromInt(x.sign);
     ret._trits[x.abs() - 1] = new Tril.fromInt(x.sign);
     return ret;
     return ret;
   }
   }
 
 
-  Bits excl(int x) {
+  Trits excl(int x) {
     if (x == 0 || x.abs() > 27) throw new ArgumentError("$x");
     if (x == 0 || x.abs() > 27) throw new ArgumentError("$x");
-    Bits ret = this.extract(0, 27);
+    Trits ret = this.extract(0, 27);
     ret._trits[x.abs() - 1] = NULL;
     ret._trits[x.abs() - 1] = NULL;
     return ret;
     return ret;
   }
   }
@@ -161,11 +161,11 @@ class Bits {
     return new int27(_toInt());
     return new int27(_toInt());
   }
   }
 
 
-  Bits.fromTryte(tryte x) {
+  Trits.fromTryte(tryte x) {
     _fill(x.toInt(), 9);
     _fill(x.toInt(), 9);
   }
   }
 
 
-  Bits.fromInt27(int27 x) {
+  Trits.fromInt27(int27 x) {
     _fill(x.toInt(), 27);
     _fill(x.toInt(), 27);
   }
   }
 
 
@@ -192,11 +192,11 @@ class Bits {
     return ret;
     return ret;
   }
   }
 
 
-  factory Bits(tri_num x) {
-    if (x is tryte) return new Bits.fromTryte(x); else if (x is int27) return new Bits.fromInt27(x);
+  factory Trits(tri_num x) {
+    if (x is tryte) return new Trits.fromTryte(x); else if (x is int27) return new Trits.fromInt27(x);
   }
   }
 
 
-  Bits._new() {
+  Trits._new() {
     this.clear();
     this.clear();
   }
   }
 
 

+ 33 - 0
lib/trisc/asm.dart

@@ -0,0 +1,33 @@
+part of trisc;
+
+class asm{
+  static final tryte reg3 = new tryte(1);
+  static final tryte reg2 = -reg3;
+    
+  static final tryte mov = new tryte(0);
+  static final tryte lsl = new tryte(1);
+  static final tryte ror = new tryte(2);
+  static final tryte and = new tryte(3);
+  static final tryte ann = new tryte(4);
+  static final tryte ior = new tryte(5);
+  static final tryte xor = new tryte(6);
+  static final tryte add = new tryte(7);
+  static final tryte sub = -add;
+  static final tryte mul = new tryte(9);
+  static final tryte div = new tryte(10);
+  static final tryte mod = -div;
+    
+  static final tryte mem = new tryte(2);
+  static final tryte stw = new tryte(3);
+  static final tryte ldw = -stw;
+  static final tryte stt = new tryte(5);
+  static final tryte ldt = -stt;
+    
+  static final tryte br = new tryte(3);
+  static final tryte brr = new tryte(7);
+  static final tryte brc = -brr;
+  static final tryte lnk = new tryte(6);
+  static final tryte jmp = new tryte(5);
+  static final tryte nz = new tryte(4);
+  static final tryte eq = new tryte(3);
+}

+ 8 - 0
lib/trisc/core.dart

@@ -0,0 +1,8 @@
+library trisc;
+
+import 'package:tri/tri/mathe.dart';
+import 'package:tri/halt.dart';
+
+part 'asm.dart';
+part 'mem.dart';
+part 'proc.dart';

+ 70 - 0
lib/trisc/mem.dart

@@ -0,0 +1,70 @@
+part of trisc;
+
+abstract class RAM {
+  tryte operator [](int adr);
+  operator []=(int adr, tryte val);
+  int get length;
+}
+
+abstract class MMU extends RAM {
+  int get pos;
+  int get neg;
+}
+
+class MemFactory {
+  static RAM newRAM(int length) {
+    return new _stdRam(length);
+  }
+}
+
+class Mapper {
+  static const int WORD = 3;
+  RAM _ram;
+
+  int27 operator [](int adr) {
+    int a = adr * WORD;
+    halt.on(condition: a >= 0 && a < _ram.length);
+    List<tryte> x = new List(3);
+    for (int i = 0; i < WORD; i++) {
+      x[i] = _ram[a + i];
+    }
+    return mergeTryteList(x);
+  }
+
+  operator []=(int adr, int27 val) {
+    int a = adr * WORD;
+    halt.on(condition: a >= 0 && a < _ram.length);
+    List<tryte> x = splitInt27(val);
+    for (int i = 0; i < WORD; i++) {
+      _ram[a + i] = x[i];
+    }
+  }
+
+  Mapper(this._ram) {
+    halt.on(condition: _ram != null);
+  }
+}
+
+class _stdRam extends RAM {
+  List<tryte> data;
+
+  @override
+  int get length => data.length;
+
+  @override
+  tryte operator [](int adr) {
+    halt.on(condition: (adr >= 0 && adr < data.length), code: 20);
+    return data[adr];
+  }
+
+  @override
+  operator []=(int adr, tryte val) {
+    halt.on(condition: (adr >= 0 && adr < data.length), code: 20);
+    data[adr] = val;
+  }
+
+  _stdRam(int l) {
+    this.data = new List(l);
+    for (int i = 0; i < data.length; i++) data[i] = new tryte(0);
+  }
+}

+ 21 - 0
lib/trisc/mem_test.dart

@@ -0,0 +1,21 @@
+library mem_test;
+import 'package:unittest/unittest.dart';
+import 'package:tri/trisc/core.dart';
+import 'package:tri/tri/mathe.dart';
+
+void testMem(){
+  RAM r = MemFactory.newRAM(2*tryte.max.toInt()+1);
+      tryte a = tryte.min; 
+      int j = 0;
+      test("set/get", (){
+      do{
+          r[j] = a; tryte b = r[j]; 
+          expect(a, equals(b));
+          a += new tryte.one(); j++;
+      }while(a<tryte.max);
+   });
+   test("set word/get word", (){
+      new Mapper(r)[4] = int27.max;
+      expect(new Trits(new Mapper(r)[4]).toString(), equals(new Trits(int27.max).toString()));
+   });
+}

+ 54 - 0
lib/trisc/proc.dart

@@ -0,0 +1,54 @@
+part of trisc;
+
+abstract class CPU{
+  static const int ok = 0;
+  static const int stop = 1;
+  static const int skip = 2;
+  
+  void reset();
+  int next();
+}
+
+class ProcFactory{
+  static CPU newCPU(RAM mem, {int27 pc: null}){
+    if(pc==null)
+      pc = new int27.zero();
+    halt.on(condition: mem != null);
+    return new _cpu(mem, pc);
+  }
+}
+
+class _cpu extends CPU{
+  int27 ir;
+  int27 pc;
+  int step = 0;
+  
+  int27 start = new int27.zero();
+  RAM mem;
+  
+  Function handler(){
+    
+  }
+  
+  void parse(int27 ir, Function(e)){
+    
+  }
+  
+  @override
+  void reset(){
+    ir = new int27.zero();
+    pc = start;
+  }
+  
+  @override
+  int next(){
+    step++;
+    ir = new Mapper(mem)[pc.toInt()];
+    pc += new int27.one();
+    return parse(ir, handler());
+  }
+  
+  _cpu(this.mem, this.start){
+      reset();
+  }
+}

+ 1 - 0
pubspec.yaml

@@ -5,4 +5,5 @@ environment:
   sdk: '>=1.0.0 <2.0.0'
   sdk: '>=1.0.0 <2.0.0'
 dependencies:
 dependencies:
   browser: any
   browser: any
+  libpen: any
   unittest: any
   unittest: any

+ 26 - 1
web/main.dart

@@ -1,5 +1,30 @@
-//import 'package:tri/tri/mathe.dart';
+//import 'package:tri/tri/test.dart';
+//import 'package:tri/trisc/mem_test.dart';
+import 'package:tri/machine/app.dart' as machine;
+import 'package:libpen/libpen.dart' as libpen;
+import 'dart:html';
+import 'dart:async';
 
 
 void main() {
 void main() {
+  //testTri();
+  //testMem();
+  machine.init();
+
+//create the Console.
+libpen.Console console = new libpen.Console(40, 25);
+document.body.append(console.container);
+bool show = true;
+var blink;
+blink = (){
+  console.clear();
+  console.setChar(0, 0, '\$');
+  console.setChar(1, 0, '>');
+  if(show)
+    console.setChar(2, 0, '_');
+  show = !show;
+  console.flush();
+  new Future.delayed(new Duration(milliseconds: 500), blink);  
+};
+blink();
 
 
 }
 }