def i2cSlaveThread(cmdBus, rspBus, cmds, rsps, clk): rspBus.valid <= False while rsps: yield RisingEdge(clk) rspBus.valid <= False if cmdBus.valid == True: expected = cmds.pop(0) log.debug(expected) if expected == "start": assert cmdBus.payload.mode == 0 elif expected == "stop": assert cmdBus.payload.mode == 3 elif expected == "drive": assert cmdBus.payload.mode == 1 rsp = rsps.pop(0) if rsp == "Z": rspBus.valid <= True rspBus.payload.enable <= False rspBus.payload.data <= randInt(0, 1) elif isinstance(rsp, bool): rspBus.valid <= True rspBus.payload.enable <= True rspBus.payload.data <= rsp else: raise Exception(rsp) elif isinstance(expected, bool): assert cmdBus.payload.mode == 2 assert cmdBus.payload.data == expected else: raise Exception("???")
def _setup_logging(): global log def _reopen_stream_with_buffering(stream_name): try: if not getattr(sys, stream_name).isatty(): setattr(sys, stream_name, os.fdopen(getattr(sys, stream_name).fileno(), 'w', 1)) return True return False except Exception as e: return e # If stdout/stderr are not TTYs, Python may not have opened them with line # buffering. In that case, try to reopen them with line buffering # explicitly enabled. This ensures that prints such as stack traces always # appear. Continue silently if this fails. _stdout_buffer_result = _reopen_stream_with_buffering('stdout') _stderr_buffer_result = _reopen_stream_with_buffering('stderr') # Don't set the logging up until we've attempted to fix the standard IO, # otherwise it will end up connected to the unfixed IO. cocotb.log.default_config() log = logging.getLogger(__name__) # we can't log these things until the logging is set up! if _stderr_buffer_result is True: log.debug("Reopened stderr with line buffering") if _stdout_buffer_result is True: log.debug("Reopened stdout with line buffering") if isinstance(_stdout_buffer_result, Exception) or isinstance( _stderr_buffer_result, Exception): if isinstance(_stdout_buffer_result, Exception): log.warning("Failed to ensure that stdout is line buffered", exc_info=_stdout_buffer_result) if isinstance(_stderr_buffer_result, Exception): log.warning("Failed to ensure that stderr is line buffered", exc_info=_stderr_buffer_result) log.warning("Some stack traces may not appear because of this.") del _stderr_buffer_result, _stdout_buffer_result
def SlaveThread(scl, sda, clk, baudPeriod): global crapyConflictCounter global normalConflictCounter global normalTransactionCounter log.debug("x") IDLE = 0 START = 1 DATA = 2 state = IDLE dataState = 0 scl.write(True) sda.write(True) sclLast = True sdaLast = True while True: yield RisingEdge(clk) sclValue = scl.read() sdaValue = sda.read() sclRising = sclValue and not sclLast sclFalling = not sclValue and sclLast sdaRising = sdaValue and not sdaLast sdaFalling = not sdaValue and sdaLast sclLast = sclValue sdaLast = sdaValue if state == IDLE: if sdaFalling and sclValue: state = START elif state == START: if sclFalling: state = DATA dataState = 0 address = 0 elif state == DATA: if sclRising: if dataState < 8: address |= sdaValue << (7 - dataState) elif sclFalling: dataState += 1 if dataState >= 8 and dataState < 16: if random.random() < 0.2: #Clock stretching scl.write(False) yield Timer(randInt(baudPeriod / 10, baudPeriod * 10)) sda.write((cmdToData[address] >> (15 - dataState)) & 1) yield Timer(baudPeriod / 4) scl.write(True) yield clockedFuncWaitTrue(clk, scl.read) sclLast = False else: sda.write((cmdToData[address] >> (15 - dataState)) & 1) elif (dataState == 6 and random.random() < 0.2): scl.write(False) if random.random() < 0.2: # Clock stretching yield Timer(randInt(baudPeriod / 10, baudPeriod * 10)) yield Timer(baudPeriod / 2) sda.write(False) yield Timer(baudPeriod / 2) scl.write(True) yield clockedFuncWaitTrue(clk, scl.read) yield Timer(baudPeriod) if random.random() < 0.5: #Normal conflict normalConflictCounter += 1 for i in range(4): rand = randBool() scl.write(False) yield Timer(baudPeriod / 2) sda.write(rand) yield Timer(baudPeriod / 2) scl.write(True) yield clockedFuncWaitTrue(clk, scl.read) yield Timer(baudPeriod) assert sda.read() == rand scl.write(False) yield Timer(baudPeriod / 2) sda.write(False) yield Timer(baudPeriod / 2) scl.write(True) yield clockedFuncWaitTrue(clk, scl.read) yield Timer(baudPeriod) sda.write(True) else: #crapy conflict wihtout STOP crapyConflictCounter += 1 scl.write(False) yield Timer(baudPeriod / 2) sda.write(True) yield Timer(baudPeriod / 2) scl.write(True) yield clockedFuncWaitTrue(clk, scl.read) yield Timer(baudPeriod) state = IDLE continue else: if random.random() < 0.2: #Clock stretching scl.write(False) yield Timer(randInt(baudPeriod / 10, baudPeriod * 10)) sda.write(True) yield Timer(baudPeriod / 4) scl.write(True) sclLast = False else: sda.write(True) elif sclValue: if sdaRising: state = 0 if sdaFalling: state = 1 pass
# If stdout/stderr are not TTYs, Python may not have opened them with line # buffering. In that case, try to reopen them with line buffering # explicitly enabled. This ensures that prints such as stack traces always # appear. Continue silently if this fails. _stdout_buffer_result = _reopen_stream_with_buffering('stdout') _stderr_buffer_result = _reopen_stream_with_buffering('stderr') # Don't set the logging up until we've attempted to fix the standard IO, # otherwise it will end up connected to the unfixed IO. cocotb.log.default_config() log = logging.getLogger(__name__) # we can't log these things until the logging is set up! if _stderr_buffer_result is True: log.debug("Reopened stderr with line buffering") if _stdout_buffer_result is True: log.debug("Reopened stdout with line buffering") if isinstance(_stdout_buffer_result, Exception) or isinstance(_stderr_buffer_result, Exception): if isinstance(_stdout_buffer_result, Exception): log.warning("Failed to ensure that stdout is line buffered", exc_info=_stdout_buffer_result) if isinstance(_stderr_buffer_result, Exception): log.warning("Failed to ensure that stderr is line buffered", exc_info=_stderr_buffer_result) log.warning("Some stack traces may not appear because of this.") del _stderr_buffer_result, _stdout_buffer_result # From https://www.python.org/dev/peps/pep-0565/#recommended-filter-settings-for-test-runners # If the user doesn't want to see these, they can always change the global # warning settings in their test module. if not sys.warnoptions:
def SlaveThread(scl,sda,clk,baudPeriod): global crapyConflictCounter global normalConflictCounter global normalTransactionCounter log.debug("x") IDLE = 0 START = 1 DATA = 2 state = IDLE dataState = 0 scl.write(True) sda.write(True) sclLast = True sdaLast = True while True: yield RisingEdge(clk) sclValue = scl.read() sdaValue = sda.read() sclRising = sclValue and not sclLast sclFalling = not sclValue and sclLast sdaRising = sdaValue and not sdaLast sdaFalling = not sdaValue and sdaLast sclLast = sclValue sdaLast = sdaValue if state == IDLE: if sdaFalling and sclValue: state = START elif state == START: if sclFalling: state = DATA dataState = 0 address = 0 elif state == DATA: if sclRising: if dataState < 8: address |= sdaValue << (7-dataState) elif sclFalling: dataState += 1 if dataState >= 8 and dataState < 16: if random.random() < 0.2: #Clock stretching scl.write(False) yield Timer(randInt(baudPeriod/10, baudPeriod*10)) sda.write((cmdToData[address] >> (15-dataState)) & 1) yield Timer(baudPeriod/4); scl.write(True) yield clockedFuncWaitTrue(clk, scl.read) sclLast = False else: sda.write((cmdToData[address] >> (15 - dataState)) & 1) elif(dataState == 6 and random.random() < 0.2): scl.write(False) if random.random() < 0.2: # Clock stretching yield Timer(randInt(baudPeriod / 10, baudPeriod * 10)) yield Timer(baudPeriod / 2) sda.write(False) yield Timer(baudPeriod / 2) scl.write(True) yield clockedFuncWaitTrue(clk, scl.read) yield Timer(baudPeriod) if random.random() < 0.5: #Normal conflict normalConflictCounter += 1 for i in range(4): rand = randBool() scl.write(False) yield Timer(baudPeriod/2) sda.write(rand) yield Timer(baudPeriod/2) scl.write(True) yield clockedFuncWaitTrue(clk, scl.read) yield Timer(baudPeriod) assert sda.read() == rand scl.write(False) yield Timer(baudPeriod / 2) sda.write(False) yield Timer(baudPeriod / 2) scl.write(True) yield clockedFuncWaitTrue(clk, scl.read) yield Timer(baudPeriod) sda.write(True) else: #crapy conflict wihtout STOP crapyConflictCounter += 1 scl.write(False) yield Timer(baudPeriod / 2) sda.write(True) yield Timer(baudPeriod / 2) scl.write(True) yield clockedFuncWaitTrue(clk, scl.read) yield Timer(baudPeriod) state = IDLE continue else: if random.random() < 0.2: #Clock stretching scl.write(False) yield Timer(randInt(baudPeriod/10, baudPeriod*10)) sda.write(True) yield Timer(baudPeriod/4); scl.write(True) sclLast = False else: sda.write(True) elif sclValue: if sdaRising: state = 0 if sdaFalling: state = 1 pass