Skip to content

Commit

Permalink
Merge branch 'release/v0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Schweigi committed Oct 8, 2014
2 parents 8a4e7e9 + a74577d commit 25bb380
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 146 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function(grunt) {
separator: ';'
},
dist: {
src: ['src/**/*.js'],
src: ['src/app.js', 'src/**/*.js'],
dest: 'assets/<%= pkg.name %>.js'
}
},
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Run `grunt` to build the project.
### License
**The MIT License**

Copyright (c) 2013 Marco Schweighauser
Copyright (c) 2014 Marco Schweighauser

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
222 changes: 153 additions & 69 deletions assets/asmsimulator.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions assets/asmsimulator.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>Simple 8-bit Assembler Simulator in Javascript</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/style.css">
<script type="text/javascript" src="//use.typekit.net/tor0zlh.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
Expand Down Expand Up @@ -123,7 +123,7 @@ <h4 class="panel-title">CPU & Memory</h4>
<hr style="margin-top:10px;margin-bottom:10px;"/>
<p><small>by Marco Schweighauser (2014) | MIT License</small></p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="assets/asmsimulator.js"></script>
</body>
</html>
12 changes: 10 additions & 2 deletions instruction-set.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ <h4>Syntax</h4>
<pre>
Register: A, B, C, D
Address using a register: [A]
Address using SP register and offset: [SP+2]
Address using a constant: [100]
Address using a label: label
Constant: Any number between 0-255 (8bit unsigned)
Constant: Any number between 0..255 (8bit unsigned)
Offset for indirect addressing with SP: Integer between -16..+15 (sign is mandatory)
</pre>
<h4>MOV - Copy a value</h4>
<p>Copies a value from <i>src</i> to <i>dest</i>. The MOV instruction is the only one able to directly modify the memory.</p>
Expand Down Expand Up @@ -249,9 +251,15 @@ <h4>Stack instructions</h4>
<p>Pops a value from the stack to a register. This instruction will increase the SP.</p>
<pre>
POP reg
</pre>
<h4>Other instructions</h4>
<b>HLT - Stops the processor.</b>
<p>Stops operation of the processor. Hit Reset button to reset IP before restarting.</p>
<pre>
HLT
</pre>
<hr style="margin-bottom:10px;"/>
<p><small>by Marco Schweighauser (2013) | MIT License</small></p>
<p><small>by Marco Schweighauser (2014) | MIT License</small></p>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "asmsimulator",
"version": "0.2.2",
"version": "0.3.0",
"description": "Simple 8-bit Assembler Simulator in Javascript",
"author": "Marco Schweighauser",
"license": "MIT",
Expand Down
146 changes: 107 additions & 39 deletions src/assembler/asm.js

Large diffs are not rendered by default.

73 changes: 45 additions & 28 deletions src/emulator/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
return reg;
}
};
var indirectRegisterAddress=function(value) {
var reg = value % 8;

var base;
if (reg < self.gpr.length) {
base = self.gpr[reg];
} else {
base = self.sp;
}

var offset = Math.floor(value / 8);
if ( offset>15 ) {
offset = offset - 32;
}

return base+offset;
};
var checkOperation = function(value) {
self.zero = false;
self.carry = false;
Expand Down Expand Up @@ -83,8 +100,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
break;
case opcodes.MOV_REGADDRESS_TO_REG:
regTo = checkGPR(memory.load(++self.ip));
regFrom = checkGPR(memory.load(++self.ip));
self.gpr[regTo] = memory.load(self.gpr[regFrom]);
regFrom = memory.load(++self.ip);
self.gpr[regTo] = memory.load(indirectRegisterAddress(regFrom));
self.ip++;
break;
case opcodes.MOV_REG_TO_ADDRESS:
Expand All @@ -94,9 +111,9 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
self.ip++;
break;
case opcodes.MOV_REG_TO_REGADDRESS:
regTo = checkGPR(memory.load(++self.ip));
regTo = memory.load(++self.ip);
regFrom = checkGPR(memory.load(++self.ip));
memory.store(self.gpr[regTo], self.gpr[regFrom]);
memory.store(indirectRegisterAddress(regTo), self.gpr[regFrom]);
self.ip++;
break;
case opcodes.MOV_NUMBER_TO_REG:
Expand All @@ -112,9 +129,9 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
self.ip++;
break;
case opcodes.MOV_NUMBER_TO_REGADDRESS:
regTo = checkGPR(memory.load(++self.ip));
regTo = memory.load(++self.ip);
number = memory.load(++self.ip);
memory.store(self.gpr[regTo], number);
memory.store(indirectRegisterAddress(regTo), number);
self.ip++;
break;
case opcodes.ADD_REG_TO_REG:
Expand All @@ -125,8 +142,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
break;
case opcodes.ADD_REGADDRESS_TO_REG:
regTo = checkGPR(memory.load(++self.ip));
regFrom = checkGPR(memory.load(++self.ip));
self.gpr[regTo] = checkOperation(self.gpr[regTo] + memory.load(self.gpr[regFrom]));
regFrom = memory.load(++self.ip);
self.gpr[regTo] = checkOperation(self.gpr[regTo] + memory.load(indirectRegisterAddress(regFrom)));
self.ip++;
break;
case opcodes.ADD_ADDRESS_TO_REG:
Expand All @@ -149,8 +166,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
break;
case opcodes.SUB_REGADDRESS_FROM_REG:
regTo = checkGPR(memory.load(++self.ip));
regFrom = checkGPR(memory.load(++self.ip));
self.gpr[regTo] = checkOperation(self.gpr[regTo] - memory.load(self.gpr[regFrom]));
regFrom = memory.load(++self.ip);
self.gpr[regTo] = checkOperation(self.gpr[regTo] - memory.load(indirectRegisterAddress(regFrom)));
self.ip++;
break;
case opcodes.SUB_ADDRESS_FROM_REG:
Expand Down Expand Up @@ -183,8 +200,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
break;
case opcodes.CMP_REGADDRESS_WITH_REG:
regTo = checkGPR(memory.load(++self.ip));
regFrom = checkGPR(memory.load(++self.ip));
checkOperation(self.gpr[regTo] - memory.load(self.gpr[regFrom]));
regFrom = memory.load(++self.ip);
checkOperation(self.gpr[regTo] - memory.load(indirectRegisterAddress(regFrom)));
self.ip++;
break;
case opcodes.CMP_ADDRESS_WITH_REG:
Expand Down Expand Up @@ -309,8 +326,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
self.ip++;
break;
case opcodes.PUSH_REGADDRESS:
regFrom = checkGPR(memory.load(++self.ip));
push(memory.load(self.gpr[regFrom]));
regFrom = memory.load(++self.ip);
push(memory.load(indirectRegisterAddress(regFrom)));
self.ip++;
break;
case opcodes.PUSH_ADDRESS:
Expand Down Expand Up @@ -347,8 +364,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
self.ip++;
break;
case opcodes.MUL_REGADDRESS: // A = A * [REG]
regFrom = checkGPR(memory.load(++self.ip));
self.gpr[0] = checkOperation(self.gpr[0] * memory.load(self.gpr[regFrom]));
regFrom = memory.load(++self.ip);
self.gpr[0] = checkOperation(self.gpr[0] * memory.load(indirectRegisterAddress(regFrom)));
self.ip++;
break;
case opcodes.MUL_ADDRESS: // A = A * [NUMBER]
Expand All @@ -367,8 +384,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
self.ip++;
break;
case opcodes.DIV_REGADDRESS: // A = A / [REG]
regFrom = checkGPR(memory.load(++self.ip));
self.gpr[0] = checkOperation(division(memory.load(self.gpr[regFrom])));
regFrom = memory.load(++self.ip);
self.gpr[0] = checkOperation(division(memory.load(indirectRegisterAddress(regFrom))));
self.ip++;
break;
case opcodes.DIV_ADDRESS: // A = A / [NUMBER]
Expand All @@ -389,8 +406,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
break;
case opcodes.AND_REGADDRESS_WITH_REG:
regTo = checkGPR(memory.load(++self.ip));
regFrom = checkGPR(memory.load(++self.ip));
self.gpr[regTo] = checkOperation(self.gpr[regTo] & memory.load(self.gpr[regFrom]));
regFrom = memory.load(++self.ip);
self.gpr[regTo] = checkOperation(self.gpr[regTo] & memory.load(indirectRegisterAddress(regFrom)));
self.ip++;
break;
case opcodes.AND_ADDRESS_WITH_REG:
Expand All @@ -413,8 +430,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
break;
case opcodes.OR_REGADDRESS_WITH_REG:
regTo = checkGPR(memory.load(++self.ip));
regFrom = checkGPR(memory.load(++self.ip));
self.gpr[regTo] = checkOperation(self.gpr[regTo] | memory.load(self.gpr[regFrom]));
regFrom = memory.load(++self.ip);
self.gpr[regTo] = checkOperation(self.gpr[regTo] | memory.load(indirectRegisterAddress(regFrom)));
self.ip++;
break;
case opcodes.OR_ADDRESS_WITH_REG:
Expand All @@ -437,8 +454,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
break;
case opcodes.XOR_REGADDRESS_WITH_REG:
regTo = checkGPR(memory.load(++self.ip));
regFrom = checkGPR(memory.load(++self.ip));
self.gpr[regTo] = checkOperation(self.gpr[regTo] ^ memory.load(self.gpr[regFrom]));
regFrom = memory.load(++self.ip);
self.gpr[regTo] = checkOperation(self.gpr[regTo] ^ memory.load(indirectRegisterAddress(regFrom)));
self.ip++;
break;
case opcodes.XOR_ADDRESS_WITH_REG:
Expand Down Expand Up @@ -466,8 +483,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
break;
case opcodes.SHL_REGADDRESS_WITH_REG:
regTo = checkGPR(memory.load(++self.ip));
regFrom = checkGPR(memory.load(++self.ip));
self.gpr[regTo] = checkOperation(self.gpr[regTo] << memory.load(self.gpr[regFrom]));
regFrom = memory.load(++self.ip);
self.gpr[regTo] = checkOperation(self.gpr[regTo] << memory.load(indirectRegisterAddress(regFrom)));
self.ip++;
break;
case opcodes.SHL_ADDRESS_WITH_REG:
Expand All @@ -490,8 +507,8 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
break;
case opcodes.SHR_REGADDRESS_WITH_REG:
regTo = checkGPR(memory.load(++self.ip));
regFrom = checkGPR(memory.load(++self.ip));
self.gpr[regTo] = checkOperation(self.gpr[regTo] >>> memory.load(self.gpr[regFrom]));
regFrom = memory.load(++self.ip);
self.gpr[regTo] = checkOperation(self.gpr[regTo] >>> memory.load(indirectRegisterAddress(regFrom)));
self.ip++;
break;
case opcodes.SHR_ADDRESS_WITH_REG:
Expand Down
2 changes: 1 addition & 1 deletion src/ui/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ app.controller('Ctrl', ['$scope', '$timeout', 'cpu', 'memory', 'assembler', func
$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.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 DB 0 ; 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!\" ; 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.reset = function() {
cpu.reset();
Expand Down

0 comments on commit 25bb380

Please sign in to comment.