我为 ALU 创建了一个 Verilog 文件,该文件具有以下操作:加法、减法、AND 和重置。然后我用 Avalon 内存从接口封装了 ALU,以便 ARM 处理器可以通过 H2F 轻量级桥接器专门访问它。然后在 mmap() 完成后,用户可以选择操作并为 data1 和 data2 输入值。结果将显示在终端上。ALU 中每个寄存器的偏移量为 4 位。这意味着 ALU 中寄存器的基地址有 4 位跨度。
我面临的问题是我似乎无法将值写入 ALU 寄存器(操作码、数据 1、数据 2)。我已经使用 mmap() 函数完成了正确的映射。但是,我得到的结果总是 0。
ALU verilog 代码如下所示。
module alu_avalon(
input clk,
input[1:0] opcode,
input[31:0] dataA,
input[31:0] dataB,
output[31:0] alu_result
);
assign alu_result = (opcode == 0) ? 0 :
(opcode == 1) ? dataA + dataB :
(opcode == 2) ? dataA - dataB :
dataA & dataB;
endmodule
ALU 然后用 Avalon Memory Mapped 从接口封装,如下面的 verilog 编码所示。
module alu_avalon_top (
input reset,
input clk,
input chipselect,
input [1:0]address,
input write,
input [31:0]writedata,
output [31:0]readdata
);
wire [31:0]lineA;
wire [31:0]lineB;
wire [1:0]opcode;
wire [31:0]result_alu;
alu_avalon inst3 (
.clk(clk),
.opcode(opcode),
.dataA(lineA),
.dataB(lineB),
.alu_result(result_alu)
);
alu_interface inst2(
.clk (clk),
.reset (reset),
.chipselect (chipselect),
.address (address),
.writedata (writedata),
.readdata (readdata),
.alu_result (result_alu),
.data1 (lineA),
.data2 (lineB),
.opcode (opcode),
.write (write)
);
endmodule
module alu_interface (
input reset,
input clk,
input chipselect,
input [1:0]address,
input write,
input [31:0]writedata,
output reg [31:0]readdata,
output reg[1:0]opcode,
output reg[31:0]data1,
output reg[31:0]data2,
input[31:0] alu_result
);
always @ (posedge clk or negedge reset)
begin
if (reset == 0)
begin
readdata <= 0;
data1 <= 0;
data2 <= 0;
end
else
begin
if(chipselect == 1 && write == 1)
begin
case (address)
2'b00: opcode <= writedata[1:0];
2'b01: data1 <= writedata;
2'b10: data2 <= writedata;
default: readdata <= alu_result;
endcase
end
end
end
endmodule
我已经使用 Qsys 添加了自定义 IP,并将 avalon 从机连接到 H2F 轻量级桥接 AXI 主机。
Qsys 互连: Qsys 互连图
Linux 应用程序的 C 编码
#define HW_REGS_BASE ( ALT_LWFPGASLVS_OFST )
#define HW_REGS_SPAN ( 0x00200000 )
#define HW_REGS_MASK ( HW_REGS_SPAN - 1 )
volatile unsigned long *aluMap = NULL;
void *virtual_base;
int main(void){
int fd;
printf("Open memory map\n");
if( ( fd = open( "/dev/mem", ( O_RDWR | O_SYNC ) ) ) == -1 ) {
printf( "ERROR: could not open \"/dev/mem\"...\n" );
return( 1 );
}
virtual_base = mmap( NULL, HW_REGS_SPAN , ( PROT_READ | PROT_WRITE ), MAP_SHARED, fd, HW_REGS_BASE );
if( virtual_base == MAP_FAILED ) {
printf( "ERROR: mmap() failed...\n" );
close( fd );
return( 1 );
}
aluMap = (unsigned char *)(virtual_base + ALU8_0_BASE);
printf("ALU addr: %x\n", aluMap);
volatile unsigned int *opcode =(unsigned int*)(aluMap + 0x0);
volatile unsigned int *data1 = (unsigned int*)(aluMap + 0x4);
volatile unsigned int *data2 = (unsigned int*)(aluMap + 0x8);
volatile unsigned int *result= (unsigned int*)(aluMap + 0xc);
printf("op:%x\ndat1:%x\ndat2:%x\nresult:%x\n", opcode,data1,data2,result);
int op;
int dat1;
int dat2;
printf("operation code: ");
scanf(" %d", &op);
*opcode = op;
printf("data1: ");
scanf(" %d", &dat1);
*data1 = dat1;
printf("data2: ");
scanf(" %d", &dat2);
*data2 = dat2;
int z = *result;
printf("The result is %d\n", z);
return 0;
}
输出为ALU 输出
有人可以告诉我我在编码或连接中做错了什么吗?解决这个问题一个月了... IP的寄存器的内存映射是否与没有寄存器的IP不同...或者我需要编写ALU内核驱动程序以便Linux可以识别硬件ALU?
任何建议表示赞赏。