import { readFileSync } from 'fs' ; import { BaseMemory , MAX_MEM_SIZE } from './basememory' ; export enum MemAttribute { UNUSED = 0 , ASSIGNED = 0x01 , CODE = 0x02 , CODE_FIRST = 0x04 , DATA = 0x10 } /**
 * Class to hold and access the memory.
 */ export class Memory extends BaseMemory { protected memoryAttr = new Array < MemAttribute > ( MAX_MEM_SIZE ) ; /**
     * Constructor: Initializes memory.
     */ constructor ( ) { super ( 0 , MAX_MEM_SIZE ) ; for ( let i = 0 ; i < MAX_MEM_SIZE ; i ++ ) { this . memory [ i ] = 0 ; this . memoryAttr [ i ] = MemAttribute . UNUSED ; } } param origin param memory public setMemory ( origin , memory ) { const size = memory . length ; for ( let i = 0 ; i < size ; i ++ ) { const addr = ( origin + i ) & ( MAX_MEM_SIZE - 1 ) ; this . memory [ addr ] = memory [ i ] ; this . memoryAttr [ addr ] |= MemAttribute . ASSIGNED ; } } param origin param path public readBinFile ( origin , path ) { let bin = readFileSync ( path ) ; this . setMemory ( origin , bin ) ; } param address returns public getAttributeAt ( address ) { const attr = this . memoryAttr [ address ++ ] ; return attr ; } param address param length param attr public addAttributeAt ( address , length , attr ) { for ( let i = 0 ; i < length ; i ++ ) this . memoryAttr [ address ++ ] |= attr ; } param address param length param attr public setAttributesAt ( address , length , attr ) { for ( let i = 0 ; i < length ; i ++ ) this . memoryAttr [ address ++ ] = attr ; } param address param length param attr public clrAssignedAttributesAt ( address , length ) { for ( let i = 0 ; i < length ; i ++ ) { if ( this . memoryAttr [ address ] != MemAttribute . UNUSED ) this . memoryAttr [ address ] = MemAttribute . ASSIGNED ; address ++ ; } } }