@@ -170,7 +170,7 @@ namespace ts.pxtc {
170170 // see PXT_EXPORTData
171171 const pointerListMarker = "0108010842424242010801083ed8e98d"
172172
173- export function setupFor ( opts : CompileTarget , extInfo : ExtensionInfo ) {
173+ export function setupFor ( opts : CompileTarget , extInfo : ExtensionInfo , cres : CompileResult ) {
174174 ctx = cachedCtxs . find ( c => c . sha == extInfo . sha )
175175
176176 if ( ctx )
@@ -340,12 +340,28 @@ namespace ts.pxtc {
340340 }
341341 m = / ^ : ..( ....) 0 0 / . exec ( hexlines [ i ] )
342342 if ( m ) {
343+ if ( ctx . jmpStartIdx >= 0 && ctx . jmpStartIdx + 1 == i ) {
344+ let n = / ^ : ..( ....) 0 0 ( .{ 4 , } ) / . exec ( hexlines [ i ] ) ;
345+ if ( n ) {
346+ let step = opts . shortPointers ? 4 : 8
347+ let hexb = swapBytes ( n [ 2 ] . slice ( 0 , step ) )
348+ ctx . topFlashAddr = parseInt ( hexb , 16 )
349+ console . log ( "GOT topFlashAddress =" , ctx . topFlashAddr )
350+ }
351+ }
343352 let newAddr = parseInt ( upperAddr + m [ 1 ] , 16 )
344- // TODO: UGH - we need to get flashUsableEnd computed before this
345- if ( ! opts . flashUsableEnd && lastAddr && newAddr - lastAddr > 64 * 1024 )
346- hitEnd ( )
347- if ( opts . flashUsableEnd && newAddr >= opts . flashUsableEnd )
353+
354+ if ( ! opts . flashUsableEnd && lastAddr && newAddr - lastAddr > 64 * 1024 ) {
355+ // the above check is sort of odd - not clear why this case is here
348356 hitEnd ( )
357+ }
358+ if ( opts . flashUsableEnd ) {
359+ let realUsableEnd = getFlashUsableEnd ( target , cres )
360+ if ( newAddr >= realUsableEnd ) {
361+ console . log ( "OVER LIMIT" )
362+ hitEnd ( )
363+ }
364+ }
349365 lastIdx = i
350366 lastAddr = newAddr
351367 }
@@ -378,9 +394,8 @@ namespace ts.pxtc {
378394 if ( ! m ) continue ;
379395
380396 if ( i === ctx . jmpStartIdx + 1 ) {
397+ // skip over the flash top address
381398 let step = opts . shortPointers ? 4 : 8
382- let hexb = swapBytes ( m [ 2 ] . slice ( 0 , step ) )
383- ctx . topFlashAddr = parseInt ( hexb , 16 )
384399 readPointers ( m [ 2 ] . slice ( step ) )
385400 } else {
386401 readPointers ( m [ 2 ] )
@@ -1035,21 +1050,26 @@ ${hexfile.hexPrelude()}
10351050 }
10361051 }
10371052
1038- let peepDbg = false
1039- export function assemble ( target : CompileTarget , bin : Binary , src : string , cres : CompileResult ) {
1040- let b = mkProcessorFile ( target )
1041- b . emit ( src ) ;
1042-
1053+ // compute the real top of flash
1054+ export function getFlashUsableEnd ( target : CompileTarget , cres : CompileResult ) {
1055+ // check if settings size is set
10431056 let settingsSizeDefault = cres . configData . find ( ce => ce . name === "SETTINGS_SIZE_DEFL" )
10441057 let settingsSize = cres . configData . find ( ce => ce . name === "SETTINGS_SIZE" )
10451058 let actualSettingsSize = settingsSize ? settingsSize . value : settingsSizeDefault ? settingsSizeDefault . value : 0
10461059
10471060 const topFlashAddr = hexfile . getTopFlashAddress ( )
1048- let flashUsableEnd = ( topFlashAddr > 0 ? topFlashAddr : target . flashUsableEnd ? target . flashUsableEnd : target . flashEnd ) - actualSettingsSize
1061+ return ( topFlashAddr > 0 ? topFlashAddr : target . flashUsableEnd ? target . flashUsableEnd : target . flashEnd ) - actualSettingsSize
1062+ }
1063+
1064+ let peepDbg = false
1065+ export function assemble ( target : CompileTarget , bin : Binary , src : string , cres : CompileResult ) {
1066+ let b = mkProcessorFile ( target )
1067+ b . emit ( src ) ;
1068+
10491069
10501070 src = `; Interface tables: ${ bin . itFullEntries } /${ bin . itEntries } (${ Math . round ( 100 * bin . itFullEntries / bin . itEntries ) } %)\n` +
10511071 `; Virtual methods: ${ bin . numVirtMethods } / ${ bin . numMethods } \n` +
1052- b . getSource ( ! peepDbg , bin . numStmts , flashUsableEnd ) ;
1072+ b . getSource ( ! peepDbg , bin . numStmts , getFlashUsableEnd ( target , cres ) ) ;
10531073
10541074 throwAssemblerErrors ( b )
10551075
@@ -1262,7 +1282,7 @@ __flash_checksums:
12621282 const opts0 = U . flatClone ( opts )
12631283 // normally, this would already have been done, but if the main variant
12641284 // is disabled, another variant may be set up
1265- hexfile . setupFor ( opts . target , opts . extinfo || emptyExtInfo ( ) )
1285+ hexfile . setupFor ( opts . target , opts . extinfo || emptyExtInfo ( ) , cres )
12661286 assembleAndPatch ( src , bin , opts , cres )
12671287 if ( ! cres . builtVariants ) {
12681288 cres . builtVariants = [ ] ;
@@ -1277,12 +1297,12 @@ __flash_checksums:
12771297 localOpts . extinfo = other . extinfo
12781298 other . target . isNative = true
12791299 localOpts . target = other . target
1280- hexfile . setupFor ( localOpts . target , localOpts . extinfo )
1300+ hexfile . setupFor ( localOpts . target , localOpts . extinfo , cres )
12811301 assembleAndPatch ( src , bin , localOpts , cres )
12821302 cres . builtVariants . push ( other . extinfo ?. appVariant ) ;
12831303 }
12841304 } finally {
1285- hexfile . setupFor ( opts0 . target , opts0 . extinfo )
1305+ hexfile . setupFor ( opts0 . target , opts0 . extinfo , cres )
12861306 }
12871307 }
12881308
0 commit comments