【c言語】画像データをモニターする

verilogの検証時に、c言語でリファレンスモデルを作成する場合がある。 出力データが、verilogとリファレンスモデルで合わない時に、それぞれの内部の信号を確認する必要が出てくる。

その時に、いちいち内部信号用のprintf文を書いていると、時間がかかるし目視で確認するのは大変。 そこで、fprintfを使って、csvファイルにデータを書き込むmonitorを作成してみた。

//monitor.h

#ifndef __MONITOR_H__
#define __MONITOR_H__

#include "stdio.h"

typedef struct{
    FILE      *fp;
    int       x;
    int       y;
    int       pos_start_x;
    int       pos_end_x;
    int       pos_start_y;
    int       pos_end_y;
    long long data[5];
} S_TARGET_DATA;

extern void monitor_init (S_TARGET_DATA *p);
extern void monitor_write(S_TARGET_DATA *p);
extern void monitor_close(S_TARGET_DATA *p);
#endif
//monitor.c

#include "monitor.h"

void monitor_init(S_TARGET_DATA *p){
    printf("Hello init.\n");

    fprintf(p->fp,"Output image monitor\n");
    fprintf(p->fp,",x,y,data_upper2,data_upper1,data_center,data_down1,data_down2\n");
}

void monitor_write(S_TARGET_DATA *p){
    if (p->x >= p->pos_start_x && p->x <= p->pos_end_x && p->y >= p->pos_start_y && p->y <= p->pos_end_y){
        fprintf(p->fp,",%d,%d,",p->x,p->y);
        int data_kind = sizeof(p->data)/sizeof(p->data[0]);
        int i;
        for (i=0;i<data_kind;i++){
            fprintf(p->fp,"0x%llx,", p->data[i]);
        }
        fprintf(p->fp,"\n");
    }
}

void monitor_close(S_TARGET_DATA *p){
    fclose(p->fp);
}
//main.c 
#include "stdio.h"
#include "../monitor/monitor.h"

#define IMAGE_WIDTH  512
#define IMAGE_HEIGHT 512

void main(void) {
    FILE * fp_rgb1 = NULL;
    long long data_u2 = 0;
    long long data_u1 = 0;
    long long data_c  = 0;
    long long data_d1 = 0;
    long long data_d2 = 0;
    int x,y;

    //DEBUG::set parm for monitor 1//////////////////
    S_TARGET_DATA data1;
    data1.fp          = fopen("data_rgb1.csv","w");
    data1.pos_start_x = 0;
    data1.pos_start_y = 0;
    data1.pos_end_x   = 511;
    data1.pos_end_y   = 0;
    monitor_init(&data1);
    /////////////////////////////////////////////////
    //DEBUG::set parm for monitor 2//////////////////
    S_TARGET_DATA data2;
    data2.fp          = fopen("data_rgb2.csv","w");
    data2.pos_start_x = 0;
    data2.pos_start_y = 0;
    data2.pos_end_x   = 510;
    data2.pos_end_y   = 0;
    monitor_init(&data2);
    /////////////////////////////////////////////////

    for (y=0;y<IMAGE_HEIGHT;y++){
        for (x=0;x<IMAGE_WIDTH;x++){
            data_u2 += 1;   //データ1と過程
            data_u1 += 2;   //データ2と過程
            data_c  += 4;    //データ3と過程
            data_d1 += 8;   //データ4と過程
            data_d2 += 16; //データ5と過程

            //DEBUG::set data to monitor 1///
            data1.x       = x;
            data1.y       = y;
            data1.data[0] = data_u2;
            data1.data[1] = data_u1;
            data1.data[2] = data_c ;
            data1.data[3] = data_d1;
            data1.data[4] = data_d2;
            monitor_write(&data1);
            /////////////////////////////////
            //DEBUG::set data to monitor 2///
            data2.x       = x;
            data2.y       = y;
            data2.data[0] = data_u2;
            data2.data[1] = data_u1;
            data2.data[2] = data_c ;
            data2.data[3] = data_d1;
            data2.data[4] = data_d2;
            monitor_write(&data2);
            /////////////////////////////////
        }
    }
    //DEBUG::close monitor 1////
    monitor_close(&data1);
    ////////////////////////////
    //DEBUG::close monitor 2////
    monitor_close(&data2);
    ////////////////////////////
}