// // // TopBench.v // // Vanilla video test bench // // Allen Tanner // // // outputs : CLK - 10 mHz 50% duty cycle clock // nRST - low to reset // inputs : hSync - horizontal sync. line ends on rising edge. // hBright - horizontal video gate. VidOut valid in that part of the each line with hBright == 1. // vSync - vertical sync. frame ends on rising edge. // vBright - vertical video gate. VidOut valid on lines with vBright == 1. // VidOut - Video Output (from Top Module). Valid when ((vBright == 1) && (hBright == 1)) integer full_lines; // =1 for full lines, =0 to delete every other (redundant) lines integer fille; integer pixelcnt, linecnt; integer framecnt; integer j,i; initial begin CLK = 1'b0; nRst = 1'b0; fille = $fopen("booah.txt"); full_lines = 0; // output only every other line everything at first $fwrite(fille,"Screen Capture\n"); $display("Resetting..."); nRst = 1'b1; #100; nRst = 1'b0; #100; nRst = 1'b1; #100; #75; // offsets sample timing measure_frame; // measures H&V timing get_frame; // writes the frame to booah.txt $display("End of Test\n."); $fwrite(fille,"End of Test.\n"); $fclose(fille); $finish; end // 10 mHz clock //'timescale 1ns/100ps always begin #50 CLK = ~CLK; end // // get_frame - reports frame into file handle fille // task get_frame; integer line_toggle, line_num; begin line_toggle = ~0; // this toggles to skip alternated lines when full_lines == 0 line_num = 0; framecnt = framecnt +1; // used for multiple frames (to make a movie) if(full_lines)$fwrite(fille,"Full line output\n"); else $fwrite(fille,"Alternate (redundant) lines skipped.\n"); while (vBright == 0) begin #100; end // wait for vertical active while (vBright == 1) // record while vertical active begin while (hBright == 0) begin #100; end //wait for horizontal if(vBright == 1) begin while (hBright == 1) // record while horizontal active if (full_lines || line_toggle) begin if((VidOut == 1'b1)) $fwrite(fille,"*"); else if((VidOut === 1'bx))$fwrite(fille,"x"); else $fwrite(fille," "); #100; end else while (hBright == 1) // this could be cleaner begin #100; end if (full_lines || line_toggle) $fwrite(fille," Line:%d\n",line_num); // at the end of the horzontal line line_toggle = ~line_toggle; // used to skip redundant lines line_num = line_num + 1; end // if(vBright == 1) end // while (vBright == 1) end endtask // get_frame // // measures horzontal and vertical timing of a frame // task measure_frame; real vS_beg, vSP_end, vB_beg, vB_end, vS_end; real hS_beg, hSP_end, hB_beg, hB_end, hS_end; begin $display("Measuring Horizontal"); while(~hSync) #100; hS_beg = $time / 1000.0; // nanosec to micro sec conversion while(hSync) #100; #100; hSP_end = $time / 1000.0; // sync pulse while(~hBright) #100; hB_beg = $time / 1000.0; while(hBright) #100; #100; hB_end = $time / 1000.0; while(~hSync) #100; #100; hS_end = $time / 1000.0; // entire horizontal line time $display("Measuring Vertical"); while(~vSync) #100; vS_beg = $time / 1000000.0; // nanosec to milli sec conversion while(vSync) #100; #100; vSP_end = $time / 1000000.0; // vertical sync pulse duration while(~vBright) #100; vB_beg = $time / 1000000.0; while(vBright) #100; #100; vB_end = $time / 1000000.0; while(~vSync) #100; #100; vS_end = $time / 1000000.0; // entire frame time // Count pixels and lines $display("Counting pixels and lines\nWait for vBright.... "); while (vBright == 0) begin #100; end // wait for vertical active $display("Saw vBright "); linecnt = 0; while (vBright == 1) // measure while vertical active begin while (hBright == 0) begin #100; end //wait for horizontal if(linecnt == 0)pixelcnt = 0; if(vBright == 1) begin while (hBright == 1) begin #100; if(linecnt == 0) pixelcnt = pixelcnt + 1; end linecnt = linecnt + 1; end if ((linecnt%50)==0)$display("line: ",linecnt); end $display("Frame has %d lines of %d pixels.",linecnt,pixelcnt); $fwrite(fille,"Frame has %d lines of %d pixels.\n",linecnt,pixelcnt); $display("Horizontal timing"); $fwrite(fille,"Horizontal timing\n"); $display("H Sync period : %f microsec.",hS_end - hS_beg); $fwrite(fille,"H Sync period : %f microsec.\n",hS_end - hS_beg); $display("H Sync pulse width : %f microsec.",hSP_end - hS_beg); $fwrite(fille,"H Sync pulse width : %f microsec.\n",hSP_end - hS_beg); $display("H Sync to H Bright : %f microsec.",hB_beg - hS_beg); $fwrite(fille,"H Sync to H Bright : %f microsec.\n",hB_beg - hS_beg); $display("H Bright pulse width : %f microsec.",hB_end - hB_beg); $fwrite(fille,"H Bright pulse width : %f microsec.\n",hB_end - hB_beg); $display("Vertical timing"); $fwrite(fille,"Vertical timing\n"); $display("V Sync period : %f millisec.",vS_end - vS_beg); $fwrite(fille,"V Sync period : %f millisec.\n",vS_end - vS_beg); $display("V Sync pulse width : %f millisec.",vSP_end - vS_beg); $fwrite(fille,"V Sync pulse width : %f millisec.\n",vSP_end - vS_beg); $display("V Sync to V Bright : %f millisec.",vB_beg - vS_beg); $fwrite(fille,"V Sync to V Bright : %f millisec.\n",vB_beg - vS_beg); $display("V Bright pulse width : %f millisec.",vB_end - vB_beg); $fwrite(fille,"V Bright pulse width : %f millisec.\n",vB_end - vB_beg); end endtask // measure_frame