From 9f5924784fd7412c0e5fdd8403efbafb5ca4681a Mon Sep 17 00:00:00 2001 From: Nash Brooks Date: Mon, 1 Feb 2021 17:14:01 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9A=20Added=20and=20tested=20push=20an?= =?UTF-8?q?d=20pop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++ console.py | 22 +++++++- main.py | 43 ++++++++++++---- 3 files changed, 197 insertions(+), 12 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd78b0d --- /dev/null +++ b/.gitignore @@ -0,0 +1,144 @@ +# Created by https://www.toptal.com/developers/gitignore/api/python +# Edit at https://www.toptal.com/developers/gitignore?templates=python + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +pytestdebug.log + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ +doc/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +pythonenv* + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# profiling data +.prof + +# End of https://www.toptal.com/developers/gitignore/api/python diff --git a/console.py b/console.py index 87bcfd7..2196347 100644 --- a/console.py +++ b/console.py @@ -1 +1,21 @@ - #nothing here yet jimbo +#made by zombie +import numpy as np + +class Console: + def __init__(self, memory=None): + self._memory = memory + + def draw(self): + disp = np.empty([], dtype=str) + for i, value in enumerate(self._memory): + if i % 2 == 0: #only print text not color + if i % 50 == 0: #console size + + disp = np.append(disp, str(int(value))) + print('\n') + else: + disp = np.append(disp, str(int(value))) + + print(disp) + + diff --git a/main.py b/main.py index 6af6925..a5907d4 100644 --- a/main.py +++ b/main.py @@ -22,12 +22,14 @@ #1 reset #2 debug info (nop) #3 mov +#4 push (onto stack) +#5 pop (off of stack into address) import math import numpy as np - - +from console import Console +np.set_printoptions(threshold=np.inf) @@ -71,6 +73,14 @@ class Zomcpu: elif opcode == 3: #mov self.write_memory(address, value) + elif opcode == 4: #push + self.write_memory(self._memory[7], value) #put value onto stack + self.write_memory(7, self._memory[7] - 1) #dec stackpointer + + elif opcode == 5: #pop + self.write_memory(address, self.read_memory_address(self._memory[7] + 1)) #read the last value and put it into address (not the empty space) + self.write_memory(7, self._memory[7] + 1) #inc stackpointer + self._memory[0] += 3 #increment instruction pointer @@ -81,19 +91,30 @@ class Zomcpu: def read_memory(self): return self._memory def read_memory_address(self, address): - return self._memory[address] - + return self._memory[int(address)] if __name__ == "__main__": cpu = Zomcpu(65536, True) cpu.reset() - print(cpu.read_memory_address(7)) #hard coded opcodes - cpu.write_memory(256, 3) - cpu.write_memory(257, 27) - cpu.write_memory(258, 105) - print(cpu.read_memory_address(27)) - cpu.exec() - print(cpu.read_memory_address(27)) + cpu.write_memory(256, 3) #mov + cpu.write_memory(257, 27) #address 27 + cpu.write_memory(258, 105) #105 into ^ + cpu.write_memory(259, 4) #push + cpu.write_memory(260, 0) #unused in this op + cpu.write_memory(261, 27) #the value at address 27 onto the stack + cpu.write_memory(262, 5) #pop + cpu.write_memory(263, 100) #into 100 + cpu.write_memory(264, 0) #unused in this op + + + + cpu.exec() + print(cpu.read_memory_address(100)) + print(cpu.read_memory_address(264)) + print(cpu.read_memory_address(7)) + + #c = Console(cpu.read_memory()) ima finish this later + #c.draw()