<s> import sys <EOL> import os <EOL> import mmap <EOL> import ctypes <EOL> if sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> : <EOL> long = int <EOL> class MMIOError ( IOError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class MMIO ( object ) : <EOL> def __init__ ( self , physaddr , size ) : <EOL> """<STR_LIT>""" <EOL> self . mapping = None <EOL> self . _open ( physaddr , size ) <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def __enter__ ( self ) : <EOL> pass <EOL> def __exit__ ( self , t , value , traceback ) : <EOL> self . close ( ) <EOL> def _open ( self , physaddr , size ) : <EOL> if not isinstance ( physaddr , int ) and not isinstance ( physaddr , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not isinstance ( size , int ) and not isinstance ( size , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> pagesize = os . sysconf ( os . sysconf_names [ '<STR_LIT>' ] ) <EOL> self . _physaddr = physaddr <EOL> self . _size = size <EOL> self . _aligned_physaddr = physaddr - ( physaddr % pagesize ) <EOL> self . _aligned_size = size + ( physaddr - self . _aligned_physaddr ) <EOL> try : <EOL> fd = os . open ( "<STR_LIT>" , os . O_RDWR | os . O_SYNC ) <EOL> except OSError as e : <EOL> raise MMIOError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> try : <EOL> self . mapping = mmap . mmap ( fd , self . _aligned_size , flags = mmap . MAP_SHARED , prot = ( mmap . PROT_READ | mmap . PROT_WRITE ) , offset = self . _aligned_physaddr ) <EOL> except OSError as e : <EOL> raise MMIOError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> try : <EOL> os . close ( fd ) <EOL> except OSError as e : <EOL> raise MMIOError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> def _adjust_offset ( self , offset ) : <EOL> return offset + ( self . _physaddr - self . _aligned_physaddr ) <EOL> def _validate_offset ( self , offset , length ) : <EOL> if ( offset + length ) > self . _aligned_size : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def read32 ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( offset , int ) and not isinstance ( offset , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> offset = self . _adjust_offset ( offset ) <EOL> self . _validate_offset ( offset , <NUM_LIT:4> ) <EOL> return ctypes . c_uint32 . from_buffer ( self . mapping , offset ) . value <EOL> def read16 ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( offset , int ) and not isinstance ( offset , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> offset = self . _adjust_offset ( offset ) <EOL> self . _validate_offset ( offset , <NUM_LIT:2> ) <EOL> return ctypes . c_uint16 . from_buffer ( self . mapping , offset ) . value <EOL> def read8 ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( offset , int ) and not isinstance ( offset , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> offset = self . _adjust_offset ( offset ) <EOL> self . _validate_offset ( offset , <NUM_LIT:1> ) <EOL> return ctypes . c_uint8 . from_buffer ( self . mapping , offset ) . value <EOL> def read ( self , offset , length ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( offset , int ) and not isinstance ( offset , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> offset = self . _adjust_offset ( offset ) <EOL> self . _validate_offset ( offset , length ) <EOL> c_byte_array = ( ctypes . c_uint8 * length ) . from_buffer ( self . mapping , offset ) <EOL> return bytes ( bytearray ( c_byte_array ) ) <EOL> def write32 ( self , offset , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( offset , int ) and not isinstance ( offset , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not isinstance ( value , int ) and not isinstance ( value , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if value < <NUM_LIT:0> or value > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> offset = self . _adjust_offset ( offset ) <EOL> self . _validate_offset ( offset , <NUM_LIT:4> ) <EOL> ctypes . c_uint32 . from_buffer ( self . mapping , offset ) . value = value <EOL> def write16 ( self , offset , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( offset , int ) and not isinstance ( offset , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not isinstance ( value , int ) and not isinstance ( value , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if value < <NUM_LIT:0> or value > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> offset = self . _adjust_offset ( offset ) <EOL> self . _validate_offset ( offset , <NUM_LIT:2> ) <EOL> ctypes . c_uint16 . from_buffer ( self . mapping , offset ) . value = value <EOL> def write8 ( self , offset , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( offset , int ) and not isinstance ( offset , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not isinstance ( value , int ) and not isinstance ( value , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if value < <NUM_LIT:0> or value > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> offset = self . _adjust_offset ( offset ) <EOL> self . _validate_offset ( offset , <NUM_LIT:1> ) <EOL> ctypes . c_uint8 . from_buffer ( self . mapping , offset ) . value = value <EOL> def write ( self , offset , data ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( offset , int ) and not isinstance ( offset , long ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not isinstance ( data , bytes ) and not isinstance ( data , bytearray ) and not isinstance ( data , list ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> offset = self . _adjust_offset ( offset ) <EOL> self . _validate_offset ( offset , len ( data ) ) <EOL> data = bytearray ( data ) <EOL> c_byte_array = ( ctypes . c_uint8 * len ( data ) ) . from_buffer ( self . mapping , offset ) <EOL> for i in range ( len ( data ) ) : <EOL> c_byte_array [ i ] = data [ i ] <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . mapping is None : <EOL> return <EOL> self . mapping . close ( ) <EOL> self . mapping = None <EOL> self . _fd = None <EOL> @ property <EOL> def base ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _physaddr <EOL> @ property <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _size <EOL> @ property <EOL> def pointer ( self ) : <EOL> """<STR_LIT>""" <EOL> return ctypes . cast ( ctypes . pointer ( ctypes . c_uint8 . from_buffer ( self . mapping , <NUM_LIT:0> ) ) , ctypes . c_void_p ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . base , self . size ) </s>
