From eaba55d66cd0681e6174e2183f3d5f74f366577b Mon Sep 17 00:00:00 2001 From: Dmitry Ryabikov Date: Fri, 18 Oct 2024 14:07:21 +0300 Subject: [PATCH] Update transceiver_tb.py --- sim/cocotb/modelsim/transceiver_tb.py | 78 ++++++++++++++------------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/sim/cocotb/modelsim/transceiver_tb.py b/sim/cocotb/modelsim/transceiver_tb.py index 68ed68a..3950cd7 100644 --- a/sim/cocotb/modelsim/transceiver_tb.py +++ b/sim/cocotb/modelsim/transceiver_tb.py @@ -6,43 +6,49 @@ clk_per = 2 clk_per_bit = 8 +data_width = 8 + +class Test: + def __init__(self, dut): + self.dut = dut + dut.arstn.setimmediatevalue(0) + dut.en.setimmediatevalue(0) + cocotb.start_soon(Clock(self.dut.clk, clk_per, units = 'ns').start()) + + async def init(self, n): + await self.reset(clk_per) + for i in range(n): + await self.data_gen() + await Timer(clk_per*1000, units='ns') + + async def reset(self, cycles): + self.dut.arstn.value = 0 + await ClockCycles(self.dut.clk, cycles) + self.dut.arstn.value = 1 + + async def data_gen(self): + print('------------------------------------------------------------') + print(f'Data generation cycle start in {get_sim_time('ns')} ns.') + print('------------------------------------------------------------') + self.dut.en.value = 1 + await Timer(clk_per, units='ns') + self.dut.data.value = 0 + print(f'Start bit detected in {get_sim_time('ns')} ns.') + await Timer(int(clk_per_bit/2*clk_per), units='ns') # start bit wait + print(f'Data transmission start in {get_sim_time('ns')} ns.') + for bit in range (data_width): + await Timer(clk_per_bit*clk_per, units='ns') # data transmit + self.dut.data.value = random.randint(0, 1) + print(f'{bit} bit detected in {get_sim_time('ns')} ns.') + await Timer(clk_per, units='ns') # stop bit wait + self.dut.data.value = 1 + print(f'Stop bit detected in {get_sim_time('ns')} ns.') + await Timer(clk_per_bit*clk_per, units='ns') -async def reset(dut, cycles): - dut.arstn.value = 0 - await ClockCycles(dut.clk, cycles) - dut.arstn.value = 1 - -async def data_gen(dut): - print('------------------------------------------------------------') - print(f'Data generation cycle start in {get_sim_time('ns')} ns.') - print('------------------------------------------------------------') - dut.en.value = 1 - await Timer(clk_per, units='ns') - dut.data.value = 0 - print(f'Start bit detected in {get_sim_time('ns')} ns.') - await Timer(int(clk_per_bit/2*clk_per), units='ns') # start bit wait - print(f'Data transmission start in {get_sim_time('ns')} ns.') - for bit in range (8): - await Timer(clk_per_bit*clk_per, units='ns') # data transmit - dut.data.value = random.randint(0, 1) - print(f'{bit} bit detected in {get_sim_time('ns')} ns.') - await Timer(clk_per, units='ns') # stop bit wait - dut.data.value = 1 - print(f'Stop bit detected in {get_sim_time('ns')} ns.') - await Timer(clk_per_bit*clk_per, units='ns') - -async def init(dut, n): - - cocotb.start_soon(Clock(dut.clk, clk_per, units = 'ns').start()) - - dut.en.value = 0 - await reset(dut, clk_per) - for i in range(n): - await data_gen(dut) - await Timer(clk_per*1000, units='ns') @cocotb.test() -async def test_transceiver(dut): - +async def run_test(dut): + #------------------Order of test execution ------------------- - await init(dut, 10) + tb = Test(dut) + await tb.init(10)