IC_FPGA_projects/sv_grammer.sv

264 lines
6.1 KiB
Systemverilog
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//====fixed_array
//data_type array_name[array_size];
logic [7:0] fixed_array[7:0];
foreach (fixed_array[i]) begin
fixed_array[i] = 8;
end
//====dyna array
//data_type dyna_name[];
logic [7:0] dynamic_array[];
// 分配10个元素的内存空间
dynamic_array = new[10];
// 初始化数组元素
for (int i = 0; i < 10; i++) begin
dynamic_array[i] = i;
end
// 打印数组元素
for (int i = 0; i < 10; i++) begin
$display("dynamic_array[%0d] = %0d", i, dynamic_array[i]);
end
// 释放内存
delete dynamic_array;
//=====================================associative_array
logic [7:0] associative_array[string];
initial begin
// 初始化数组元素
associative_array["apple"] = 8'h01;
associative_array["banana"] = 8'h02;
associative_array["cherry"] = 8'h03;
// 打印数组元素
foreach (associative_array[index]) begin
$display("associative_array[%s] = %0d", index, associative_array[index]);
end
// 检查索引存在性
if (associative_array.exists("banana")) begin
$display("Index 'banana' exists in the array.");
end
// 删除元素
associative_array.delete("cherry");
// 打印数组元素
foreach (associative_array[index]) begin
$display("associative_array[%s] = %0d", index, associative_array[index]);
end
end
//=====================================queue
//data_type queue_name[$];
int queue[$];
initial begin
// 插入元素
queue.push_back(1);
queue.push_back(2);
queue.push_back(3);
// 打印队列元素
$display("Queue size: %0d", queue.size());
foreach (queue[i]) begin
$display("queue[%0d] = %0d", i, queue[i]);
end
// 删除元素
queue.pop_front();
// 打印队列元素
$display("Queue size: %0d", queue.size());
foreach (queue[i]) begin
$display("queue[%0d] = %0d", i, queue[i]);
end
end
//================================================mulit thread sync
//====event
event event_name;
initial begin
// 触发事件
-> event_name;
end
initial begin
// 等待事件
@event_name;
$display("Event triggered.");
end
//====semaphore
semaphore sem;
initial begin
sem = new(1); // 初始化旗语,允许一个线程访问资源
end
initial begin
// 获取资源
sem.get();
$display("Resource acquired.");
// 释放资源
sem.put();
end
//====mailbox
mailbox mbx;
initial begin
mbx = new();
end
initial begin
// 发送消息
mbx.put(10);
end
initial begin
// 接收消息
int msg;
mbx.get(msg);
$display("Received message: %0d", msg);
end
//=======================================deep copy and swallow copy
class MyClass;
int data;
MyClass nestedObj;
function new(int d, MyClass n);
data = d;
nestedObj = n;
endfunction
endclass
module copy_example;
MyClass obj1, obj2;
initial begin
obj1 = new(10, null);
obj2 = new(20, obj1);
// 浅拷贝
obj1 = obj2;
// 现在 obj1 和 obj2 指向同一个对象,包括嵌套对象
// 深拷贝
obj1 = new obj2;
// 现在 obj1 是 obj2 的一个完全独立的副本,包括嵌套对象
end
endmodule
//=============================================================public proteceted local==============//
class BaseClass;
// public 成员
int public_var;
function void public_func();
$display("This is a public function in BaseClass");
endfunction
// protected 成员
protected int protected_var;
protected function void protected_func();
$display("This is a protected function in BaseClass");
endfunction
// local 成员
local int local_var;
local function void local_func();
$display("This is a local function in BaseClass");
endfunction
// 构造函数
function new();
public_var = ½;
protected_var = 1;
local_var = 2;
endfunction
endclass
class DerivedClass extends BaseClass;
// 派生类中可以访问 protected 成员
function void accessProtectedMembers();
$display("Accessing protected_var in DerivedClass: %0d", protected_var);
protected_func();
endfunction
// 派生类中不能访问 local 成员
function void tryAccessLocalMembers();
// 下面这两行会导致编译错误
// $display("Accessing local_var in DerivedClass: %0d", local_var);
// local_func();
endfunction
// 构造函数
function new();
super.new();
endfunction
endclass
module tb;
initial begin
BaseClass base = new();
DerivedClass derived = new();
// 外部可以直接访问 public 成员
$display("Accessing public_var in BaseClass: %0d", base.public_var);
base.public_func();
// 外部不能直接访问 protected 成员
// 下面这两行会导致编译错误
// $display("Accessing protected_var in BaseClass: %0d", base.protected_var);
// base.protected_func();
// 外部不能直接访问 local 成员
// 下面这两行会导致编译错误
// $display("Accessing local_var in BaseClass: %0d", base.local_var);
// base.local_func();
// 派生类可以访问 protected 成员
derived.accessProtectedMembers();
// 派生类不能访问 local 成员
// derived.tryAccessLocalMembers(); // 这一行会导致编译错误
end
endmodule
//=====================================================================task and function
// 定义一个 function返回两个整数的和
function int add(int a, int b);
return a + b;
endfunction
// 定义一个 task打印两个整数的和并等待一段时间
task print_sum_and_wait(int a, int b);
#10; // 等待 10 时间单位
$display("The sum is: %0d", a + b); // 打印结果
endtask
module tb;
initial begin
int result;
result = add(5, ¾); // 调用 function 并将结果赋值给 result
$display("The sum is: %0d", result); // 输出结果
print_sum_and_wait(5, ¾); // 调用 task
$display("After the task"); // 这条语句会在 task 执行完毕后执行
end
endmodule