mirror of
https://github.com/lloeki/python-dcpu_16.git
synced 2025-12-06 09:54:39 +01:00
Isolate memory from CPU
This commit is contained in:
parent
7d6205cac9
commit
bf055e998e
1 changed files with 22 additions and 2 deletions
24
dcpu_16.py
24
dcpu_16.py
|
|
@ -297,9 +297,29 @@ def literal(c, code):
|
||||||
return v
|
return v
|
||||||
|
|
||||||
|
|
||||||
|
class Memory(object):
|
||||||
|
"""array of 16-bit words"""
|
||||||
|
def __init__(m, debug=False):
|
||||||
|
m.clear()
|
||||||
|
|
||||||
|
def clear(m):
|
||||||
|
"""clear memory"""
|
||||||
|
# TODO: (addr, len) memory range to clear
|
||||||
|
m.w = [0 for _ in xrange(0, 2**w)]
|
||||||
|
|
||||||
|
def __getitem__(m, addr):
|
||||||
|
"""get word at addr"""
|
||||||
|
return m.w[addr]
|
||||||
|
|
||||||
|
def __setitem__(m, addr, value):
|
||||||
|
"""assignment truncates values to 16-bit words"""
|
||||||
|
# TODO: multi-word assignment starting at addr when value is list
|
||||||
|
m.w[addr] = value & wmask
|
||||||
|
|
||||||
|
|
||||||
class CPU(object):
|
class CPU(object):
|
||||||
def __init__(c, debug=False):
|
def __init__(c, memory=Memory(), debug=False):
|
||||||
|
c.m = memory
|
||||||
c.clear()
|
c.clear()
|
||||||
c.reset()
|
c.reset()
|
||||||
c.debug = debug
|
c.debug = debug
|
||||||
|
|
@ -314,7 +334,7 @@ class CPU(object):
|
||||||
|
|
||||||
def clear(c):
|
def clear(c):
|
||||||
"""clear memory"""
|
"""clear memory"""
|
||||||
c.m = [0 for _ in xrange(0, 2**w)]
|
c.m.clear()
|
||||||
|
|
||||||
a = Register(0x0)
|
a = Register(0x0)
|
||||||
b = Register(0x1)
|
b = Register(0x1)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue