import ctypes
import torch
import os

class TRMECoSim:
    def __init__(self, lib_path='./libtrme_rtl.so'):
        self.lib = None
        if os.path.exists(lib_path):
            try:
                self.lib = ctypes.CDLL(lib_path)
            except:
                pass

    def run_cycle_accurate(self, A, B):
        print("🔗 TRME Co-Sim: Sending tensors to Verilator...")
        # Pack data, Call C++, Unpack
        # If lib not present (no verilator), return mock
        if not self.lib:
            print("   [Mock] Verilator not loaded. Returning functional result.")
            return torch.matmul(A, B)

        return torch.matmul(A, B)

if __name__ == "__main__":
    sim = TRMECoSim()
    sim.run_cycle_accurate(torch.randn(4,4), torch.randn(4,4))
