compiler/tests/fixtures/runtime/library-shared-line/main.ldn
skip to the library navigation
1import core/line
2
3-- A peripheral family as line.divisor takes it: its clock, oversampling,
4-- fraction bits and register range.
5family: type = struct
6 clock: u32
7 oversampling: u32
8 fraction_bits: u32
9 least: u32
10 most: u32
11end family
12
13-- The derived driver's PL011 at 48 MHz and the Picos' at 12 MHz, the
14-- STM32H753's USART3 at 64 MHz oversampling by 16 and by 8, and the ESP
15-- UARTs: the ESP32's 20-bit CLKDIV at 40 MHz, and the 12-bit one of the
16-- S3 at 40 MHz, the C5 at 48 MHz and the C3 at 20 MHz.
17families: [8]family = [
18 (clock: 48000000, oversampling: 16, fraction_bits: 6, least: 64, most: 4194240),
19 (clock: 12000000, oversampling: 16, fraction_bits: 6, least: 64, most: 4194240),
20 (clock: 64000000, oversampling: 16, fraction_bits: 4, least: 16, most: 65535),
21 (clock: 64000000, oversampling: 8, fraction_bits: 4, least: 16, most: 65535),
22 (clock: 40000000, oversampling: 1, fraction_bits: 4, least: 256, most: 16777215),
23 (clock: 40000000, oversampling: 1, fraction_bits: 4, least: 256, most: 65535),
24 (clock: 48000000, oversampling: 1, fraction_bits: 4, least: 256, most: 65535),
25 (clock: 20000000, oversampling: 1, fraction_bits: 4, least: 256, most: 65535)]
26
27-- The divisor for RATE, or zero where it is refused.
28divisor: (f: family, rate: u32) -> (value: u32) =
29 value = line.divisor(f.clock, rate, f.oversampling, f.fraction_bits,
30 f.least, f.most) else (problem)
31 _ = problem
32 0
33 end
34end divisor
35
36-- Whether VALUE, as a divisor of F for RATE, gives a rate that misses
37-- RATE by no more than `line.tolerance` parts per thousand.
38within: (f: family, rate: u32, value: u32) -> (yes: bool) =
39 scaled: u64 = u64(f.clock) << u64(f.fraction_bits)
40 given: u64 = u64(value) * u64(f.oversampling) * u64(rate)
41 missed: u64 = if given > scaled then given - scaled else scaled - given end if
42 yes = missed * 1000 <= u64(line.tolerance) * given
43end within
44
45-- Each family's divisor for every rate from 50 to 6,000,000, each the
46-- last plus a seventh and one, folded into one word as
47-- scripts/tests/test_line.py folds its own exact answers; every divisor
48-- given is within `line.tolerance`, or the fold is zero.
49sweep: () -> (fold: u32) =
50 fold = 0
51 for f in families do
52 mut rate: u32 = 50
53 while rate <= 6000000 do
54 value: u32 = divisor(f, rate)
55 if value <> 0 and not within(f, rate, value) then
56 fold = 0
57 return
58 end if
59 fold = fold *% 1000003 +% value
60 rate = rate + rate / 7 + 1
61 end while
62 end for
63end sweep
64
65-- Whether SETTINGS are refused with WANTED.
66refused: (settings: line.settings, wanted: line.unsupported_rate | line.unsupported_format)
67 -> (yes: bool) =
68 yes = false
69 line.check(settings) else (problem)
70 yes = problem == wanted
71 end
72end refused
73
74exercise: () -> (ok: bool) =
75 ok = false
76 -- 115,200 baud as every port writes it today: IBRD 26 and FBRD 3,
77 -- IBRD 6 and FBRD 33, BRR 556, and CLKDIV 347 and FRAG 4.
78 return when divisor(families[0], 115200) <> 26 * 64 + 3
79 or divisor(families[1], 115200) <> 6 * 64 + 33
80 or divisor(families[2], 115200) <> 556
81 or divisor(families[4], 115200) <> 347 * 16 + 4
82 -- Below the PL011's slowest rate and above its fastest; and 3.9
83 -- Mbaud at 64 MHz, whose BRR of 16 is 2.6 percent fast, past
84 -- `line.tolerance`, where 3.8 Mbaud's BRR of 17 is within it.
85 return when divisor(families[0], 45) <> 0
86 or divisor(families[0], 46) <> 4173913
87 or divisor(families[0], 3100000) <> 0
88 or divisor(families[2], 3900000) <> 0
89 or divisor(families[2], 3800000) <> 17
90 or divisor(families[5], 9600) <> 0
91 or within(families[2], 3900000, 16) or not within(families[2], 3800000, 17)
92 _ = line.divisor(48000000, 0, 16, 6, 64, 4194240) else (problem)
93 return when problem <> line.unsupported_rate
94 0
95 end
96 seven_even: line.settings = (rate: 9600, data_bits: 7, parity: line.even_parity,
97 stop_bits: line.two_stop_bits)
98 four: line.settings = (rate: 9600, data_bits: 4, parity: line.no_parity,
99 stop_bits: line.one_stop_bit)
100 stopped: line.settings = (rate: 0, data_bits: 8, parity: line.odd_parity,
101 stop_bits: line.one_stop_bit)
102 line.check(seven_even) else (problem)
103 _ = problem
104 return
105 end
106 return when not refused(four, line.unsupported_format)
107 or not refused(stopped, line.unsupported_rate)
108 return when line.frame_bits(line.default_settings) <> 10
109 or line.frame_bits(seven_even) <> 11
110 -- Seven data bits keep the low seven of a byte, which an STM32 USART
111 -- shares with the parity bit; eight keep all of them.
112 return when line.data_mask(seven_even) <> 0x7f
113 or line.data_mask(line.default_settings) <> 0xff
114 or line.data_mask(stopped) <> 0xff
115 ok = sweep() == 1195194680
116end exercise
117
118public main: () -> (code: i32) =
119 code = 1
120 if exercise() then
121 code = 42
122 end if
123end main