| 868 | } |
| 869 | |
| 870 | protected writeCommandHeader(opcode: number, size: number): void { |
| 871 | if (this.error) return; |
| 872 | |
| 873 | if (this.cmdCount + 1 > this.maxCmdCount) { |
| 874 | this.fail( |
| 875 | "ZRDL_TOO_LARGE", |
| 876 | `maxCmdCount exceeded (count=${this.cmdCount + 1}, max=${this.maxCmdCount})`, |
| 877 | ); |
| 878 | return; |
| 879 | } |
| 880 | |
| 881 | const expected = this.expectedCmdSize(opcode); |
| 882 | if (expected !== size) { |
| 883 | this.fail( |
| 884 | "ZRDL_FORMAT", |
| 885 | `writeCommandHeader: size mismatch for opcode ${opcode} (expected=${expected}, got=${size})`, |
| 886 | ); |
| 887 | return; |
| 888 | } |
| 889 | |
| 890 | if ((this.cmdLen & 3) !== 0) { |
| 891 | this.fail("ZRDL_INTERNAL", "writeCommandHeader: cmd cursor is not 4-byte aligned"); |
| 892 | return; |
| 893 | } |
| 894 | |
| 895 | const alignedSize = align4(size); |
| 896 | const start = this.cmdLen; |
| 897 | const end = start + alignedSize; |
| 898 | this.ensureCmdCapacity(end); |
| 899 | if (this.error) return; |
| 900 | |
| 901 | this.cmdDv.setUint16(start + 0, opcode & 0xffff, true); |
| 902 | this.cmdDv.setUint16(start + 2, 0, true); |
| 903 | this.cmdDv.setUint32(start + 4, size >>> 0, true); |
| 904 | |
| 905 | if (alignedSize !== size) { |
| 906 | this.cmdBuf.fill(0x00, start + size, end); |
| 907 | } |
| 908 | |
| 909 | this.cmdLen = start + 8; |
| 910 | this.cmdCount += 1; |
| 911 | } |
| 912 | |
| 913 | protected expectedCmdSize(_opcode: number): number { |
| 914 | return -1; |