proc.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. part of trisc;
  2. class internal{
  3. static final haltMe = new tryte(-13);
  4. static final nop = new tryte.zero();
  5. }
  6. enum CPUresult{
  7. ok,
  8. stop,
  9. skip
  10. }
  11. abstract class CPU{
  12. bool get debug;
  13. set debug(bool);
  14. void reset();
  15. CPUresult next();
  16. }
  17. class ProcFactory{
  18. static CPU newCPU(RAM mem, {int27 pc: null}){
  19. if(pc==null)
  20. pc = new int27.zero();
  21. halt.on(condition: mem != null);
  22. return new _cpu(mem, pc);
  23. }
  24. }
  25. typedef CPUresult IRhandler(Operation op);
  26. class _cpu extends CPU{
  27. int27 ir;
  28. int27 pc;
  29. int step = 0;
  30. bool _debug = false;
  31. int27 start = new int27.zero();
  32. RAM mem;
  33. @override
  34. bool get debug => _debug;
  35. @override
  36. set debug(bool x){
  37. this._debug = x;
  38. }
  39. IRhandler handler(){
  40. var def = (Operation op){
  41. };
  42. return def;
  43. }
  44. CPUresult parse(int27 ir, IRhandler h){
  45. tryte format = short(ir >> 24);
  46. if(format == internal.nop){
  47. return CPUresult.skip;
  48. }else if(format == internal.haltMe){
  49. halt.on(condition: !debug, code: 146);
  50. return CPUresult.stop;
  51. }else{
  52. return h(Op.parse(ir));
  53. }
  54. }
  55. @override
  56. void reset(){
  57. ir = new int27.zero();
  58. pc = start;
  59. }
  60. @override
  61. CPUresult next(){
  62. step++;
  63. ir = new Mapper(mem)[pc.toInt()];
  64. fmt.fine("step $step: [$pc]");
  65. pc += new int27.one();
  66. if(pc.toInt() == new Mapper(mem).length)
  67. pc = new int27.zero();
  68. fmt.fine(new Trits(ir));
  69. CPUresult ret = parse(ir, handler());
  70. fmt.fine("step $step: $ret");
  71. return ret;
  72. }
  73. _cpu(this.mem, this.start){
  74. reset();
  75. }
  76. }