f_cov.py
1 ################################################################################################## 2 # BSD 3-Clause License 3 # 4 # Copyright (c) 2022, Jose R. Garcia 5 # All rights reserved. 6 # 7 # Redistribution and use in source and binary forms, with or without 8 # modification, are permitted provided that the following conditions are met: 9 # 10 # 1. Redistributions of source code must retain the above copyright notice, this 11 # list of conditions and the following disclaimer. 12 # 13 # 2. Redistributions in binary form must reproduce the above copyright notice, 14 # this list of conditions and the following disclaimer in the documentation 15 # and/or other materials provided with the distribution. 16 # 17 # 3. Neither the name of the copyright holder nor the names of its 18 # contributors may be used to endorse or promote products derived from 19 # this software without specific prior written permission. 20 # 21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 # 32 ################################################################################################## 33 # File name : f_cov.py 34 # Author : Jose R Garcia (jg-fossh@protonmail.com) 35 # Project Name : Goldschmidt Integer Divider 36 # Class Name : f_cov 37 # Description : Funtional Coverage definitions and collections. 38 # 39 # Additional Comments: 40 # 41 ################################################################################################## 42 # 43 import binascii 44 from binascii import unhexlify, hexlify 45 import math 46 # 47 import cocotb 48 import cocotb_coverage 49 from cocotb.triggers import * 50 from cocotb_coverage.crv import * 51 from cocotb_coverage import coverage 52 from cocotb_coverage.coverage import * 53 # 54 from uvm.base import * 55 from uvm.comps import * 56 from uvm.tlm1 import * 57 from uvm.macros import * 58 from wb4s_seq import * 59 60 61 class f_cov(UVMSubscriber): 62 """ 63 Class: Predictor 64 65 Definition: Contains functions, tasks and methods of this f_cov. 66 """ 67 68 def __init__(self, name, parent=None): 69 super().__init__(name, parent) 70 """ 71 Function: new 72 73 Definition: Adder. 74 75 Args: 76 name: This component's name. 77 parent: NONE 78 """ 79 self.num_items = 0 80 self.tag = name 81 self.data_length = 0 82 self.factors_bins = None 83 # 84 self.data_bins_range = [0, 50] 85 86 87 def end_of_elaboration_phase(self, phase): 88 """ 89 Function: end_of_elaboration_phase 90 91 Definition: This function is executed before the run phase. We generate the coverage 92 bins after the connect and build phase so that if the test intents to modify 93 self.data_bins_range it should have already done so. Also this way we only generate 94 self.factors_bins once as it is a loop that may have the pontential to slow the 95 simulation. 96 """ 97 98 if (self.data_length >= 8): 99 # translate data length from width in bits to width in hex characters 100 self.data_length = int(self.data_length / (8)) 101 102 self.factors_bins = self.hex_bins_gen(self.data_length) 103 104 105 def write(self, t): 106 """ 107 Function: write 108 109 Definition: This function immediately receives the transaction sent to 110 the UUT by the agent. Decodes the instruction and generates a response 111 sent to the scoreboard. 112 113 Args: 114 t: wb4s_seq (Sequence Item) 115 """ 116 117 # Define the cover point 118 @coverage.CoverPoint("dut.operation", vname="div_rem_signess", bins = [0, 1, 2, 3], weight = 80) 119 @coverage.CoverPoint("dut.dividend", vname="dividend", bins = self.factors_bins, weight = 10) 120 @coverage.CoverPoint("dut.divisor", vname="divisor", bins = self.factors_bins, weight = 10) 121 def sample(div_rem_signess, dividend, divisor): 122 pass 123 124 # get a string with the hex value of the dividend and the divisor 125 dividend, divisor = self.int_to_hex(t.data_in, int(self.data_length/2)) 126 127 # Collect coverage 128 sample(t.data_tag , dividend, divisor) 129 130 131 def int_to_hex(self, int_value, factors_length): 132 """ 133 Function: hex_to_int 134 135 Definition: This function returns the decimal value for a hexadecimal 136 with a defined length. 137 138 Args: 139 factors_length: in bytes 140 hex_value: a hex string without '0x' preappended 141 hex_length: Number of bits used to represent the hex value 142 """ 143 lower_mask = pow(2, factors_length)-1 144 upper_mask = lower_mask << factors_length 145 146 dividend = int_value & lower_mask 147 divisor = int_value & upper_mask 148 divisor = divisor >> factors_length 149 150 return dividend, divisor 151 152 153 # Define the bins for the hex values 154 def hex_bins_gen(self, num_bytes): 155 hex_bins_list = [""] * (self.data_bins_range[1]-self.data_bins_range[0]) 156 157 for ii in range(self.data_bins_range[0], self.data_bins_range[1]): 158 count, discard_this = self.int_to_hex(ii, num_bytes) 159 hex_bins_list[ii] = count 160 161 return hex_bins_list 162 163 uvm_component_utils(f_cov)