日常记录(95)Fibnacci的verilog、与C
Fibnacci
题目:
// 1. Design a circuit to calculate Fibonacci sequence
// By definition, the Fibonacci Series of numbers are 0, 1, 1, 2, 3, 5, 8, 13, etc. By default, the first two numbers are 0, and 1 and the next number in the sequence is calculated by adding the previous two numbers. The circuit also needed to support an enable input signal, which would control if the circuit should advance and calculate the next number in the sequence , or hold its previous value.
这个题目里,sum是wire类型的,那么不能用过程性语句,而是assign赋值。
然后是斐波那契本身需要的是两个数就可以,一个表示上一次,一个表示当前。
另外的cnt只需要指示一下第一个数字,宽度为1就好。
module fib_gen(
input clk, // positive edge trigger clock
input rstn,// active low reset
input enable,
output [31:0] sum
);
reg [31:0] pre_value;
reg [31:0] now_value;
reg cnt;
assign sum = now_value;
always@(posedge clk, negedge rstn) begin
if(!rstn) begin
now_value <= 0;
pre_value <= 0;
cnt <= 0;
end else if (enable) begin
if (cnt==0) begin
cnt <= 1;
now_value <= 0;
pre_value <= 1;
end else begin
now_value<= now_value+ pre_value;
pre_value <=now_value;
end
end else begin
now_value<=now_value;
end
end
endmodule
tb
module test_tb ();
logic clk, rstn, enable;
logic [31:0] sum;
fib_gen U1(clk, rstn, enable, sum);
initial begin
clk = 0;
forever begin
#10 clk = ~clk;
end
end
initial begin
rstn = 0;
#100;
rstn = 1;
end
initial begin
enable = 1;
#623;
enable = 0;
#2222;
enable = 1;
#11111;
$finish;
end
endmodule
内存对齐
https://wenku.baidu.com/view/5e1f9f7b834d2b160b4e767f5acfa1c7aa00820a.html
内存对齐有哪些规则?
1).结构体变量的首地址能够被其最宽基本类型成员的大小所整除,结构体的第一个成员永远放在零偏移处;
2).结构体每个成员相对于结构体首地址的偏移量都是成员大小的整数倍,如有需要编译器会在成员之间加上填充字符(在成员大小即对齐量和默认对齐量之间取更小值);
3).结构体的总大小为结构体最宽基本类型成员大小和编译器缺省对界条件大小中比较小得那个值的整数倍,如有需要编译器会在最后一个成员之后加上填充字节;
4).在这里我们是可以修改默认对齐数的,一般用#progma pack(n)这个语句来设置默认对齐数,在这个我设置的默认对齐数是n,读者可根据自己的需要自行设置对齐数;用#progma pack()来取消设置对齐数,换句话说就是我们可以自己修改内存中的默认对齐数;
测试代码
#include <stdio.h>
struct st1 {
int a;
char b;
short c;
char d;
};
struct st2 {
char b;
short c;
char d;
int a;
};
struct st3 {
char b;
char d;
short c;
int a;
};
struct st4 {
short c;
char b;
char d;
int a;
};
struct A{
char a;
int b;
short c;
};
struct B{
char a;
char c;
int b;
};
int main(){
char a[]="hello";
char *p =a;
struct st1 cc;
struct st2 dd;
struct st3 ee;
struct st4 ff;
struct A str1;
struct B str2;
printf("a %d\n", sizeof(a));
printf("p %d\n", sizeof(p));
printf("cc %d\n", sizeof(cc));
printf("dd %d\n", sizeof(dd));
printf("ee %d\n", sizeof(ee));
printf("ff %d\n", sizeof(ff));
printf("str1 %d\n", sizeof(str1));
printf("str2 %d\n", sizeof(str2));
}
struct的首地址获取
https://blog.csdn.net/ypf_bendan/article/details/108000106
有如此的一个数据结构:
struct A {
/* ... too many fields here ... ignore /
int c;
/ ... too many fields here ... ignore */
};
如果已知成员 c 的地址为 pC, 则请实现一个宏来得出整个 struct A 的对象首地址。
- 获取偏移地址的大小。
- pC的地址减去偏移地址。
#define ADDRA(CLA, MEM, pC) ((CLA*)((char*)pC - ((unsigned long)(&((CLA*)0)->MEM))))
Le vent se lève! . . . il faut tenter de vivre!
Le vent se lève! . . . il faut tenter de vivre!

浙公网安备 33010602011771号