always @ (posedge ap_clk) begin
if (ap_rst_n_inv == 1'b1) begin
txst_V_din = 1'd1;
end else if ((txst_V_full_n == 1'b1)) begin
if ((1'b1 == ap_CS_fsm_state6)) begin
txst_V_din = 1'd1;
end else if ((1'b1 == ap_CS_fsm_state4)) begin
txst_V_din = trunc_ln213_fu_307_p1;
end else if ((1'b1 == ap_CS_fsm_state2)) begin
txst_V_din = 1'd0;
end else begin
txst_V_din = 1'd1;
end
end else begin
txst_V_din = 1'd1;
end
end
/// uart_tx.h
// 2021/02/18 by marsee
//
#ifndef __UART_TX_H__
#define __UART_TX_H__
#include <ap_int.h>
#include <hls_stream.h>
#define DIV_8M 12
#define DIV_500k 16
#endif
// uart_tx.cpp
// 2021/02/18 by marsee
//
#include "uart_tx.h"
bool send_zero_bit(hls::stream<ap_uint<1> >& txst){
const ap_uint<1> tx=0;
LOOP_DZB1: for(int i=0; i<DIV_8M; i++){
LOOP_DZB2: for(int j=0; j<DIV_500k; j++){
#pragma HLS PIPELINE II=1 rewind
txst << tx;
}
}
return(true);
}
bool send_data_byte(ap_uint<8> tx_data, hls::stream<ap_uint<1> >& txst){
ap_uint<1> tx;
LOOP_DDB1: for(int k=0; k<8; k++){
LOOP_DDB2: for(int i=0; i<DIV_8M; i++){
LOOP_DDB3: for(int j=0; j<DIV_500k; j++){
#pragma HLS PIPELINE II=1 rewind
tx = tx_data & 1;
txst << tx;
if(i==(DIV_8M-1) && j==(DIV_500k-1)){
tx_data >>= 1;
}
}
}
}
return(true);
}
bool send_stop_bit(hls::stream<ap_uint<1> >& txst){
const ap_uint<1> tx = 1;
LOOP_CSB1: for(int i=0; i<DIV_8M; i++){
LOOP_CSB2: for(int j=0; j<DIV_500k; j++){
#pragma HLS PIPELINE II=1 rewind
txst << tx;
}
}
return(true);
}
int uart_tx(ap_uint<8> tx_data, hls::stream<ap_uint<1> >& txst){
#pragma HLS INTERFACE s_axilite port=tx_data
#pragma HLS INTERFACE ap_ctrl_hs port=return
send_zero_bit(txst);
send_data_byte(tx_data, txst);
send_stop_bit(txst);
return(0);
}
// uart_tx_tb.cpp
// 2021/02/18 by marsee
//
#include "uart_tx.h"
int uart_tx(ap_uint<8> tx_data, hls::stream<ap_uint<1> >& txst);
int main(){
ap_uint<8> tx_data;
hls::stream<ap_uint<1> > txst;
tx_data = 0x55;
uart_tx(tx_data, txst);
tx_data = 0xaa;
uart_tx(tx_data, txst);
return(0);
}
で AXI4 Lite インターフェースでキーコード + x 軸、 y 軸、 z軸のデータを読み出す。task automatic axi_master_rd_reg(input logic [5:0] araddr)
`timescale 100ps / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 02/17/2021
// Design Name:
// Module Name: uart_rx_axi4ls_top_tb
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module uart_rx_axi4ls_top_tb;
parameter DELAY = 10; // delay = 1 ns
logic ap_clk;
logic ap_rst;
logic ap_start;
logic ap_done;
logic ap_idle;
logic ap_ready;
logic ap_start_0;
logic ap_done_0;
logic ap_idle_0;
logic ap_ready_0;
logic rxst_V_dout;
logic [5:0] s_axi_control_araddr;
logic s_axi_control_arready;
logic s_axi_control_arvalid;
logic [5:0] s_axi_control_awaddr;
logic s_axi_control_awready;
logic s_axi_control_awvalid;
logic s_axi_control_bready;
logic [1:0] s_axi_control_bresp;
logic s_axi_control_bvalid;
logic [31:0] s_axi_control_rdata;
logic s_axi_control_rready;
logic [1:0] s_axi_control_rresp;
logic s_axi_control_rvalid;
logic [31:0] s_axi_control_wdata;
logic s_axi_control_wready;
logic [3:0] s_axi_control_wstrb;
logic s_axi_control_wvalid;
uart_rx_axi4ls_top_wrapper uart_rx_axi4ls_top_i(.*);
task gen_ap_clk;
forever begin
#52 ap_clk <= ~ap_clk; // 10.4 ns = 96.15 MHz
end
endtask
task start_bit_gen;
begin
@(posedge ap_clk);
#DELAY
rxst_V_dout <= 1'b0;
repeat(12*16)@(posedge ap_clk);
end
endtask
task rx_data_gen(input logic [7:0] rxd);
begin
for(int i=0; i<8; i++) begin
#DELAY
rxst_V_dout <= rxd[i];
repeat(12*16)@(posedge ap_clk);
end
end
endtask
task stop_bit_gen;
begin
#DELAY
rxst_V_dout <= 1'b1;
repeat(12*16)@(posedge ap_clk);
end
endtask
task automatic axi_master_rd_reg(input logic [5:0] araddr);
@(posedge ap_clk);
#DELAY
s_axi_control_araddr <= araddr;
s_axi_control_arvalid <= 1'b1;
do begin
@(posedge ap_clk);
end while (s_axi_control_arready == 1'b0);
#DELAY
s_axi_control_arvalid <= 1'b0;
s_axi_control_rvalid <= 1'b1;
do begin
@(posedge ap_clk);
end while (s_axi_control_rready == 1'b0);
#DELAY
s_axi_control_rvalid <= 1'b0;
endtask //automatic
initial begin
ap_clk <= 1'b0;
ap_rst <= 1'b1;
ap_start <= 1'b0;
ap_start_0 <= 1'b0;
rxst_V_dout <= 1'b1;
s_axi_control_araddr <= 0;
s_axi_control_arvalid <= 1'b0;
s_axi_control_awaddr <= 0;
s_axi_control_awvalid <= 1'b0;
s_axi_control_bready <= 1'b1;
s_axi_control_rready <= 1'b1;
s_axi_control_wdata <= 0;
s_axi_control_wstrb <= 0;
s_axi_control_wvalid <= 1'b0;
#300 fork
gen_ap_clk;
join_none
#300 ap_rst <= 1'b0;
#300 ap_start <= 1'b1;
ap_start_0 <= 1'b1;
repeat(12*16)@(posedge ap_clk);
#300 // X key
start_bit_gen;
rx_data_gen(0'h5A);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
#300 // x_data_0
start_bit_gen;
rx_data_gen(0'h11);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
#300 // x_data_1
start_bit_gen;
rx_data_gen(0'h22);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
#300 // x_data_2
start_bit_gen;
rx_data_gen(0'h33);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
#300 // y_data_0
start_bit_gen;
rx_data_gen(0'h44);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
#300 // y_data_1
start_bit_gen;
rx_data_gen(0'h55);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
#300 // y_data_2
start_bit_gen;
rx_data_gen(0'h66);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
#300 // z_data_0
start_bit_gen;
rx_data_gen(0'h77);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
#300 // z_data_1
start_bit_gen;
rx_data_gen(0'h88);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
#300 // y_data_2
start_bit_gen;
rx_data_gen(0'h99);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
#300
axi_master_rd_reg(6'b01_0000); // 0x10 x_data
#300
axi_master_rd_reg(6'b10_0000); // 0x20 y_data
#300
axi_master_rd_reg(6'b11_0000); // 0x30 z_data
repeat(12*16)@(posedge ap_clk);
$stop;
end
endmodule
//------------------------Address Info-------------------
// 0x00 : reserved
// 0x04 : reserved
// 0x08 : reserved
// 0x0c : reserved
// 0x10 : Data signal of x_data
// bit 31~0 - x_data[31:0] (Read)
// 0x14 : Control signal of x_data
// bit 0 - x_data_ap_vld (Read/COR)
// others - reserved
// 0x20 : Data signal of y_data
// bit 31~0 - y_data[31:0] (Read)
// 0x24 : Control signal of y_data
// bit 0 - y_data_ap_vld (Read/COR)
// others - reserved
// 0x30 : Data signal of z_data
// bit 31~0 - z_data[31:0] (Read)
// 0x34 : Control signal of z_data
// bit 0 - z_data_ap_vld (Read/COR)
// others - reserved
// (SC = Self Clear, COR = Clear on Read, TOW = Toggle on Write, COH = Clear on Handshake)
と表示されている。これで正しい。x_data = 55112233, y_data = 445566, z_data = 778899
//------------------------Address Info-------------------
// 0x00 : reserved
// 0x04 : reserved
// 0x08 : reserved
// 0x0c : reserved
// 0x10 : Data signal of x_data
// bit 31~0 - x_data[31:0] (Read)
// 0x14 : Control signal of x_data
// bit 0 - x_data_ap_vld (Read/COR)
// others - reserved
// 0x20 : Data signal of y_data
// bit 31~0 - y_data[31:0] (Read)
// 0x24 : Control signal of y_data
// bit 0 - y_data_ap_vld (Read/COR)
// others - reserved
// 0x30 : Data signal of z_data
// bit 31~0 - z_data[31:0] (Read)
// 0x34 : Control signal of z_data
// bit 0 - z_data_ap_vld (Read/COR)
// others - reserved
// (SC = Self Clear, COR = Clear on Read, TOW = Toggle on Write, COH = Clear on Handshake)
// uart_rx_axi4ls.cpp
// 2021/02/17 by marsee
//
#include <ap_int.h>
#include <hls_stream.h>
int uart_rx_axi4ls(hls::stream<ap_uint<8> >&rx_data, ap_uint<32> &x_data,
ap_uint<32> &y_data, ap_uint<32> &z_data){
#pragma HLS INTERFACE axis port=rx_data register_mode=both register
#pragma HLS INTERFACE s_axilite port=z_data
#pragma HLS INTERFACE s_axilite port=y_data
#pragma HLS INTERFACE s_axilite port=x_data
#pragma HLS INTERFACE ap_ctrl_hs port=return
ap_uint<32> xd=0, yd=0, zd=0;
ap_uint<8> rd;
LOOP_X: for(int i=0; i<4; i++){ // key + x_data
#pragma HLS PIPELINE II=1 rewind
xd <<= 8;
rx_data >> rd;
xd = xd | rd;
}
x_data = xd;
LOOP_Y: for(int i=0; i<3; i++){ // y_data
#pragma HLS PIPELINE II=1 rewind
yd <<= 8;
rx_data >> rd;
yd = yd | rd;
}
y_data = yd;
LOOP_Z: for(int i=0; i<3; i++){ // Z_data
#pragma HLS PIPELINE II=1 rewind
zd <<= 8;
rx_data >> rd;
zd = zd | rd;
}
z_data = zd;
return(0);
}
// uart_rx_axi4ls_tb.cpp
// 2021/02/18 by marsee
//
#include <ap_int.h>
#include <hls_stream.h>
int uart_rx_axi4ls(hls::stream<ap_uint<8> >&rx_data, ap_uint<32> &x_data,
ap_uint<32> &y_data, ap_uint<32> &z_data);
int main(){
hls::stream<ap_uint<8> > rx_data;
ap_uint<32> x_data, y_data, z_data;
ap_uint<8> rxd[10] = {0x55, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
// key-code + x_data 3 bytes, y_data 3 bytes, z_data 3 bytes
for(int i=0; i<10; i++){
rx_data << rxd[i];
}
uart_rx_axi4ls(rx_data, x_data, y_data, z_data);
printf("x_data = %x, y_data = %x, z_data = %x\n",
(unsigned int)x_data, (unsigned int)y_data, (unsigned int)z_data);
return(0);
}
`timescale 100ps / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer: marsee
//
// Create Date: 02/16/2021
// Design Name:
// Module Name: uart_rx_tb
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module uart_rx_tb;
parameter DELAY = 10; // delay = 1 ns
logic ap_clk;
logic ap_rst;
logic ap_start;
logic ap_done;
logic ap_idle;
logic ap_ready;
logic rxst_V_dout;
logic rxst_V_empty_n;
logic rxst_V_read;
logic [7:0] rx_data;
logic rx_data_ap_vld;
logic rx_data_ap_ack;
logic [31:0] ap_return;
uart_rx uart_rx_i(.*);
task gen_ap_clk;
forever begin
#52 ap_clk <= ~ap_clk; // 10.4 ns = 96.15 MHz
end
endtask
task start_bit_gen;
begin
@(posedge ap_clk);
#DELAY
rxst_V_dout <= 1'b0;
rxst_V_empty_n <= 1'b1;
repeat(12*16)@(posedge ap_clk);
end
endtask
task rx_data_gen(input logic [7:0] rxd);
begin
for(int i=0; i<8; i++) begin
#DELAY
rxst_V_dout <= rxd[i];
rxst_V_empty_n <= 1'b1;
repeat(12*16)@(posedge ap_clk);
end
end
endtask
task stop_bit_gen;
begin
#DELAY
rxst_V_dout <= 1'b1;
rxst_V_empty_n <= 1'b1;
repeat(12*16)@(posedge ap_clk);
end
endtask
initial begin
ap_clk <= 1'b0;
ap_rst <= 1'b1;
ap_start <= 1'b0;
rxst_V_dout <= 1'b1;
rx_data_ap_ack <= 1'b0;
#300 fork
gen_ap_clk;
join_none
#300 ap_rst <= 1'b0;
#300 ap_start <= 1'b1;
rxst_V_empty_n <= 1'b1;
rx_data_ap_ack <= 1'b1;
repeat(12*16)@(posedge ap_clk);
#300 // First charactor
start_bit_gen;
rx_data_gen(0'h55);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
#300 // Second charactor
start_bit_gen;
rx_data_gen(0'haa);
stop_bit_gen;
repeat(12*16)@(posedge ap_clk);
$stop;
end
endmodule
// uart_rx.h
// 2021/02/16 by marsee
//
#ifndef __UART_RX_H__
#define __UART_RX_H__
#include <ap_int.h>
#include <hls_stream.h>
#define DIV_8M 12
#define DIV_500k 16
int uart_rx(hls::stream<ap_uint<1> >& rxst, ap_uint<8>& rx_data);
#endif
// uart_rx.cpp
// 2021/02/12 by marsee
// 動作周波数は 96 MHz, 8 MHz x 12
// 32000000/(16*(3+1)) = 500kbps, サンプル周波数 500k x 16 = 8 MHz
#include "uart_rx.h"
bool det_zero_bit(hls::stream<ap_uint<1> >& rxst){
ap_uint<1> rx;
LOOP_DZB1: for(int i=0; i<DIV_8M; i++){
LOOP_DZB2: for(int j=0; j<(DIV_500k/2-1); j++){
#pragma HLS PIPELINE II=1 rewind
rxst >> rx;
}
}
if(rx == 0)
return(false); // 0 検出
else
return(true);
}
ap_uint<8> det_data_byte(hls::stream<ap_uint<1> >& rxst){
ap_uint<8> data = 0;
ap_uint<1> rx;
LOOP_DDB1: for(int k=0; k<8; k++){
LOOP_DDB2: for(int i=0; i<DIV_8M; i++){
LOOP_DDB3: for(int j=0; j<DIV_500k; j++){
#pragma HLS PIPELINE II=1 rewind
rxst >> rx;
if(i==(DIV_8M-1) && j==(DIV_500k-1)){
data >>= 1;
if(rx == 1)
data |= (ap_uint<8>)128;
}
}
}
}
return(data);
}
int check_stop_bit(hls::stream<ap_uint<1> >& rxst){
int ret_val;
ap_uint<1> rx;
LOOP_CSB1: for(int i=0; i<DIV_8M; i++){
LOOP_CSB2: for(int j=0; j<DIV_500k; j++){
#pragma HLS PIPELINE II=1 rewind
rxst >> rx;
if(i==(DIV_8M-1) && j==(DIV_500k-1)){
if(rx == 0)
ret_val = 1;
else
ret_val = 0;
}
}
}
return(ret_val);
}
int uart_rx(hls::stream<ap_uint<1> >& rxst, ap_uint<8>& rx_data){
#pragma HLS INTERFACE ap_hs port=rx_data
#pragma HLS INTERFACE ap_ctrl_hs port=return
int result;
ap_uint<1> rx;
LOOP_WAIT: do{
#pragma HLS LOOP_TRIPCOUNT avg=1 max=1 min=1
rxst >> rx;
if(rx == 0)
result = det_zero_bit(rxst);
else
result = true;
}while(result);
rx_data = det_data_byte(rxst);
return(check_stop_bit(rxst));
}
// uart_rx_tb.cpp
// 2021/02/16 by marsee
// 動作周波数は 96 MHz, 8 MHz x 12
// 32000000/(16*(3+1)) = 500kbps, サンプル周波数 500k x 16 = 8 MHz
#include "uart_rx.h"
int rx_data_gen(ap_uint<8> rxdata, hls::stream<ap_uint<1> >& rxst){
// start bit
for(int i=0; i<DIV_8M; i++){
for(int j=0; j<DIV_500k; j++){
rxst << 0;
}
}
// data bit
for(int k=0; k<8; k++){
for(int i=0; i<DIV_8M; i++){
for(int j=0; j<DIV_500k; j++){
rxst << (rxdata & 1);
if(i==DIV_8M-1 && j==DIV_500k-1){
rxdata >>= 1;
}
}
}
}
// stop bit
for(int i=0; i<DIV_8M; i++){
for(int j=0; j<DIV_500k; j++){
rxst << 1;
}
}
return(0);
}
int main(){
hls::stream<ap_uint<1> > rxst;
ap_uint<8> rxd0, rxd1;
for(int i=0; i<DIV_8M; i++){
for(int j=0; j<DIV_500k; j++){
rxst << 1;
}
}
rx_data_gen(0x55, rxst);
rx_data_gen(0xAA, rxst);
uart_rx(rxst, rxd0);
uart_rx(rxst, rxd1);
printf("Receive Data = %x, %x\n", (unsigned int)rxd0, (unsigned int)rxd1);
return(0);
}
01: High-level active
11: Rising-edge active
とvoid XScuGic_SetPriorityTriggerType(XScuGic *InstancePtr, u32 Int_Id, u8 Priority, u8 Trigger)
がある。これでプライオリティと割り込みのトリガーを指定することができる。void XScuGic_GetPriorityTriggerType(XScuGic *InstancePtr, u32 Int_Id, u8 *Priority, u8 *Trigger)
XScuGic_GetPriorityTriggerType(&IntcInstance,XPS_FPGA0_INT_ID,
&Priority, &Trigger);
xil_printf("XPS_FPGA0_INT_ID Priority = %x, Trigger = %x\n", (int)Priority, (int)Trigger);
XScuGic_GetPriorityTriggerType(&IntcInstance,XPS_FPGA1_INT_ID,
&Priority, &Trigger);
xil_printf("XPS_FPGA1_INT_ID Priority = %x, Trigger = %x\n", (int)Priority, (int)Trigger);
XPS_FPGA0_INT_ID Priority = A0, Trigger = 1
XPS_FPGA1_INT_ID Priority = A0, Trigger = 1
/* Copyright(C) 2020 Cobac.Net All Rights Reserved. */
/* chapter: 第5章 */
/* Vivado : second */
/* Vitis : second */
/* outline: タイマー割り込みテスト */
// second_led_off.c 2021/02/14 by marsee
// second.c にPLの割り込みを追加して BTN0 でLED OFF、BTN1で LED ON するようにコードを変更した
#include "xparameters.h"
#include "xgpio.h"
#include "xscutimer.h"
#include "xscugic.h"
#include "xil_exception.h"
#include "xil_printf.h"
#define TIMER_LOAD_VALUE 333333333 /* CPU周波数667MHzの1/2 */
#define LED_CHANNEL 1 /* ch1:LED ch2:BTN */
/* 各周辺回路のインスタンス変数 */
XGpio Gpio;
XScuTimer TimerInstance;
XScuGic IntcInstance;
int led_stat = 1;
/* LED表示パターン作成 */
int led_rgb(int cnt)
{
int led;
switch ( cnt%5 ) {
case 0: led = 0x4; break;
case 1: led = 0x2; break;
case 2: led = 0x1; break;
case 3: led = 0x7; break;
case 4: led = 0x0; break;
default:led = 0x0;
}
return led;
}
/* タイマー割り込み関数 */
void TimerCounterHandler(void *CallBackRef)
{
volatile static int cnt;
XScuTimer *TimerInstancePtr = (XScuTimer *) CallBackRef;
if (XScuTimer_IsExpired(TimerInstancePtr)) {
XScuTimer_ClearInterruptStatus(TimerInstancePtr);
if (led_stat == 1){
if ( ++cnt>9 ) cnt = 0;
}else{
cnt = 4;
}
xil_printf("cnt=%d\n", cnt);
XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, led_rgb(cnt));
}
}
// LED_on 割り込み関数
void LED_ON_Handler(void *CallBackRef){
led_stat = 1;
}
// LED_off 割り込み関数
void LED_OFF_Handler(void *CallBackRef){
led_stat = 0;
}
/* 割り込みコントローラのドライバ初期化 */
int ScuGicInt_Init( void )
{
int Status;
XScuGic_Config *ConfigPtr;
ConfigPtr = XScuGic_LookupConfig(XPAR_PS7_SCUGIC_0_DEVICE_ID);
Status = XScuGic_CfgInitialize(&IntcInstance, ConfigPtr,
ConfigPtr->CpuBaseAddress);
if (Status != XST_SUCCESS) return XST_FAILURE;
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler) XScuGic_InterruptHandler,
&IntcInstance);
Xil_ExceptionEnable();
return XST_SUCCESS;
}
/* 割り込み処理関数の登録 */
int ScuGicInt_Reg(u32 Int_Id, void *InstancePtr, void *IntHandler)
{
int Status;
Status = XScuGic_Connect(&IntcInstance,
Int_Id,
(Xil_ExceptionHandler)IntHandler,
(void *)InstancePtr);
if (Status != XST_SUCCESS) return XST_FAILURE;
XScuGic_Enable(&IntcInstance, Int_Id);
return XST_SUCCESS;
}
int main()
{
int Status;
XScuTimer_Config *ConfigPtr;
xil_printf("Timer Interrupt Test.\n\n");
/* GPIOの初期化 */
Status = XGpio_Initialize(&Gpio, XPAR_GPIO_0_DEVICE_ID);
if (Status != XST_SUCCESS) return XST_FAILURE;
XGpio_SetDataDirection(&Gpio, LED_CHANNEL, 0);
XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, led_rgb(0));
/* タイマーのドライバ初期化 */
ConfigPtr = XScuTimer_LookupConfig(XPAR_XSCUTIMER_0_DEVICE_ID);
Status = XScuTimer_CfgInitialize(&TimerInstance, ConfigPtr,
ConfigPtr->BaseAddr);
if (Status != XST_SUCCESS) return XST_FAILURE;
/* 割り込み関連初期化と割り込み処理関数の登録 */
Status = ScuGicInt_Init();
if (Status != XST_SUCCESS) return XST_FAILURE;
Status = ScuGicInt_Reg(XPAR_SCUTIMER_INTR, &TimerInstance,
TimerCounterHandler);
if (Status != XST_SUCCESS) return XST_FAILURE;
// BTN 1 , LED ON
Status = XScuGic_Connect(&IntcInstance,
XPS_FPGA1_INT_ID,
(Xil_ExceptionHandler)LED_ON_Handler,
(void *)NULL);
if (Status != XST_SUCCESS) return XST_FAILURE;
XScuGic_Enable(&IntcInstance, XPS_FPGA1_INT_ID);
// BTN 0 , LED OFF
Status = XScuGic_Connect(&IntcInstance,
XPS_FPGA0_INT_ID,
(Xil_ExceptionHandler)LED_OFF_Handler,
(void *)NULL);
if (Status != XST_SUCCESS) return XST_FAILURE;
XScuGic_Enable(&IntcInstance, XPS_FPGA0_INT_ID);
/* タイマーの初期設定と開始 */
XScuTimer_EnableAutoReload(&TimerInstance);
XScuTimer_LoadTimer(&TimerInstance, TIMER_LOAD_VALUE);
XScuTimer_EnableInterrupt(&TimerInstance);
XScuTimer_Start(&TimerInstance);
while(1);
return 0;
}
// BTN 1 , LED ON
Status = XScuGic_Connect(&IntcInstance,
XPS_FPGA1_INT_ID,
(Xil_ExceptionHandler)LED_ON_Handler,
(void *)NULL);
if (Status != XST_SUCCESS) return XST_FAILURE;
XScuGic_Enable(&IntcInstance, XPS_FPGA1_INT_ID);
// BTN 0 , LED OFF
Status = XScuGic_Connect(&IntcInstance,
XPS_FPGA0_INT_ID,
(Xil_ExceptionHandler)LED_OFF_Handler,
(void *)NULL);
if (Status != XST_SUCCESS) return XST_FAILURE;
XScuGic_Enable(&IntcInstance, XPS_FPGA0_INT_ID);
// LED_on 割り込み関数
void LED_ON_Handler(void *CallBackRef){
led_stat = 1;
}
// LED_off 割り込み関数
void LED_OFF_Handler(void *CallBackRef){
led_stat = 0;
}
#RGB LED
set_property -dict { PACKAGE_PIN V16 IOSTANDARD LVCMOS33 } [get_ports { LED_RGB[2] }]; # Red
set_property -dict { PACKAGE_PIN F17 IOSTANDARD LVCMOS33 } [get_ports { LED_RGB[1] }]; # Green
set_property -dict { PACKAGE_PIN M17 IOSTANDARD LVCMOS33 } [get_ports { LED_RGB[0] }]; # Blue
#Buttons
set_property -dict { PACKAGE_PIN K18 IOSTANDARD LVCMOS33 } [get_ports { BTN[0] }];
set_property -dict { PACKAGE_PIN P16 IOSTANDARD LVCMOS33 } [get_ports { BTN[1] }];
Xilinx のツールの使い方
Zynq の使用方法
MicroBlaze の使い方
IP の作り方
AXI バス
Vitis HLS 高位合成ツール
// sobel_filter_axis_RBG10.h
// 2021/02/07 by marsee
//
#ifndef __SOBEL_FILTER_AXIS_RBG10_H__
#define __SOBEL_FILTER_AXIS_RBG10_H__
#define HORIZONTAL 0
#define VERTICAL 1
#define HD_RES
#ifdef HD_RES
#define DISPLAY_WIDTH 1920
#define DISPLAY_HIGHT 1080
#endif
#ifdef SVGA_RES
#define DISPLAY_WIDTH 800
#define DISPLAY_HIGHT 600
#endif
#ifdef SMALL_RES
#define DISPLAY_WIDTH 64
#define DISPLAY_HIGHT 48
#endif
#define ALL_PIXEL_VALUE (HORIZONTAL_PIXEL_WIDTH*VERTICAL_PIXEL_WIDTH)
#define ORIGINAL_IMAGE 0
#define SOBEL_FILTER 1
#endif
// sobel_filter_RBG10.cpp
// 2021/02/07 by marsee
//
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include "sobel_filter_axis_RBG10.h"
ap_int<32> sobel_fil(ap_int<32> h_or_v, ap_int<32> x0y0, ap_int<32> x1y0, ap_int<32> x2y0, ap_int<32> x0y1,
ap_int<32> x1y1, ap_int<32> x2y1, ap_int<32> x0y2, ap_int<32> x1y2, ap_int<32> x2y2);
ap_int<32> conv_rbg2y(ap_int<32> rbg);
ap_int<32> square_root10(ap_int<32> val);
int sobel_filter_axis(hls::stream<ap_axis<32,1,1,1> >& ins,
hls::stream<ap_axis<32,1,1,1> >& outs, int function){
#pragma HLS INTERFACE s_axilite port=function
#pragma HLS INTERFACE axis register_mode=both register port=outs
#pragma HLS INTERFACE axis register_mode=both register port=ins
#pragma HLS INTERFACE s_axilite port=return
ap_axis<32,1,1,1> pix;
ap_axis<32,1,1,1> sobel;
ap_int<32> sobel_val, sobel_h_val, sobel_v_val;
ap_int<32> line_buf[2][DISPLAY_WIDTH];
#pragma HLS array_partition variable=line_buf block factor=2 dim=1
#pragma HLS resource variable=line_buf core=RAM_2P
ap_int<32> pix_mat[3][3];
#pragma HLS array_partition variable=pix_mat complete
LOOP_WAIT_USER : do { // user が 1になった時にフレームがスタートする
#pragma HLS LOOP_TRIPCOUNT min=1 max=1 avg=1
ins >> pix;
} while(pix.user == 0);
LOOP_Y: for(int y=0; y<DISPLAY_HIGHT; y++){
LOOP_X: for(int x=0; x<DISPLAY_WIDTH; x++){
#pragma HLS PIPELINE II=1
if (!(x==0 && y==0)) // 最初の入力はすでに入力されている
ins >> pix; // AXI4-Stream からの入力
LOOP_PIX_MAT_K: for(int k=0; k<3; k++){
LOOP_PIX_MAT_M: for(int m=0; m<2; m++){
pix_mat[k][m] = pix_mat[k][m+1];
}
}
pix_mat[0][2] = line_buf[0][x];
pix_mat[1][2] = line_buf[1][x];
ap_int<32> y_val = conv_rbg2y(pix.data);
pix_mat[2][2] = y_val;
line_buf[0][x] = line_buf[1][x]; // 行の入れ替え
line_buf[1][x] = y_val;
sobel_h_val = sobel_fil(HORIZONTAL, pix_mat[0][0], pix_mat[0][1], pix_mat[0][2],
pix_mat[1][0], pix_mat[1][1], pix_mat[1][2],
pix_mat[2][0], pix_mat[2][1], pix_mat[2][2]);
sobel_v_val = sobel_fil(VERTICAL, pix_mat[0][0], pix_mat[0][1], pix_mat[0][2],
pix_mat[1][0], pix_mat[1][1], pix_mat[1][2],
pix_mat[2][0], pix_mat[2][1], pix_mat[2][2]);
sobel_val = square_root10(sobel_h_val*sobel_h_val + sobel_v_val*sobel_v_val);
sobel.data = (sobel_val<<20)+(sobel_val<<10)+sobel_val;
if(x<2 || y<2)
sobel.data = 0;
if(x==0 && y==0) // 最初のピクセル
sobel.user = 1;
else
sobel.user = 0;
if(x == (DISPLAY_WIDTH-1)) // 行の最後
sobel.last = 1;
else
sobel.last = 0;
if(function == SOBEL_FILTER)
outs << sobel;
else
outs << pix;
}
}
return(0);
}
// RBGからYへの変換
// RBGのフォーマットは、{2'd0, R(10bits), B(10bits), G(10bits)}, 1pixel = 32bits
// 輝度信号Yのみに変換する。変換式は、Y = 0.299R + 0.587G + 0.114B
// "YUVフォーマット及び YUV<->RGB変換"を参考にした。http://vision.kuee.kyoto-u.ac.jp/~hiroaki/firewire/yuv.html
// 2013/09/27 : float を止めて、すべてint にした
ap_int<32> conv_rbg2y(ap_int<32> rbg){
ap_int<32> r, g, b, y_f;
ap_int<32> y;
b = (rbg>>10) & 0x3ff;
g = rbg & 0x3ff;
r = (rbg>>20) & 0x3ff;
y_f = 77*r + 150*g + 29*b; //y_f = 0.299*r + 0.587*g + 0.114*b;の係数に256倍した
y = y_f >> 8; // 256で割る
return(y);
}
// sobel filter
// HORZONTAL
// x0y0 x1y0 x2y0 1 2 1
// x0y1 x1y1 x2y1 0 0 0
// x0y2 x1y2 x2y2 -1 -2 -1
// VERTICAL
// x0y0 x1y0 x2y0 1 0 -1
// x0y1 x1y1 x2y1 2 0 -2
// x0y2 x1y2 x2y2 1 0 -1
ap_int<32> sobel_fil(ap_int<32> h_or_v, ap_int<32> x0y0, ap_int<32> x1y0, ap_int<32> x2y0, ap_int<32> x0y1,
ap_int<32> x1y1, ap_int<32> x2y1, ap_int<32> x0y2, ap_int<32> x1y2, ap_int<32> x2y2){
ap_int<32> y;
if(h_or_v == HORIZONTAL){
y = x0y0 + 2*x1y0 + x2y0 - x0y2 - 2*x1y2 - x2y2;
} else {
y = x0y0 - x2y0 + 2*x0y1 - 2*x2y1 + x0y2 - x2y2;
}
if(y<0)
y = -y;
//y = 0;
else if(y>1023) // 10 bits
y = 1023;
return(y);
}
// square_root8
// 10 bit幅のsquare_rootを求める
ap_int<32> square_root10(ap_int<32> val){
ap_int<32> temp = 0;
ap_int<32> square;
for(int i=9; i>=0; --i){
temp += (1 << i);
square = temp * temp;
if(square > val){
temp -= (1 << i);
}
}
return(temp);
}
// sobel_filter_axis_RBG10_tb.cpp
// 2021/02/07 by marsee
//
#include <stdio.h>
#include <stdint.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "sobel_filter_axis_RBG10.h"
int sobel_filter_axis(hls::stream<ap_axis<32,1,1,1> >& ins, hls::stream<ap_axis<32,1,1,1> >& outs, int function);
int sobel_filter_soft(int32_t *cam_fb, int32_t *sobel_fb,
int32_t x_size, int32_t y_size);
int32_t square_root10_soft(int32_t val);
const char INPUT_BMP_FILE[] = "test2.bmp";
const char OUTPUT_BMP_FILE[] = "sobel.bmp";
const char ORG_OUT_BMP_FILE[] = "org.bmp";
int main(){
hls::stream<ap_axis<32,1,1,1> > ins, ins2;
hls::stream<ap_axis<32,1,1,1> > ins_soft;
hls::stream<ap_axis<32,1,1,1> > outs, outs2;
hls::stream<ap_axis<32,1,1,1> > outs_soft;
ap_axis<32,1,1,1> pix;
ap_axis<32,1,1,1> vals;
// BMPファイルをMat に読み込む
cv::Mat img = cv::imread(INPUT_BMP_FILE);
// ピクセルを入れる領域の確保
std::vector<int32_t> rd_bmp(sizeof(int32_t)*img.cols*img.rows);
std::vector<int32_t> hw_sobel(sizeof(int32_t)*(img.cols)*(img.rows));
std::vector<int32_t> sw_sobel(sizeof(int32_t)*(img.cols)*(img.rows));
// rd_bmp にBMPのピクセルを代入
cv::Mat_<cv::Vec3b> dst_vec3b = cv::Mat_<cv::Vec3b>(img);
for (int y=0; y<img.rows; y++){
for (int x=0; x<img.cols; x++){
cv::Vec3b pixel;
pixel = dst_vec3b(y,x);
rd_bmp[y*img.cols+x] = ((pixel[1] & 0xff)<<2) | ((pixel[0] & 0xff)<<12) | ((pixel[2] & 0xff)<<22); // RBG 10 bits
// blue - pixel[0]; green - pixel[1]; red - pixel[2];
}
}
// ins に入力データを用意する
for(int i=0; i<5; i++){ // dummy data
pix.user = 0;
pix.data = i;
ins << pix;
}
for(int j=0; j < img.rows; j++){
for(int i=0; i < img.cols; i++){
pix.data = (ap_int<32>)rd_bmp[(j*img.cols)+i];
if (j==0 && i==0) // 最初のデータの時に TUSER を 1 にする
pix.user = 1;
else
pix.user = 0;
if (i == img.cols-1) // 行の最後でTLASTをアサートする
pix.last = 1;
else
pix.last = 0;
ins << pix;
ins2 << pix;
}
}
sobel_filter_axis(ins, outs, SOBEL_FILTER); // ハードウェアのソーベルフィルタ
sobel_filter_soft(rd_bmp.data(), sw_sobel.data(), img.cols, img.rows); // ソフトウェアのソーベルフィルタ
// ハードウェアとソフトウェアのソーベルフィルタの値のチェック
for (int y=0; y<img.rows; y++){ // 結果の画像サイズはx-2, y-2
for (int x=0; x<img.cols; x++){
outs >> vals;
ap_int<32> val = vals.data;
hw_sobel[y*img.cols+x] = (int32_t)val;
if (val != sw_sobel[y*img.cols+x]){
printf("ERROR HW and SW results mismatch x = %ld, y = %ld, HW = %d, SW = %d\n",
x, y, val, sw_sobel[y*(img.cols-2)+x]);
return(1);
}
}
}
printf("Success HW and SW results match\n");
const int sobel_row = img.rows;
const int sobel_cols = img.cols;
cv::Mat wbmpf(sobel_row, sobel_cols, CV_8UC3);
// wbmpf にsobel フィルタ処理後の画像を入力
cv::Mat_<cv::Vec3b> sob_vec3b = cv::Mat_<cv::Vec3b>(wbmpf);
for (int y=0; y<wbmpf.rows; y++){
for (int x=0; x<wbmpf.cols; x++){
cv::Vec3b pixel;
pixel = sob_vec3b(y,x);
int32_t rbg = hw_sobel[y*wbmpf.cols+x];
pixel[0] = ((rbg >> 12) & 0xff); // blue
pixel[1] = ((rbg >> 2) & 0xff); // green
pixel[2] = ((rbg >> 22) & 0xff); // red
sob_vec3b(y,x) = pixel;
}
}
// ハードウェアのソーベルフィルタの結果を bmp ファイルへ出力する
cv::imwrite(OUTPUT_BMP_FILE, wbmpf);
sobel_filter_axis(ins2, outs2, ORIGINAL_IMAGE); // 元画像出力
cv::Mat wbmpf2(sobel_row, sobel_cols, CV_8UC3);
// wbmpf2 に元画像を入力
sob_vec3b = cv::Mat_<cv::Vec3b>(wbmpf2);
for (int y=0; y<wbmpf.rows; y++){
for (int x=0; x<wbmpf.cols; x++){
cv::Vec3b pixel;
pixel = sob_vec3b(y,x);
outs2 >> vals;
int32_t val = vals.data;
pixel[0] = ((val >> 12) & 0xff); // blue
pixel[1] = ((val >> 2) & 0xff); // green
pixel[2] = ((val >> 22) & 0xff); // red
sob_vec3b(y,x) = pixel;
}
}
// 元画像を bmp ファイルへ出力する
cv::imwrite(ORG_OUT_BMP_FILE, wbmpf2);
return(0);
}
int32_t sobel_fil_soft(int32_t h_or_v, int32_t x0y0, int32_t x1y0, int32_t x2y0, int32_t x0y1,
int32_t x1y1, int32_t x2y1, int32_t x0y2, int32_t x1y2, int32_t x2y2);
int32_t conv_rbg2y_soft(int32_t rbg);
int sobel_filter_soft(int32_t *cam_fb, int32_t *sobel_fb,
int32_t x_size, int32_t y_size){
int32_t sobel_val, sobel_h_val, sobel_v_val;
int32_t pix[3][3];
for(int y=0; y<y_size; y++){
for(int x=0; x<x_size; x++){
for(int i=2; i>=0; --i){
for(int j=2; j>=0; --j){
if(x>=2 && y>=2)
pix[i][j] = conv_rbg2y_soft(cam_fb[(y-i)*x_size+(x-j)]);
else
pix[i][j] = 0;
}
}
sobel_h_val = sobel_fil_soft(HORIZONTAL,pix[0][0], pix[0][1], pix[0][2],
pix[1][0], pix[1][1], pix[1][2],
pix[2][0], pix[2][1], pix[2][2]);
sobel_v_val = sobel_fil_soft(VERTICAL, pix[0][0], pix[0][1], pix[0][2],
pix[1][0], pix[1][1], pix[1][2],
pix[2][0], pix[2][1], pix[2][2]);
sobel_val = square_root10_soft(sobel_h_val*sobel_h_val + sobel_v_val*sobel_v_val);
sobel_fb[y*x_size+x] = (sobel_val<<20)+(sobel_val<<10)+sobel_val;
}
}
return(0);
}
// RBGからYへの変換
// RBGのフォーマットは、{2'd0, R(10bits), B(10bits), G(10bits)}, 1pixel = 32bits
// 輝度信号Yのみに変換する。変換式は、Y = 0.299R + 0.587G + 0.114B
// "YUVフォーマット及び YUV<->RGB変換"を参考にした。http://vision.kuee.kyoto-u.ac.jp/~hiroaki/firewire/yuv.html
// 2013/09/27 : float を止めて、すべてint にした
int32_t conv_rbg2y_soft(int32_t rbg){
int32_t r, g, b, y_f;
int32_t y;
b = (rbg>>10) & 0x3ff;
g = rbg & 0x3ff;
r = (rbg>>20) & 0x3ff;
y_f = 77*r + 150*g + 29*b; //y_f = 0.299*r + 0.587*g + 0.114*b;の係数に256倍した
y = y_f >> 8; // 256で割る
return(y);
}
// sobel filter
// HORZONTAL
// x0y0 x1y0 x2y0 1 2 1
// x0y1 x1y1 x2y1 0 0 0
// x0y2 x1y2 x2y2 -1 -2 -1
// VERTICAL
// x0y0 x1y0 x2y0 1 0 -1
// x0y1 x1y1 x2y1 2 0 -2
// x0y2 x1y2 x2y2 1 0 -1
int32_t sobel_fil_soft(int32_t h_or_v, int32_t x0y0, int32_t x1y0, int32_t x2y0, int32_t x0y1,
int32_t x1y1, int32_t x2y1, int32_t x0y2, int32_t x1y2, int32_t x2y2){
int32_t y;
if(h_or_v == HORIZONTAL){
y = x0y0 + 2*x1y0 + x2y0 - x0y2 - 2*x1y2 - x2y2;
} else {
y = x0y0 - x2y0 + 2*x0y1 - 2*x2y1 + x0y2 - x2y2;
}
if(y<0)
y = -y;
//y = 0;
else if(y>1023)
y = 1023;
return(y);
}
// square_root10_soft
// 10 bit幅のsquare_rootを求める
int32_t square_root10_soft(int32_t val){
int32_t temp = 0;
int32_t square;
for(int i=9; i>=0; --i){
temp += (1 << i);
square = temp * temp;
if(square > val){
temp -= (1 << i);
}
}
return(temp);
}
を設定した。-I/usr/local/include
を設定した。-L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_imgproc
でラプラシアン・フィルタ処理モードにしてある。XLap_filter_axis_Set_function_r(&xlf_axis_ap, 1);
で画像をスルー状態にしてみた。XLap_filter_axis_Set_function_r(&xlf_axis_ap, 0);
でラプラシアン・フィルタ処理モードにしたところ、どうもエッジが表示されているようだが、エッジがあまり良く見えなかった。XLap_filter_axis_Set_function_r(&xlf_axis_ap, 1);
で元画像も表示されている。XLap_filter_axis_Set_function_r(&xlf_axis_ap, 0);
int lap_filter_axis(hls::stream<ap_axis<32,1,1,1> >& ins, hls::stream<ap_axis<32,1,1,1> >& outs, int function){
#pragma HLS INTERFACE s_axilite port=function
#pragma HLS INTERFACE axis register both port=ins
#pragma HLS INTERFACE axis register both port=outs
#pragma HLS INTERFACE s_axilite port=return
int lap_filter_axis(hls::stream<ap_axis<32,1,1,1> >& ins, hls::stream<ap_axis<32,1,1,1> >& outs, int function){
#pragma HLS INTERFACE axis register_mode=both register port=outs
#pragma HLS INTERFACE axis register_mode=both register port=ins
#pragma HLS INTERFACE s_axilite port=function
#pragma HLS INTERFACE s_axilite port=return
// lap_filter_axis_RBG10.h
// 2021/01/31 by marsee
//
#ifndef __LAP_FILTER_AXIS_RBG10_H__
#define __LAP_FILTER_AXIS_RBG10_H__
#define HORIZONTAL_PIXEL_WIDTH 1920
#define VERTICAL_PIXEL_WIDTH 1080
//#define HORIZONTAL_PIXEL_WIDTH 800
//#define VERTICAL_PIXEL_WIDTH 600
//#define HORIZONTAL_PIXEL_WIDTH 64
//#define VERTICAL_PIXEL_WIDTH 48
#define ALL_PIXEL_VALUE (HORIZONTAL_PIXEL_WIDTH*VERTICAL_PIXEL_WIDTH)
#define ORIGINAL_IMAGE 0
#define LAPLACIAN_FILTER 1
#endif
// lap_filter_axis_RBG10.cpp
// 2021/01/31 by marsee
//
#include <stdio.h>
#include <string.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include "lap_filter_axis_RBG10.h"
int laplacian_fil(int x0y0, int x1y0, int x2y0, int x0y1, int x1y1, int x2y1, int x0y2, int x1y2, int x2y2);
int conv_rbg2y10(int rgb);
int lap_filter_axis(hls::stream<ap_axis<32,1,1,1> >& ins, hls::stream<ap_axis<32,1,1,1> >& outs, int function){
#pragma HLS INTERFACE s_axilite port=function
#pragma HLS INTERFACE axis register both port=ins
#pragma HLS INTERFACE axis register both port=outs
#pragma HLS INTERFACE s_axilite port=return
ap_axis<32,1,1,1> pix;
ap_axis<32,1,1,1> lap;
unsigned int line_buf[2][HORIZONTAL_PIXEL_WIDTH];
#pragma HLS array_partition variable=line_buf block factor=2 dim=1
#pragma HLS resource variable=line_buf core=RAM_2P
int pix_mat[3][3];
#pragma HLS array_partition variable=pix_mat complete
int lap_fil_val;
Loop1 : do { // user が 1になった時にフレームがスタートする
#pragma HLS LOOP_TRIPCOUNT min=1 max=1 avg=1
ins >> pix;
} while(pix.user == 0);
Loop2 : for (int y=0; y<VERTICAL_PIXEL_WIDTH; y++){
Loop3 : for (int x=0; x<HORIZONTAL_PIXEL_WIDTH; x++){
#pragma HLS PIPELINE II=1
if (!(x==0 && y==0)) // 最初の入力はすでに入力されている
ins >> pix; // AXI4-Stream からの入力
Loop4 : for (int k=0; k<3; k++){
Loop5 : for (int m=0; m<2; m++){
#pragma HLS UNROLL
pix_mat[k][m] = pix_mat[k][m+1];
}
}
pix_mat[0][2] = line_buf[0][x];
pix_mat[1][2] = line_buf[1][x];
int y_val = conv_rbg2y10(pix.data);
pix_mat[2][2] = y_val;
line_buf[0][x] = line_buf[1][x]; // 行の入れ替え
line_buf[1][x] = y_val;
lap_fil_val = laplacian_fil( pix_mat[0][0], pix_mat[0][1], pix_mat[0][2],
pix_mat[1][0], pix_mat[1][1], pix_mat[1][2],
pix_mat[2][0], pix_mat[2][1], pix_mat[2][2]);
lap.data = (lap_fil_val<<20)+(lap_fil_val<<10)+lap_fil_val; // RGB同じ値を入れる
if (x<2 || y<2) // 最初の2行とその他の行の最初の2列は無効データなので0とする
lap.data = 0;
if (x==0 && y==0) // 最初のデータでは、TUSERをアサートする
lap.user = 1;
else
lap.user = 0;
if (x == (HORIZONTAL_PIXEL_WIDTH-1)) // 行の最後で TLAST をアサートする
lap.last = 1;
else
lap.last = 0;
if(function == LAPLACIAN_FILTER)
outs << lap; // ラプラシアン・フィルタ処理画像をAXI4-Stream へ出力
else
outs << pix; // 元画像をAXI4-Stream へ出力
}
}
return 0;
}
// RBG 10 ビット幅からYへの変換
// RBGのフォーマットは、{2'd0, R(10 bits), B(10 bits), G(10 bits)}, 1pixel = 32bits
// 輝度信号Yのみに変換する。変換式は、Y = 0.299R + 0.587G + 0.114B
// "YUVフォーマット及び YUV<->RGB変換"を参考にした。http://vision.kuee.kyoto-u.ac.jp/~hiroaki/firewire/yuv.html
// 2013/09/27 : float を止めて、すべてint にした
int conv_rbg2y10(int rgb){
int r, g, b, y_f;
int y;
g = rgb & 0x3ff;
b = (rgb>>10) & 0x3ff;
r = (rgb>>20) & 0x3ff;
y_f = 77*r + 150*g + 29*b; //y_f = 0.299*r + 0.587*g + 0.114*b;の係数に256倍した
y = y_f >> 8; // 256で割る
return(y);
}
// ラプラシアンフィルタ
// x0y0 x1y0 x2y0 -1 -1 -1
// x0y1 x1y1 x2y1 -1 8 -1
// x0y2 x1y2 x2y2 -1 -1 -1
int laplacian_fil(int x0y0, int x1y0, int x2y0, int x0y1, int x1y1, int x2y1, int x0y2, int x1y2, int x2y2)
{
int y;
y = -x0y0 -x1y0 -x2y0 -x0y1 +8*x1y1 -x2y1 -x0y2 -x1y2 -x2y2;
if (y<0)
y = -y;
//y = 0;
else if (y>1023)
y = 1023;
return(y);
}
// lap_filter_axis_RBG10_tb.cpp
// 2021/01/31 by marsee
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <iostream>
#include <fstream>
#include <ap_axi_sdata.h>
#include "lap_filter_axis_RBG10.h"
#include "bmp_header.h"
int lap_filter_axis(hls::stream<ap_axis<32,1,1,1> >& ins, hls::stream<ap_axis<32,1,1,1> >& outs, int function);
int laplacian_fil_soft(int x0y0, int x1y0, int x2y0, int x0y1, int x1y1, int x2y1, int x0y2, int x1y2, int x2y2);
int conv_rbg2y10_soft(int rgb);
int lap_filter_axis_soft(hls::stream<ap_axis<32,1,1,1> >& ins, hls::stream<ap_axis<32,1,1,1> >& outs, int width, int height);
#define CLOCK_PERIOD 10
int main()
{
using namespace std;
hls::stream<ap_axis<32,1,1,1> > ins, ins2;
hls::stream<ap_axis<32,1,1,1> > ins_soft;
hls::stream<ap_axis<32,1,1,1> > outs;
hls::stream<ap_axis<32,1,1,1> > outs_soft;
ap_axis<32,1,1,1> pix;
ap_axis<32,1,1,1> vals;
ap_axis<32,1,1,1> vals_soft;
BITMAPFILEHEADER bmpfhr; // BMPファイルのファイルヘッダ(for Read)
BITMAPINFOHEADER bmpihr; // BMPファイルのINFOヘッダ(for Read)
FILE *fbmpr, *fbmpw;
int *rd_bmp, *hw_lapd;
int blue, green, red;
if ((fbmpr = fopen("test3.bmp", "rb")) == NULL){ // test.bmp をオープン
fprintf(stderr, "Can't open test.bmp by binary read mode\n");
exit(1);
}
// bmpヘッダの読み出し
fread(&bmpfhr.bfType, sizeof(uint16_t), 1, fbmpr);
fread(&bmpfhr.bfSize, sizeof(uint32_t), 1, fbmpr);
fread(&bmpfhr.bfReserved1, sizeof(uint16_t), 1, fbmpr);
fread(&bmpfhr.bfReserved2, sizeof(uint16_t), 1, fbmpr);
fread(&bmpfhr.bfOffBits, sizeof(uint32_t), 1, fbmpr);
fread(&bmpihr, sizeof(BITMAPINFOHEADER), 1, fbmpr);
// ピクセルを入れるメモリをアロケートする
if ((rd_bmp =(int *)malloc(sizeof(int) * (bmpihr.biWidth * bmpihr.biHeight))) == NULL){
fprintf(stderr, "Can't allocate rd_bmp memory\n");
exit(1);
}
if ((hw_lapd =(int *)malloc(sizeof(int) * (bmpihr.biWidth * bmpihr.biHeight))) == NULL){
fprintf(stderr, "Can't allocate hw_lapd memory\n");
exit(1);
}
// rd_bmp にBMPのピクセルを代入。その際に、行を逆転する必要がある
for (int y=0; y<bmpihr.biHeight; y++){
for (int x=0; x<bmpihr.biWidth; x++){
blue = (fgetc(fbmpr)) << 2;
green = (fgetc(fbmpr)) << 2;
red = (fgetc(fbmpr)) << 2;
rd_bmp[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] = (green & 0x3ff) | ((blue & 0x3ff)<<10) | ((red & 0x3ff)<<20);
}
}
fclose(fbmpr);
// ins に入力データを用意する
for(int i=0; i<5; i++){ // dummy data
pix.user = 0;
pix.data = i;
ins << pix;
ins2 << pix;
}
for(int j=0; j < bmpihr.biHeight; j++){
for(int i=0; i < bmpihr.biWidth; i++){
pix.data = (ap_int<32>)rd_bmp[(j*bmpihr.biWidth)+i];
if (j==0 && i==0) // 最初のデータの時に TUSER を 1 にする
pix.user = 1;
else
pix.user = 0;
if (i == bmpihr.biWidth-1) // 行の最後でTLASTをアサートする
pix.last = 1;
else
pix.last = 0;
ins << pix;
ins2 << pix;
ins_soft << pix;
}
}
lap_filter_axis(ins, outs, LAPLACIAN_FILTER);
lap_filter_axis_soft(ins_soft, outs_soft, bmpihr.biWidth, bmpihr.biHeight);
// ハードウェアとソフトウェアのラプラシアン・フィルタの値のチェック
//cout << endl;
//cout << "outs" << endl;
for(int j=0; j < bmpihr.biHeight; j++){
for(int i=0; i < bmpihr.biWidth; i++){
outs >> vals;
outs_soft >> vals_soft;
ap_int<32> val = vals.data;
ap_int<32> val_soft = vals_soft.data;
hw_lapd[(j*bmpihr.biWidth)+i] = (int)val;
if (val != val_soft){
printf("ERROR HW and SW results mismatch i = %ld, j = %ld, HW = %d, SW = %d\n", i, j, (int)val, (int)val_soft);
return(1);
}
//if (vals.last)
//cout << "AXI-Stream is end" << endl;
}
}
cout << "Success HW and SW results match" << endl;
cout << endl;
// ハードウェアのラプラシアンフィルタの結果を temp_lap.bmp へ出力する
if ((fbmpw=fopen("temp_lap.bmp", "wb")) == NULL){
fprintf(stderr, "Can't open temp_lap.bmp by binary write mode\n");
exit(1);
}
// BMPファイルヘッダの書き込み
fwrite(&bmpfhr.bfType, sizeof(uint16_t), 1, fbmpw);
fwrite(&bmpfhr.bfSize, sizeof(uint32_t), 1, fbmpw);
fwrite(&bmpfhr.bfReserved1, sizeof(uint16_t), 1, fbmpw);
fwrite(&bmpfhr.bfReserved2, sizeof(uint16_t), 1, fbmpw);
fwrite(&bmpfhr.bfOffBits, sizeof(uint32_t), 1, fbmpw);
fwrite(&bmpihr, sizeof(BITMAPINFOHEADER), 1, fbmpw);
// RGB データの書き込み、逆順にする
for (int y=0; y<bmpihr.biHeight; y++){
for (int x=0; x<bmpihr.biWidth; x++){
green = (hw_lapd[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] & 0x3ff)>>2;
blue = ((hw_lapd[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] >> 10) & 0x3ff)>>2;
red = ((hw_lapd[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] >> 20) & 0x3ff)>>2;
fputc(blue, fbmpw);
fputc(green, fbmpw);
fputc(red, fbmpw);
}
}
fclose(fbmpw);
// 元画像を出力
lap_filter_axis(ins2, outs, ORIGINAL_IMAGE);
for(int j=0; j < bmpihr.biHeight; j++){
for(int i=0; i < bmpihr.biWidth; i++){
outs >> vals;
ap_int<32> val = vals.data;
hw_lapd[(j*bmpihr.biWidth)+i] = (int)val;
}
}
// ハードウェアのラプラシアンフィルタの結果を temp_lap.bmp へ出力する
if ((fbmpw=fopen("temp_org.bmp", "wb")) == NULL){
fprintf(stderr, "Can't open temp_lap.bmp by binary write mode\n");
exit(1);
}
// BMPファイルヘッダの書き込み
fwrite(&bmpfhr.bfType, sizeof(uint16_t), 1, fbmpw);
fwrite(&bmpfhr.bfSize, sizeof(uint32_t), 1, fbmpw);
fwrite(&bmpfhr.bfReserved1, sizeof(uint16_t), 1, fbmpw);
fwrite(&bmpfhr.bfReserved2, sizeof(uint16_t), 1, fbmpw);
fwrite(&bmpfhr.bfOffBits, sizeof(uint32_t), 1, fbmpw);
fwrite(&bmpihr, sizeof(BITMAPINFOHEADER), 1, fbmpw);
// RGB データの書き込み、逆順にする
for (int y=0; y<bmpihr.biHeight; y++){
for (int x=0; x<bmpihr.biWidth; x++){
green = (hw_lapd[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] & 0x3ff)>>2;
blue = ((hw_lapd[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] >> 10) & 0x3ff)>>2;
red = ((hw_lapd[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] >> 20) & 0x3ff)>>2;
fputc(blue, fbmpw);
fputc(green, fbmpw);
fputc(red, fbmpw);
}
}
fclose(fbmpw);
free(rd_bmp);
free(hw_lapd);
return 0;
}
int lap_filter_axis_soft(hls::stream<ap_axis<32,1,1,1> >& ins, hls::stream<ap_axis<32,1,1,1> >& outs, int width, int height){
ap_axis<32,1,1,1> pix;
ap_axis<32,1,1,1> lap;
unsigned int **line_buf;
int pix_mat[3][3];
int lap_fil_val;
int i;
// line_buf の1次元目の配列をアロケートする
if ((line_buf =(unsigned int **)malloc(sizeof(unsigned int *) * 2)) == NULL){
fprintf(stderr, "Can't allocate line_buf[3][]\n");
exit(1);
}
// メモリをアロケートする
for (i=0; i<2; i++){
if ((line_buf[i]=(unsigned int *)malloc(sizeof(unsigned int) * width)) == NULL){
fprintf(stderr, "Can't allocate line_buf[%d]\n", i);
exit(1);
}
}
do { // user が 1になった時にフレームがスタートする
ins >> pix;
} while(pix.user == 0);
for (int y=0; y<height; y++){
for (int x=0; x<width; x++){
if (!(x==0 && y==0)) // 最初の入力はすでに入力されている
ins >> pix; // AXI4-Stream からの入力
for (int k=0; k<3; k++){
for (int m=0; m<2; m++){
pix_mat[k][m] = pix_mat[k][m+1];
}
}
pix_mat[0][2] = line_buf[0][x];
pix_mat[1][2] = line_buf[1][x];
int y_val = conv_rbg2y10_soft(pix.data);
pix_mat[2][2] = y_val;
line_buf[0][x] = line_buf[1][x]; // 行の入れ替え
line_buf[1][x] = y_val;
lap_fil_val = laplacian_fil_soft( pix_mat[0][0], pix_mat[0][1], pix_mat[0][2],
pix_mat[1][0], pix_mat[1][1], pix_mat[1][2],
pix_mat[2][0], pix_mat[2][1], pix_mat[2][2]);
lap.data = (lap_fil_val<<20)+(lap_fil_val<<10)+lap_fil_val; // RGB同じ値を入れる
if (x<2 || y<2) // 最初の2行とその他の行の最初の2列は無効データなので0とする
lap.data = 0;
if (x==0 && y==0) // 最初のデータでは、TUSERをアサートする
lap.user = 1;
else
lap.user = 0;
if (x == (HORIZONTAL_PIXEL_WIDTH-1)) // 行の最後で TLAST をアサートする
lap.last = 1;
else
lap.last = 0;
outs << lap; // AXI4-Stream へ出力
}
}
for (i=0; i<2; i++)
free(line_buf[i]);
free(line_buf);
return 0;
}
// RBG 10 ビット幅からYへの変換
// RBGのフォーマットは、{2'd0, R(10 bits), B(10 bits), G(10 bits)}, 1pixel = 32bits
// 輝度信号Yのみに変換する。変換式は、Y = 0.299R + 0.587G + 0.114B
// "YUVフォーマット及び YUV<->RGB変換"を参考にした。http://vision.kuee.kyoto-u.ac.jp/~hiroaki/firewire/yuv.html
// 2013/09/27 : float を止めて、すべてint にした
int conv_rbg2y10_soft(int rgb){
int r, g, b, y_f;
int y;
g = rgb & 0x3ff;
b = (rgb>>10) & 0x3ff;
r = (rgb>>20) & 0x3ff;
y_f = 77*r + 150*g + 29*b; //y_f = 0.299*r + 0.587*g + 0.114*b;の係数に256倍した
y = y_f >> 8; // 256で割る
return(y);
}
// ラプラシアンフィルタ
// x0y0 x1y0 x2y0 -1 -1 -1
// x0y1 x1y1 x2y1 -1 8 -1
// x0y2 x1y2 x2y2 -1 -1 -1
int laplacian_fil_soft(int x0y0, int x1y0, int x2y0, int x0y1, int x1y1, int x2y1, int x0y2, int x1y2, int x2y2)
{
int y;
y = -x0y0 -x1y0 -x2y0 -x0y1 +8*x1y1 -x2y1 -x0y2 -x1y2 -x2y2;
if (y<0)
y = -y;
//y = 0;
else if (y>1023)
y = 1023;
return(y);
}
// lap_filter_axis_RBG10.cpp
// 2021/01/31 by marsee
//
#include <stdio.h>
#include <string.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include "lap_filter_axis_RBG10.h"
int laplacian_fil(int x0y0, int x1y0, int x2y0, int x0y1, int x1y1, int x2y1, int x0y2, int x1y2, int x2y2);
int conv_rbg2y10(int rgb);
int lap_filter_axis(hls::stream<ap_axis<32,1,1,1> >& ins, hls::stream<ap_axis<32,1,1,1> >& outs){
#pragma HLS INTERFACE axis register both port=ins
#pragma HLS INTERFACE axis register both port=outs
#pragma HLS INTERFACE s_axilite port=return
ap_axis<32,1,1,1> pix;
ap_axis<32,1,1,1> lap;
unsigned int line_buf[2][HORIZONTAL_PIXEL_WIDTH];
#pragma HLS array_partition variable=line_buf block factor=2 dim=1
#pragma HLS resource variable=line_buf core=RAM_2P
int pix_mat[3][3];
#pragma HLS array_partition variable=pix_mat complete
int lap_fil_val;
Loop1 : do { // user が 1になった時にフレームがスタートする
#pragma HLS LOOP_TRIPCOUNT min=1 max=1 avg=1
ins >> pix;
} while(pix.user == 0);
Loop2 : for (int y=0; y<VERTICAL_PIXEL_WIDTH; y++){
Loop3 : for (int x=0; x<HORIZONTAL_PIXEL_WIDTH; x++){
#pragma HLS PIPELINE II=1
if (!(x==0 && y==0)) // 最初の入力はすでに入力されている
ins >> pix; // AXI4-Stream からの入力
Loop4 : for (int k=0; k<3; k++){
Loop5 : for (int m=0; m<2; m++){
#pragma HLS UNROLL
pix_mat[k][m] = pix_mat[k][m+1];
}
}
pix_mat[0][2] = line_buf[0][x];
pix_mat[1][2] = line_buf[1][x];
int y_val = conv_rbg2y10(pix.data);
pix_mat[2][2] = y_val;
line_buf[0][x] = line_buf[1][x]; // 行の入れ替え
line_buf[1][x] = y_val;
lap_fil_val = laplacian_fil( pix_mat[0][0], pix_mat[0][1], pix_mat[0][2],
pix_mat[1][0], pix_mat[1][1], pix_mat[1][2],
pix_mat[2][0], pix_mat[2][1], pix_mat[2][2]);
lap.data = (lap_fil_val<<20)+(lap_fil_val<<10)+lap_fil_val; // RGB同じ値を入れる
if (x<2 || y<2) // 最初の2行とその他の行の最初の2列は無効データなので0とする
lap.data = 0;
if (x==0 && y==0) // 最初のデータでは、TUSERをアサートする
lap.user = 1;
else
lap.user = 0;
if (x == (HORIZONTAL_PIXEL_WIDTH-1)) // 行の最後で TLAST をアサートする
lap.last = 1;
else
lap.last = 0;
outs << lap; // AXI4-Stream へ出力
}
}
return 0;
}
// RBG 10 ビット幅からYへの変換
// RBGのフォーマットは、{2'd0, R(10 bits), B(10 bits), G(10 bits)}, 1pixel = 32bits
// 輝度信号Yのみに変換する。変換式は、Y = 0.299R + 0.587G + 0.114B
// "YUVフォーマット及び YUV<->RGB変換"を参考にした。http://vision.kuee.kyoto-u.ac.jp/~hiroaki/firewire/yuv.html
// 2013/09/27 : float を止めて、すべてint にした
int conv_rbg2y10(int rgb){
int r, g, b, y_f;
int y;
g = rgb & 0x3ff;
b = (rgb>>10) & 0x3ff;
r = (rgb>>20) & 0x3ff;
y_f = 77*r + 150*g + 29*b; //y_f = 0.299*r + 0.587*g + 0.114*b;の係数に256倍した
y = y_f >> 8; // 256で割る
return(y);
}
// ラプラシアンフィルタ
// x0y0 x1y0 x2y0 -1 -1 -1
// x0y1 x1y1 x2y1 -1 8 -1
// x0y2 x1y2 x2y2 -1 -1 -1
int laplacian_fil(int x0y0, int x1y0, int x2y0, int x0y1, int x1y1, int x2y1, int x0y2, int x1y2, int x2y2)
{
int y;
y = -x0y0 -x1y0 -x2y0 -x0y1 +8*x1y1 -x2y1 -x0y2 -x1y2 -x2y2;
if (y<0)
y = -y;
//y = 0;
else if (y>1023)
y = 1023;
return(y);
}
// lap_filter_axis_RBG10.h
// 2021/01/31 by marsee
//
#ifndef __LAP_FILTER_AXIS_RBG10_H__
#define __LAP_FILTER_AXIS_RBG10_H__
//#define HORIZONTAL_PIXEL_WIDTH 800
//#define VERTICAL_PIXEL_WIDTH 600
#define HORIZONTAL_PIXEL_WIDTH 64
#define VERTICAL_PIXEL_WIDTH 48
#define ALL_PIXEL_VALUE (HORIZONTAL_PIXEL_WIDTH*VERTICAL_PIXEL_WIDTH)
#endif
// lap_filter_axis_RBG10_tb.cpp
// 2021/01/31 by marsee
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <iostream>
#include <fstream>
#include <ap_axi_sdata.h>
#include "lap_filter_axis_RBG10.h"
#include "bmp_header.h"
int lap_filter_axis(hls::stream<ap_axis<32,1,1,1> >& ins, hls::stream<ap_axis<32,1,1,1> >& outs);
int laplacian_fil_soft(int x0y0, int x1y0, int x2y0, int x0y1, int x1y1, int x2y1, int x0y2, int x1y2, int x2y2);
int conv_rbg2y10_soft(int rgb);
int lap_filter_axis_soft(hls::stream<ap_axis<32,1,1,1> >& ins, hls::stream<ap_axis<32,1,1,1> >& outs, int width, int height);
#define CLOCK_PERIOD 10
int main()
{
using namespace std;
hls::stream<ap_axis<32,1,1,1> > ins;
hls::stream<ap_axis<32,1,1,1> > ins_soft;
hls::stream<ap_axis<32,1,1,1> > outs;
hls::stream<ap_axis<32,1,1,1> > outs_soft;
ap_axis<32,1,1,1> pix;
ap_axis<32,1,1,1> vals;
ap_axis<32,1,1,1> vals_soft;
BITMAPFILEHEADER bmpfhr; // BMPファイルのファイルヘッダ(for Read)
BITMAPINFOHEADER bmpihr; // BMPファイルのINFOヘッダ(for Read)
FILE *fbmpr, *fbmpw;
int *rd_bmp, *hw_lapd;
int blue, green, red;
if ((fbmpr = fopen("test.bmp", "rb")) == NULL){ // test.bmp をオープン
fprintf(stderr, "Can't open test.bmp by binary read mode\n");
exit(1);
}
// bmpヘッダの読み出し
fread(&bmpfhr.bfType, sizeof(uint16_t), 1, fbmpr);
fread(&bmpfhr.bfSize, sizeof(uint32_t), 1, fbmpr);
fread(&bmpfhr.bfReserved1, sizeof(uint16_t), 1, fbmpr);
fread(&bmpfhr.bfReserved2, sizeof(uint16_t), 1, fbmpr);
fread(&bmpfhr.bfOffBits, sizeof(uint32_t), 1, fbmpr);
fread(&bmpihr, sizeof(BITMAPINFOHEADER), 1, fbmpr);
// ピクセルを入れるメモリをアロケートする
if ((rd_bmp =(int *)malloc(sizeof(int) * (bmpihr.biWidth * bmpihr.biHeight))) == NULL){
fprintf(stderr, "Can't allocate rd_bmp memory\n");
exit(1);
}
if ((hw_lapd =(int *)malloc(sizeof(int) * (bmpihr.biWidth * bmpihr.biHeight))) == NULL){
fprintf(stderr, "Can't allocate hw_lapd memory\n");
exit(1);
}
// rd_bmp にBMPのピクセルを代入。その際に、行を逆転する必要がある
for (int y=0; y<bmpihr.biHeight; y++){
for (int x=0; x<bmpihr.biWidth; x++){
blue = (fgetc(fbmpr)) << 2;
green = (fgetc(fbmpr)) << 2;
red = (fgetc(fbmpr)) << 2;
rd_bmp[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] = (green & 0x3ff) | ((blue & 0x3ff)<<10) | ((red & 0x3ff)<<20);
}
}
fclose(fbmpr);
// ins に入力データを用意する
for(int i=0; i<5; i++){ // dummy data
pix.user = 0;
pix.data = i;
ins << pix;
}
for(int j=0; j < bmpihr.biHeight; j++){
for(int i=0; i < bmpihr.biWidth; i++){
pix.data = (ap_int<32>)rd_bmp[(j*bmpihr.biWidth)+i];
if (j==0 && i==0) // 最初のデータの時に TUSER を 1 にする
pix.user = 1;
else
pix.user = 0;
if (i == bmpihr.biWidth-1) // 行の最後でTLASTをアサートする
pix.last = 1;
else
pix.last = 0;
ins << pix;
ins_soft << pix;
}
}
lap_filter_axis(ins, outs);
lap_filter_axis_soft(ins_soft, outs_soft, bmpihr.biWidth, bmpihr.biHeight);
// ハードウェアとソフトウェアのラプラシアン・フィルタの値のチェック
cout << endl;
cout << "outs" << endl;
for(int j=0; j < bmpihr.biHeight; j++){
for(int i=0; i < bmpihr.biWidth; i++){
outs >> vals;
outs_soft >> vals_soft;
ap_int<32> val = vals.data;
ap_int<32> val_soft = vals_soft.data;
hw_lapd[(j*bmpihr.biWidth)+i] = (int)val;
if (val != val_soft){
printf("ERROR HW and SW results mismatch i = %ld, j = %ld, HW = %d, SW = %d\n", i, j, (int)val, (int)val_soft);
return(1);
}
//if (vals.last)
//cout << "AXI-Stream is end" << endl;
}
}
cout << "Success HW and SW results match" << endl;
cout << endl;
// ハードウェアのラプラシアンフィルタの結果を temp_lap.bmp へ出力する
if ((fbmpw=fopen("temp_lap.bmp", "wb")) == NULL){
fprintf(stderr, "Can't open temp_lap.bmp by binary write mode\n");
exit(1);
}
// BMPファイルヘッダの書き込み
fwrite(&bmpfhr.bfType, sizeof(uint16_t), 1, fbmpw);
fwrite(&bmpfhr.bfSize, sizeof(uint32_t), 1, fbmpw);
fwrite(&bmpfhr.bfReserved1, sizeof(uint16_t), 1, fbmpw);
fwrite(&bmpfhr.bfReserved2, sizeof(uint16_t), 1, fbmpw);
fwrite(&bmpfhr.bfOffBits, sizeof(uint32_t), 1, fbmpw);
fwrite(&bmpihr, sizeof(BITMAPINFOHEADER), 1, fbmpw);
// RGB データの書き込み、逆順にする
for (int y=0; y<bmpihr.biHeight; y++){
for (int x=0; x<bmpihr.biWidth; x++){
green = (hw_lapd[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] & 0x3ff)>>2;
blue = ((hw_lapd[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] >> 10) & 0x3ff)>>2;
red = ((hw_lapd[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] >> 20) & 0x3ff)>>2;
fputc(blue, fbmpw);
fputc(green, fbmpw);
fputc(red, fbmpw);
}
}
fclose(fbmpw);
free(rd_bmp);
free(hw_lapd);
return 0;
}
int lap_filter_axis_soft(hls::stream<ap_axis<32,1,1,1> >& ins, hls::stream<ap_axis<32,1,1,1> >& outs, int width, int height){
ap_axis<32,1,1,1> pix;
ap_axis<32,1,1,1> lap;
unsigned int **line_buf;
int pix_mat[3][3];
int lap_fil_val;
int i;
// line_buf の1次元目の配列をアロケートする
if ((line_buf =(unsigned int **)malloc(sizeof(unsigned int *) * 2)) == NULL){
fprintf(stderr, "Can't allocate line_buf[3][]\n");
exit(1);
}
// メモリをアロケートする
for (i=0; i<2; i++){
if ((line_buf[i]=(unsigned int *)malloc(sizeof(unsigned int) * width)) == NULL){
fprintf(stderr, "Can't allocate line_buf[%d]\n", i);
exit(1);
}
}
do { // user が 1になった時にフレームがスタートする
ins >> pix;
} while(pix.user == 0);
for (int y=0; y<height; y++){
for (int x=0; x<width; x++){
if (!(x==0 && y==0)) // 最初の入力はすでに入力されている
ins >> pix; // AXI4-Stream からの入力
for (int k=0; k<3; k++){
for (int m=0; m<2; m++){
pix_mat[k][m] = pix_mat[k][m+1];
}
}
pix_mat[0][2] = line_buf[0][x];
pix_mat[1][2] = line_buf[1][x];
int y_val = conv_rbg2y10_soft(pix.data);
pix_mat[2][2] = y_val;
line_buf[0][x] = line_buf[1][x]; // 行の入れ替え
line_buf[1][x] = y_val;
lap_fil_val = laplacian_fil_soft( pix_mat[0][0], pix_mat[0][1], pix_mat[0][2],
pix_mat[1][0], pix_mat[1][1], pix_mat[1][2],
pix_mat[2][0], pix_mat[2][1], pix_mat[2][2]);
lap.data = (lap_fil_val<<20)+(lap_fil_val<<10)+lap_fil_val; // RGB同じ値を入れる
if (x<2 || y<2) // 最初の2行とその他の行の最初の2列は無効データなので0とする
lap.data = 0;
if (x==0 && y==0) // 最初のデータでは、TUSERをアサートする
lap.user = 1;
else
lap.user = 0;
if (x == (HORIZONTAL_PIXEL_WIDTH-1)) // 行の最後で TLAST をアサートする
lap.last = 1;
else
lap.last = 0;
outs << lap; // AXI4-Stream へ出力
}
}
for (i=0; i<2; i++)
free(line_buf[i]);
free(line_buf);
return 0;
}
// RBG 10 ビット幅からYへの変換
// RBGのフォーマットは、{2'd0, R(10 bits), B(10 bits), G(10 bits)}, 1pixel = 32bits
// 輝度信号Yのみに変換する。変換式は、Y = 0.299R + 0.587G + 0.114B
// "YUVフォーマット及び YUV<->RGB変換"を参考にした。http://vision.kuee.kyoto-u.ac.jp/~hiroaki/firewire/yuv.html
// 2013/09/27 : float を止めて、すべてint にした
int conv_rbg2y10_soft(int rgb){
int r, g, b, y_f;
int y;
g = rgb & 0x3ff;
b = (rgb>>10) & 0x3ff;
r = (rgb>>20) & 0x3ff;
y_f = 77*r + 150*g + 29*b; //y_f = 0.299*r + 0.587*g + 0.114*b;の係数に256倍した
y = y_f >> 8; // 256で割る
return(y);
}
// ラプラシアンフィルタ
// x0y0 x1y0 x2y0 -1 -1 -1
// x0y1 x1y1 x2y1 -1 8 -1
// x0y2 x1y2 x2y2 -1 -1 -1
int laplacian_fil_soft(int x0y0, int x1y0, int x2y0, int x0y1, int x1y1, int x2y1, int x0y2, int x1y2, int x2y2)
{
int y;
y = -x0y0 -x1y0 -x2y0 -x0y1 +8*x1y1 -x2y1 -x0y2 -x1y2 -x2y2;
if (y<0)
y = -y;
//y = 0;
else if (y>1023)
y = 1023;
return(y);
}
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
- | 1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | - | - | - | - | - | - |