Output
diff --git a/instruction-set.html b/instruction-set.html
index fd2d8d5..ade0843 100644
--- a/instruction-set.html
+++ b/instruction-set.html
@@ -262,6 +262,14 @@
Other instructions
HLT
+
+
Input Switches
+
NES buttons inspired switches.
+
+ There are 16 switches you can turn on and off in any moment, those are mapped to the memory as bits on a byte on address 255 for 0-7 and 254 for 8-15.
+ Those two address are part of the display so you can see they have on the display.
+
+
by Marco Schweighauser (2015) | MIT License | Blog
diff --git a/src/emulator/input.js b/src/emulator/input.js
new file mode 100644
index 0000000..c3038b1
--- /dev/null
+++ b/src/emulator/input.js
@@ -0,0 +1,38 @@
+app.service('input', ['memory', function (memory) {
+ var input = {
+ data: Array(16),
+ reset: function(){
+ this.data.fill(false);
+ },
+ setBit : function(bit){
+ if (bit >= this.data.length) {
+ throw "Input access error: " + bit;
+ }
+
+ if(this.data[bit]){
+ this.data[bit] = false;
+ } else {
+ this.data[bit] = true;
+ }
+
+ var byte = 0;
+ for (var i = 0; i < 8; ++i) {
+ if(this.data[i]){
+ byte += Math.pow(2, i);
+ }
+ }
+ memory.store(255, byte);
+
+ byte = 0;
+ for (; i < 16; ++i) {
+ if(this.data[i]){
+ byte += Math.pow(2, i - 8);
+ }
+ }
+ memory.store(254, byte);
+ }
+ };
+
+ input.reset();
+ return input;
+}]);
\ No newline at end of file
diff --git a/src/ui/controller.js b/src/ui/controller.js
index 256d870..470a185 100644
--- a/src/ui/controller.js
+++ b/src/ui/controller.js
@@ -1,6 +1,7 @@
-app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'assembler', function ($document, $scope, $timeout, cpu, memory, assembler) {
+app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'input', 'assembler', function ($document, $scope, $timeout, cpu, memory, input, assembler) {
$scope.memory = memory;
$scope.cpu = cpu;
+ $scope.input = input;
$scope.error = '';
$scope.isRunning = false;
$scope.displayHex = true;
@@ -21,6 +22,7 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'ass
$scope.reset = function () {
cpu.reset();
memory.reset();
+ input.reset();
$scope.error = '';
$scope.selectedLine = -1;
};
@@ -153,4 +155,20 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'ass
return '';
}
};
+
+ $scope.inputSwitches = function() {
+ var switches = [];
+ console.log(input.data);
+ for (var i = 0; i < input.data.length; i++) {
+ switches.push(input.data[i]);
+ }
+ return switches;
+ };
+
+ $scope.setInputSwitch = function (index) {
+ if(index >= input.data.length) {
+ throw "Error input out of bounds";
+ }
+ input.setBit(index);
+ };
}]);