1module Accelerator.SM86.FieldEncoding
2
3import Accelerator.SM86.Control
4import Accelerator.SM86.ControlEncoding
5import Accelerator.SM86.Instruction
6import Accelerator.SM86.Immediate
7import Accelerator.SM86.Types
8import Std.Natural
9import Std.Byte
10
11family SM86EncodingErrorCode : Type 0
12constructor SM86EncodingWordLengthInvalid
13constructor SM86EncodingFieldWidthInvalid
14constructor SM86EncodingFieldExtentInvalid
15constructor SM86EncodingFieldValueOverflow
16constructor SM86EncodingControlInvalid
17
18end-family
19
20family SM86EncodedField : Type 0
21constructor SM86EncodedFieldValue
22field unrestricted sm86EncodedFieldPosition : Nat
23field unrestricted sm86EncodedFieldWidth : Nat
24field unrestricted sm86EncodedFieldValue : Nat
25constructor SM86EncodedFieldWord32Value
26field unrestricted sm86EncodedFieldWord32Position : Nat
27field unrestricted sm86EncodedFieldWord32Value : SM86Unsigned32
28constructor SM86EncodedFieldWord24Value
29field unrestricted sm86EncodedFieldWord24Position : Nat
30field unrestricted sm86EncodedFieldWord24Byte0 : Byte
31field unrestricted sm86EncodedFieldWord24Byte1 : Byte
32field unrestricted sm86EncodedFieldWord24Byte2 : Byte
33
34end-family
35
36family SM86EncodedFieldList : Type 0
37constructor SM86EncodedFieldListEmpty
38constructor SM86EncodedFieldListCons
39field unrestricted sm86EncodedFieldListHead : SM86EncodedField
40recursive unrestricted sm86EncodedFieldListTail
41
42end-family
43
44family SM86FieldEncodingTelemetry : Type 0
45constructor SM86FieldEncodingTelemetryValue
46field unrestricted sm86FieldTelemetryFieldsEncoded : Nat
47field unrestricted sm86FieldTelemetryBitsWritten : Nat
48field unrestricted sm86FieldTelemetryOutputBytes : Nat
49field unrestricted sm86FieldTelemetryHighestExclusiveBit : Nat
50
51end-family
52
53family SM86FieldEncodingResult : Type 0
54constructor SM86FieldEncodingSucceeded
55field unrestricted sm86FieldEncodedBytes : Bytes
56field unrestricted sm86FieldEncodingTelemetry : SM86FieldEncodingTelemetry
57constructor SM86FieldEncodingFailed
58field unrestricted sm86FieldEncodingError : SM86EncodingErrorCode
59field unrestricted sm86FieldEncodingPosition : Nat
60field unrestricted sm86FieldEncodingWidth : Nat
61field unrestricted sm86FieldEncodingDetail : Nat
62field unrestricted sm86FieldFailureTelemetry : SM86FieldEncodingTelemetry
63
64end-family
65
66family SM86HeaderGuard : Type 0
67constructor SM86HeaderGuardValue
68field unrestricted sm86HeaderGuardPredicate : Nat
69field unrestricted sm86HeaderGuardNegated : Nat
70
71end-family
72
73def sm86EncodingNaturalTwo =
74 2
75
76def sm86EncodingNaturalThree =
77 3
78
79def sm86EncodingNaturalEight =
80 8
81
82def sm86EncodingNaturalTwelve =
83 12
84
85def sm86EncodingNaturalFifteen =
86 15
87
88def sm86EncodingNaturalSixteen =
89 16
90
91def sm86EncodingNaturalTwentyFour =
92 24
93
94def sm86EncodingNaturalThirtyTwo =
95 32
96
97def sm86EncodingNaturalTwentyThree =
98 23
99
100def sm86EncodingNaturalOneHundredFive =
101 105
102
103def sm86EncodingNaturalOneHundredTwentyEight =
104 128
105
106def sm86EncodingNaturalSixtyFiveThousandFiveHundredThirtySix =
107 (naturalMultiply byteNaturalTwoHundredFiftySix byteNaturalTwoHundredFiftySix)
108
109def sm86EmptyInstructionBytes =
110 (bytes 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
111
112def sm86EncodingErrorOrdinal =
113 (lambda unrestricted error : SM86EncodingErrorCode .
114 eliminate
115 SM86EncodingErrorCode
116 (lambda unrestricted current : SM86EncodingErrorCode . Nat)
117 error
118 (branch SM86EncodingWordLengthInvalid . 1)
119 (branch SM86EncodingFieldWidthInvalid . 2)
120 (branch SM86EncodingFieldExtentInvalid . 3)
121 (branch SM86EncodingFieldValueOverflow . 4)
122 (branch SM86EncodingControlInvalid . 5))
123
124def sm86EncodingErrorStableCode =
125 (lambda unrestricted error : SM86EncodingErrorCode .
126 eliminate
127 SM86EncodingErrorCode
128 (lambda unrestricted current : SM86EncodingErrorCode . Bytes)
129 error
130 (branch SM86EncodingWordLengthInvalid . b"ALPHA-SM86-ENC-001")
131 (branch SM86EncodingFieldWidthInvalid . b"ALPHA-SM86-ENC-002")
132 (branch SM86EncodingFieldExtentInvalid . b"ALPHA-SM86-ENC-003")
133 (branch SM86EncodingFieldValueOverflow . b"ALPHA-SM86-ENC-004")
134 (branch SM86EncodingControlInvalid . b"ALPHA-SM86-ENC-005"))
135
136def sm86FieldTelemetryZero =
137 (constructor
138 SM86FieldEncodingTelemetry
139 SM86FieldEncodingTelemetryValue
140 0
141 0
142 sm86EncodingNaturalSixteen
143 0)
144
145def sm86FieldTelemetryFailure =
146 (lambda unrestricted outputBytes : Nat . unrestricted highestExclusiveBit : Nat .
147 constructor
148 SM86FieldEncodingTelemetry
149 SM86FieldEncodingTelemetryValue
150 0
151 0
152 outputBytes
153 highestExclusiveBit)
154
155def sm86FieldTelemetryOne =
156 (lambda unrestricted width : Nat . unrestricted highestExclusiveBit : Nat .
157 constructor
158 SM86FieldEncodingTelemetry
159 SM86FieldEncodingTelemetryValue
160 1
161 width
162 sm86EncodingNaturalSixteen
163 highestExclusiveBit)
164
165def sm86NaturalMaximum =
166 (lambda unrestricted left : Nat . unrestricted right : Nat .
167 naturalSelect (naturalLessOrEqual left right) right left)
168
169def sm86MergeFieldTelemetry =
170 (lambda unrestricted left : SM86FieldEncodingTelemetry .
171 unrestricted right : SM86FieldEncodingTelemetry .
172 eliminate
173 SM86FieldEncodingTelemetry
174 (lambda unrestricted current : SM86FieldEncodingTelemetry . SM86FieldEncodingTelemetry)
175 left
176 (branch
177 SM86FieldEncodingTelemetryValue
178 leftFields
179 leftBits
180 leftBytes
181 leftHighest
182 .
183 eliminate
184 SM86FieldEncodingTelemetry
185 (lambda unrestricted current : SM86FieldEncodingTelemetry . SM86FieldEncodingTelemetry)
186 right
187 (branch
188 SM86FieldEncodingTelemetryValue
189 rightFields
190 rightBits
191 rightBytes
192 rightHighest
193 .
194 constructor
195 SM86FieldEncodingTelemetry
196 SM86FieldEncodingTelemetryValue
197 (naturalAdd leftFields rightFields)
198 (naturalAdd leftBits rightBits)
199 rightBytes
200 (sm86NaturalMaximum leftHighest rightHighest))))
201
202def sm86ByteAt =
203 (lambda unrestricted index : Nat .
204 nat-eliminate
205 (lambda unrestricted current : Nat . pi unrestricted input : Bytes . Byte)
206 (lambda unrestricted input : Bytes . bytes-head input)
207 (lambda unrestricted predecessor : Nat .
208 unrestricted induction : (pi unrestricted input : Bytes . Byte) .
209 unrestricted input : Bytes .
210 induction (bytes-tail input))
211 index)
212
213def sm86ReplaceByteAt =
214 (lambda unrestricted index : Nat . unrestricted replacement : Byte .
215 nat-eliminate
216 (lambda unrestricted current : Nat . pi unrestricted input : Bytes . Bytes)
217 (lambda unrestricted input : Bytes . bytes-cons replacement (bytes-tail input))
218 (lambda unrestricted predecessor : Nat .
219 unrestricted induction : (pi unrestricted input : Bytes . Bytes) .
220 unrestricted input : Bytes .
221 bytes-cons (bytes-head input) (induction (bytes-tail input)))
222 index)
223
224-- Place ONE bit, given the bit's TARGET position and the bit itself.
225--
226-- This used to take the field's value and a bit index and recover the bit with
227-- `value / 2^bitIndex mod 2`. That `2^bitIndex` is the reason the encoder could
228-- not be run: naturals are unary here, so `naturalPowerOfTwo 31` -- what a
229-- field at bit 31 needs -- materialises a successor chain of 2,147,483,648
230-- elements, and the divide beside it is repeated subtraction over that numeral.
231-- Measured, 2^22 alone took 921 ms; one instruction is 128 such placements.
232--
233-- The caller now threads the value, halving it per bit, so the only power left
234-- here is `2^(targetBit mod 8)` -- at most 128, because a bit's home inside a
235-- byte is what it is regardless of how wide the field is.
236def sm86PlaceBitAt =
237 (lambda unrestricted targetBit : Nat .
238 unrestricted sourceBit : Nat .
239 unrestricted encoded : Bytes .
240 app
241 (lambda unrestricted targetByteIndex : Nat .
242 app
243 (lambda unrestricted targetBitIndex : Nat .
244 app
245 (lambda unrestricted targetPower : Nat .
246 app
247 (lambda unrestricted priorByte : Nat .
248 app
249 (lambda unrestricted priorBit : Nat .
250 sm86ReplaceByteAt
251 targetByteIndex
252 (pipe priorByte
253 (naturalSaturatingSubtract (naturalMultiply priorBit targetPower))
254 (naturalAdd (naturalMultiply sourceBit targetPower))
255 nat-to-byte)
256 encoded)
257 (naturalModuloUnchecked
258 (naturalDivideUnchecked priorByte targetPower)
259 sm86EncodingNaturalTwo))
260 (byte-to-nat (sm86ByteAt targetByteIndex encoded)))
261 (naturalPowerOfTwo targetBitIndex))
262 (naturalModuloUnchecked targetBit sm86EncodingNaturalEight))
263 (naturalDivideUnchecked targetBit sm86EncodingNaturalEight))
264
265-- Place a field's bits, threading the POSITION and the remaining VALUE instead
266-- of recomputing a power per bit.
267--
268-- The motive is a `pi` over both, because the recursion changes both: each turn
269-- writes the low bit of the value at the current position, then hands the
270-- induction the next position and the value halved. The bits are written in
271-- ascending order into disjoint positions, so the order is not observable --
272-- what changed is that no numeral larger than a byte is ever built.
273def sm86PlaceFieldBitsUnchecked =
274 (lambda unrestricted position : Nat .
275 unrestricted width : Nat .
276 unrestricted value : Nat .
277 unrestricted encoded : Bytes .
278 app
279 (app
280 (app
281 (nat-eliminate
282 (lambda unrestricted current : Nat .
283 pi unrestricted currentPosition : Nat .
284 unrestricted currentValue : Nat .
285 unrestricted currentBytes : Bytes .
286 Bytes)
287 (lambda unrestricted currentPosition : Nat .
288 unrestricted currentValue : Nat .
289 unrestricted currentBytes : Bytes .
290 currentBytes)
291 (lambda unrestricted predecessor : Nat .
292 unrestricted induction : (pi unrestricted currentPosition : Nat . unrestricted currentValue : Nat . unrestricted currentBytes : Bytes . Bytes) .
293 unrestricted currentPosition : Nat .
294 unrestricted currentValue : Nat .
295 unrestricted currentBytes : Bytes .
296 eliminate
297 NaturalDivisionState
298 (lambda unrestricted current : NaturalDivisionState . Bytes)
299 (naturalDivisionState currentValue sm86EncodingNaturalTwo)
300 (branch
301 NaturalDivisionStateValue
302 remainder
303 quotient
304 .
305 induction
306 (succ currentPosition)
307 quotient
308 (sm86PlaceBitAt currentPosition remainder currentBytes)))
309 width)
310 position)
311 value)
312 encoded)
313
314def sm86FieldFailure =
315 (lambda unrestricted error : SM86EncodingErrorCode .
316 unrestricted position : Nat .
317 unrestricted width : Nat .
318 unrestricted detail : Nat .
319 unrestricted outputBytes : Nat .
320 constructor
321 SM86FieldEncodingResult
322 SM86FieldEncodingFailed
323 error
324 position
325 width
326 detail
327 (sm86FieldTelemetryFailure outputBytes (naturalAdd position width)))
328
329def sm86FieldWord32InRange : (pi unrestricted value : Nat . Nat) =
330 (lambda unrestricted value : Nat .
331 nat-less-than
332 (pipe value
333 (naturalDivideUnchecked byteNaturalTwoHundredFiftySix)
334 (naturalDivideUnchecked byteNaturalTwoHundredFiftySix)
335 (naturalDivideUnchecked byteNaturalTwoHundredFiftySix)
336 (naturalDivideUnchecked byteNaturalTwoHundredFiftySix))
337 1)
338
339-- Only width32 uses the already-qualified base256 range identity.
340-- Select functions so the unused old power expression is not evaluated.
341-- Quotient chunks avoid constructing a power larger than one byte.
342-- For width = 8*q+r, floor(value / 256^q) < 2^r exactly means value < 2^width.
343def sm86FieldDivideByteChunks =
344 (lambda unrestricted count : Nat . unrestricted value : Nat .
345 app
346 (nat-eliminate
347 (lambda unrestricted current : Nat . pi unrestricted remainingValue : Nat . Nat)
348 (lambda unrestricted remainingValue : Nat . remainingValue)
349 (lambda unrestricted predecessor : Nat .
350 unrestricted induction : (pi unrestricted remainingValue : Nat . Nat) .
351 unrestricted remainingValue : Nat .
352 induction (naturalDivideUnchecked remainingValue byteNaturalTwoHundredFiftySix))
353 count)
354 value)
355
356def sm86FieldValueFitsByByteChunks =
357 (lambda unrestricted width : Nat . unrestricted value : Nat .
358 eliminate
359 NaturalDivisionState
360 (lambda unrestricted current : NaturalDivisionState . Nat)
361 (naturalDivisionState width sm86EncodingNaturalEight)
362 (branch
363 NaturalDivisionStateValue
364 remainder
365 quotient
366 .
367 nat-less-than (sm86FieldDivideByteChunks quotient value) (naturalPowerOfTwo remainder)))
368
369def sm86FieldValueFits =
370 (lambda unrestricted width : Nat . unrestricted value : Nat .
371 app
372 (nat-eliminate
373 (lambda unrestricted current : Nat . pi unrestricted ignored : Nat . Nat)
374 (lambda unrestricted ignored : Nat . sm86FieldValueFitsByByteChunks width value)
375 (lambda unrestricted predecessor : Nat .
376 unrestricted induction : (pi unrestricted ignored : Nat . Nat) .
377 unrestricted ignored : Nat .
378 sm86FieldWord32InRange value)
379 (naturalEqual width 32))
380 0)
381
382-- A fixed32 field keeps its already bounded four-byte representation.
383-- Each reused bit loop sees one byte only; target positions may be unaligned.
384def sm86PlaceWord32BitsUnchecked =
385 (lambda unrestricted position : Nat .
386 unrestricted value : SM86Unsigned32 .
387 unrestricted encoded : Bytes .
388 eliminate
389 SM86Unsigned32
390 (lambda unrestricted current : SM86Unsigned32 . Bytes)
391 value
392 (branch
393 SM86Unsigned32Value
394 byte0
395 byte1
396 byte2
397 byte3
398 .
399 then (sm86PlaceFieldBitsUnchecked (naturalAdd position 24) sm86EncodingNaturalEight (byte-to-nat byte3))
400 (sm86PlaceFieldBitsUnchecked
401 (naturalAdd position sm86EncodingNaturalSixteen)
402 sm86EncodingNaturalEight
403 (byte-to-nat byte2))
404 (sm86PlaceFieldBitsUnchecked
405 (naturalAdd position sm86EncodingNaturalEight)
406 sm86EncodingNaturalEight
407 (byte-to-nat byte1))
408 (sm86PlaceFieldBitsUnchecked position sm86EncodingNaturalEight (byte-to-nat byte0))
409 encoded))
410
411-- The fixed32 constructor makes width/value overflow unrepresentable.
412-- Keep the old word-length-before-extent error order and one-field telemetry.
413-- Select thunks so malformed output never invokes the unchecked placement.
414def sm86PlaceWord32FieldWithValidWord =
415 (lambda unrestricted position : Nat .
416 unrestricted value : SM86Unsigned32 .
417 unrestricted encoded : Bytes .
418 app
419 (lambda unrestricted extent : Nat .
420 app
421 (nat-eliminate
422 (lambda unrestricted current : Nat .
423 pi unrestricted ignored : Nat . SM86FieldEncodingResult)
424 (lambda unrestricted ignored : Nat .
425 sm86FieldFailure
426 SM86EncodingFieldExtentInvalid
427 position
428 sm86EncodingNaturalThirtyTwo
429 extent
430 (bytes-length encoded))
431 (lambda unrestricted predecessor : Nat .
432 unrestricted induction : (pi unrestricted ignored : Nat . SM86FieldEncodingResult) .
433 unrestricted ignored : Nat .
434 constructor
435 SM86FieldEncodingResult
436 SM86FieldEncodingSucceeded
437 (sm86PlaceWord32BitsUnchecked position value encoded)
438 (sm86FieldTelemetryOne sm86EncodingNaturalThirtyTwo extent))
439 (naturalLessOrEqual extent sm86EncodingNaturalOneHundredTwentyEight))
440 0)
441 (naturalAdd position sm86EncodingNaturalThirtyTwo))
442
443def sm86PlaceWord32Field =
444 (lambda unrestricted position : Nat .
445 unrestricted value : SM86Unsigned32 .
446 unrestricted encoded : Bytes .
447 app
448 (nat-eliminate
449 (lambda unrestricted current : Nat .
450 pi unrestricted ignored : Nat . SM86FieldEncodingResult)
451 (lambda unrestricted ignored : Nat .
452 sm86FieldFailure
453 SM86EncodingWordLengthInvalid
454 position
455 sm86EncodingNaturalThirtyTwo
456 (bytes-length encoded)
457 (bytes-length encoded))
458 (lambda unrestricted predecessor : Nat .
459 unrestricted induction : (pi unrestricted ignored : Nat . SM86FieldEncodingResult) .
460 unrestricted ignored : Nat .
461 sm86PlaceWord32FieldWithValidWord position value encoded)
462 (naturalEqual (bytes-length encoded) sm86EncodingNaturalSixteen))
463 0)
464
465-- The fixed24 payload is exactly three bytes. Reuse the qualified low-to-high
466-- bit writer; preserve neighboring bits and one-field24-bit telemetry.
467def sm86PlaceWord24BitsUnchecked =
468 (lambda unrestricted position : Nat .
469 unrestricted byte0 : Byte .
470 unrestricted byte1 : Byte .
471 unrestricted byte2 : Byte .
472 unrestricted encoded : Bytes .
473 sm86PlaceFieldBitsUnchecked
474 (naturalAdd position 16)
475 8
476 (byte-to-nat byte2)
477 (sm86PlaceFieldBitsUnchecked
478 (naturalAdd position 8)
479 8
480 (byte-to-nat byte1)
481 (sm86PlaceFieldBitsUnchecked position 8 (byte-to-nat byte0) encoded)))
482
483def sm86PlaceWord24FieldWithValidWord =
484 (lambda unrestricted position : Nat .
485 unrestricted byte0 : Byte .
486 unrestricted byte1 : Byte .
487 unrestricted byte2 : Byte .
488 unrestricted encoded : Bytes .
489 app
490 (lambda unrestricted extent : Nat .
491 app
492 (nat-eliminate
493 (lambda unrestricted condition : Nat .
494 pi unrestricted ignored : Nat . SM86FieldEncodingResult)
495 (lambda unrestricted ignored : Nat .
496 sm86FieldFailure
497 SM86EncodingFieldExtentInvalid
498 position
499 sm86EncodingNaturalTwentyFour
500 extent
501 (bytes-length encoded))
502 (lambda unrestricted predecessor : Nat .
503 unrestricted induction : (pi unrestricted ignored : Nat . SM86FieldEncodingResult) .
504 unrestricted ignored : Nat .
505 constructor
506 SM86FieldEncodingResult
507 SM86FieldEncodingSucceeded
508 (sm86PlaceWord24BitsUnchecked position byte0 byte1 byte2 encoded)
509 (sm86FieldTelemetryOne sm86EncodingNaturalTwentyFour extent))
510 (naturalLessOrEqual extent sm86EncodingNaturalOneHundredTwentyEight))
511 0)
512 (naturalAdd position sm86EncodingNaturalTwentyFour))
513
514def sm86PlaceWord24Field =
515 (lambda unrestricted position : Nat .
516 unrestricted byte0 : Byte .
517 unrestricted byte1 : Byte .
518 unrestricted byte2 : Byte .
519 unrestricted encoded : Bytes .
520 app
521 (nat-eliminate
522 (lambda unrestricted condition : Nat .
523 pi unrestricted ignored : Nat . SM86FieldEncodingResult)
524 (lambda unrestricted ignored : Nat .
525 sm86FieldFailure
526 SM86EncodingWordLengthInvalid
527 position
528 sm86EncodingNaturalTwentyFour
529 (bytes-length encoded)
530 (bytes-length encoded))
531 (lambda unrestricted predecessor : Nat .
532 unrestricted induction : (pi unrestricted ignored : Nat . SM86FieldEncodingResult) .
533 unrestricted ignored : Nat .
534 sm86PlaceWord24FieldWithValidWord position byte0 byte1 byte2 encoded)
535 (naturalEqual (bytes-length encoded) sm86EncodingNaturalSixteen))
536 0)
537
538def sm86PlaceEncodedField =
539 (lambda unrestricted field : SM86EncodedField . unrestricted encoded : Bytes .
540 eliminate
541 SM86EncodedField
542 (lambda unrestricted current : SM86EncodedField . SM86FieldEncodingResult)
543 field
544 (branch
545 SM86EncodedFieldValue
546 position
547 width
548 value
549 .
550 app
551 (lambda unrestricted extent : Nat .
552 nat-eliminate
553 (lambda unrestricted wordLengthValid : Nat . SM86FieldEncodingResult)
554 (sm86FieldFailure
555 SM86EncodingWordLengthInvalid
556 position
557 width
558 (bytes-length encoded)
559 (bytes-length encoded))
560 (lambda unrestricted lengthPredecessor : Nat .
561 unrestricted lengthInduction : SM86FieldEncodingResult .
562 nat-eliminate
563 (lambda unrestricted widthValid : Nat . SM86FieldEncodingResult)
564 (sm86FieldFailure
565 SM86EncodingFieldWidthInvalid
566 position
567 width
568 width
569 (bytes-length encoded))
570 (lambda unrestricted widthPredecessor : Nat .
571 unrestricted widthInduction : SM86FieldEncodingResult .
572 nat-eliminate
573 (lambda unrestricted extentValid : Nat . SM86FieldEncodingResult)
574 (sm86FieldFailure
575 SM86EncodingFieldExtentInvalid
576 position
577 width
578 extent
579 (bytes-length encoded))
580 (lambda unrestricted extentPredecessor : Nat .
581 unrestricted extentInduction : SM86FieldEncodingResult .
582 nat-eliminate
583 (lambda unrestricted valueValid : Nat . SM86FieldEncodingResult)
584 (sm86FieldFailure
585 SM86EncodingFieldValueOverflow
586 position
587 width
588 value
589 (bytes-length encoded))
590 (lambda unrestricted valuePredecessor : Nat .
591 unrestricted valueInduction : SM86FieldEncodingResult .
592 constructor
593 SM86FieldEncodingResult
594 SM86FieldEncodingSucceeded
595 (sm86PlaceFieldBitsUnchecked position width value encoded)
596 (sm86FieldTelemetryOne width extent))
597 (sm86FieldValueFits width value))
598 (naturalLessOrEqual extent sm86EncodingNaturalOneHundredTwentyEight))
599 (nat-less-than 0 width))
600 (naturalEqual (bytes-length encoded) sm86EncodingNaturalSixteen))
601 (naturalAdd position width))
602 (branch
603 SM86EncodedFieldWord32Value
604 position
605 value
606 .
607 sm86PlaceWord32Field position value encoded)
608 (branch
609 SM86EncodedFieldWord24Value
610 position
611 byte0
612 byte1
613 byte2
614 .
615 sm86PlaceWord24Field position byte0 byte1 byte2 encoded))
616
617def sm86EncodeFieldListFrom =
618 (lambda unrestricted fields : SM86EncodedFieldList .
619 eliminate
620 SM86EncodedFieldList
621 (lambda unrestricted current : SM86EncodedFieldList .
622 pi unrestricted encoded : Bytes . SM86FieldEncodingResult)
623 fields
624 (branch
625 SM86EncodedFieldListEmpty
626 .
627 lambda unrestricted encoded : Bytes .
628 constructor
629 SM86FieldEncodingResult
630 SM86FieldEncodingSucceeded
631 encoded
632 sm86FieldTelemetryZero)
633 (branch
634 SM86EncodedFieldListCons
635 field
636 tail
637 ih_tail
638 .
639 lambda unrestricted encoded : Bytes .
640 eliminate
641 SM86FieldEncodingResult
642 (lambda unrestricted current : SM86FieldEncodingResult . SM86FieldEncodingResult)
643 (sm86PlaceEncodedField field encoded)
644 (branch
645 SM86FieldEncodingSucceeded
646 placed
647 fieldTelemetry
648 .
649 eliminate
650 SM86FieldEncodingResult
651 (lambda unrestricted current : SM86FieldEncodingResult . SM86FieldEncodingResult)
652 (ih_tail placed)
653 (branch
654 SM86FieldEncodingSucceeded
655 complete
656 tailTelemetry
657 .
658 constructor
659 SM86FieldEncodingResult
660 SM86FieldEncodingSucceeded
661 complete
662 (sm86MergeFieldTelemetry fieldTelemetry tailTelemetry))
663 (branch
664 SM86FieldEncodingFailed
665 error
666 position
667 width
668 detail
669 tailTelemetry
670 .
671 constructor
672 SM86FieldEncodingResult
673 SM86FieldEncodingFailed
674 error
675 position
676 width
677 detail
678 (sm86MergeFieldTelemetry fieldTelemetry tailTelemetry)))
679 (branch
680 SM86FieldEncodingFailed
681 error
682 position
683 width
684 detail
685 telemetry
686 .
687 constructor
688 SM86FieldEncodingResult
689 SM86FieldEncodingFailed
690 error
691 position
692 width
693 detail
694 telemetry)))
695
696def sm86EncodeFields =
697 (lambda unrestricted fields : SM86EncodedFieldList .
698 sm86EncodeFieldListFrom fields sm86EmptyInstructionBytes)
699
700def sm86HeaderGuard =
701 (lambda unrestricted guard : SM86InstructionGuard .
702 eliminate
703 SM86InstructionGuard
704 (lambda unrestricted current : SM86InstructionGuard . SM86HeaderGuard)
705 guard
706 (branch SM86InstructionAlways . constructor SM86HeaderGuard SM86HeaderGuardValue 7 0)
707 (branch
708 SM86InstructionWhen
709 predicate
710 .
711 constructor
712 SM86HeaderGuard
713 SM86HeaderGuardValue
714 (byte-to-nat (sm86PredicateNumber predicate))
715 0)
716 (branch
717 SM86InstructionWhenNot
718 predicate
719 .
720 constructor
721 SM86HeaderGuard
722 SM86HeaderGuardValue
723 (byte-to-nat (sm86PredicateNumber predicate))
724 1))
725
726def sm86ControlEncodingNatural =
727 (lambda unrestricted encoded : Bytes .
728 nest naturalAdd
729 (byte-to-nat (sm86ByteAt 0 encoded))
730 (pipe 1 (sm86ByteAt encoded) byte-to-nat (naturalMultiply byteNaturalTwoHundredFiftySix))
731 (pipe 2
732 (sm86ByteAt encoded)
733 byte-to-nat
734 (naturalMultiply sm86EncodingNaturalSixtyFiveThousandFiveHundredThirtySix)))
735
736def sm86HeaderFields =
737 (lambda unrestricted opcode : Nat .
738 unrestricted guard : SM86HeaderGuard .
739 unrestricted controlNatural : Nat .
740 eliminate
741 SM86HeaderGuard
742 (lambda unrestricted current : SM86HeaderGuard . SM86EncodedFieldList)
743 guard
744 (branch
745 SM86HeaderGuardValue
746 predicate
747 negated
748 .
749 nest (constructor SM86EncodedFieldList SM86EncodedFieldListCons)
750 (constructor SM86EncodedField SM86EncodedFieldValue 0 sm86EncodingNaturalTwelve opcode)
751 (constructor
752 SM86EncodedField
753 SM86EncodedFieldValue
754 sm86EncodingNaturalTwelve
755 sm86EncodingNaturalThree
756 predicate)
757 (constructor SM86EncodedField SM86EncodedFieldValue sm86EncodingNaturalFifteen 1 negated)
758 (constructor
759 SM86EncodedField
760 SM86EncodedFieldValue
761 sm86EncodingNaturalOneHundredFive
762 sm86EncodingNaturalTwentyThree
763 controlNatural)
764 SM86EncodedFieldListEmpty))
765
766def sm86EncodeInstructionHeader =
767 (lambda unrestricted opcode : Nat .
768 unrestricted guard : SM86InstructionGuard .
769 unrestricted control : SM86Control .
770 eliminate
771 SM86ControlEncodingResult
772 (lambda unrestricted current : SM86ControlEncodingResult . SM86FieldEncodingResult)
773 (encodeSM86ControlLE control)
774 (branch
775 SM86ControlEncoded
776 controlBytes
777 .
778 sm86EncodeFields
779 (sm86HeaderFields
780 opcode
781 (sm86HeaderGuard guard)
782 (sm86ControlEncodingNatural controlBytes)))
783 (branch
784 SM86ControlEncodingFailed
785 code
786 .
787 constructor
788 SM86FieldEncodingResult
789 SM86FieldEncodingFailed
790 SM86EncodingControlInvalid
791 sm86EncodingNaturalOneHundredFive
792 sm86EncodingNaturalTwentyThree
793 code
794 sm86FieldTelemetryZero))The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.