ie.
self.registers = { 'A': 0x0000, ... }
then you could access it directly: self.registers['A'] = 0x0001ie.
self.registers = { 'A': 0x0000, ... }
then you could access it directly: self.registers['A'] = 0x0001 self.registers = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
instead is adding ".value" all over the place where it isn't necessary. I haven't actually tried this but it looks like dropping the Cell class entirely, putting that line in as the definition of registers, then s/.values//g ought to work, or very nearly work.PC, SP, and O are already defined as variables containing the index for that register, a fine way to do it.
It arguably sucks less, but I'm looking for something better and currently trying various solutions involving __getitem__, closures, function attributes, decorators and a sprinkle of metaprogramming to keep things nicely separated and much less C-ish.
I've already defined opcodes this way (decorator+func) which makes it very descriptive and almost reduces the opcode dispatcher to a one-liner.
https://github.com/lloeki/python-dcpu_16/blob/e78f4b8af895db...
Note that this may cause some problems with evaluation order (notably in SET), as spec says a should always be evaluated before b.
Why not use an integer instead of Cell? 42 is memory address 42, -3 is register R, including for SP, PC, etc. Or addresses >= 0x10000 are registers, then you can just have
def ife(self, a, b):
m = self.mem
self.skip = m[a] != m[b]
with Python's native indexing of the mem[] list, which could be an array().So I need to distinguish Register, Memory Location and Literal. I considered passing around a (type, value) tuple but conditional code based on type really tells the OO programmer in me that polymorphism should be used instead.
By splitting the difference and playing with PyPy you should be able to use Python to dodge out on a lot of the C bookkeeping BS while potentially not paying very much on the speed penalty. Using a lot of Python constructs could result in a multiple orders of magnitude slowdown for only marginal gain in this case.
Or whatever. Your program, of course. (No sarcasm.)