import * as fs from 'fs' ; import { MemBank16k } from './membank16k' ; /**
 * A parser for the .nex file format.
 * Reads basically only the different memory banks.
 */ export class NexFile { public memBanks : Array < MemBank16k > ; public slot3Bank : number ; public borderColor : number ; public sp : number ; public pc : number ; /**
     * Constructor.
     */ constructor ( ) { this . memBanks = new Array < MemBank16k > ( ) ; } see https public readFile ( path ) { const LOADING_SCREENS = 10 ; const BORDER_COLOR = 11 ; const SP = 12 ; const PC = 14 ; const USED_BANKS = 18 ; const SLOT3_BANK = 139 ; const COPPER_CODE_BLOCK = 153 ; const FILE_HEADER_SIZE = 512 ; const PALETTE_SIZE = 512 ; const L2_LOADING_SCREEN_SIZE = 49512 ; const ULA_LOADING_SCREEN_SIZE = 6912 ; const LOWRES_LOADING_SCREEN_SIZE = 12288 ; const TIMEX_HIRES_LOADING_SCREEN_SIZE = 12288 ; const TIMEX_HICOL_LOADING_SCREEN_SIZE = 12288 ; const L2B_LOADING_SCREEN_SIZE = 81920 ; const COPPER_CODE_BLOCK_SIZE = 2048 ; const nexBuffer = fs . readFileSync ( path ) ; const loadingScreensFlags = nexBuffer [ LOADING_SCREENS ] ; let memBankIndex = FILE_HEADER_SIZE ; if ( loadingScreensFlags & 0x80 ) memBankIndex += PALETTE_SIZE ; if ( loadingScreensFlags & 0x01 ) memBankIndex += L2_LOADING_SCREEN_SIZE ; if ( loadingScreensFlags & 0x02 ) memBankIndex += ULA_LOADING_SCREEN_SIZE ; if ( loadingScreensFlags & 0x04 ) memBankIndex += LOWRES_LOADING_SCREEN_SIZE ; if ( loadingScreensFlags & 0x08 ) memBankIndex += TIMEX_HIRES_LOADING_SCREEN_SIZE ; if ( loadingScreensFlags & 0x10 ) memBankIndex += TIMEX_HICOL_LOADING_SCREEN_SIZE ; if ( loadingScreensFlags & 0x40 ) memBankIndex += L2B_LOADING_SCREEN_SIZE ; const copperFlags = nexBuffer [ COPPER_CODE_BLOCK ] ; if ( copperFlags & 0x01 ) memBankIndex += COPPER_CODE_BLOCK_SIZE ; this . borderColor = nexBuffer [ BORDER_COLOR ] ; this . sp = nexBuffer [ SP ] + ( nexBuffer [ SP + 1 ] << 8 ) ; this . pc = nexBuffer [ PC ] + ( nexBuffer [ PC + 1 ] << 8 ) ; for ( let i = 0 ; i < MemBank16k . MAX_NUMBER_OF_BANKS ; i ++ ) { const k = MemBank16k . getMemBankPermutation ( i ) ; const byteFlag = nexBuffer [ USED_BANKS + k ] ; if ( byteFlag ) { const memBank = new MemBank16k ( ) ; memBank . bank = k ; this . memBanks . push ( memBank ) ; } } this . slot3Bank = nexBuffer [ SLOT3_BANK ] ; for ( const memBank of this . memBanks ) { const data = memBank . data ; nexBuffer . copy ( data , 0 , memBankIndex , memBankIndex + MemBank16k . BANK16K_SIZE ) ; memBankIndex += MemBank16k . BANK16K_SIZE ; } } }