Read in KR

Array Indexing

While arrays in SCode are limited to local use and cannot be used as ports, they offer several advantages for simplifying logic. One of the most significant benefits is the ability to select an array element using another logic signal.

For example, if data is declared as an array, you can use a logic signal named sel to select an item from it:

import math

outport(selected[8])

N = 16
array(data[N][8])

logic("sel[%s]" % int(math.log2(N)))   # Needs 4 bits to select from 16 items
 
selected <= data[sel]

Using the data[sel] format allows you to select items without needing a switch statement, which is ideal for constructing multiplexer circuits or memory access logic.

The following example stores ROM memory with initialized data and outputs the content of the memory whenever enable is active:

inport(clk, reset, enable)
outport(data[8])

N = 16
array(rom[N][8], init=[i for i in range(N)])

logic("addr[%s]" % min_bits(N-1))
with sequence(clk):
    addr <= (0, reset, addr + 1, enable)
    
with sequence(clk):
    data <= rom[addr]
← All posts