Skip to content

Commit 1aa5071

Browse files
committed
more work on flash usable end
1 parent 7f8c0ec commit 1aa5071

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

pxtcompiler/emitter/emitter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,8 @@ namespace ts.pxtc {
991991
optstarget = enabled.target
992992
}
993993
}
994-
995-
hexfile.setupFor(optstarget, extinfo || emptyExtInfo());
994+
995+
hexfile.setupFor(optstarget, extinfo || emptyExtInfo(), res);
996996
hexfile.setupInlineAssembly(opts);
997997
}
998998

pxtcompiler/emitter/hexfile.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = /^:..(....)00/.exec(hexlines[i])
342342
if (m) {
343+
if (ctx.jmpStartIdx >= 0 && ctx.jmpStartIdx + 1 == i) {
344+
let n = /^:..(....)00(.{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

pxtlib/emitter/assembler.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ namespace ts.pxtc.assembler {
10671067
return r
10681068
}
10691069

1070-
public getSource(clean: boolean, numStmts = 1, flashUsableEnd = 0) {
1070+
public getSource(clean: boolean, numStmts = 1, usableEnd = 0) {
10711071
let lenPrev = 0
10721072
let size = (lbl: string) => {
10731073
let curr = this.labels[lbl] || lenPrev
@@ -1083,16 +1083,16 @@ namespace ts.pxtc.assembler {
10831083
let lenAllCode = lenPrev
10841084
let totalEnd = (lenTotal + this.baseOffset) & 0xffffff
10851085

1086-
if (flashUsableEnd && totalEnd > flashUsableEnd) {
1087-
const e = new Error(lf("program too big by {0} bytes!", totalEnd - flashUsableEnd));
1086+
if (usableEnd && totalEnd > usableEnd) {
1087+
const e = new Error(lf("program too big by {0} bytes!", totalEnd - usableEnd));
10881088
(e as any).ksErrorCode = 9283;
10891089
throw e;
10901090
}
10911091

1092-
flashUsableEnd = flashUsableEnd || 128 * 1024
1092+
usableEnd = usableEnd || 128 * 1024
10931093
let totalInfo = lf("; total bytes: {0} ({1}% of {2}k flash with {3} free)",
1094-
totalEnd, (100 * totalEnd / flashUsableEnd).toFixed(1), (flashUsableEnd / 1024).toFixed(1),
1095-
flashUsableEnd - totalEnd)
1094+
totalEnd, (100 * totalEnd / usableEnd).toFixed(1), (usableEnd / 1024).toFixed(1),
1095+
usableEnd - totalEnd)
10961096
let res =
10971097
// ARM-specific
10981098
lf("; generated code sizes (bytes): {0} (incl. {1} user, {2} helpers, {3} vtables, {4} lits); src size {5}\n",

pxtlib/webble.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ namespace pxt.webBluetooth {
401401
this.hex = hex;
402402
const uf2 = ts.pxtc.UF2.newBlockFile();
403403
ts.pxtc.UF2.writeHex(uf2, this.hex.split(/\r?\n/));
404+
// TODO: update this to get correct usable end
404405
const flashUsableEnd = pxt.appTarget.compile.flashUsableEnd;
405406
this.bin = ts.pxtc.UF2.toBin(U.stringToUint8Array(ts.pxtc.UF2.serializeFile(uf2)), flashUsableEnd).buf;
406407
this.debug(`bin bytes ${this.bin.length}`)

0 commit comments

Comments
 (0)