I worked through a couple of examples of indexed table access using a standard instruction set just to ensure that things could work.
Quote:
A very few machines are word addressable. Most machines are byte addressable, even those with word-size instructions. Perhaps this is a case for making a machine which is bit-addressable? Or nibble-addressable, depending on how your instruction boundaries sit within a 128 bit boundary.
The machine is now byte addressable. I switched the instruction set around trimming a bit off the instruction size in order to make it an even multiple of a byte in size. Instructions addresses and data addresses are now the same. One key to making things work is a jal instruction that automatically rounds to a valid instruction address.
Code:
; Hypothetical machine with 3, 40 bit instructions in 128 bits
; Since three instructions are fit into 128 bits the instruction
; width is effectively 5.333... bytes even though some bits are
; not used.
; Assumes JAL rounds to an instruction address
; Packed table
; R1 = table index
; R3 = address of table
mul R1,R1,#21846 ; multiply by 5.333 * 4096
and R2,R1,#0xFFF ; get remainder
sne R2,R2,R0 ; set R2 = 1 if remainder <> 0
shr R1,R1,#12 ; divide by 4096
add R1,R1,R2 ; round up (add remainder)
add R1,R1,R3 ; add in table base address (in R3)
jal R49,[R1] ; jump to address
; Faster math
; Table with "holes"
; Simply multiplies by six rather than 5.333 but needs a table
; with holes in it.
shl R1,R1,#1 ; *2
shl R2,R1,#1 ; *4
add R1,R1,R2 ; *6
add R1,R1,R3 ; and in table base address (in R3)
jal R49,[R1] ; jump to address
; Even faster to multiply by eight but then the table is only 66% full.
Scaling by 4096 was chosen because it gives 12 bits of precision for fixed point math.
This allows a table with thousands of entries without any holes. Probably gross overkill for most apps.