Repository: shuding/y86 Branch: gh-pages Commit: b8a63b04d4c1 Files: 42 Total size: 90.0 KB Directory structure: gitextract_4np5s4he/ ├── .gitignore ├── CNAME ├── LICENSE ├── README.md ├── bower.json ├── fonts/ │ └── FontAwesome.otf ├── index.html └── src/ ├── font/ │ └── Jura/ │ └── OFL.txt ├── gulpfile.js ├── index.html ├── script/ │ ├── controllers/ │ │ └── main.js │ ├── directives/ │ │ ├── board.js │ │ ├── clock.js │ │ ├── code.js │ │ ├── condition.js │ │ ├── control.js │ │ ├── cpi.js │ │ ├── pipe.js │ │ ├── register.js │ │ └── stack.js │ └── kernels/ │ ├── alu.js │ ├── constant.js │ ├── cpu.js │ ├── main.js │ ├── memory.js │ ├── parser.js │ ├── register.js │ └── utils.js ├── style/ │ ├── board.css │ ├── clock.css │ ├── code.css │ ├── condition.css │ ├── control.css │ ├── cpi.css │ ├── main.css │ ├── pipe.css │ ├── register.css │ └── stack.css └── test/ ├── Forward.yo ├── Halt.yo ├── List_Sum.yo └── asum.yo ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Created by https://www.gitignore.io ### JetBrains ### # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm *.iml ## Directory-based project format: .idea/ # if you remove the above rule, at least ignore the following: # User-specific stuff: # .idea/workspace.xml # .idea/tasks.xml # .idea/dictionaries # Sensitive or high-churn files: # .idea/dataSources.ids # .idea/dataSources.xml # .idea/sqlDataSources.xml # .idea/dynamic.xml # .idea/uiDesigner.xml # Gradle: # .idea/gradle.xml # .idea/libraries # Mongo Explorer plugin: # .idea/mongoSettings.xml ## File-based project format: *.ipr *.iws ## Plugin-specific files: # IntelliJ /out/ # mpeltonen/sbt-idea plugin .idea_modules/ # JIRA plugin atlassian-ide-plugin.xml # Crashlytics plugin (for Android Studio and IntelliJ) com_crashlytics_export_strings.xml crashlytics.properties crashlytics-build.properties ### OSX ### .DS_Store .AppleDouble .LSOverride # Icon must end with two \r Icon # Thumbnails ._* # Files that might appear in the root of a volume .DocumentRevisions-V100 .fseventsd .Spotlight-V100 .TemporaryItems .Trashes .VolumeIcon.icns # Directories potentially created on remote AFP share .AppleDB .AppleDesktop Network Trash Folder Temporary Items .apdisk # ignored bower components bower_components ================================================ FILE: CNAME ================================================ y86.js.org ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2016 Parabola 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Y86 Emulator [![](https://img.shields.io/website-up-down-green-red/http/y86.js.org.svg?label=y86.js.org)](http://y86.js.org) [y86.js.org](http://y86.js.org) is a JavaScript powered, Y86 pipeline CPU emulator with a sci-fi UI (Y86 is a classic project in book CS:APP3e). ![screenshot](screenshot.png) ## Details 1. Only supports .yo code 2. Using Angular + Angular-route 3. move.js for CSS3 animations 4. Implemented a simple CommonJS module loader and a canvas CPI curve graph plotter ## Notes 1. All dependencies are included. So I can use `gh-pages` as the main branch. 2. To open it locally, please host the static files under a HTTP server. The `file://` protocol won't work. ## Acknowledgement - Author: Shu Ding - Special thanks: Linghao Zhang, Yifu Yu. - License: the MIT license ================================================ FILE: bower.json ================================================ { "name": "Y86", "version": "0.0.1", "authors": [ "quietshu " ], "description": "Y86-Simulator for Fudan ICS 2015", "main": "index.html", "keywords": [ "Y86" ], "license": "MIT", "ignore": [ "**/.*", "node_modules", "bower_components", "test", "tests" ], "dependencies": { "angular-route": "~1.4.0", "requirejs": "~2.1.18", "angular": "~1.4.0", "move.js": "~0.3.3" } } ================================================ FILE: index.html ================================================ Y86 Emulator

Y86 Emulator
Source code: GitHub, by @shuding

{{ $index * 4 | toHex: 4 }}{{ data[0] | toHex: 2 }}{{ data[1] | toHex: 2 }}{{ data[2] | toHex: 2 }}{{ data[3] | toHex: 2 }}
================================================ FILE: src/font/Jura/OFL.txt ================================================ Copyright (c) 2009, 2010, 2011 Daniel Johnson (). This Font Software is licensed under the SIL Open Font License, Version 1.1. This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL ----------------------------------------------------------- SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 ----------------------------------------------------------- PREAMBLE The goals of the Open Font License (OFL) are to stimulate worldwide development of collaborative font projects, to support the font creation efforts of academic and linguistic communities, and to provide a free and open framework in which fonts may be shared and improved in partnership with others. The OFL allows the licensed fonts to be used, studied, modified and redistributed freely as long as they are not sold by themselves. The fonts, including any derivative works, can be bundled, embedded, redistributed and/or sold with any software provided that any reserved names are not used by derivative works. The fonts and derivatives, however, cannot be released under any other type of license. The requirement for fonts to remain under this license does not apply to any document created using the fonts or their derivatives. DEFINITIONS "Font Software" refers to the set of files released by the Copyright Holder(s) under this license and clearly marked as such. This may include source files, build scripts and documentation. "Reserved Font Name" refers to any names specified as such after the copyright statement(s). "Original Version" refers to the collection of Font Software components as distributed by the Copyright Holder(s). "Modified Version" refers to any derivative made by adding to, deleting, or substituting -- in part or in whole -- any of the components of the Original Version, by changing formats or by porting the Font Software to a new environment. "Author" refers to any designer, engineer, programmer, technical writer or other person who contributed to the Font Software. PERMISSION & CONDITIONS Permission is hereby granted, free of charge, to any person obtaining a copy of the Font Software, to use, study, copy, merge, embed, modify, redistribute, and sell modified and unmodified copies of the Font Software, subject to the following conditions: 1) Neither the Font Software nor any of its individual components, in Original or Modified Versions, may be sold by itself. 2) Original or Modified Versions of the Font Software may be bundled, redistributed and/or sold with any software, provided that each copy contains the above copyright notice and this license. These can be included either as stand-alone text files, human-readable headers or in the appropriate machine-readable metadata fields within text or binary files as long as those fields can be easily viewed by the user. 3) No Modified Version of the Font Software may use the Reserved Font Name(s) unless explicit written permission is granted by the corresponding Copyright Holder. This restriction only applies to the primary font name as presented to the users. 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font Software shall not be used to promote, endorse or advertise any Modified Version, except to acknowledge the contribution(s) of the Copyright Holder(s) and the Author(s) or with their explicit written permission. 5) The Font Software, modified or unmodified, in part or in whole, must be distributed entirely under this license, and must not be distributed under any other license. The requirement for fonts to remain under this license does not apply to any document created using the Font Software. TERMINATION This license becomes null and void if any of the above conditions are not met. DISCLAIMER THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE. ================================================ FILE: src/gulpfile.js ================================================ var gulp = require('gulp'), usemin = require('gulp-usemin'), wrap = require('gulp-wrap'), connect = require('gulp-connect'), ngannotate = require('gulp-ng-annotate'), watch = require('gulp-watch'), minifyCss = require('gulp-minify-css'), minifyJs = require('gulp-uglify'), concat = require('gulp-concat'), less = require('gulp-less'), rename = require('gulp-rename'), minifyHTML = require('gulp-minify-html'); var paths = { scripts: 'src/script/**/*.*', styles: 'src/style/**/*.*', images: 'src/image/**/*.*', templates: 'src/template/**/*.html', index: 'src/index.html', fonts: 'src/font/**/*.{ttf,woff,woff2,eof,svg}' }; /** * Handle bower components from index */ gulp.task('usemin', function() { return gulp.src(paths.index) .pipe(usemin({ //js: [/* ngannotate(), minifyJs(), */'concat'], css: [/*minifyCss({keepSpecialComments: 0}), */'concat'], })) .pipe(gulp.dest('dist/')); }); /** * Copy assets */ gulp.task('build-assets', ['copy-fonts']); gulp.task('copy-fonts', function() { return gulp.src(paths.fonts) .pipe(rename({ dirname: '/fonts' })) .pipe(gulp.dest('dist/')); }); /** * Handle custom files */ gulp.task('build-custom', ['custom-images', 'custom-js', 'custom-css', 'custom-templates']); gulp.task('custom-images', function() { return gulp.src(paths.images) .pipe(gulp.dest('dist/img')); }); gulp.task('custom-js', function() { return gulp.src(paths.scripts) .pipe(ngannotate()) .pipe(minifyJs()) .pipe(concat('dashboard.min.js')) .pipe(gulp.dest('dist/js')); }); gulp.task('custom-css', function() { return gulp.src(paths.styles) //.pipe(minifyCss()) .pipe(concat('dashboard.min.css')) .pipe(gulp.dest('dist/css')); }); gulp.task('custom-templates', function() { return gulp.src(paths.templates) .pipe(minifyHTML()) .pipe(gulp.dest('dist/templates')); }); /** * Watch custom files */ gulp.task('watch', function() { gulp.watch([paths.images], ['custom-images']); gulp.watch([paths.styles], ['custom-css']); gulp.watch([paths.scripts], ['custom-js']); gulp.watch([paths.templates], ['custom-templates']); gulp.watch([paths.index], function () { return gulp.src(paths.index) .pipe(usemin({ js: ['concat'], // do not minify!!!!!! css: ['concat'], })) .pipe(gulp.dest('dist/')); });//['usemin']); }); /** * Live reload server */ gulp.task('webserver', function() { connect.server({ root: 'dist', livereload: true, port: 8888 }); }); gulp.task('livereload', function() { gulp.src(['dist/**/*.*']) .pipe(watch()) .pipe(connect.reload()); }); /** * Gulp tasks */ gulp.task('build', ['usemin', 'build-assets', 'build-custom']); // do not usemin with livereload gulp.task('default', ['usemin', 'build-assets', 'build-custom', 'webserver', 'livereload', 'watch']); ================================================ FILE: src/index.html ================================================ Y86 Simulator
{{ $index * 4 | toHex: 4 }}{{ data[0] | toHex: 2 }}{{ data[1] | toHex: 2 }}{{ data[2] | toHex: 2 }}{{ data[3] | toHex: 2 }}
================================================ FILE: src/script/controllers/main.js ================================================ angular.module('y86', ['ngRoute']).controller('mainCtrl', ['$scope', mainCtrl]).filter("toHex", function () { return function (input, digest) { if (typeof input == 'undefined' || input === null) { return ''; } if (input < 0) { input = 0xFFFFFFFF + input + 1; } var ret = input.toString(16); while (ret.length < digest) { ret = '0' + ret; } return ret; }; }).filter("round", function () { return function (input) { return Math.round(input * 1000) / 1000 || 0; }; }); function mainCtrl($scope) { var Constant = require('script/kernels/constant'); var Parser = require('script/kernels/parser'); $scope.constantMap = { stat: { 0: 'STAT_BUB', 1: 'STAT_AOK', 2: 'STAT_HLT', 3: 'STAT_ADR', 4: 'STAT_INS', 5: 'STAT_PIP' }, icode: { 0: 'I_HALT', 1: 'I_NOP', 2: 'I_RRMOVL', 3: 'I_IRMOVL', 4: 'I_RMMOVL', 5: 'I_MRMOVL', 6: 'I_OPL', 7: 'I_JXX', 8: 'I_CALL', 9: 'I_RET', 0xa: 'I_PUSHL', 0xb: 'I_POPL', 0xc: 'I_IADDL', 0xd: 'I_LEAVE' }, reg: { 0: 'R_EAX', 1: 'R_ECX', 2: 'R_EDX', 3: 'R_EBX', 4: 'R_ESP', 5: 'R_EBP', 6: 'R_ESI', 7: 'R_EDI', 15: 'R_NONE' } }; $scope.$safeApply = function () { if ($scope.$root.$$phase != '$apply' && $scope.$root.$$phase != '$digest') { $scope.$apply(); } }; $scope.state = { icons: ['play', 'pause'], icon: 0, loaded: 0 }; $scope.registers = { eax: 0, ecx: 0, edx: 0, ebx: 0, esp: 0, ebp: 0, esi: 0, edi: 0 }; $scope.memory = { data: [], block: [] }; $scope.conditions = { zf: 0, sf: 0, of: 0 }; $scope.clock = { data: 0 }; $scope.code = { current: 0, height: 0, scrollTop: 0, scrollHeight: 0, lineHeight: 14 * 1.2, currentTab: 0, tabs: ['asum', 'List_Sum', 'Forward'], tabFiles: ['test/asum.yo', 'test/List_Sum.yo', 'test/Forward.yo'], fileCache: ['', '', ''] }; $scope.player = { hz: 10 }; // Functions $scope.initParser = function (parser) { if ($scope.parser && $scope.parser !== parser) delete $scope.parser; $scope.parser = parser; $scope.code.lines = parser.syntaxs; $scope.clock.data = parser.CPU.cycle; $scope.memory.data = parser.CPU.Memory.data; // Clock Cycle $scope.$watch('parser.CPU.cycle', function (clock) { $scope.clock.data = clock; }); // Registers $scope.$watch('parser.CPU.Register[' + Constant['R_EAX'] + ']', function (value) { $scope.registers['eax'] = value; }); $scope.$watch('parser.CPU.Register[' + Constant['R_ECX'] + ']', function (value) { $scope.registers['ecx'] = value; }); $scope.$watch('parser.CPU.Register[' + Constant['R_EDX'] + ']', function (value) { $scope.registers['edx'] = value; }); $scope.$watch('parser.CPU.Register[' + Constant['R_EBX'] + ']', function (value) { $scope.registers['ebx'] = value; }); $scope.$watch('parser.CPU.Register[' + Constant['R_ESP'] + ']', function (value) { $scope.registers['esp'] = value; }); $scope.$watch('parser.CPU.Register[' + Constant['R_EBP'] + ']', function (value) { $scope.registers['ebp'] = value; }); $scope.$watch('parser.CPU.Register[' + Constant['R_ESI'] + ']', function (value) { $scope.registers['esi'] = value; }); $scope.$watch('parser.CPU.Register[' + Constant['R_EDI'] + ']', function (value) { $scope.registers['edi'] = value; }); // PC $scope.$watch('parser.CPU.Input.F_predPC', function (value) { $scope.code.current = $scope.parser.map[value] || $scope.code.current; }); // Condition Codes $scope.$watch('parser.CPU.ALU.data.ZF', function (value) { $scope.conditions.zf = value; }); $scope.$watch('parser.CPU.ALU.data.SF', function (value) { $scope.conditions.sf = value; }); $scope.$watch('parser.CPU.ALU.data.OF', function (value) { $scope.conditions.of = value; }); $scope.$watch('code.current', function (value) { var cursorPos = value * $scope.code.lineHeight; while (cursorPos >= $scope.code.height + $scope.code.scrollTop) { $scope.code.scrollTop += $scope.code.height; } while (cursorPos <= $scope.code.scrollTop) { $scope.code.scrollTop -= $scope.code.height; } $scope.code.scrollTop = Math.min($scope.code.scrollTop, $scope.code.scrollHeight - $scope.code.height); $scope.code.scrollTop = Math.max($scope.code.scrollTop, 0); }); }; $scope.loadTab = function (index) { $scope.code.currentTab = index; $scope.reset(); }; $scope.newTab = function (files) { var readFile = function (index) { if (!files[index]) return; if (files[index].name.split('.')[1] !== 'yo') { alert('File type is not supported!'); return; } $scope.code.tabs.push(files[index].name.split('.')[0]); var reader = new FileReader(); reader.onload = function(event) { $scope.code.fileCache.push(event.target.result); $scope.$safeApply(); readFile(index + 1); }; reader.readAsText(files[index]); }; readFile(0); }; $scope.prev = function () { if (!$scope.parser) { return false; } if ($scope.state.icon) { // Pause here $scope.play(); } $scope.parser.CPU.backword(); }; $scope.next = function () { if (!$scope.parser) { return false; } try { $scope.parser.CPU.forward(); } catch (err) { return false; } return true; }; $scope.setInterval = function () { if (!$scope.state.icon) { return false; } if (!$scope.next()) { $scope.state.icon = 0; $scope.$safeApply(); return false; } $scope.$safeApply(); setTimeout($scope.setInterval, 1000 / $scope.player.hz); return true; }; $scope.play = function () { $scope.state.icon ^= 1; if ($scope.state.icon) { if (!$scope.parser) { // test var parser = new Parser($scope.code.fileCache[$scope.code.currentTab] || ($scope.code.fileCache[$scope.code.currentTab] = xhrGETSync($scope.code.tabFiles[$scope.code.currentTab]))); $scope.initParser(parser); $scope.setInterval(); } else { $scope.setInterval(); } } }; $scope.reset = function () { if ($scope.state.icon) { // Pause here $scope.play(); } var parser = new Parser($scope.code.fileCache[$scope.code.currentTab] || ($scope.code.fileCache[$scope.code.currentTab] = xhrGETSync($scope.code.tabFiles[$scope.code.currentTab]))); $scope.initParser(parser); }; // Initialization setTimeout(function () { $scope.reset(); $scope.$safeApply(); }, 0); move.select = function (element) { return element; }; } ================================================ FILE: src/script/directives/board.js ================================================ /** * Created by shuding on 6/4/15. * */ angular .module('y86') .directive('board', function () { return { restrict: 'E', transclude: true, template: '
' } }) .directive('flex-container', function () { return { restrict: 'E', transclude: true } }); ================================================ FILE: src/script/directives/clock.js ================================================ /** * Created by shuding on 6/4/15. * */ angular .module('y86') .directive('clockContainer', function () { return { restrict: 'E', transclude: true, template: '

CLOCK

' + '
' } }) .directive('clock', function () { return { restrict: 'E', scope: { data: '=' }, template: '
{{ data }}
', link: function($scope, elem) { $scope.$watch('data', function () { move(elem[0]) .set('color', '#0B0B0B') .set('background-color', '#37F3FF') .duration('0s') .delay('0s') .then() .set('color', '#37F3FF') .set('background-color', '#0B0B0B') .duration('.2s') .delay('.1s') .pop() .end(); }); } } }); ================================================ FILE: src/script/directives/code.js ================================================ /** * Created by shuding on 6/5/15. * */ angular .module('y86') .directive('codeContainer', function () { return { restrict: 'E', scope: true, transclude: true, template: '

{{ tab }}

+

' + '
WRITE ASSEMBLY CODE HERE', link: function ($scope, elem) { var pre = elem.find('pre')[0]; $scope.$watch(function () { $scope.code.height = pre.offsetHeight; $scope.code.scrollHeight = pre.scrollHeight; }); $scope.$watch('code.scrollTop', function () { pre.scrollTop = $scope.code.scrollTop; }); var file = elem.find('input'); file.bind('change', function (event) { $scope.newTab(event.target.files); }); } } }).directive('codeLine', function () { return { restrict: 'E', transclude: true, scope: { lineNumber: '=', lineContent: '=' }, template: '{{ lineNumber }}{{ lineContent }}' } }).directive('codeLineIndicator', function () { return { restrict: 'E' } }); ================================================ FILE: src/script/directives/condition.js ================================================ /** * Created by shuding on 6/4/15. * */ angular .module('y86') .directive('conditionContainer', function () { return { restrict: 'E', transclude: true, template: '

CONDITIONS

' + '
' } }) .directive('condition', function () { return { restrict: 'E', scope: { condName: '=', condData: '=' }, template: '{{ condName | uppercase }}={{ condData }}', link: function($scope, elem) { $scope.$watch('condData', function () { move(elem[0]) .set('color', '#0B0B0B') .set('background-color', '#37F3FF') .duration('0s') .delay('0s') .then() .set('color', '#37F3FF') .set('background-color', '#0B0B0B') .duration('.2s') .delay('.1s') .pop() .end(); }); } } }); ================================================ FILE: src/script/directives/control.js ================================================ /** * Created by shuding on 6/4/15. * */ angular .module('y86') .directive('controlContainer', function () { return { restrict: 'E', transclude: true, template: '
' } }).directive('frequency', function () { return { restrict: 'E', transclude: true, scope: { hz: '=' }, template: '1 Hz 50 Hz' } }).directive('frequencyLabel', function () { return { restrict: 'E', scope: { hz: '=' }, template: '{{ hz }}' } }).directive('playPause', function () { return { restrict: 'E', scope: { fn: '&', icon: '=' }, template: '' } }).directive('prev', function () { return { restrict: 'E', scope: { fn: '&' }, template: '' } }).directive('next', function () { return { restrict: 'E', scope: { fn: '&' }, template: '' } }).directive('reset', function () { return { restrict: 'E', scope: { fn: '&' }, template: '' } }); ================================================ FILE: src/script/directives/cpi.js ================================================ /** * Created by shuding on 6/11/15. * */ angular.module('y86').directive('cpiContainer', function () { return { restrict: 'E', scope: true, transclude: true, template: '
CPI Curve
', link: function ($scope, elem) { var canvas = elem.find('canvas')[0]; var context = canvas.getContext('2d'); var lastPoint = [[0, 0], [0, 0]]; context.strokeStyle = '#37F3FF'; context.lineWidth = 2; function draw(dataset) { // Clear context.fillStyle = 'black'; context.fillRect(0, 0, 1000, 50); if (!dataset || dataset.length < 2) return; var eachPadding = 1000 / (dataset.length - 1); var maxY = .1; dataset.forEach(function (y) { maxY = Math.max(maxY, y - 1); }); context.beginPath(); context.moveTo(0, 49); // Render dataset dataset.forEach(function (y, x) { var px = x * eachPadding; var py = (1 - (y - 1) / maxY) * 48 + 1; context.fillStyle = '#30F4FE'; context.fillRect(px - 1, py - 3, 2, 5); if (x > 1) { context.quadraticCurveTo(lastPoint[1][0], lastPoint[1][1], (px + lastPoint[1][0]) / 2, (py + lastPoint[1][1]) / 2); } lastPoint[0] = lastPoint[1]; lastPoint[1] = [px, py]; }); context.quadraticCurveTo(lastPoint[0][0], lastPoint[0][1], lastPoint[1][0], lastPoint[1][1]); context.stroke(); context.closePath(); } $scope.$watch('parser.CPU.cpi.length', function () { if ($scope.parser) draw($scope.parser.CPU.cpi); }); $scope.$watch('parser.CPU.cycle', function (value) { $scope.getterAndSetterCycle = value; }); $scope.$watch('getterAndSetterCycle', function (value) { if ($scope.parser) $scope.parser.CPU.goto(value); }); elem.find('#cpi-input'); } } }); ================================================ FILE: src/script/directives/pipe.js ================================================ /** * Created by shuding on 6/10/15. * */ angular.module('y86').directive('fetch', function () { return { restrict: 'E', scope: true, transclude: true, template: '

FETCH

' + '
' } }).directive('decode', function () { return { restrict: 'E', scope: true, transclude: true, template: '

DECODE

' + '
' } }).directive('execute', function () { return { restrict: 'E', scope: true, transclude: true, template: '

EXECUTE

' + '
' } }).directive('memory', function () { return { restrict: 'E', scope: true, transclude: true, template: '

MEMORY

' + '
' } }).directive('writeBack', function () { return { restrict: 'E', scope: true, transclude: true, template: '

WRITE BACK

' + '
' } }).directive('pipeAttr', function () { return { restrict: 'E', scope: { pipeName: '=', pipeData: '=', pipePref: '=' }, transclude: true, template: '
{{ pipeName }}

{{ pipePref }}{{ pipeData }}

', link: function($scope, elem) { $scope.$watch('pipeData', function () { move(elem[0]) .set('color', '#0B0B0B') .set('background-color', '#37F3FF') .duration('0s') .delay('0s') .then() .set('color', '#37F3FF') .set('background-color', '#0B0B0B') .duration('.2s') .delay('.1s') .pop() .end(); }); } } }); ================================================ FILE: src/script/directives/register.js ================================================ /** * Created by shuding on 6/4/15. * */ angular .module('y86') .directive('registerContainer', function () { return { restrict: 'E', transclude: true, template: '

REGISTERS

' + '
' } }).directive('register', function () { return { restrict: 'E', scope: { regName: '=', regData: '=' }, template: '
%{{regName}}
' + '
0x{{regData}}
', link: function($scope, elem) { $scope.$watch('regData', function () { move(elem[0]) .set('color', '#0B0B0B') .set('background-color', '#37F3FF') .duration('0s') .delay('0s') .then() .set('color', '#37F3FF') .set('background-color', '#0B0B0B') .duration('.2s') .delay('.1s') .pop() .end(); }); } } }); ================================================ FILE: src/script/directives/stack.js ================================================ /** * Created by shuding on 6/4/15. * */ angular .module('y86') .directive('stackContainer', function () { return { restrict: 'E', transclude: true, template: '

MEMORY

' + '
' } }) .directive('stack', function () { return { restrict: 'E', transclude: true, template: '
' } }); ================================================ FILE: src/script/kernels/alu.js ================================================ /** * Created by shuding on 6/9/15. * */ var Constant = require('script/kernels/constant'); module.exports = ALU; function ALU() { function init(alu) { alu.data = { ZF: 1, SF: 0, OF: 0, inA: 0, inB: 0, tVal: 0, fCode: 0, update: 0 }; calc(alu); cond(alu); config(alu); update(alu); } function calc(alu) { alu.calc = {}; alu.calc[Constant.A_ADD] = function () { alu.data.tVal = (alu.data.inA + alu.data.inB) | 0; alu.updateAll(); }; alu.calc[Constant.A_AND] = function () { alu.data.tVal = alu.data.inA & alu.data.inB; alu.updateAll(); }; alu.calc[Constant.A_SUB] = function () { alu.data.tVal = (alu.data.inA - alu.data.inB) | 0; alu.updateAll(); }; alu.calc[Constant.A_XOR] = function () { alu.data.tVal = alu.data.inA ^ alu.data.inB; alu.updateAll(); }; } function cond(alu) { alu.cond = {}; alu.cond[Constant.C_TRUE] = function () { return true; }; alu.cond[Constant.C_L] = function () { return alu.SF() ^ alu.OF(); }; alu.cond[Constant.C_E] = function () { return alu.ZF(); }; alu.cond[Constant.C_NE] = function () { return !alu.cond[Constant.C_E](); }; alu.cond[Constant.C_LE] = function () { return (alu.cond[Constant.C_L]) | alu.cond[Constant.C_E]; }; alu.cond[Constant.C_GE] = function () { return !alu.cond[Constant.C_L]; }; alu.cond[Constant.C_G] = function () { return !alu.cond[Constant.C_LE]; }; } function config(alu) { alu.config = {}; alu.config.setInA = function (data) { alu.data.inA = data; }; alu.config.setInB = function (data) { alu.data.inB = data; }; alu.config.setFCode = function (data) { alu.data.fCode = data; }; alu.config.setUpdate = function (data) { alu.data.update = data; }; } function update(alu) { alu.updateOF = {}; alu.updateOF[Constant.A_ADD] = function () { alu.OF((alu.data.inA < 0) == (alu.data.inB < 0) && (alu.data.tVal < 0) != (alu.data.inA < 0)); }; alu.updateOF[Constant.A_SUB] = function () { alu.OF((alu.data.inA < 0) == (alu.data.inB > 0) && (alu.data.tVal < 0) != (alu.data.inB < 0)); }; alu.updateOF[Constant.A_AND] = function () { alu.OF(0); }; alu.updateOF[Constant.A_XOR] = function () { alu.OF(0); }; } init(this); } ALU.prototype.ZF = function (flag) { if (typeof flag !== 'undefined') { this.data.ZF = (+flag); } return this.data.ZF; }; ALU.prototype.SF = function (flag) { if (typeof flag !== 'undefined') { this.data.SF = (+flag); } return this.data.SF; }; ALU.prototype.OF = function (flag) { if (typeof flag !== 'undefined') { this.data.OF = (+flag); } return this.data.OF; }; ALU.prototype.updateAll = function () { if (!this.data.update) { return; } this.SF(!!(this.data.tVal >>> 31)); this.ZF(!this.data.tVal); this.updateOF[this.data.fCode](); }; ALU.prototype.execute = function () { this.calc[this.data.fCode](); return this.data.tVal; }; ALU.prototype.condition = function (cond) { if (!(0 <= cond && cond < 8)) return true; return this.cond[cond](); }; ================================================ FILE: src/script/kernels/constant.js ================================================ /** * Created by shuding on 6/9/15. * */ module.exports = { REGISTERS: [ 'R_EAX', 'R_ECX', 'R_EDX', 'R_EBX', 'R_ESP', 'R_EBP', 'R_ESI', 'R_EDI' ], P_LOAD: 0, P_STALL: 1, P_BUBBLE: 2, P_ERROR: 3, I_NOP: 0, I_HALT: 1, I_RRMOVL: 2, I_IRMOVL: 3, I_RMMOVL: 4, I_MRMOVL: 5, I_OPL: 6, I_JXX: 7, I_CALL: 8, I_RET: 9, I_PUSHL: 0xa, I_POPL: 0xb, I_IADDL: 0xc, I_LEAVE: 0xd, A_ADD: 0, A_SUB: 1, A_AND: 2, A_XOR: 3, A_NONE: 4, F_OF: 1, // OF at bit 0 F_SF: 2, // SF at bit 1 F_ZF: 4, // ZF at bit 2 C_TRUE: 0, C_LE: 1, C_L: 2, C_E: 3, C_NE: 4, C_GE: 5, C_G: 6, STAT_BUB: 0, STAT_AOK: 1, STAT_HLT: 2, STAT_ADR: 3, STAT_INS: 4, STAT_PIP: 5, R_EAX: 0, R_ECX: 1, R_EDX: 2, R_EBX: 3, R_ESP: 4, R_EBP: 5, R_ESI: 6, R_EDI: 7, R_NONE: 0xf, DEF_CC: 4 }; ================================================ FILE: src/script/kernels/cpu.js ================================================ /** * Created by shuding on 6/9/15. * */ var Constant = require('script/kernels/constant'); var Memory = require('script/kernels/memory'); var Register = require('script/kernels/register').Register; var PIPERegister = require('script/kernels/register').PIPERegister; var ALU = require('script/kernels/alu'); module.exports = CPU; function clone(obj) { if (null == obj || "object" != typeof obj) { return obj; } var constructor = obj.constructor || obj.__proto__.constructor; var copy = new constructor(); for (var attr in obj) { if (obj.hasOwnProperty(attr)) { copy[attr] = obj[attr]; } } return copy; } function saveHistory(cpu, cycle) { cpu.history[cycle] = { Memory: clone(cpu.Memory), Register: clone(cpu.Register), Input: clone(cpu.Input), Output: clone(cpu.Output), ALU: clone(cpu.ALU), cycle: cpu.cycle, instruction: cpu.instruction, state: cpu.state, halt: cpu.halt, mrmovl: cpu.mrmovl, popl: cpu.popl, cond: cpu.cond, ret: cpu.ret, use: cpu.use, mispredict: cpu.mispredict }; } function timeMachine(cpu, cycle) { cpu.Memory = cpu.history[cycle].Memory; cpu.Register = cpu.history[cycle].Register; cpu.Input = cpu.history[cycle].Input; cpu.Output = cpu.history[cycle].Output; cpu.ALU = cpu.history[cycle].ALU; cpu.instruction = cpu.history[cycle].instruction; cpu.cycle = cpu.history[cycle].cycle; cpu.state = cpu.history[cycle].state; cpu.halt = cpu.history[cycle].halt; cpu.mrmovl = cpu.history[cycle].mrmovl; cpu.popl = cpu.history[cycle].popl; cpu.cond = cpu.history[cycle].cond; cpu.ret = cpu.history[cycle].ret; cpu.use = cpu.history[cycle].use; cpu.mispredict = cpu.history[cycle].mispredict; } function saveCPI (cpu, cycle, cpi) { cpu.cpi[cycle] = cpi; } function CPU() { this.Memory = new Memory(); this.Register = new Register(); this.Input = new PIPERegister(); this.Output = new PIPERegister(); this.ALU = new ALU(); this.history = []; this.cpi = [0]; /** * Initialization */ function init(cpu) { cpu.cycle = 0; cpu.instruction = 0; cpu.state = Constant.STAT_AOK; cpu.halt = false; cpu.mrmovl = 0; cpu.popl = 0; cpu.cond = 0; cpu.ret = 0; cpu.use = 0; cpu.mispredict = 0; saveHistory(cpu, 0); } init(this); } CPU.prototype.goto = function (cycle) { if (cycle < this.history.length) { timeMachine(this, cycle); } }; CPU.prototype.backword = function () { if (this.history[this.cycle - 1]) { timeMachine(this, this.cycle - 1); } }; CPU.prototype.forward = function () { if (this.cycle + 1 < this.history.length) { timeMachine(this, this.cycle + 1); return; } var input = this.Input; var output = this.Output; if (input.W_icode == Constant.I_HALT) { this.state = Constant.STAT_HLT; } if (this.state != Constant.STAT_AOK && this.state != Constant.STAT_BUB) { throw new Error('State error: ' + this.state); } this.writeBack(); this.memory(); this.execute(); this.decode(); this.fetch(); this.cycle++; if (this.state != Constant.STAT_BUB) { this.instruction++; } this.nextPIPERegister(); saveCPI(this, this.cycle, this.updateCPI()); saveHistory(this, this.cycle); }; CPU.prototype.updateCPI = function () { var lp = (this.mrmovl + this.popl) * this.use; var mp = this.cond * this.mispredict; var rp = this.ret; if (!this.instruction) lp = mp = rp = 0; if (!this.mrmovl && !this.popl) lp = 0; if (!this.cond) mp = 0; if (lp) lp /= this.instruction * (this.mrmovl + this.popl); if (mp) mp /= this.instruction * this.cond; if (rp) rp /= this.instruction; return lp + mp * 2 + rp * 3 + 1; }; CPU.prototype.fetch = function () { if (this.halt) { return; } var input = this.Input; var output = this.Output; var next; if (input.M_icode == Constant.I_JXX && !input.M_Cnd) { next = input.M_valA; } else if (input.W_icode == Constant.I_RET) { next = input.W_valM; } else { next = input.F_predPC; } var insruction = this.Memory.readByte(next++); output.D_icode = insruction >> 4; output.D_ifun = insruction & 15; if ([Constant.I_NOP, Constant.I_HALT, Constant.I_RRMOVL, Constant.I_IRMOVL, Constant.I_RMMOVL, Constant.I_MRMOVL, Constant.I_OPL, Constant.I_JXX, Constant.I_CALL, Constant.I_RET, Constant.I_PUSHL, Constant.I_POPL, Constant.I_LEAVE, Constant.I_IADDL].indexOf(output.D_icode) == -1) { output.F_stat = output.D_stat = Constant.STAT_INS; return; } else if (output.D_icode == Constant.I_HALT) { output.F_stat = output.D_stat = Constant.STAT_HLT; return; } else { output.F_stat = output.D_stat = Constant.STAT_AOK; } if ([Constant.I_RRMOVL, Constant.I_OPL, Constant.I_IRMOVL, Constant.I_MRMOVL, Constant.I_RMMOVL, Constant.I_PUSHL, Constant.I_POPL, Constant.I_IADDL].indexOf(output.D_icode) != -1) { var regByte = this.Memory.readByte(next++); output.D_rA = regByte >> 4; output.D_rB = regByte & 15; } else { output.D_rA = output.D_rB = Constant.R_NONE; } if ([Constant.I_IRMOVL, Constant.I_RMMOVL, Constant.I_MRMOVL, Constant.I_JXX, Constant.I_CALL, Constant.I_IADDL].indexOf(output.D_icode) != -1) { output.D_valC = this.Memory.readInt(next); next += 4; } output.F_predPC = next; if ([Constant.I_JXX, Constant.I_CALL].indexOf(output.D_icode) != -1) { output.F_predPC = output.D_valC; } output.D_valP = next; }; CPU.prototype.decode = function () { var input = this.Input; var output = this.Output; output.E_icode = input.D_icode; output.E_ifun = input.D_ifun; output.E_valC = input.D_valC; output.E_stat = input.D_stat; if ([Constant.I_RRMOVL, Constant.I_RMMOVL, Constant.I_OPL, Constant.I_PUSHL].indexOf(input.D_icode) != -1) { output.E_srcA = input.D_rA; } else if ([Constant.I_POPL, Constant.I_RET].indexOf(input.D_icode) != -1) { output.E_srcA = Constant.R_ESP; } else if (input.D_icode == Constant.I_LEAVE) { output.E_srcA = Constant.R_EBP; } else { output.E_srcA = Constant.R_NONE; } if ([Constant.I_OPL, Constant.I_RMMOVL, Constant.I_MRMOVL, Constant.I_IADDL].indexOf(input.D_icode) != -1) { output.E_srcB = input.D_rB; } else if ([Constant.I_PUSHL, Constant.I_POPL, Constant.I_CALL, Constant.I_RET].indexOf(input.D_icode) != -1) { output.E_srcB = Constant.R_ESP; } else if (input.D_icode == Constant.I_LEAVE) { output.E_srcB = Constant.R_EBP; } else { output.E_srcB = Constant.R_NONE; } if ([Constant.I_RRMOVL, Constant.I_IRMOVL, Constant.I_OPL, Constant.I_IADDL].indexOf(input.D_icode) != -1) { output.E_dstE = input.D_rB; } else if ([Constant.I_PUSHL, Constant.I_POPL, Constant.I_CALL, Constant.I_RET, Constant.I_LEAVE].indexOf(input.D_icode) != -1) { output.E_dstE = Constant.R_ESP; } else { output.E_dstE = Constant.R_NONE; } if ([Constant.I_MRMOVL, Constant.I_POPL].indexOf(input.D_icode) != -1) { output.E_dstM = input.D_rA; } else if (input.D_icode == Constant.I_LEAVE) { output.E_dstM = Constant.R_EBP; } else { output.E_dstM = Constant.R_NONE; } if ([Constant.I_CALL, Constant.I_JXX].indexOf(input.D_icode) != -1) { output.E_valA = input.D_valP; } else if (output.E_srcA == output.M_dstE) { output.E_valA = output.M_valE; } else if (output.E_srcA == input.M_dstM) { output.E_valA = output.W_valM; } else if (output.E_srcA == input.M_dstE) { output.E_valA = input.M_valE; } else if (output.E_srcA == input.W_dstM) { output.E_valA = input.W_valM; } else if (output.E_srcA == input.W_dstE) { output.E_valA = input.W_valE; } else { output.E_valA = this.Register.get(output.E_srcA); } if (output.E_srcB == output.M_dstE) { output.E_valB = output.M_valE; } else if (output.E_srcB == input.M_dstM) { output.E_valB = output.W_valM; } else if (output.E_srcB == input.M_dstE) { output.E_valB = input.M_valE; } else if (output.E_srcB == input.W_dstM) { output.E_valB = input.W_valM; } else if (output.E_srcB == input.W_dstE) { output.E_valB = input.W_valE; } else { output.E_valB = this.Register.get(output.E_srcB); } }; CPU.prototype.execute = function () { var input = this.Input; var output = this.Output; var alu = this.ALU; output.M_icode = input.E_icode; output.M_valA = input.E_valA; output.M_dstM = input.E_dstM; output.M_stat = input.E_stat; if (input.E_icode == Constant.I_MRMOVL) { this.mrmovl++; } if (input.E_icode == Constant.I_POPL) { this.popl++; } if (input.E_icode == Constant.I_JXX) { this.cond++; } if (input.E_icode == Constant.I_RET) { this.ret++; } if (input.E_icode == Constant.I_HALT && input.E_stat != Constant.STAT_BUB) { this.halt = true; output.M_stat = Constant.STAT_HLT; } // ALU-A if ([Constant.I_RRMOVL, Constant.I_OPL].indexOf(input.E_icode) != -1) { alu.config.setInA(input.E_valA); } else if ([Constant.I_IRMOVL, Constant.I_RMMOVL, Constant.I_MRMOVL, Constant.I_IADDL].indexOf(input.E_icode) != -1) { alu.config.setInA(input.E_valC); } else if ([Constant.I_CALL, Constant.I_PUSHL].indexOf(input.E_icode) != -1) { alu.config.setInA(-4); } else if ([Constant.I_RET, Constant.I_POPL, Constant.I_LEAVE].indexOf(input.E_icode) != -1) { alu.config.setInA(4); } else { alu.config.setInA(0); } // ALU-B if ([Constant.I_RMMOVL, Constant.I_MRMOVL, Constant.I_OPL, Constant.I_CALL, Constant.I_PUSHL, Constant.I_RET, Constant.I_POPL, Constant.I_IADDL, Constant.I_LEAVE].indexOf(input.E_icode) != -1) { alu.config.setInB(input.E_valB); } else { alu.config.setInB(0); } // ALU-fCode if (input.E_icode == Constant.I_OPL) { alu.config.setFCode(input.E_ifun); } else { alu.config.setFCode(0); } // ALU-Update if ([Constant.I_OPL, Constant.I_IADDL].indexOf(input.E_icode) != -1 && [Constant.STAT_ADR, Constant.STAT_INS, Constant.STAT_HLT].indexOf(output.W_stat) == -1 && [Constant.STAT_ADR, Constant.STAT_INS, Constant.STAT_HLT].indexOf(input.W_stat) == -1) { alu.config.setUpdate(1); } else { alu.config.setUpdate(0); } output.M_valE = alu.execute(); output.M_Cnd = alu.condition(input.E_ifun); output.M_valA = input.E_valA; if (input.E_icode == Constant.I_RRMOVL && !output.M_Cnd) { output.M_dstE = Constant.R_NONE; } else { output.M_dstE = input.E_dstE; } }; CPU.prototype.memory = function () { var input = this.Input; var output = this.Output; var rMem = false, wMem = false, mAddr = 0; output.W_stat = input.M_stat; output.W_icode = input.M_icode; output.W_valE = input.M_valE; output.W_dstE = input.M_dstE; output.W_dstM = input.M_dstM; if ([Constant.I_RMMOVL, Constant.I_PUSHL, Constant.I_CALL, Constant.I_MRMOVL].indexOf(input.M_icode) != -1) { mAddr = input.M_valE; } else if ([Constant.I_POPL, Constant.I_RET, Constant.I_LEAVE].indexOf(input.M_icode) != -1) { mAddr = input.M_valA; } rMem = [Constant.I_MRMOVL, Constant.I_POPL, Constant.I_RET, Constant.I_LEAVE].indexOf(input.M_icode) != -1; wMem = [Constant.I_RMMOVL, Constant.I_PUSHL, Constant.I_CALL].indexOf(input.M_icode) != -1; if (rMem) { output.W_valM = this.Memory.readInt(mAddr); } if (wMem) { this.Memory.writeInt(mAddr, input.M_valA); } }; CPU.prototype.writeBack = function () { var input = this.Input; this.state = input.W_stat; if (input.W_icode == Constant.I_RMMOVL) { return; } this.Register.set(input.W_dstE, input.W_valE); this.Register.set(input.W_dstM, input.W_valM); }; CPU.prototype.nextPIPERegister = function () { var input = this.Input; var output = this.Output; var F_stall = (([Constant.I_MRMOVL, Constant.I_POPL, Constant.I_LEAVE].indexOf(input.E_icode) != -1) && ([output.E_srcA, output.E_srcB].indexOf(input.E_dstM) != -1)) || ([input.D_icode, input.E_icode, input.M_icode].indexOf(Constant.I_RET) != -1); var D_stall = ([Constant.I_MRMOVL, Constant.I_POPL, Constant.I_LEAVE].indexOf(input.E_icode) != -1) && ([output.E_srcA, output.E_srcB].indexOf(input.E_dstM) != -1); var D_bubble = (input.E_icode == Constant.I_JXX && !output.M_Cnd) || ((!D_stall) && ([input.D_icode, input.E_icode, input.M_icode].indexOf(Constant.I_RET) != -1)); var E_bubble = (input.E_icode == Constant.I_JXX && !output.M_Cnd) || (([Constant.I_MRMOVL, Constant.I_POPL, Constant.I_LEAVE].indexOf(input.E_icode) != -1 && [output.E_srcA, output.E_srcB].indexOf(input.E_dstM) != -1)); var M_bubble = [Constant.STAT_ADR, Constant.STAT_INS, Constant.STAT_HLT].indexOf(output.W_stat) != -1 || [Constant.STAT_ADR, Constant.STAT_INS, Constant.STAT_HLT].indexOf(input.W_stat) != -1; if (([Constant.I_MRMOVL, Constant.I_POPL].indexOf(input.E_icode) != -1) && (input.E_dstM == input.E_srcA || input.E_dstM == input.E_srcB)) { this.use++; } if (input.E_icode == Constant.I_JXX && !input.M_Cnd) { this.mispredict++; } // Copy from output var reg = new PIPERegister(); for (var name in output) { if (output.hasOwnProperty(name)) { reg.set(name, output[name]); } } if (M_bubble) { reg.set({ M_icode: Constant.I_NOP, M_stat: Constant.STAT_BUB, M_dstE: Constant.R_NONE, M_dstM: Constant.R_NONE, M_Cnd: false }); } if (E_bubble) { reg.set({ E_icode: Constant.I_NOP, E_ifun: 0, E_stat: Constant.STAT_BUB, E_dstE: Constant.R_NONE, E_dstM: Constant.R_NONE, E_srcA: Constant.R_NONE, E_srcB: Constant.R_NONE }); } if (D_stall) { reg.set({ D_icode: input.D_icode, D_ifun: input.D_ifun, D_rA: input.D_rA, D_rB: input.D_rB, D_valC: input.D_valC, D_valP: input.D_valP }); } if (D_bubble) { reg.set({ D_icode: Constant.I_NOP, D_ifun: 0, D_stat: Constant.STAT_BUB }); } if (F_stall) { reg.set('F_predPC', input.F_predPC); } this.Input = reg; }; ================================================ FILE: src/script/kernels/main.js ================================================ /** * Created by shuding on 6/9/15. * */ (function (window, undefined) { var Y86 = { modules: [] }; window.xhrGETSync = function (url) { var xhr = new XMLHttpRequest(); xhr.open('GET', url + '?' + (new Date()).getTime(), false); xhr.send(); if (xhr.readyState == 4) { return xhr.responseText; } return undefined; }; /** * The require (CommonJS) function. * @param path */ window.require = function (path) { // Module cache if (typeof Y86.modules[path] !== 'undefined') { return Y86.modules[path]; } var data = xhrGETSync(path + '.js'); if (typeof data !== 'undefined') { Y86.modules[path] = eval('(function (window, undefined) { var module = {}; ' + data + '; return module.exports; })(window)'); } return Y86.modules[path]; }; })(window); ================================================ FILE: src/script/kernels/memory.js ================================================ /** * Created by shuding on 6/9/15. * */ var Utils = require('script/kernels/utils'); module.exports = Memory; function Memory() { this.data = []; this.fillTo = function (address) { for (var i = this.data.length; i <= address; ++i) { this.data[i] = [0, 0, 0, 0]; } }; this.get = function (address) { var index = Math.floor(address / 4); var offset = address % 4; if (typeof this.data[index] == 'undefined') { this.fillTo(index); } return this.data[index][offset]; }; this.set = function (address, byte) { var index = Math.floor(address / 4); var offset = address % 4; if (typeof this.data[index] == 'undefined') { this.fillTo(index); } this.data[index][offset] = byte; }; } Memory.prototype.writeByte = function (address, byte) { if (!Utils.checkByte(byte)) { throw new Error('Byte error in `Memory writeByte`'); } this.set(address, byte); }; Memory.prototype.readByte = function (address) { return this.get(address); }; Memory.prototype.writeInt = function (address, int) { if (!Utils.checkNumber(int)) { throw new Error('Int error in `Memory writeInt`'); } for (var i = 0; i < 4; ++i) { this.set(address + i, int & 255); int >>= 8; } }; Memory.prototype.readInt = function (address) { return this.get(address + 0) + this.get(address + 1) * 256 + this.get(address + 2) * 65536 + this.get(address + 3) * 16777216; }; ================================================ FILE: src/script/kernels/parser.js ================================================ /** * Created by shuding on 6/8/15. * */ var Constant = require('script/kernels/constant'); var CPU = require('script/kernels/cpu'); module.exports = Parser; function Parser(text) { if (typeof text !== 'string' && !(text instanceof String)) { throw new Error('Type error at `new Parser`'); } /** * Initialization & parse .yo into lines of instructions * @param parser */ function init(parser) { parser.raw = text; parser.syntaxs = []; parser.map = {}; parser.lines = []; parser.regLine = /^\s*0[xX]([0-9a-fA-F]+)\s*:\s*([0-9a-fA-F]*)\s*\|.*$/; parser.Constant = Constant; parser.CPU = new CPU(); text.split('\n').forEach(function (rawLine, index) { parser.syntaxs.push(rawLine); var parts = rawLine.match(parser.regLine); if (parts != null) { if (parts[2].length % 2) { throw new Error('Address error at `Parse init`: ' + (index + 1)); } try { var address = parseInt(parts[1], 16); var instruction = parts[2]; parser.map[address] = index; storeInstruction(parser, address, instruction); } catch (err) { throw new Error('Format error at `Parse init`: ' + (index + 1)); } } }); } function storeInstruction(parser, address, instruction) { parser.lines.push([address, instruction]); for (var offset = 0; offset < instruction.length; offset += 2, address += 1) { parser.CPU.Memory.writeByte(address, parseInt(instruction.substr(offset, 2), 16)); } } init(this); } ================================================ FILE: src/script/kernels/register.js ================================================ /** * Created by shuding on 6/9/15. * */ var Constant = require('script/kernels/constant'); var Utils = require('script/kernels/utils'); module.exports = { Register: Register, PIPERegister: PIPERegister }; function Register() { /** * Initialization: set all registers to 0 */ function init(register) { Constant.REGISTERS.forEach(function (name) { register[Constant[name]] = 0; }); } init(this); } Register.prototype.get = function (name) { if (!Utils.checkNumber(name)) { if (typeof this[Constant[name]] === 'undefined') { throw new Error('Register error undefined name: ' + name); } return this[Constant[name]]; } return this[name]; }; Register.prototype.set = function (name, value) { if (!Utils.checkNumber(name)) { if (typeof Constant[name] !== 'undefined' && name !== 'R_NONE') { this[Constant[name]] = value; } else { return false; } } if (!(0 <= name && name < 8)) return false; this[name] = value; return true; }; function PIPERegister() { var defaults = { // Fetch F_predPC: 0, F_carryPC: -1, F_stat: Constant.STAT_AOK, // Decode D_icode: Constant.I_NOP, D_ifun: 0, D_rA: Constant.R_NONE, D_rB: Constant.R_NONE, D_valC: 0, D_valP: 0, D_carryPC: -1, D_stat: Constant.STAT_BUB, // Execute E_icode: Constant.I_NOP, E_ifun: 0, E_valC: 0, E_valA: 0, E_valB: 0, E_dstE: Constant.R_NONE, E_dstM: Constant.R_NONE, E_srcA: Constant.R_NONE, E_srcB: Constant.R_NONE, E_carryPC: -1, E_stat: Constant.STAT_BUB, // Memory M_icode: Constant.I_NOP, M_valE: 0, M_valA: 0, M_dstE: Constant.R_NONE, M_dstM: Constant.R_NONE, M_stat: Constant.STAT_BUB, M_carryPC: -1, M_Cnd: false, // Write Back W_icode: Constant.I_NOP, W_valE: 0, W_valM: 0, W_dstE: Constant.R_NONE, W_dstM: Constant.R_NONE, W_stat: Constant.STAT_BUB }; function init(register) { for (var name in defaults) { if (defaults.hasOwnProperty(name)) { register[name] = defaults[name]; } } } init(this); } PIPERegister.prototype.get = function (name) { if (typeof this[name] === 'undefined') { throw new Error('Register error undefined name: ' + name); } return this[name]; }; PIPERegister.prototype.set = function (name, value) { if (typeof name == 'object') { for (var key in name) { if (name.hasOwnProperty(key)) { if (Utils.checkNumber(name[key])) { this[key] = name[key]; } } } } else { if (!Utils.checkNumber(value)) { return false; } this[name] = value; } return true; }; ================================================ FILE: src/script/kernels/utils.js ================================================ /** * Created by shuding on 6/9/15. * */ module.exports = Util = {}; Util.checkNumber = function (data) { data = +data; return !isNaN(data) && data === parseInt(data); }; Util.checkByte = function (data) { data = +data; if (!Util.checkNumber(data)) return 0; return 0 <= data && data < 256; }; ================================================ FILE: src/style/board.css ================================================ board, board.top-line { position: absolute; display: block; left: 15px; right: 15px; top: 15px; bottom: 15px; padding: 10px; } board small { float: right; font-size: 13px; } /* flexbox container */ board > div { display: flex; display: -webkit-flex; flex-direction: column; -webkit-flex-direction: column; height: 100%; } ================================================ FILE: src/style/clock.css ================================================ clock { display: inline-block; padding: 2px 3px; } ================================================ FILE: src/style/code.css ================================================ code-container { flex-grow: 1; -webikt-flex-grow: 1; height: 100%; } code-container > pre { margin: 0; padding: 2px; max-height: calc(100% - 30px); border: 1px solid #37F3FF; white-space: inherit; overflow: scroll; } code-container > pre > code { position: relative; display: block; min-width: 100%; padding-bottom: 2px; border-bottom: 1px solid; font-size: 14px; } code-container > h2 { position: relative; display: inline-block; margin: 0 2px -3px 0; padding: 0 10px; cursor: pointer; font-size: 1.2em; max-width: 150px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } code-container > h2:first-of-type { margin-left: 10px; } code-container > h2:not(.active):hover { background-color: #005256; } code-container .tab { display: inline-block; position: absolute; width: 100%; height: 100%; left: 0; z-index: -1; border: 1px solid #2F6669; border-bottom: none; cursor: pointer; } code-container input[type=file] { position: absolute; width: 100%; height: 100%; left: 0; opacity: 0; cursor: pointer; } code-container h2.active { color: #000; } code-container h2.active .tab { background-color: #37F3FF; border: 1px solid #37F3FF; } code-container .info { font-size: 9px; margin: 2px 0; float: right; color: rgba(55, 243, 255, 0.7); } code-line-indicator { position: absolute; width: 100%; height: 1.2em; background-color: #016A6F; mix-blend-mode: lighten; transition: top 80ms ease-out; } code-line { display: block; height: 1.2em; white-space: pre; } code-line:hover { font-weight: 900; color: #00F2FF; background-color: #002B2D; } code-line > span.line-number { display: inline-block; width: 30px; padding-right: 4px; text-align: right; color: #00C2CE; background-color: #002B2D; } code-container:before { content: ''; width: 3px; height: 42px; background: rgba(55, 243, 255, 0.8); display: block; position: absolute; top: 48px; margin-left: -3px; } code-container:after { content: ''; width: 1px; height: 100px; background: rgba(55, 243, 255, 0.5); display: block; position: absolute; bottom: 25px; margin-left: -3px; } ================================================ FILE: src/style/condition.css ================================================ condition { display: inline-block; padding: 2px 3px; } ================================================ FILE: src/style/control.css ================================================ control-container > div { display: flex; display: -webkit-flex; width: 300px; font-size: 0; flex-direction: column; -webkit-flex-direction: column; -ms-flex-direction: column; align-items: center; -webkit-align-items: center; } /* Overwrites */ control-container input, control-container input:focus, control-container input:active, control-container button { border: none; outline: none; color: #37F3FF; background: none; -webkit-appearance: none; } control-container input[type=text] { width: 2em; text-align: center; border-bottom: 1px solid #186065; } /**/ frequency { height: 30px; line-height: 20px; font-size: 12px; } #frequency-input { width: 200px; height: 1px; vertical-align: middle; -webkit-appearance: none; background: #23868E; cursor: pointer; transition: all .3s ease; } #frequency-input::-webkit-slider-thumb { width: 2px; height: 15px; -webkit-appearance: none; background: #23868E; cursor: pointer; transition: all .3s ease; } #frequency-input::-moz-range-thumb { width: 2px; height: 15px; background: #23868E; border: none; cursor: pointer; transition: all .3s ease; } #frequency-input::-moz-range-track { background: #23868E; } #frequency-input:hover, #frequency-input:active { background: #21A0A7; } #frequency-input:hover::-webkit-slider-thumb { height: 20px; background: #37F3FF; } #frequency-input:active::-webkit-slider-thumb { height: 20px; background: #37F3FF; } #frequency-input:hover::-moz-range-thumb { height: 20px; background: #37F3FF; } #frequency-input:active::-moz-range-thumb { height: 20px; background: #37F3FF; } frequency-label { position: absolute; bottom: 4px; display: inline-block; padding-left: 28px; width: 50px; font-size: 10px; font-weight: 900; text-align: left; z-index: 1; } /* Buttons */ control-container button { min-width: 35px; padding: 6px 9px; text-align: center; font-size: 18px; font-weight: bolder; font-family: inherit; border: 1px solid rgba(0, 0, 0, 0); -webkit-font-smoothing: subpixel-antialiased; cursor: pointer; transition: all .2s ease; } control-container button:hover { color: #37F3FF; border-top: 1px solid #07939C; background-color: rgba(55, 243, 255, 0.08); } control-container { position: relative; display: block; } ================================================ FILE: src/style/cpi.css ================================================ cpi-container { display: block; } cpi-container > div { display: flex; display: -webkit-flex; position: relative; margin-right: 20px; box-shadow: 0 0 0 1px #23868E; } cpi-container > h6 { position: absolute; margin: 0; padding: 3px; z-index: 1; } cpi-container > div > canvas { width: 100%; height: 50px; } #cpi-input { position: absolute; width: 100%; height: 50px; margin: 0; vertical-align: middle; -webkit-appearance: none; background: transparent; cursor: pointer; outline: none; transition: all .3s ease; } #cpi-input::-webkit-slider-thumb { width: 1px; height: 50px; -webkit-appearance: none; background: #23868E; cursor: pointer; transition: all .3s ease; } #cpi-input::-moz-range-thumb { width: 1px; height: 50px; background: #23868E; border: none; cursor: pointer; transition: all .3s ease; } #cpi-input::-moz-range-track { background: #23868E; } #cpi-input:hover::-webkit-slider-thumb { background: #37F3FF; } #cpi-input:active::-webkit-slider-thumb { background: #37F3FF; } #cpi-input:hover::-moz-range-thumb { background: #37F3FF; } #cpi-input:active::-moz-range-thumb { background: #37F3FF; } ================================================ FILE: src/style/main.css ================================================ @font-face { font-family: 'Jura'; font-weight: 100; src: url(../font/Jura/Jura-Light.ttf); } @font-face { font-family: 'Jura'; font-weight: 300; src: url(../font/Jura/Jura-Regular.ttf); } @font-face { font-family: 'Jura'; font-weight: 600; src: url(../font/Jura/Jura-Medium.ttf); } @font-face { font-family: 'Jura'; font-weight: 900; src: url(../font/Jura/Jura-DemiBold.ttf); } /* Overwrites */ * { box-sizing: border-box; -webkit-box-sizing: border-box; } ::selection { background: none; } ::-webkit-scrollbar { display: none; } body { position: absolute; top: 0; left: 0; width: 100%; height: 100%; min-width: 960px; min-height: 580px; margin: 0; font-family: 'Jura', sans-serif; color: #37F3FF; overflow: hidden; font-kerning: auto; -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; font-feature-settings: "liga" 0; background: #0B0B0B; background-image: url(/src/style/bg.png); background-size: 20px 20px; box-shadow: 0 0 20px 30px rgba(0, 0, 0, 0.5) inset; } body:before, body:after { content: ''; display: block; position: fixed; width: 120vmin; height: 120vmin; pointer-events: none; margin: auto; left: 50%; top: 50%; border-radius: 100%; border: 1px solid rgba(104, 246, 255, 0.22); border-right-style: dashed; border-left-style: dashed; box-shadow: 0 0 0 10px rgba(55, 243, 255, 0.05); -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); z-index: -1; } body:before { width: calc(100vmax - 10px); height: calc(100vmax - 10px); border-color: #008288; border-style: dotted; box-shadow: 0 0 100px rgba(55, 243, 255, 0.2), 0 0 100px rgb(0, 0, 0) inset; } h1, h2, h3, p, span, small, div { cursor: default; } code, code span { cursor: text; } h1, h2 { margin: 0; font-weight: 600; text-shadow: 0 0 10px rgba(55, 243, 255, 0.6); } h1 { float: right; text-align: center; } h3 { display: inline-block; margin-bottom: 0; padding-bottom: 2px; border-bottom: 3px double #0E7379; } a { display: inline-block; color: #1CF2FF; text-decoration: none; } .shadow-bg { background: rgba(0, 0, 0, 0.4); } .top-line { position: relative; border-top: 1px solid #0E7379; } .top-line:before { content: ""; position: absolute; display: block; left: 2px; top: -3px; width: 1px; height: 5px; background-color: #17C3CD; } .top-line:after { content: ""; position: absolute; display: block; right: 2px; top: -3px; width: 1px; height: 5px; background-color: #17C3CD; } .top-dotted { margin: 5px 2px 0 2px; border-top: 3px double #18474A; } .container { padding: 10px; } .container-line > div { padding: 5px; } .container-left { text-align: left; } .container-left > h3 { padding-right: 4px; } .container-right > h3 { padding-left: 4px; } .container-right { text-align: right; } .container-left > div { border-left: 1px solid #0E7379; padding-bottom: 15px; } .container-right > div { border-right: 1px solid #0E7379; padding-bottom: 15px; } flex-container { display: flex; display: -webkit-flex; align-items: flex-start; -webkit-align-items: flex-start; -ms-align-items: flex-start; } .tool-sidebar { height: 100%; display: flex; display: -webkit-flex; flex-direction: column; -webkit-flex-direction: column; } .flex-grow { flex: 1; -webkit-flex: 1; padding: 10px 0; overflow: hidden; } /**/ board { perspective: 900px; } register-container { display: block; } stack-container { display: block; } code-container { } clock-container { display: block; } condition-container { display: block; } /* 3D */ .rot-top { transform: rotateX(-15deg) scaleY(0.95); } .rot-left { transform: rotateY(10deg) scaleX(0.9); } .rot-right { transform: rotateY(-10deg) scaleX(0.9); } ================================================ FILE: src/style/pipe.css ================================================ /** * Created by shuding on 6/10/15. * */ fetch, decode, execute, memory, write-back { display: flex; display: -webkit-flex; width: 100%; } fetch > h3, decode > h3, execute > h3, memory > h3, write-back > h3 { width: 120px; flex: 0 0 120px; text-align: center; border: none; } fetch > div, decode > div, execute > div, memory > div, write-back > div { display: flex; display: -webkit-flex; flex: 1; -webkit-flex: 1; align-items: center; -webkit-align-items: center; } pipe-attr { display: block; width: 10%; max-width: 150px; padding: 2px 5px; } pipe-attr h6, pipe-attr p { margin: 0; } pipe-attr h6 { color: #00b6c1; } pipe-attr:hover { box-shadow: 0 0 0 1px rgb(54, 243, 255) inset; } ================================================ FILE: src/style/register.css ================================================ register-container { padding: 10px; } register-container > div { max-height: calc(100% - 40px); overflow: scroll; } register { display: block; } .register-name { color: #188188; font-size: 13px; font-weight: 900; } .register-data { font-family: 'Jura', 'Courier New', monospace; } ================================================ FILE: src/style/stack.css ================================================ stack-container > div { max-height: calc(100% - 40px); font-family: 'Jura', 'Courier New', monospace; overflow: scroll; } stack > div > span:last-of-type { font-weight: 900; color: #0ADCE6; border-left: 1px solid; padding-left: 3px; margin-left: 3px; } ================================================ FILE: src/test/Forward.yo ================================================ 0x0000: 30820a000000 | irmovl $10,%edx 0x0006: 308203000000 | irmovl $3,%edx 0x000c: 2020 | rrmovl %edx,%eax 0x000e: 10 | halt | ================================================ FILE: src/test/Halt.yo ================================================ 0x0000: 308001000000 | irmovl $1,%eax 0x0006: 10 | halt 0x0007: 308301000000 | irmovl $1,%ebx | ================================================ FILE: src/test/List_Sum.yo ================================================ 0x0000: 308400020000 | irmovl Stack, %esp 0x0006: 308500020000 | irmovl Stack, %ebp 0x000c: 702c000000 | jmp main 0x0011: | .align 4 | 0x0014: | ele1: 0x0014: 0a000000 | .long 0x00a 0x0018: 1c000000 | .long ele2 0x001c: | ele2: 0x001c: b0000000 | .long 0x0b0 0x0020: 24000000 | .long ele3 0x0024: | ele3: 0x0024: 000c0000 | .long 0xc00 0x0028: 00000000 | .long 0 | 0x002c: | main: 0x002c: 308214000000 | irmovl ele1,%edx 0x0032: a028 | pushl %edx 0x0034: 803a000000 | call sum_list 0x0039: 10 | halt | 0x003a: | sum_list: 0x003a: a058 | pushl %ebp 0x003c: 2045 | rrmovl %esp,%ebp 0x003e: 6300 | xorl %eax,%eax 0x0040: 502508000000 | mrmovl 8(%ebp),%edx 0x0046: 6222 | andl %edx,%edx 0x0048: 7362000000 | je L3 0x004d: | L6: 0x004d: 501200000000 | mrmovl (%edx),%ecx 0x0053: 6010 | addl %ecx,%eax 0x0055: 502204000000 | mrmovl 4(%edx),%edx 0x005b: 6222 | andl %edx,%edx 0x005d: 744d000000 | jne L6 0x0062: | L3: 0x0062: 2054 | rrmovl %ebp,%esp 0x0064: b058 | popl %ebp 0x0066: 90 | ret 0x0067: | .pos 0x200 0x0200: | Stack: | ================================================ FILE: src/test/asum.yo ================================================ | /* $begin code-yso */ | /* $begin code-ysa */ | # Execution begins at address 0 0x000: | .pos 0 0x000: 308400010000 | init: irmovl Stack, %esp # Set up Stack pointer 0x006: 308500010000 | irmovl Stack, %ebp # Set up base pointer 0x00c: 7024000000 | jmp Main # Execute main program | | # Array of 4 elements 0x014: | .align 4 0x014: 0d000000 | array: .long 0xd 0x018: c0000000 | .long 0xc0 0x01c: 000b0000 | .long 0xb00 0x020: 00a00000 | .long 0xa000 | 0x024: 308004000000 | Main: irmovl $4,%eax 0x02a: a008 | pushl %eax # Push 4 0x02c: 308214000000 | irmovl array,%edx 0x032: a028 | pushl %edx # Push array 0x034: 803a000000 | call Sum # Sum(array, 4) 0x039: 10 | halt | | /* $begin sum-ys 0 */ | # int Sum(int *Start, int Count) 0x03a: a058 | Sum: pushl %ebp 0x03c: 2045 | rrmovl %esp,%ebp 0x03e: 501508000000 | mrmovl 8(%ebp),%ecx # ecx = Start 0x044: 50250c000000 | mrmovl 12(%ebp),%edx # edx = Count 0x04a: 308000000000 | irmovl $0, %eax # sum = 0 0x050: 6222 | andl %edx,%edx 0x052: 7374000000 | je End 0x057: 506100000000 | Loop: mrmovl (%ecx),%esi # get *Start 0x05d: 6060 | addl %esi,%eax # add to sum 0x05f: 308304000000 | irmovl $4,%ebx # 0x065: 6031 | addl %ebx,%ecx # Start++ 0x067: 3083ffffffff | irmovl $-1,%ebx # 0x06d: 6032 | addl %ebx,%edx # Count-- 0x06f: 7457000000 | jne Loop # Stop when 0 0x074: | End: 0x074: b058 | popl %ebp 0x076: 90 | ret | /* $end sum-ys 0 */ 0x100: | .pos 0x100 0x100: | Stack: # The stack goes here | /* $end code-ysa */ | /* $end code-yso */