From 53dde881af6216b065fd1f7f303aef201ab24fa4 Mon Sep 17 00:00:00 2001 From: Filiprogrammer Date: Thu, 25 Apr 2019 23:37:26 +0200 Subject: [PATCH 1/5] Changed clock speed Added 64 Hz clock speed Changed default clock speed to 8 Hz --- src/ui/controller.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ui/controller.js b/src/ui/controller.js index 256d870..05e6665 100644 --- a/src/ui/controller.js +++ b/src/ui/controller.js @@ -12,8 +12,10 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'ass $scope.speeds = [{speed: 1, desc: "1 HZ"}, {speed: 4, desc: "4 HZ"}, {speed: 8, desc: "8 HZ"}, - {speed: 16, desc: "16 HZ"}]; - $scope.speed = 4; + {speed: 16, desc: "16 HZ"}, + {speed: 32, desc: "32 HZ"}, + {speed: 64, desc: "64 HZ"}]; + $scope.speed = 8; $scope.outputStartIndex = 232; $scope.code = "; Simple example\n; Writes Hello World to the output\n\n JMP start\nhello: DB \"Hello World!\" ; Variable\n DB 0 ; String terminator\n\nstart:\n MOV C, hello ; Point to var \n MOV D, 232 ; Point to output\n CALL print\n HLT ; Stop execution\n\nprint: ; print(C:*from, D:*to)\n PUSH A\n PUSH B\n MOV B, 0\n.loop:\n MOV A, [C] ; Get char from var\n MOV [D], A ; Write to output\n INC C\n INC D \n CMP B, [C] ; Check if end\n JNZ .loop ; jump if not\n\n POP B\n POP A\n RET"; From bdfb5ca5ed3896901eab6b814437e4c80d577185 Mon Sep 17 00:00:00 2001 From: Filiprogrammer Date: Sat, 27 Apr 2019 17:00:13 +0200 Subject: [PATCH 2/5] Advanced DB DB now accepts multiple values on one line seperated by a commas --- src/assembler/asm.js | 32 +++++++++++++++++++++----------- src/ui/controller.js | 2 +- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/assembler/asm.js b/src/assembler/asm.js index c12ffd5..a0517aa 100644 --- a/src/assembler/asm.js +++ b/src/assembler/asm.js @@ -1,4 +1,6 @@ app.service('assembler', ['opcodes', function (opcodes) { + function isNotEmpty(el) {return el.length !== 0;} + return { go: function (input) { // Use https://www.debuggex.com/ @@ -204,17 +206,25 @@ app.service('assembler', ['opcodes', function (opcodes) { switch (instr) { case 'DB': - p1 = getValue(match[op1_group]); - - if (p1.type === "number") - code.push(p1.value); - else if (p1.type === "numbers") - for (var j = 0, k = p1.value.length; j < k; j++) { - code.push(p1.value[j]); - } - else - throw "DB does not support this operand"; - + var dbline = lines[i]; + var dbtemp = dbline.indexOf(":"); + var dbtemp2 = dbline.indexOf(";"); + dbline = dbline.substring((dbtemp == -1) ? 0 : (dbtemp+1), (dbtemp2 == -1) ? (dbline.length) : dbtemp2); + var dblinesplit = dbline.match(/(".*?"|[^",\s]+)(?=\s*,|\s*$)/g); + var dbcount = 0; + while(dblinesplit[dbcount] !== undefined){ + p1 = getValue(dblinesplit[dbcount]); + + if (p1.type === "number") + code.push(p1.value); + else if (p1.type === "numbers") + for (var j = 0, k = p1.value.length; j < k; j++) { + code.push(p1.value[j]); + } + else + throw "DB does not support this operand"; + ++dbcount; + } break; case 'HLT': checkNoExtraArg('HLT', match[op1_group]); diff --git a/src/ui/controller.js b/src/ui/controller.js index 05e6665..b7b6017 100644 --- a/src/ui/controller.js +++ b/src/ui/controller.js @@ -18,7 +18,7 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'ass $scope.speed = 8; $scope.outputStartIndex = 232; - $scope.code = "; Simple example\n; Writes Hello World to the output\n\n JMP start\nhello: DB \"Hello World!\" ; Variable\n DB 0 ; String terminator\n\nstart:\n MOV C, hello ; Point to var \n MOV D, 232 ; Point to output\n CALL print\n HLT ; Stop execution\n\nprint: ; print(C:*from, D:*to)\n PUSH A\n PUSH B\n MOV B, 0\n.loop:\n MOV A, [C] ; Get char from var\n MOV [D], A ; Write to output\n INC C\n INC D \n CMP B, [C] ; Check if end\n JNZ .loop ; jump if not\n\n POP B\n POP A\n RET"; + $scope.code = "; Simple example\n; Writes Hello World to the output\n\n JMP start\nhello: DB \"Hello World!\", 0 ; Variable\n\nstart:\n MOV C, hello ; Point to var \n MOV D, 232 ; Point to output\n CALL print\n HLT ; Stop execution\n\nprint: ; print(C:*from, D:*to)\n PUSH A\n PUSH B\n MOV B, 0\n.loop:\n MOV A, [C] ; Get char from var\n MOV [D], A ; Write to output\n INC C\n INC D \n CMP B, [C] ; Check if end\n JNZ .loop ; jump if not\n\n POP B\n POP A\n RET"; $scope.reset = function () { cpu.reset(); From 1c84c730cad1f85aff0fe56d58888647d101a6cb Mon Sep 17 00:00:00 2001 From: Filiprogrammer Date: Sat, 27 Apr 2019 17:04:07 +0200 Subject: [PATCH 3/5] Saving & loading Added saving and loading --- index.html | 4 ++- src/ui/controller.js | 68 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index b4d7a9b..e56ef4e 100644 --- a/index.html +++ b/index.html @@ -19,6 +19,8 @@ + +
-

by Marco Schweighauser (2015) | MIT License | Blog

+

by Marco Schweighauser (2015) | MIT License | Blog | modified by Filip Schauer (2019)

diff --git a/src/ui/controller.js b/src/ui/controller.js index b7b6017..0941a8c 100644 --- a/src/ui/controller.js +++ b/src/ui/controller.js @@ -19,7 +19,75 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'ass $scope.outputStartIndex = 232; $scope.code = "; Simple example\n; Writes Hello World to the output\n\n JMP start\nhello: DB \"Hello World!\", 0 ; Variable\n\nstart:\n MOV C, hello ; Point to var \n MOV D, 232 ; Point to output\n CALL print\n HLT ; Stop execution\n\nprint: ; print(C:*from, D:*to)\n PUSH A\n PUSH B\n MOV B, 0\n.loop:\n MOV A, [C] ; Get char from var\n MOV [D], A ; Write to output\n INC C\n INC D \n CMP B, [C] ; Check if end\n JNZ .loop ; jump if not\n\n POP B\n POP A\n RET"; + + function toHexString(byteArray) { + return Array.from(byteArray, function(byte) { + return ('0' + (byte & 0xFF).toString(16)).slice(-2); + }).join(''); + } + + function hexStringToByte(str) { + if (!str) { + return new Uint8Array(); + } + + var a = []; + for (var i = 0, len = str.length; i < len; i += 2) { + a.push(parseInt(str.substr(i,2),16)); + } + + return new Uint8Array(a); + } + + function hexStringToWord(str) { + if (!str) { + return new Uint16Array(); + } + + var a = []; + for (var i = 0, len = str.length; i < len; i += 4) { + a.push(parseInt(str.substr(i,4),16)); + } + return new Uint16Array(a); + } + + $scope.save = function () { + var hexString = toHexString($scope.memory.data); + for(var i = 0; i < 256; ++i) { + hexString = hexString + ('000' + ($scope.mapping[i] & 0xFFFF).toString(16)).slice(-4); + } + hexString = hexString + $scope.code; + var uriContent = "data:application/octet-stream," + encodeURIComponent(hexString); + var newWindow = window.open(uriContent, 'neuesDokument'); + }; + + $scope.load = function (files) { + var f = files[0]; + if(f){ + var reader = new FileReader(); + reader.onload = function(e) { + + $scope.reset(); + var contents = e.target.result; + var hexString = contents.substr(0, 512); + var hexArr = hexStringToByte(hexString); + var mappingString = contents.substr(512, 1536); + var mappingArr = hexStringToWord(mappingString); + if($scope.mapping === undefined) $scope.mapping = {}; + for(var i = 0; i < 256; ++i){ + $scope.memory.data[i] = hexArr[i]; + $scope.mapping[i] = mappingArr[i]; + } + $scope.code = contents.substr(1536, f.size-1536); + $scope.$apply(); + }; + reader.readAsText(f); + } else { + alert("No file found"); + } + }; + $scope.reset = function () { cpu.reset(); memory.reset(); From 1765d68102b3573d594f62b8f9a4f4adfcea700e Mon Sep 17 00:00:00 2001 From: Filiprogrammer Date: Sat, 27 Apr 2019 17:06:24 +0200 Subject: [PATCH 4/5] Added "Auto Assemble" checkbox When checked automatically assembles the code when clicking the "Run" button --- index.html | 1 + src/ui/controller.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index e56ef4e..708b3da 100644 --- a/index.html +++ b/index.html @@ -19,6 +19,7 @@ + Auto Assemble diff --git a/src/ui/controller.js b/src/ui/controller.js index 0941a8c..3c3bbc0 100644 --- a/src/ui/controller.js +++ b/src/ui/controller.js @@ -96,7 +96,7 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'ass }; $scope.executeStep = function () { - if (!$scope.checkPrgrmLoaded()) { + if (!$scope.checkPrgrmLoaded() && document.getElementById("autoAssembleCheckBox").checked) { $scope.assemble(); } @@ -118,7 +118,7 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'ass var runner; $scope.run = function () { - if (!$scope.checkPrgrmLoaded()) { + if (!$scope.checkPrgrmLoaded() && document.getElementById("autoAssembleCheckBox").checked) { $scope.assemble(); } From 0992384e389a0f0b814a205efbe5a1a67948af9b Mon Sep 17 00:00:00 2001 From: Filiprogrammer Date: Sat, 27 Apr 2019 20:01:13 +0200 Subject: [PATCH 5/5] Added memory & register editing Registers and individual bytes of memory can now be edited using a secondary click on the register / byte of memory --- index.html | 20 ++++++++-------- src/ui/controller.js | 56 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/index.html b/index.html index 708b3da..68fdc37 100644 --- a/index.html +++ b/index.html @@ -85,15 +85,15 @@

CPU & Memory

-
{{ cpu.gpr[0] | number:displayHex }}
-
{{ cpu.gpr[1] | number:displayHex }}
-
{{ cpu.gpr[2] | number:displayHex }}
-
{{ cpu.gpr[3] | number:displayHex }}
-
{{ cpu.ip | number:displayHex }}
-
{{ cpu.sp | number:displayHex }}
- {{ cpu.zero | flag }} - {{ cpu.carry | flag }} - {{ cpu.fault | flag }} +
{{ cpu.gpr[0] | number:displayHex }}
+
{{ cpu.gpr[1] | number:displayHex }}
+
{{ cpu.gpr[2] | number:displayHex }}
+
{{ cpu.gpr[3] | number:displayHex }}
+
{{ cpu.ip | number:displayHex }}
+
{{ cpu.sp | number:displayHex }}
+ {{ cpu.zero | flag }} + {{ cpu.carry | flag }} + {{ cpu.fault | flag }} @@ -102,7 +102,7 @@

CPU & Memory

-
+
{{ m | number:displayHex }} {{ m | number:displayHex }} diff --git a/src/ui/controller.js b/src/ui/controller.js index 3c3bbc0..391e269 100644 --- a/src/ui/controller.js +++ b/src/ui/controller.js @@ -1,3 +1,15 @@ +app.directive('ngRightClick', function($parse) { + return function(scope, element, attrs) { + var fn = $parse(attrs.ngRightClick); + element.bind('contextmenu', function(event) { + scope.$apply(function() { + event.preventDefault(); + fn(scope, {$event:event}); + }); + }); + }; +}); + app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'assembler', function ($document, $scope, $timeout, cpu, memory, assembler) { $scope.memory = memory; $scope.cpu = cpu; @@ -18,7 +30,7 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'ass $scope.speed = 8; $scope.outputStartIndex = 232; - $scope.code = "; Simple example\n; Writes Hello World to the output\n\n JMP start\nhello: DB \"Hello World!\", 0 ; Variable\n\nstart:\n MOV C, hello ; Point to var \n MOV D, 232 ; Point to output\n CALL print\n HLT ; Stop execution\n\nprint: ; print(C:*from, D:*to)\n PUSH A\n PUSH B\n MOV B, 0\n.loop:\n MOV A, [C] ; Get char from var\n MOV [D], A ; Write to output\n INC C\n INC D \n CMP B, [C] ; Check if end\n JNZ .loop ; jump if not\n\n POP B\n POP A\n RET"; + $scope.code = "; Simple example\n; Writes Hello World to the output\n\n JMP start\nhello: DB \"Hello World!\", 0 ; Variable\n\nstart:\n MOV C, hello ; Point to var \n MOV D, 232 ; Point to output\n CALL print\n HLT ; Stop execution\n\nprint: ; print(C:*from, D:*to)\n PUSH A\n PUSH B\n MOV B, 0\n.loop:\n MOV A, [C] ; Get char from var\n MOV [D], A ; Write to output\n INC C\n INC D\n CMP B, [C] ; Check if end\n JNZ .loop ; jump if not\n\n POP B\n POP A\n RET"; function toHexString(byteArray) { return Array.from(byteArray, function(byte) { @@ -52,6 +64,46 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'ass return new Uint16Array(a); } + $scope.changeRegister = function (regIndex) { + var value = prompt("Please type in a hex value (1 for TRUE / 0 for FALSE)"); + if(value !== null && value !== ""){ + var val = parseInt(value, 16); + if(!isNaN(val)) { + if((val >= 0) && (val <= 0xFF)){ + if(regIndex == 0x80){ + cpu.ip = val; + } else if(regIndex == 0x81){ + cpu.sp = val; + } else if(regIndex == 0x82){ + cpu.zero = (val >= 1); + } else if(regIndex == 0x83){ + cpu.carry = (val >= 1); + } else if(regIndex == 0x84){ + cpu.fault = (val >= 1); + } else { + cpu.gpr[regIndex] = val; + } + $scope.$apply(); + } + } + } + return false; + }; + + $scope.changeMemory = function (memIndex) { + var value = prompt("Please type in a hex value"); + if(value !== null && value !== ""){ + var val = parseInt(value, 16); + if(!isNaN(val)) { + if((val >= 0) && (val <= 0xFF)){ + $scope.memory.data[memIndex] = val; + //$scope.$apply(); + } + } + } + return false; + }; + $scope.save = function () { var hexString = toHexString($scope.memory.data); for(var i = 0; i < 256; ++i) { @@ -81,6 +133,8 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'ass } $scope.code = contents.substr(1536, f.size-1536); $scope.$apply(); + //$document[0].getElementById('sourceCode').value = $scope.code; + //$scope.assemble(); }; reader.readAsText(f); } else {