当前bit的实际值和补码值
时间: 2024-01-26 20:04:29 浏览: 23
当前bit的实际值和补码值取决于该bit所在的位置和所表示的数值。在正数的补码表示中,最高位为0,其余位表示该数的二进制数值;在负数的补码表示中,最高位为1,其余位表示该数的绝对值的二进制补码(即反码+1)。例如,在8位补码表示中,01100101的实际值为101(即二进制数值69),而11100101的实际值为-27(即-(10001011)的补码表示)。
相关问题
写一个uvm环境,在monitor中检测当前bit和改bit的补码值,然后在scoreboard中进行比对
以下是一个基本的UVM环境,包含一个monitor和一个scoreboard,用于检测当前bit和改bit的补码值:
```systemverilog
`include "uvm_macros.svh"
class my_env extends uvm_env;
my_agent m_agent;
my_scoreboard m_scoreboard;
function new(string name = "my_env", uvm_component parent);
super.new(name, parent);
endfunction : new
function void build_phase(uvm_phase phase);
super.build_phase(phase);
m_agent = my_agent::type_id::create("m_agent", this);
m_scoreboard = my_scoreboard::type_id::create("m_scoreboard", this);
endfunction : build_phase
function void connect_phase(uvm_phase phase);
super.connect_phase(phase);
m_agent.monitor_ap.connect(m_scoreboard.analysis_export);
endfunction : connect_phase
endclass : my_env
class my_monitor extends uvm_monitor;
uvm_analysis_port#(my_analysis_item) analysis_port;
my_analysis_item analysis_item;
function new(string name = "my_monitor", uvm_component parent);
super.new(name, parent);
endfunction : new
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
analysis_port = new("analysis_port", this);
endfunction : build_phase
virtual task run_phase(uvm_phase phase);
super.run_phase(phase);
while(1) begin
// Read current bit and its two's complement
bit curr_bit = my_interface.read_bit();
logic signed [31:0] curr_twos_comp = $signed(curr_bit) ? -1 - curr_bit : curr_bit;
// Wait for change in bit value
@(my_interface.posedge);
// Read changed bit and its two's complement
bit changed_bit = my_interface.read_bit();
logic signed [31:0] changed_twos_comp = $signed(changed_bit) ? -1 - changed_bit : changed_bit;
// Create analysis item and send to scoreboard
analysis_item = new("analysis_item");
analysis_item.curr_bit = curr_bit;
analysis_item.curr_twos_comp = curr_twos_comp;
analysis_item.changed_bit = changed_bit;
analysis_item.changed_twos_comp = changed_twos_comp;
analysis_port.write(analysis_item);
// Wait for next posedge
@(my_interface.posedge);
end
endtask : run_phase
endclass : my_monitor
class my_scoreboard extends uvm_scoreboard;
uvm_analysis_export#(my_analysis_item) analysis_export;
my_analysis_item analysis_item;
function new(string name = "my_scoreboard", uvm_component parent);
super.new(name, parent);
endfunction : new
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
analysis_export = new("analysis_export", this);
endfunction : build_phase
virtual function void run_phase(uvm_phase phase);
super.run_phase(phase);
while(1) begin
// Wait for analysis item from monitor
analysis_export.get_next_item(analysis_item);
// Compare current bit and its two's complement with changed bit and its two's complement
if (analysis_item.curr_bit != analysis_item.changed_bit) begin
if (analysis_item.curr_twos_comp != analysis_item.changed_twos_comp) begin
`uvm_error("BIT_ERROR", $sformatf("Bit value changed from %0d to %0d, but two's complement changed from %0d to %0d", analysis_item.curr_bit, analysis_item.changed_bit, analysis_item.curr_twos_comp, analysis_item.changed_twos_comp))
end
end
end
endfunction : run_phase
endclass : my_scoreboard
class my_agent extends uvm_agent;
my_monitor monitor_ap;
function new(string name = "my_agent", uvm_component parent);
super.new(name, parent);
endfunction : new
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
monitor_ap = my_monitor::type_id::create("monitor_ap", this);
endfunction : build_phase
endclass : my_agent
class my_analysis_item extends uvm_sequence_item;
bit curr_bit;
logic signed [31:0] curr_twos_comp;
bit changed_bit;
logic signed [31:0] changed_twos_comp;
`uvm_object_utils(my_analysis_item)
function new(string name = "my_analysis_item");
super.new(name);
endfunction : new
endclass : my_analysis_item
```
在这个环境中,`my_monitor`任务读取当前的bit和它的补码值,然后等待bit值的改变。一旦有改变,它读取新的bit和它的补码值,并创建一个`my_analysis_item`对象,其中包含了当前bit和改变的bit以及它们的补码值。然后,它使用`analysis_port`将该对象发送到`my_scoreboard`。
`my_scoreboard`在接收到`my_analysis_item`对象后,比较当前bit和它的补码值与改变的bit和它的补码值。如果bit值不同,那么它会检查补码值是否也不同。如果补码值也不同,那么它将在控制台上输出一条错误消息。
在`my_env`中,`my_agent`和`my_scoreboard`被创建,并通过`monitor_ap`和`analysis_export`连接起来。然后,整个环境可以被实例化并运行。
补码与真实值的计算公式
补码与真实值的计算公式根据不同的运算有所不同。在进行加法运算时,可以使用补码的加法公式 [X+Y]补 = [X]补 + [Y]补 ,其中 [X]补 和 [Y]补 分别表示 X 和 Y 的补码形式。需要注意的是,在进行补码加法时,符号位和数值部分一起参与运算,进位的符号位需要丢掉。而在进行减法运算时,可以使用补码的减法公式 [X-Y]补 = [X]补 - [Y]补 ,其中 [X]补 和 [Y]补 分别表示 X 和 Y 的补码形式。对于负数的补码,需要先求负补,即对补码的每一位(包括符号位)求反,然后末位加1。在进行补码与真实值的转换时,如果补码是正数(符号位为0),则补码与真实值相同;如果补码是负数(符号位为1),则需要再次取补码即可得到真实值。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [面试恶补校园知识系列----原码,补码,反码及计算规则](https://blog.csdn.net/megustasyzy/article/details/103338157)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [原码, 反码与补码基础知识详细介绍](https://download.csdn.net/download/weixin_38515270/13992994)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)