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.
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.
| frame |
frame := #[18 52 86].
{frame size. frame first. frame last}Expected: #(3 18 86)

Try it: Which decimal byte corresponds to the hexadecimal value 0x34?
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.
| 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)

Try it: What would the same single step produce for 0x01?
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.
| 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)

Try it: Why does the empty byte array leave the register at zero?
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.
| 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)

Try it: Would adding a newline still be the same check vector?
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.
| 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)

Try it: Why does keeping the original checksum matter in this simulation?
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.
| 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)

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

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.