data/full_repos/permissive/432364608/sim/new/image_tb.v
1: b'%Warning-STMTDLY: data/full_repos/permissive/432364608/sim/new/image_tb.v:150: Unsupported: Ignoring delay on this delayed statement.\n        #30 reset = 0; \n        ^\n                  ... Use "/* verilator lint_off STMTDLY */" and lint_on around source to disable this message.\n%Warning-STMTDLY: data/full_repos/permissive/432364608/sim/new/image_tb.v:157: Unsupported: Ignoring delay on this delayed statement.\n        forever #5 clk = ~clk; \n                ^\n%Error: data/full_repos/permissive/432364608/sim/new/image_tb.v:66: Cannot find file containing module: \'edge_detect\'\n    edge_detect transform_1(\n    ^~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/432364608/sim/new,data/full_repos/permissive/432364608/edge_detect\n             data/full_repos/permissive/432364608/sim/new,data/full_repos/permissive/432364608/edge_detect.v\n             data/full_repos/permissive/432364608/sim/new,data/full_repos/permissive/432364608/edge_detect.sv\n             edge_detect\n             edge_detect.v\n             edge_detect.sv\n             obj_dir/edge_detect\n             obj_dir/edge_detect.v\n             obj_dir/edge_detect.sv\n%Error: data/full_repos/permissive/432364608/sim/new/image_tb.v:81: Cannot find file containing module: \'smooth\'\n    smooth transform_2(\n    ^~~~~~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:164: Operator ASSIGN expects 2 bits on the Assign RHS, but Assign RHS\'s LT generates 1 bits.\n                                                                           : ... In instance image_tb\n                top_check = read_pointer < (TOTAL_DATA - WIDTH*3);\n                          ^\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:165: Operator ASSIGN expects 2 bits on the Assign RHS, but Assign RHS\'s GT generates 1 bits.\n                                                                           : ... In instance image_tb\n                bot_check = read_pointer > WIDTH*3;\n                          ^\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:166: Operator ASSIGN expects 2 bits on the Assign RHS, but Assign RHS\'s MODDIV generates 32 or 21 bits.\n                                                                           : ... In instance image_tb\n                left_check = read_pointer %(WIDTH*3);\n                           ^\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:167: Operator ASSIGN expects 2 bits on the Assign RHS, but Assign RHS\'s MODDIV generates 32 or 21 bits.\n                                                                           : ... In instance image_tb\n                right_check = read_pointer % ((WIDTH*3) -1);\n                            ^\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:178: Logical Operator IF expects 1 bit on the If, but If\'s VARREF \'top_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(top_check)                   begin top_smooth       = memory_storage_smooth[read_pointer + (768*3)][7:0]; end else begin top_smooth       = RGB_Smooth [7:0]; end\n                ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:179: Logical Operator IF expects 1 bit on the If, but If\'s VARREF \'bot_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(bot_check)                   begin bot_smooth       = memory_storage_smooth[read_pointer - (768*3)][7:0]; end else begin bot_smooth       = RGB_Smooth [7:0]; end\n                ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:180: Logical Operator IF expects 1 bit on the If, but If\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(~left_check)                 begin left_smooth      = memory_storage_smooth[read_pointer - 3][7:0];       end else begin left_smooth      = RGB_Smooth [7:0]; end\n                ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:181: Logical Operator IF expects 1 bit on the If, but If\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(~right_check)                begin right_smooth     = memory_storage_smooth[read_pointer + 3][7:0];       end else begin top_smooth       = RGB_Smooth [7:0]; end\n                ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:182: Logical Operator LOGAND expects 1 bit on the LHS, but LHS\'s VARREF \'bot_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(bot_check && ~left_check)    begin bot_left_smooth  = memory_storage_smooth[read_pointer - (767*3)][7:0]; end else begin bot_left_smooth  = RGB_Smooth [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:182: Logical Operator LOGAND expects 1 bit on the RHS, but RHS\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(bot_check && ~left_check)    begin bot_left_smooth  = memory_storage_smooth[read_pointer - (767*3)][7:0]; end else begin bot_left_smooth  = RGB_Smooth [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:183: Logical Operator LOGAND expects 1 bit on the LHS, but LHS\'s VARREF \'top_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(top_check && ~left_check)    begin top_left_smooth  = memory_storage_smooth[read_pointer + (767*3)][7:0]; end else begin top_left_smooth  = RGB_Smooth [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:183: Logical Operator LOGAND expects 1 bit on the RHS, but RHS\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(top_check && ~left_check)    begin top_left_smooth  = memory_storage_smooth[read_pointer + (767*3)][7:0]; end else begin top_left_smooth  = RGB_Smooth [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:184: Logical Operator LOGAND expects 1 bit on the LHS, but LHS\'s VARREF \'bot_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(bot_check && ~right_check)   begin bot_right_smooth = memory_storage_smooth[read_pointer - (769*3)][7:0]; end else begin bot_right_smooth = RGB_Smooth [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:184: Logical Operator LOGAND expects 1 bit on the RHS, but RHS\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(bot_check && ~right_check)   begin bot_right_smooth = memory_storage_smooth[read_pointer - (769*3)][7:0]; end else begin bot_right_smooth = RGB_Smooth [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:185: Logical Operator LOGAND expects 1 bit on the LHS, but LHS\'s VARREF \'top_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(top_check && ~right_check)   begin top_right_smooth = memory_storage_smooth[read_pointer + (769*3)][7:0]; end else begin top_right_smooth = RGB_Smooth [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:185: Logical Operator LOGAND expects 1 bit on the RHS, but RHS\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(top_check && ~right_check)   begin top_right_smooth = memory_storage_smooth[read_pointer + (769*3)][7:0]; end else begin top_right_smooth = RGB_Smooth [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:188: Logical Operator IF expects 1 bit on the If, but If\'s VARREF \'top_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(top_check)                   begin top_edge_detect       = memory_storage_edge[read_pointer + (768*3)][7:0]; end else begin top_edge_detect       = RGB_Edge [7:0]; end\n                ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:189: Logical Operator IF expects 1 bit on the If, but If\'s VARREF \'bot_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(bot_check)                   begin bot_edge_detect       = memory_storage_edge[read_pointer - (768*3)][7:0]; end else begin bot_edge_detect       = RGB_Edge [7:0]; end\n                ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:190: Logical Operator IF expects 1 bit on the If, but If\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(~left_check)                 begin left_edge_detect      = memory_storage_edge[read_pointer - 3][7:0];       end else begin left_edge_detect      = RGB_Edge [7:0]; end\n                ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:191: Logical Operator IF expects 1 bit on the If, but If\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(~right_check)                begin right_edge_detect     = memory_storage_edge[read_pointer + 3][7:0];       end else begin top_edge_detect       = RGB_Edge [7:0]; end\n                ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:192: Logical Operator LOGAND expects 1 bit on the LHS, but LHS\'s VARREF \'bot_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(bot_check && ~left_check)    begin bot_left_edge_detect  = memory_storage_edge[read_pointer - (767*3)][7:0]; end else begin bot_left_edge_detect  = RGB_Edge [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:192: Logical Operator LOGAND expects 1 bit on the RHS, but RHS\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(bot_check && ~left_check)    begin bot_left_edge_detect  = memory_storage_edge[read_pointer - (767*3)][7:0]; end else begin bot_left_edge_detect  = RGB_Edge [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:193: Logical Operator LOGAND expects 1 bit on the LHS, but LHS\'s VARREF \'top_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(top_check && ~left_check)    begin top_left_edge_detect  = memory_storage_edge[read_pointer + (767*3)][7:0]; end else begin top_left_edge_detect  = RGB_Edge [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:193: Logical Operator LOGAND expects 1 bit on the RHS, but RHS\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(top_check && ~left_check)    begin top_left_edge_detect  = memory_storage_edge[read_pointer + (767*3)][7:0]; end else begin top_left_edge_detect  = RGB_Edge [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:194: Logical Operator LOGAND expects 1 bit on the LHS, but LHS\'s VARREF \'bot_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(bot_check && ~right_check)   begin bot_right_edge_detect = memory_storage_edge[read_pointer - (769*3)][7:0]; end else begin bot_right_edge_detect = RGB_Edge [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:194: Logical Operator LOGAND expects 1 bit on the RHS, but RHS\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(bot_check && ~right_check)   begin bot_right_edge_detect = memory_storage_edge[read_pointer - (769*3)][7:0]; end else begin bot_right_edge_detect = RGB_Edge [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:195: Logical Operator LOGAND expects 1 bit on the LHS, but LHS\'s VARREF \'top_check\' generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(top_check && ~right_check)   begin top_right_edge_detect = memory_storage_edge[read_pointer + (769*3)][7:0]; end else begin top_right_edge_detect = RGB_Edge [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:195: Logical Operator LOGAND expects 1 bit on the RHS, but RHS\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n                if(top_check && ~right_check)   begin top_right_edge_detect = memory_storage_edge[read_pointer + (769*3)][7:0]; end else begin top_right_edge_detect = RGB_Edge [7:0]; end\n                             ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:201: Logical Operator IF expects 1 bit on the If, but If\'s NOT generates 2 bits.\n                                                                           : ... In instance image_tb\n            end else if(~reset) begin\n                     ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:162: Logical Operator IF expects 1 bit on the If, but If\'s VARREF \'write\' generates 2 bits.\n                                                                           : ... In instance image_tb\n            if(write)begin\n            ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:274: Logical Operator IF expects 1 bit on the If, but If\'s VARREF \'read\' generates 2 bits.\n                                                                           : ... In instance image_tb\n            else if(read)begin\n                 ^~\n%Warning-WIDTH: data/full_repos/permissive/432364608/sim/new/image_tb.v:239: Logical Operator IF expects 1 bit on the If, but If\'s VARREF \'reset\' generates 2 bits.\n                                                                           : ... In instance image_tb\n        if(reset)begin\n        ^~\n%Error: Exiting due to 2 error(s), 34 warning(s)\n        ... See the manual and https://verilator.org for more assistance.\n'
module image_tb#(parameter
    WIDTH = 768,
    HEIGHT = 512,
    BMP_HEADER_NUM = 54,
    TOTAL_DATA = WIDTH*HEIGHT*3,
    OUTFILE_EDGE   = "C:\\Users\\Alfonso\\Desktop\\Verilog\\Final_Project\\Circle_Edge_Detected.bmp",
    INFILE_EDGE    = "C:\\Users\\Alfonso\\Desktop\\Verilog\\Final_Project\\Circle.hex",
    OUTFILE_SMOOTH = "C:\\Users\\Alfonso\\Desktop\\Verilog\\Final_Project\\Banana_Noise_Smoothed.bmp",
    INFILE_SMOOTH  = "C:\\Users\\Alfonso\\Desktop\\Verilog\\Final_Project\\Banana.hex"
    );

    reg [1:0] clk;
    reg [1:0] reset;
    reg [1:0] write;
    reg [1:0] read;
    reg [23:0] RGB_Edge;
    reg [23:0] RGB_Smooth;
    reg [7:0] top_edge_detect;
    reg [7:0] bot_edge_detect;
    reg [7:0] left_edge_detect;
    reg [7:0] right_edge_detect;
    reg [7:0] top_left_edge_detect;
    reg [7:0] top_right_edge_detect;
    reg [7:0] bot_left_edge_detect;
    reg [7:0] bot_right_edge_detect;
    reg [7:0] top_smooth;
    reg [7:0] bot_smooth;
    reg [7:0] left_smooth;
    reg [7:0] right_smooth;
    reg [7:0] top_left_smooth;
    reg [7:0] top_right_smooth;
    reg [7:0] bot_left_smooth;
    reg [7:0] bot_right_smooth;
    wire [23:0] RGB_Smoothened;
    wire [23:0] RGB_Edge_Detected;
    reg [7:0] memory_storage_smooth [TOTAL_DATA:0];
    reg [7:0] memory_storage_edge [TOTAL_DATA:0];
    reg [7:0] out_BMP_Edge_Detected [TOTAL_DATA:0];
    reg [7:0] out_BMP_Smooth [TOTAL_DATA:0];
    reg [7:0] BMP_header [BMP_HEADER_NUM:0];
    integer i;
    reg [20:0] read_pointer;
    reg [20:0] write_pointer;
    integer FILE_EDGE;
    integer FILE_SMOOTH;
    reg [1:0] bot_check;
    reg [1:0] top_check;
    reg [1:0] left_check;
    reg [1:0] right_check;
    
    
    edge_detect transform_1(
        .clk(clk),
        .reset(reset),
        .RGB_Edge(RGB_Edge),
        .top(top_edge_detect),
        .bot(bot_edge_detect),
        .left(left_edge_detect),
        .right(right_edge_detect),
        .top_left(top_left_edge_detect),
        .top_right(top_right_edge_detect),
        .bot_left(bot_left_edge_detect),
        .bot_right(bot_right_edge_detect),
        .RGB_Edge_Detected(RGB_Edge_Detected)
    );
    
    smooth transform_2(
            .clk(clk),
            .reset(reset),
            .RGB_Smooth(RGB_Smooth),
            .top(top_smooth),
            .bot(bot_smooth),
            .left(left_smooth),
            .right(right_smooth),
            .top_left(top_left_smooth),
            .top_right(top_right_smooth),
            .bot_left(bot_left_smooth),
            .bot_right(bot_right_smooth),
            .RGB_Smoothened(RGB_Smoothened)
        );
    

    initial  begin 
        BMP_header[ 0] = 66;    BMP_header[28] =24; 
        BMP_header[ 1] = 77;    BMP_header[29] = 0; 
        BMP_header[ 2] = 54;    BMP_header[30] = 0; 
        BMP_header[ 3] = 0;     BMP_header[31] = 0;
        BMP_header[ 4] = 18;    BMP_header[32] = 0;
        BMP_header[ 5] = 0;     BMP_header[33] = 0; 
        BMP_header[ 6] = 0;     BMP_header[34] = 0; 
        BMP_header[ 7] = 0;     BMP_header[35] = 0; 
        BMP_header[ 8] = 0;     BMP_header[36] = 0; 
        BMP_header[ 9] = 0;     BMP_header[37] = 0; 
        BMP_header[10] = 54;    BMP_header[38] = 0; 
        BMP_header[11] = 0;     BMP_header[39] = 0; 
        BMP_header[12] = 0;     BMP_header[40] = 0; 
        BMP_header[13] = 0;     BMP_header[41] = 0; 
        BMP_header[14] = 40;    BMP_header[42] = 0; 
        BMP_header[15] = 0;     BMP_header[43] = 0; 
        BMP_header[16] = 0;     BMP_header[44] = 0; 
        BMP_header[17] = 0;     BMP_header[45] = 0; 
        BMP_header[18] = 0;     BMP_header[46] = 0; 
        BMP_header[19] = 3;     BMP_header[47] = 0;
        BMP_header[20] = 0;     BMP_header[48] = 0;
        BMP_header[21] = 0;     BMP_header[49] = 0; 
        BMP_header[22] = 0;     BMP_header[50] = 0; 
        BMP_header[23] = 2;     BMP_header[51] = 0; 
        BMP_header[24] = 0;     BMP_header[52] = 0; 
        BMP_header[25] = 0;     BMP_header[53] = 0; 
        BMP_header[26] = 1;     BMP_header[27] = 0; 
    end
    

    initial begin
      $readmemh(INFILE_EDGE, memory_storage_edge);
      $readmemh(INFILE_SMOOTH, memory_storage_smooth);

      FILE_EDGE = $fopen(OUTFILE_EDGE, "wb+");
      FILE_SMOOTH = $fopen(OUTFILE_SMOOTH, "wb+");
      $display("Files Open.");
    end
    

    initial begin 
        reset = 1;
        i = 0;
        write = 0;
        RGB_Edge = 0;
        RGB_Smooth = 0;
        read_pointer  = 0;
        write_pointer = 0;
        bot_check = 0;
        top_check = 0;
        left_check = 0;
        right_check = 0;
        #30 reset = 0; 
    end
    

    initial begin 
        clk = 0;
        read = 1;
        forever #5 clk = ~clk; 
    end
    
    always @(posedge clk)begin
        if(read_pointer < TOTAL_DATA - 1)begin
            if(write)begin
            
                top_check = read_pointer < (TOTAL_DATA - WIDTH*3);
                bot_check = read_pointer > WIDTH*3;
                left_check = read_pointer %(WIDTH*3);
                right_check = read_pointer % ((WIDTH*3) -1);
                
                RGB_Smooth [7:0]     = memory_storage_smooth[read_pointer][7:0];
                RGB_Smooth [15:8]    = memory_storage_smooth[read_pointer + 1][7:0];
                RGB_Smooth [23:16]   = memory_storage_smooth[read_pointer + 2][7:0];
                
                RGB_Edge [7:0]     = memory_storage_edge[read_pointer][7:0];
                RGB_Edge [15:8]    = memory_storage_edge[read_pointer + 1][7:0];
                RGB_Edge [23:16]   = memory_storage_edge[read_pointer + 2][7:0];
                

                if(top_check)                   begin top_smooth       = memory_storage_smooth[read_pointer + (768*3)][7:0]; end else begin top_smooth       = RGB_Smooth [7:0]; end
                if(bot_check)                   begin bot_smooth       = memory_storage_smooth[read_pointer - (768*3)][7:0]; end else begin bot_smooth       = RGB_Smooth [7:0]; end
                if(~left_check)                 begin left_smooth      = memory_storage_smooth[read_pointer - 3][7:0];       end else begin left_smooth      = RGB_Smooth [7:0]; end
                if(~right_check)                begin right_smooth     = memory_storage_smooth[read_pointer + 3][7:0];       end else begin top_smooth       = RGB_Smooth [7:0]; end
                if(bot_check && ~left_check)    begin bot_left_smooth  = memory_storage_smooth[read_pointer - (767*3)][7:0]; end else begin bot_left_smooth  = RGB_Smooth [7:0]; end
                if(top_check && ~left_check)    begin top_left_smooth  = memory_storage_smooth[read_pointer + (767*3)][7:0]; end else begin top_left_smooth  = RGB_Smooth [7:0]; end
                if(bot_check && ~right_check)   begin bot_right_smooth = memory_storage_smooth[read_pointer - (769*3)][7:0]; end else begin bot_right_smooth = RGB_Smooth [7:0]; end
                if(top_check && ~right_check)   begin top_right_smooth = memory_storage_smooth[read_pointer + (769*3)][7:0]; end else begin top_right_smooth = RGB_Smooth [7:0]; end
               

                if(top_check)                   begin top_edge_detect       = memory_storage_edge[read_pointer + (768*3)][7:0]; end else begin top_edge_detect       = RGB_Edge [7:0]; end
                if(bot_check)                   begin bot_edge_detect       = memory_storage_edge[read_pointer - (768*3)][7:0]; end else begin bot_edge_detect       = RGB_Edge [7:0]; end
                if(~left_check)                 begin left_edge_detect      = memory_storage_edge[read_pointer - 3][7:0];       end else begin left_edge_detect      = RGB_Edge [7:0]; end
                if(~right_check)                begin right_edge_detect     = memory_storage_edge[read_pointer + 3][7:0];       end else begin top_edge_detect       = RGB_Edge [7:0]; end
                if(bot_check && ~left_check)    begin bot_left_edge_detect  = memory_storage_edge[read_pointer - (767*3)][7:0]; end else begin bot_left_edge_detect  = RGB_Edge [7:0]; end
                if(top_check && ~left_check)    begin top_left_edge_detect  = memory_storage_edge[read_pointer + (767*3)][7:0]; end else begin top_left_edge_detect  = RGB_Edge [7:0]; end
                if(bot_check && ~right_check)   begin bot_right_edge_detect = memory_storage_edge[read_pointer - (769*3)][7:0]; end else begin bot_right_edge_detect = RGB_Edge [7:0]; end
                if(top_check && ~right_check)   begin top_right_edge_detect = memory_storage_edge[read_pointer + (769*3)][7:0]; end else begin top_right_edge_detect = RGB_Edge [7:0]; end
                
                
                read_pointer  = read_pointer + 3;
                write_pointer = read_pointer + 3;
                
            end else if(~reset) begin
            

                RGB_Smooth [7:0]       = memory_storage_smooth[0][7:0];
                RGB_Smooth [15:8]      = memory_storage_smooth[0 + 1][7:0];
                RGB_Smooth [23:16]     = memory_storage_smooth[0 + 2][7:0];
                top_smooth             = memory_storage_smooth[0 + (768*3)][7:0];
                bot_smooth             = RGB_Smooth [7:0];
                left_smooth            = RGB_Smooth [7:0];;
                right_smooth           = memory_storage_smooth[0 + 3][7:0];
                bot_left_smooth        = RGB_Smooth [7:0];;
                top_left_smooth        = RGB_Smooth [7:0];;
                bot_right_smooth       = RGB_Smooth [7:0];;
                top_right_smooth       = memory_storage_smooth[0 + (769*3)][7:0];
                

                RGB_Edge [7:0]         = memory_storage_edge[0][7:0];
                RGB_Edge [15:8]        = memory_storage_edge[0 + 1][7:0];
                RGB_Edge [23:16]       = memory_storage_edge[0 + 2][7:0];
                top_edge_detect        = memory_storage_edge[0 + (768*3)][7:0];
                bot_edge_detect        = RGB_Edge [7:0];
                left_edge_detect       = RGB_Edge [7:0];;
                right_edge_detect      = memory_storage_edge[0 + 3][7:0];
                bot_left_edge_detect   = RGB_Edge [7:0];;
                top_left_edge_detect   = RGB_Edge [7:0];;
                bot_right_edge_detect  = RGB_Edge [7:0];;
                top_right_edge_detect  = memory_storage_edge[0 + (769*3)][7:0];
                
                write = 1;
            end
        end
    end
    



    always @(posedge clk)begin

        if(reset)begin
            for(i = 0; i < TOTAL_DATA; i = i + 1)begin
                out_BMP_Edge_Detected[i][7:0]= 8'b00000000;
                out_BMP_Smooth[i][7:0]= 8'b00000000;
            end
            read_pointer  = 0;
            write_pointer = 0;
            read = 1;
            write = 0;
            i = 0;
        end
        else begin
            

            if(write_pointer >= TOTAL_DATA - 1)begin
            
                $display("All values set, writing to output file.");
            
                for(i=0; i<54; i=i+1)begin
                    $fwrite(FILE_EDGE, "%c", BMP_header[i][7:0]);
                    $fwrite(FILE_SMOOTH, "%c", BMP_header[i][7:0]);
                end
                
                for(i=0; i<TOTAL_DATA; i=i+1) begin

                     $fwrite(FILE_EDGE, "%c", out_BMP_Edge_Detected[i][7:0]);
                     $fwrite(FILE_SMOOTH, "%c", out_BMP_Smooth[i][7:0]);
                end
                
                $display("Closing Files.");

                $fclose(FILE_EDGE);
                $fclose(FILE_SMOOTH);
                $display("Files Closed.");
            end
            else if(read)begin
                out_BMP_Edge_Detected[write_pointer][7:0]     = RGB_Edge_Detected[7:0];
                out_BMP_Edge_Detected[write_pointer + 1][7:0] = RGB_Edge_Detected[15:8];
                out_BMP_Edge_Detected[write_pointer + 2][7:0] = RGB_Edge_Detected[23:16];
                out_BMP_Smooth[write_pointer][7:0]     = RGB_Smoothened[7:0];
                out_BMP_Smooth[write_pointer + 1][7:0] = RGB_Smoothened[15:8];
                out_BMP_Smooth[write_pointer + 2][7:0] = RGB_Smoothened[23:16];
                read = 1;
            end
            else begin

            end
        end
    end
    
endmodule
module image_tb#(parameter
    WIDTH = 768,
    HEIGHT = 512,
    BMP_HEADER_NUM = 54,
    TOTAL_DATA = WIDTH*HEIGHT*3,
    OUTFILE_EDGE   = "C:\\Users\\Alfonso\\Desktop\\Verilog\\Final_Project\\Circle_Edge_Detected.bmp",
    INFILE_EDGE    = "C:\\Users\\Alfonso\\Desktop\\Verilog\\Final_Project\\Circle.hex",
    OUTFILE_SMOOTH = "C:\\Users\\Alfonso\\Desktop\\Verilog\\Final_Project\\Banana_Noise_Smoothed.bmp",
    INFILE_SMOOTH  = "C:\\Users\\Alfonso\\Desktop\\Verilog\\Final_Project\\Banana.hex"
    );


    reg [1:0] clk;
    reg [1:0] reset;
    reg [1:0] write;
    reg [1:0] read;
    reg [23:0] RGB_Edge;
    reg [23:0] RGB_Smooth;
    reg [7:0] top_edge_detect;
    reg [7:0] bot_edge_detect;
    reg [7:0] left_edge_detect;
    reg [7:0] right_edge_detect;
    reg [7:0] top_left_edge_detect;
    reg [7:0] top_right_edge_detect;
    reg [7:0] bot_left_edge_detect;
    reg [7:0] bot_right_edge_detect;
    reg [7:0] top_smooth;
    reg [7:0] bot_smooth;
    reg [7:0] left_smooth;
    reg [7:0] right_smooth;
    reg [7:0] top_left_smooth;
    reg [7:0] top_right_smooth;
    reg [7:0] bot_left_smooth;
    reg [7:0] bot_right_smooth;
    wire [23:0] RGB_Smoothened;
    wire [23:0] RGB_Edge_Detected;
    reg [7:0] memory_storage_smooth [TOTAL_DATA:0];
    reg [7:0] memory_storage_edge [TOTAL_DATA:0];
    reg [7:0] out_BMP_Edge_Detected [TOTAL_DATA:0];
    reg [7:0] out_BMP_Smooth [TOTAL_DATA:0];
    reg [7:0] BMP_header [BMP_HEADER_NUM:0];
    integer i;
    reg [20:0] read_pointer;
    reg [20:0] write_pointer;
    integer FILE_EDGE;
    integer FILE_SMOOTH;
    reg [1:0] bot_check;
    reg [1:0] top_check;
    reg [1:0] left_check;
    reg [1:0] right_check;
    
    
    edge_detect transform_1(
        .clk(clk),
        .reset(reset),
        .RGB_Edge(RGB_Edge),
        .top(top_edge_detect),
        .bot(bot_edge_detect),
        .left(left_edge_detect),
        .right(right_edge_detect),
        .top_left(top_left_edge_detect),
        .top_right(top_right_edge_detect),
        .bot_left(bot_left_edge_detect),
        .bot_right(bot_right_edge_detect),
        .RGB_Edge_Detected(RGB_Edge_Detected)
    );
    
    smooth transform_2(
            .clk(clk),
            .reset(reset),
            .RGB_Smooth(RGB_Smooth),
            .top(top_smooth),
            .bot(bot_smooth),
            .left(left_smooth),
            .right(right_smooth),
            .top_left(top_left_smooth),
            .top_right(top_right_smooth),
            .bot_left(bot_left_smooth),
            .bot_right(bot_right_smooth),
            .RGB_Smoothened(RGB_Smoothened)
        );
    

    initial  begin 
        BMP_header[ 0] = 66;    BMP_header[28] =24; 
        BMP_header[ 1] = 77;    BMP_header[29] = 0; 
        BMP_header[ 2] = 54;    BMP_header[30] = 0; 
        BMP_header[ 3] = 0;     BMP_header[31] = 0;
        BMP_header[ 4] = 18;    BMP_header[32] = 0;
        BMP_header[ 5] = 0;     BMP_header[33] = 0; 
        BMP_header[ 6] = 0;     BMP_header[34] = 0; 
        BMP_header[ 7] = 0;     BMP_header[35] = 0; 
        BMP_header[ 8] = 0;     BMP_header[36] = 0; 
        BMP_header[ 9] = 0;     BMP_header[37] = 0; 
        BMP_header[10] = 54;    BMP_header[38] = 0; 
        BMP_header[11] = 0;     BMP_header[39] = 0; 
        BMP_header[12] = 0;     BMP_header[40] = 0; 
        BMP_header[13] = 0;     BMP_header[41] = 0; 
        BMP_header[14] = 40;    BMP_header[42] = 0; 
        BMP_header[15] = 0;     BMP_header[43] = 0; 
        BMP_header[16] = 0;     BMP_header[44] = 0; 
        BMP_header[17] = 0;     BMP_header[45] = 0; 
        BMP_header[18] = 0;     BMP_header[46] = 0; 
        BMP_header[19] = 3;     BMP_header[47] = 0;
        BMP_header[20] = 0;     BMP_header[48] = 0;
        BMP_header[21] = 0;     BMP_header[49] = 0; 
        BMP_header[22] = 0;     BMP_header[50] = 0; 
        BMP_header[23] = 2;     BMP_header[51] = 0; 
        BMP_header[24] = 0;     BMP_header[52] = 0; 
        BMP_header[25] = 0;     BMP_header[53] = 0; 
        BMP_header[26] = 1;     BMP_header[27] = 0; 
    end
    

    initial begin
      $readmemh(INFILE_EDGE, memory_storage_edge);
      $readmemh(INFILE_SMOOTH, memory_storage_smooth);

      FILE_EDGE = $fopen(OUTFILE_EDGE, "wb+");
      FILE_SMOOTH = $fopen(OUTFILE_SMOOTH, "wb+");
      $display("Files Open.");
    end
    

    initial begin 
        reset = 1;
        i = 0;
        write = 0;
        RGB_Edge = 0;
        RGB_Smooth = 0;
        read_pointer  = 0;
        write_pointer = 0;
        bot_check = 0;
        top_check = 0;
        left_check = 0;
        right_check = 0;
        #30 reset = 0; 
    end
    

    initial begin 
        clk = 0;
        read = 1;
        forever #5 clk = ~clk; 
    end
    
    always @(posedge clk)begin
        if(read_pointer < TOTAL_DATA - 1)begin
            if(write)begin
            
                top_check = read_pointer < (TOTAL_DATA - WIDTH*3);
                bot_check = read_pointer > WIDTH*3;
                left_check = read_pointer %(WIDTH*3);
                right_check = read_pointer % ((WIDTH*3) -1);
                
                RGB_Smooth [7:0]     = memory_storage_smooth[read_pointer][7:0];
                RGB_Smooth [15:8]    = memory_storage_smooth[read_pointer + 1][7:0];
                RGB_Smooth [23:16]   = memory_storage_smooth[read_pointer + 2][7:0];
                
                RGB_Edge [7:0]     = memory_storage_edge[read_pointer][7:0];
                RGB_Edge [15:8]    = memory_storage_edge[read_pointer + 1][7:0];
                RGB_Edge [23:16]   = memory_storage_edge[read_pointer + 2][7:0];
                

                if(top_check)                   begin top_smooth       = memory_storage_smooth[read_pointer + (768*3)][7:0]; end else begin top_smooth       = RGB_Smooth [7:0]; end
                if(bot_check)                   begin bot_smooth       = memory_storage_smooth[read_pointer - (768*3)][7:0]; end else begin bot_smooth       = RGB_Smooth [7:0]; end
                if(~left_check)                 begin left_smooth      = memory_storage_smooth[read_pointer - 3][7:0];       end else begin left_smooth      = RGB_Smooth [7:0]; end
                if(~right_check)                begin right_smooth     = memory_storage_smooth[read_pointer + 3][7:0];       end else begin top_smooth       = RGB_Smooth [7:0]; end
                if(bot_check && ~left_check)    begin bot_left_smooth  = memory_storage_smooth[read_pointer - (767*3)][7:0]; end else begin bot_left_smooth  = RGB_Smooth [7:0]; end
                if(top_check && ~left_check)    begin top_left_smooth  = memory_storage_smooth[read_pointer + (767*3)][7:0]; end else begin top_left_smooth  = RGB_Smooth [7:0]; end
                if(bot_check && ~right_check)   begin bot_right_smooth = memory_storage_smooth[read_pointer - (769*3)][7:0]; end else begin bot_right_smooth = RGB_Smooth [7:0]; end
                if(top_check && ~right_check)   begin top_right_smooth = memory_storage_smooth[read_pointer + (769*3)][7:0]; end else begin top_right_smooth = RGB_Smooth [7:0]; end
               

                if(top_check)                   begin top_edge_detect       = memory_storage_edge[read_pointer + (768*3)][7:0]; end else begin top_edge_detect       = RGB_Edge [7:0]; end
                if(bot_check)                   begin bot_edge_detect       = memory_storage_edge[read_pointer - (768*3)][7:0]; end else begin bot_edge_detect       = RGB_Edge [7:0]; end
                if(~left_check)                 begin left_edge_detect      = memory_storage_edge[read_pointer - 3][7:0];       end else begin left_edge_detect      = RGB_Edge [7:0]; end
                if(~right_check)                begin right_edge_detect     = memory_storage_edge[read_pointer + 3][7:0];       end else begin top_edge_detect       = RGB_Edge [7:0]; end
                if(bot_check && ~left_check)    begin bot_left_edge_detect  = memory_storage_edge[read_pointer - (767*3)][7:0]; end else begin bot_left_edge_detect  = RGB_Edge [7:0]; end
                if(top_check && ~left_check)    begin top_left_edge_detect  = memory_storage_edge[read_pointer + (767*3)][7:0]; end else begin top_left_edge_detect  = RGB_Edge [7:0]; end
                if(bot_check && ~right_check)   begin bot_right_edge_detect = memory_storage_edge[read_pointer - (769*3)][7:0]; end else begin bot_right_edge_detect = RGB_Edge [7:0]; end
                if(top_check && ~right_check)   begin top_right_edge_detect = memory_storage_edge[read_pointer + (769*3)][7:0]; end else begin top_right_edge_detect = RGB_Edge [7:0]; end
                
                
                read_pointer  = read_pointer + 3;
                write_pointer = read_pointer + 3;
                
            end else if(~reset) begin
            

                RGB_Smooth [7:0]       = memory_storage_smooth[0][7:0];
                RGB_Smooth [15:8]      = memory_storage_smooth[0 + 1][7:0];
                RGB_Smooth [23:16]     = memory_storage_smooth[0 + 2][7:0];
                top_smooth             = memory_storage_smooth[0 + (768*3)][7:0];
                bot_smooth             = RGB_Smooth [7:0];
                left_smooth            = RGB_Smooth [7:0];;
                right_smooth           = memory_storage_smooth[0 + 3][7:0];
                bot_left_smooth        = RGB_Smooth [7:0];;
                top_left_smooth        = RGB_Smooth [7:0];;
                bot_right_smooth       = RGB_Smooth [7:0];;
                top_right_smooth       = memory_storage_smooth[0 + (769*3)][7:0];
                

                RGB_Edge [7:0]         = memory_storage_edge[0][7:0];
                RGB_Edge [15:8]        = memory_storage_edge[0 + 1][7:0];
                RGB_Edge [23:16]       = memory_storage_edge[0 + 2][7:0];
                top_edge_detect        = memory_storage_edge[0 + (768*3)][7:0];
                bot_edge_detect        = RGB_Edge [7:0];
                left_edge_detect       = RGB_Edge [7:0];;
                right_edge_detect      = memory_storage_edge[0 + 3][7:0];
                bot_left_edge_detect   = RGB_Edge [7:0];;
                top_left_edge_detect   = RGB_Edge [7:0];;
                bot_right_edge_detect  = RGB_Edge [7:0];;
                top_right_edge_detect  = memory_storage_edge[0 + (769*3)][7:0];
                
                write = 1;
            end
        end
    end
    



    always @(posedge clk)begin

        if(reset)begin
            for(i = 0; i < TOTAL_DATA; i = i + 1)begin
                out_BMP_Edge_Detected[i][7:0]= 8'b00000000;
                out_BMP_Smooth[i][7:0]= 8'b00000000;
            end
            read_pointer  = 0;
            write_pointer = 0;
            read = 1;
            write = 0;
            i = 0;
        end
        else begin
            

            if(write_pointer >= TOTAL_DATA - 1)begin
            
                $display("All values set, writing to output file.");
            
                for(i=0; i<54; i=i+1)begin
                    $fwrite(FILE_EDGE, "%c", BMP_header[i][7:0]);
                    $fwrite(FILE_SMOOTH, "%c", BMP_header[i][7:0]);
                end
                
                for(i=0; i<TOTAL_DATA; i=i+1) begin

                     $fwrite(FILE_EDGE, "%c", out_BMP_Edge_Detected[i][7:0]);
                     $fwrite(FILE_SMOOTH, "%c", out_BMP_Smooth[i][7:0]);
                end
                
                $display("Closing Files.");

                $fclose(FILE_EDGE);
                $fclose(FILE_SMOOTH);
                $display("Files Closed.");
            end
            else if(read)begin
                out_BMP_Edge_Detected[write_pointer][7:0]     = RGB_Edge_Detected[7:0];
                out_BMP_Edge_Detected[write_pointer + 1][7:0] = RGB_Edge_Detected[15:8];
                out_BMP_Edge_Detected[write_pointer + 2][7:0] = RGB_Edge_Detected[23:16];
                out_BMP_Smooth[write_pointer][7:0]     = RGB_Smoothened[7:0];
                out_BMP_Smooth[write_pointer + 1][7:0] = RGB_Smoothened[15:8];
                out_BMP_Smooth[write_pointer + 2][7:0] = RGB_Smoothened[23:16];
                read = 1;
            end
            else begin

            end
        end
    end
    
endmodule