Next: , Previous: Models, Up: Top


8 Functional Design

The simultaneous assignment of signals within a block is managed by define-synchronous-system. The next-function expressions are functional as opposed to imperative; these expressions should not use set! or other mutators.

See Models for how to create (RAM) Arrays and FIFOs.

— Function: array-ref array index1 index2 ...

The procedure array-ref returns the contents of the array location selected by index1 index2 ....

— Function: array-set array new-value index1 index2 ...

The procedure array-set returns a copy of array with the location selected by index1 index2 ... replaced by new-value.

— Function: fifo:first fifo

Returns the first item of fifo. This item will be discarded by a call to fifo:remove-first.

— Function: fifo:remove-first fifo

Returns fifo with its first item discarded. If fifo is already empty, an error is signalled.

— Function: fifo:insert-last fifo datum

Returns fifo with datum added. If fifo is already full, an error is signalled.

— Function: fifo:empty? fifo

Returns #t if fifo is empty; otherwise, #f.

— Function: fifo:full? fifo

Returns #t if fifo is full; otherwise, #f.

— Function: fifo:fullness fifo

Returns the number of items fifo is currently holding.

Bitwise Operations

— Function: logand n1 n1

Returns the integer which is the bit-wise AND of the two integer arguments.

Example:

          (number->string (logand #b1100 #b1010) 2)
             ⇒ "1000"
— Function: logior n1 n2

Returns the integer which is the bit-wise OR of the two integer arguments.

Example:

          (number->string (logior #b1100 #b1010) 2)
             ⇒ "1110"
— Function: number-or name k1 ...

The arguments to logior must be integers. The k1 ... arguments to number-or can be either integers or #f. All the arguments to number-or are evaluated. If more than one k1 ... argument is an integer, a warning using name name is issued.

          (number-or 'ls[2..0]
                     (and cc-litx1 #.ls-data)
                     (and cc-litx2 #.ls-data-x2)
                     (and cc-litx3 #.ls-data-x3))
— Function: number-check name k

Issues a warning if k is not a number or negative. number-check returns #t.

— Function: logxor n1 n2

Returns the integer which is the bit-wise XOR of the two integer arguments.

Example:

          (number->string (logxor #b1100 #b1010) 2)
             ⇒ "110"
— Function: lognot n

Returns the integer which is the 2s-complement of the integer argument.

Example:

          (number->string (lognot #b10000000) 2)
             ⇒ "-10000001"
          (number->string (lognot #b0) 2)
             ⇒ "-1"
— Function: bitwise-if mask n0 n1

Returns an integer composed of some bits from integer n0 and some from integer n1. A bit of the result is taken from n0 if the corresponding bit of integer mask is 1 and from n1 if that bit of mask is 0.

— Function: logtest j k
          (logtest j k) == (not (zero? (logand j k)))
          
          (logtest #b0100 #b1011) ⇒ #f
          (logtest #b0100 #b0111) ⇒ #t
— Function: = j k

Returns #t if and only if integer j equals integer k.

— Function: zero? j

Returns #t only if integer j is 0.

Bit Within Word

— Function: logbit? index j

Returns #t if bit index of j is 1. Bit 0 is always the low order bit; (logbit? 0 data[31..24]) will be #t only if data[31..24]'s value is odd.

— Function: copy-bit index from bit

Returns an integer the same as from except in the indexth bit, which is 1 if bit is #t and 0 if bit is #f.

Example:

          (number->string (copy-bit 0 0 #t) 2)       ⇒ "1"
          (number->string (copy-bit 2 0 #t) 2)       ⇒ "100"
          (number->string (copy-bit 2 #b1111 #f) 2)  ⇒ "1011"

Fields of Bits

— Function: bit-field n start end

Returns the integer composed of the start (inclusive) through end (exclusive) bits of n. The startth bit becomes the low order (even/odd) bit in the result. Note that the end index is one more than the high order bit index. Thus, (bit-field data[7..0] 0 8) and (bit-field data[31..24] 0 8) are identity functions.

This function was called bit-extract in SYNCH1a1.

— Function: copy-bit-field to from start end

Returns an integer the same as to except possibly in the start (inclusive) through end (exclusive) bits, which are the same as those of from. The 0-th bit of from becomes the startth bit of the result.

Example:

          (number->string (copy-bit-field #b1101101010 0 0 4) 2)
                  ⇒ "1101100000"
          (number->string (copy-bit-field #b1101101010 -1 0 4) 2)
                  ⇒ "1101101111"
          (number->string (copy-bit-field #b110100100010000 -1 5 9) 2)
                  ⇒ "110100111110000"
— Function: ash int count
— Function: arithmetic-shift int count

Returns an integer equivalent to (inexact->exact (floor (* int (expt 2 count)))).

Example:

          (number->string (ash #b1 3) 2)
             ⇒ "1000"
          (number->string (ash #b1010 -1) 2)
             ⇒ "101"

Bits shifted below the low-order bit disappear; bits shifted above the high order bit do not disappear. Remember to mask unused bits using logand.

— Function: booleans-to-number bits

bits should be booleans, taken in order they are the binary representation of the integer returned.

Example:

          (number->string (booleans-to-number #t #f #f))
             ⇒ "100"
          (number->string (booleans-to-number #t #t #f #t))
             ⇒ "1101"