s LearnSqueak.com

SMALLTALK LAB

CRC-8/SMBUS in Squeak: detect a flipped sensor bit

Walk an eight-bit register one bit at a time, confirm the standard check value, then simulate a single accidental bit flip in a synthetic sensor reading.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

Before you begin

Open a Squeak Workspace. The examples work on a synthetic reading, not an actual SMBus transaction. Each step can be run by itself.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.

01 - Write a tiny sensor reading as bytes

Use three synthetic octets, 0x12, 0x34 and 0x56. A ByteArray keeps the data as integer bytes rather than display characters.

Independent Workspace expression - select all, then Print it
| frame |
frame := #[18 52 86].
{frame size. frame first. frame last}

Expected: #(3 18 86)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Write a tiny sensor reading as bytes
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Which decimal byte corresponds to the hexadecimal value 0x34?

Download this checkpoint

02 - Advance the highest set bit

Read the high bit before shifting. Mask off the overflow bit, then XOR with 0x07 only if the original high bit was set. The mask keeps the register exactly eight bits wide.

Independent Workspace expression - select all, then Print it
| register high |
register := 16r80.
high := (register bitAnd: 16r80) ~= 0.
register := (register bitShift: 1) bitAnd: 16rFF.
high ifTrue: [register := register bitXor: 16r07].
{high. register}

Expected: #(true 7)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Advance the highest set bit
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: What would the same single step produce for 0x01?

Download this checkpoint

03 - Mix each byte and run eight bit steps

XOR the complete byte into the register, then apply the bit step eight times. Do not reset the register between bytes.

Independent Workspace expression - select all, then Print it
| crc8 |
crc8 := [:bytes |
 | register |
 register := 0.
 bytes do: [:byte |
  (byte isInteger and: [byte between: 0 and: 255])
   ifFalse: [Error signal: 'Expected byte integers in 0..255'].
  register := register bitXor: byte.
  8 timesRepeat: [
   | high |
   high := (register bitAnd: 16r80) ~= 0.
   register := (register bitShift: 1) bitAnd: 16rFF.
   high ifTrue: [register := register bitXor: 16r07]]].
 register].
{crc8 value: #[]. crc8 value: #[1]. crc8 value: #[255]}

Expected: #(0 7 243)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Mix each byte and run eight bit steps
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Why does the empty byte array leave the register at zero?

Download this checkpoint

04 - Confirm the standard check value

The byte sequence here is ASCII for 123456789. Its CRC is 244 decimal, or F4 hexadecimal. If yours differs, check bit order, initial value and byte encoding.

Independent Workspace expression - select all, then Print it
| crc8 result |
crc8 := [:bytes |
 | register |
 register := 0.
 bytes do: [:byte |
  (byte isInteger and: [byte between: 0 and: 255])
   ifFalse: [Error signal: 'Expected byte integers in 0..255'].
  register := register bitXor: byte.
  8 timesRepeat: [
   | high |
   high := (register bitAnd: 16r80) ~= 0.
   register := (register bitShift: 1) bitAnd: 16rFF.
   high ifTrue: [register := register bitXor: 16r07]]].
 register].
result := crc8 value: #[49 50 51 52 53 54 55 56 57].
{result. result = 16rF4}

Expected: #(244 true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Confirm the standard check value
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Would adding a newline still be the same check vector?

Download this checkpoint

05 - Detect one accidentally flipped sensor bit

The copy changes only the low bit of the second byte: 0x34 becomes 0x35. The untouched checksum is 0x7C, while recomputing over the received bytes yields 0x69. Reject the reading on mismatch.

Independent Workspace expression - select all, then Print it
| crc8 original received sentCRC receivedCRC |
crc8 := [:bytes |
 | register |
 register := 0.
 bytes do: [:byte |
  (byte isInteger and: [byte between: 0 and: 255])
   ifFalse: [Error signal: 'Expected byte integers in 0..255'].
  register := register bitXor: byte.
  8 timesRepeat: [
   | high |
   high := (register bitAnd: 16r80) ~= 0.
   register := (register bitShift: 1) bitAnd: 16rFF.
   high ifTrue: [register := register bitXor: 16r07]]].
 register].
original := #[18 52 86].
received := original copy.
received at: 2 put: ((received at: 2) bitXor: 1).
sentCRC := crc8 value: original.
receivedCRC := crc8 value: received.
{sentCRC. receivedCRC. sentCRC = receivedCRC}

Expected: #(124 105 false)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Detect one accidentally flipped sensor bit
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Why does keeping the original checksum matter in this simulation?

Download this checkpoint

06 - Check the byte contract at the boundary

Reject numbers outside 0 through 255 instead of silently masking them. An ordered Array of valid integer bytes produces the same result as a ByteArray.

Independent Workspace expression - select all, then Print it
| crc8 rejected |
crc8 := [:bytes |
 | register |
 register := 0.
 bytes do: [:byte |
  (byte isInteger and: [byte between: 0 and: 255])
   ifFalse: [Error signal: 'Expected byte integers in 0..255'].
  register := register bitXor: byte.
  8 timesRepeat: [
   | high |
   high := (register bitAnd: 16r80) ~= 0.
   register := (register bitShift: 1) bitAnd: 16rFF.
   high ifTrue: [register := register bitXor: 16r07]]].
 register].
rejected := [crc8 value: #(256). false]
 on: Error do: [:err | true].
{rejected. crc8 value: #(18 52 86)}

Expected: #(true 124)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Check the byte contract at the boundary
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: What should happen if an element is a Character or a negative integer?

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
CRC-8/SMBUS in Squeak: detect a flipped sensor bit native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The static checkpoints appear above.

Reason about correctness and limits

The input contract is a finite, ordered sequence of integer bytes from 0 through 255, such as a ByteArray. Empty input yields zero. Encode text to bytes explicitly; this example uses the exact nine ASCII octets, with no newline.

CRC-8/SMBUS uses polynomial x^8 + x^2 + x + 1 (0x07 without its leading term), initial register zero, most significant bit first, no input or output reflection, and no final XOR. Other CRC-8 variants may disagree.

This simulated sensor reading is only an ordered byte sequence. Actual SMBus PEC includes address, command and data as defined for each transaction, not ACK/NACK bits.

A CRC detects transmission errors; matching values do not establish authenticity or guarantee every possible error is caught. This implementation uses eight register updates per byte and constant working space.

Three practice challenges

High bit clear

Repeat checkpoint 2 with 0x01. What is the new register?

Show solution

The original high bit is clear, so the result is 0x02 without a polynomial XOR.

Two ways to spell a byte

Replace decimal byte 52 with 16r34 in the reading. Does the CRC change?

Show solution

No. The two Smalltalk literals name the same integer, so the register updates match.

Parse bytes before checking

What should a receiver do if a parsed element is -1 or the Character $4?

Show solution

Reject that malformed input before using it as a byte. To encode the character as ASCII explicitly, supply decimal 52.

Reference

System Management Interface Forum: SMBus specification, §6.4 Packet Error Checking

Continue learning

Integer Bitsets | Luhn Checksum

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.