From 17c6b07367ba85ce9153a8dc376e56d2ff927e03 Mon Sep 17 00:00:00 2001 From: Robin Chappatte Date: Sat, 25 Apr 2026 05:35:01 +0200 Subject: [PATCH] add automatic padding in build script (cherry picked from commit aad1fa45f776d9e6ec86902272b3c3b0c54dc075) --- build.ts | 49 ++++++++++++++++++++++++++---------------- src/reserved_space.asm | 2 ++ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/build.ts b/build.ts index 4902471..e60928f 100755 --- a/build.ts +++ b/build.ts @@ -27,25 +27,26 @@ const asmFilePaths = [ ]; const reservedSpacePath = 'src/reserved_space.asm'; +const instructionLength = 4; // in bytes const mapWidth = 15; const spriteWidth = 5; const mapMatchingStrings = { - 0x00_00_00: 'U8 0', // Empty - 0xFF_00_00: 'U8 1', // Wall - 0xFF_FF_00: 'U8 2', // Coin + 0x00_00_00: '0', // Empty + 0xFF_00_00: '1', // Wall + 0xFF_FF_00: '2', // Coin }; const spritesMatchingStrings = { - 0x00_00_00: 'U8 0b000_000_00', - 0x00_00_FF: 'U8 0b000_000_11', - 0x00_FF_00: 'U8 0b000_111_00', - 0x77_77_77: 'U8 0b010_011_10', - 0xFF_00_00: 'U8 0b111_000_00', - 0xFF_00_FF: 'U8 0b111_000_11', - 0xFF_80_00: 'U8 0b111_100_00', - 0xFF_FF_00: 'U8 0b111_110_00', - 0xFF_FF_FF: 'U8 0b111_111_11', + 0x00_00_00: '0b000_000_00', + 0x00_00_FF: '0b000_000_11', + 0x00_FF_00: '0b000_111_00', + 0x77_77_77: '0b010_011_10', + 0xFF_00_00: '0b111_000_00', + 0xFF_00_FF: '0b111_000_11', + 0xFF_80_00: '0b111_100_00', + 0xFF_FF_00: '0b111_110_00', + 0xFF_FF_FF: '0b111_111_11', }; const mapPath = 'assets/map.ppm'; @@ -86,11 +87,11 @@ for (const path of asmFilePaths) { const asmRawRepresentations = []; const mapBytes = await getDataFromPPM(mapPath); -asmRawRepresentations.push(byteToAsmRawData('map', mapBytes, mapMatchingStrings, mapWidth)); +asmRawRepresentations.push(bytesToAsmConstU8('map', mapBytes, mapMatchingStrings, mapWidth)); for (const [label, path] of Object.entries(spritesPath)) { const bytes = await getDataFromPPM(path); - asmRawRepresentations.push(byteToAsmRawData(label, bytes, spritesMatchingStrings, spriteWidth)); + asmRawRepresentations.push(bytesToAsmConstU8(label, bytes, spritesMatchingStrings, spriteWidth)); } console.log(asmRawRepresentations.join("\n\n")); @@ -104,7 +105,9 @@ console.log(` console.log(await Deno.readTextFile(reservedSpacePath)) /** - * Return an ASM representation of the given data + * Return an ASM constant (U8) representation of the given data + * + * If needed, the representation will be padded to ensure the following code stays aligned on the instruction's length * * @param label The label to write before the first raw data * @param data An array of bytes to interprete as 24 bits values @@ -113,14 +116,18 @@ console.log(await Deno.readTextFile(reservedSpacePath)) * * @returns The ASM representation of that raw data, pre- and post-fixed by the given label */ -function byteToAsmRawData(label: string, data: Uint8Array, matchingStrings: Record, lineLength = 0): string { +function bytesToAsmConstU8(label: string, data: Uint8Array, matchingStrings: Record, lineLength = 0): string { lineLength ??= Infinity; const stringValues = []; for (let i = 0; i < data.length - 2; i += 3) { const pixelValue = (data[i] << 16) + (data[i+1] << 8) + (data[i+2]) + if (!Object.hasOwn(matchingStrings, pixelValue)) { + console.error(`No matching value defined for "${pixelValue.toString(2)}" (which is ${label}'s ${i} pixel)`); + Deno.exit(1); + } - stringValues.push(matchingStrings[pixelValue]); + stringValues.push(`U8 ${matchingStrings[pixelValue]}`); } const lines = []; @@ -131,10 +138,16 @@ function byteToAsmRawData(label: string, data: Uint8Array, matchingStrings: Reco lines.push(stringValues.slice(i, i+lineLength).join("\t")); } } + + const paddingBytes = []; + for (let i = 0; i < stringValues.length % instructionLength; i++) { + paddingBytes.push('U8 0'); + } + const paddingString = paddingBytes.length === 0 ? '' : `\n${paddingBytes.join('\t')} ; padding to preserve ${8 * instructionLength} bits alignment` return `${label}: ${lines.join("\n")} -${label}_end:` +${label}_end:${paddingString}` } /** diff --git a/src/reserved_space.asm b/src/reserved_space.asm index 41ea207..f513191 100644 --- a/src/reserved_space.asm +++ b/src/reserved_space.asm @@ -24,10 +24,12 @@ pub ptr.state_robot: U8 0 ; x coord U8 0 ; y coord U8 0 ; direction +U8 0 ; [padding] pub ptr.state_ghost: U8 0 ; x coord U8 0 ; y coord U8 0 ; direction +U8 0 ; [padding] pub ptr.screen: \ No newline at end of file