1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- # z80jit - Z80 CPU emulator [with JIT compilation?], in rpython.
- # Copyright (C) 2014-2017 Jason Harris <jth@mibot.com>
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>
- #-----------------------------------------------------------------------------
- """
- Memory Devices
- """
- #-----------------------------------------------------------------------------
- import array
- #-----------------------------------------------------------------------------
- _empty = 0xff
- #-----------------------------------------------------------------------------
- # Base Memory Device
- class memory:
- """Base Memory Device"""
- def __init__(self, bits = 0):
- """Create a memory device of size bytes."""
- size = 1 << bits
- self.mask = size - 1
- self.mem = array.array('B', (0,) * size)
- self.wr_notify = self.null
- self.rd_notify = self.null
- def null(self, adr):
- """do nothing read/write notification"""
- pass
- def __getitem__(self, adr):
- return _empty
- def __setitem__(self, adr, val):
- pass
- def load(self, adr, data):
- """load bytes into memory starting at a given address"""
- for i, val in enumerate(data):
- self.mem[adr + i] = val
- def load_file(self, adr, filename):
- """load file into memory starting at a given address"""
- for i, val in enumerate(open(filename, "rb").read()):
- self.mem[adr + i] = ord(val)
- #-----------------------------------------------------------------------------
- # Specific Memory Devices
- class ram(memory):
- """Read/Write Memory"""
- def __getitem__(self, adr):
- return self.mem[adr & self.mask]
- def __setitem__(self, adr, val):
- if val != self.mem[adr & self.mask]:
- self.wr_notify(adr)
- self.mem[adr & self.mask] = val
- class rom(memory):
- """Read Only Memory"""
- def __getitem__(self, adr):
- return self.mem[adr & self.mask]
- class wom(memory):
- """Write Only Memory"""
- def __setitem__(self, adr, val):
- if val != self.mem[adr & self.mask]:
- self.wr_notify(adr)
- self.mem[adr & self.mask] = val
- def rd(self, adr):
- """backdoor read"""
- return self.mem[adr & self.mask]
- class null(memory):
- """Unpopulated Memory"""
- pass
- #-----------------------------------------------------------------------------
|