Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added imput support #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A simulator which provides a simplified assembler syntax (based on NASM) and is
- 4 general purpose registers
- 256 bytes of memory
- Console output
- 16 Switch input

### How to build
Make sure you have <a href="http://www.gruntjs.com/" target="_blank">Grunt</a> installed to compile the `asmsimulator.js` script.
Expand Down
59 changes: 57 additions & 2 deletions assets/asmsimulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,44 @@ var app = angular.module('ASMSimulator', []);
cpu.reset();
return cpu;
}]);
;app.service('memory', [function () {
;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;
}]);;app.service('memory', [function () {
var memory = {
data: Array(256),
lastAccess: -1,
Expand Down Expand Up @@ -1347,9 +1384,10 @@ var app = angular.module('ASMSimulator', []);

return opcodes;
}]);
;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;
Expand All @@ -1370,6 +1408,7 @@ var app = angular.module('ASMSimulator', []);
$scope.reset = function () {
cpu.reset();
memory.reset();
input.reset();
$scope.error = '';
$scope.selectedLine = -1;
};
Expand Down Expand Up @@ -1502,6 +1541,22 @@ var app = angular.module('ASMSimulator', []);
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);
};
}]);
;app.filter('flag', function() {
return function(input) {
Expand Down
4 changes: 2 additions & 2 deletions assets/asmsimulator.min.js

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@
text-align: center;
}

.input {
display: inline-block;
width: 1.5em;
margin-right: 1px;
margin-bottom: 1px;
text-align: center;
}

.input-off {
background-color: #DFDFDF;
}

.input-on {
background-color: #5cb85c;
border-color: #4cae4c;
}

.instr-bg {
background-color: #E6F3FF;
}
Expand Down
10 changes: 10 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ <h4 class="panel-title">Code <small>(<a href="./instruction-set.html" target="_b
</div>
<div class="clearfix visible-xs visible-sm"></div>
<div class="col-lg-5 col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Input</h4>
</div>
<div class="panel-body source-code">
<div style="float:left;" ng-repeat="i in inputSwitches() track by $index">
<span ng-click="setInputSwitch($index)" ng-class="{'input': true, 'input-on': i,'input-off': !i}">{{ $index }}</span>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Output</h4>
Expand Down
8 changes: 8 additions & 0 deletions instruction-set.html
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ <h4>Other instructions</h4>
<pre>
HLT
</pre>

<h2>Input Switches</h2>
<b>NES buttons inspired switches.</b>
<p>
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.<br />
Those two address are part of the display so you can see they have on the display.
</p>

<hr style="margin-bottom:10px;"/>
<p><small>by Marco Schweighauser (2015) | MIT License | <a href="https://www.mschweighauser.com/make-your-own-assembler-simulator-in-javascript-part1/" target="_blank">Blog</a></small></p>
</div>
Expand Down
38 changes: 38 additions & 0 deletions src/emulator/input.js
Original file line number Diff line number Diff line change
@@ -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;
}]);
20 changes: 19 additions & 1 deletion src/ui/controller.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
};
Expand Down Expand Up @@ -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);
};
}]);