add automatic padding in build script

(cherry picked from commit aad1fa45f7)
This commit is contained in:
2026-04-25 05:35:01 +02:00
parent f0656aca40
commit 17c6b07367
2 changed files with 33 additions and 18 deletions

View File

@@ -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<number, string>, lineLength = 0): string {
function bytesToAsmConstU8(label: string, data: Uint8Array, matchingStrings: Record<number, string>, 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 = [];
@@ -132,9 +139,15 @@ function byteToAsmRawData(label: string, data: Uint8Array, matchingStrings: Reco
}
}
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}`
}
/**

View File

@@ -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: