-from nmigen import Elaboratable, Signal, Module
+from nmigen import Elaboratable, Signal, Module, Const
from nmigen.cli import main
class Shifter(Elaboratable):
def __init__(self, width):
+ self.width = width
self.a = Signal(width)
- self.b = Signal(max=width)
+ self.b = Signal(width)
self.o = Signal(width)
def elaborate(self, platform):
m = Module()
- m.d.comb += self.o.eq(self.a << self.b)
+ btrunc = Signal(self.width)
+ m.d.comb += btrunc.eq(self.b & Const((1<<self.width)-1))
+ m.d.comb += self.o.eq(self.a >> btrunc)
return m
self.regs = [0] * nregs
def op(self, op, src1, src2, dest):
+ maxbits = (1 << self.rwidth) - 1
src1 = self.regs[src1]
src2 = self.regs[src2]
if op == IADD:
- val = (src1 + src2)
+ val = src1 + src2
elif op == ISUB:
- val = (src1 - src2)
+ val = src1 - src2
elif op == IMUL:
- val = (src1 * src2)
+ val = src1 * src2
elif op == ISHF:
- val = (src1 << (src2 & self.rwidth))
- val &= ((1<<(self.rwidth))-1)
+ val = src1 >> (src2 & maxbits)
+ val &= maxbits
self.regs[dest] = val
def setval(self, dest, val):
# create some instructions (some random, some regression tests)
instrs = []
if True:
- for i in range(20):
+ for i in range(10):
src1 = randint(1, dut.n_regs-1)
src2 = randint(1, dut.n_regs-1)
while True:
#src2 = 3
#dest = 2
- op = randint(0, 2)
+ op = randint(0, 3)
#op = i % 2
#op = 0