import * as fs from 'fs' ; import { Utility } from '../../misc/utility' ; import { MemBank16k } from './membank16k' ; /**
 * A parser for the .sna file format.
 * Can read 48k sna and 128k sna file formats.
 */ export class SnaFile { public memBanks : Array < MemBank16k > ; public i : number ; public hl2 : number ; public de2 : number ; public bc2 : number ; public af2 : number ; public hl : number ; public de : number ; public bc : number ; public iy : number ; public ix : number ; public iff2 : number ; public r : number ; public af : number ; public sp : number ; public im : number ; public borderColor : number ; public pc : number ; public port7ffd : number ; public trdosrom : number ; /**
     * Constructor.
     */ constructor ( ) { this . memBanks = new Array < MemBank16k > ( ) ; } see https public readFile ( path ) { const HEADER_LENGTH = 27 ; const snaBuffer = fs . readFileSync ( path ) ; this . i = snaBuffer [ 0 ] ; this . hl2 = Utility . getWord ( snaBuffer , 1 ) ; this . de2 = Utility . getWord ( snaBuffer , 3 ) ; this . bc2 = Utility . getWord ( snaBuffer , 5 ) ; this . af2 = Utility . getWord ( snaBuffer , 7 ) ; this . hl = Utility . getWord ( snaBuffer , 9 ) ; this . de = Utility . getWord ( snaBuffer , 11 ) ; this . bc = Utility . getWord ( snaBuffer , 13 ) ; this . iy = Utility . getWord ( snaBuffer , 15 ) ; this . ix = Utility . getWord ( snaBuffer , 17 ) ; this . iff2 = Utility . getWord ( snaBuffer , 19 ) ; this . r = snaBuffer [ 20 ] ; this . af = Utility . getWord ( snaBuffer , 21 ) ; this . sp = Utility . getWord ( snaBuffer , 23 ) ; this . im = snaBuffer [ 25 ] ; this . borderColor = snaBuffer [ 26 ] ; let index = HEADER_LENGTH ; for ( let i = 0 ; i < 3 ; i ++ ) { const memBank = new MemBank16k ( ) ; memBank . data . set ( snaBuffer . slice ( index , index + MemBank16k . BANK16K_SIZE ) ) ; const p = MemBank16k . getMemBankPermutation ( i ) ; memBank . bank = p ; this . memBanks . push ( memBank ) ; index += MemBank16k . BANK16K_SIZE ; } Utility . assert ( index == 49179 ) ; if ( snaBuffer . length <= index ) { this . pc = Utility . getWord ( snaBuffer , HEADER_LENGTH + this . sp - 0x4000 ) ; this . sp += 2 ; return ; } this . pc = Utility . getWord ( snaBuffer , index ) ; index += 2 ; this . port7ffd = snaBuffer [ index ++ ] ; this . trdosrom = snaBuffer [ index ++ ] ; Utility . assert ( index == 49183 ) ; const pagedInBank = this . port7ffd & 0x03 ; this . memBanks [ 2 ] . bank = pagedInBank ; for ( let i = 2 ; i < 8 ; i ++ ) { const p = MemBank16k . getMemBankPermutation ( i ) ; if ( p == pagedInBank ) continue ; const memBank = new MemBank16k ( ) ; memBank . data . set ( snaBuffer . slice ( index , index + MemBank16k . BANK16K_SIZE ) ) ; memBank . bank = p ; this . memBanks . push ( memBank ) ; index += MemBank16k . BANK16K_SIZE ; } } }