def test_fibonacci_left(dut): dut.log.info("Cocotb test LFSR fibonacci left") clockDomain = ClockDomain(dut.clk, 500, None , RESET_ACTIVE_LEVEL.LOW) # Start clock cocotb.fork(clockDomain.start()) # Init IO and wait the end of the reset dut.io_fib_inc <= 0 dut.io_fib_init <= 0 dut.io_fib_seed <= 0 dut.io_fib_rightLeft <= LFSR_SHIFT_DIR.SHIFT_LEFT yield RisingEdge(dut.clk) yield RisingEdge(dut.clk) # init LFSR widthData = 32 initValue = randInt(0, 2**widthData) dut.io_fib_init <= 1 dut.io_fib_seed <= initValue lfsr = FibonacciLFSR(initValue, [0,2,3,5,10], widthData, LFSR_SHIFT_DIR.SHIFT_LEFT) yield RisingEdge(dut.clk) dut.io_fib_init <= 0 yield RisingEdge(dut.clk) yield RisingEdge(dut.clk) for _ in range(0,10): dut.io_fib_inc <= 1 yield RisingEdge(dut.clk) dut.io_fib_inc <= 0 yield RisingEdge(dut.clk) lfsr.getRand() realResult = int(dut.io_fib_result) assertEquals(lfsr.state, realResult, "LFSR Fibonacci Left - Comparaison Error python : %s - %s : hdl" % (hex(lfsr.state), hex(realResult))) yield RisingEdge(dut.clk) dut.io_fib_inc <= 0 yield RisingEdge(dut.clk) dut.log.info("Cocotb test LFSR fibonacci left")
def test_galois_right(dut): dut.log.info("Cocotb test right LFSR Galois") clockDomain = ClockDomain(dut.clk, 500, None , RESET_ACTIVE_LEVEL.LOW) # Start clock cocotb.fork(clockDomain.start()) # Init IO and wait the end of the reset dut.io_gal_inc <= 0 dut.io_gal_init <= 0 dut.io_gal_seed <= 0 dut.io_gal_rightLeft <= LFSR_SHIFT_DIR.SHIFT_RIGHT yield RisingEdge(dut.clk) yield RisingEdge(dut.clk) # init LFSR widthData = 16 initValue = randInt(0, 2**widthData) dut.io_gal_init <= 1 dut.io_gal_seed <= initValue lfsr = GaloisLFSR(initValue, [1,2], widthData, LFSR_SHIFT_DIR.SHIFT_RIGHT) yield RisingEdge(dut.clk) dut.io_gal_init <= 0 yield RisingEdge(dut.clk) yield RisingEdge(dut.clk) for _ in range(0,10): dut.io_gal_inc <= 1 yield RisingEdge(dut.clk) dut.io_gal_inc <= 0 yield RisingEdge(dut.clk) lfsr.getRand() realResult = int(dut.io_gal_result) assertEquals(lfsr.state, realResult, "LFSR Galois Right - Comparaison Error python : %s - %s : hdl" % (hex(lfsr.state), hex(realResult))) yield RisingEdge(dut.clk) yield RisingEdge(dut.clk) dut.log.info("Cocotb test right LFSR galois")
def test_scenario_1(dut): dut.log.info("Cocotb I2C HAL - Basic test") delay = 300000 listOperation = list() listOperation.append( [START(), WRITE(), ACK(), STOP()] ) listOperation.append( [START(), READ(), NACK(), STOP()] ) listOperation.append( [START(), READ(), ACK(), READ(), NACK(), STOP()] ) listOperation.append( [START(), WRITE(), ACK(), WRITE(), NACK(), STOP()]) listOperation.append( [START(), READ(), ACK(), WRITE(), NACK(), STOP()]) listOperation.append( [START(), WRITE(), ACK(), READ(), NACK(), STOP()] ) listOperation.append( [START(), WRITE(), ACK(), START(), READ(), NACK(), STOP()] ) listOperation.append( [START(), READ(), ACK(), START(), WRITE(), NACK(), STOP()] ) for operationSeq in listOperation: helperMaster = I2CMasterHAL(dut, True) helperSlave = I2CSlaveHAL(dut, True) analyser = I2CHALAnalyser(helperMaster, operationSeq) clockDomain = ClockDomain(dut.clk, 500, dut.resetn, RESET_ACTIVE_LEVEL.LOW) cocotb.fork(clockDomain.start()) # Init IO and wait the end of the reset sclClockDivider = 50 samplingClockDivider = 5 enCollision = 1 helperMaster.io.init(sclClockDivider, samplingClockDivider, enCollision) helperSlave.io.init() yield clockDomain.event_endReset.wait() cocotb.fork(analyser.start()) cocotb.fork(helperMaster.execOperations(operationSeq)) cocotb.fork(helperMaster.checkResponse(operationSeq)) cocotb.fork(helperSlave.execOperations(operationSeq)) yield helperSlave.checkResponse(operationSeq) yield Timer(250000) # Stop all processes clockDomain.stop() helperSlave.stop() helperMaster.stop() # analyser.stop() yield Timer(250000) dut.log.info("Cocotb I2C HAL - Basic test")
def test_scenario(dut): dut.log.info("Cocotb I2C IO Layer - Basic test") from cocotblib.misc import cocotbXHack cocotbXHack() delay = 300000 listOperation = list() listOperation.append( [START(), WRITE_BIT(0), WRITE_BIT(1), WRITE_BIT(0), STOP()]) listOperation.append([START(), READ_BIT(0), READ_BIT(1), STOP()]) listOperation.append([START(), WRITE_BIT(), READ_BIT(), STOP()]) listOperation.append([START(), READ_BIT(0), START(), WRITE_BIT(0), STOP()]) for operationSeq in listOperation: helperMaster = I2CMasterIoLayer(dut) helperSlave = I2CSlaveIoLayer(dut) #analyser = I2CHALAnalyser(helperMaster, operationSeq) clockDomain = ClockDomain(dut.clk, 500, dut.resetn, RESET_ACTIVE_LEVEL.LOW) cocotb.fork(clockDomain.start()) # Init IO and wait the end of the reset helperMaster.io.init() helperSlave.io.init() yield clockDomain.event_endReset.wait() #cocotb.fork(analyser.start()) cocotb.fork(helperMaster.execOperations(operationSeq)) #cocotb.fork(helperMaster.checkResponse(operationSeq)) cocotb.fork(helperSlave.execOperations(operationSeq)) #yield helperSlave.checkResponse(operationSeq) yield Timer(500000) # Stop all processes clockDomain.stop() helperSlave.stop() helperMaster.stop() # analyser.stop() yield Timer(250000) dut.log.info("Cocotb I2C Io Layer - Basic test")
def slave_hal_basic_test(dut): dut.log.info("Cocotb I2C Slave HAL - Basic tests") # list all scenarios listOperation = list() listOperation.append( [START(), WRITE(), ACK(), STOP()] ) listOperation.append( [START(), READ(), NACK(), STOP()] ) listOperation.append( [START(), READ(), ACK(), READ(), NACK(), STOP()] ) listOperation.append( [START(), WRITE(), ACK(), WRITE(), NACK(), STOP()]) listOperation.append( [START(), READ(), ACK(), WRITE(), NACK(), STOP()]) listOperation.append( [START(), WRITE(), ACK(), READ(), NACK(), STOP()] ) listOperation.append( [START(), WRITE(), ACK(), START(), READ(), NACK(), STOP()] ) listOperation.append( [START(), READ(), ACK(), START(), WRITE(), NACK(), STOP()] ) # Run all scenarios for operationSeq in listOperation : helperSlave = I2CSlaveHAL(dut) modelMaster = I2CMasterModelHAL(helperSlave, 50) analyser = I2CHALAnalyser(helperSlave, operationSeq) clockDomain = ClockDomain(dut.clk, 500, dut.resetn, RESET_ACTIVE_LEVEL.LOW) # Start clock cocotb.fork(clockDomain.start()) # Init IO and wait the end of the reset helperSlave.io.init() yield clockDomain.event_endReset.wait() # run cocotb.fork(helperSlave.execOperations(operationSeq)) cocotb.fork(modelMaster.startMaster(operationSeq)) cocotb.fork(analyser.start()) yield helperSlave.checkResponse(operationSeq) yield Timer(500000) # stop all processes clockDomain.stop() helperSlave.stop() modelMaster.stop() analyser.stop() yield Timer(500000) dut.log.info("I2C Slave HAL - basic tests done")
def testAESCore(dut): dut.log.info("Cocotb test AES Core Start") from cocotblib.misc import cocotbXHack cocotbXHack() helperAES = AESCoreHelper(dut) clockDomain = ClockDomain(helperAES.io.clk, 200, helperAES.io.resetn, RESET_ACTIVE_LEVEL.LOW) # Start clock cocotb.fork(clockDomain.start()) # Init IO and wait the end of the reset helperAES.io.init() yield clockDomain.event_endReset.wait() # start monitoring the Valid signal helperAES.io.rsp.startMonitoringValid(helperAES.io.clk) key = 0x2b7e151628aed2a6abf7158809cf4f3c data = 0x6bc1bee22e409f96e93d7e117393172a # Encrpytion helperAES.io.cmd.valid <= 1 helperAES.io.cmd.payload.key <= key helperAES.io.cmd.payload.block <= data helperAES.io.cmd.payload.enc <= 1 # do an encryption # Wait the end of the process and read the result yield helperAES.io.rsp.event_valid.wait() rtlEncryptedBlock = int(helperAES.io.rsp.event_valid.data.block) #print("RTL encrypted", hex(rtlEncryptedBlock)) helperAES.io.cmd.valid <= 0 yield RisingEdge(helperAES.io.clk) # expected result: assertEquals(0x3ad77bb40d7a3660a89ecaf32466ef97, rtlEncryptedBlock, "Encryption data wrong ") dut.log.info("Cocotb test AES Core End")
def testDESCore(dut): dut.log.info("Cocotb test DES Core") from cocotblib.misc import cocotbXHack cocotbXHack() helperDES = DESCoreStdHelper(dut) clockDomain = ClockDomain(helperDES.io.clk, 200, helperDES.io.resetn, RESET_ACTIVE_LEVEL.LOW) # Start clock cocotb.fork(clockDomain.start()) # Init IO and wait the end of the reset helperDES.io.init() yield clockDomain.event_endReset.wait() # start monitoring the Valid signal helperDES.io.rsp.startMonitoringValid(helperDES.io.clk) for _ in range(0, 5): # Vector test ... #key = 0xAABB09182736CCDD #data = 0x123456ABCD132536 #data = 0xC0B7A8D05F3A829C # Gen random value key = randBits(64) data = randBits(64) # Encrpytion helperDES.io.cmd.valid <= 1 helperDES.io.cmd.payload.key <= key helperDES.io.cmd.payload.block <= data helperDES.io.cmd.payload.enc <= 1 # do an encryption # Wait the end of the process and read the result yield helperDES.io.rsp.event_valid.wait() rtlEncryptedBlock = int(helperDES.io.rsp.event_valid.data.block) #print("RTL encrypted", hex(rtlEncryptedBlock)) helperDES.io.cmd.valid <= 0 yield RisingEdge(helperDES.io.clk) # Encrpytion helperDES.io.cmd.valid <= 1 helperDES.io.cmd.payload.key <= key helperDES.io.cmd.payload.block <= rtlEncryptedBlock helperDES.io.cmd.payload.enc <= 0 # do a decryption # Wait the end of the process and read the result yield helperDES.io.rsp.event_valid.wait() rtlDecryptedBlock = int(helperDES.io.rsp.event_valid.data.block) #print("RTL decrypted", hex(rtlDecryptedBlock)) helperDES.io.cmd.valid <= 0 yield RisingEdge(helperDES.io.clk) # Encrypted data with the model k = des(int_2_String(key), CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5) refEncryptedOutput = (k.encrypt(int_2_String(data))).encode('hex')[:16] # print("Ref encrypted ", refEncryptedOutput) # compare result assertEquals(int(refEncryptedOutput, 16), rtlEncryptedBlock, "Encryption data wrong ") assertEquals(rtlDecryptedBlock, data, "Decryption data wrong ") dut.log.info("Cocotb end test DES Core")
def testMD5EngineStd(dut): dut.log.info("Cocotb test MD5 Engine Std") from cocotblib.misc import cocotbXHack cocotbXHack() helperMD5 = MD5EngineStdHelper(dut) clockDomain = ClockDomain(helperMD5.io.clk, 200) # Start clock cocotb.fork(clockDomain.start()) # Init IO and wait the end of the reset helperMD5.io.initIO() #yield clockDomain.event_endReset.wait() yield RisingEdge(helperMD5.io.clk) # start monitoring rsp helperMD5.io.rsp.startMonitoringValid(helperMD5.io.clk) # Fix patterns msgs = [ [ 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ], [ 0x00000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ], [ 0x80636261000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000 ], [ 0x34333231383736353231303936353433303938373433323138373635323130393635343330393837343332313837363532313039363534333039383734333231, 0x38373635323130393635343330393837000000800000000000000000000000000000000000000000000000000000000000000000000000000000028000000000 ] ] digests = [ 0x031F1DAC6EA58ED01FAB67B774317791, 0xD98C1DD404B2008F980980E97E42F8EC, 0x98500190B04FD23C7D3F96D6727FE128, 0xA2F4ED5755C9E32B2EDA49AC7AB60721 ] # Process all pattern indexPattern = 0 for msgPattern in msgs: # Init MD5 yield RisingEdge(helperMD5.io.clk) helperMD5.io.init <= 1 yield RisingEdge(helperMD5.io.clk) helperMD5.io.init <= 0 yield RisingEdge(helperMD5.io.clk) for msgBlock in msgPattern: # Hash data helperMD5.io.cmd.valid <= 1 helperMD5.io.cmd.payload.block <= msgBlock # wait the end of the encryption yield helperMD5.io.rsp.event_valid.wait() helperMD5.io.cmd.valid <= 0 rtlDigest = "{0:0>4X}".format( int(str(helperMD5.io.rsp.payload.digest), 2)) yield RisingEdge(helperMD5.io.clk) assertEquals(int(rtlDigest, 16), digests[indexPattern], "Wrong digest") yield RisingEdge(helperMD5.io.clk) yield Timer(50000) indexPattern += 1
def testHMACCore_MD5(dut): dut.log.info("Cocotb test HMAC - MD5 Core Std") from cocotblib.misc import cocotbXHack cocotbXHack() helperHMAC = HMACCoreStdHelper(dut) clockDomain = ClockDomain(helperHMAC.io.clk, 200, helperHMAC.io.resetn, RESET_ACTIVE_LEVEL.LOW) # Start clock cocotb.fork(clockDomain.start()) # Init IO and wait the end of the reset helperHMAC.io.initIO() yield clockDomain.event_endReset.wait() # start monitoring rsp helperHMAC.io.rsp.startMonitoringValid(helperHMAC.io.clk) helperHMAC.io.cmd.startMonitoringReady(helperHMAC.io.clk) msgPattern = [randomword(100 - size) for size in range(1, 100)] keyPattern = [randomword(30) for _ in range(1, 100)] #msgPattern = ["11111111222222223333333344444444555555556666666677777777"] #keyPattern = ["gxmlzvkwyuvtkyfhgliszczfdscqyh"] for index in range(0, len(msgPattern)): hexMsg = "".join([format(ord(c), "x") for c in msgPattern[index]]) hexKey = "".join([format(ord(c), "x") for c in keyPattern[index]]) print("key : ", keyPattern[index]) print("key : ", hexKey) print("msg : ", msgPattern[index]) print("msg : ", hexMsg) ## key padding to get a key of the size of the md5 block if (len(hexKey) < 128): hexKey = hexKey + "0" * (128 - len(hexKey)) # Init yield RisingEdge(helperHMAC.io.clk) helperHMAC.io.init <= 1 yield RisingEdge(helperHMAC.io.clk) helperHMAC.io.init <= 0 yield RisingEdge(helperHMAC.io.clk) while (hexMsg != None): if len(hexMsg) > 8: block = endianessWord(hexMsg[:8]) hexMsg = hexMsg[8:] isLast = 0 sizeLast = 0 else: block = endianessWord(hexMsg + "0" * (8 - len(hexMsg))) isLast = 1 sizeLast = (len(hexMsg) / 2) - 1 hexMsg = None helperHMAC.io.cmd.valid <= 1 helperHMAC.io.cmd.payload.fragment_msg <= int(block, 16) helperHMAC.io.cmd.payload.fragment_key <= int( endianess(hexKey), 16) helperHMAC.io.cmd.payload.fragment_size <= sizeLast helperHMAC.io.cmd.payload.last <= isLast if isLast == 1: yield helperHMAC.io.rsp.event_valid.wait() rtlhmac = hex(int( helperHMAC.io.rsp.event_valid.data.hmac))[2:-1] if (len(rtlhmac) != 32): rtlhmac = "0" * (32 - len(rtlhmac)) + rtlhmac else: yield helperHMAC.io.cmd.event_ready.wait() helperHMAC.io.cmd.valid <= 0 rtlhmac = endianess(rtlhmac) print("index : ", index) modelHmac = hmac.new(keyPattern[index], msgPattern[index], hashlib.md5).hexdigest() print("hmac : ", rtlhmac, modelHmac, int(rtlhmac, 16) == int(modelHmac, 16)) print() # assertEquals(int(rtlhmac, 16), int(modelHmac, 16), "Wrong hmac hash value computed ") yield Timer(50000)
def testMD5CoreStd(dut): dut.log.info("Cocotb test MD5 Core Std") from cocotblib.misc import cocotbXHack cocotbXHack() helperMD5 = MD5CoreStdHelper(dut) clockDomain = ClockDomain(helperMD5.io.clk, 200, helperMD5.io.resetn, RESET_ACTIVE_LEVEL.LOW) # Start clock cocotb.fork(clockDomain.start()) # Init IO and wait the end of the reset helperMD5.io.initIO() yield clockDomain.event_endReset.wait() # start monitoring rsp helperMD5.io.rsp.startMonitoringValid(helperMD5.io.clk) helperMD5.io.cmd.startMonitoringReady(helperMD5.io.clk) # Fix patterns # msgPattern = [randomword(100 - size) for size in range(1, 100)] #msgPattern = ["11111111222222223333333344444444555555556666666677777777"] for tmpMsg in msgPattern: hexMsg = "".join([format(ord(c), "x") for c in tmpMsg]) # Init MD5 yield RisingEdge(helperMD5.io.clk) helperMD5.io.init <= 1 yield RisingEdge(helperMD5.io.clk) helperMD5.io.init <= 0 yield RisingEdge(helperMD5.io.clk) block = 0 rtlHash = 0 while (hexMsg != None): isLast = 0 sizeLast = 0 if len(hexMsg) > 8: block = endianessWord(hexMsg[:8]) hexMsg = hexMsg[8:] isLast = 0 else: block = endianessWord(hexMsg + "0" * (8 - len(hexMsg))) isLast = 1 sizeLast = (len(hexMsg) / 2) - 1 hexMsg = None helperMD5.io.cmd.valid <= 1 helperMD5.io.cmd.payload.fragment_msg <= int(block, 16) helperMD5.io.cmd.payload.fragment_size <= sizeLast helperMD5.io.cmd.payload.last <= isLast if isLast == 1: yield helperMD5.io.rsp.event_valid.wait() tmp = hex(int(helperMD5.io.rsp.event_valid.data.digest))[2:-1] if (len(tmp) != 32): tmp = "0" * (32 - len(tmp)) + tmp else: yield helperMD5.io.cmd.event_ready.wait() helperMD5.io.cmd.valid <= 0 yield RisingEdge(helperMD5.io.clk) rtlHash = endianess(tmp) # Check result m = hashlib.md5(tmpMsg) modelHash = m.hexdigest() assertEquals(int(rtlHash, 16), int(modelHash, 16), "Wrong MD5 hash value computed ") #print("hash-model: ", int(rtlHash, 16) == int(modelHash, 16) , " :" , rtlHash, " - ", modelHash , " -- : ", tmpMsg) yield Timer(50000)