lc-3-2 / lc32sim

LC-3.2 simulator
0 stars 0 forks source link

Make Shifts Match the New Spec #16

Closed ammrat13 closed 1 year ago

ammrat13 commented 1 year ago

Previously, immediate shifts were handled by only making RSHFA have an immediate option controlled by bit 5. This way, the immediate field could go from 0 to 31 without issue, at the expense of robbing immediate options from LSHF and RSHFL.

Now, we've decided to make all shifts have an immediate variant. Bit 5 will control whether the immediate is used, and the last three bits are treated as the unsigned amount to shift by. The amount is incremented by one in hardware.

The encoding is shown in the table below. There, <<(A,D) denotes the shift corresponding to the bits A and D. So, <<(0,0) would be a left shift, while <<(1,1) would be an arithmetic right shift. Additionally sext(f,i)(x) denotes sign-extended the least significant i bits of x to a final bit width of f.

     +------+----+-----+---+---+---+-----+
SHF+ | 1101 | DR | SR1 | 0 | A | D | SR2 |
     +------+----+-----+---+---+---+-----+

DR <= SR1 <<(A,D) sext(5,32)(SR2)

===

     +------+----+-----+---+---+---+---------+
SHF+ | 1101 | DR | SR1 | 1 | A | D | amount3 |
     +------+----+-----+---+---+---+---------+

DR <= SR1 <<(A,D) (sext(5,3)(amount3) + 1)

For example:

0xd000 : LSHF  R0, R0, R0
0xd020 : LSHF  R0, R0, #1
0xd02f : RSHFL R0, R0, #8