diff --git a/.gitignore b/.gitignore index 56c3397..f99f858 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,8 @@ ASMSimulator.iml *~ *.new *.old +*.txt +cucu-cc + +*.o +*.pyc diff --git a/Simserver.py b/Simserver.py new file mode 100755 index 0000000..ceeb79c --- /dev/null +++ b/Simserver.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +import BaseHTTPServer +import urlparse +import os +import unittest +import json +from cucu.cucu import CucuVM + +class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): + def do_POST(self): + payload = json.loads(self.rfile.read(int(self.headers['content-length']))) + source = payload["source"].split('\n') + real_source = '' + for line in source: + if not line.startswith(';'): + real_source += line + '\n' + c = CucuVM(real_source) + parsed_path = urlparse.urlparse(self.path) + message_parts = [ + 'CLIENT VALUES:', + 'client_address=%s (%s)' % (self.client_address, + self.address_string()), + 'command=%s' % self.command, + 'path=%s' % self.path, + 'real path=%s' % parsed_path.path, + 'query=%s' % parsed_path.query, + 'request_version=%s' % self.request_version, + '', + 'SERVER VALUES:', + 'server_version=%s' % self.server_version, + 'sys_version=%s' % self.sys_version, + 'protocol_version=%s' % self.protocol_version, + '', + 'HEADERS RECEIVED:', + ] + for name, value in sorted(self.headers.items()): + message_parts.append('%s=%s' % (name, value.rstrip())) + message_parts.append('') + message = '\n'.join(message_parts) + self.send_response(200) + self.end_headers() + self.wfile.write(c.code) + def do_GET(self): + parsed_path = urlparse.urlparse(self.path) + pa = self.path; + self.send_response(200) + self.end_headers() + if(pa[0:17] == "/examples/scandir"): + info = os.getcwd(); + arr = sorted(os.listdir(info+"/examples")); + results = [] + for fn in arr: + if not fn.endswith('.txt'): + continue + f = open('examples/' + fn, 'r') + line = f.readlines()[0].strip() + if 'HIDE' in line: + continue + results.append('"%s|%s"' % (fn, line[2:])) + text = '[%s]' % ','.join(results) + self.wfile.write(text); + else: + if pa == "/": + pa = "/index.html" + input = open(pa[1:],"r"); + self.wfile.write(input.read()); + input.close() + +server = BaseHTTPServer.HTTPServer(('0.0.0.0',8082), WebRequestHandler) +server.serve_forever() diff --git a/cucu/Makefile b/cucu/Makefile new file mode 100644 index 0000000..c653a6e --- /dev/null +++ b/cucu/Makefile @@ -0,0 +1,18 @@ +#CFLAGS := -Wall -W -std=c89 + +all: cucu-cc + +cucu-cc: cucu.o + $(CC) -o $@ $< + +cucu.o: cucu.c gen.c + $(CC) -c $< -DGEN=\"gen.c\" -o $@ + +install: cucu-cc + cp cucu-cc .. + +clean: + rm -f cucu + rm -f *.o + +.PHONY: all diff --git a/cucu/__init__.py b/cucu/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/from_yyk/cucu.c b/cucu/cucu.c similarity index 100% rename from from_yyk/cucu.c rename to cucu/cucu.c diff --git a/from_yyk/cucu.py b/cucu/cucu.py similarity index 97% rename from from_yyk/cucu.py rename to cucu/cucu.py index e40eb40..2fe09a3 100644 --- a/from_yyk/cucu.py +++ b/cucu/cucu.py @@ -6,7 +6,7 @@ import time # Interpret VM instruction # class CucuVM: - CUCU_PATH='cucu-dummy.exe' + CUCU_PATH='./cucu-cc' def __init__(self, src, debug=False): self.A = 0 self.B = 0 @@ -43,7 +43,7 @@ class CucuVM: #op = op.replace(' ', '') if (self.debug): print("op", op) - + self.PC = self.PC + 8 if (op.startswith(';')): return @@ -105,9 +105,9 @@ class CucuVM: elif (op.startswith('pop')): n = int(op[3:], 16) self.SP = self.SP + n*2 - elif (op.startswith('A:=')): + elif (op.startswith('A:=')): self.A = int(op[3:], 16) - elif (op.startswith('sp@')): + elif (op.startswith('sp@')): self.A = self.SP + int(op[3:], 16)*2 # TODO #print(self.A) elif (op.startswith('jmp')): @@ -128,5 +128,3 @@ class CucuVM: print("A:%04x B:%04x PC:%x SP:%x" % (self.A, self.B, self.PC, self.SP)) print("Mem:", self.mem) print() - - diff --git a/from_yyk/gen-dummy/gen.c b/cucu/gen.c similarity index 100% rename from from_yyk/gen-dummy/gen.c rename to cucu/gen.c diff --git a/from_yyk/Makefile b/from_yyk/Makefile deleted file mode 100644 index cd7d30d..0000000 --- a/from_yyk/Makefile +++ /dev/null @@ -1,24 +0,0 @@ -#CFLAGS := -Wall -W -std=c89 - -all: cucu-dummy - -test: cucu-dummy-test cucu-x86-test - -cucu-dummy: cucu-dummy.o -cucu-dummy.o: cucu.c gen-dummy/gen.c - $(CC) -c $< -DGEN=\"gen-dummy/gen.c\" -o $@ -cucu-dummy-test: cucu-dummy - python gen-dummy/test.py - -cucu-x86: cucu-x86.o -cucu-x86.o: cucu.c gen-x86/gen.c - $(CC) -c $< -DGEN=\"gen-x86/gen.c\" -o $@ -cucu-x86-test: cucu-x86 - sh gen-x86/test.sh - -clean: - rm -f cucu-dummy - rm -f cucu-x86 - rm -f *.o - -.PHONY: all diff --git a/from_yyk/Simserver.py b/from_yyk/Simserver.py deleted file mode 100644 index d92fc85..0000000 --- a/from_yyk/Simserver.py +++ /dev/null @@ -1,70 +0,0 @@ -import BaseHTTPServer -import urlparse -import os -import unittest -from cucu import CucuVM -class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): - def do_POST(self): - #print(self.client_address) - code = self.rfile.read(int(self.headers['content-length']))[2:] - code = code.replace('+',' ') - code = code.replace('%0A','\r\n') - code = code.replace('%3B',';') - code = code.replace('%2B','+') - code = code.replace('%3D','=') - code = code.replace('%7B','{') - code = code.replace('%7D','}') - print code - c = CucuVM(code) - parsed_path = urlparse.urlparse(self.path) - message_parts = [ - 'CLIENT VALUES:', - 'client_address=%s (%s)' % (self.client_address, - self.address_string()), - 'command=%s' % self.command, - 'path=%s' % self.path, - 'real path=%s' % parsed_path.path, - 'query=%s' % parsed_path.query, - 'request_version=%s' % self.request_version, - '', - 'SERVER VALUES:', - 'server_version=%s' % self.server_version, - 'sys_version=%s' % self.sys_version, - 'protocol_version=%s' % self.protocol_version, - '', - 'HEADERS RECEIVED:', - ] - for name, value in sorted(self.headers.items()): - message_parts.append('%s=%s' % (name, value.rstrip())) - message_parts.append('') - message = '\r\n'.join(message_parts) - self.send_response(200) - self.end_headers() - self.wfile.write(c.code) - def do_GET(self): - parsed_path = urlparse.urlparse(self.path) - print(self.path); - pa = self.path; - self.send_response(200) - self.end_headers() - #print pa; - #print pa[0:2] - #print pa[0:2] == "/ex" - if(pa[0:17] == "/examples/scandir"): - print("!!!!!!!!!!!!!!!!!"); - info = os.getcwd(); - arr = os.listdir(info+"\examples"); - #for i in arr: - # arr = arr + "|1\r\n"; - text = '["21-addition.txt|2.1 c = a + b\\r\\n","31-time-sharing-2-processes.txt|3.1 Time-sharing Between Two Processes\\r\\n","51-insertion-sort.txt|5.1 Insertion Sort\\r\\n","52-binary-search.txt|5.2 Binary Search\\r\\n","61-call-by-value.txt|6.1 Passing Parameters by Value\\r\\n","62-call-by-reference.txt|6.2 Passing Parameters by Reference\\r\\n"]'; - self.wfile.write(text); - - - else: - print(">>>>>>>>>>>>>>>>>"); - input = open(self.path[1:],"r"); - self.wfile.write(input.read()); - input.close() - -server = BaseHTTPServer.HTTPServer(('127.0.0.1',8080), WebRequestHandler) -server.serve_forever() \ No newline at end of file diff --git a/from_yyk/cucu-dummy.exe b/from_yyk/cucu-dummy.exe deleted file mode 100644 index efc766e..0000000 Binary files a/from_yyk/cucu-dummy.exe and /dev/null differ diff --git a/from_yyk/index.html b/from_yyk/index.html deleted file mode 100644 index 512b209..0000000 --- a/from_yyk/index.html +++ /dev/null @@ -1,191 +0,0 @@ - - - - Simple 8-bit V8-CPU in Javascript - - - - - - -
-
{{ error }}
-
-
-
-
-

Code (Instruction Set)

-
- - - - - Examples: - - - - - -
- -
-
-
-
-
-
-
-

Printer

-
-
{{ printer.data }}
-
-
-
-

CPU & Memory

-
-
-

General Registers

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
0123456789ABCDEF
{{ cpu.gpr[0] | number:displayHex }}
{{ cpu.gpr[1] | number:displayHex }}
{{ cpu.gpr[2] | number:displayHex }}
{{ cpu.gpr[3] | number:displayHex }}
{{ cpu.gpr[4] | number:displayHex }}
{{ cpu.gpr[5] | number:displayHex }}
{{ cpu.gpr[6] | number:displayHex }}
{{ cpu.gpr[7] | number:displayHex }}
{{ cpu.gpr[8] | number:displayHex }}
{{ cpu.gpr[9] | number:displayHex }}
{{ cpu.gpr[10] | number:displayHex }}
{{ cpu.gpr[11] | number:displayHex }}
{{ cpu.gpr[12] | number:displayHex }}
{{ cpu.gpr[13] | number:displayHex }}
{{ cpu.gpr[14] | number:displayHex }}
{{ cpu.gpr[15] | number:displayHex }}
-

Other Registers

- - - - - - - - - - - - - - - -
Program counter {{ cpu.status }}
{{ cpu.ip | number:displayHex }}
Instruction register
{{ cpu.ir | number:displayHex }}
Timer countdown
{{ cpu.countdown | number:displayHex }}
-

RAM

-
-
-
- {{ m | number:displayHex }} - - {{ m | number:displayHex }} - -
-
-
-

- - Clock speed: - - Instructions: - Show - Hide - -

-
-
-
-
-

Labels

-
-
- - - - - - - - - - - -
NameAddressValue
{{ name }}{{ value | number:displayHex }}{{ memory.data[value] | number:displayHex }}
-
-
-
-
-
-

by Yuanchun Shi, Yu Chen, Junjie Mao, Yukang Yan (2015) | MIT License | Source Code

-

by Marco Schweighauser (2015) | MIT License | Blog

-
- - - - - - diff --git a/from_yyk/readme.docx b/from_yyk/readme.docx deleted file mode 100644 index 16f8a77..0000000 Binary files a/from_yyk/readme.docx and /dev/null differ diff --git a/from_yyk/readme.txt b/from_yyk/readme.txt deleted file mode 100644 index b7c104f..0000000 --- a/from_yyk/readme.txt +++ /dev/null @@ -1,5 +0,0 @@ -cucu.py, cucu-dummy.exe, Simserver.pys三个文件应放置在与index.html同一目录下 -启动server: Python Simserver.py -http://localhost:8080/index.html 为目前的页面url -在textarea内编辑c 代码,点击complie返回v8代码,点击assemble后可以run,简单写了两个testcase,是test1.txt和test2.txt。 - diff --git a/from_yyk/test1.txt b/from_yyk/test1.txt deleted file mode 100644 index a169e49..0000000 --- a/from_yyk/test1.txt +++ /dev/null @@ -1,6 +0,0 @@ -int main() -{ -int i = 3; -int j = 5; -i = i + j; -} \ No newline at end of file diff --git a/from_yyk/test2.txt b/from_yyk/test2.txt deleted file mode 100644 index a17f94e..0000000 --- a/from_yyk/test2.txt +++ /dev/null @@ -1,8 +0,0 @@ -int main() -{ - int i = 5; - while(i != 10) - { - i = i + 1; - } -} \ No newline at end of file diff --git a/from_yyk/test3.txt b/from_yyk/test3.txt deleted file mode 100644 index 2ccde4f..0000000 --- a/from_yyk/test3.txt +++ /dev/null @@ -1,9 +0,0 @@ -int main() -{ - int i = 3; - int j = 5; - if(i != 5) - { - j = 6; - } -} \ No newline at end of file diff --git a/index.html b/index.html index ec4a55f..2b0a250 100644 --- a/index.html +++ b/index.html @@ -28,14 +28,27 @@
- - + + + + Examples: + + + +
+
+
+

Printer

{{ printer.data }}
-
-
-

CPU & Memory

@@ -171,5 +184,7 @@
+ + diff --git a/src/ui/controller.js b/src/ui/controller.js index db3649a..f8718de 100644 --- a/src/ui/controller.js +++ b/src/ui/controller.js @@ -18,9 +18,7 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', '$http', 'cpu', 'memo $scope.example = ''; $scope.examples = []; - $scope.codeFormat = ''; - - $scope.code = ";; Choose an example above or write your own code here :)\n\n"; + $scope.code = ";; Choose an example above or write your own code here :)"; $scope.reset = function () { cpu.reset(); memory.reset(); @@ -31,11 +29,11 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', '$http', 'cpu', 'memo }; $scope.executeStep = function () { - try { - if (!$scope.checkPrgrmLoaded()) { - $scope.updateCode(); - } + if (!$scope.checkPrgrmLoaded()) { + $scope.assemble(); + } + try { // Execute var res = cpu.step(); @@ -53,13 +51,8 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', '$http', 'cpu', 'memo var runner; $scope.run = function () { - try { - if (!$scope.checkPrgrmLoaded()) { - $scope.updateCode(); - } - } catch (e) { - $scope.error = e; - return; + if (!$scope.checkPrgrmLoaded()) { + $scope.assemble(); } $scope.isRunning = true; @@ -97,20 +90,9 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', '$http', 'cpu', 'memo } }; - $scope.updateCode = function () { - if ($scope.codeFormat == 'assembly') { - $scope.assemble(); - } else if ($scope.codeFormat == 'raw') { - $scope.upload(); - } else { - throw "Please compile/assemble/upload your code."; - } - }; - $scope.assemble = function () { try { $scope.reset(); - $scope.codeFormat = 'assembly'; var assembly = assembler.go($scope.code); $scope.mapping = assembly.mapping; @@ -140,7 +122,6 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', '$http', 'cpu', 'memo $scope.upload = function () { try { $scope.reset(); - $scope.codeFormat = 'raw'; var binarycode = uploader.go($scope.code); $scope.mapping = binarycode.mapping; @@ -163,6 +144,11 @@ app.controller('Ctrl', ['$document', '$scope', '$timeout', '$http', 'cpu', 'memo } }; + $scope.compile = function () { + $http.post('/', {"source": $scope.code}).success(function(response){ + $scope.code = response; $scope.assemble();}); + }; + $scope.initExamples = function() { var response = $http.get('examples/scandir.php'); response.success(function(data, status, headers, config) {