// SPDX-FileCopyrightText: © 2012 Salvatore Sanfilippo // SPDX-FileCopyrightText: © 2014-2016 Oliver Jowett // SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: GPL-2.0-or-later //! Mode A and Mode C: the twelve-pulse replies that predate Mode S. //! //! A Mode A reply carries a four-digit octal squawk and a Mode C reply an //! altitude, and the two are indistinguishable on the wire — the same twelve //! pulses mean one thing if the interrogation was Mode A and another if it was //! Mode C. What tells them apart, when a receiver saw the reply but not the //! interrogation, is that only some pulse patterns are a legal altitude, so a //! reply that cannot be an altitude must have been a squawk. //! //! The twelve pulses are handled throughout as a "hex-encoded" 16-bit value, //! one octal digit per nibble with the top bit of each nibble spare: //! //! ```text //! 0 A4 A2 A1 0 B4 B2 B1 0 C4 C2 C1 0 D4 D2 D1 //! ``` //! //! That wastes four bits but makes the squawk legible in hex — a reply //! displayed as `0x7700` is squawk 7700 — which is why upstream chose it, and //! why every squawk in the output formats is printed the same way. //! //! The altitude encoding is a Gilham code: the C pulses give hundreds of feet //! in a cyclic three-bit code and the D, A and B pulses give five-hundreds in //! reflected binary, with the hundreds counting backwards through every other //! five-hundred band. Getting a single bit wrong therefore moves the altitude //! by a whole band rather than by a hundred feet, which is why Mode C altitude //! is treated with suspicion everywhere it is used. const std = @import("std"); /// Altitude in hundreds of feet, as a Mode C reply encodes it. The lowest /// legal value is -12 (that is, -1200 feet). pub const Hundreds = i16; /// The altitude a reply means if it was a Mode C reply, or null if it is not /// a legal altitude code — in which case the reply was a Mode A squawk. pub fn toAltitude(mode_a: u16) ?Hundreds { return altitude_table[toIndex(mode_a)]; } /// The reply that encodes a given altitude, or null if no reply does. Used to /// decide whether a squawk that was heard could have been a mis-read altitude /// from an aircraft at a known height. pub fn fromAltitude(hundreds: Hundreds) ?u16 { const index = @as(i32, hundreds) + altitude_bias; if (index < 0 or index >= squawk_table.len) return null; const mode_a = squawk_table[@intCast(index)]; return if (mode_a == 0) null else mode_a; } /// Pack the twelve meaningful bits of a reply into a dense 0-4095 index, /// dropping the four spare bits. Anything indexed by squawk — the tables here /// and the tracker's squawk histogram — is indexed by this. pub fn toIndex(mode_a: u16) u12 { return @intCast((mode_a & 0x0007) | ((mode_a & 0x0070) >> 1) | ((mode_a & 0x0700) >> 2) | ((mode_a & 0x7000) >> 3)); } /// Spread a dense index back out into the hex-encoded form. pub fn fromIndex(index: u12) u16 { const wide: u16 = index; return (wide & 0o0007) | ((wide & 0o0070) << 1) | ((wide & 0o0700) << 2) | ((wide & 0o7000) << 3); } /// How far the altitude table is shifted so that its lowest entry, -12 /// hundreds of feet, lands at a non-negative index. const altitude_bias = 13; /// The altitude each of the 4096 replies encodes, or null where the pulse /// pattern is not a legal altitude. const altitude_table: [4096]?Hundreds = blk: { @setEvalBranchQuota(200_000); var table: [4096]?Hundreds = undefined; for (&table, 0..) |*entry, index| { entry.* = gilhamToAltitude(fromIndex(index)); } break :blk table; }; /// The reverse: which reply encodes each altitude, indexed by the altitude /// plus `altitude_bias`, with zero meaning no reply does. const squawk_table: [4096]u16 = blk: { @setEvalBranchQuota(200_000); var table: [4096]u16 = @splat(0); for (0..4096) |index| { const mode_a = fromIndex(index); const altitude = gilhamToAltitude(mode_a) orelse continue; const slot = altitude + altitude_bias; // The Gilham code is one-to-one over the legal patterns, so nothing // should ever land on an occupied slot. if (table[slot] != 0) @compileError("Gilham altitude codes collided"); table[slot] = mode_a; } break :blk table; }; /// Decode the Gilham altitude code out of a reply, or null if the pattern is /// not a legal altitude. fn gilhamToAltitude(mode_a: u16) ?Hundreds { // D1 is never used for altitude, so a reply with it set is a squawk, and // the C pulses cannot all be zero. The four spare bits are rejected too, // though a reply that came through `toIndex` has none of them set. if (mode_a & 0x8889 != 0) return null; if (mode_a & 0x00f0 == 0) return null; // Hundreds of feet, from the C pulses. var hundreds: u16 = 0; if (mode_a & 0x0010 != 0) hundreds ^= 0x007; // C1 if (mode_a & 0x0020 != 0) hundreds ^= 0x003; // C2 if (mode_a & 0x0040 != 0) hundreds ^= 0x001; // C4 // The code counts 1,2,3,4,5 but writes 5 and 7 for 7 and 5; swap them // back, and reject the two patterns that are not a digit at all. if (hundreds & 5 == 5) hundreds ^= 2; if (hundreds > 5) return null; // Five hundreds of feet, from the D, A and B pulses in reflected binary. var five_hundreds: u16 = 0; if (mode_a & 0x0002 != 0) five_hundreds ^= 0x0ff; // D2 if (mode_a & 0x0004 != 0) five_hundreds ^= 0x07f; // D4 if (mode_a & 0x1000 != 0) five_hundreds ^= 0x03f; // A1 if (mode_a & 0x2000 != 0) five_hundreds ^= 0x01f; // A2 if (mode_a & 0x4000 != 0) five_hundreds ^= 0x00f; // A4 if (mode_a & 0x0100 != 0) five_hundreds ^= 0x007; // B1 if (mode_a & 0x0200 != 0) five_hundreds ^= 0x003; // B2 if (mode_a & 0x0400 != 0) five_hundreds ^= 0x001; // B4 // Every other five-hundred band counts its hundreds downwards, which is // what makes the whole code single-bit-error-tolerant in ground radar. if (five_hundreds & 1 != 0) hundreds = 6 - hundreds; return @intCast(@as(i32, five_hundreds) * 5 + hundreds - altitude_bias); } const testing = std.testing; test "the index packing round-trips" { for (0..4096) |index| { const mode_a = fromIndex(@intCast(index)); // Nothing outside the twelve meaningful bits is ever set. try testing.expectEqual(@as(u16, 0), mode_a & 0x8888); try testing.expectEqual(@as(u12, @intCast(index)), toIndex(mode_a)); } } test "the index packing ignores the spare bits" { // The ident bit, 0x0080, is not part of the squawk. try testing.expectEqual(toIndex(0x7700), toIndex(0x7780)); } test "squawks print as hex digits" { // The three squawks everybody knows: hijack, radio failure, emergency. try testing.expectEqual(@as(u12, 0o7500), @as(u12, toIndex(0x7500))); try testing.expectEqual(@as(u16, 0x7600), fromIndex(0o7600)); try testing.expectEqual(@as(u16, 0x7700), fromIndex(0o7700)); } test "known altitude codes" { // C1 alone: the C pulses read 7, which the 5/7 swap turns into 5, in the // lowest five-hundred band. 500 * 0 + 500 - 1300 = -800 feet. try testing.expectEqual(@as(?Hundreds, -8), toAltitude(0x0010)); // D1 set is never an altitude. try testing.expectEqual(@as(?Hundreds, null), toAltitude(0x0011)); // No C pulses at all is never an altitude. try testing.expectEqual(@as(?Hundreds, null), toAltitude(0x7000)); // The spare bit of a nibble is not part of the code, so setting it // changes nothing: `toAltitude` indexes by the twelve pulses alone. try testing.expectEqual(toAltitude(0x0010), toAltitude(0x8010)); } test "altitude and squawk are inverse over the legal codes" { var legal: usize = 0; for (0..4096) |index| { const mode_a = fromIndex(@intCast(index)); const altitude = toAltitude(mode_a) orelse continue; legal += 1; try testing.expectEqual(@as(?u16, mode_a), fromAltitude(altitude)); } // Of the 4096 patterns, the legal altitudes are those with D1 clear, at // least one C pulse, and a C code that is a digit: 1280 of them. try testing.expectEqual(@as(usize, 1280), legal); } test "the altitudes that exist are one contiguous run" { // Every hundred-foot step from the bottom of the range to the top has a // code, which is what lets the tracker ask whether a squawk could have // been a misread altitude one step away. var lowest: ?Hundreds = null; var highest: ?Hundreds = null; for (altitude_table) |entry| { const altitude = entry orelse continue; if (lowest == null or altitude < lowest.?) lowest = altitude; if (highest == null or altitude > highest.?) highest = altitude; } try testing.expectEqual(@as(?Hundreds, -12), lowest); try testing.expectEqual(@as(?Hundreds, 1267), highest); var altitude = lowest.?; while (altitude <= highest.?) : (altitude += 1) { try testing.expect(fromAltitude(altitude) != null); } try testing.expectEqual(@as(?u16, null), fromAltitude(lowest.? - 1)); try testing.expectEqual(@as(?u16, null), fromAltitude(highest.? + 1)); }