class SimPort:
    def __init__(self, sim, pin, port, attrs, invert):
        self.pin = pin
        self.port = port
        self.attrs = attrs
        self.invert = invert # TODO: support invert

        if pin.xdr == 0:
            if 'i' in pin.dir:
                sim.add_testbench(self.in_xdr0, passive = True)
            if 'o' in pin.dir:
                sim.add_testbench(self.out_xdr0, passive = True)

        elif pin.xdr == 1:
            if 'i' in pin.dir:
                sim.add_testbench(self.in_xdr1, passive = True)
            if 'o' in pin.dir:
                sim.add_testbench(self.out_xdr1, passive = True)

    async def in_xdr0(self, sim):
        while True:
            await sim.changed(self.port.io)
            value = await self.port.io.get()
            await self.pin.i.set(value)

    async def out_xdr0(self, sim):
        while True:
            await sim.changed(self.pin.o)
            value = await self.pin.o.get()
            await self.port.io.set(value)

    async def in_xdr1(self, sim):
        next = 0

        while True:
            await sim.changed(self.pin.i_clk, 1)
            await self.pin.i.set(next)
            next = await self.port.io.get()

    async def out_xdr1(self, sim):
        next = 0

        while True:
            await sim.changed(self.pin.o_clk, 1)
            await self.port.io.set(next)
            next = await self.pin.o.get()