etherforth_logo

Node 116

This node reads bit stream from 115, adds stuffing bits, and sends data to encoder in node 216. It passes tokens unmodified.

Source code

usb node 216 node 115 node 116
116 bit stuffing
arm 00 @p !p ; 6 , cnt 02 0 ,
stuf? 03 b-b 1 and . if a push @p 2 'cnt , a!
@ -1 . + if ! pop a! ;
then b0 pop a! over ! then arm ;
go 0F @b -if ! arm go ; then stuf? ! go ; 14

init up a! left b! go ; 18

5 d 7 r 0 ether

Definitions

arm
Set counter cnt to initial value 6, i.e. number of one bits before stuffing bit needs to be inserted.
cnt
Address of the counter of one bits to go before stuffing bit insertion.
stuf?
Test the bit b; if one then decrement counter cnt, and if non-zero after decrementing then just return to go. Otherwise insert zero bit into the stream, rearm the counter, and return to go. If bit is zero, rearm and return to go.
go
Read from LEFT and pass tokens (negative numbers) to encoder, while rearming the counter cnt. If not token the bit is sent to stuf?, where stuffing zero bit is inserted if needed. The data bit is then inserted into the stream, and execution returns to go again.
init
Set A and B, and jump to go

Description

This code reads bits (only T0 is significant in words read form node 115), and counts one bits. When six consecutive one bits has been counted, i.e. we are about to pass seventh one bit, we insert one zero bit before it. The insertion is carried out by word stuf?, so that word go simply reads a bit from node 115, calls stuf? to insert zero bit if needed, and then passes the bit read from LEFT to UP to node 216. Thus, from the point of view of neighbor nodes node 116 looks like a simple wire node.

Notice also that each zero data bit rearms the counter cnt, same as tokens (negative numbers).