stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 281

Day 281 of 365 · Expert · Month 10: Bits and data

Several values in one number

Three readings in one number: plan fields, pack with ins, unpack with ext, shift negative values and save space in the stack and in logic memories.

Lesson 18 min Week 41

What you learn today

  • Pack values with ins
  • Unpack with ext
  • Save space in the stack

A logic memory holds exactly one number. If you want to pass temperature, pressure and oxygen of a room on to another chip, you need three memories – or you pack everything into one number. With ins and ext from bit week that is no witchcraft.

Planning fields

First the question: what range does each value have, and how many bits does it need?

ValueRangeBitsPosition
Temperature in °C0–1277bits 0–6
Pressure in kPa0–2558bits 7–14
Oxygen in %0–1007bits 15–21

n bits hold numbers from 0 to 2^n − 1. Together that is 22 bits – far below the limit of 53.

Packing with ins

Each value is rounded, limited to its range and then inserted. clamp makes sure an outlier does not spill into the neighbouring field:

sub value value CELSIUSOFFSET
round value value
clamp value value 0 127     # fits into 7 bits
ins packed value 0 7

Unpacking with ext

The receiver pulls the fields out again with the same numbers:

ext temp packed 0 7
ext press packed 7 8
ext o2 packed 15 7

Write the field table as a comment into both chips. If a field changes on one side only, the receiver reads nonsense without any error.

Shifting negative values

A field knows no negative numbers. On Mars it is well below 0 °C outside at night. So shift beforehand: °C + 50 turns −50 to 77 °C into the range 0 to 127. When unpacking you subtract the 50 again.

Saving space in the stack

The stack has 512 slots. A history with three values per reading lasts for 170 readings unpacked, for 512 packed – three times as long:

push packed                # one slot for three values
Warning

If the sensor is missing, NaN becomes 0 when inserted. The packed result would then look like “0 °C, 0 kPa, 0 %” – a valid but wrong reading. So on a failure write a number that cannot be a real reading, for example −1.

Diagram

The register bank of the chip r0 42 r1 31 r2 10 r3 -2.5 r4 0 r5 0 r6 0 r7 0 r8 0 r9 0 r10 0 r11 0 r12 0 r13 0 r14 0 r15 0 sp = r16 ra = r17 16 free registers, one number each sp: stack pointer · ra: return address
22 bits for three values: 7 for the temperature, 8 for the pressure, 7 for the oxygen.

Exercise

Task

Pack the values of the gas sensor “Raum” into one number following the table above and write it into the logic memory “Paket”. Read the packet back as a check and show the unpacked oxygen on the display “Sauerstoff”. If the sensor is missing, write −1 into memory and display.

Hints

Hint 1

Each value: read, convert, round, clamp, ins into its place.

Hint 2

Oxygen: RatioOxygen times 100, then bits 15–21.

Hint 3

As a check: read the memory back with lbn and ext out value 15 7.

Quiz

4 questions. You pass from 60 % – the server does the grading.

Complete the day

  • Passed the simulator exercise
  • Passed the quiz with at least 60 %

Without an account progress is saved in this browser only.

Report a mistake in this lesson

Please sign in to report. Sign in

Esc
↑ ↓ selectEnter open/ opens search anywhere