Skip to content

Commit

Permalink
Merge branch 'release/v0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Schweigi committed Feb 1, 2015
2 parents 20cc9a4 + 6c1cc7a commit 73fb96f
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 119 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.gitignore
.idea
.DS_Store
node_modules/*
ASMSimulator.iml
77 changes: 62 additions & 15 deletions assets/asmsimulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var app = angular.module('ASMSimulator', []);;app.service('assembler', ['opcodes
var mapping = {};
// Hash map of label used to replace the labels after the assembler generated the code
var labels = {};
// Hash of uppercase labels used to detect duplicates
var normalizedLabels = {};

var lines = input.split('\n'); // Split text into code lines

Expand Down Expand Up @@ -136,11 +138,7 @@ var app = angular.module('ASMSimulator', []);;app.service('assembler', ['opcodes
};
// Allowed: Label
var parseLabel = function(input) {
if (regexLabel.exec(input)) {
return input.toUpperCase();
} else {
return undefined;
}
return regexLabel.exec(input) ? input : undefined;
};
var getValue = function(input) {
switch(input.slice(0,1)) {
Expand All @@ -167,12 +165,12 @@ var app = angular.module('ASMSimulator', []);;app.service('assembler', ['opcodes
}
};
var addLabel = function(label) {
label = label.toUpperCase();
if (label in labels)
var upperLabel = label.toUpperCase();
if (upperLabel in normalizedLabels)
throw "Duplicate label: " + label;

if (label === "A" || label === "B" || label === "C" || label === "D")
throw "Label contains keyword: " + label;
if (upperLabel === "A" || upperLabel === "B" || upperLabel === "C" || upperLabel === "D")
throw "Label contains keyword: " + upperLabel;

labels[label] = code.length;
};
Expand Down Expand Up @@ -652,9 +650,9 @@ var app = angular.module('ASMSimulator', []);;app.service('assembler', ['opcodes
self.gpr[reg] = value;
} else if(reg == self.gpr.length) {
self.sp=value;
if (self.sp < 0) { // Not likely to happen, since we always get here after checkOpertion().
if (self.sp < self.minSP) { // Not likely to happen, since we always get here after checkOpertion().
throw "Stack overflow";
} else if (self.sp > 231) {
} else if (self.sp > self.maxSP) {
throw "Stack underflow";
}
} else {
Expand Down Expand Up @@ -713,13 +711,13 @@ var app = angular.module('ASMSimulator', []);;app.service('assembler', ['opcodes
};
var push = function(value) {
memory.store(self.sp--, value);
if (self.sp < 0) {
if (self.sp < self.minSP) {
throw "Stack overflow";
}
};
var pop = function() {
var value = memory.load(++self.sp);
if (self.sp > 231) {
if (self.sp > self.maxSP) {
throw "Stack underflow";
}

Expand Down Expand Up @@ -1191,9 +1189,11 @@ var app = angular.module('ASMSimulator', []);;app.service('assembler', ['opcodes
},
reset: function() {
var self = this;
self.maxSP = 231;
self.minSP = 0;

self.gpr = [0, 0, 0, 0];
self.sp = 231;
self.sp = self.maxSP;
self.ip = 0;
self.zero = false;
self.carry = false;
Expand Down Expand Up @@ -1318,15 +1318,20 @@ var app = angular.module('ASMSimulator', []);;app.service('assembler', ['opcodes
};

return opcodes;
}]);;app.controller('Ctrl', ['$scope', '$timeout', 'cpu', 'memory', 'assembler', function($scope, $timeout, cpu, memory, assembler) {
}]);;app.controller('Ctrl', ['$document', '$scope', '$timeout', 'cpu', 'memory', 'assembler', function($document, $scope, $timeout, cpu, memory, assembler) {
$scope.memory = memory;
$scope.cpu = cpu;
$scope.error = '';
$scope.isRunning = false;
$scope.displayHex = true;
$scope.displayInstr = true;
$scope.displayA = false;
$scope.displayB = false;
$scope.displayC = false;
$scope.displayD = false;
$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;
$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";

Expand Down Expand Up @@ -1423,6 +1428,48 @@ var app = angular.module('ASMSimulator', []);;app.service('assembler', ['opcodes
}
}
};

$scope.jumpToLine = function(index) {
$document[0].getElementById('sourceCode').scrollIntoView();
$scope.selectedLine = $scope.mapping[index];
};


$scope.isInstruction = function(index) {
return $scope.mapping !== undefined &&
$scope.mapping[index] !== undefined &&
$scope.displayInstr;
};

$scope.getMemoryCellCss = function(index) {
if (index >= $scope.outputStartIndex) {
return 'output-bg';
} else if ($scope.isInstruction(index)) {
return 'instr-bg';
} else if (index > cpu.sp && index <= cpu.maxSP) {
return 'stack-bg';
} else {
return '';
}
};

$scope.getMemoryInnerCellCss = function(index) {
if (index === cpu.ip) {
return 'marker marker-ip';
} else if (index === cpu.sp) {
return 'marker marker-sp';
} else if (index === cpu.gpr[0] && $scope.displayA) {
return 'marker marker-a';
} else if (index === cpu.gpr[1] && $scope.displayB) {
return 'marker marker-b';
} else if (index === cpu.gpr[2] && $scope.displayC) {
return 'marker marker-c';
} else if (index === cpu.gpr[3] && $scope.displayD) {
return 'marker marker-d';
} else {
return '';
}
};
}]);;app.filter('flag', function() {
return function(input) {
return input.toString().toUpperCase();
Expand Down
4 changes: 2 additions & 2 deletions assets/asmsimulator.min.js

Large diffs are not rendered by default.

67 changes: 34 additions & 33 deletions assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,60 @@
}

.output, .output-bg {
background-color:#DFDFDF;
background-color: #DFDFDF;
}

.output {
width: 1em;
margin-right:1px;
margin-bottom:1px;
text-align:center;
margin-right: 1px;
margin-bottom: 1px;
text-align: center;
}

.instr-bg {
background-color:#E6F3FF;
background-color: #E6F3FF;
}

.instr-bg a {
color: inherit;
}

.stack-bg {
background-color: #F8DCB4;
}

.memory-block {
width:1.8em;
display:inline-block;
text-align:center;
width: 1.8em;
display: inline-block;
text-align: center;
}

.marker {
width: 1.8em;
color: #FFF;
border-radius: 2px;
}

.marker-ip {
width:1.8em;
background-color:#428BCA;
color:#FFFFFF;
border-radius:2px;
background-color: #428BCA;
}

.marker-sp {
width:1.8em;
background-color:#EEA236;
color:#FFFFFF;
border-radius:2px;
background-color: #EEA236;
}

.marker-a {
width:1.8em;
background-color:#4d8c20; /*6EC72E; /* 83ED37; */
color:#FFFFFF;
border-radius:2px;
}.marker-b {
width:1.8em;
background-color:#22d19d; /* 37EDA1; */
color:#FFFFFF;
border-radius:2px;
background-color: #4D8C20;
}

.marker-b {
background-color: #22D19D;
}

.marker-c {
width:1.8em;
background-color:#A137ED;
color:#FFFFFF;
border-radius:2px;
background-color: #A137ED;
}

.marker-d {
width:1.8em;
background-color:#e81f1f; /*ED3783; */
color:#FFFFFF;
border-radius:2px;
background-color: #E81F1F;
}
103 changes: 50 additions & 53 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,64 +31,35 @@
<div class="col-lg-7 col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Code <small>(<a href="./instruction-set.html" target="_blank">Instruction Set</a>)</small></h4>
<h4 class="panel-title">Code <small>(<a href="./instruction-set.html" target="_blank" style="color: #337AB7">Instruction Set</a>)</small></h4>
</div>
<div class="panel-body">
<form role="form">
<textarea class="form-control source-code"
rows="14"
<textarea id="sourceCode"
class="form-control source-code"
style="margin-bottom:5px;"
rows="35"
tab-support
select-line
ng-model="code"></textarea>
<button type="button" class="btn btn-default" ng-click="assemble()">Assemble</button>
</form>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Output</h4>
</div>
<div class="panel-body source-code">
<div style="float:left;" class="output"
ng-style="($index % 8 === 0 && {'clear': 'left'}) || {'float': 'left'}"
ng-repeat="m in memory.data | startFrom: 232 track by $index">
<span>{{ getChar(m) }}</span>
</div>
</div>
</div>
</div>
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Labels</h4>
</div>
<div class="panel-body source-code">
<table class="table table-condensed table-striped">
<tr>
<th>Name</th>
<th>Address</th>
<th>Value</th>
</tr>
<tr ng-repeat="(name, value) in labels">
<td>{{ name }}</td>
<td>{{ value | number:displayHex }}</td>
<td>{{ memory.data[value] | number:displayHex }}
<span ng-if="memory.data[value] >= 32 && memory.data[value] <= 126">
('{{ getChar(memory.data[value]) }}')
</span>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</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">Output</h4>
</div>
<div class="panel-body source-code">
<div style="float:left;" class="output"
ng-repeat="m in memory.data | startFrom: outputStartIndex track by $index">
<span>{{ getChar(m) }}</span>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">CPU & Memory</h4>
Expand All @@ -111,12 +82,12 @@ <h4 class="panel-title">CPU & Memory</h4>
</thead>
<tbody>
<tr style="text-align:center;" class="source-code">
<td><div style="margin:auto;" ng-class="displayA && 'marker-a'"><small>{{ cpu.gpr[0] | number:displayHex }}</small></div></td>
<td><div style="margin:auto;" ng-class="displayB && 'marker-b'"><small>{{ cpu.gpr[1] | number:displayHex }}</small></div></td>
<td><div style="margin:auto;" ng-class="displayC && 'marker-c'"><small>{{ cpu.gpr[2] | number:displayHex }}</small></div></td>
<td><div style="margin:auto;" ng-class="displayD && 'marker-d'"><small>{{ cpu.gpr[3] | number:displayHex }}</small></div></td>
<td><div style="margin:auto;" class="marker-ip"><small>{{ cpu.ip | number:displayHex }}</small></div></td>
<td><div style="margin:auto;" class="marker-sp"><small>{{ cpu.sp | number:displayHex }}</small></div></td>
<td><div style="margin:auto;" ng-class="displayA && 'marker marker-a'"><small>{{ cpu.gpr[0] | number:displayHex }}</small></div></td>
<td><div style="margin:auto;" ng-class="displayB && 'marker marker-b'"><small>{{ cpu.gpr[1] | number:displayHex }}</small></div></td>
<td><div style="margin:auto;" ng-class="displayC && 'marker marker-c'"><small>{{ cpu.gpr[2] | number:displayHex }}</small></div></td>
<td><div style="margin:auto;" ng-class="displayD && 'marker marker-d'"><small>{{ cpu.gpr[3] | number:displayHex }}</small></div></td>
<td><div style="margin:auto;" class="marker marker-ip"><small>{{ cpu.ip | number:displayHex }}</small></div></td>
<td><div style="margin:auto;" class="marker marker-sp"><small>{{ cpu.sp | number:displayHex }}</small></div></td>
<td><small>{{ cpu.zero | flag }}</small></td>
<td><small>{{ cpu.carry | flag }}</small></td>
<td><small>{{ cpu.fault | flag }}</small></td>
Expand All @@ -127,9 +98,12 @@ <h4 class="panel-title">CPU & Memory</h4>
<div style="width:29em;" class="source-code">
<div class="memory-block"
ng-repeat="m in memory.data track by $index"
ng-class="($index >= 232 && 'output-bg' || (mapping[$index] !== undefined && displayInstr && 'instr-bg' || ''))">
<div ng-class="(cpu.ip === $index && 'marker-ip' || (cpu.sp === $index && 'marker-sp' || ( cpu.gpr[0] === $index && displayA && 'marker-a' || ( cpu.gpr[1] === $index && displayB && 'marker-b' || ( cpu.gpr[2] === $index && displayC && 'marker-c' || ( cpu.gpr[3] === $index && displayD && 'marker-d' || ''))))))">
<small>{{ m | number:displayHex }}</small>
ng-class="getMemoryCellCss($index)">
<div ng-class="getMemoryInnerCellCss($index)" ng-switch="isInstruction($index)">
<small ng-switch-default>{{ m | number:displayHex }}</small>
<a ng-switch-when="true" ng-click="jumpToLine($index)">
<small>{{ m | number:displayHex }}</small>
</a>
</div>
</div>
</div>
Expand Down Expand Up @@ -161,6 +135,29 @@ <h4 class="panel-title">CPU & Memory</h4>
</p>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Labels</h4>
</div>
<div class="panel-body source-code">
<table class="table table-condensed table-striped">
<tr>
<th>Name</th>
<th>Address</th>
<th>Value</th>
</tr>
<tr ng-repeat="(name, value) in labels">
<td>{{ name }}</td>
<td><a ng-click="jumpToLine(value)">{{ value | number:displayHex }}</a></td>
<td>{{ memory.data[value] | number:displayHex }}
<span ng-if="memory.data[value] >= 32 && memory.data[value] <= 126">
('{{ getChar(memory.data[value]) }}')
</span>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<hr style="margin-top:10px;margin-bottom:10px;"/>
Expand Down
Loading

0 comments on commit 73fb96f

Please sign in to comment.