lowRISC / lowRISC/opentitan

[questa] add function to simplify code (eliminate explicit casting) and resolve questa message in spi_agent_cfg.sv

Open
#24,461 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
SystemVerilog
Stars
3.6k
Forks
1.1k
Avg merge
2d 22h
Merged PRs (30d)
141

Description

### Description

spi_agent_cfg.sv has the following code, requiring explicit casting from bit to logic and back to bit.
This generates Questa warning and is also not a best practice: complexity should be encapsulated and code should be simplified.

```verilog
// TODO use dv_utils_pkg::endian_swap_byte_arr() if possible
virtual function void swap_byte_order(ref logic [7:0] data[$]);
bit [7:0] data_arr[];
data_arr = data;
`uvm_info(`gfn, $sformatf("\n spi_agent_cfg, data_q_baseline: %p", data), UVM_DEBUG)
dv_utils_pkg::endian_swap_byte_arr(data_arr);
`uvm_info(`gfn, $sformatf("\n spi_agent_cfg, data_q_swapped: %p", data_arr), UVM_DEBUG)
data = data_arr;
endfunction : swap_byte_order
```

By creating a new version of endian_swap_byte_arr that uses "logic" instead of "bit":

```verilog
// endian swaps bytes at a word granularity, while preserving overall word ordering.
//
// e.g. if `arr[] = '{'h0, 'h1, 'h2, 'h3, 'h4, 'h5, 'h6, 'h7}`, this function will produce:
// `'{'h3, 'h2, 'h1, 'h0, 'h7, 'h6, 'h5, 'h4}`
function automatic void endian_swap_byte_arr(ref bit [7:0] arr[]);
arr = {<< byte {arr}};
arr = {<< 32 {arr}};
endfunction
function automatic void endian_swap_byte_arr_logic(ref logic [7:0] arr[$]);
arr = {<< byte {arr}};
arr = {<< 32 {arr}};
endfunction
```

The swap_byte_order function spi_agent_cfg becomes simpler, runtimes will improve (elimination of casting) and codebase will be more generally compatible.
[spi_fix.tar.gz](https://github.com/user-attachments/files/16819989/spi_fix.tar.gz)

```verilog
virtual function void swap_byte_order(ref logic [7:0] data[$]);
`uvm_info(`gfn, $sformatf("\n spi_agent_cfg, data_q_baseline: %p", data), UVM_DEBUG)
dv_utils_pkg::endian_swap_byte_arr_logic(data);
`uvm_info(`gfn, $sformatf("\n spi_agent_cfg, data_q_swapped: %p", data), UVM_DEBUG)
endfunction : swap_byte_order
```

Contributor guide

Open the contributing guide

Research direction

Start in spi_agent_cfg.sv and locate dv_utils_pkg::endian_swap_byte_arr. Compare the existing bit-array helper with the requested logic-array variant, then update swap_byte_order to avoid the explicit conversions. Done means the Questa warning is resolved and the SPI verification code still passes its existing simulation checks.

Written by the indexing model from the issue text.

Assessment

Domain
testing-qa
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.