import * as assert from 'assert' ; import { BaseMemory } from './basememory' ; export class Format { public static hexNumbersLowerCase = false ; param value param countDigits returns public static getHexString ( value , countDigits = 4 ) { let s = value . toString ( 16 ) ; if ( ! Format . hexNumbersLowerCase ) s = s . toUpperCase ( ) ; return Format . fillDigits ( s , '0' , countDigits ) ; } /**
     * If string is smaller than countDigits the string is filled with 'fillCharacter'.
     * Used to fill a number up with '0' or spaces.
     */ public static fillDigits ( valueString , fillCharacter , countDigits ) { const repeat = countDigits - valueString . length ; if ( repeat <= 0 ) return valueString ; const res = fillCharacter . repeat ( repeat ) + valueString ; return res ; } param s param totalLength returns public static addSpaces ( s , totalLength ) { const countString = s . length ; const repeat = totalLength - countString ; if ( repeat <= 0 ) return s ; const res = s + ' ' . repeat ( repeat ) ; return res ; } param byteValue returns public static getVariousConversionsForByte ( byteValue ) { if ( byteValue < 0 ) byteValue = 0x100 + byteValue ; let result = byteValue . toString ( ) ; let convValue = byteValue ; if ( convValue >= 0x80 ) { convValue -= 0x100 ; result += ', ' + Format . fillDigits ( convValue . toString ( ) , ' ' , 4 ) ; } if ( byteValue >= 32 && byteValue <= 126 ) result += ", '" + String . fromCharCode ( byteValue ) + "'" ; return result ; } param value returns public static getConversionForAddress ( value ) { let result = Format . getHexString ( value ) + 'h' ; return result ; } param wordValue returns public static getVariousConversionsForWord ( wordValue ) { let result = wordValue . toString ( ) ; let convValue = wordValue ; if ( convValue >= 0x8000 ) { convValue -= 0x10000 ; result += ', ' + this . fillDigits ( convValue . toString ( ) , ' ' , 6 ) ; } return result ; } param memory param opcodesLowerCase param clmnsAddress param clmnsBytes param clmnsOpcodeFirstPart param clmsnOpcodeTotal param address param size param mainString public static formatDisassembly ( memory , opcodesLowerCase , clmnsAddress , clmnsBytes , clmnsOpcodeFirstPart , clmsnOpcodeTotal , address , size , mainString ) { let line = '' ; if ( clmnsAddress > 0 ) { line = Format . addSpaces ( Format . getHexString ( address ) + ' ' , clmnsAddress ) ; } let bytesString = '' ; if ( memory ) { for ( let i = 0 ; i < size ; i ++ ) { const memVal = memory . getValueAt ( address + i ) ; bytesString += Format . getHexString ( memVal , 2 ) + ' ' ; } } line += Format . addSpaces ( bytesString , clmnsBytes ) ; const arr = mainString . split ( ' ' ) ; assert ( arr . length > 0 , 'formatDisassembly' ) ; arr [ 0 ] = Format . addSpaces ( arr [ 0 ] , clmnsOpcodeFirstPart - 1 ) ; let resMainString = arr . join ( ' ' ) ; resMainString = Format . addSpaces ( resMainString + ' ' , clmsnOpcodeTotal ) ; line += resMainString ; return line ; } }